From 27379f51bf7346cdb9ae9ac1ed16b593f54aa76e Mon Sep 17 00:00:00 2001 From: Erik Dubovyk Date: Wed, 31 Jul 2024 02:29:20 +0300 Subject: [PATCH] added abstract mock interpreter and constants --- test/abstract/InterpreterMockTest.sol | 41 +++++++++++++++++++++++++++ test/abstract/TestConstants.sol | 14 +++++++++ 2 files changed, 55 insertions(+) create mode 100644 test/abstract/InterpreterMockTest.sol create mode 100644 test/abstract/TestConstants.sol diff --git a/test/abstract/InterpreterMockTest.sol b/test/abstract/InterpreterMockTest.sol new file mode 100644 index 00000000..72e8d2cc --- /dev/null +++ b/test/abstract/InterpreterMockTest.sol @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: CAL +pragma solidity ^0.8.18; + +import {Test, Vm} from "forge-std/Test.sol"; +import {REVERTING_MOCK_BYTECODE} from "./TestConstants.sol"; +import {IInterpreterStoreV2} from "rain.interpreter.interface/interface/IInterpreterStoreV2.sol"; +import {IInterpreterV2} from "rain.interpreter.interface/interface/IInterpreterV2.sol"; +import {IExpressionDeployerV3} from "rain.interpreter.interface/interface/IExpressionDeployerV3.sol"; + +abstract contract InterpreterMockTest is Test { + IInterpreterV2 internal immutable iInterpreter; + IInterpreterStoreV2 internal immutable iStore; + IExpressionDeployerV3 internal immutable iDeployer; + + constructor() { + vm.pauseGasMetering(); + iInterpreter = IInterpreterV2(address(uint160(uint256(keccak256("interpreter.rain.test"))))); + vm.etch(address(iInterpreter), REVERTING_MOCK_BYTECODE); + + iStore = IInterpreterStoreV2(address(uint160(uint256(keccak256("store.rain.test"))))); + vm.etch(address(iStore), REVERTING_MOCK_BYTECODE); + + iDeployer = IExpressionDeployerV3(address(uint160(uint256(keccak256("deployer.rain.test"))))); + vm.etch(address(iDeployer), REVERTING_MOCK_BYTECODE); + vm.resumeGasMetering(); + } + + function interpreterEval2MockCall(uint256[] memory stack, uint256[] memory writes) public { + vm.mockCall( + address(iInterpreter), abi.encodeWithSelector(IInterpreterV2.eval2.selector), abi.encode(stack, writes) + ); + } + + function expressionDeployerDeployExpression2MockCall(address expression, bytes memory io) public { + vm.mockCall( + address(iDeployer), + abi.encodeWithSelector(IExpressionDeployerV3.deployExpression2.selector), + abi.encode(iInterpreter, iStore, expression, io) + ); + } +} diff --git a/test/abstract/TestConstants.sol b/test/abstract/TestConstants.sol new file mode 100644 index 00000000..340dfce4 --- /dev/null +++ b/test/abstract/TestConstants.sol @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: CAL +pragma solidity ^0.8.18; + +/// @dev Mocks need to be etched with some bytecode or they cannot even be +/// called. This is because Solidity first checks the bytecode size before +/// calling, so it never even gets to the point that mocking logic can intercept +/// the call. We want all non-mocked calls to revert, so all mocks should be +/// etched with a revert opcode. +bytes constant REVERTING_MOCK_BYTECODE = hex"FD"; + +/// @dev Stub expression bytecode used for testing purposes. +/// This is a simple bytecode stub that can be used as a placeholder for +/// expressions with mock initialization in tests. The bytecode is arbitrary +bytes constant STUB_EXPRESSION_BYTECODE = hex"010000";