bug(`anvil`): `nonce too low` after anvil restarts with state
Component
Anvil
Have you ensured that all of these are up to date?
- [X] Foundry
- [X] Foundryup
What version of Foundry are you on?
forge 0.2.0 (8905af3 2024-10-07T03:18:50.978938000Z)
What command(s) is the bug in?
No response
Operating System
macOS (Apple Silicon)
Describe the bug
Steps to reproduce the bug:
anvil -f https://eth.llamarpc.com --chain-id 31337 --state anvil-statewhereanvil-stateis an empty directory.cast send 0x9276449EaC5b4f7Bc17cFC6700f7BeeB86F9bCd0 --value "1 ether" --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 --rpc-url http://127.0.0.1:8545cast balance 0x9276449EaC5b4f7Bc17cFC6700f7BeeB86F9bCd0 --rpc-url http://127.0.0.1:8545shows corect balance
Now restart anvil with the same command.
cast balance 0x9276449EaC5b4f7Bc17cFC6700f7BeeB86F9bCd0 --rpc-url http://127.0.0.1:8545shows 0 balance. This should show the same balance as before since it loads the dumped state.cast send 0x9276449EaC5b4f7Bc17cFC6700f7BeeB86F9bCd0 --value "1 ether" --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 --rpc-url http://127.0.0.1:8545says:
server returned an error response: error code -32003: nonce too low
cc @yash-atreya
Similar to https://github.com/foundry-rs/foundry/pull/9021 I think
Able to reproduce, state looks like this:
Where sender is 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 to 0x9276449EaC5b4f7Bc17cFC6700f7BeeB86F9bCd0
When reloading the state it attempts to send with nonce 784 instead of 785
{
"block":{
"number":"0x13f2216",
"coinbase":"0x0000000000000000000000000000000000000000",
"timestamp":"0x67040449",
"gas_limit":"0x1c9c380",
"basefee":"0x8668a9b0c",
"difficulty":"0x0",
"prevrandao":"0xc94440a55c39aa8130b5f1300a748c3f00641c2835b34aa8681a1bfa3015fec9",
"blob_excess_gas_and_price":{
"excess_blob_gas":0,
"blob_gasprice":1
}
},
"accounts":{
"0x0000000000000000000000000000000000000000":{
"nonce":0,
"balance":"0x2d62361ec22a83b4bb5",
"code":"0x",
"storage":{
}
},
"0x14dC79964da2C08b23698B3D3cc7Ca32193d9955":{
"nonce":0,
"balance":"0x21e19e0c9bab2400000",
"code":"0x",
"storage":{
}
},
"0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65":{
"nonce":18,
"balance":"0x21e19e0c9bab2400000",
"code":"0x",
"storage":{
}
},
"0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f":{
"nonce":11,
"balance":"0x21e19e0c9bab2400000",
"code":"0x",
"storage":{
}
},
"0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC":{
"nonce":47,
"balance":"0x21e19e0c9bab2400000",
"code":"0x",
"storage":{
}
},
"0x70997970C51812dc3A010C7d01b50e0d17dc79C8":{
"nonce":157,
"balance":"0x21e19e0c9bab2400000",
"code":"0x",
"storage":{
}
},
"0x90F79bf6EB2c4f870365E785982E1f101E93b906":{
"nonce":41,
"balance":"0x21e19e0c9bab2400000",
"code":"0x",
"storage":{
}
},
"0x9276449EaC5b4f7Bc17cFC6700f7BeeB86F9bCd0":{
"nonce":0,
"balance":"0xde0b6b3a7640000",
"code":"0x",
"storage":{
}
},
"0x976EA74026E726554dB657fA54763abd0C3a0aa9":{
"nonce":12,
"balance":"0x21e19e0c9bab2400000",
"code":"0x",
"storage":{
}
},
"0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc":{
"nonce":30,
"balance":"0x21e19e0c9bab2400000",
"code":"0x",
"storage":{
}
},
"0xa0Ee7A142d267C1f36714E4a8F75612F20a79720":{
"nonce":9,
"balance":"0x21e19e0c9bab2400000",
"code":"0x",
"storage":{
}
},
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266":{
"nonce":785,
"balance":"0x21e0bfd4ed1ff52ffa0",
"code":"0x",
"storage":{
}
}
},
"best_block_number":"0x13f2216",
"blocks":[
{
"header":{
"parentHash":"0x766f342443de603109f02e2a5911db3c8e563bb3de8608aaf70a36d5f7d6b188",
"ommersHash":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"beneficiary":"0x0000000000000000000000000000000000000000",
"stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000",
"transactionsRoot":"0xab23a7c791e63f60aa204f11b8a60a8719d1fe823a87947a5aa628b5478bb228",
"receiptsRoot":"0xf78dfb743fbd92ade140711c8bbc542b5e307f0ab7984eff35d751969fe57efa",
"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"difficulty":"0xed697dd933971e0e7f46b64c2e31a11285f0450c9ed0ed902c9ccd7d87fad712",
"number":"0x13f2216",
"gasLimit":"0x1c9c380",
"gasUsed":"0x5208",
"timestamp":"0x67040449",
"mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"nonce":"0x0000000000000000",
"baseFeePerGas":"0x8668a9b0c",
"blobGasUsed":"0x0",
"excessBlobGas":"0x0",
"extraData":"0x"
},
"transactions":[
{
"transaction":{
"EIP1559":{
"chainId":"0x7a69",
"nonce":"0x310",
"gasLimit":"0x5209",
"maxFeePerGas":"0x1278ddf460",
"maxPriorityFeePerGas":"0x3b9aca00",
"to":"0x9276449EaC5b4f7Bc17cFC6700f7BeeB86F9bCd0",
"value":"0xde0b6b3a7640000",
"accessList":[
],
"input":"0x",
"r":"0x2a31feee2256f0f6925343771cd2dc0e5f7f7b79178728c6b9c1e1c53c7c4c60",
"s":"0x67da3f72f6f17013f3698b10c8f207ff1551b101299acc16afc22acdb4297534",
"yParity":"0x0",
"hash":"0x0ddd17c639262bd7fa98bfcde685e0b50a1828c3a2f2935c07b54fdacc3df216"
}
},
"impersonated_sender":null
}
],
"ommers":[
]
}
],
"transactions":[
{
"info":{
"transaction_hash":"0x0ddd17c639262bd7fa98bfcde685e0b50a1828c3a2f2935c07b54fdacc3df216",
"transaction_index":0,
"from":"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"to":"0x9276449EaC5b4f7Bc17cFC6700f7BeeB86F9bCd0",
"contract_address":null,
"traces":[
{
"parent":null,
"children":[
],
"idx":0,
"trace":{
"depth":0,
"success":true,
"caller":"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"address":"0x9276449EaC5b4f7Bc17cFC6700f7BeeB86F9bCd0",
"maybe_precompile":null,
"selfdestruct_address":null,
"selfdestruct_refund_target":null,
"selfdestruct_transferred_value":null,
"kind":"CALL",
"value":"0xde0b6b3a7640000",
"data":"0x",
"output":"0x",
"gas_used":0,
"gas_limit":1,
"status":"Stop",
"steps":[
],
"decoded":{
"label":null,
"return_data":null,
"call_data":null
}
},
"logs":[
],
"ordering":[
]
}
],
"exit":"Stop",
"out":"0x",
"nonce":784,
"gas_used":21000
},
"receipt":{
"type":"0x2",
"status":"0x1",
"cumulativeGasUsed":"0x5208",
"logs":[
],
"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
},
"block_hash":"0x01940dd7c1f991657b92de0783d6e781e5b7eef67024604ec02240d1f3851fa1",
"block_number":20914710
}
],
"historical_states":null
}
also notice the other bug where the balance is reset to 0 (so the state is ignored).
Able to reproduce, state looks like this:
Where
senderis0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266to0x9276449EaC5b4f7Bc17cFC6700f7BeeB86F9bCd0When reloading the state it attempts to send with nonce
784instead of785
Looking into it
I think one aspect that it key is that the bug only (?) occurs when you wait some time between launching the forks
I think that if the block on mainnet has advanced beyond the forked state it rejects (~15 secs) and this happens mostly when you apply only a single transaction
If you launch and tear down the described setup multiple times rapidly the balance is correct and the nonce is OK