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 filecoin-project#10255
  • Loading branch information
Stebalien committed Feb 17, 2023
1 parent 00b6d06 commit a3bc65b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 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.

27 changes: 24 additions & 3 deletions itests/eth_bytecode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import (
"github.com/stretchr/testify/require"

"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
"github.com/filecoin-project/lotus/itests/kit"
)

// TestGetCode ensures that GetCode returns the correct results for:
// TestGetCodeAndNonce ensures that GetCode and GetTransactionCount return the correct results for:
// 1. Placeholders.
// 2. Non-existent actors.
// 3. Normal EVM actors.
// 4. Self-destructed EVM actors.
func TestGetCode(t *testing.T) {
func TestGetCodeAndNonce(t *testing.T) {
kit.QuietMiningLogs()

blockTime := 100 * time.Millisecond
Expand All @@ -28,21 +29,31 @@ func TestGetCode(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

// Accounts should have empty code.
// Accounts should have empty code, empty nonce.
{
// A random eth address should have no code.
_, ethAddr, filAddr := client.EVM().NewAccount()
bytecode, err := client.EVM().EthGetCode(ctx, ethAddr, "latest")
require.NoError(t, err)
require.Empty(t, bytecode)

// Nonce should also be zero
nonce, err := client.EVM().EthGetTransactionCount(ctx, ethAddr, "latest")
require.NoError(t, err)
require.Zero(t, nonce)

// send some funds to the account.
kit.SendFunds(ctx, t, client, filAddr, types.FromFil(10))

// The code should still be empty, target is now a placeholder.
bytecode, err = client.EVM().EthGetCode(ctx, ethAddr, "latest")
require.NoError(t, err)
require.Empty(t, bytecode)

// Nonce should still be zero.
nonce, err = client.EVM().EthGetTransactionCount(ctx, ethAddr, "latest")
require.NoError(t, err)
require.Zero(t, nonce)
}

// Check contract code.
Expand All @@ -61,6 +72,11 @@ func TestGetCode(t *testing.T) {
require.NoError(t, err)
require.NotEmpty(t, bytecode)

// Nonce should be one.
nonce, err := client.EVM().EthGetTransactionCount(ctx, contractAddr, "latest")
require.NoError(t, err)
require.Equal(t, ethtypes.EthUint64(1), nonce)

// Destroy it.
_, _, err = client.EVM().InvokeContractByFuncName(ctx, client.DefaultKey.Address, contractFilAddr, "destroy()", nil)
require.NoError(t, err)
Expand All @@ -69,6 +85,11 @@ func TestGetCode(t *testing.T) {
bytecode, err = client.EVM().EthGetCode(ctx, contractAddr, "latest")
require.NoError(t, err)
require.Empty(t, bytecode)

// Nonce should go back to zero
nonce, err = client.EVM().EthGetTransactionCount(ctx, contractAddr, "latest")
require.NoError(t, err)
require.Zero(t, nonce)
}

}
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 a3bc65b

Please sign in to comment.