Skip to content

Commit

Permalink
feat: implement revoking multiple nonces at once
Browse files Browse the repository at this point in the history
  • Loading branch information
ashhanai committed May 22, 2024
1 parent 1a5bd9a commit 8a614c7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/nonce/PWNRevokedNonce.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,25 @@ contract PWNRevokedNonce {
|*----------------------------------------------------------*/

/**
* @notice Revoke a nonce in the current nonce space.
* @dev Caller is used as a nonce owner.
* @notice Revoke callers nonce in the current nonce space.
* @param nonce Nonce to be revoked.
*/
function revokeNonce(uint256 nonce) external {
_revokeNonce(msg.sender, _nonceSpace[msg.sender], nonce);
}

/**
* @notice Revoke a nonce in a nonce space.
* @dev Caller is used as a nonce owner.
* @notice Revoke multiple caller nonces in the current nonce space.
* @param nonces List of nonces to be revoked.
*/
function revokeNonces(uint256[] calldata nonces) external {
for (uint256 i; i < nonces.length; ++i) {
_revokeNonce(msg.sender, _nonceSpace[msg.sender], nonces[i]);
}
}

/**
* @notice Revoke caller nonce in a nonce space.
* @param nonceSpace Nonce space where a nonce will be revoked.
* @param nonce Nonce to be revoked.
*/
Expand Down
64 changes: 64 additions & 0 deletions test/unit/PWNRevokedNonce.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,70 @@ contract PWNRevokedNonce_RevokeNonce_Test is PWNRevokedNonceTest {
}


/*----------------------------------------------------------*|
|* # REVOKE NONCES *|
|*----------------------------------------------------------*/

contract PWNRevokedNonce_RevokeNonces_Test is PWNRevokedNonceTest {

uint256[] nonces;

function testFuzz_shouldFail_whenAnyNonceAlreadyRevoked(uint256 nonceSpace, uint256 nonce) external {
nonce = bound(nonce, 0, type(uint256).max - 1);
vm.store(address(revokedNonce), _nonceSpaceSlot(alice), bytes32(nonceSpace));
vm.store(address(revokedNonce), _revokedNonceSlot(alice, nonceSpace, nonce), bytes32(uint256(1)));

nonces = new uint256[](2);
nonces[0] = nonce;
nonces[1] = nonce + 1;

vm.expectRevert(abi.encodeWithSelector(PWNRevokedNonce.NonceAlreadyRevoked.selector, alice, nonceSpace, nonce));
vm.prank(alice);
revokedNonce.revokeNonces(nonces);
}

function testFuzz_shouldStoreNoncesAsRevoked(
uint256 nonceSpace, uint256 nonce1, uint256 nonce2, uint256 nonce3
) external {
vm.assume(nonce1 != nonce2 && nonce2 != nonce3 && nonce1 != nonce3);
vm.store(address(revokedNonce), _nonceSpaceSlot(alice), bytes32(nonceSpace));

nonces = new uint256[](3);
nonces[0] = nonce1;
nonces[1] = nonce2;
nonces[2] = nonce3;

vm.prank(alice);
revokedNonce.revokeNonces(nonces);

assertTrue(revokedNonce.isNonceRevoked(alice, nonceSpace, nonce1));
assertTrue(revokedNonce.isNonceRevoked(alice, nonceSpace, nonce2));
assertTrue(revokedNonce.isNonceRevoked(alice, nonceSpace, nonce3));
}

function testFuzz_shouldEmit_NonceRevoked(
uint256 nonceSpace, uint256 nonce1, uint256 nonce2, uint256 nonce3
) external {
vm.assume(nonce1 != nonce2 && nonce2 != nonce3 && nonce1 != nonce3);
vm.store(address(revokedNonce), _nonceSpaceSlot(alice), bytes32(nonceSpace));

nonces = new uint256[](3);
nonces[0] = nonce1;
nonces[1] = nonce2;
nonces[2] = nonce3;

for (uint256 i; i < nonces.length; ++i) {
vm.expectEmit();
emit NonceRevoked(alice, nonceSpace, nonces[i]);
}

vm.prank(alice);
revokedNonce.revokeNonces(nonces);
}

}


/*----------------------------------------------------------*|
|* # REVOKE NONCE WITH NONCE SPACE *|
|*----------------------------------------------------------*/
Expand Down

0 comments on commit 8a614c7

Please sign in to comment.