Skip to content

Commit a90ee17

Browse files
authored
refactor(eth): attach ToFilecoinMessage converter to EthCall. (#12844)
1 parent 4c9687a commit a90ee17

File tree

4 files changed

+64
-61
lines changed

4 files changed

+64
-61
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
1010
# UNRELEASED
1111

12+
- refactor(eth): attach ToFilecoinMessage converter to EthCall method for improved package/module import structure. This change also exports the converter as a public method, enhancing usability for developers utilizing Lotus as a library. ([filecoin-project/lotus#12844](https://github.com/filecoin-project/lotus/pull/12844))
1213
- Add json output of tipsets to `lotus chain list`. ([filecoin-project/lotus#12691](https://github.com/filecoin-project/lotus/pull/12691))
1314
- Remove IPNI advertisement relay over pubsub via Lotus node as it now has been deprecated. ([filecoin-project/lotus#12768](https://github.com/filecoin-project/lotus/pull/12768)
1415
- During a network upgrade, log migration progress every 2 seconds so they are more helpful and informative. The `LOTUS_MIGRATE_PROGRESS_LOG_SECONDS` environment variable can be used to change this if needed. ([filecoin-project/lotus#12732](https://github.com/filecoin-project/lotus/pull/12732))

chain/types/ethtypes/eth_types.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import (
2323
"github.com/filecoin-project/go-state-types/big"
2424
builtintypes "github.com/filecoin-project/go-state-types/builtin"
2525

26+
"github.com/filecoin-project/lotus/build"
2627
"github.com/filecoin-project/lotus/build/buildconstants"
28+
"github.com/filecoin-project/lotus/chain/actors"
29+
"github.com/filecoin-project/lotus/chain/types"
2730
"github.com/filecoin-project/lotus/lib/must"
2831
)
2932

@@ -247,6 +250,64 @@ type EthCall struct {
247250
Data EthBytes `json:"data"`
248251
}
249252

253+
func (c *EthCall) ToFilecoinMessage() (*types.Message, error) {
254+
var from address.Address
255+
if c.From == nil || *c.From == (EthAddress{}) {
256+
// Send from the filecoin "system" address.
257+
var err error
258+
from, err = (EthAddress{}).ToFilecoinAddress()
259+
if err != nil {
260+
return nil, fmt.Errorf("failed to construct the ethereum system address: %w", err)
261+
}
262+
} else {
263+
// The from address must be translatable to an f4 address.
264+
var err error
265+
from, err = c.From.ToFilecoinAddress()
266+
if err != nil {
267+
return nil, fmt.Errorf("failed to translate sender address (%s): %w", c.From.String(), err)
268+
}
269+
if p := from.Protocol(); p != address.Delegated {
270+
return nil, fmt.Errorf("expected a class 4 address, got: %d: %w", p, err)
271+
}
272+
}
273+
274+
var params []byte
275+
if len(c.Data) > 0 {
276+
initcode := abi.CborBytes(c.Data)
277+
params2, err := actors.SerializeParams(&initcode)
278+
if err != nil {
279+
return nil, fmt.Errorf("failed to serialize params: %w", err)
280+
}
281+
params = params2
282+
}
283+
284+
var to address.Address
285+
var method abi.MethodNum
286+
if c.To == nil {
287+
// this is a contract creation
288+
to = builtintypes.EthereumAddressManagerActorAddr
289+
method = builtintypes.MethodsEAM.CreateExternal
290+
} else {
291+
addr, err := c.To.ToFilecoinAddress()
292+
if err != nil {
293+
return nil, xerrors.Errorf("cannot get Filecoin address: %w", err)
294+
}
295+
to = addr
296+
method = builtintypes.MethodsEVM.InvokeContract
297+
}
298+
299+
return &types.Message{
300+
From: from,
301+
To: to,
302+
Value: big.Int(c.Value),
303+
Method: method,
304+
Params: params,
305+
GasLimit: build.BlockGasLimit,
306+
GasFeeCap: big.Zero(),
307+
GasPremium: big.Zero(),
308+
}, nil
309+
}
310+
250311
func (c *EthCall) UnmarshalJSON(b []byte) error {
251312
type EthCallRaw EthCall // Avoid a recursive call.
252313
type EthCallDecode struct {

node/impl/eth/gas.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (e *ethGas) EthEstimateGas(ctx context.Context, p jsonrpc.RawParams) (ethty
186186
return ethtypes.EthUint64(0), xerrors.Errorf("decoding params: %w", err)
187187
}
188188

189-
msg, err := ethCallToFilecoinMessage(ctx, params.Tx)
189+
msg, err := params.Tx.ToFilecoinMessage()
190190
if err != nil {
191191
return ethtypes.EthUint64(0), err
192192
}
@@ -237,7 +237,7 @@ func (e *ethGas) EthEstimateGas(ctx context.Context, p jsonrpc.RawParams) (ethty
237237
}
238238

239239
func (e *ethGas) EthCall(ctx context.Context, tx ethtypes.EthCall, blkParam ethtypes.EthBlockNumberOrHash) (ethtypes.EthBytes, error) {
240-
msg, err := ethCallToFilecoinMessage(ctx, tx)
240+
msg, err := tx.ToFilecoinMessage()
241241
if err != nil {
242242
return nil, xerrors.Errorf("failed to convert ethcall to filecoin message: %w", err)
243243
}

node/impl/eth/utils.go

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/filecoin-project/lotus/api"
2323
"github.com/filecoin-project/lotus/build/buildconstants"
24-
"github.com/filecoin-project/lotus/chain/actors"
2524
"github.com/filecoin-project/lotus/chain/actors/builtin"
2625
"github.com/filecoin-project/lotus/chain/actors/policy"
2726
"github.com/filecoin-project/lotus/chain/state"
@@ -99,64 +98,6 @@ func getTipsetByEthBlockNumberOrHash(ctx context.Context, cp ChainStore, blkPara
9998
return nil, xerrors.New("invalid block param")
10099
}
101100

102-
func ethCallToFilecoinMessage(ctx context.Context, tx ethtypes.EthCall) (*types.Message, error) {
103-
var from address.Address
104-
if tx.From == nil || *tx.From == (ethtypes.EthAddress{}) {
105-
// Send from the filecoin "system" address.
106-
var err error
107-
from, err = (ethtypes.EthAddress{}).ToFilecoinAddress()
108-
if err != nil {
109-
return nil, xerrors.Errorf("failed to construct the ethereum system address: %w", err)
110-
}
111-
} else {
112-
// The from address must be translatable to an f4 address.
113-
var err error
114-
from, err = tx.From.ToFilecoinAddress()
115-
if err != nil {
116-
return nil, xerrors.Errorf("failed to translate sender address (%s): %w", tx.From.String(), err)
117-
}
118-
if p := from.Protocol(); p != address.Delegated {
119-
return nil, xerrors.Errorf("expected a class 4 address, got: %d: %w", p, err)
120-
}
121-
}
122-
123-
var params []byte
124-
if len(tx.Data) > 0 {
125-
initcode := abi.CborBytes(tx.Data)
126-
params2, err := actors.SerializeParams(&initcode)
127-
if err != nil {
128-
return nil, xerrors.Errorf("failed to serialize params: %w", err)
129-
}
130-
params = params2
131-
}
132-
133-
var to address.Address
134-
var method abi.MethodNum
135-
if tx.To == nil {
136-
// this is a contract creation
137-
to = builtintypes.EthereumAddressManagerActorAddr
138-
method = builtintypes.MethodsEAM.CreateExternal
139-
} else {
140-
addr, err := tx.To.ToFilecoinAddress()
141-
if err != nil {
142-
return nil, xerrors.Errorf("cannot get Filecoin address: %w", err)
143-
}
144-
to = addr
145-
method = builtintypes.MethodsEVM.InvokeContract
146-
}
147-
148-
return &types.Message{
149-
From: from,
150-
To: to,
151-
Value: big.Int(tx.Value),
152-
Method: method,
153-
Params: params,
154-
GasLimit: buildconstants.BlockGasLimit,
155-
GasFeeCap: big.Zero(),
156-
GasPremium: big.Zero(),
157-
}, nil
158-
}
159-
160101
func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTxInfo bool, cp ChainStore, sp StateManager) (ethtypes.EthBlock, error) {
161102
parentKeyCid, err := ts.Parents().Cid()
162103
if err != nil {

0 commit comments

Comments
 (0)