Forge Test: vm.getNonce(player), Obtaining the nonce is incorrect. #8584
-
In the latest version, it will only be increased in the // SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import {Test} from "forge-std/Test.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract VmNonceTest is Test {
address public player = vm.addr(666666);
function setUp() external {}
function testNonce() public {
vm.startPrank(player, player);
ERC20 token = new ERC20("", "");
token.transfer(address(2), 0);
assertEq(2, vm.getNonce(player));
vm.stopPrank();
}
function testNonce_2() public {
vm.startPrank(player, player);
new ERC20("", "");
new ERC20("", "");
assertEq(2, vm.getNonce(player));
vm.stopPrank();
}
}
[FAIL. Reason: assertion failed: 2 != 1] testNonce() (gas: 479181)
Traces:
[479181] VmNonceTest::testNonce()
├─ [0] VM::startPrank(0xacB4888c9780e1edf78Ad7d3b3Ef5084a9c94895, 0xacB4888c9780e1edf78Ad7d3b3Ef5084a9c94895)
│ └─ ← [Return]
├─ [432587] → new ERC20@0x1eaEcB92761a82978FdE14BCdd01383fb84162Fe
│ └─ ← [Return] 2130 bytes of code
├─ [7138] ERC20::transfer(0x0000000000000000000000000000000000000002, 0)
│ ├─ emit Transfer(from: 0xacB4888c9780e1edf78Ad7d3b3Ef5084a9c94895, to: 0x0000000000000000000000000000000000000002, value: 0)
│ └─ ← [Return] true
├─ [0] VM::getNonce(0xacB4888c9780e1edf78Ad7d3b3Ef5084a9c94895) [staticcall]
│ └─ ← [Return] 1
├─ [0] VM::assertEq(2, 1) [staticcall]
│ └─ ← [Revert] assertion failed: 2 != 1
└─ ← [Revert] assertion failed: 2 != 1
[PASS] testNonce_2() (gas: 937254) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
your transfer function seems to be internal, so nonce isnt being counted. changed it to an external function and lets see |
Beta Was this translation helpful? Give feedback.
-
This is a strange behavior. I've been testing this and the nonce doesn't seem to update on any transactions except on contract creations. You should consider reporting this bug. |
Beta Was this translation helpful? Give feedback.
-
Resolved by #8811 |
Beta Was this translation helpful? Give feedback.
Resolved by #8811