Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(extsload): expose generic storage getter #147

Merged
merged 3 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,21 @@ contract Blue {

return collateralValue.mulWadDown(market.lltv) >= borrowValue;
}

// Storage view.

function extsload(bytes32[] calldata slots) external view returns (bytes32[] memory res) {
uint256 nSlots = slots.length;

res = new bytes32[](nSlots);

for (uint256 i; i < nSlots;) {
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
bytes32 slot = slots[i++];

/// @solidity memory-safe-assembly
assembly {
mstore(add(res, mul(i, 32)), sload(slot))
}
}
}
}
16 changes: 16 additions & 0 deletions test/forge/Blue.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,22 @@ contract BlueTest is Test {

vm.stopPrank();
}

function testExtsLoad(uint256 slot, bytes32 value0) public {
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
bytes32[] memory slots = new bytes32[](2);
slots[0] = bytes32(slot);
slots[1] = bytes32(slot / 2);

bytes32 value1 = keccak256(abi.encode(value0));
vm.store(address(blue), slots[0], value0);
vm.store(address(blue), slots[1], value1);

bytes32[] memory values = blue.extsload(slots);

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

function neq(Market memory a, Market memory b) pure returns (bool) {
Expand Down