Skip to content

Commit

Permalink
fix: eth: return the correct nonce from EthGetTransactionCount
Browse files Browse the repository at this point in the history
EVM contracts track this number internally.

fixes #10255
  • Loading branch information
Stebalien committed Feb 17, 2023
1 parent 00b6d06 commit 88f9b9d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions chain/actors/builtin/evm/actor.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type State interface {
cbor.Marshaler

Nonce() (uint64, error)
IsAlive() (bool, error)
GetState() interface{}

GetBytecode() ([]byte, error)
Expand Down
1 change: 1 addition & 0 deletions chain/actors/builtin/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type State interface {
cbor.Marshaler

Nonce() (uint64, error)
IsAlive() (bool, error)
GetState() interface{}

GetBytecode() ([]byte, error)
Expand Down
6 changes: 6 additions & 0 deletions chain/actors/builtin/evm/state.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/ipfs/go-cid"

"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/go-state-types/abi"

evm{{.v}} "github.com/filecoin-project/go-state-types/builtin{{.import}}evm"
)
Expand Down Expand Up @@ -40,6 +42,10 @@ func (s *state{{.v}}) Nonce() (uint64, error) {
return s.State.Nonce, nil
}

func (s *state{{.v}}) IsAlive() (bool, error) {
return s.State.Tombstone == nil, nil
}

func (s *state{{.v}}) GetState() interface{} {
return &s.State
}
Expand Down
4 changes: 4 additions & 0 deletions chain/actors/builtin/evm/v10.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions node/impl/full/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors"
builtinactors "github.com/filecoin-project/lotus/chain/actors/builtin"
builtinevm "github.com/filecoin-project/lotus/chain/actors/builtin/evm"
"github.com/filecoin-project/lotus/chain/ethhashlookup"
"github.com/filecoin-project/lotus/chain/events/filter"
"github.com/filecoin-project/lotus/chain/messagepool"
Expand Down Expand Up @@ -367,6 +368,29 @@ func (a *EthModule) EthGetTransactionCount(ctx context.Context, sender ethtypes.
return ethtypes.EthUint64(0), xerrors.Errorf("cannot parse block param: %s", blkParam)
}

// First, handle the case where the "sender" is an EVM actor.
actor, err := a.StateManager.LoadActor(ctx, addr, ts)
if err != nil {
if xerrors.Is(err, types.ErrActorNotFound) {
return 0, nil
}
return 0, xerrors.Errorf("failed to lookup contract %s: %w", sender, err)
}

if builtinactors.IsEvmActor(actor.Code) {
evmState, err := builtinevm.Load(a.Chain.ActorStore(ctx), actor)
if err != nil {
return 0, xerrors.Errorf("failed to load evm state: %w", err)
}
if alive, err := evmState.IsAlive(); err != nil {
return 0, err
} else if !alive {
return 0, nil
}
nonce, err := evmState.Nonce()
return ethtypes.EthUint64(nonce), err
}

nonce, err := a.Mpool.GetNonce(ctx, addr, ts.Key())
if err != nil {
return ethtypes.EthUint64(0), nil
Expand Down

0 comments on commit 88f9b9d

Please sign in to comment.