Skip to content

Commit

Permalink
real i9r tests for add order
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidmeister committed Jul 17, 2023
1 parent 3c4bd81 commit 80446f2
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 71 deletions.
42 changes: 24 additions & 18 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
GenericPoolOrderBookFlashBorrowerTest:testMinimumOutput(uint256,uint256) (runs: 5096, μ: 2315797, ~: 2319054)
GenericPoolOrderBookFlashBorrowerTest:testTakeOrdersSender() (gas: 2230889)
OrderBookAddOrderTest:testAddOrderSameAccountWithDifferentConfig(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),address,address) (runs: 5096, μ: 3333974, ~: 3312367)
OrderBookAddOrderTest:testAddOrderTwoAccountsWithDifferentConfig(address,address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),address,address) (runs: 5096, μ: 3179371, ~: 3184722)
OrderBookAddOrderTest:testAddOrderTwoAccountsWithSameConfig(address,address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),address) (runs: 5096, μ: 2984380, ~: 2983689)
OrderBookAddOrderTest:testAddOrderWithCalculationsInputsAndOutputsSucceeds(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),address) (runs: 5096, μ: 1552071, ~: 1543469)
OrderBookAddOrderTest:testAddOrderWithNonEmptyMetaEmitsMetaV1(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),address) (runs: 5096, μ: 1561951, ~: 1553398)
OrderBookAddOrderTest:testAddOrderWithNonEmptyMetaReverts(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),address) (runs: 5096, μ: 861666, ~: 859767)
OrderBookAddOrderTest:testAddOrderWithoutCalculationsReverts(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes)) (runs: 5096, μ: 285893, ~: 284865)
OrderBookAddOrderTest:testAddOrderWithoutInputsReverts(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes)) (runs: 5096, μ: 297783, ~: 295915)
OrderBookAddOrderTest:testAddOrderWithoutOutputsReverts(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes)) (runs: 5096, μ: 298502, ~: 296458)
GenericPoolOrderBookFlashBorrowerTest:testMinimumOutput(uint256,uint256) (runs: 5096, μ: 2315773, ~: 2319046)
GenericPoolOrderBookFlashBorrowerTest:testTakeOrdersSender() (gas: 2230881)
OrderBookAddOrderMockTest:testAddOrderSameAccountWithDifferentConfig(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),address,address) (runs: 5096, μ: 3332897, ~: 3311290)
OrderBookAddOrderMockTest:testAddOrderTwoAccountsWithDifferentConfig(address,address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),address,address) (runs: 5096, μ: 3178640, ~: 3183991)
OrderBookAddOrderMockTest:testAddOrderTwoAccountsWithSameConfig(address,address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),address) (runs: 5096, μ: 2984203, ~: 2983512)
OrderBookAddOrderMockTest:testAddOrderWithCalculationsInputsAndOutputsSucceeds(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),address) (runs: 5096, μ: 1552081, ~: 1543479)
OrderBookAddOrderMockTest:testAddOrderWithNonEmptyMetaEmitsMetaV1(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),address) (runs: 5096, μ: 1561961, ~: 1553408)
OrderBookAddOrderMockTest:testAddOrderWithNonEmptyMetaReverts(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes),address) (runs: 5096, μ: 861676, ~: 859777)
OrderBookAddOrderMockTest:testAddOrderWithoutCalculationsReverts(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes)) (runs: 5096, μ: 285903, ~: 284875)
OrderBookAddOrderMockTest:testAddOrderWithoutInputsReverts(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes)) (runs: 5096, μ: 297793, ~: 295925)
OrderBookAddOrderMockTest:testAddOrderWithoutOutputsReverts(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes)) (runs: 5096, μ: 298512, ~: 296468)
OrderBookAddOrderTest:testAddOrderRealNoHandleIOReverts(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes)) (runs: 5096, μ: 249422, ~: 248374)
OrderBookAddOrderTest:testAddOrderRealNoSourcesReverts(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes)) (runs: 5096, μ: 241014, ~: 239965)
OrderBookAddOrderTest:testAddOrderRealOneStackCalculateReverts(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes)) (runs: 5096, μ: 272576, ~: 271479)
OrderBookAddOrderTest:testAddOrderRealThreeStackCalculate(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes)) (runs: 5096, μ: 1677570, ~: 1667344)
OrderBookAddOrderTest:testAddOrderRealTwoStackCalculateReverts(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes)) (runs: 5096, μ: 1671739, ~: 1661513)
OrderBookAddOrderTest:testAddOrderRealZeroStackCalculateReverts(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes[],uint256[]),bytes)) (runs: 5096, μ: 268146, ~: 267050)
OrderBookDepositTest:testDepositEvent(address,uint256,uint256) (runs: 5096, μ: 38710, ~: 38710)
OrderBookDepositTest:testDepositFail(address,uint256,uint256) (runs: 5096, μ: 8937393460516740785, ~: 8937393460516740786)
OrderBookDepositTest:testDepositGas00() (gas: 8176)
OrderBookDepositTest:testDepositGas01() (gas: 34620)
OrderBookDepositTest:testDepositMany((address,address,uint256,uint248)[]) (runs: 5096, μ: 5182548, ~: 4940723)
OrderBookDepositTest:testDepositMany((address,address,uint256,uint248)[]) (runs: 5096, μ: 5023411, ~: 4663258)
OrderBookDepositTest:testDepositOverflow(address,uint256,uint256,uint256) (runs: 5096, μ: 46645, ~: 46645)
OrderBookDepositTest:testDepositReentrancy(address,uint256,uint256,address,uint256,uint256) (runs: 5096, μ: 495093, ~: 496632)
OrderBookDepositTest:testDepositReentrancy(address,uint256,uint256,address,uint256,uint256) (runs: 5096, μ: 495175, ~: 496632)
OrderBookDepositTest:testDepositSimple(address,uint256,uint256) (runs: 5096, μ: 37840, ~: 37840)
OrderBookDepositTest:testDepositZero(address,uint256) (runs: 5096, μ: 12639, ~: 12639)
OrderBookDepositTest:testVaultBalanceNoDeposits(address,uint256) (runs: 5096, μ: 8263, ~: 8263)
OrderBookDepositTest:testVaultBalanceReentrant(address,uint256,uint256,address,address,uint256) (runs: 5096, μ: 494116, ~: 495964)
OrderBookDepositTest:testVaultBalanceReentrant(address,uint256,uint256,address,address,uint256) (runs: 5096, μ: 494155, ~: 495964)
OrderBookWithdrawTest:testWithdrawEmptyVault(address,address,uint256,uint256) (runs: 5096, μ: 15251, ~: 15251)
OrderBookWithdrawTest:testWithdrawFailure(address,uint256,uint256,uint256) (runs: 5096, μ: 8937393460516719635, ~: 8937393460516700435)
OrderBookWithdrawTest:testWithdrawFullVault(address,uint256,uint256,uint256) (runs: 5096, μ: 41244, ~: 41242)
OrderBookWithdrawTest:testWithdrawMany((bool,address,address,uint256,uint248)[]) (runs: 5096, μ: 1398046, ~: 1336923)
OrderBookWithdrawTest:testWithdrawFailure(address,uint256,uint256,uint256) (runs: 5096, μ: 8937393460516719508, ~: 8937393460516700411)
OrderBookWithdrawTest:testWithdrawFullVault(address,uint256,uint256,uint256) (runs: 5096, μ: 41245, ~: 41244)
OrderBookWithdrawTest:testWithdrawMany((bool,address,address,uint256,uint248)[]) (runs: 5096, μ: 1392625, ~: 1325216)
OrderBookWithdrawTest:testWithdrawPartialVault(address,uint256,uint256,uint256) (runs: 5096, μ: 51914, ~: 51914)
OrderBookWithdrawTest:testWithdrawReentrant(address,uint256,uint256,address,address,uint256) (runs: 5096, μ: 506261, ~: 507991)
OrderBookWithdrawTest:testWithdrawReentrant(address,uint256,uint256,address,address,uint256) (runs: 5096, μ: 506222, ~: 507991)
OrderBookWithdrawTest:testWithdrawZero(address,address,uint256) (runs: 5096, μ: 12809, ~: 12809)
2 changes: 1 addition & 1 deletion lib/rain.interpreter
64 changes: 20 additions & 44 deletions test/concrete/OrderBook.addOrder.mock.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,20 @@ pragma solidity =0.8.19;
import "rain.metadata/LibMeta.sol";

