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

Update basic order flow to avoid allocating unnecessary memory #45

Merged
merged 3 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 15 additions & 29 deletions src/core/lib/BasicOrderFulfiller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -874,35 +874,6 @@ contract BasicOrderFulfiller is OrderValidator {
* `keccak256(abi.encodePacked(offerItemHashes))`
*/
mstore(BasicOrder_order_offerHashes_ptr, keccak256(0, OneWord))

/*
* 3. Write SpentItem to offer array in OrderFulfilled event.
*/
let eventConsiderationArrPtr :=
add(
OrderFulfilled_offer_length_baseOffset,
shl(
OneWordShift,
calldataload(
BasicOrder_additionalRecipients_length_cdPtr
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was wrong, we needed to use:

BasicOrder_totalOriginalAdditionalRecipients_cdPtr

)
)
)

// Set a length of 1 for the offer array.
mstore(eventConsiderationArrPtr, 1)

// Write itemType to the SpentItem struct.
mstore(add(eventConsiderationArrPtr, OneWord), offeredItemType)

// Copy calldata region with (offerToken, offerIdentifier,
// offerAmount) from OrderParameters to (token, identifier,
// amount) in SpentItem struct.
calldatacopy(
add(eventConsiderationArrPtr, AdditionalRecipient_size),
BasicOrder_offerToken_cdPtr,
ThreeWords
)
}
}

Expand Down Expand Up @@ -1035,6 +1006,21 @@ contract BasicOrderFulfiller is OrderValidator {
OrderFulfilled_consideration_body_offset
)

// Set a length of 1 for the offer array.
mstore(add(eventDataPtr, 0x80), 1)

// Write itemType to the SpentItem struct.
mstore(add(eventDataPtr, 0xa0), offeredItemType)

// Copy calldata region with (offerToken, offerIdentifier,
// offerAmount) from OrderParameters to (token, identifier,
// amount) in SpentItem struct.
calldatacopy(
add(eventDataPtr, 0xc0),
Comment on lines +1010 to +1019
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: define named constants for these 3 new offsets.

BasicOrder_offerToken_cdPtr,
ThreeWords
)
Comment on lines +1009 to +1022
Copy link
Collaborator

@Saw-mon-and-Natalie Saw-mon-and-Natalie Mar 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved these down here, since it would make it more clear what we are doing in memory and also would require less calculation of the pointer.

and automatically the offset is also corrected.

Comment on lines +1009 to +1022
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note for myself: these are all shifted by 0x80 compared to the previous version because old eventConsiderationArrPtr used OrderFulfilled_offer_length_baseOffset = 0x200, whereas this eventDataPtr only uses OrderFulfilled_baseOffset = 0x180. The diff is the 0x80.
Then we write to 0x80+0, 0x80+0x20, 0x80+0x40


// Derive total data size including SpentItem and ReceivedItem data.
// SpentItem portion is already included in the baseSize constant,
// as there can only be one element in the array.
Expand Down
10 changes: 8 additions & 2 deletions src/core/lib/ConsiderationEncoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
BasicOrder_offerer_cdPtr,
BasicOrder_startTime_cdPtr,
BasicOrder_startTimeThroughZoneHash_size,
BasicOrder_totalOriginalAdditionalRecipients_cdPtr,
Common_amount_offset,
Common_identifier_offset,
Common_token_offset,
Expand Down Expand Up @@ -613,14 +614,19 @@ contract ConsiderationEncoder {
tailOffset + BasicOrder_consideration_offset_from_offer
);

// Retrieve the offset to the length of additional recipients.
// Retrieve the length of additional recipients.
uint256 additionalRecipientsLength = CalldataPointer.wrap(
BasicOrder_additionalRecipients_length_cdPtr
).readUint256();

// Retrieve the length of additional recipients.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Retrieve the length of additional recipients.
// Retrieve the total original recipients length.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be fixed in a different PR or even this one.

uint256 totalOriginalAdditionalRecipientsLength = CalldataPointer.wrap(
BasicOrder_totalOriginalAdditionalRecipients_cdPtr
).readUint256();

// Derive offset to event data using base offset & total recipients.
uint256 offerDataOffset = OrderFulfilled_offer_length_baseOffset
+ (additionalRecipientsLength << OneWordShift);
+ (totalOriginalAdditionalRecipientsLength << OneWordShift);

// Derive size of offer and consideration data.
// 2 words (lengths) + 4 (offer data) + 5 (consideration 1) + 5 * ar
Expand Down
Loading