|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// This file was procedurally generated from scripts/generate/templates/Packing.t.js. |
| 3 | + |
| 4 | +pragma solidity ^0.8.20; |
| 5 | + |
| 6 | +import {Test} from "forge-std/Test.sol"; |
| 7 | +import {NoncesKeyed} from "@openzeppelin/contracts/utils/NoncesKeyed.sol"; |
| 8 | +import {Nonces} from "@openzeppelin/contracts/utils/Nonces.sol"; |
| 9 | + |
| 10 | +// CAUTION: Unsafe mock for testing purposes. |
| 11 | +contract NoncesKeyedMock is NoncesKeyed { |
| 12 | + function useNonce(address owner, uint192 key) public returns (uint256) { |
| 13 | + return _useNonce(owner, key); |
| 14 | + } |
| 15 | + |
| 16 | + function useCheckedNonce(address owner, uint192 key, uint64 nonce) public { |
| 17 | + _useCheckedNonce(owner, key, nonce); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +contract NoncesKeyedTest is Test { |
| 22 | + NoncesKeyedMock private _mock; |
| 23 | + |
| 24 | + function setUp() public { |
| 25 | + _mock = new NoncesKeyedMock(); |
| 26 | + } |
| 27 | + |
| 28 | + function testSymbolicUseNonce(address owner, uint192 key) public { |
| 29 | + uint256 prevNonce = _mock.useNonce(owner, key); |
| 30 | + assertEq(prevNonce + 1, _mock.nonces(owner, key)); |
| 31 | + } |
| 32 | + |
| 33 | + function testSymbolicUseCheckedNonceLiveness(address owner, uint192 key) public { |
| 34 | + uint256 currNonce = _mock.nonces(owner, key); |
| 35 | + |
| 36 | + // Does not revert |
| 37 | + _mock.useCheckedNonce(owner, key, uint64(currNonce)); |
| 38 | + assertEq(currNonce + 1, _mock.nonces(owner, key)); |
| 39 | + } |
| 40 | + |
| 41 | + function testUseCheckedNonce(address owner, uint192 key, uint64 nonce) public { |
| 42 | + uint256 currNonce = _mock.nonces(owner, key); |
| 43 | + |
| 44 | + if (uint64(currNonce) == nonce) { |
| 45 | + _mock.useCheckedNonce(owner, key, nonce); |
| 46 | + } else { |
| 47 | + vm.expectRevert(abi.encodeWithSelector(Nonces.InvalidAccountNonce.selector, owner, currNonce)); |
| 48 | + _mock.useCheckedNonce(owner, key, nonce); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments