Skip to content

Commit

Permalink
fix: cbor encode/decode params/return values
Browse files Browse the repository at this point in the history
To match new EVM behavior.
  • Loading branch information
Stebalien committed Oct 21, 2022
1 parent aaaa2ba commit dae8b7e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
23 changes: 17 additions & 6 deletions api/eth_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -57,31 +58,37 @@ 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{
ChainID: build.Eip155ChainId,
Nonce: int(msg.Nonce),
To: to,
Value: msg.Value,
Input: params,
Input: decodedParams,
MaxFeePerGas: msg.GasFeeCap,
MaxPriorityFeePerGas: msg.GasPremium,
GasLimit: int(msg.GasLimit),
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
12 changes: 9 additions & 3 deletions node/impl/full/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit dae8b7e

Please sign in to comment.