Bug Report: Inconsistent Historical Block Hash Retrieval On Smart Contracts
Summary
The blockhash() function is only returning the hash for the immediate parent of the current block, while returning 0x0000000000000000000000000000000000000000000000000000000000000000 for other recent block numbers within the expected range of 256 blocks. The issue was identified when attempting to retrieve the previous 10 block hashes via a smart contract.
Deploy a smart contract on the Sei Network that attempts to retrieve the previous 10 block hashes using the blockhash() function.
Call the contract's function to retrieve and display the block hashes.
Observe the returned values for block hashes.
Expected Behavior
The blockhash() function should correctly return the hashes for the previous 10 blocks, including the immediate parent and preceding blocks within the last 256 blocks.
Actual Behavior
The blockhash() function correctly returns the hash for the immediate parent of the current block (i.e., block.number - 1), but returns 0x0000000000000000000000000000000000000000000000000000000000000000 for other recent blocks within the last 10 blocks.
This behavior indicates that only the parent block's hash is accessible, while the hashes of other recent blocks within the expected range are not being returned correctly.
Impact
This issue limits the functionality of applications that rely on retrieving multiple recent block hashes, such as those used in randomness generation, historical data verification, and other decentralized applications (dApps) that require accurate and consistent block state information.
Recommendation
Investigate the configuration and state management of the Sei Network’s EVM implementation to ensure that the blockhash() function is fully compliant with the expected behavior on EVM-compatible chains, allowing retrieval of block hashes for the last 256 blocks.
Example Code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract BlockInfo {
function getBlockInfo()
public
view
returns (
uint256 currentBlockNumber,
bytes32 currentBlockHash,
uint256 previousBlockNumber,
bytes32 previousBlockHash,
bytes32[10] memory lastTenBlockHashes
)
{
currentBlockNumber = block.number;
currentBlockHash = blockhash(block.number);
previousBlockNumber = block.number - 1;
previousBlockHash = blockhash(block.number - 1);
for (uint i = 0; i < 10; i++) {
if (block.number > i) {
lastTenBlockHashes[i] = blockhash(block.number - i);
} else {
lastTenBlockHashes[i] = bytes32(0); // Fill with zeroes if block number is out of range
}
}
}
}
const { ethers } = require('ethers');
async function main() {
// Initialize the JSON-RPC provider
const jsonRpcProvider = new ethers.providers.JsonRpcProvider("https://evm-rpc.sei-apis.com");
// Contract address where the BlockInfo contract is deployed
const contractAddress = "0xFF720A74656ce29A07ADF1c249e1Ca3978f8A251"; // Replace with your contract's address
// ABI for the BlockInfo contract
const blockInfoAbi = [
"function getBlockInfo() view returns (uint256, bytes32, uint256, bytes32, bytes32[10])"
];
// Initialize the contract
const blockInfoContract = new ethers.Contract(contractAddress, blockInfoAbi, jsonRpcProvider);
// Call the getBlockInfo function
console.log("Fetching block information...");
const blockInfo = await blockInfoContract.getBlockInfo();
// Destructure the returned values
const [
currentBlockNumber,
currentBlockHash,
previousBlockNumber,
previousBlockHash,
lastTenBlockHashes
] = blockInfo;
// Display the fetched block information
console.log("Last 10 Block Hashes:");
lastTenBlockHashes.forEach((hash, index) => {
console.log(`Block ${currentBlockNumber - index}: ${hash}`);
});
}
main().catch(console.error);
Bug Report: Inconsistent Historical Block Hash Retrieval On Smart Contracts
Summary
The
blockhash()
function is only returning the hash for the immediate parent of the current block, while returning0x0000000000000000000000000000000000000000000000000000000000000000
for other recent block numbers within the expected range of 256 blocks. The issue was identified when attempting to retrieve the previous 10 block hashes via a smart contract.Environment
v22.4.1
5.7.2
https://evm-rpc.sei-apis.com
0xFF720A74656ce29A07ADF1c249e1Ca3978f8A251
blockhash()
Steps to Reproduce
blockhash()
function.Expected Behavior
blockhash()
function should correctly return the hashes for the previous 10 blocks, including the immediate parent and preceding blocks within the last 256 blocks.Actual Behavior
blockhash()
function correctly returns the hash for the immediate parent of the current block (i.e.,block.number - 1
), but returns0x0000000000000000000000000000000000000000000000000000000000000000
for other recent blocks within the last 10 blocks.Impact
Recommendation
blockhash()
function is fully compliant with the expected behavior on EVM-compatible chains, allowing retrieval of block hashes for the last 256 blocks.Example Code