diff --git a/conformance/chaos/actor.go b/conformance/chaos/actor.go index cdda1db83ef..5760b0f3353 100644 --- a/conformance/chaos/actor.go +++ b/conformance/chaos/actor.go @@ -64,6 +64,8 @@ const ( MethodDeleteActor // MethodSend is the identifier for the method that sends a message to another actor. MethodSend + // MethodCreateState is the identifier for the method that creates the chaos actor's state. + MethodCreateState // MethodMutateState is the identifier for the method that attempts to mutate // a state value in the actor. MethodMutateState @@ -84,6 +86,7 @@ func (a Actor) Exports() []interface{} { MethodResolveAddress: a.ResolveAddress, MethodDeleteActor: a.DeleteActor, MethodSend: a.Send, + MethodCreateState: a.CreateState, MethodMutateState: a.MutateState, MethodAbortWith: a.AbortWith, MethodInspectRuntime: a.InspectRuntime, @@ -227,6 +230,14 @@ type MutateStateArgs struct { 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. func (a Actor) MutateState(rt runtime2.Runtime, args *MutateStateArgs) *abi.EmptyValue { rt.ValidateImmediateCallerAcceptAny() diff --git a/conformance/chaos/actor_test.go b/conformance/chaos/actor_test.go index dbce4f4c552..e68b9a4df76 100644 --- a/conformance/chaos/actor_test.go +++ b/conformance/chaos/actor_test.go @@ -129,8 +129,9 @@ func TestMutateStateInTransaction(t *testing.T) { var a Actor rt.ExpectValidateCallerAny() - rt.StateCreate(&State{}) + rt.Call(a.CreateState, nil) + rt.ExpectValidateCallerAny() val := "__mutstat test" rt.Call(a.MutateState, &MutateStateArgs{ Value: val, @@ -155,23 +156,30 @@ func TestMutateStateAfterTransaction(t *testing.T) { var a Actor rt.ExpectValidateCallerAny() - rt.StateCreate(&State{}) + rt.Call(a.CreateState, nil) + rt.ExpectValidateCallerAny() val := "__mutstat test" + defer func() { + if r := recover(); r == nil { + t.Fatal("The code did not panic") + } else { + var st State + rt.GetState(&st) + + // state should be updated successfully _in_ the transaction but not outside + if st.Value != val+"-in" { + t.Fatal("state was not updated") + } + + rt.Verify() + } + }() rt.Call(a.MutateState, &MutateStateArgs{ Value: val, Branch: MutateAfterTransaction, }) - var st State - rt.GetState(&st) - - // state should be updated successfully _in_ the transaction but not outside - if st.Value != val+"-in" { - t.Fatal("state was not updated") - } - - rt.Verify() } func TestMutateStateReadonly(t *testing.T) { @@ -182,22 +190,30 @@ func TestMutateStateReadonly(t *testing.T) { var a Actor rt.ExpectValidateCallerAny() - rt.StateCreate(&State{}) + rt.Call(a.CreateState, nil) + rt.ExpectValidateCallerAny() val := "__mutstat test" + defer func() { + if r := recover(); r == nil { + t.Fatal("The code did not panic") + } else { + var st State + rt.GetState(&st) + + if st.Value != "" { + t.Fatal("state was not expected to be updated") + } + + rt.Verify() + } + }() + rt.Call(a.MutateState, &MutateStateArgs{ Value: val, Branch: MutateReadonly, }) - var st State - rt.GetState(&st) - - if st.Value != "" { - t.Fatal("state was not expected to be updated") - } - - rt.Verify() } func TestMutateStateInvalidBranch(t *testing.T) { @@ -254,11 +270,13 @@ func TestInspectRuntime(t *testing.T) { receiver := atesting2.NewIDAddr(t, 101) builder := mock2.NewBuilder(context.Background(), receiver) - rt := builder.Build(t) - rt.SetCaller(caller, builtin2.AccountActorCodeID) - rt.StateCreate(&State{}) var a Actor + rt := builder.Build(t) + rt.ExpectValidateCallerAny() + rt.Call(a.CreateState, nil) + + rt.SetCaller(caller, builtin2.AccountActorCodeID) rt.ExpectValidateCallerAny() ret := rt.Call(a.InspectRuntime, abi.Empty) rtr, ok := ret.(*InspectRuntimeReturn)