Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lordshisho opened this issue Aug 19, 2024 · 1 comment
Open
Assignees
Labels
bug Something isn't working linear Created by Linear-GitHub Sync

Comments

@lordshisho
Copy link

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

  • NodeJS: v22.4.1
  • Ethers: 5.7.2
  • Node URL: https://evm-rpc.sei-apis.com
  • Contract Address: 0xFF720A74656ce29A07ADF1c249e1Ca3978f8A251
  • Smart Contract Function: blockhash()

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

  • 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);
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
@lordshisho lordshisho added bug Something isn't working linear Created by Linear-GitHub Sync labels Aug 19, 2024
@philipsu522
Copy link
Contributor

thanks @lordshisho we'll take a look!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working linear Created by Linear-GitHub Sync
Projects
None yet
Development

No branches or pull requests

3 participants