From dae8b7e8fd34b088eae9f79109b11f2e5cb2c44d Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 20 Oct 2022 23:20:40 -0700 Subject: [PATCH] fix: cbor encode/decode params/return values To match new EVM behavior. --- api/eth_transactions.go | 23 +++++++++++++++++------ go.mod | 2 +- go.sum | 4 ++-- node/impl/full/eth.go | 12 +++++++++--- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/api/eth_transactions.go b/api/eth_transactions.go index cd8ca143673..66a4fbb3457 100644 --- a/api/eth_transactions.go +++ b/api/eth_transactions.go @@ -6,6 +6,7 @@ import ( "fmt" mathbig "math/big" + cbg "github.com/whyrusleeping/cbor-gen" "golang.org/x/crypto/sha3" "github.com/filecoin-project/go-address" @@ -57,23 +58,29 @@ type EthTxArgs struct { func NewEthTxArgsFromMessage(msg *types.Message) (EthTxArgs, error) { var to *EthAddress - params := msg.Params + + paramsReader := bytes.NewReader(msg.Params) + var decodedParams []byte if msg.To == builtintypes.EthereumAddressManagerActorAddr { to = nil var create2 eam.Create2Params - reader := bytes.NewReader(msg.Params) - if err := create2.UnmarshalCBOR(reader); err != nil { + if err := create2.UnmarshalCBOR(paramsReader); err != nil { return EthTxArgs{}, err } - params = create2.Initcode + decodedParams = create2.Initcode } else { addr, err := EthAddressFromFilecoinIDAddress(msg.To) if err != nil { return EthTxArgs{}, nil } to = &addr + params, err := cbg.ReadByteArray(paramsReader, uint64(len(msg.Params))) + if err != nil { + return EthTxArgs{}, err + } + decodedParams = params } return EthTxArgs{ @@ -81,7 +88,7 @@ func NewEthTxArgsFromMessage(msg *types.Message) (EthTxArgs, error) { Nonce: int(msg.Nonce), To: to, Value: msg.Value, - Input: params, + Input: decodedParams, MaxFeePerGas: msg.GasFeeCap, MaxPriorityFeePerGas: msg.GasPremium, GasLimit: int(msg.GasLimit), @@ -125,7 +132,11 @@ func (tx *EthTxArgs) ToSignedMessage() (*types.SignedMessage, error) { return nil, err } to = addr - params = tx.Input + var buf bytes.Buffer + if err := cbg.WriteByteArray(&buf, tx.Input); err != nil { + return nil, fmt.Errorf("failed to encode tx input into a cbor byte-string") + } + params = buf.Bytes() } msg := &types.Message{ diff --git a/go.mod b/go.mod index 487fdeda420..2f0182064d8 100644 --- a/go.mod +++ b/go.mod @@ -138,7 +138,7 @@ require ( github.com/syndtr/goleveldb v1.0.0 github.com/urfave/cli/v2 v2.8.1 github.com/whyrusleeping/bencher v0.0.0-20190829221104-bb6607aa8bba - github.com/whyrusleeping/cbor-gen v0.0.0-20220514204315-f29c37e9c44c + github.com/whyrusleeping/cbor-gen v0.0.0-20221021053955-c138aae13722 github.com/whyrusleeping/ledger-filecoin-go v0.9.1-0.20201010031517-c3dcc1bddce4 github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 github.com/xorcare/golden v0.6.1-0.20191112154924-b87f686d7542 diff --git a/go.sum b/go.sum index d7c43a0e0ec..98c730bf26d 100644 --- a/go.sum +++ b/go.sum @@ -2026,8 +2026,8 @@ github.com/whyrusleeping/cbor-gen v0.0.0-20210219115102-f37d292932f2/go.mod h1:f github.com/whyrusleeping/cbor-gen v0.0.0-20210303213153-67a261a1d291/go.mod h1:fgkXqYy7bV2cFeIEOkVTZS/WjXARfBqSH6Q2qHL33hQ= github.com/whyrusleeping/cbor-gen v0.0.0-20220302191723-37c43cae8e14/go.mod h1:fgkXqYy7bV2cFeIEOkVTZS/WjXARfBqSH6Q2qHL33hQ= github.com/whyrusleeping/cbor-gen v0.0.0-20220323183124-98fa8256a799/go.mod h1:fgkXqYy7bV2cFeIEOkVTZS/WjXARfBqSH6Q2qHL33hQ= -github.com/whyrusleeping/cbor-gen v0.0.0-20220514204315-f29c37e9c44c h1:6VPKXBDRt7mDUyiHx9X8ROnPYFDf3L7OfEuKCI5dZDI= -github.com/whyrusleeping/cbor-gen v0.0.0-20220514204315-f29c37e9c44c/go.mod h1:fgkXqYy7bV2cFeIEOkVTZS/WjXARfBqSH6Q2qHL33hQ= +github.com/whyrusleeping/cbor-gen v0.0.0-20221021053955-c138aae13722 h1:0HEhvpGQJ2Gd0ngPW83aduQQuF/V9v13+3zpSrR3lrA= +github.com/whyrusleeping/cbor-gen v0.0.0-20221021053955-c138aae13722/go.mod h1:fgkXqYy7bV2cFeIEOkVTZS/WjXARfBqSH6Q2qHL33hQ= github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f h1:jQa4QT2UP9WYv2nzyawpKMOCl+Z/jW7djv2/J50lj9E= github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f/go.mod h1:p9UJB6dDgdPgMJZs7UjUOdulKyRr9fqkS+6JKAInPy8= github.com/whyrusleeping/go-ctrlnet v0.0.0-20180313164037-f564fbbdaa95/go.mod h1:SJqKCCPXRfBFCwXjfNT/skfsceF7+MBFLI2OrvuRA7g= diff --git a/node/impl/full/eth.go b/node/impl/full/eth.go index ae5c3fe79a6..a5c8d133a26 100644 --- a/node/impl/full/eth.go +++ b/node/impl/full/eth.go @@ -472,7 +472,11 @@ func (a *EthModule) applyEvmMsg(ctx context.Context, tx api.EthCall) (*api.Invoc return nil, xerrors.Errorf("cannot get Filecoin address: %w", err) } to = addr - params = tx.Data + var buf bytes.Buffer + if err := cbg.WriteByteArray(&buf, tx.Data); err != nil { + return nil, fmt.Errorf("failed to encode tx input into a cbor byte-string") + } + params = buf.Bytes() } msg := &types.Message{ @@ -523,7 +527,7 @@ func (a *EthModule) EthCall(ctx context.Context, tx api.EthCall, blkParam string return nil, err } if len(invokeResult.MsgRct.Return) > 0 { - return invokeResult.MsgRct.Return, nil + return cbg.ReadByteArray(bytes.NewReader(invokeResult.MsgRct.Return), uint64(len(invokeResult.MsgRct.Return))) } return api.EthBytes{}, nil } @@ -649,7 +653,9 @@ func (a *EthModule) ethTxFromFilecoinMessageLookup(ctx context.Context, msgLooku V: api.EthBytes{}, R: api.EthBytes{}, S: api.EthBytes{}, - Input: msg.Params, + // TODO: this will be wrong (both for contract creation, and for normal messages). + // Do we fix? + Input: msg.Params, } return tx, nil }