Skip to content

Commit

Permalink
Minor contract size optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
reednaa committed Jan 22, 2025
1 parent e21dc27 commit 3dea019
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
8 changes: 4 additions & 4 deletions snapshots/TheCompactTest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"batchClaimRegisteredWithDeposit": "112534",
"batchClaimRegisteredWithDepositWithWitness": "113359",
"batchClaimWithWitness": "113353",
"batchDepositAndRegisterViaPermit2": "222176",
"batchDepositAndRegisterWithWitnessViaPermit2": "222154",
"batchDepositAndRegisterViaPermit2": "222147",
"batchDepositAndRegisterWithWitnessViaPermit2": "222125",
"batchTransfer": "81344",
"batchWithdrawal": "100137",
"claim": "57062",
"claimAndWithdraw": "73520",
"claimWithWitness": "59493",
"depositAndRegisterViaPermit2": "124480",
"depositAndRegisterViaPermit2": "124451",
"depositBatchSingleERC20": "68138",
"depositBatchSingleNative": "28441",
"depositBatchViaPermit2NativeAndERC20": "129839",
Expand All @@ -31,7 +31,7 @@
"qualifiedSplitBatchClaimWithWitness": "141640",
"qualifiedSplitClaim": "87065",
"qualifiedSplitClaimWithWitness": "87540",
"register": "25408",
"register": "25379",
"splitBatchClaim": "140876",
"splitBatchClaimWithWitness": "140837",
"splitBatchTransfer": "110696",
Expand Down
12 changes: 11 additions & 1 deletion src/lib/HashLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,17 @@ library HashLib {
}

function toIdsAndAmountsHash(uint256[2][] calldata idsAndAmounts) internal pure returns (bytes32 idsAndAmountsHash) {
idsAndAmountsHash = keccak256(abi.encodePacked(idsAndAmounts));
assembly ("memory-safe") {
// Retrieve the free memory pointer; memory will be left dirtied.
let ptr := mload(0x40)
// Get the total length of the calldata slice.
// For every 1 instance of uint256[], it takes up 2 words.
let len := mul(idsAndAmounts.length, 0x40)
// Copy calldata into memory at the free memory pointer.
calldatacopy(ptr, idsAndAmounts.offset, len)
// Compute the hash of the calldata that has been copied into memory.
idsAndAmountsHash := keccak256(ptr, len)
}
}

/**
Expand Down
38 changes: 14 additions & 24 deletions src/lib/RegistrationLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,43 +71,33 @@ library RegistrationLib {
}
}

/**
* @notice Helper function for registerCompactWithSpecificDuration, if the expiry is known
* but the duration is not. Is less efficient than registerCompactWithSpecificDuration
* @dev Expires will be converted into a duration by subtracting the current timestamp from it
* If the current timestamp is greater than the expires, the call will revert.
* @param sponsor The account registering the claim hash.
* @param claimHash A bytes32 hash derived from the details of the compact.
* @param typehash The EIP-712 typehash associated with the claim hash.
* @param expires Timestamp when the claim will expire.
*/
function registerCompactWithSpecificExpiry(address sponsor, bytes32 claimHash, bytes32 typehash, uint256 expires) internal {
uint256 duration;
assembly ("memory-safe") {
// Retrieve the current free memory pointer.
let m := mload(0x40)

// Pack data for deriving active registration storage slot.
mstore(add(m, 0x14), sponsor)
mstore(m, _ACTIVE_REGISTRATIONS_SCOPE)
mstore(add(m, 0x34), claimHash)
mstore(add(m, 0x54), typehash)

// Derive and load active registration storage slot to get current expiration.
let cutoffSlot := keccak256(add(m, 0x1c), 0x58)

// Compute new expiration based on current timestamp and supplied duration.
// This may overflow. We check overflow during InvalidRegistrationDuration.
let duration := sub(expires, timestamp())
duration := sub(expires, timestamp())

// Ensure new expiration does not exceed current and duration does not exceed 30 days.
// If duration > expires AND duration = expires - timestmap > expires, then overflow.
if or(or(gt(duration, expires) ,lt(expires, sload(cutoffSlot))), gt(duration, 0x278d00)) {
if gt(duration, expires) {
// revert InvalidRegistrationDuration(uint256 duration)
mstore(0, 0x1f9a96f4)
mstore(0x20, duration)
revert(0x1c, 0x24)
}

// Store new expiration in active registration storage slot.
sstore(cutoffSlot, expires)

// Emit the CompactRegistered event:
// - topic1: CompactRegistered event signature
// - topic2: sponsor address (sanitized)
// - data: [claimHash, typehash, expires]
mstore(add(m, 0x74), expires)
log2(add(m, 0x34), 0x60, _COMPACT_REGISTERED_SIGNATURE, shr(0x60, shl(0x60, sponsor)))
}
registerCompactWithSpecificDuration(sponsor, claimHash, typehash, duration);
}

/**
Expand Down

0 comments on commit 3dea019

Please sign in to comment.