Skip to content

Commit

Permalink
Merge pull request #136 from BeanstalkFarms/libBytes-3-tokens
Browse files Browse the repository at this point in the history
LibBytes fix for 3 tokens
  • Loading branch information
Brean0 authored Aug 14, 2024
2 parents 58ca597 + 4e5e632 commit 0c90c49
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/libraries/LibBytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ library LibBytes {
assembly {
sstore(
add(slot, maxI),
add(mload(add(reserves, add(iByte, 32))), shr(128, shl(128, sload(add(slot, maxI)))))
add(mload(add(reserves, add(iByte, 32))), shl(128, shr(128, sload(add(slot, maxI)))))
)
}
}
Expand Down
14 changes: 9 additions & 5 deletions test/libraries/LibBytes.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ contract LibBytesTest is TestHelper {
bytes32 constant RESERVES_STORAGE_SLOT = bytes32(uint256(keccak256("reserves.storage.slot")) - 1);

/// @dev Store fuzzed reserves, re-read and compare.
// Performs 3 iterations, so it checks that it overwrites the existing value properly in addition to just setting it.
function testFuzz_storeAndRead(uint256 n, uint128[8] memory _reserves) public {
n = bound(n, 0, NUM_RESERVES_MAX);

Expand All @@ -18,12 +19,15 @@ contract LibBytesTest is TestHelper {
for (uint256 i; i < n; i++) {
reserves[i] = uint256(_reserves[i]);
}
LibBytes.storeUint128(RESERVES_STORAGE_SLOT, reserves);

// Re-read reserves and compare
uint256[] memory reserves2 = LibBytes.readUint128(RESERVES_STORAGE_SLOT, n);
for (uint256 i; i < reserves2.length; i++) {
assertEq(reserves2[i], reserves[i], "ByteStorage: reserves mismatch");
for (uint256 i; i < 3; ++i) {
LibBytes.storeUint128(RESERVES_STORAGE_SLOT, reserves);

// Re-read reserves and compare
uint256[] memory reserves2 = LibBytes.readUint128(RESERVES_STORAGE_SLOT, n);
for (uint256 j; j < reserves2.length; j++) {
assertEq(reserves2[j], reserves[j], "ByteStorage: reserves mismatch");
}
}
}

Expand Down

0 comments on commit 0c90c49

Please sign in to comment.