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
4 changes: 2 additions & 2 deletions snapshots/NativeTokenGateway.Operations.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"borrowNative": "228656",
"repayNative": "166471",
"borrowNative": "228678",
"repayNative": "166493",
"supplyAsCollateralNative": "160133",
"supplyNative": "135762",
"withdrawNative: full": "125557",
Expand Down
6 changes: 3 additions & 3 deletions snapshots/SignatureGateway.Operations.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"borrowWithSig": "213889",
"repayWithSig": "186743",
"borrowWithSig": "213911",
"repayWithSig": "186765",
"setSelfAsUserPositionManagerWithSig": "75385",
"setUsingAsCollateralWithSig": "85387",
"supplyWithSig": "151994",
"supplyWithSig": "152012",
"updateUserDynamicConfigWithSig": "63120",
"updateUserRiskPremiumWithSig": "62090",
"withdrawWithSig": "130812"
Expand Down
10 changes: 5 additions & 5 deletions snapshots/Spoke.Getters.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"getUserAccountData: supplies: 0, borrows: 0": "13014",
"getUserAccountData: supplies: 1, borrows: 0": "49426",
"getUserAccountData: supplies: 2, borrows: 0": "81102",
"getUserAccountData: supplies: 2, borrows: 1": "101531",
"getUserAccountData: supplies: 2, borrows: 2": "120791"
"getUserAccountData: supplies: 0, borrows: 0": "13036",
"getUserAccountData: supplies: 1, borrows: 0": "49448",
"getUserAccountData: supplies: 2, borrows: 0": "81124",
"getUserAccountData: supplies: 2, borrows: 1": "101553",
"getUserAccountData: supplies: 2, borrows: 2": "120813"
}
20 changes: 10 additions & 10 deletions snapshots/Spoke.Operations.ZeroRiskPremium.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"borrow: first": "190384",
"borrow: second action, same reserve": "170250",
"liquidationCall (receiveShares): full": "303257",
"liquidationCall (receiveShares): partial": "302675",
"liquidationCall (reportDeficit): full": "367686",
"liquidationCall: full": "320893",
"liquidationCall: partial": "320311",
"permitReserve + repay (multicall)": "164576",
"borrow: first": "190406",
"borrow: second action, same reserve": "170272",
"liquidationCall (receiveShares): full": "303279",
"liquidationCall (receiveShares): partial": "302697",
"liquidationCall (reportDeficit): full": "367708",
"liquidationCall: full": "320915",
"liquidationCall: partial": "320333",
"permitReserve + repay (multicall)": "164598",
"permitReserve + supply (multicall)": "146756",
"permitReserve + supply + enable collateral (multicall)": "161207",
"repay: full": "123914",
"repay: partial": "128872",
"repay: full": "123936",
"repay: partial": "128894",
"setUserPositionManagersWithSig: disable": "47039",
"setUserPositionManagersWithSig: enable": "68951",
"supply + enable collateral (multicall)": "141409",
Expand Down
20 changes: 10 additions & 10 deletions snapshots/Spoke.Operations.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"borrow: first": "259319",
"borrow: second action, same reserve": "202185",
"liquidationCall (receiveShares): full": "335301",
"liquidationCall (receiveShares): partial": "334719",
"liquidationCall (reportDeficit): full": "359886",
"liquidationCall: full": "352937",
"liquidationCall: partial": "352355",
"permitReserve + repay (multicall)": "162044",
"borrow: first": "259341",
"borrow: second action, same reserve": "202207",
"liquidationCall (receiveShares): full": "335323",
"liquidationCall (receiveShares): partial": "334741",
"liquidationCall (reportDeficit): full": "359908",
"liquidationCall: full": "352959",
"liquidationCall: partial": "352377",
"permitReserve + repay (multicall)": "162062",
"permitReserve + supply (multicall)": "146756",
"permitReserve + supply + enable collateral (multicall)": "161207",
"repay: full": "117993",
"repay: partial": "137351",
"repay: full": "118015",
"repay: partial": "137373",
"setUserPositionManagersWithSig: disable": "47039",
"setUserPositionManagersWithSig: enable": "68951",
"supply + enable collateral (multicall)": "141409",
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/INoncesKeyed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ interface INoncesKeyed {
/// @dev This does not invalidate nonce at other `key`s namespace.
/// @param key The key which specifies namespace of the nonce.
/// @return keyNonce The revoked key-prefixed nonce.
function useNonce(uint192 key) external returns (uint256 keyNonce);
function useNonce(uint160 key) external returns (uint256 keyNonce);

/// @notice Returns the next unused nonce for an address and key. Result contains the key prefix.
/// @param owner The address of the nonce owner.
/// @param key The key which specifies namespace of the nonce.
/// @return keyNonce The first 24 bytes are for the key, & the last 8 bytes for the nonce.
function nonces(address owner, uint192 key) external view returns (uint256 keyNonce);
/// @return keyNonce The first 20 bytes are for the key, & the last 12 bytes for the nonce.
function nonces(address owner, uint160 key) external view returns (uint256 keyNonce);
}
18 changes: 9 additions & 9 deletions src/utils/NoncesKeyed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {INoncesKeyed} from 'src/interfaces/INoncesKeyed.sol';
contract NoncesKeyed is INoncesKeyed {
/// @custom:storage-location erc7201:aave-v4.storage.NoncesKeyed
struct NoncesKeyedStorage {
mapping(address owner => mapping(uint192 key => uint64 nonce)) _nonces;
mapping(address owner => mapping(uint160 key => uint96 nonce)) _nonces;
}

/// @dev The storage slot for the NoncesKeyed storage struct.
Expand All @@ -21,19 +21,19 @@ contract NoncesKeyed is INoncesKeyed {
0x474d4a5585c1bae3dbeb574bb96408c7174aadd8ab635de4ab498e2723195f00;

/// @inheritdoc INoncesKeyed
function useNonce(uint192 key) external returns (uint256) {
function useNonce(uint160 key) external returns (uint256) {
return _useNonce(msg.sender, key);
}

/// @inheritdoc INoncesKeyed
function nonces(address owner, uint192 key) external view returns (uint256) {
function nonces(address owner, uint160 key) external view returns (uint256) {
return _pack(key, _getNoncesKeyedStorage()._nonces[owner][key]);
}

/// @notice Consumes the next unused nonce for an address and key.
/// @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) {
function _useNonce(address owner, uint160 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 {
Expand All @@ -44,19 +44,19 @@ contract NoncesKeyed is INoncesKeyed {

/// @dev Same as `_useNonce` but checking that `nonce` is the next valid for `owner` for specified packed `keyNonce`.
function _useCheckedNonce(address owner, uint256 keyNonce) internal {
(uint192 key, ) = _unpack(keyNonce);
(uint160 key, ) = _unpack(keyNonce);
uint256 current = _useNonce(owner, key);
require(keyNonce == current, InvalidAccountNonce(owner, current));
}

/// @dev Pack key and nonce into a keyNonce.
function _pack(uint192 key, uint64 nonce) private pure returns (uint256) {
return (uint256(key) << 64) | nonce;
function _pack(uint160 key, uint96 nonce) private pure returns (uint256) {
return (uint256(key) << 96) | nonce;
}

/// @dev Unpack a keyNonce into its key and nonce components.
function _unpack(uint256 keyNonce) private pure returns (uint192 key, uint64 nonce) {
return (uint192(keyNonce >> 64), uint64(keyNonce));
function _unpack(uint256 keyNonce) private pure returns (uint160 key, uint96 nonce) {
return (uint160(keyNonce >> 96), uint96(keyNonce));
}

/// @dev Loads the NoncesKeyed storage struct.
Expand Down
30 changes: 15 additions & 15 deletions tests/Base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1295,12 +1295,12 @@ abstract contract Base is Test {
return vm.randomUint(min, max);
}

function _randomNonceKey() internal returns (uint192) {
return uint192(vm.randomUint());
function _randomNonceKey() internal returns (uint160) {
return uint160(vm.randomUint());
}

function _randomNonce() internal returns (uint64) {
return uint64(vm.randomUint());
function _randomNonce() internal returns (uint96) {
return uint96(vm.randomUint());
}

// assumes spoke has usdx supported
Expand Down Expand Up @@ -3044,12 +3044,12 @@ abstract contract Base is Test {
function _burnRandomNoncesAtKey(
INoncesKeyed verifier,
address user,
uint192 key
uint160 key
) internal returns (uint256) {
uint256 currentKeyNonce = verifier.nonces(user, key);
(, uint64 nonce) = _unpackNonce(currentKeyNonce);
(, uint96 nonce) = _unpackNonce(currentKeyNonce);

uint64 toBurn = vm.randomUint(1, 100).toUint64();
uint96 toBurn = vm.randomUint(1, 100).toUint96();
for (uint256 i; i < toBurn; ++i) {
vm.prank(user);
verifier.useNonce(key);
Expand All @@ -3075,11 +3075,11 @@ abstract contract Base is Test {
function _getRandomInvalidNonceAtKey(
INoncesKeyed verifier,
address user,
uint192 key
uint160 key
) internal returns (uint256) {
(uint192 currentKey, uint64 currentNonce) = _unpackNonce(verifier.nonces(user, key));
(uint160 currentKey, uint96 currentNonce) = _unpackNonce(verifier.nonces(user, key));
assertEq(currentKey, key);
uint64 nonce = _randomNonce();
uint96 nonce = _randomNonce();
while (currentNonce == nonce) nonce = _randomNonce();
return _packNonce(key, nonce);
}
Expand All @@ -3089,20 +3089,20 @@ abstract contract Base is Test {
address who,
uint256 prevKeyNonce
) internal view {
(uint192 nonceKey, uint64 nonce) = _unpackNonce(prevKeyNonce);
(uint160 nonceKey, uint96 nonce) = _unpackNonce(prevKeyNonce);
// prettier-ignore
unchecked { ++nonce; }
assertEq(verifier.nonces(who, nonceKey), _packNonce(nonceKey, nonce));
}

/// @dev Pack key and nonce into a keyNonce
function _packNonce(uint192 key, uint64 nonce) internal pure returns (uint256) {
return (uint256(key) << 64) | nonce;
function _packNonce(uint160 key, uint96 nonce) internal pure returns (uint256) {
return (uint256(key) << 96) | nonce;
}

/// @dev Unpack a keyNonce into its key and nonce components
function _unpackNonce(uint256 keyNonce) internal pure returns (uint192 key, uint64 nonce) {
return (uint192(keyNonce >> 64), uint64(keyNonce));
function _unpackNonce(uint256 keyNonce) internal pure returns (uint160 key, uint96 nonce) {
return (uint160(keyNonce >> 96), uint96(keyNonce));
}

function _bpsToRay(uint256 bps) internal pure returns (uint256) {
Expand Down
2 changes: 1 addition & 1 deletion tests/gas/Gateways.Operations.gas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ contract NativeTokenGateway_Gas_Tests is Base {
/// forge-config: default.isolate = true
contract SignatureGateway_Gas_Tests is SignatureGatewayBaseTest {
string internal NAMESPACE = 'SignatureGateway.Operations';
uint192 internal nonceKey = 0;
uint160 internal nonceKey = 0;

function setUp() public virtual override {
super.setUp();
Expand Down
2 changes: 1 addition & 1 deletion tests/gas/Spoke.Operations.gas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ contract SpokeOperations_Gas_Tests is SpokeBase {
vm.prank(SPOKE_ADMIN);
spoke.updatePositionManager(positionManager, true);

uint192 nonceKey = 100;
uint160 nonceKey = 100;
vm.prank(user);
spoke.useNonce(nonceKey);

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/NoncesKeyed.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract NoncesKeyedTest is Base {
vm.setArbitraryStorage(address(mock));

address owner = vm.randomAddress();
uint192 key = _randomNonceKey();
uint160 key = _randomNonceKey();

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

Expand All @@ -31,7 +31,7 @@ contract NoncesKeyedTest is Base {
vm.setArbitraryStorage(address(mock));

address owner = vm.randomAddress();
uint192 key = _randomNonceKey();
uint160 key = _randomNonceKey();

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

Expand All @@ -44,7 +44,7 @@ contract NoncesKeyedTest is Base {
vm.setArbitraryStorage(address(mock));

address owner = vm.randomAddress();
uint192 key = _randomNonceKey();
uint160 key = _randomNonceKey();

uint256 currentNonce = _burnRandomNoncesAtKey(mock, owner, key);
uint256 invalidNonce = _getRandomInvalidNonceAtKey(mock, owner, key);
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/Spoke/Spoke.SetUserPositionManagerWithSig.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ contract SpokeSetUserPositionManagersWithSigTest is SpokeBase {
function test_useNonce_monotonic(bytes32) public {
vm.setArbitraryStorage(address(spoke1));
address user = vm.randomAddress();
uint192 nonceKey = vm.randomUint(0, type(uint192).max).toUint192();
uint160 nonceKey = vm.randomUint(0, type(uint160).max).toUint160();

(, uint64 nonce) = _unpackNonce(spoke1.nonces(user, nonceKey));
(, uint96 nonce) = _unpackNonce(spoke1.nonces(user, nonceKey));

vm.prank(user);
spoke1.useNonce(nonceKey);
Expand Down Expand Up @@ -136,7 +136,7 @@ contract SpokeSetUserPositionManagersWithSigTest is SpokeBase {
spoke1.updatePositionManager(positionManager, true);
uint256 deadline = _warpBeforeRandomDeadline();

uint192 nonceKey = _randomNonceKey();
uint160 nonceKey = _randomNonceKey();
ISpoke.SetUserPositionManagers memory params = _setUserPositionManagerData(user, deadline);
uint256 currentNonce = _burnRandomNoncesAtKey(spoke1, params.onBehalfOf, nonceKey);
params.nonce = _getRandomInvalidNonceAtKey(spoke1, params.onBehalfOf, nonceKey);
Expand Down Expand Up @@ -306,7 +306,7 @@ contract SpokeSetUserPositionManagersWithSigTest is SpokeBase {
MockERC1271Wallet smartWallet = new MockERC1271Wallet(alice);
uint256 deadline = _warpBeforeRandomDeadline();

uint192 nonceKey = _randomNonceKey();
uint160 nonceKey = _randomNonceKey();
ISpoke.SetUserPositionManagers memory params = _setUserPositionManagerData(
address(smartWallet),
deadline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ contract SignatureGatewayInvalidSignatureTest is SignatureGatewayBaseTest {

function test_supplyWithSig_revertsWith_InvalidAccountNonce(bytes32) public {
ISignatureGateway.Supply memory p = _supplyData(spoke1, alice, _warpBeforeRandomDeadline());
uint192 nonceKey = _randomNonceKey();
uint160 nonceKey = _randomNonceKey();
uint256 currentNonce = _burnRandomNoncesAtKey(gateway, p.onBehalfOf, nonceKey);
p.nonce = _getRandomInvalidNonceAtKey(gateway, p.onBehalfOf, nonceKey);

Expand All @@ -217,7 +217,7 @@ contract SignatureGatewayInvalidSignatureTest is SignatureGatewayBaseTest {

function test_withdrawWithSig_revertsWith_InvalidAccountNonce(bytes32) public {
ISignatureGateway.Withdraw memory p = _withdrawData(spoke1, alice, _warpBeforeRandomDeadline());
uint192 nonceKey = _randomNonceKey();
uint160 nonceKey = _randomNonceKey();
uint256 currentNonce = _burnRandomNoncesAtKey(gateway, p.onBehalfOf, nonceKey);
p.nonce = _getRandomInvalidNonceAtKey(gateway, p.onBehalfOf, nonceKey);

Expand All @@ -232,7 +232,7 @@ contract SignatureGatewayInvalidSignatureTest is SignatureGatewayBaseTest {

function test_borrowWithSig_revertsWith_InvalidAccountNonce(bytes32) public {
ISignatureGateway.Borrow memory p = _borrowData(spoke1, alice, _warpBeforeRandomDeadline());
uint192 nonceKey = _randomNonceKey();
uint160 nonceKey = _randomNonceKey();
uint256 currentNonce = _burnRandomNoncesAtKey(gateway, p.onBehalfOf, nonceKey);
p.nonce = _getRandomInvalidNonceAtKey(gateway, p.onBehalfOf, nonceKey);

Expand All @@ -247,7 +247,7 @@ contract SignatureGatewayInvalidSignatureTest is SignatureGatewayBaseTest {

function test_repayWithSig_revertsWith_InvalidAccountNonce(bytes32) public {
ISignatureGateway.Repay memory p = _repayData(spoke1, alice, _warpBeforeRandomDeadline());
uint192 nonceKey = _randomNonceKey();
uint160 nonceKey = _randomNonceKey();
uint256 currentNonce = _burnRandomNoncesAtKey(gateway, p.onBehalfOf, nonceKey);
p.nonce = _getRandomInvalidNonceAtKey(gateway, p.onBehalfOf, nonceKey);

Expand All @@ -263,7 +263,7 @@ contract SignatureGatewayInvalidSignatureTest is SignatureGatewayBaseTest {
function test_setUsingAsCollateralWithSig_revertsWith_InvalidAccountNonce(bytes32) public {
uint256 deadline = _warpBeforeRandomDeadline();
ISignatureGateway.SetUsingAsCollateral memory p = _setAsCollateralData(spoke1, alice, deadline);
uint192 nonceKey = _randomNonceKey();
uint160 nonceKey = _randomNonceKey();
uint256 currentNonce = _burnRandomNoncesAtKey(gateway, p.onBehalfOf, nonceKey);
p.nonce = _getRandomInvalidNonceAtKey(gateway, p.onBehalfOf, nonceKey);

Expand All @@ -283,7 +283,7 @@ contract SignatureGatewayInvalidSignatureTest is SignatureGatewayBaseTest {
alice,
deadline
);
uint192 nonceKey = _randomNonceKey();
uint160 nonceKey = _randomNonceKey();
uint256 currentNonce = _burnRandomNoncesAtKey(gateway, p.onBehalfOf, nonceKey);
p.nonce = _getRandomInvalidNonceAtKey(gateway, p.onBehalfOf, nonceKey);

Expand All @@ -302,7 +302,7 @@ contract SignatureGatewayInvalidSignatureTest is SignatureGatewayBaseTest {
alice,
_warpBeforeRandomDeadline()
);
uint192 nonceKey = _randomNonceKey();
uint160 nonceKey = _randomNonceKey();
uint256 currentNonce = _burnRandomNoncesAtKey(gateway, p.onBehalfOf, nonceKey);
p.nonce = _getRandomInvalidNonceAtKey(gateway, p.onBehalfOf, nonceKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ contract SignatureGatewaySetSelfAsUserPositionManagerTest is SignatureGatewayBas
}

function test_setSelfAsUserPositionManagerWithSig() public {
uint192 nonceKey = _randomNonceKey();
uint160 nonceKey = _randomNonceKey();
vm.prank(alice);
spoke1.useNonce(nonceKey);
ISpoke.PositionManagerUpdate[] memory updates = new ISpoke.PositionManagerUpdate[](1);
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/misc/SignatureGateway/SignatureGateway.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ contract SignatureGatewayTest is SignatureGatewayBaseTest {
function test_useNonce_monotonic(bytes32) public {
vm.setArbitraryStorage(address(gateway));
address user = vm.randomAddress();
uint192 nonceKey = vm.randomUint(0, type(uint192).max).toUint192();
uint160 nonceKey = vm.randomUint(0, type(uint160).max).toUint160();

(, uint64 nonce) = _unpackNonce(gateway.nonces(user, nonceKey));
(, uint96 nonce) = _unpackNonce(gateway.nonces(user, nonceKey));

vm.prank(user);
gateway.useNonce(nonceKey);
Expand Down
Loading