Skip to content

Commit

Permalink
check off a few TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Oct 19, 2024
1 parent 0411c73 commit 88fd788
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/TheCompact.sol
Original file line number Diff line number Diff line change
Expand Up @@ -795,13 +795,15 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {

unchecked {
uint256 totalIds = transfer.transfers.length;
uint256 id;
for (uint256 i = 0; i < totalIds; ++i) {
SplitByIdComponent calldata component = transfer.transfers[i];
id = component.id;
SplitComponent[] calldata portions = component.portions;
uint256 totalPortions = portions.length;
for (uint256 j = 0; j < totalPortions; ++j) {
SplitComponent calldata portion = portions[j];
operation(msg.sender, portion.claimant, component.id, portion.amount);
operation(msg.sender, portion.claimant, id, portion.amount);
}
}
}
Expand Down Expand Up @@ -902,7 +904,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
sponsorSignature.offset := add(0x20, sponsorSignaturePtr)
sponsorSignature.length := calldataload(sponsorSignaturePtr)

sponsor := calldataload(add(calldataPointer, 0x40)) // TODO: sanitize
sponsor := shr(96, shl(96, calldataload(add(calldataPointer, 0x40))))
nonce := calldataload(add(calldataPointer, 0x60))
expires := calldataload(add(calldataPointer, 0x80))
}
Expand Down Expand Up @@ -969,7 +971,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
let calldataPointerWithOffset := add(calldataPointer, offsetToId)
id := calldataload(calldataPointerWithOffset)
allocatedAmount := calldataload(add(calldataPointerWithOffset, 0x20))
claimant := calldataload(add(calldataPointerWithOffset, 0x40)) // TODO: sanitize
claimant := shr(96, shl(96, calldataload(add(calldataPointerWithOffset, 0x40))))
amount := calldataload(add(calldataPointerWithOffset, 0x60))
}

Expand Down
26 changes: 20 additions & 6 deletions src/lib/HashLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ import { Scope } from "../types/Scope.sol";
import { FunctionCastLib } from "./FunctionCastLib.sol";
import { EfficiencyLib } from "./EfficiencyLib.sol";

// TODO: make calldata versions of these where useful
library HashLib {
using EfficiencyLib for bool;
using FunctionCastLib for function(BatchTransfer calldata, bytes32) internal view returns (bytes32);
Expand Down Expand Up @@ -135,13 +134,29 @@ library HashLib {
}

function toMessageHash(SplitTransfer calldata transfer) internal view returns (bytes32 messageHash) {
// TODO: optimize this part (but remember to watch out for an amount overflow)
uint256 amount = 0;
for (uint256 i = 0; i < transfer.recipients.length; ++i) {
amount += transfer.recipients[i].amount;
uint256 currentAmount;

SplitComponent[] calldata recipients = transfer.recipients;
uint256 totalRecipients = recipients.length;
uint256 errorBuffer;

unchecked {
for (uint256 i = 0; i < totalRecipients; ++i) {
currentAmount = recipients[i].amount;
amount += currentAmount;
errorBuffer |= (amount < currentAmount).asUint256();
}
}

assembly ("memory-safe") {
if errorBuffer {
// Revert Panic(0x11) (arithmetic overflow)
mstore(0, 0x4e487b71)
mstore(0x20, 0x11)
revert(0x1c, 0x24)
}

let m := mload(0x40) // Grab the free memory pointer; memory will be left dirtied.

mstore(m, COMPACT_TYPEHASH)
Expand Down Expand Up @@ -274,7 +289,6 @@ library HashLib {
}

function toMessageHash(SplitBatchTransfer calldata transfer) internal view returns (bytes32 messageHash) {
// TODO: make this more efficient especially once using calldata
SplitByIdComponent[] calldata transfers = transfer.transfers;
uint256 totalIds = transfers.length;

Expand Down Expand Up @@ -913,7 +927,7 @@ library HashLib {
let additionalChainsData := add(0x20, additionalChainsPtr)
let chainIndex := shl(5, calldataload(add(claimWithAdditionalOffset, 0xc0)))

// TODO: rather than using extraOffset, consider breaking into two distinct
// NOTE: rather than using extraOffset, consider breaking into two distinct
// loops or potentially even two calldatacopy operations based on chainIndex
let extraOffset := 0
for { let i := 0 } lt(i, additionalChainsLength) { i := add(i, 0x20) } {
Expand Down

0 comments on commit 88fd788

Please sign in to comment.