From c4e144447a1d61d4c8157785ff0e8f06bfee8994 Mon Sep 17 00:00:00 2001 From: Ratan Kaliani Date: Wed, 28 Feb 2024 13:05:10 -0800 Subject: [PATCH] fix --- contracts/src/libraries/OutputReader.sol | 13 +- contracts/test/libraries/OutputReader.t.sol | 124 ++++++++++---------- 2 files changed, 72 insertions(+), 65 deletions(-) diff --git a/contracts/src/libraries/OutputReader.sol b/contracts/src/libraries/OutputReader.sol index 376ad452..62d90c1b 100644 --- a/contracts/src/libraries/OutputReader.sol +++ b/contracts/src/libraries/OutputReader.sol @@ -1,5 +1,6 @@ pragma solidity ^0.8.16; +// OutputReader should be used for reading encodePacked output uint256, uint128, uint64, uint32. library OutputReader { function readUint256(bytes memory _output, uint256 _offset) internal @@ -20,7 +21,9 @@ library OutputReader { { uint128 value; assembly { - value := mload(add(add(_output, 0x20), _offset)) + // Reading into u128 only parses the lowest 16 bytes of slot, this is where + // encodePacked writes the uint128. + value := mload(add(add(_output, 0x10), _offset)) } return (_offset + 16, value); } @@ -32,7 +35,9 @@ library OutputReader { { uint64 value; assembly { - value := mload(add(add(_output, 0x20), _offset)) + // Reading into u64 only parses the lowest 8 bytes of slot, this is where + // encodePacked writes the uint64. + value := mload(add(add(_output, 0x08), _offset)) } return (_offset + 8, value); } @@ -44,7 +49,9 @@ library OutputReader { { uint32 value; assembly { - value := mload(add(add(_output, 0x20), _offset)) + // Reading into u32 only parses the lowest 4 bytes of slot, this is where + // encodePacked writes the uint32. + value := mload(add(add(_output, 0x04), _offset)) } return (_offset + 4, value); } diff --git a/contracts/test/libraries/OutputReader.t.sol b/contracts/test/libraries/OutputReader.t.sol index 6f2cf54c..d5a36864 100644 --- a/contracts/test/libraries/OutputReader.t.sol +++ b/contracts/test/libraries/OutputReader.t.sol @@ -75,71 +75,71 @@ contract OutputReaderTest is Test { assertEq(value2, 2); } - // function test_ReadUint64() public { - // bytes memory output = abi.encodePacked(uint128(1)); - // console.logBytes(output); - // uint256 offset = 0; - // uint64 value; - // (offset, value) = OutputReader.readUint64(output, 0); - // console.log("offset", offset); - // console.log("value", value); - // assertEq(offset, 16); - // assertEq(value, 1); - // } + function test_ReadUint64() public { + bytes memory output = abi.encodePacked(uint64(1)); + console.logBytes(output); + uint256 offset = 0; + uint64 value; + (offset, value) = OutputReader.readUint64(output, 0); + console.log("offset", offset); + console.log("value", value); + assertEq(offset, 8); + assertEq(value, 1); + } - // function testFuzz_ReadUint64(uint128 v) public { - // bytes memory output = abi.encodePacked(v); - // uint256 offset = 0; - // uint64 value; - // (offset, value) = OutputReader.readUint64(output, 0); - // assertEq(offset, 16); - // assertEq(value, v); - // } + function testFuzz_ReadUint64(uint64 v) public { + bytes memory output = abi.encodePacked(v); + uint256 offset = 0; + uint64 value; + (offset, value) = OutputReader.readUint64(output, 0); + assertEq(offset, 8); + assertEq(value, v); + } - // function test_ReadUint64Multiple() public { - // bytes memory output = abi.encodePacked(uint128(1), uint128(2)); - // uint256 offset = 0; - // uint64 value1; - // uint64 value2; - // (offset, value1) = OutputReader.readUint64(output, 0); - // assertEq(offset, 16); - // assertEq(value1, 1); - // (offset, value2) = OutputReader.readUint64(output, offset); - // assertEq(offset, 32); - // assertEq(value2, 2); - // } + function test_ReadUint64Multiple() public { + bytes memory output = abi.encodePacked(uint64(1), uint64(2)); + uint256 offset = 0; + uint64 value1; + uint64 value2; + (offset, value1) = OutputReader.readUint64(output, 0); + assertEq(offset, 8); + assertEq(value1, 1); + (offset, value2) = OutputReader.readUint64(output, offset); + assertEq(offset, 16); + assertEq(value2, 2); + } - // function test_ReadUint32() public { - // bytes memory output = abi.encodePacked(uint128(1)); - // console.logBytes(output); - // uint256 offset = 0; - // uint32 value; - // (offset, value) = OutputReader.readUint32(output, 0); - // console.log("offset", offset); - // console.log("value", value); - // assertEq(offset, 16); - // assertEq(value, 1); - // } + function test_ReadUint32() public { + bytes memory output = abi.encodePacked(uint32(1)); + console.logBytes(output); + uint256 offset = 0; + uint32 value; + (offset, value) = OutputReader.readUint32(output, 0); + console.log("offset", offset); + console.log("value", value); + assertEq(offset, 4); + assertEq(value, 1); + } - // function testFuzz_ReadUint32(uint128 v) public { - // bytes memory output = abi.encodePacked(v); - // uint256 offset = 0; - // uint32 value; - // (offset, value) = OutputReader.readUint32(output, 0); - // assertEq(offset, 16); - // assertEq(value, v); - // } + function testFuzz_ReadUint32(uint32 v) public { + bytes memory output = abi.encodePacked(v); + uint256 offset = 0; + uint32 value; + (offset, value) = OutputReader.readUint32(output, 0); + assertEq(offset, 4); + assertEq(value, v); + } - // function test_ReadUint32Multiple() public { - // bytes memory output = abi.encodePacked(uint32(1), uint32(2)); - // uint256 offset = 0; - // uint32 value1; - // uint32 value2; - // (offset, value1) = OutputReader.readUint32(output, 0); - // assertEq(offset, 16); - // assertEq(value1, 1); - // (offset, value2) = OutputReader.readUint32(output, offset); - // assertEq(offset, 32); - // assertEq(value2, 2); - // } + function test_ReadUint32Multiple() public { + bytes memory output = abi.encodePacked(uint32(1), uint32(2)); + uint256 offset = 0; + uint32 value1; + uint32 value2; + (offset, value1) = OutputReader.readUint32(output, 0); + assertEq(offset, 4); + assertEq(value1, 1); + (offset, value2) = OutputReader.readUint32(output, offset); + assertEq(offset, 8); + assertEq(value2, 2); + } }