Skip to content

Commit

Permalink
added abstract mock interpreter and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Erikd-dev committed Jul 30, 2024
1 parent c6f1949 commit 27379f5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
41 changes: 41 additions & 0 deletions test/abstract/InterpreterMockTest.sol
Original file line number Diff line number Diff line change
@@ -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)
);
}
}
14 changes: 14 additions & 0 deletions test/abstract/TestConstants.sol
Original file line number Diff line number Diff line change
@@ -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";

0 comments on commit 27379f5

Please sign in to comment.