Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidmeister committed Aug 23, 2024
1 parent 2485a11 commit 0a2e48e
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 92 deletions.
3 changes: 2 additions & 1 deletion src/concrete/extern/RainterpreterReferenceExternNPE2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
OPCODE_FUNCTION_POINTERS
} from "../../generated/RainterpreterReferenceExternNPE2.pointers.sol";
import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol";
import {console2} from "forge-std/Test.sol";

/// @dev The number of subparser functions available to the parser. This is NOT
/// 1:1 with the number of opcodes provided by the extern component of this
Expand Down Expand Up @@ -253,7 +254,7 @@ contract RainterpreterReferenceExternNPE2 is BaseRainterpreterSubParserNPE2, Bas
state, cursor + SUB_PARSER_LITERAL_REPEAT_KEYWORD_BYTES_LENGTH, end
);
// We can only repeat a single digit.
if (LibDecimalFloat.lt(signedCoefficient, exponent, 9, 0)) {
if (LibDecimalFloat.gt(signedCoefficient, exponent, 9, 0)) {
revert InvalidRepeatCount();
}

Expand Down
2 changes: 1 addition & 1 deletion src/error/ErrParse.sol
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,4 @@ error BadSubParserResult(bytes bytecode);
error OpcodeIOOverflow(uint256 offset);

/// Thrown when an operand value is larger than the maximum allowed.
error OperandOverflow();
error OperandOverflow();
3 changes: 1 addition & 2 deletions src/generated/RainterpreterNPE2.pointers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ bytes32 constant BYTECODE_HASH = bytes32(0xb2bb360cac3a5504625d94621550913d5024d
/// By setting these as a constant they can be inlined into the interpreter
/// and loaded at eval time for very low gas (~100) due to the compiler
/// optimising it to a single `codecopy` to build the in memory bytes array.
bytes constant OPCODE_FUNCTION_POINTERS =
hex"06b407040746091209f90a0b0a1d0a360a780aca0adb0aec0b8e0bcb0c7a0cfe0d4d0e43";
bytes constant OPCODE_FUNCTION_POINTERS = hex"06b407040746091209f90a0b0a1d0a360a780aca0adb0aec0b8e0bcb0c7a0cfe0d4d0e43";
8 changes: 4 additions & 4 deletions src/generated/RainterpreterReferenceExternNPE2.pointers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
pragma solidity =0.8.25;

/// @dev Hash of the known bytecode.
bytes32 constant BYTECODE_HASH = bytes32(0xa6f1f65cad3f9e2d59cbe0234b3d684442e6152a23f7966772d1bc21270e4ccf);
bytes32 constant BYTECODE_HASH = bytes32(0xbccd8c9958e6b017765d7f08f8e30d9b0deaa8062599680e7f37776d052c7638);

/// @dev The hash of the meta that describes the contract.
bytes32 constant DESCRIBED_BY_META_HASH = bytes32(0xadf71693c6ecf3fd560904bc46973d1b6e651440d15366673f9b3984749e7c16);
Expand Down Expand Up @@ -47,17 +47,17 @@ bytes constant SUB_PARSER_WORD_PARSERS = hex"0775079707a607b607c7";
/// @dev Every two bytes is a function pointer for an operand handler.
/// These positional indexes all map to the same indexes looked up in the parse
/// meta.
bytes constant OPERAND_HANDLER_FUNCTION_POINTERS = hex"0a370a7c0a370a370a37";
bytes constant OPERAND_HANDLER_FUNCTION_POINTERS = hex"0abf0b040abf0abf0abf";

/// @dev Every two bytes is a function pointer for a literal parser.
/// Literal dispatches are determined by the first byte(s) of the literal
/// rather than a full word lookup, and are done with simple conditional
/// jumps as the possibilities are limited compared to the number of words we
/// have.
bytes constant LITERAL_PARSER_FUNCTION_POINTERS = hex"0a06";
bytes constant LITERAL_PARSER_FUNCTION_POINTERS = hex"0a8e";

/// @dev The function pointers for the integrity check fns.
bytes constant INTEGRITY_FUNCTION_POINTERS = hex"08ad";
bytes constant INTEGRITY_FUNCTION_POINTERS = hex"0935";

/// @dev The function pointers known to the interpreter for dynamic dispatch.
/// By setting these as a constant they can be inlined into the interpreter
Expand Down
5 changes: 4 additions & 1 deletion src/lib/extern/reference/op/LibExternOpIntIncNPE2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.25;
import {Operand} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";
import {LibSubParse} from "../../../parse/LibSubParse.sol";
import {IInterpreterExternV3} from "rain.interpreter.interface/interface/IInterpreterExternV3.sol";
import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol";

/// @dev Opcode index of the extern increment opcode. Needs to be manually kept
/// in sync with the extern opcode function pointers. Definitely write tests for
Expand All @@ -20,7 +21,9 @@ library LibExternOpIntIncNPE2 {
//slither-disable-next-line dead-code
function run(Operand, uint256[] memory inputs) internal pure returns (uint256[] memory) {
for (uint256 i = 0; i < inputs.length; i++) {
++inputs[i];
(int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(inputs[i]);
(signedCoefficient, exponent) = LibDecimalFloat.add(signedCoefficient, exponent, 1e37, -37);
inputs[i] = LibDecimalFloat.pack(signedCoefficient, exponent);
}
return inputs;
}
Expand Down
15 changes: 8 additions & 7 deletions test/src/concrete/RainterpreterNPE2.stateOverlay.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import {RainterpreterExpressionDeployerNPE2DeploymentTest} from
import {FullyQualifiedNamespace, StateNamespace} from "rain.interpreter.interface/interface/IInterpreterStoreV2.sol";
import {EvalV4, SourceIndexV2} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";
import {LibNamespace} from "rain.interpreter.interface/lib/ns/LibNamespace.sol";
import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol";

contract RainterpreterNPE2StateOverlayTest is RainterpreterExpressionDeployerNPE2DeploymentTest {
/// Show that state overlay can prewarm a get.
function testStateOverlayGet() external {
function testStateOverlayGet() external view {
bytes memory bytecode = iDeployer.parse2("_: get(9);");

uint256 k = 9e18;
uint256 k = LibDecimalFloat.pack(9e37, -37);
uint256 v = 42;
uint256[] memory stateOverlay = new uint256[](2);
stateOverlay[0] = k;
Expand All @@ -38,11 +39,11 @@ contract RainterpreterNPE2StateOverlayTest is RainterpreterExpressionDeployerNPE
}

/// Show that state overlay can be overridden by a set in the bytecode.
function testStateOverlaySet() external {
function testStateOverlaySet() external view {
bytes memory bytecode = iDeployer.parse2("_:get(9),:set(9 42),_:get(9);");

uint256 k = 9e18;
uint256 v = 43e18;
uint256 k = LibDecimalFloat.pack(9e37, -37);
uint256 v = LibDecimalFloat.pack(43e37, -37);
uint256[] memory stateOverlay = new uint256[](2);
stateOverlay[0] = k;
stateOverlay[1] = v;
Expand All @@ -60,10 +61,10 @@ contract RainterpreterNPE2StateOverlayTest is RainterpreterExpressionDeployerNPE
);

assertEq(stack.length, 2);
assertEq(stack[0], 42e18);
assertEq(stack[0], LibDecimalFloat.pack(42e37, -37));
assertEq(stack[1], v);
assertEq(kvs.length, 2);
assertEq(kvs[0], k);
assertEq(kvs[1], 42e18);
assertEq(kvs[1], LibDecimalFloat.pack(42e37, -37));
}
}
15 changes: 7 additions & 8 deletions test/src/concrete/RainterpreterReferenceExternNPE2.intInc.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {Strings} from "openzeppelin-contracts/contracts/utils/Strings.sol";
import {COMPATIBILITY_V4} from "rain.interpreter.interface/interface/ISubParserV3.sol";
import {OPCODE_EXTERN} from "rain.interpreter.interface/interface/unstable/IInterpreterV4.sol";
import {ExternDispatchConstantsHeightOverflow} from "src/error/ErrSubParse.sol";
import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol";

contract RainterpreterReferenceExternNPE2IntIncTest is OpTest {
using Strings for address;
Expand All @@ -38,15 +39,15 @@ contract RainterpreterReferenceExternNPE2IntIncTest is OpTest {
);

uint256[] memory expectedStack = new uint256[](3);
expectedStack[0] = 4;
expectedStack[1] = 3;
expectedStack[0] = LibDecimalFloat.pack(4e37, -37);
expectedStack[1] = LibDecimalFloat.pack(3e37, -37);
expectedStack[2] = EncodedExternDispatch.unwrap(encodedExternDispatch);

checkHappy(
// Need the constant in the constant array to be indexable in the operand.
"_: 0x000000000000000000000000c7183455a4c133ae270771860664b6b7ec320bb1,"
// Operand is the constant index of the dispatch.
"three four: extern<0>(2e-18 3e-18);",
"three four: extern<0>(2 3);",
expectedStack,
"inc 2 3 = 3 4"
);
Expand All @@ -56,14 +57,12 @@ contract RainterpreterReferenceExternNPE2IntIncTest is OpTest {
RainterpreterReferenceExternNPE2 extern = new RainterpreterReferenceExternNPE2();

uint256[] memory expectedStack = new uint256[](2);
expectedStack[0] = 4;
expectedStack[1] = 3;
expectedStack[0] = LibDecimalFloat.pack(4e37, -37);
expectedStack[1] = LibDecimalFloat.pack(3e37, -37);

checkHappy(
bytes(
string.concat(
"using-words-from ", address(extern).toHexString(), " three four: ref-extern-inc(2e-18 3e-18);"
)
string.concat("using-words-from ", address(extern).toHexString(), " three four: ref-extern-inc(2 3);")
),
expectedStack,
"sugared inc 2 3 = 3 4"
Expand Down
5 changes: 3 additions & 2 deletions test/src/lib/op/00/LibOpConstantNP.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {IInterpreterStoreV2} from "rain.interpreter.interface/interface/IInterpr
import {SignedContextV1} from "rain.interpreter.interface/interface/IInterpreterCallerV3.sol";
import {LibOperand} from "test/lib/operand/LibOperand.sol";
import {BadOpOutputsLength} from "src/error/ErrIntegrity.sol";
import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol";

/// @title LibOpConstantNPTest
/// @notice Test the runtime and integrity time logic of LibOpConstantNP.
Expand Down Expand Up @@ -95,8 +96,8 @@ contract LibOpConstantNPTest is OpTest {
})
);
assertEq(stack.length, 2);
assertEq(stack[0], 1001e15);
assertEq(stack[1], 2e18);
assertEq(stack[0], LibDecimalFloat.pack(1.001e37, -37));
assertEq(stack[1], LibDecimalFloat.pack(2e37, -37));
assertEq(kvs.length, 0);
}

Expand Down
31 changes: 16 additions & 15 deletions test/src/lib/op/00/LibOpExternNP.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import {IERC165} from "openzeppelin-contracts/contracts/utils/introspection/IERC165.sol";
import {LibOperand} from "test/lib/operand/LibOperand.sol";
import {LibUint256Array} from "rain.solmem/lib/LibUint256Array.sol";
import {LibDecimalFloat} from "rain.math.float/src/lib/LibDecimalFloat.sol";

/// @title LibOpExternNPTest
/// @notice Test the runtime and integrity time logic of LibOpExternNP.
Expand Down Expand Up @@ -211,26 +212,26 @@ contract LibOpExternNPTest is OpTest {
);

uint256[] memory externInputs = new uint256[](2);
externInputs[0] = 20;
externInputs[1] = 83;
externInputs[0] = LibDecimalFloat.pack(20e36, -36);
externInputs[1] = LibDecimalFloat.pack(83e36, -36);
uint256[] memory externOutputs = new uint256[](1);
externOutputs[0] = 99;
externOutputs[0] = LibDecimalFloat.pack(99e36, -36);
vm.mockCall(
address(extern),
abi.encodeWithSelector(IInterpreterExternV3.extern.selector, externDispatch, externInputs),
abi.encode(externOutputs)
);

uint256[] memory expectedStack = new uint256[](2);
expectedStack[0] = 99;
expectedStack[0] = LibDecimalFloat.pack(99e36, -36);
expectedStack[1] = 0x00000000000000000005001000000000000000000000000000000000deadbeef;

checkHappy(
// Need the constant in the constant array to be indexable in the operand.
"_: 0x00000000000000000005001000000000000000000000000000000000deadbeef,"
// Operand is the constant index of the dispatch then the number of outputs.
// 2 inputs and 1 output matches the mocked integrity check.
"_: extern<0>(20e-18 83e-18);",
"_: extern<0>(20 83);",
expectedStack,
"0xdeadbeef 20 83 99"
);
Expand Down Expand Up @@ -264,19 +265,19 @@ contract LibOpExternNPTest is OpTest {
);

uint256[] memory externInputs = new uint256[](3);
externInputs[0] = 1;
externInputs[1] = 2;
externInputs[2] = 3;
externInputs[0] = LibDecimalFloat.pack(1e37, -37);
externInputs[1] = LibDecimalFloat.pack(2e37, -37);
externInputs[2] = LibDecimalFloat.pack(3e37, -37);

uint256[] memory externOutputs = new uint256[](3);
externOutputs[0] = 4;
externOutputs[1] = 5;
externOutputs[2] = 6;
externOutputs[0] = LibDecimalFloat.pack(4e37, -37);
externOutputs[1] = LibDecimalFloat.pack(5e37, -37);
externOutputs[2] = LibDecimalFloat.pack(6e37, -37);

uint256[] memory expectedStack = new uint256[](4);
expectedStack[0] = 6;
expectedStack[1] = 5;
expectedStack[2] = 4;
expectedStack[0] = LibDecimalFloat.pack(6e37, -37);
expectedStack[1] = LibDecimalFloat.pack(5e37, -37);
expectedStack[2] = LibDecimalFloat.pack(4e37, -37);
expectedStack[3] = 0x00000000000000000005001000000000000000000000000000000000deadbeef;

vm.mockCall(
Expand All @@ -295,7 +296,7 @@ contract LibOpExternNPTest is OpTest {
"_: 0x00000000000000000005001000000000000000000000000000000000deadbeef,"
// Operand is the constant index of the dispatch then the number of outputs.
// 3 inputs and 3 outputs matches the mocked integrity check.
"four five six: extern<0>(1e-18 2e-18 3e-18);",
"four five six: extern<0>(1 2 3);",
expectedStack,
"0xdeadbeef 1 2 3 4 5 6"
);
Expand Down
4 changes: 1 addition & 3 deletions test/src/lib/op/bitwise/LibOpShiftBitsLeftNP.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ contract LibOpShiftBitsLeftNPTest is OpTest {
"_: bitwise-shift-left<65535>(0);", abi.encodeWithSelector(UnsupportedBitwiseShiftAmount.selector, 65535)
);
// Lets go ahead and overflow the operand.
checkUnhappyParse(
"_: bitwise-shift-left<65536>(0);", abi.encodeWithSelector(OperandOverflow.selector)
);
checkUnhappyParse("_: bitwise-shift-left<65536>(0);", abi.encodeWithSelector(OperandOverflow.selector));
}
}
4 changes: 1 addition & 3 deletions test/src/lib/op/bitwise/LibOpShiftBitsRightNP.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ contract LibOpShiftBitsRightNPTest is OpTest {
"_: bitwise-shift-right<65535>(0);", abi.encodeWithSelector(UnsupportedBitwiseShiftAmount.selector, 65535)
);
// Lets go ahead and overflow the operand.
checkUnhappyParse(
"_: bitwise-shift-right<65536>(0);", abi.encodeWithSelector(OperandOverflow.selector)
);
checkUnhappyParse("_: bitwise-shift-right<65536>(0);", abi.encodeWithSelector(OperandOverflow.selector));
}
}
18 changes: 3 additions & 15 deletions test/src/lib/parse/LibParseOperand.handleOperand8M1M1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ contract LibParseOperandHandleOperand8M1M1Test is Test {

uint256[] memory values = new uint256[](1);
values[0] = value;
vm.expectRevert(
abi.encodeWithSelector(
OperandOverflow.selector
)
);
vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector));
LibParseOperand.handleOperand8M1M1(values);
}

