From 80446f2c93d73c64b45704e6bb25dfb16322cb5a Mon Sep 17 00:00:00 2001 From: thedavidmeister Date: Mon, 17 Jul 2023 23:04:25 +0400 Subject: [PATCH] real i9r tests for add order --- .gas-snapshot | 42 +++++++----- lib/rain.interpreter | 2 +- test/concrete/OrderBook.addOrder.mock.t.sol | 64 ++++++----------- test/concrete/OrderBook.addOrder.t.sol | 68 +++++++++++++++++-- .../abstract/OrderBookExternalRealTest.sol | 14 ++-- test/util/lib/LibTestAddOrder.sol | 44 ++++++++++++ 6 files changed, 163 insertions(+), 71 deletions(-) create mode 100644 test/util/lib/LibTestAddOrder.sol diff --git a/.gas-snapshot b/.gas-snapshot index 45f1cb93b..f204c8137 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -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) \ No newline at end of file diff --git a/lib/rain.interpreter b/lib/rain.interpreter index 247140dc9..083250d7d 160000 --- a/lib/rain.interpreter +++ b/lib/rain.interpreter @@ -1 +1 @@ -Subproject commit 247140dc94fd9bcd3e55c8ca62a2cde80efb5f25 +Subproject commit 083250d7d8ebff0cafbd9e59a36dfd48cb7e5421 diff --git a/test/concrete/OrderBook.addOrder.mock.t.sol b/test/concrete/OrderBook.addOrder.mock.t.sol index 3a5f41ef1..a533eee8e 100644 --- a/test/concrete/OrderBook.addOrder.mock.t.sol +++ b/test/concrete/OrderBook.addOrder.mock.t.sol @@ -4,29 +4,11 @@ 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) @@ -34,7 +16,8 @@ contract OrderBookAddOrderMockTest is OrderBookExternalMockTest, IMetaV1 { 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), @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); } @@ -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); @@ -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); @@ -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); diff --git a/test/concrete/OrderBook.addOrder.t.sol b/test/concrete/OrderBook.addOrder.t.sol index bfe61481d..b4994cfd9 100644 --- a/test/concrete/OrderBook.addOrder.t.sol +++ b/test/concrete/OrderBook.addOrder.t.sol @@ -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); } -} \ No newline at end of file +} diff --git a/test/util/abstract/OrderBookExternalRealTest.sol b/test/util/abstract/OrderBookExternalRealTest.sol index a828952f8..1eefde4f2 100644 --- a/test/util/abstract/OrderBookExternalRealTest.sol +++ b/test/util/abstract/OrderBookExternalRealTest.sol @@ -29,17 +29,23 @@ abstract contract OrderBookExternalRealTest is Test, IOrderBookV3Stub { vm.mockCall( address(IERC1820_REGISTRY), abi.encodeWithSelector(IERC1820Registry.interfaceHash.selector), - abi.encode(bytes32(uint256(5)))); - vm.mockCall(address(IERC1820_REGISTRY), abi.encodeWithSelector(IERC1820Registry.setInterfaceImplementer.selector), ""); + abi.encode(bytes32(uint256(5))) + ); + vm.mockCall( + address(IERC1820_REGISTRY), abi.encodeWithSelector(IERC1820Registry.setInterfaceImplementer.selector), "" + ); bytes memory deployerMeta = LibRainterpreterExpressionDeployerNPMeta.authoringMeta(); console2.log("current deployer meta hash:"); console2.logBytes32(keccak256(deployerMeta)); - iDeployer = IExpressionDeployerV1(address( + iDeployer = IExpressionDeployerV1( + address( new RainterpreterExpressionDeployerNP(RainterpreterExpressionDeployerConstructionConfig( address(iInterpreter), address(iStore), deployerMeta - )))); + )) + ) + ); bytes memory orderbookMeta = vm.readFileBinary(ORDER_BOOK_META_PATH); console2.log("orderbook meta hash:"); console2.logBytes(abi.encodePacked(keccak256(orderbookMeta))); diff --git a/test/util/lib/LibTestAddOrder.sol b/test/util/lib/LibTestAddOrder.sol new file mode 100644 index 000000000..47d3920c2 --- /dev/null +++ b/test/util/lib/LibTestAddOrder.sol @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: CAL +pragma solidity ^0.8.19; + +import "rain.metadata/LibMeta.sol"; +import "src/interface/unstable/IOrderBookV3.sol"; +import "src/lib/LibOrder.sol"; +import "src/concrete/OrderBook.sol"; + +library LibTestAddOrder { + /// 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, + IInterpreterV1 interpreter, + IInterpreterStoreV1 store, + address expression + ) internal pure returns (Order memory, bytes32) { + Evaluable memory expectedEvaluable = Evaluable(interpreter, store, 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)); + } + + /// Valid config has a few requirements. Mutates the config in place and + /// returns a bool that indicates whether the config is otherwise valid. The + /// bool should be passed directly to `vm.assume`. + function conformConfig(OrderConfig memory config, IExpressionDeployerV1 deployer) internal pure returns (bool) { + 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); + } + config.evaluableConfig.deployer = deployer; + return config.evaluableConfig.sources.length >= 2 && config.validInputs.length > 0 + && config.validOutputs.length > 0; + } +}