Skip to content

Commit a6ae04a

Browse files
authored
Refactor Blockhash lib (#5702)
1 parent a7d38c7 commit a6ae04a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

contracts/utils/Blockhash.sol

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,21 @@ library Blockhash {
3232
distance = current - blockNumber;
3333
}
3434

35-
return distance > 256 && distance <= 8191 ? _historyStorageCall(blockNumber) : blockhash(blockNumber);
35+
return distance < 257 ? blockhash(blockNumber) : _historyStorageCall(blockNumber);
3636
}
3737

3838
/// @dev Internal function to query the EIP-2935 history storage contract.
3939
function _historyStorageCall(uint256 blockNumber) private view returns (bytes32 hash) {
4040
assembly ("memory-safe") {
41-
mstore(0, blockNumber) // Store the blockNumber in scratch space
41+
// Store the blockNumber in scratch space
42+
mstore(0x00, blockNumber)
43+
mstore(0x20, 0)
4244

43-
// In case the history storage address is not deployed, the call will succeed
44-
// without returndata, so the hash will be 0 just as querying `blockhash` directly.
45-
if and(gt(returndatasize(), 0), staticcall(gas(), HISTORY_STORAGE_ADDRESS, 0, 0x20, 0, 0x20)) {
46-
hash := mload(0)
47-
}
45+
// call history storage address
46+
pop(staticcall(gas(), HISTORY_STORAGE_ADDRESS, 0x00, 0x20, 0x20, 0x20))
47+
48+
// load result
49+
hash := mload(0x20)
4850
}
4951
}
5052
}

0 commit comments

Comments
 (0)