Refactor Blockhash lib (#5702)
This commit is contained in:
@ -32,19 +32,21 @@ library Blockhash {
|
|||||||
distance = current - blockNumber;
|
distance = current - blockNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
return distance > 256 && distance <= 8191 ? _historyStorageCall(blockNumber) : blockhash(blockNumber);
|
return distance < 257 ? blockhash(blockNumber) : _historyStorageCall(blockNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @dev Internal function to query the EIP-2935 history storage contract.
|
/// @dev Internal function to query the EIP-2935 history storage contract.
|
||||||
function _historyStorageCall(uint256 blockNumber) private view returns (bytes32 hash) {
|
function _historyStorageCall(uint256 blockNumber) private view returns (bytes32 hash) {
|
||||||
assembly ("memory-safe") {
|
assembly ("memory-safe") {
|
||||||
mstore(0, blockNumber) // Store the blockNumber in scratch space
|
// Store the blockNumber in scratch space
|
||||||
|
mstore(0x00, blockNumber)
|
||||||
|
mstore(0x20, 0)
|
||||||
|
|
||||||
// In case the history storage address is not deployed, the call will succeed
|
// call history storage address
|
||||||
// without returndata, so the hash will be 0 just as querying `blockhash` directly.
|
pop(staticcall(gas(), HISTORY_STORAGE_ADDRESS, 0x00, 0x20, 0x20, 0x20))
|
||||||
if and(gt(returndatasize(), 0), staticcall(gas(), HISTORY_STORAGE_ADDRESS, 0, 0x20, 0, 0x20)) {
|
|
||||||
hash := mload(0)
|
// load result
|
||||||
}
|
hash := mload(0x20)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user