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: return the correct nonce from EthGetTransactionCount #10300

Merged
merged 3 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 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)
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
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)
}

}
26 changes: 26 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,31 @@ 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment should be on line 380?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the entire section. I've put it into a block to make that clear.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I switched it to an if chain.

{
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