tomusdrw / rust-web3

Ethereum JSON-RPC multi-transport client. Rust implementation of web3 library. ENS address: rust-web3.eth
MIT License
1.45k stars 465 forks source link

Why get different block numbers from block.transactions? #612

Open ngugcx opened 2 years ago

ngugcx commented 2 years ago

eth node: Geth/v1.10.15-stable-8be800ff/linux-amd64/go1.17.5

Below code retrieves all transactions in current highest block (height n) with block.transactions, then check block number of each transaction receipt. Sometimes the block number of transaction receipt is not n, but n-1 or n+1.

Is it nornal, or something wrong? Thanks!

#![recursion_limit="512"]
#[macro_use] extern crate log;

use anyhow::Result;
use web3::types::{BlockNumber, BlockId};

#[tokio::main]
async fn main() -> Result<()> {
    env_logger::init();

    let ws = web3::transports::WebSocket::new("ws://localhost:7546").await?;
    let web3 = web3::Web3::new(ws);
    let eth = web3.eth();
    let n = eth.block_number().await?.as_u64();
    let blk_id = BlockId::Number(BlockNumber::Number(n.into()));
    let _blk = eth.block(blk_id).await?;
    if let Some(blk) = _blk {
        for (_, t) in blk.transactions.iter().enumerate() {
            // info!("tx hash: 0x{:x}", t);
            let tr = eth.transaction_receipt(*t).await?;
            if let Some(_tr) = tr {
                let tx_bn = _tr.block_number.unwrap().as_u64();
                assert!(n == tx_bn, "n({}) != tx_bn({})", n, tx_bn);
            }   
        }   
    }   

    Ok(())
}