Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ratankaliani committed Feb 28, 2024
1 parent a15e28f commit c4e1444
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 65 deletions.
13 changes: 10 additions & 3 deletions contracts/src/libraries/OutputReader.sol
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
124 changes: 62 additions & 62 deletions contracts/test/libraries/OutputReader.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit c4e1444

Please sign in to comment.