Skip to content

Commit

Permalink
Add and test function to invalidate nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkeating authored Mar 26, 2024
1 parent bdb850c commit 0a912ff
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/UniStaker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,12 @@ contract UniStaker is INotifiableRewardReceiver, Multicall, EIP712, Nonces {
);
}

/// @notice Allows an address to increment their nonce and therefore invalidate any pending signed
/// actions.
function invalidateNonce() external {
_useNonce(msg.sender);
}

/// @notice Internal method which finds the existing surrogate contract—or deploys a new one if
/// none exists—for a given delegatee.
/// @param _delegatee Account for which a surrogate is sought.
Expand Down
43 changes: 43 additions & 0 deletions test/UniStaker.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2648,6 +2648,49 @@ contract SetAdmin is UniStakerTest {
}
}

contract InvalidateNonce is UniStakerTest {
using stdStorage for StdStorage;

function testFuzz_SucessfullyIncrementsTheNonceOfTheSender(address _caller, uint256 _initialNonce)
public
{
vm.assume(_caller != address(0));
vm.assume(_initialNonce != type(uint256).max);

stdstore.target(address(uniStaker)).sig("nonces(address)").with_key(_caller).checked_write(
_initialNonce
);

vm.prank(_caller);
uniStaker.invalidateNonce();

uint256 currentNonce = uniStaker.nonces(_caller);

assertEq(currentNonce, _initialNonce + 1, "Current nonce is incorrect");
}

function testFuzz_IncreasesTheNonceByTwoWhenCalledTwice(address _caller, uint256 _initialNonce)
public
{
vm.assume(_caller != address(0));
_initialNonce = bound(_initialNonce, 0, type(uint256).max - 2);

stdstore.target(address(uniStaker)).sig("nonces(address)").with_key(_caller).checked_write(
_initialNonce
);

vm.prank(_caller);
uniStaker.invalidateNonce();

vm.prank(_caller);
uniStaker.invalidateNonce();

uint256 currentNonce = uniStaker.nonces(_caller);

assertEq(currentNonce, _initialNonce + 2, "Current nonce is incorrect");
}
}

contract UniStakerRewardsTest is UniStakerTest {
// Helper methods for dumping contract state related to rewards calculation for debugging
function __dumpDebugGlobalRewards() public view {
Expand Down

0 comments on commit 0a912ff

Please sign in to comment.