Skip to content

Commit

Permalink
Merge pull request #15 from ProjectOpenSea/dan/2024/02/direct-authori…
Browse files Browse the repository at this point in the history
…ze-tests

Dan/2024/02/direct authorize tests
  • Loading branch information
DJViau authored Feb 14, 2024
2 parents 01e3d0d + 8c5b7c4 commit f97a753
Show file tree
Hide file tree
Showing 8 changed files with 1,802 additions and 67 deletions.
1 change: 1 addition & 0 deletions reference/lib/ReferenceOrderCombiner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ contract ReferenceOrderCombiner is

if (!valid) {
orderHashes[i] = bytes32(0);
ordersToExecute[i].numerator = 0;
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/lib/ConsiderationEncoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ contract ConsiderationEncoder {
orderHashesLengthLocation.write(orderIndex);

// Modify encoding size to account for the shorter orderHashes array.
size -= (1 + orderHashes.length - orderIndex) * OneWord;
size -= (orderHashes.length - orderIndex) * OneWord;
}

/**
Expand Down
14 changes: 11 additions & 3 deletions src/core/lib/OrderCombiner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,17 @@ contract OrderCombiner is OrderFulfiller, FulfillmentApplier {

// Update order status as long as there is some fraction available.
if (advancedOrder.parameters.orderType != OrderType.CONTRACT) {
_assertRestrictedAdvancedOrderAuthorization(
advancedOrder, orderHashes, orderHash, (i / OneWord) - 1
);
if (!_checkRestrictedAdvancedOrderAuthorization(
advancedOrder, orderHashes, orderHash, (i / OneWord) - 1, revertOnInvalid
)) {
assembly {
mstore(add(orderHashes, i), 0)
}

advancedOrder.numerator = 0;

continue;
}

if (!_updateStatus(
orderHash,
Expand Down
112 changes: 112 additions & 0 deletions src/core/lib/ZoneInteraction.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ contract ZoneInteraction is
}
}

function _checkRestrictedAdvancedOrderAuthorization(
AdvancedOrder memory advancedOrder,
bytes32[] memory orderHashes,
bytes32 orderHash,
uint256 orderIndex,
bool revertOnInvalid
) internal returns (bool isValid) {
// Retrieve the parameters of the order in question.
OrderParameters memory parameters = advancedOrder.parameters;

// OrderType 2-3 require zone to be caller or approve via validateOrder.
if (
_isRestrictedAndCallerNotZone(parameters.orderType, parameters.zone)
) {
// Encode the `validateOrder` call in memory.
(MemoryPointer callData, uint256 size) = _encodeAuthorizeOrder(
orderHash,
parameters,
advancedOrder.extraData,
orderHashes,
orderIndex
);

// Perform call and ensure a corresponding magic value was returned.
return _callAndCheckStatusWithSkip(parameters.zone, orderHash, callData, size, InvalidRestrictedOrder_error_selector, revertOnInvalid);
}

return true;
}

function _assertRestrictedAdvancedOrderAuthorization(
AdvancedOrder memory advancedOrder,
bytes32[] memory orderHashes,
Expand Down Expand Up @@ -332,4 +362,86 @@ contract ZoneInteraction is
}
}
}

/**
* @dev Calls the specified target with the given data and checks the status
* of the call. Revert reasons will be "bubbled up" if one is returned,
* otherwise reverting calls will throw a generic error based on the
* supplied error handler.
*
* @param target The address of the contract to call.
* @param orderHash The hash of the order associated with the call.
* @param callData The data to pass to the contract call.
* @param size The size of calldata.
* @param errorSelector The error handling function to call if the call
* fails or the magic value does not match.
* @param revertOnInvalid Whether to revert if the call is invalid. Must
* still revert if the call returns invalid data.
*/
function _callAndCheckStatusWithSkip(
address target,
bytes32 orderHash,
MemoryPointer callData,
uint256 size,
uint256 errorSelector,
bool revertOnInvalid
) internal returns (bool) {
bool success;
bool magicMatch;
assembly {
// Get magic value from the selector at start of provided calldata.
let magic := and(mload(callData), MaskOverFirstFourBytes)

// Clear the start of scratch space.
mstore(0, 0)

// Perform call, placing result in the first word of scratch space.
success := call(gas(), target, 0, callData, size, 0, OneWord)

// Determine if returned magic value matches the calldata selector.
magicMatch := eq(magic, mload(0))
}

// Revert or return false if the call was not successful.
if (!success) {
if (!revertOnInvalid) {
return false;
}

// Revert and pass reason along if one was returned.
_revertWithReasonIfOneIsReturned();

// If no reason was returned, revert with supplied error selector.
assembly {
mstore(0, errorSelector)
mstore(InvalidRestrictedOrder_error_orderHash_ptr, orderHash)
// revert(abi.encodeWithSelector(
// "InvalidRestrictedOrder(bytes32)",
// orderHash
// ))
revert(
Error_selector_offset, InvalidRestrictedOrder_error_length
)
}
}

// Revert if the correct magic value was not returned.
if (!magicMatch) {
// Revert with a generic error message.
assembly {
mstore(0, errorSelector)
mstore(InvalidRestrictedOrder_error_orderHash_ptr, orderHash)

// revert(abi.encodeWithSelector(
// "InvalidRestrictedOrder(bytes32)",
// orderHash
// ))
revert(
Error_selector_offset, InvalidRestrictedOrder_error_length
)
}
}

return true;
}
}
112 changes: 56 additions & 56 deletions test/foundry/zone/PreAndPostFulfillmentCheck.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ contract PreAndPostFulfillmentCheckTest is BaseOrderTest {
}

