From f3cae55bd44e069e1cab7ad9c5770cbf8b47b8aa Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 9 Sep 2020 11:40:30 +0100 Subject: [PATCH 1/3] feat: chaos abort --- conformance/chaos/actor.go | 22 +++++++ conformance/chaos/cbor_gen.go | 116 ++++++++++++++++++++++++++++++++++ conformance/chaos/gen/gen.go | 1 + 3 files changed, 139 insertions(+) diff --git a/conformance/chaos/actor.go b/conformance/chaos/actor.go index ed7a230c724..f162b2c8f1e 100644 --- a/conformance/chaos/actor.go +++ b/conformance/chaos/actor.go @@ -62,6 +62,9 @@ const ( // MethodMutateState is the identifier for the method that attempts to mutate // a state value in the actor. MethodMutateState + // MethodAbort is the identifier for the method that panics optionally with + // a passed exit code. + MethodAbort ) // Exports defines the methods this actor exposes publicly. @@ -74,6 +77,7 @@ func (a Actor) Exports() []interface{} { MethodDeleteActor: a.DeleteActor, MethodSend: a.Send, MethodMutateState: a.MutateState, + MethodAbort: a.Abort, } } @@ -230,3 +234,21 @@ func (a Actor) MutateState(rt runtime.Runtime, args *MutateStateArgs) *adt.Empty } return nil } + +// AbortArgs are the arguments to the abort method, specifying the exit code to +// (optionally) abort with and the message. +type AbortArgs struct { + Code exitcode.ExitCode + NoCode bool + Message string +} + +// Abort simply causes a panic or abort with the passed exit code. +func (a Actor) Abort(rt runtime.Runtime, args *AbortArgs) *adt.EmptyValue { + if args.NoCode { // no code, just plain old panic + panic(args.Message) + } else { + rt.Abortf(args.Code, args.Message) + } + return nil +} diff --git a/conformance/chaos/cbor_gen.go b/conformance/chaos/cbor_gen.go index 9f01ccce7d6..00825c8f3a1 100644 --- a/conformance/chaos/cbor_gen.go +++ b/conformance/chaos/cbor_gen.go @@ -614,3 +614,119 @@ func (t *MutateStateArgs) UnmarshalCBOR(r io.Reader) error { } return nil } + +var lengthBufAbortArgs = []byte{131} + +func (t *AbortArgs) MarshalCBOR(w io.Writer) error { + if t == nil { + _, err := w.Write(cbg.CborNull) + return err + } + if _, err := w.Write(lengthBufAbortArgs); err != nil { + return err + } + + scratch := make([]byte, 9) + + // t.Code (exitcode.ExitCode) (int64) + if t.Code >= 0 { + if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Code)); err != nil { + return err + } + } else { + if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.Code-1)); err != nil { + return err + } + } + + // t.NoCode (bool) (bool) + if err := cbg.WriteBool(w, t.NoCode); err != nil { + return err + } + + // t.Message (string) (string) + if len(t.Message) > cbg.MaxLength { + return xerrors.Errorf("Value in field t.Message was too long") + } + + if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Message))); err != nil { + return err + } + if _, err := io.WriteString(w, string(t.Message)); err != nil { + return err + } + return nil +} + +func (t *AbortArgs) UnmarshalCBOR(r io.Reader) error { + *t = AbortArgs{} + + br := cbg.GetPeeker(r) + scratch := make([]byte, 8) + + maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + if err != nil { + return err + } + if maj != cbg.MajArray { + return fmt.Errorf("cbor input should be of type array") + } + + if extra != 3 { + return fmt.Errorf("cbor input had wrong number of fields") + } + + // t.Code (exitcode.ExitCode) (int64) + { + maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + var extraI int64 + if err != nil { + return err + } + switch maj { + case cbg.MajUnsignedInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 positive overflow") + } + case cbg.MajNegativeInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 negative oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } + + t.Code = exitcode.ExitCode(extraI) + } + // t.NoCode (bool) (bool) + + maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + if err != nil { + return err + } + if maj != cbg.MajOther { + return fmt.Errorf("booleans must be major type 7") + } + switch extra { + case 20: + t.NoCode = false + case 21: + t.NoCode = true + default: + return fmt.Errorf("booleans are either major type 7, value 20 or 21 (got %d)", extra) + } + // t.Message (string) (string) + + { + sval, err := cbg.ReadStringBuf(br, scratch) + if err != nil { + return err + } + + t.Message = string(sval) + } + return nil +} diff --git a/conformance/chaos/gen/gen.go b/conformance/chaos/gen/gen.go index 308fed11e9b..dbf8463d458 100644 --- a/conformance/chaos/gen/gen.go +++ b/conformance/chaos/gen/gen.go @@ -14,6 +14,7 @@ func main() { chaos.SendArgs{}, chaos.SendReturn{}, chaos.MutateStateArgs{}, + chaos.AbortArgs{}, ); err != nil { panic(err) } From b755ce9528e4060d400e61648fb26f5049197dcd Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 10 Sep 2020 12:03:13 +0100 Subject: [PATCH 2/3] refactor: renames --- conformance/chaos/actor.go | 20 +++++++-------- conformance/chaos/cbor_gen.go | 46 +++++++++++++++++------------------ conformance/chaos/gen/gen.go | 2 +- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/conformance/chaos/actor.go b/conformance/chaos/actor.go index f162b2c8f1e..3792a4bfe99 100644 --- a/conformance/chaos/actor.go +++ b/conformance/chaos/actor.go @@ -77,7 +77,7 @@ func (a Actor) Exports() []interface{} { MethodDeleteActor: a.DeleteActor, MethodSend: a.Send, MethodMutateState: a.MutateState, - MethodAbort: a.Abort, + MethodAbort: a.AbortWith, } } @@ -235,17 +235,17 @@ func (a Actor) MutateState(rt runtime.Runtime, args *MutateStateArgs) *adt.Empty return nil } -// AbortArgs are the arguments to the abort method, specifying the exit code to -// (optionally) abort with and the message. -type AbortArgs struct { - Code exitcode.ExitCode - NoCode bool - Message string +// AbortWithArgs are the arguments to the Actor.AbortWith method, specifying the +// exit code to (optionally) abort with and the message. +type AbortWithArgs struct { + Code exitcode.ExitCode + Message string + Uncontrolled bool } -// Abort simply causes a panic or abort with the passed exit code. -func (a Actor) Abort(rt runtime.Runtime, args *AbortArgs) *adt.EmptyValue { - if args.NoCode { // no code, just plain old panic +// AbortWith simply causes a panic with the passed exit code. +func (a Actor) AbortWith(rt runtime.Runtime, args *AbortWithArgs) *adt.EmptyValue { + if args.Uncontrolled { // uncontrolled abort: directly panic panic(args.Message) } else { rt.Abortf(args.Code, args.Message) diff --git a/conformance/chaos/cbor_gen.go b/conformance/chaos/cbor_gen.go index 00825c8f3a1..710b84b9306 100644 --- a/conformance/chaos/cbor_gen.go +++ b/conformance/chaos/cbor_gen.go @@ -615,14 +615,14 @@ func (t *MutateStateArgs) UnmarshalCBOR(r io.Reader) error { return nil } -var lengthBufAbortArgs = []byte{131} +var lengthBufAbortWithArgs = []byte{131} -func (t *AbortArgs) MarshalCBOR(w io.Writer) error { +func (t *AbortWithArgs) MarshalCBOR(w io.Writer) error { if t == nil { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufAbortArgs); err != nil { + if _, err := w.Write(lengthBufAbortWithArgs); err != nil { return err } @@ -639,11 +639,6 @@ func (t *AbortArgs) MarshalCBOR(w io.Writer) error { } } - // t.NoCode (bool) (bool) - if err := cbg.WriteBool(w, t.NoCode); err != nil { - return err - } - // t.Message (string) (string) if len(t.Message) > cbg.MaxLength { return xerrors.Errorf("Value in field t.Message was too long") @@ -655,11 +650,16 @@ func (t *AbortArgs) MarshalCBOR(w io.Writer) error { if _, err := io.WriteString(w, string(t.Message)); err != nil { return err } + + // t.Uncontrolled (bool) (bool) + if err := cbg.WriteBool(w, t.Uncontrolled); err != nil { + return err + } return nil } -func (t *AbortArgs) UnmarshalCBOR(r io.Reader) error { - *t = AbortArgs{} +func (t *AbortWithArgs) UnmarshalCBOR(r io.Reader) error { + *t = AbortWithArgs{} br := cbg.GetPeeker(r) scratch := make([]byte, 8) @@ -701,7 +701,17 @@ func (t *AbortArgs) UnmarshalCBOR(r io.Reader) error { t.Code = exitcode.ExitCode(extraI) } - // t.NoCode (bool) (bool) + // t.Message (string) (string) + + { + sval, err := cbg.ReadStringBuf(br, scratch) + if err != nil { + return err + } + + t.Message = string(sval) + } + // t.Uncontrolled (bool) (bool) maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) if err != nil { @@ -712,21 +722,11 @@ func (t *AbortArgs) UnmarshalCBOR(r io.Reader) error { } switch extra { case 20: - t.NoCode = false + t.Uncontrolled = false case 21: - t.NoCode = true + t.Uncontrolled = true default: return fmt.Errorf("booleans are either major type 7, value 20 or 21 (got %d)", extra) } - // t.Message (string) (string) - - { - sval, err := cbg.ReadStringBuf(br, scratch) - if err != nil { - return err - } - - t.Message = string(sval) - } return nil } diff --git a/conformance/chaos/gen/gen.go b/conformance/chaos/gen/gen.go index dbf8463d458..97ea98dc8bb 100644 --- a/conformance/chaos/gen/gen.go +++ b/conformance/chaos/gen/gen.go @@ -14,7 +14,7 @@ func main() { chaos.SendArgs{}, chaos.SendReturn{}, chaos.MutateStateArgs{}, - chaos.AbortArgs{}, + chaos.AbortWithArgs{}, ); err != nil { panic(err) } From 0e7f14159ae04b9a1a6892297613b5c514a6b191 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 10 Sep 2020 12:06:00 +0100 Subject: [PATCH 3/3] refactor: another rename --- conformance/chaos/actor.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/conformance/chaos/actor.go b/conformance/chaos/actor.go index 3792a4bfe99..f3da42bb616 100644 --- a/conformance/chaos/actor.go +++ b/conformance/chaos/actor.go @@ -62,9 +62,9 @@ const ( // MethodMutateState is the identifier for the method that attempts to mutate // a state value in the actor. MethodMutateState - // MethodAbort is the identifier for the method that panics optionally with + // MethodAbortWith is the identifier for the method that panics optionally with // a passed exit code. - MethodAbort + MethodAbortWith ) // Exports defines the methods this actor exposes publicly. @@ -77,7 +77,7 @@ func (a Actor) Exports() []interface{} { MethodDeleteActor: a.DeleteActor, MethodSend: a.Send, MethodMutateState: a.MutateState, - MethodAbort: a.AbortWith, + MethodAbortWith: a.AbortWith, } }