Expand Down Expand Up @@ -68,11 +64,7 @@ contract LibParseOperandHandleOperand8M1M1Test is Test {
uint256[] memory values = new uint256[](2);
values[0] = a;
values[1] = b;
vm.expectRevert(
abi.encodeWithSelector(
OperandOverflow.selector
)
);
vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector));
LibParseOperand.handleOperand8M1M1(values);
}

Expand Down Expand Up @@ -106,11 +98,7 @@ contract LibParseOperandHandleOperand8M1M1Test is Test {
values[0] = a;
values[1] = b;
values[2] = c;
vm.expectRevert(
abi.encodeWithSelector(
OperandOverflow.selector
)
);
vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector));
LibParseOperand.handleOperand8M1M1(values);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ contract LibParseOperandHandleOperandDoublePerByteNoDefaultTest is Test {
values[0] = a;
values[1] = b;

vm.expectRevert(
abi.encodeWithSelector(
OperandOverflow.selector
)
);
vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector));
LibParseOperand.handleOperandDoublePerByteNoDefault(values);
}

Expand All @@ -67,11 +63,7 @@ contract LibParseOperandHandleOperandDoublePerByteNoDefaultTest is Test {
uint256[] memory values = new uint256[](2);
values[0] = a;
values[1] = b;
vm.expectRevert(
abi.encodeWithSelector(
OperandOverflow.selector
)
);
vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector));
LibParseOperand.handleOperandDoublePerByteNoDefault(values);
}

