From 18063377fbc6f77ee45ac1e9feff5e2f01cb471b Mon Sep 17 00:00:00 2001 From: navelbuffle Date: Sat, 4 Jan 2025 17:54:07 +0300 Subject: [PATCH] fix interpeter unit tests --- onchain/src/Push3Interpreter.sol | 6 +- onchain/test/Push3Interpreter.t.sol | 114 ++++++++++++++++------------ 2 files changed, 69 insertions(+), 51 deletions(-) diff --git a/onchain/src/Push3Interpreter.sol b/onchain/src/Push3Interpreter.sol index 9b5e572..c632ce8 100644 --- a/onchain/src/Push3Interpreter.sol +++ b/onchain/src/Push3Interpreter.sol @@ -140,7 +140,7 @@ contract Push3Interpreter { */ function readUint(bytes calldata code, uint32 start, uint256 numBytes) internal pure returns (uint256 word) { uint256 numBits = numBytes * 8; - if (start + bytesNum) > code.length) revert ReadUintOutOfRange(numBits); + if (start + numBytes > code.length) revert ReadUintOutOfRange(numBits); assembly { let buf := mload(0x40) // free memory // copy exactly x bytes from code into buf @@ -341,7 +341,7 @@ contract Push3Interpreter { if (tag == CodeTag.INSTRUCTION) { OpCode op = getOpCode(topDesc); - if (op < OPCODE_INTEGER_OFFSET) { + if (uint8(op) < OPCODE_INTEGER_OFFSET) { // NOOP, do nothing } else if (uint8(op) < OPCODE_BOOL_OFFSET) { @@ -437,7 +437,7 @@ contract Push3Interpreter { finalIntStack[i] = intStack[i]; } - finalBoolStack = new int256[](boolTop); + finalBoolStack = new bool[](boolTop); for (uint256 i = 0; i < boolTop; i++) { finalBoolStack[i] = boolStack[i]; } diff --git a/onchain/test/Push3Interpreter.t.sol b/onchain/test/Push3Interpreter.t.sol index c18526b..110b2a4 100644 --- a/onchain/test/Push3Interpreter.t.sol +++ b/onchain/test/Push3Interpreter.t.sol @@ -23,30 +23,30 @@ contract Push3InterpreterTest is Test { * 3) Calls INTEGER_PLUS * * We'll store everything in a single SUBLIST: - * 0x03 => SUBLIST + * 0x04 => SUBLIST * 0x00 0x0B => subLen=11 * Then the next 11 bytes are: * 0x02 + (00 00 00 0A) => INT_LITERAL(10) * 0x02 + (00 00 00 20) => INT_LITERAL(32) - * 0x01 => INTEGER_PLUS + * 0x05 => INTEGER_PLUS * * So total = 1 + 2 + (1+4 + 1+4 + 1) = 14 bytes - * => 0x03 00 0B 02 00 00 00 0A 02 00 00 00 20 01 + * => 0x04 00 0B 02 00 00 00 0A 02 00 00 00 20 05 */ function test_LiteralsAndPlus() public view { // The final code array (14 bytes): - // [0] = 0x03 + // [0] = 0x04 // [1..2] = 0x000B // [3] = 0x02 // [4..7] = 0x0000000A => 10 // [8] = 0x02 // [9..12] = 0x00000020 => 32 - // [13] = 0x01 => plus + // [13] = 0x05 => plus - bytes memory code = hex"03000B020000000A020000002001"; + bytes memory code = hex"04000B020000000A020000002005"; // Or fully concatenated: - // 0x03 00 0B 02 00 00 00 0A 02 00 00 00 20 01 + // 0x04 00 0B 02 00 00 00 0A 02 00 00 00 20 05 // We'll parse offset=0, length=14 uint256 sublistDesc = interpreter.makeDescriptor( @@ -61,13 +61,15 @@ contract Push3InterpreterTest is Test { initExecStack[0] = sublistDesc; int256[] memory initIntStack = new int256[](0); + bool[] memory initBoolStack = new bool[](0); // Run it ( , , int256[] memory finalIntStack - ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack); + , + ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack, initBoolStack); // Should produce finalIntStack = [42] assertEq(finalIntStack.length, 1, "finalIntStack should have length 1"); @@ -78,25 +80,25 @@ contract Push3InterpreterTest is Test { * @notice Test pushing literal 10, literal 3, then INTEGER_MINUS => (10 - 3) = 7 * * The plan: - * 0x03 => SUBLIST token + * 0x04 => SUBLIST token * 0x00 0x0B => subLen=11 * * Then 11 bytes: * 0x02 + (00 00 00 0A) => INT_LITERAL(10) * 0x02 + (00 00 00 03) => INT_LITERAL(3) - * 0x04 => INTEGER_MINUS (assuming 0x04 is assigned to minus) + * 0x06 => INTEGER_MINUS (assuming 0x06 is assigned to minus) */ function test_Minus() public view { // We'll produce 14 total bytes, same structure as the plus example - // Format: [0x03, 0x000B, 0x02(10), 0x03(3), 0x04 => minus] + // Format: [0x04, 0x000B, 0x02(10), 0x03(3), 0x06 => minus] - bytes memory code = hex"03000B020000000A020000000304"; + bytes memory code = hex"04000B020000000A020000000306"; // Breaking it down: - // 0x03 => SUBLIST + // 0x04 => SUBLIST // 0x00 0x0B => length=11 // 0x02 0000000A => INT_LITERAL(10) // 0x02 00000003 => INT_LITERAL(3) - // 0x04 => INTEGER_MINUS + // 0x06 => INTEGER_MINUS // We'll parse offset=0, length=14 uint256 sublistDesc = interpreter.makeDescriptor( @@ -111,13 +113,15 @@ contract Push3InterpreterTest is Test { initExecStack[0] = sublistDesc; int256[] memory initIntStack = new int256[](0); + bool[] memory initBoolStack = new bool[](0); // Run ( , , int256[] memory finalIntStack - ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack); + , + ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack, initBoolStack); // Check final result => should be [7] assertEq(finalIntStack.length, 1, "finalIntStack should have length 1"); @@ -128,25 +132,25 @@ contract Push3InterpreterTest is Test { * @notice Test pushing literal 10, literal 3, then INTEGER_MULT => (10 * 3) = 30 * * The plan: - * 0x03 => SUBLIST token + * 0x04 => SUBLIST token * 0x00 0x0B => subLen=11 * * Then 11 bytes: * 0x02 + (00 00 00 0A) => INT_LITERAL(10) * 0x02 + (00 00 00 03) => INT_LITERAL(3) - * 0x05 => INTEGER_MULT + * 0x07 => INTEGER_MULT */ function test_Mult() public view { // We'll produce 14 total bytes, same structure as the plus example - // Format: [0x03, 0x000B, 0x02(10), 0x03(3), 0x05 => mul] + // Format: [0x04, 0x000B, 0x02(10), 0x02(3), 0x07 => mul] - bytes memory code = hex"03000B020000000A020000000305"; + bytes memory code = hex"04000B020000000A020000000307"; // Breaking it down: - // 0x03 => SUBLIST + // 0x04 => SUBLIST // 0x00 0x0B => length=11 // 0x02 0000000A => INT_LITERAL(10) // 0x02 00000003 => INT_LITERAL(3) - // 0x05 => INTEGER_MUL + // 0x07 => INTEGER_MUL // We'll parse offset=0, length=14 uint256 sublistDesc = interpreter.makeDescriptor( @@ -161,13 +165,15 @@ contract Push3InterpreterTest is Test { initExecStack[0] = sublistDesc; int256[] memory initIntStack = new int256[](0); + bool[] memory initBoolStack = new bool[](0); // Run ( , , int256[] memory finalIntStack - ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack); + , + ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack, initBoolStack); // Check final result => should be [30] assertEq(finalIntStack.length, 1, "finalIntStack should have length 1"); @@ -176,12 +182,12 @@ contract Push3InterpreterTest is Test { function test_Dup() public view { - bytes memory code = hex"030006020000000A06"; + bytes memory code = hex"040006020000000A08"; uint256 sublistDesc = interpreter.makeDescriptor( Push3Interpreter.CodeTag.SUBLIST, - 0, - 9, + 0, + 9, 0 ); @@ -190,13 +196,15 @@ contract Push3InterpreterTest is Test { initExecStack[0] = sublistDesc; int256[] memory initIntStack = new int256[](0); + bool[] memory initBoolStack = new bool[](0); // Run ( , , int256[] memory finalIntStack - ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack); + , + ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack, initBoolStack); assertEq(finalIntStack.length, 2, "finalIntStack should have length 1"); assertEq(finalIntStack[0], finalIntStack[1], "The top 2 values on the stack should be equal."); @@ -204,12 +212,12 @@ contract Push3InterpreterTest is Test { function test_Pop() public view { - bytes memory code = hex"030006020000000A07"; + bytes memory code = hex"040006020000000A09"; uint256 sublistDesc = interpreter.makeDescriptor( Push3Interpreter.CodeTag.SUBLIST, - 0, - 9, + 0, + 9, 0 ); @@ -218,33 +226,35 @@ contract Push3InterpreterTest is Test { initExecStack[0] = sublistDesc; int256[] memory initIntStack = new int256[](0); + bool[] memory initBoolStack = new bool[](0); // Run ( , , int256[] memory finalIntStack - ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack); + , + ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack, initBoolStack); assertEq(finalIntStack.length, 0, "finalIntStack should be empty"); } /** * @notice Test with an unknown token (0xF0). - * We expect the parser to treat it as "unknown => NOOP" + * We expect the parser to treat it as "unknown => NOOP" * and produce a NOOP instruction descriptor. * We'll confirm finalIntStack remains empty (or unchanged). */ function test_UnknownToken() public view { // We'll create a 5-byte code: - // 0x03 => SUBLIST + // 0x04 => SUBLIST // 0x00 0x01 => length=1 // 0xF0 => unknown token - bytes memory code = hex"030001F0"; + bytes memory code = hex"040001F0"; // We'll parse offset=0, length=4 uint256 sublistDesc = interpreter.makeDescriptor( - Push3Interpreter.CodeTag.SUBLIST, + Push3Interpreter.CodeTag.SUBLIST, 0, // offset 4, // length 0 @@ -255,12 +265,14 @@ contract Push3InterpreterTest is Test { uint256[] memory initExecStack = new uint256[](1); initExecStack[0] = sublistDesc; int256[] memory initIntStack = new int256[](0); + bool[] memory initBoolStack = new bool[](0); ( , , int256[] memory finalIntStack - ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack); + , + ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack, initBoolStack); // finalIntStack should remain empty => no literal was pushed assertEq(finalIntStack.length, 0, "Expected finalIntStack to remain empty with unknown token => NOOP"); @@ -268,18 +280,18 @@ contract Push3InterpreterTest is Test { /** * @notice Test a partial literal: we claim to have 4 bytes, but the code array ends early. - * This should trigger the `else { break; }` branch in parseSublist + * This should trigger the `else { break; }` branch in parseSublist * when trying to read 4 bytes for the INT_LITERAL. */ function test_OutOfRangeLiteral() public view { - // 0x03 => SUBLIST + // 0x04 => SUBLIST // 0x00 0x04 => length=4 // 0x02 => INT_LITERAL token // Then we have only 1 byte left instead of 4 => insufficient // // total = 1 + 2 + 1 + 1 = 5 bytes // The parser tries to read 4 bytes after seeing 0x04, fails, hits break. - bytes memory code = hex"03000402FF"; + bytes memory code = hex"04000402FF"; // parse offset=0, length=5 uint256 sublistDesc = interpreter.makeDescriptor( @@ -293,12 +305,14 @@ contract Push3InterpreterTest is Test { uint256[] memory initExecStack = new uint256[](1); initExecStack[0] = sublistDesc; int256[] memory initIntStack = new int256[](0); + bool[] memory initBoolStack = new bool[](0); ( , , int256[] memory finalIntStack - ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack); + , + ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack, initBoolStack); // Because the parse breaks early, we expect finalIntStack is empty or unchanged assertEq(finalIntStack.length, 0, "Expected parse to break on out-of-range literal => no ints pushed"); @@ -310,12 +324,12 @@ contract Push3InterpreterTest is Test { * In the main loop, it sees intTop < 2 => it does nothing. */ function test_NotEnoughIntArgs() public view { - bytes memory code = hex"030006020000000305"; + bytes memory code = hex"040006020000000307"; // Breaking it down: - // 0x03 => SUBLIST + // 0x04 => SUBLIST // 0x00 0x06 => length=6 // 0x02 00000003 => INT_LITERAL(3) - // 0x05 => INTEGER_MUL + // 0x07 => INTEGER_MUL uint256 sublistDesc = interpreter.makeDescriptor( Push3Interpreter.CodeTag.SUBLIST, @@ -329,14 +343,16 @@ contract Push3InterpreterTest is Test { uint256[] memory initExecStack = new uint256[](1); initExecStack[0] = sublistDesc; int256[] memory initIntStack = new int256[](0); + bool[] memory initBoolStack = new bool[](0); ( , , int256[] memory finalIntStack - ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack); + , + ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack, initBoolStack); - // We expect finalIntStack => [5], + // We expect finalIntStack => [5], // because the PLUS instruction sees only 1 int => does nothing => 5 remains assertEq(finalIntStack.length, 1, "Should have 1 int left"); assertEq(finalIntStack[0], 3, "Expected stack top=3 after insufficient-args plus"); @@ -344,12 +360,12 @@ contract Push3InterpreterTest is Test { /** * @notice Test sublist length overflow: - * subLen claims bigger than the code array. Should trigger else { break; } + * subLen claims bigger than the code array. Should trigger else { break; } * in the sublist parsing for that sublist. */ function test_SublistLengthOverflow() public view { - - bytes memory code = hex"050010FFFFFF"; + + bytes memory code = hex"050010FFFFFF"; uint256 sublistDesc = interpreter.makeDescriptor( Push3Interpreter.CodeTag.SUBLIST, @@ -363,12 +379,14 @@ contract Push3InterpreterTest is Test { uint256[] memory initExecStack = new uint256[](1); initExecStack[0] = sublistDesc; int256[] memory initIntStack = new int256[](0); + bool[] memory initBoolStack = new bool[](0); ( , , int256[] memory finalIntStack - ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack); + , + ) = interpreter.runInterpreter(code, initCodeStack, initExecStack, initIntStack, initBoolStack); // Expect no items pushed => finalIntStack empty assertEq(finalIntStack.length, 0, "Expected empty stack if sublist length is out of range => parse break");