function testAscendingAmount() public {
// test(
// this.execAscendingAmount,
// Context({
// consideration: consideration,
// numOriginalAdditional: 0,
// numTips: 0
// })
// );
test(
this.execAscendingAmount,
Context({
consideration: consideration,
numOriginalAdditional: 0,
numTips: 0
})
);
test(
this.execAscendingAmount,
Context({
Expand Down Expand Up @@ -118,14 +118,14 @@ contract PreAndPostFulfillmentCheckTest is BaseOrderTest {
}

function testResolvedCriteria() public {
// test(
// this.execResolvedCriteria,
// Context({
// consideration: consideration,
// numOriginalAdditional: 0,
// numTips: 0
// })
// );
test(
this.execResolvedCriteria,
Context({
consideration: consideration,
numOriginalAdditional: 0,
numTips: 0
})
);
test(
this.execResolvedCriteria,
Context({
Expand Down Expand Up @@ -185,14 +185,14 @@ contract PreAndPostFulfillmentCheckTest is BaseOrderTest {
}

function testStateChange() public {
// test(
// this.execStateChange,
// Context({
// consideration: consideration,
// numOriginalAdditional: 0,
// numTips: 0
// })
// );
test(
this.execStateChange,
Context({
consideration: consideration,
numOriginalAdditional: 0,
numTips: 0
})
);
test(
this.execStateChange,
Context({
Expand Down Expand Up @@ -255,14 +255,14 @@ contract PreAndPostFulfillmentCheckTest is BaseOrderTest {
}

function testBasicStateful() public {
// test(
// this.execBasicStateful,
// Context({
// consideration: consideration,
// numOriginalAdditional: 0,
// numTips: 0
// })
// );
test(
this.execBasicStateful,
Context({
consideration: consideration,
numOriginalAdditional: 0,
numTips: 0
})
);
test(
this.execBasicStateful,
Context({
Expand Down Expand Up @@ -319,14 +319,14 @@ contract PreAndPostFulfillmentCheckTest is BaseOrderTest {
}

function testExectBasicStatefulWithConduit() public {
// test(
// this.execBasicStatefulWithConduit,
// Context({
// consideration: consideration,
// numOriginalAdditional: 0,
// numTips: 0
// })
// );
test(
this.execBasicStatefulWithConduit,
Context({
consideration: consideration,
numOriginalAdditional: 0,
numTips: 0
})
);
test(
this.execBasicStatefulWithConduit,
Context({
Expand Down Expand Up @@ -388,14 +388,14 @@ contract PreAndPostFulfillmentCheckTest is BaseOrderTest {
function testBasicStateful(uint8 numOriginalAdditional, uint8 numTips)
public
{
// test(
// this.execBasicStatefulFuzz,
// Context({
// consideration: consideration,
// numOriginalAdditional: numOriginalAdditional,
// numTips: numTips
// })
// );
test(
this.execBasicStatefulFuzz,
Context({
consideration: consideration,
numOriginalAdditional: numOriginalAdditional,
numTips: numTips
})
);
test(
this.execBasicStatefulFuzz,
Context({
Expand Down Expand Up @@ -503,14 +503,14 @@ contract PreAndPostFulfillmentCheckTest is BaseOrderTest {
}

function testFulfillAvailableAdvancedAscending() public {
// test(
// this.execFulfillAvailableAdvancedAscending,
// Context({
// consideration: consideration,
// numOriginalAdditional: 0,
// numTips: 0
// })
// );
test(
this.execFulfillAvailableAdvancedAscending,
Context({
consideration: consideration,
numOriginalAdditional: 0,
numTips: 0
})
);
test(
this.execFulfillAvailableAdvancedAscending,
Context({
Expand Down
14 changes: 7 additions & 7 deletions test/foundry/zone/TestZoneCalldataFidelity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ contract PreAndPostFulfillmentCheckTest is BaseOrderTest {
}

function testCalldataEquivalence(TestCase memory testCase) public {
// test(
// this.execCalldataEquivalence,
// Context({
// consideration: consideration,
// testCase: testCase
// })
// );
test(
this.execCalldataEquivalence,
Context({
consideration: consideration,
testCase: testCase
})
);
test(
this.execCalldataEquivalence,
Context({
Expand Down
Loading

0 comments on commit f97a753

Please sign in to comment.