Fix chaos

This commit is contained in:
Aayush Rajasekaran 2020-11-17 17:17:08 -05:00
parent 7ce4e5342b
commit 4982de89b2
2 changed files with 52 additions and 23 deletions

View File

@ -73,6 +73,8 @@ const (
// MethodInspectRuntime is the identifier for the method that returns the // MethodInspectRuntime is the identifier for the method that returns the
// current runtime values. // current runtime values.
MethodInspectRuntime MethodInspectRuntime
// MethodCreateState is the identifier for the method that creates the chaos actor's state.
MethodCreateState
) )
// Exports defines the methods this actor exposes publicly. // Exports defines the methods this actor exposes publicly.
@ -87,6 +89,7 @@ func (a Actor) Exports() []interface{} {
MethodMutateState: a.MutateState, MethodMutateState: a.MutateState,
MethodAbortWith: a.AbortWith, MethodAbortWith: a.AbortWith,
MethodInspectRuntime: a.InspectRuntime, MethodInspectRuntime: a.InspectRuntime,
MethodCreateState: a.CreateState,
} }
} }
@ -227,6 +230,14 @@ type MutateStateArgs struct {
Branch MutateStateBranch Branch MutateStateBranch
} }
// CreateState creates the chaos actor's state
func (a Actor) CreateState(rt runtime2.Runtime, _ *abi.EmptyValue) *abi.EmptyValue {
rt.ValidateImmediateCallerAcceptAny()
rt.StateCreate(&State{})
return nil
}
// MutateState attempts to mutate a state value in the actor. // MutateState attempts to mutate a state value in the actor.
func (a Actor) MutateState(rt runtime2.Runtime, args *MutateStateArgs) *abi.EmptyValue { func (a Actor) MutateState(rt runtime2.Runtime, args *MutateStateArgs) *abi.EmptyValue {
rt.ValidateImmediateCallerAcceptAny() rt.ValidateImmediateCallerAcceptAny()

View File

@ -129,8 +129,9 @@ func TestMutateStateInTransaction(t *testing.T) {
var a Actor var a Actor
rt.ExpectValidateCallerAny() rt.ExpectValidateCallerAny()
rt.StateCreate(&State{}) rt.Call(a.CreateState, nil)
rt.ExpectValidateCallerAny()
val := "__mutstat test" val := "__mutstat test"
rt.Call(a.MutateState, &MutateStateArgs{ rt.Call(a.MutateState, &MutateStateArgs{
Value: val, Value: val,
@ -155,14 +156,14 @@ func TestMutateStateAfterTransaction(t *testing.T) {
var a Actor var a Actor
rt.ExpectValidateCallerAny() rt.ExpectValidateCallerAny()
rt.StateCreate(&State{}) rt.Call(a.CreateState, nil)
rt.ExpectValidateCallerAny()
val := "__mutstat test" val := "__mutstat test"
rt.Call(a.MutateState, &MutateStateArgs{ defer func() {
Value: val, if r := recover(); r == nil {
Branch: MutateAfterTransaction, t.Fatal("The code did not panic")
}) } else {
var st State var st State
rt.GetState(&st) rt.GetState(&st)
@ -172,6 +173,13 @@ func TestMutateStateAfterTransaction(t *testing.T) {
} }
rt.Verify() rt.Verify()
}
}()
rt.Call(a.MutateState, &MutateStateArgs{
Value: val,
Branch: MutateAfterTransaction,
})
} }
func TestMutateStateReadonly(t *testing.T) { func TestMutateStateReadonly(t *testing.T) {
@ -182,14 +190,14 @@ func TestMutateStateReadonly(t *testing.T) {
var a Actor var a Actor
rt.ExpectValidateCallerAny() rt.ExpectValidateCallerAny()
rt.StateCreate(&State{}) rt.Call(a.CreateState, nil)
rt.ExpectValidateCallerAny()
val := "__mutstat test" val := "__mutstat test"
rt.Call(a.MutateState, &MutateStateArgs{ defer func() {
Value: val, if r := recover(); r == nil {
Branch: MutateReadonly, t.Fatal("The code did not panic")
}) } else {
var st State var st State
rt.GetState(&st) rt.GetState(&st)
@ -198,6 +206,14 @@ func TestMutateStateReadonly(t *testing.T) {
} }
rt.Verify() rt.Verify()
}
}()
rt.Call(a.MutateState, &MutateStateArgs{
Value: val,
Branch: MutateReadonly,
})
} }
func TestMutateStateInvalidBranch(t *testing.T) { func TestMutateStateInvalidBranch(t *testing.T) {
@ -254,11 +270,13 @@ func TestInspectRuntime(t *testing.T) {
receiver := atesting2.NewIDAddr(t, 101) receiver := atesting2.NewIDAddr(t, 101)
builder := mock2.NewBuilder(context.Background(), receiver) builder := mock2.NewBuilder(context.Background(), receiver)
rt := builder.Build(t)
rt.SetCaller(caller, builtin2.AccountActorCodeID)
rt.StateCreate(&State{})
var a Actor var a Actor
rt := builder.Build(t)
rt.ExpectValidateCallerAny()
rt.Call(a.CreateState, nil)
rt.SetCaller(caller, builtin2.AccountActorCodeID)
rt.ExpectValidateCallerAny() rt.ExpectValidateCallerAny()
ret := rt.Call(a.InspectRuntime, abi.Empty) ret := rt.Call(a.InspectRuntime, abi.Empty)
rtr, ok := ret.(*InspectRuntimeReturn) rtr, ok := ret.(*InspectRuntimeReturn)