sei-protocol / sei-chain

Apache License 2.0
2.71k stars 809 forks source link

[BUG] Inconsistent Historical Block Hash Retrieval On Smart Contracts #1823

Open lordshisho opened 2 months ago

lordshisho commented 2 months ago

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.

Environment

Steps to Reproduce

  1. Deploy a smart contract on the Sei Network that attempts to retrieve the previous 10 block hashes using the blockhash() function.
  2. Call the contract's function to retrieve and display the block hashes.
  3. Observe the returned values for block hashes.

Expected Behavior

Actual Behavior

Impact

Recommendation

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);
Fetching block information...
Last 10 Block Hashes:
Block 96624165: 0x0000000000000000000000000000000000000000000000000000000000000000
Block 96624164: 0x41f44db7e77c0bdb5bc8ea6e7ecf058991d7ada158d94f4a7491f5fae6024ffc
Block 96624163: 0x0000000000000000000000000000000000000000000000000000000000000000
Block 96624162: 0x0000000000000000000000000000000000000000000000000000000000000000
Block 96624161: 0x0000000000000000000000000000000000000000000000000000000000000000
Block 96624160: 0x0000000000000000000000000000000000000000000000000000000000000000
Block 96624159: 0x0000000000000000000000000000000000000000000000000000000000000000
Block 96624158: 0x0000000000000000000000000000000000000000000000000000000000000000
Block 96624157: 0x0000000000000000000000000000000000000000000000000000000000000000
Block 96624156: 0x0000000000000000000000000000000000000000000000000000000000000000
philipsu522 commented 2 months ago

thanks @lordshisho we'll take a look!