import "test/util/abstract/OrderBookExternalMockTest.sol";
import "test/util/lib/LibTestAddOrder.sol";

/// @title OrderBookAddOrderMockTest
/// @notice Tests the addOrder function of the OrderBook contract.
contract OrderBookAddOrderMockTest is OrderBookExternalMockTest, IMetaV1 {
/// A little boilerplate to make it easier to build the order that we expect
/// for a given order config.
function expectedOrder(address owner, OrderConfig memory config, address expression)
internal
view
returns (Order memory, bytes32)
{
Evaluable memory expectedEvaluable = Evaluable(iInterpreter, iStore, expression);
Order memory order = Order(
owner,
config.evaluableConfig.sources.length > 1
&& config.evaluableConfig.sources[SourceIndex.unwrap(HANDLE_IO_ENTRYPOINT)].length > 0,
expectedEvaluable,
config.validInputs,
config.validOutputs
);
return (order, LibOrder.hash(order));
}

/// Boilerplate to add an order with a mocked deployer and checks events and
/// storage accesses.
function addOrderWithChecks(address owner, OrderConfig memory config, address expression)
internal
returns (Order memory, bytes32)
{
config.evaluableConfig.deployer = iDeployer;
(Order memory order, bytes32 orderHash) = expectedOrder(owner, config, expression);
(Order memory order, bytes32 orderHash) =
LibTestAddOrder.expectedOrder(owner, config, iInterpreter, iStore, expression);
assertTrue(!iOrderbook.orderExists(orderHash));
vm.mockCall(
address(iDeployer),
Expand Down Expand Up @@ -75,26 +58,14 @@ contract OrderBookAddOrderMockTest is OrderBookExternalMockTest, IMetaV1 {
return (order, orderHash);
}

/// Valid config has a few requirements, this boilerplate makes it easier to
/// get the fuzzer to meet them.
function assumeValidConfig(OrderConfig memory config) internal pure {
vm.assume(config.evaluableConfig.sources.length >= 2);
vm.assume(config.validInputs.length > 0);
vm.assume(config.validOutputs.length > 0);
if (config.meta.length > 0) {
// This is a bit of a hack, but it's the easiest way to get a valid
// meta document.
config.meta = abi.encodePacked(META_MAGIC_NUMBER_V1, config.meta);
}
}

/// Adding an order without calculations MUST revert.
function testAddOrderWithoutCalculationsReverts(address owner, OrderConfig memory config) public {
vm.prank(owner);
config.evaluableConfig.sources = new bytes[](0);
vm.expectRevert(abi.encodeWithSelector(OrderNoSources.selector, owner));
iOrderbook.addOrder(config);
(Order memory order, bytes32 orderHash) = expectedOrder(owner, config, address(0));
(Order memory order, bytes32 orderHash) =
LibTestAddOrder.expectedOrder(owner, config, iInterpreter, iStore, address(0));
(order);
assertTrue(!iOrderbook.orderExists(orderHash));
}
Expand All @@ -106,7 +77,8 @@ contract OrderBookAddOrderMockTest is OrderBookExternalMockTest, IMetaV1 {
config.validInputs = new IO[](0);
vm.expectRevert(abi.encodeWithSelector(OrderNoInputs.selector, owner));
iOrderbook.addOrder(config);
(Order memory order, bytes32 orderHash) = expectedOrder(owner, config, address(0));
(Order memory order, bytes32 orderHash) =
LibTestAddOrder.expectedOrder(owner, config, iInterpreter, iStore, address(0));
(order);
assertTrue(!iOrderbook.orderExists(orderHash));
}
Expand All @@ -119,7 +91,8 @@ contract OrderBookAddOrderMockTest is OrderBookExternalMockTest, IMetaV1 {
config.validOutputs = new IO[](0);
vm.expectRevert(abi.encodeWithSelector(OrderNoOutputs.selector, owner));
iOrderbook.addOrder(config);
(Order memory order, bytes32 orderHash) = expectedOrder(owner, config, address(0));
(Order memory order, bytes32 orderHash) =
LibTestAddOrder.expectedOrder(owner, config, iInterpreter, iStore, address(0));
(order);
assertTrue(!iOrderbook.orderExists(orderHash));
}
Expand Down Expand Up @@ -160,7 +133,8 @@ contract OrderBookAddOrderMockTest is OrderBookExternalMockTest, IMetaV1 {
vm.expectRevert(abi.encodeWithSelector(NotRainMetaV1.selector, config.meta));
iOrderbook.addOrder(config);

(Order memory order, bytes32 orderHash) = expectedOrder(owner, config, expression);
(Order memory order, bytes32 orderHash) =
LibTestAddOrder.expectedOrder(owner, config, iInterpreter, iStore, expression);
(order);
assertTrue(!iOrderbook.orderExists(orderHash));
}
Expand Down Expand Up @@ -193,7 +167,7 @@ contract OrderBookAddOrderMockTest is OrderBookExternalMockTest, IMetaV1 {
address expression
) public {
vm.assume(alice != bob);
assumeValidConfig(config);
vm.assume(LibTestAddOrder.conformConfig(config, iDeployer));
(Order memory aliceOrder, bytes32 aliceOrderHash) = addOrderWithChecks(alice, config, expression);
(Order memory bobOrder, bytes32 bobOrderHash) = addOrderWithChecks(bob, config, expression);
(aliceOrder);
Expand All @@ -212,8 +186,8 @@ contract OrderBookAddOrderMockTest is OrderBookExternalMockTest, IMetaV1 {
address bobExpression
) public {
vm.assume(alice != bob);
assumeValidConfig(aliceConfig);
assumeValidConfig(bobConfig);
vm.assume(LibTestAddOrder.conformConfig(aliceConfig, iDeployer));
vm.assume(LibTestAddOrder.conformConfig(bobConfig, iDeployer));
(Order memory aliceOrder, bytes32 aliceOrderHash) = addOrderWithChecks(alice, aliceConfig, aliceExpression);
(Order memory bobOrder, bytes32 bobOrderHash) = addOrderWithChecks(bob, bobConfig, bobExpression);
(aliceOrder);
Expand All @@ -230,10 +204,12 @@ contract OrderBookAddOrderMockTest is OrderBookExternalMockTest, IMetaV1 {
address expressionOne,
address expressionTwo
) public {
assumeValidConfig(configOne);
assumeValidConfig(configTwo);
(Order memory expectedOrderOne, bytes32 expectedOrderOneHash) = expectedOrder(alice, configOne, expressionOne);
(Order memory expectedOrderTwo, bytes32 expectedOrderTwoHash) = expectedOrder(alice, configTwo, expressionTwo);
vm.assume(LibTestAddOrder.conformConfig(configOne, iDeployer));
vm.assume(LibTestAddOrder.conformConfig(configTwo, iDeployer));
(Order memory expectedOrderOne, bytes32 expectedOrderOneHash) =
LibTestAddOrder.expectedOrder(alice, configOne, iInterpreter, iStore, expressionOne);
(Order memory expectedOrderTwo, bytes32 expectedOrderTwoHash) =
LibTestAddOrder.expectedOrder(alice, configTwo, iInterpreter, iStore, expressionTwo);
(expectedOrderOne);
(expectedOrderTwo);
assertTrue(expectedOrderOneHash != expectedOrderTwoHash);
Expand Down
68 changes: 64 additions & 4 deletions test/concrete/OrderBook.addOrder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,73 @@
pragma solidity =0.8.19;

import "test/util/abstract/OrderBookExternalRealTest.sol";
import "test/util/lib/LibTestAddOrder.sol";

/// @title OrderBookAddOrderTest
/// @notice A test harness for testing the OrderBook addOrder function.
contract OrderBookAddOrderTest is OrderBookExternalRealTest {

/// No sources reverts as we need at least a calculate expression.
function testAddOrderRealNoSourcesReverts(OrderConfig memory config) public {
// iOrderbook.addOrder(config);
function testAddOrderRealNoSourcesReverts(address owner, OrderConfig memory config) public {
vm.assume(LibTestAddOrder.conformConfig(config, iDeployer));
config.evaluableConfig.sources = new bytes[](0);
vm.expectRevert(abi.encodeWithSelector(OrderNoSources.selector, owner));
vm.prank(owner);
iOrderbook.addOrder(config);
}

/// No handle IO reverts.
function testAddOrderRealNoHandleIOReverts(address owner, OrderConfig memory config) public {
vm.assume(LibTestAddOrder.conformConfig(config, iDeployer));
(bytes[] memory sources, uint256[] memory constants) = IParserV1(address(iDeployer)).parse(":;");
(constants);
config.evaluableConfig.sources = sources;
vm.expectRevert(abi.encodeWithSelector(OrderNoHandleIO.selector, owner));
vm.prank(owner);
iOrderbook.addOrder(config);
}

/// A stack of 0 for calculate order reverts.
function testAddOrderRealZeroStackCalculateReverts(address owner, OrderConfig memory config) public {
vm.assume(LibTestAddOrder.conformConfig(config, iDeployer));
(bytes[] memory sources, uint256[] memory constants) = IParserV1(address(iDeployer)).parse(":;:;");
(constants);
config.evaluableConfig.sources = sources;
vm.expectRevert(abi.encodeWithSelector(MinFinalStack.selector, 2, 0));
vm.prank(owner);
iOrderbook.addOrder(config);
}

/// A stack of 1 for calculate order reverts.
function testAddOrderRealOneStackCalculateReverts(address owner, OrderConfig memory config) public {
vm.assume(LibTestAddOrder.conformConfig(config, iDeployer));
(bytes[] memory sources, uint256[] memory constants) =
IParserV1(address(iDeployer)).parse("_:block-timestamp();:;");
(constants);
config.evaluableConfig.sources = sources;
vm.expectRevert(abi.encodeWithSelector(MinFinalStack.selector, 2, 1));
vm.prank(owner);
iOrderbook.addOrder(config);
}

/// A stack of 2 for calculate order deploys.
function testAddOrderRealTwoStackCalculateReverts(address owner, OrderConfig memory config) public {
vm.assume(LibTestAddOrder.conformConfig(config, iDeployer));
(bytes[] memory sources, uint256[] memory constants) =
IParserV1(address(iDeployer)).parse("_ _:block-timestamp() chain-id();:;");
(constants);
config.evaluableConfig.sources = sources;
vm.prank(owner);
iOrderbook.addOrder(config);
}

/// A stack of 3 for calculate order deploys.
function testAddOrderRealThreeStackCalculate(address owner, OrderConfig memory config) public {
vm.assume(LibTestAddOrder.conformConfig(config, iDeployer));
(bytes[] memory sources, uint256[] memory constants) =
IParserV1(address(iDeployer)).parse("_ _ _:block-timestamp() chain-id() block-number();:;");
(constants);
config.evaluableConfig.sources = sources;
vm.prank(owner);
iOrderbook.addOrder(config);
}
}
}
Loading

0 comments on commit 80446f2

Please sign in to comment.