Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 8 additions & 8 deletions snapshots/SignatureGateway.Operations.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"borrowWithSig": "213889",
"repayWithSig": "186743",
"setSelfAsUserPositionManagerWithSig": "75385",
"setUsingAsCollateralWithSig": "85387",
"supplyWithSig": "151994",
"updateUserDynamicConfigWithSig": "63120",
"updateUserRiskPremiumWithSig": "62090",
"withdrawWithSig": "130812"
"borrowWithSig": "214105",
"repayWithSig": "186959",
"setSelfAsUserPositionManagerWithSig": "75504",
"setUsingAsCollateralWithSig": "85603",
"supplyWithSig": "152167",
"updateUserDynamicConfigWithSig": "63336",
"updateUserRiskPremiumWithSig": "62306",
"withdrawWithSig": "130984"
}
4 changes: 2 additions & 2 deletions snapshots/Spoke.Operations.ZeroRiskPremium.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"permitReserve + supply + enable collateral (multicall)": "161207",
"repay: full": "123914",
"repay: partial": "128872",
"setUserPositionManagersWithSig: disable": "47039",
"setUserPositionManagersWithSig: enable": "68951",
"setUserPositionManagersWithSig: disable": "47158",
"setUserPositionManagersWithSig: enable": "69070",
"supply + enable collateral (multicall)": "141409",
"supply: 0 borrows, collateral disabled": "122846",
"supply: 0 borrows, collateral enabled": "105817",
Expand Down
4 changes: 2 additions & 2 deletions snapshots/Spoke.Operations.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"permitReserve + supply + enable collateral (multicall)": "161207",
"repay: full": "117993",
"repay: partial": "137351",
"setUserPositionManagersWithSig: disable": "47039",
"setUserPositionManagersWithSig: enable": "68951",
"setUserPositionManagersWithSig: disable": "47158",
"setUserPositionManagersWithSig: enable": "69070",
"supply + enable collateral (multicall)": "141409",
"supply: 0 borrows, collateral disabled": "122846",
"supply: 0 borrows, collateral enabled": "105817",
Expand Down
8 changes: 2 additions & 6 deletions src/utils/NoncesKeyed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ contract NoncesKeyed is INoncesKeyed {
/// @dev Returns the current packed `keyNonce`. Consumed nonce is increased, so calling this function twice
/// with the same arguments will return different (sequential) results.
function _useNonce(address owner, uint192 key) internal returns (uint256) {
// For each account, the nonce has an initial value of 0, can only be incremented by one, and cannot be
// decremented or reset. This guarantees that the nonce never overflows.
unchecked {
// It is important to do x++ and not ++x here.
return _pack(key, _getNoncesKeyedStorage()._nonces[owner][key]++);
}
// It is important to do x++ and not ++x here.
return _pack(key, _getNoncesKeyedStorage()._nonces[owner][key]++);
}

/// @dev Same as `_useNonce` but checking that `nonce` is the next valid for `owner` for specified packed `keyNonce`.
Expand Down
15 changes: 15 additions & 0 deletions tests/mocks/MockNoncesKeyed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,22 @@ pragma solidity ^0.8.10;
import {NoncesKeyed} from 'src/utils/NoncesKeyed.sol';

contract MockNoncesKeyed is NoncesKeyed {
bytes32 private constant NAMESPACE_SLOT =
0x474d4a5585c1bae3dbeb574bb96408c7174aadd8ab635de4ab498e2723195f00;

function useCheckedNonce(address owner, uint256 keyNonce) public {
_useCheckedNonce(owner, keyNonce);
}

function setNonce(address owner, uint192 key, uint64 nonce) external {
assembly ('memory-safe') {
mstore(0x00, owner)
mstore(0x20, NAMESPACE_SLOT)
let slot1 := keccak256(0x00, 0x40)
mstore(0x00, key)
mstore(0x20, slot1)
let slot2 := keccak256(0x00, 0x40)
sstore(slot2, nonce)
}
}
}
27 changes: 27 additions & 0 deletions tests/unit/NoncesKeyed.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ contract NoncesKeyedTest is Base {
using SafeCast for *;
MockNoncesKeyed public mock;

uint64 internal constant MAX_NONCE = type(uint64).max;

function setUp() public override {
mock = new MockNoncesKeyed();
}
Expand Down Expand Up @@ -54,4 +56,29 @@ contract NoncesKeyedTest is Base {
);
mock.useCheckedNonce(owner, invalidNonce);
}

function test_useNonce_revertsOnOverflow() public {
address owner = vm.randomAddress();
uint192 key = _randomNonceKey();

mock.setNonce(owner, key, MAX_NONCE);
assertEq(mock.nonces(owner, key), _packNonce(key, MAX_NONCE));

vm.prank(owner);
vm.expectRevert();
mock.useNonce(key);
}

function test_useCheckedNonce_revertsOnOverflow() public {
address owner = vm.randomAddress();
uint192 key = _randomNonceKey();

mock.setNonce(owner, key, MAX_NONCE);
assertEq(mock.nonces(owner, key), _packNonce(key, MAX_NONCE));

uint256 keyNonce = mock.nonces(owner, key);

vm.expectRevert();
mock.useCheckedNonce(owner, keyNonce);
}
}
Loading