Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: eth: use the correct state-tree when resolving addresses #11387

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- fix: Add time slicing to splitstore purging step during compaction to reduce lock congestion [filecoin-project/lotus#11269](https://github.com/filecoin-project/lotus/pull/11269)
- feat: Added instructions on how to setup Prometheus/Grafana for monitoring a local Lotus node [filecoin-project/lotus#11276](https://github.com/filecoin-project/lotus/pull/11276)
- fix: Exclude reverted events in `eth_getLogs` results [filecoin-project/lotus#11318](https://github.com/filecoin-project/lotus/pull/11318)
- fix: The Ethereum API will now use the correct state-tree when resolving "native" addresses into masked ID addresses. Additionally, pending messages from native account types won't be visible in the Ethereum API because there is no "correct" state-tree to pick in this case. However, pending _Ethereum_ transactions and native messages that have landed on-chain will still be visible through the Ethereum API.

## New features
- feat: Add move-partition command ([filecoin-project/lotus#11290](https://github.com/filecoin-project/lotus/pull/11290))
Expand Down
15 changes: 14 additions & 1 deletion chain/types/ethtypes/eth_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,14 @@ type EthTxArgs struct {
// - BlockHash
// - BlockNumber
// - TransactionIndex
// - From
fridrik01 marked this conversation as resolved.
Show resolved Hide resolved
// - Hash
func EthTxFromSignedEthMessage(smsg *types.SignedMessage) (EthTx, error) {
// The from address is always an f410f address, never an ID or other address.
if !IsEthAddress(smsg.Message.From) {
fridrik01 marked this conversation as resolved.
Show resolved Hide resolved
return EthTx{}, xerrors.Errorf("sender must be an eth account, was %s", smsg.Message.From)
}

// Probably redundant, but we might as well check.
if smsg.Signature.Type != typescrypto.SigTypeDelegated {
return EthTx{}, xerrors.Errorf("signature is not delegated type, is type: %d", smsg.Signature.Type)
}
Expand All @@ -79,10 +84,18 @@ func EthTxFromSignedEthMessage(smsg *types.SignedMessage) (EthTx, error) {
return EthTx{}, xerrors.Errorf("failed to recover signature: %w", err)
}

from, err := EthAddressFromFilecoinAddress(smsg.Message.From)
fridrik01 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
// This should be impossible as we've already asserted that we have an EthAddress
// sender...
return EthTx{}, xerrors.Errorf("sender was not an eth account")
}

return EthTx{
Nonce: EthUint64(txArgs.Nonce),
ChainID: EthUint64(txArgs.ChainID),
To: txArgs.To,
From: from,
Value: EthBigInt(txArgs.Value),
Type: Eip1559TxType,
Gas: EthUint64(txArgs.GasLimit),
Expand Down
29 changes: 21 additions & 8 deletions itests/eth_hash_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ func TestTransactionHashLookupBlsFilecoinMessage(t *testing.T) {
kit.MockProofs(),
kit.ThroughRPC(),
)
ens.InterconnectAll().BeginMining(blocktime)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
Expand All @@ -146,9 +145,13 @@ func TestTransactionHashLookupBlsFilecoinMessage(t *testing.T) {
hash, err := ethtypes.EthHashFromCid(sm.Message.Cid())
require.NoError(t, err)

mpoolTx, err := client.EthGetTransactionByHash(ctx, &hash)
require.NoError(t, err)
require.Equal(t, hash, mpoolTx.Hash)
// Assert that BLS messages cannot be retrieved from the message pool until it lands
// on-chain via the eth API.
_, err = client.EthGetTransactionByHash(ctx, &hash)
require.Error(t, err)

// Now start mining.
ens.InterconnectAll().BeginMining(blocktime)

// Wait for message to land on chain
var receipt *api.EthTxReceipt
Expand Down Expand Up @@ -177,6 +180,13 @@ func TestTransactionHashLookupBlsFilecoinMessage(t *testing.T) {
require.NotEmpty(t, *chainTx.BlockHash)
require.NotNil(t, chainTx.TransactionIndex)
require.Equal(t, uint64(*chainTx.TransactionIndex), uint64(0)) // only transaction

// verify that we correctly reported the to address.
toId, err := client.StateLookupID(ctx, addr, types.EmptyTSK)
require.NoError(t, err)
toEth, err := client.FilecoinAddressToEthAddress(ctx, toId)
require.NoError(t, err)
require.Equal(t, &toEth, chainTx.To)
}

// TestTransactionHashLookupSecpFilecoinMessage tests to see if lotus can find a Secp Filecoin Message using the transaction hash
Expand Down Expand Up @@ -228,10 +238,6 @@ func TestTransactionHashLookupSecpFilecoinMessage(t *testing.T) {
hash, err := ethtypes.EthHashFromCid(secpSmsg.Cid())
require.NoError(t, err)

mpoolTx, err := client.EthGetTransactionByHash(ctx, &hash)
require.NoError(t, err)
require.Equal(t, hash, mpoolTx.Hash)

_, err = client.StateWaitMsg(ctx, secpSmsg.Cid(), 3, api.LookbackNoLimit, true)
require.NoError(t, err)

Expand All @@ -253,6 +259,13 @@ func TestTransactionHashLookupSecpFilecoinMessage(t *testing.T) {
require.NotEmpty(t, *chainTx.BlockHash)
require.NotNil(t, chainTx.TransactionIndex)
require.Equal(t, uint64(*chainTx.TransactionIndex), uint64(0)) // only transaction

// verify that we correctly reported the to address.
toId, err := client.StateLookupID(ctx, client.DefaultKey.Address, types.EmptyTSK)
require.NoError(t, err)
toEth, err := client.FilecoinAddressToEthAddress(ctx, toId)
require.NoError(t, err)
require.Equal(t, &toEth, chainTx.To)
}

// TestTransactionHashLookupSecpFilecoinMessage tests to see if lotus can find a Secp Filecoin Message using the transaction hash
Expand Down
43 changes: 32 additions & 11 deletions node/impl/full/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,20 @@ func (a *EthModule) EthGetTransactionByHashLimited(ctx context.Context, txHash *

for _, p := range pending {
if p.Cid() == c {
tx, err := newEthTxFromSignedMessage(ctx, p, a.StateAPI)
// We only return pending eth-account messages because we can't guarantee
// that the from/to addresses of other messages are conversable to 0x-style
// addresses. So we just ignore them.
//
// This should be "fine" as anyone using an "Ethereum-centric" block
// explorer shouldn't care about seeing pending messages from native
// accounts.
tx, err := ethtypes.EthTxFromSignedEthMessage(p)
if err != nil {
return nil, fmt.Errorf("could not convert Filecoin message into tx: %s", err)
return nil, fmt.Errorf("could not convert Filecoin message into tx: %w", err)
}
tx.Hash, err = tx.TxHash()
if err != nil {
return nil, fmt.Errorf("could not compute tx hash for eth txn: %w", err)
}
return &tx, nil
}
Expand Down Expand Up @@ -718,7 +729,7 @@ func (a *EthModule) EthFeeHistory(ctx context.Context, p jsonrpc.RawParams) (eth
)

for blocksIncluded < int(params.BlkCount) && ts.Height() > 0 {
msgs, rcpts, err := messagesAndReceipts(ctx, ts, a.Chain, a.StateAPI)
_, msgs, rcpts, err := executeTipset(ctx, ts, a.Chain, a.StateAPI)
if err != nil {
return ethtypes.EthFeeHistory{}, xerrors.Errorf("failed to retrieve messages and receipts for height %d: %w", ts.Height(), err)
}
Expand Down Expand Up @@ -837,11 +848,16 @@ func (a *EthModule) EthTraceBlock(ctx context.Context, blkNum string) ([]*ethtyp
return nil, xerrors.Errorf("failed to get tipset: %w", err)
}

_, trace, err := a.StateManager.ExecutionTrace(ctx, ts)
stRoot, trace, err := a.StateManager.ExecutionTrace(ctx, ts)
if err != nil {
return nil, xerrors.Errorf("failed when calling ExecutionTrace: %w", err)
}

st, err := a.StateManager.StateTree(stRoot)
if err != nil {
return nil, xerrors.Errorf("failed load computed state-tree: %w", err)
}

cid, err := ts.Key().Cid()
if err != nil {
return nil, xerrors.Errorf("failed to get tipset key cid: %w", err)
Expand Down Expand Up @@ -872,7 +888,7 @@ func (a *EthModule) EthTraceBlock(ctx context.Context, blkNum string) ([]*ethtyp
}

traces := []*ethtypes.EthTrace{}
err = buildTraces(ctx, &traces, nil, []int{}, ir.ExecutionTrace, int64(ts.Height()), a.StateAPI)
err = buildTraces(&traces, nil, []int{}, ir.ExecutionTrace, int64(ts.Height()), st)
if err != nil {
return nil, xerrors.Errorf("failed building traces: %w", err)
}
Expand Down Expand Up @@ -904,11 +920,16 @@ func (a *EthModule) EthTraceReplayBlockTransactions(ctx context.Context, blkNum
return nil, xerrors.Errorf("failed to get tipset: %w", err)
}

_, trace, err := a.StateManager.ExecutionTrace(ctx, ts)
stRoot, trace, err := a.StateManager.ExecutionTrace(ctx, ts)
if err != nil {
return nil, xerrors.Errorf("failed when calling ExecutionTrace: %w", err)
}

st, err := a.StateManager.StateTree(stRoot)
if err != nil {
return nil, xerrors.Errorf("failed load computed state-tree: %w", err)
}

allTraces := make([]*ethtypes.EthTraceReplayBlockTransaction, 0, len(trace))
for _, ir := range trace {
// ignore messages from system actor
Expand Down Expand Up @@ -943,7 +964,7 @@ func (a *EthModule) EthTraceReplayBlockTransactions(ctx context.Context, blkNum
VmTrace: nil,
}

err = buildTraces(ctx, &t.Trace, nil, []int{}, ir.ExecutionTrace, int64(ts.Height()), a.StateAPI)
err = buildTraces(&t.Trace, nil, []int{}, ir.ExecutionTrace, int64(ts.Height()), st)
if err != nil {
return nil, xerrors.Errorf("failed building traces: %w", err)
}
Expand Down Expand Up @@ -1184,7 +1205,7 @@ func (e *EthEvent) EthGetLogs(ctx context.Context, filterSpec *ethtypes.EthFilte

_ = e.uninstallFilter(ctx, f)

return ethFilterResultFromEvents(ces, e.SubManager.StateAPI)
return ethFilterResultFromEvents(ctx, ces, e.SubManager.StateAPI)
}

func (e *EthEvent) EthGetFilterChanges(ctx context.Context, id ethtypes.EthFilterID) (*ethtypes.EthFilterResult, error) {
Expand All @@ -1199,11 +1220,11 @@ func (e *EthEvent) EthGetFilterChanges(ctx context.Context, id ethtypes.EthFilte

switch fc := f.(type) {
case filterEventCollector:
return ethFilterResultFromEvents(fc.TakeCollectedEvents(ctx), e.SubManager.StateAPI)
return ethFilterResultFromEvents(ctx, fc.TakeCollectedEvents(ctx), e.SubManager.StateAPI)
case filterTipSetCollector:
return ethFilterResultFromTipSets(fc.TakeCollectedTipSets(ctx))
case filterMessageCollector:
return ethFilterResultFromMessages(fc.TakeCollectedMessages(ctx), e.SubManager.StateAPI)
return ethFilterResultFromMessages(fc.TakeCollectedMessages(ctx))
}

return nil, xerrors.Errorf("unknown filter type")
Expand All @@ -1221,7 +1242,7 @@ func (e *EthEvent) EthGetFilterLogs(ctx context.Context, id ethtypes.EthFilterID

switch fc := f.(type) {
case filterEventCollector:
return ethFilterResultFromEvents(fc.TakeCollectedEvents(ctx), e.SubManager.StateAPI)
return ethFilterResultFromEvents(ctx, fc.TakeCollectedEvents(ctx), e.SubManager.StateAPI)
}

return nil, xerrors.Errorf("wrong filter type")
Expand Down
14 changes: 7 additions & 7 deletions node/impl/full/eth_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func ethLogFromEvent(entries []types.EventEntry) (data []byte, topics []ethtypes
return data, topics, true
}

func ethFilterResultFromEvents(evs []*filter.CollectedEvent, sa StateAPI) (*ethtypes.EthFilterResult, error) {
func ethFilterResultFromEvents(ctx context.Context, evs []*filter.CollectedEvent, sa StateAPI) (*ethtypes.EthFilterResult, error) {
res := &ethtypes.EthFilterResult{}
for _, ev := range evs {
log := ethtypes.EthLog{
Expand All @@ -117,7 +117,7 @@ func ethFilterResultFromEvents(evs []*filter.CollectedEvent, sa StateAPI) (*etht
return nil, err
}

log.TransactionHash, err = ethTxHashFromMessageCid(context.TODO(), ev.MsgCid, sa)
log.TransactionHash, err = ethTxHashFromMessageCid(ctx, ev.MsgCid, sa)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -155,11 +155,11 @@ func ethFilterResultFromTipSets(tsks []types.TipSetKey) (*ethtypes.EthFilterResu
return res, nil
}

func ethFilterResultFromMessages(cs []*types.SignedMessage, sa StateAPI) (*ethtypes.EthFilterResult, error) {
func ethFilterResultFromMessages(cs []*types.SignedMessage) (*ethtypes.EthFilterResult, error) {
res := &ethtypes.EthFilterResult{}

for _, c := range cs {
hash, err := ethTxHashFromSignedMessage(context.TODO(), c, sa)
hash, err := ethTxHashFromSignedMessage(c)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -321,14 +321,14 @@ func (e *ethSubscription) send(ctx context.Context, v interface{}) {
}

func (e *ethSubscription) start(ctx context.Context) {
for {
for ctx.Err() == nil {
select {
case <-ctx.Done():
return
case v := <-e.in:
switch vt := v.(type) {
case *filter.CollectedEvent:
evs, err := ethFilterResultFromEvents([]*filter.CollectedEvent{vt}, e.StateAPI)
evs, err := ethFilterResultFromEvents(ctx, []*filter.CollectedEvent{vt}, e.StateAPI)
if err != nil {
continue
}
Expand All @@ -344,7 +344,7 @@ func (e *ethSubscription) start(ctx context.Context) {

e.send(ctx, ev)
case *types.SignedMessage: // mpool txid
evs, err := ethFilterResultFromMessages([]*types.SignedMessage{vt}, e.StateAPI)
evs, err := ethFilterResultFromMessages([]*types.SignedMessage{vt})
if err != nil {
continue
}
Expand Down
10 changes: 5 additions & 5 deletions node/impl/full/eth_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package full

import (
"bytes"
"context"

"github.com/multiformats/go-multicodec"
cbg "github.com/whyrusleeping/cbor-gen"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/filecoin-project/go-state-types/builtin/v10/evm"

builtinactors "github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
)
Expand Down Expand Up @@ -39,18 +39,18 @@ func decodePayload(payload []byte, codec uint64) (ethtypes.EthBytes, error) {
}

// buildTraces recursively builds the traces for a given ExecutionTrace by walking the subcalls
func buildTraces(ctx context.Context, traces *[]*ethtypes.EthTrace, parent *ethtypes.EthTrace, addr []int, et types.ExecutionTrace, height int64, sa StateAPI) error {
func buildTraces(traces *[]*ethtypes.EthTrace, parent *ethtypes.EthTrace, addr []int, et types.ExecutionTrace, height int64, st *state.StateTree) error {
// lookup the eth address from the from/to addresses. Note that this may fail but to support
// this we need to include the ActorID in the trace. For now, just log a warning and skip
// this trace.
//
// TODO: Add ActorID in trace, see https://github.com/filecoin-project/lotus/pull/11100#discussion_r1302442288
from, err := lookupEthAddress(ctx, et.Msg.From, sa)
from, err := lookupEthAddress(et.Msg.From, st)
if err != nil {
log.Warnf("buildTraces: failed to lookup from address %s: %v", et.Msg.From, err)
return nil
}
to, err := lookupEthAddress(ctx, et.Msg.To, sa)
to, err := lookupEthAddress(et.Msg.To, st)
if err != nil {
log.Warnf("buildTraces: failed to lookup to address %s: %w", et.Msg.To, err)
return nil
Expand Down Expand Up @@ -239,7 +239,7 @@ func buildTraces(ctx context.Context, traces *[]*ethtypes.EthTrace, parent *etht
*traces = append(*traces, trace)

for i, call := range et.Subcalls {
err := buildTraces(ctx, traces, trace, append(addr, i), call, height, sa)
err := buildTraces(traces, trace, append(addr, i), call, height, st)
if err != nil {
return err
}
Expand Down
Loading