Skip to content

Commit

Permalink
test(extsload): add generic test
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Jul 25, 2023
1 parent 651d362 commit 8042549
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ contract Blue {
function extsload(bytes32[] calldata slots) external view returns (bytes32[] memory res) {
uint256 nSlots = slots.length;

res = new bytes32[]( nSlots);
res = new bytes32[](nSlots);

for (uint256 i; i < nSlots;) {
bytes32 slot = slots[i++];
Expand Down
35 changes: 11 additions & 24 deletions test/forge/Blue.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -686,33 +686,20 @@ contract BlueTest is Test {
vm.stopPrank();
}

function testExtSLoad(uint256 amount, address onBehalf) public {
vm.assume(onBehalf != address(blue));
amount = bound(amount, 1, 2 ** 64);

borrowableAsset.setBalance(address(this), amount);
blue.supply(market, amount, onBehalf);

bytes32[] memory supplyShareSlot = new bytes32[](1);
bytes32[] memory totalSupplySlot = new bytes32[](1);
bytes32[] memory totalSupplyShareSlot = new bytes32[](1);

supplyShareSlot[0] = keccak256(abi.encode(onBehalf, keccak256(abi.encode(id, 2))));
totalSupplySlot[0] = keccak256(abi.encode(id, 5));
totalSupplyShareSlot[0] = keccak256(abi.encode(id, 6));

bytes32[] memory supplyShare = blue.extsload(supplyShareSlot);
bytes32[] memory totalSupply = blue.extsload(totalSupplySlot);
bytes32[] memory totalSupplyShare = blue.extsload(totalSupplyShareSlot);
function testExtsLoad(uint256 slot, bytes32 value0) public {
bytes32[] memory slots = new bytes32[](2);
slots[0] = bytes32(slot);
slots[1] = bytes32(slot / 2);

assertEq(supplyShare.length, 1, "supplyShare.length");
assertEq(uint256(supplyShare[0]), 1e18, "supplyShare");
bytes32 value1 = keccak256(abi.encode(value0));
vm.store(address(blue), slots[0], value0);
vm.store(address(blue), slots[1], value1);

assertEq(totalSupply.length, 1, "totalSupply.length");
assertEq(uint256(totalSupply[0]), amount, "totalSupply");
bytes32[] memory values = blue.extsload(slots);

assertEq(totalSupplyShare.length, 1, "totalSupplyShare.length");
assertEq(uint256(totalSupplyShare[0]), 1e18, "totalSupplyShare");
assertEq(values.length, 2, "values.length");
assertEq(values[0], slot > 0 ? value0 : value1, "value0");
assertEq(values[1], value1, "value1");
}
}

Expand Down

0 comments on commit 8042549

Please sign in to comment.