Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address L2/Q4 #112

Merged
merged 6 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
22 changes: 17 additions & 5 deletions src/QuarkStateManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,30 @@ contract QuarkStateManager {
* @dev Any unset nonce is valid to use, but using this method
* increases the likelihood that the nonce you use will be in a bucket that
* has already been written to, which costs less gas
* @param wallet Address of the wallet to find the next nonce
cwang25 marked this conversation as resolved.
Show resolved Hide resolved
cwang25 marked this conversation as resolved.
Show resolved Hide resolved
* @return The next unused nonce
*/
function nextNonce(address wallet) external view returns (uint96) {
for (uint96 i = 0; i <= type(uint96).max;) {
if (!isNonceSet(wallet, i) && (nonceScriptAddress[wallet][i] == address(0))) {
return i;
for (uint256 bucket = 0; bucket < type(uint256).max;) {
cwang25 marked this conversation as resolved.
Show resolved Hide resolved
uint256 bucketNonces = nonces[wallet][bucket];
uint96 bucketValue = uint96(bucket << 8);
cwang25 marked this conversation as resolved.
Show resolved Hide resolved
for (uint256 maskOffset = 0; maskOffset < 256;) {
uint256 mask = 1 << maskOffset;
if ((bucketNonces & mask) == 0) {
uint96 nonce = uint96(bucketValue + maskOffset);
if (nonceScriptAddress[wallet][nonce] == address(0)) {
return nonce;
}
}
unchecked {
++maskOffset;
}
}

unchecked {
++i;
++bucket;
}
}

revert NoUnusedNonces();
}

Expand Down
11 changes: 0 additions & 11 deletions src/QuarkWalletFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,6 @@ contract QuarkWalletFactory {
);
}

/**
* @notice Returns the next unset nonce for the wallet corresponding to the given signer and salt
* @dev Any unset nonce is valid to use, but using this method increases
* the likelihood that the nonce you use will be on a bucket that has
* already been written to, which costs less gas
* @return The next unused nonce
*/
function nextNonce(address signer, bytes32 salt) external view returns (uint96) {
return stateManager.nextNonce(walletAddressForSignerWithSalt(signer, salt));
}

cwang25 marked this conversation as resolved.
Show resolved Hide resolved
/**
* @notice Returns the EIP-712 domain separator used for signing operations for the given salted wallet
* @dev Only use for wallets deployed by this factory, or counterfactual wallets that will be deployed;
Expand Down
8 changes: 4 additions & 4 deletions test/QuarkWalletFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ contract QuarkWalletFactoryTest is Test {
vm.pauseGasMetering();
bytes memory incrementer = new YulHelper().getDeployed("Incrementer.sol/Incrementer.json");

uint96 nonce = factory.nextNonce(alice, factory.DEFAULT_SALT());
uint96 nonce = factory.stateManager().nextNonce(factory.walletAddressForSigner(alice));
QuarkWallet.QuarkOperation memory op = QuarkWallet.QuarkOperation({
scriptAddress: address(0),
scriptSource: incrementer,
Expand Down Expand Up @@ -132,7 +132,7 @@ contract QuarkWalletFactoryTest is Test {
vm.pauseGasMetering();
bytes memory incrementer = new YulHelper().getDeployed("Incrementer.sol/Incrementer.json");

uint96 nonce = factory.nextNonce(alice, factory.DEFAULT_SALT());
uint96 nonce = factory.stateManager().nextNonce(factory.walletAddressForSigner(alice));
QuarkWallet.QuarkOperation memory op = QuarkWallet.QuarkOperation({
scriptAddress: address(0),
scriptSource: incrementer,
Expand Down Expand Up @@ -172,7 +172,7 @@ contract QuarkWalletFactoryTest is Test {
vm.pauseGasMetering();
bytes memory incrementer = new YulHelper().getDeployed("Incrementer.sol/Incrementer.json");

uint96 nonce = factory.nextNonce(alice, factory.DEFAULT_SALT());
uint96 nonce = factory.stateManager().nextNonce(factory.walletAddressForSigner(alice));
QuarkWallet.QuarkOperation memory op = QuarkWallet.QuarkOperation({
scriptAddress: address(0),
scriptSource: incrementer,
Expand Down Expand Up @@ -244,8 +244,8 @@ contract QuarkWalletFactoryTest is Test {
vm.pauseGasMetering();
bytes memory getMessageDetails = new YulHelper().getDeployed("GetMessageDetails.sol/GetMessageDetails.json");
bytes32 salt = bytes32("salty salt salt");
uint96 nonce = factory.nextNonce(alice, salt);
address aliceWallet = factory.walletAddressForSignerWithSalt(alice, salt);
uint96 nonce = factory.stateManager().nextNonce(aliceWallet);
QuarkWallet.QuarkOperation memory op = QuarkWallet.QuarkOperation({
scriptAddress: address(0),
scriptSource: getMessageDetails,
Expand Down