From 949f9bd7c8b113510ef3daf3a8b67149bfb5fc26 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Tue, 27 May 2025 10:58:26 +0200 Subject: [PATCH 1/3] Refactor Blockhash lib --- contracts/utils/Blockhash.sol | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/contracts/utils/Blockhash.sol b/contracts/utils/Blockhash.sol index cb16cd63433..253501c7cc8 100644 --- a/contracts/utils/Blockhash.sol +++ b/contracts/utils/Blockhash.sol @@ -32,19 +32,21 @@ library Blockhash { distance = current - blockNumber; } - return distance > 256 && distance <= 8191 ? _historyStorageCall(blockNumber) : blockhash(blockNumber); + return distance > 256 && distance < 8192 ? _historyStorageCall(blockNumber) : blockhash(blockNumber); } /// @dev Internal function to query the EIP-2935 history storage contract. function _historyStorageCall(uint256 blockNumber) private view returns (bytes32 hash) { 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 - // without returndata, so the hash will be 0 just as querying `blockhash` directly. - if and(gt(returndatasize(), 0), staticcall(gas(), HISTORY_STORAGE_ADDRESS, 0, 0x20, 0, 0x20)) { - hash := mload(0) - } + // call history storage contract + pop(staticcall(gas(), HISTORY_STORAGE_ADDRESS, 0x00, 0x20, 0x20, 0x20)) + + // load result + hash := mload(0x20) } } } From 752bc6f4277faf00596c246f02cf861af85ff096 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Tue, 27 May 2025 11:07:42 +0200 Subject: [PATCH 2/3] Update contracts/utils/Blockhash.sol --- contracts/utils/Blockhash.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/utils/Blockhash.sol b/contracts/utils/Blockhash.sol index 253501c7cc8..8439a90396f 100644 --- a/contracts/utils/Blockhash.sol +++ b/contracts/utils/Blockhash.sol @@ -42,7 +42,7 @@ library Blockhash { mstore(0x00, blockNumber) mstore(0x20, 0) - // call history storage contract + // call history storage address pop(staticcall(gas(), HISTORY_STORAGE_ADDRESS, 0x00, 0x20, 0x20, 0x20)) // load result From da5c867beaf99223fa6880595575640d9943a9ad Mon Sep 17 00:00:00 2001 From: ernestognw Date: Thu, 29 May 2025 08:56:44 -0600 Subject: [PATCH 3/3] up --- contracts/utils/Blockhash.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/utils/Blockhash.sol b/contracts/utils/Blockhash.sol index 8439a90396f..a4a7cb0034c 100644 --- a/contracts/utils/Blockhash.sol +++ b/contracts/utils/Blockhash.sol @@ -32,7 +32,7 @@ library Blockhash { distance = current - blockNumber; } - return distance > 256 && distance < 8192 ? _historyStorageCall(blockNumber) : blockhash(blockNumber); + return distance < 257 ? blockhash(blockNumber) : _historyStorageCall(blockNumber); } /// @dev Internal function to query the EIP-2935 history storage contract.