Skip to content

Nonce calculation is broken for private networks #1999

@iwooden

Description

@iwooden

Currently, nonce calculation is broken for private networks. The main problem is that transaction history is retained for an account even after switching the RPC endpoint to a new network. This can cause the nonce on a new network to be too high, resulting in Metamask transactions never going through.

To reproduce:

  1. Get access to two different private network RPC endpoints.
  2. Create a new account/address in Metamask, with no previous transaction history.
  3. Connect Metamask to the first private endpoint (select "Custom RPC" from the network selection dropdown, enter the RPC URL).
  4. Submit and sign an Ethereum transaction. Note the transaction entry in the transaction history section.
  5. Connect Metamask to the second private endpoint. Note that the transaction entry is still in the transaction history section.
  6. Submit and sign an Ethereum transaction. Note that the transaction hangs in a "pending" state in the Metamask UI.
  7. If you have access to the geth console for one of the nodes servicing the second RPC endpoint, check the txpool. Note that the transaction submitted was submitted with a nonce of 0x1, when the transaction count for the address in question is 0. The transaction will never go through.

I can see in app/scripts/lib/nonce-tracker.js that you're taking the max value between the locally calculated nonce and the transaction count for the address in question. This works for public/test networks where the expected nonce for an account can never go down, but that assumption doesn't hold for private networks when you can switch from a chain where an account's transaction count is 8 to one where the transaction count is 0.

One possible fix is clearing the "Private Network" transaction history when switching RPC endpoints. This isn't totally ideal, but does result in the correct nonce being calculated, and doesn't require you to redo any of the current nonce calculation functions.

  • Expected Behavior: Transactions go through on a new private network
  • Actual Behavior: Transactions are submitted with nonces that are too high
  • Browser Used: Chrome, Metamask version 3.9.11
  • Operating System Used: Windows 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions