From 3d98bd090c83cd22eddf957a79713dd231ff94cd Mon Sep 17 00:00:00 2001 From: raulk Date: Fri, 31 May 2024 18:43:53 +0100 Subject: [PATCH 1/2] refactor(eth): attach ToFilecoinMessage converter to EthCall. --- chain/types/ethtypes/eth_types.go | 60 +++++++++++++++++++++++++++++++ node/impl/full/eth.go | 6 ++-- node/impl/full/eth_utils.go | 59 ------------------------------ 3 files changed, 63 insertions(+), 62 deletions(-) diff --git a/chain/types/ethtypes/eth_types.go b/chain/types/ethtypes/eth_types.go index 893c0721c85..e5feb8fcadb 100644 --- a/chain/types/ethtypes/eth_types.go +++ b/chain/types/ethtypes/eth_types.go @@ -23,6 +23,8 @@ import ( builtintypes "github.com/filecoin-project/go-state-types/builtin" "github.com/filecoin-project/lotus/build" + "github.com/filecoin-project/lotus/chain/actors" + "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/lib/must" ) @@ -228,6 +230,64 @@ type EthCall struct { Data EthBytes `json:"data"` } +func (c *EthCall) ToFilecoinMessage() (*types.Message, error) { + var from address.Address + if c.From == nil || *c.From == (EthAddress{}) { + // Send from the filecoin "system" address. + var err error + from, err = (EthAddress{}).ToFilecoinAddress() + if err != nil { + return nil, fmt.Errorf("failed to construct the ethereum system address: %w", err) + } + } else { + // The from address must be translatable to an f4 address. + var err error + from, err = c.From.ToFilecoinAddress() + if err != nil { + return nil, fmt.Errorf("failed to translate sender address (%s): %w", c.From.String(), err) + } + if p := from.Protocol(); p != address.Delegated { + return nil, fmt.Errorf("expected a class 4 address, got: %d: %w", p, err) + } + } + + var params []byte + if len(c.Data) > 0 { + initcode := abi.CborBytes(c.Data) + params2, err := actors.SerializeParams(&initcode) + if err != nil { + return nil, fmt.Errorf("failed to serialize params: %w", err) + } + params = params2 + } + + var to address.Address + var method abi.MethodNum + if c.To == nil { + // this is a contract creation + to = builtintypes.EthereumAddressManagerActorAddr + method = builtintypes.MethodsEAM.CreateExternal + } else { + addr, err := c.To.ToFilecoinAddress() + if err != nil { + return nil, xerrors.Errorf("cannot get Filecoin address: %w", err) + } + to = addr + method = builtintypes.MethodsEVM.InvokeContract + } + + return &types.Message{ + From: from, + To: to, + Value: big.Int(c.Value), + Method: method, + Params: params, + GasLimit: build.BlockGasLimit, + GasFeeCap: big.Zero(), + GasPremium: big.Zero(), + }, nil +} + func (c *EthCall) UnmarshalJSON(b []byte) error { type EthCallRaw EthCall // Avoid a recursive call. type EthCallDecode struct { diff --git a/node/impl/full/eth.go b/node/impl/full/eth.go index b9fcdb8e846..954f9ba3ed2 100644 --- a/node/impl/full/eth.go +++ b/node/impl/full/eth.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "github.com/filecoin-project/go-jsonrpc" "github.com/ipfs/go-cid" "github.com/multiformats/go-multicodec" cbg "github.com/whyrusleeping/cbor-gen" @@ -18,7 +19,6 @@ import ( "golang.org/x/xerrors" "github.com/filecoin-project/go-address" - "github.com/filecoin-project/go-jsonrpc" "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/big" builtintypes "github.com/filecoin-project/go-state-types/builtin" @@ -1012,7 +1012,7 @@ func (a *EthModule) EthEstimateGas(ctx context.Context, p jsonrpc.RawParams) (et return ethtypes.EthUint64(0), xerrors.Errorf("decoding params: %w", err) } - msg, err := ethCallToFilecoinMessage(ctx, params.Tx) + msg, err := params.Tx.ToFilecoinMessage() if err != nil { return ethtypes.EthUint64(0), err } @@ -1179,7 +1179,7 @@ func ethGasSearch( } func (a *EthModule) EthCall(ctx context.Context, tx ethtypes.EthCall, blkParam ethtypes.EthBlockNumberOrHash) (ethtypes.EthBytes, error) { - msg, err := ethCallToFilecoinMessage(ctx, tx) + msg, err := tx.ToFilecoinMessage() if err != nil { return nil, xerrors.Errorf("failed to convert ethcall to filecoin message: %w", err) } diff --git a/node/impl/full/eth_utils.go b/node/impl/full/eth_utils.go index a4b1c66bb84..7d5620962db 100644 --- a/node/impl/full/eth_utils.go +++ b/node/impl/full/eth_utils.go @@ -21,7 +21,6 @@ import ( "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/build" - "github.com/filecoin-project/lotus/chain/actors" "github.com/filecoin-project/lotus/chain/actors/builtin" "github.com/filecoin-project/lotus/chain/state" "github.com/filecoin-project/lotus/chain/store" @@ -134,64 +133,6 @@ func getTipsetByEthBlockNumberOrHash(ctx context.Context, chain *store.ChainStor return nil, errors.New("invalid block param") } -func ethCallToFilecoinMessage(ctx context.Context, tx ethtypes.EthCall) (*types.Message, error) { - var from address.Address - if tx.From == nil || *tx.From == (ethtypes.EthAddress{}) { - // Send from the filecoin "system" address. - var err error - from, err = (ethtypes.EthAddress{}).ToFilecoinAddress() - if err != nil { - return nil, fmt.Errorf("failed to construct the ethereum system address: %w", err) - } - } else { - // The from address must be translatable to an f4 address. - var err error - from, err = tx.From.ToFilecoinAddress() - if err != nil { - return nil, fmt.Errorf("failed to translate sender address (%s): %w", tx.From.String(), err) - } - if p := from.Protocol(); p != address.Delegated { - return nil, fmt.Errorf("expected a class 4 address, got: %d: %w", p, err) - } - } - - var params []byte - if len(tx.Data) > 0 { - initcode := abi.CborBytes(tx.Data) - params2, err := actors.SerializeParams(&initcode) - if err != nil { - return nil, fmt.Errorf("failed to serialize params: %w", err) - } - params = params2 - } - - var to address.Address - var method abi.MethodNum - if tx.To == nil { - // this is a contract creation - to = builtintypes.EthereumAddressManagerActorAddr - method = builtintypes.MethodsEAM.CreateExternal - } else { - addr, err := tx.To.ToFilecoinAddress() - if err != nil { - return nil, xerrors.Errorf("cannot get Filecoin address: %w", err) - } - to = addr - method = builtintypes.MethodsEVM.InvokeContract - } - - return &types.Message{ - From: from, - To: to, - Value: big.Int(tx.Value), - Method: method, - Params: params, - GasLimit: build.BlockGasLimit, - GasFeeCap: big.Zero(), - GasPremium: big.Zero(), - }, nil -} - func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTxInfo bool, cs *store.ChainStore, sa StateAPI) (ethtypes.EthBlock, error) { parentKeyCid, err := ts.Parents().Cid() if err != nil { From c7e8c9670415b8131e685bae5a3ddf01aabb2760 Mon Sep 17 00:00:00 2001 From: Phi Date: Mon, 3 Jun 2024 21:54:45 +0200 Subject: [PATCH 2/2] Make gen / Make docsgen-cli Make gen / Make docsgen-cli --- node/impl/full/eth.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/impl/full/eth.go b/node/impl/full/eth.go index 954f9ba3ed2..29effb12f9c 100644 --- a/node/impl/full/eth.go +++ b/node/impl/full/eth.go @@ -11,7 +11,6 @@ import ( "strings" "time" - "github.com/filecoin-project/go-jsonrpc" "github.com/ipfs/go-cid" "github.com/multiformats/go-multicodec" cbg "github.com/whyrusleeping/cbor-gen" @@ -19,6 +18,7 @@ import ( "golang.org/x/xerrors" "github.com/filecoin-project/go-address" + "github.com/filecoin-project/go-jsonrpc" "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/big" builtintypes "github.com/filecoin-project/go-state-types/builtin"