Expand Down
12 changes: 2 additions & 10 deletions test/src/lib/parse/LibParseOperand.handleOperandM1M1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ contract LibParseOperandHandleOperandM1M1Test is Test {

uint256[] memory values = new uint256[](1);
values[0] = value;
vm.expectRevert(
abi.encodeWithSelector(
OperandOverflow.selector
)
);
vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector));
LibParseOperand.handleOperandM1M1(values);
}

Expand Down Expand Up @@ -67,11 +63,7 @@ contract LibParseOperandHandleOperandM1M1Test is Test {
uint256[] memory values = new uint256[](2);
values[0] = a;
values[1] = b;
vm.expectRevert(
abi.encodeWithSelector(
OperandOverflow.selector
)
);
vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector));
LibParseOperand.handleOperandM1M1(values);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ contract LibParseOperandHandleOperandSingleFullTest is Test {
value *= 1e18;
uint256[] memory values = new uint256[](1);
values[0] = value;
vm.expectRevert(
abi.encodeWithSelector(
OperandOverflow.selector
)
);
vm.expectRevert(abi.encodeWithSelector(OperandOverflow.selector));
LibParseOperand.handleOperandSingleFull(values);
}

Expand Down
Loading

0 comments on commit 0a2e48e

Please sign in to comment.