From 51c56a8c7393f267b9e2f6fa9279eeb52f3c7971 Mon Sep 17 00:00:00 2001 From: Aayush Rajasekaran Date: Mon, 2 Jan 2023 17:34:46 -0500 Subject: [PATCH 1/2] Add new methods and types for FEVM --- abi/cbor_bytes.go | 51 +++++++++++ builtin/methods.go | 6 ++ builtin/v10/eam/methods.go | 12 +++ builtin/v10/embryo/methods.go | 9 ++ builtin/v10/ethaccount/methods.go | 10 +++ builtin/v10/evm/cbor_gen.go | 88 +++++++++++++++++++ .../evm/{evm_actor_state.go => evm_state.go} | 0 .../evm/{evm_actor_params.go => evm_types.go} | 7 ++ builtin/v10/evm/methods.go | 17 ++++ builtin/v10/gen/gen.go | 1 + 10 files changed, 201 insertions(+) create mode 100644 abi/cbor_bytes.go create mode 100644 builtin/v10/eam/methods.go create mode 100644 builtin/v10/embryo/methods.go create mode 100644 builtin/v10/ethaccount/methods.go rename builtin/v10/evm/{evm_actor_state.go => evm_state.go} (100%) rename builtin/v10/evm/{evm_actor_params.go => evm_types.go} (58%) create mode 100644 builtin/v10/evm/methods.go diff --git a/abi/cbor_bytes.go b/abi/cbor_bytes.go new file mode 100644 index 00000000..f3acf177 --- /dev/null +++ b/abi/cbor_bytes.go @@ -0,0 +1,51 @@ +package abi + +import ( + "fmt" + "io" + + cbg "github.com/whyrusleeping/cbor-gen" + "golang.org/x/xerrors" +) + +type CborBytes []byte + +func (t *CborBytes) MarshalCBOR(w io.Writer) error { + if len(*t) > cbg.ByteArrayMaxLen { + return xerrors.Errorf("byte array was too long") + } + + if err := cbg.WriteMajorTypeHeader(w, cbg.MajByteString, uint64(len(*t))); err != nil { + return err + } + + _, err := w.Write((*t)[:]) + return err +} + +func (t *CborBytes) UnmarshalCBOR(r io.Reader) error { + + br := cbg.GetPeeker(r) + maj, extra, err := cbg.CborReadHeader(br) + if err != nil { + return err + } + + if extra > cbg.ByteArrayMaxLen { + return fmt.Errorf("byte array too large (%d)", extra) + } + if maj != cbg.MajByteString { + return fmt.Errorf("expected byte array") + } + + if extra > 0 { + ret := make([]byte, extra) + if _, err := io.ReadFull(br, ret[:]); err != nil { + return err + } + + *t = ret + } + + return nil +} diff --git a/builtin/methods.go b/builtin/methods.go index d47ed0d9..2a99c478 100644 --- a/builtin/methods.go +++ b/builtin/methods.go @@ -386,3 +386,9 @@ var MethodsEAM = struct { 2, 3, } + +var MethodsEthAccount = struct { + Constructor abi.MethodNum +}{ + MethodConstructor, +} diff --git a/builtin/v10/eam/methods.go b/builtin/v10/eam/methods.go new file mode 100644 index 00000000..c1b3b5d7 --- /dev/null +++ b/builtin/v10/eam/methods.go @@ -0,0 +1,12 @@ +package eam + +import ( + "github.com/filecoin-project/go-state-types/abi" + "github.com/filecoin-project/go-state-types/builtin" +) + +var Methods = map[abi.MethodNum]builtin.MethodMeta{ + 1: {"Constructor", *new(func(*abi.EmptyValue) *abi.EmptyValue)}, // Constructor + 2: {"Create", *new(func(*CreateParams) *CreateReturn)}, // Create + 3: {"Create2", *new(func(*Create2Params) *Create2Return)}, // Create2 +} diff --git a/builtin/v10/embryo/methods.go b/builtin/v10/embryo/methods.go new file mode 100644 index 00000000..e4adb20a --- /dev/null +++ b/builtin/v10/embryo/methods.go @@ -0,0 +1,9 @@ +package embryo + +import ( + "github.com/filecoin-project/go-state-types/abi" + "github.com/filecoin-project/go-state-types/builtin" +) + +// Placeholder has no methods, not even a constructor +var Methods = map[abi.MethodNum]builtin.MethodMeta{} diff --git a/builtin/v10/ethaccount/methods.go b/builtin/v10/ethaccount/methods.go new file mode 100644 index 00000000..f6d15d17 --- /dev/null +++ b/builtin/v10/ethaccount/methods.go @@ -0,0 +1,10 @@ +package ethaccount + +import ( + "github.com/filecoin-project/go-state-types/abi" + "github.com/filecoin-project/go-state-types/builtin" +) + +var Methods = map[abi.MethodNum]builtin.MethodMeta{ + 1: {"Constructor", *new(func(value *abi.EmptyValue) *abi.EmptyValue)}, +} diff --git a/builtin/v10/evm/cbor_gen.go b/builtin/v10/evm/cbor_gen.go index 03c3c048..c06fc5b3 100644 --- a/builtin/v10/evm/cbor_gen.go +++ b/builtin/v10/evm/cbor_gen.go @@ -282,3 +282,91 @@ func (t *GetStorageAtParams) UnmarshalCBOR(r io.Reader) error { } return nil } + +var lengthBufDelegateCallParams = []byte{130} + +func (t *DelegateCallParams) MarshalCBOR(w io.Writer) error { + if t == nil { + _, err := w.Write(cbg.CborNull) + return err + } + if _, err := w.Write(lengthBufDelegateCallParams); err != nil { + return err + } + + scratch := make([]byte, 9) + + // t.Code (cid.Cid) (struct) + + if err := cbg.WriteCidBuf(scratch, w, t.Code); err != nil { + return xerrors.Errorf("failed to write cid field t.Code: %w", err) + } + + // t.Input ([]uint8) (slice) + if len(t.Input) > cbg.ByteArrayMaxLen { + return xerrors.Errorf("Byte array in field t.Input was too long") + } + + if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.Input))); err != nil { + return err + } + + if _, err := w.Write(t.Input[:]); err != nil { + return err + } + return nil +} + +func (t *DelegateCallParams) UnmarshalCBOR(r io.Reader) error { + *t = DelegateCallParams{} + + 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 != 2 { + return fmt.Errorf("cbor input had wrong number of fields") + } + + // t.Code (cid.Cid) (struct) + + { + + c, err := cbg.ReadCid(br) + if err != nil { + return xerrors.Errorf("failed to read cid field t.Code: %w", err) + } + + t.Code = c + + } + // t.Input ([]uint8) (slice) + + maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + if err != nil { + return err + } + + if extra > cbg.ByteArrayMaxLen { + return fmt.Errorf("t.Input: byte array too large (%d)", extra) + } + if maj != cbg.MajByteString { + return fmt.Errorf("expected byte array") + } + + if extra > 0 { + t.Input = make([]uint8, extra) + } + + if _, err := io.ReadFull(br, t.Input[:]); err != nil { + return err + } + return nil +} diff --git a/builtin/v10/evm/evm_actor_state.go b/builtin/v10/evm/evm_state.go similarity index 100% rename from builtin/v10/evm/evm_actor_state.go rename to builtin/v10/evm/evm_state.go diff --git a/builtin/v10/evm/evm_actor_params.go b/builtin/v10/evm/evm_types.go similarity index 58% rename from builtin/v10/evm/evm_actor_params.go rename to builtin/v10/evm/evm_types.go index 22ec75c6..ee75a51e 100644 --- a/builtin/v10/evm/evm_actor_params.go +++ b/builtin/v10/evm/evm_types.go @@ -1,5 +1,7 @@ package evm +import "github.com/ipfs/go-cid" + type ConstructorParams struct { Creator []byte Initcode []byte @@ -8,3 +10,8 @@ type ConstructorParams struct { type GetStorageAtParams struct { StorageKey []byte } + +type DelegateCallParams struct { + Code cid.Cid + Input []byte +} diff --git a/builtin/v10/evm/methods.go b/builtin/v10/evm/methods.go new file mode 100644 index 00000000..fb19aedd --- /dev/null +++ b/builtin/v10/evm/methods.go @@ -0,0 +1,17 @@ +package evm + +import ( + "github.com/filecoin-project/go-state-types/abi" + "github.com/filecoin-project/go-state-types/big" + "github.com/filecoin-project/go-state-types/builtin" + typegen "github.com/whyrusleeping/cbor-gen" +) + +var Methods = map[abi.MethodNum]builtin.MethodMeta{ + 1: {"Constructor", *new(func(*ConstructorParams) *abi.EmptyValue)}, + 2: {"InvokeContract", *new(func(bytes *abi.CborBytes) *abi.CborBytes)}, + 3: {"GetBytecode", *new(func(*abi.EmptyValue) *typegen.CborCid)}, + 4: {"GetStorageAt", *new(func(*GetStorageAtParams) *big.Int)}, + 5: {"InvokeContractDelegate", *new(func(params *DelegateCallParams) *abi.CborBytes)}, + 6: {"GetBytecodeHash", *new(func(*abi.EmptyValue) *typegen.CborCid)}, +} diff --git a/builtin/v10/gen/gen.go b/builtin/v10/gen/gen.go index 89e0e429..2e0171a0 100644 --- a/builtin/v10/gen/gen.go +++ b/builtin/v10/gen/gen.go @@ -291,6 +291,7 @@ func main() { // method params and returns evm.ConstructorParams{}, evm.GetStorageAtParams{}, + evm.DelegateCallParams{}, ); err != nil { panic(err) } From c0bdb02c9249fb08db37a14dc3f4563fab2d7f7b Mon Sep 17 00:00:00 2001 From: Aayush Date: Wed, 4 Jan 2023 12:50:07 -0500 Subject: [PATCH 2/2] Add empty MethodsEmbryo --- builtin/methods.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/builtin/methods.go b/builtin/methods.go index 2a99c478..cb589d2a 100644 --- a/builtin/methods.go +++ b/builtin/methods.go @@ -387,6 +387,10 @@ var MethodsEAM = struct { 3, } +var MethodsEmbryo = struct { + Constructor abi.MethodNum +}{} + var MethodsEthAccount = struct { Constructor abi.MethodNum }{