a16z / helios

A fast, secure, and portable multichain light client for Ethereum
MIT License
1.86k stars 291 forks source link

get_block_by_number skip some blocks #376

Closed prz23 closed 1 month ago

prz23 commented 1 month ago

Call this method get_block_by_number(), and there is block data every other one, skipping a block in between.

client.start().await?;
client.wait_synced().await;

let head_block_num = client.get_block_number().await?;
let head_block = client.get_block_by_number(BlockTag::Number(head_block_num.as_u64()), false).await;

std::thread::sleep(std::time::Duration::from_secs(24));
let head_block = client.get_block_by_number(BlockTag::Number(head_block_num.as_u64() + 1), false).await;
println!("head_block+1 {:?}",head_block.unwrap().is_some());    // false
let head_block = client.get_block_by_number(BlockTag::Number(head_block_num.as_u64() + 2), false).await;
println!("head_block+2 {:?}",head_block.unwrap().is_some());   // true
let head_block = client.get_block_by_number(BlockTag::Number(head_block_num.as_u64() + 3), false).await;
println!("head_block+3 {:?}",head_block.unwrap().is_some());   // false
prz23 commented 1 month ago

use latest helios commit:0cc5a34aaf350a26e6f374f9f1d58de1212f5934
0.6.0:

Cargo.toml:

[package]
name = "helios-test"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { version = "1.4", features = ["rt-multi-thread"] }
helios = { git = "https://github.com/ABMatrix/helios"}

code:

use helios::{
    client::{Client, ClientBuilder, ConfigDB},
    config::{checkpoints, networks::Network},
    types::BlockTag,
};

#[tokio::main]
async fn main() {
    let cf = checkpoints::CheckpointFallback::new()
        .build()
        .await
        .unwrap();
    let mainnet_checkpoint = cf.fetch_latest_checkpoint(&Network::MAINNET).await.unwrap();
    println!("Fetched latest mainnet checkpoint: {}", mainnet_checkpoint);

    let mut client: Client<ConfigDB> = ClientBuilder::new()
        .network(Network::MAINNET)
        .consensus_rpc("https://www.lightclientdata.org")
        .execution_rpc("https://eth-mainnet.g.alchemy.com/v2/{api-key}")
        .data_dir("./db/data".into())
        .checkpoint(mainnet_checkpoint)
        .build()
        .unwrap();

    client.start().await.unwrap();

    println!("wait_synced");
    client.wait_synced().await;
    println!("synced");

    let head_block_num = client.get_block_number().await.unwrap();
    println!("synced up to block: {}", head_block_num);
    println!("synced up to block: {:?}", head_block_num.as_limbs()[0]);

    let head_block = client
    .get_block_by_number(BlockTag::Number(head_block_num.as_limbs()[0]), false)
    .await;
    println!("head_block {:?}", head_block.unwrap().is_some()); 

    std::thread::sleep(std::time::Duration::from_secs(90));
    let head_block = client
        .get_block_by_number(BlockTag::Number(head_block_num.as_limbs()[0] + 1), false)
        .await;
    println!("head_block+1 {:?}", head_block.unwrap().is_some());
    let head_block = client
        .get_block_by_number(BlockTag::Number(head_block_num.as_limbs()[0] + 2), false)
        .await;
    println!("head_block+2 {:?}", head_block.unwrap().is_some()); 
    let head_block = client
        .get_block_by_number(BlockTag::Number(head_block_num.as_limbs()[0] + 3), false)
        .await;
    println!("head_block+3 {:?}", head_block.unwrap().is_some()); 
    let head_block = client
        .get_block_by_number(BlockTag::Number(head_block_num.as_limbs()[0] + 4), false)
        .await;
    println!("head_block+4 {:?}", head_block.unwrap().is_some()); 
    let head_block = client
        .get_block_by_number(BlockTag::Number(head_block_num.as_limbs()[0] + 5), false)
        .await;
    println!("head_block+5 {:?}", head_block.unwrap().is_some()); 
}

run: cargo run --release -- --nocapture

result:

Fetched latest mainnet checkpoint: 0x7cfd788d8187e6d6ee4596a314146329425d77622393fbff254b3ca9534ab630
wait_synced
synced
synced up to block: 20771152
synced up to block: 20771152
head_block true
head_block+1 false
head_block+2 true
head_block+3 true
head_block+4 false
head_block+5 true
geet-eth commented 1 month ago
Screenshot 2024-09-21 at 13 26 30

Couldn't reproduce. (also i havent grokked helios personally yet)

prz23 commented 1 month ago
Screenshot 2024-09-21 at 13 26 30

Couldn't reproduce. (also i havent grokked helios personally yet)

head_block+1 false indicates that you have reproduce This loss of get_block is random; if there is a false, it means it's lost.

prz23 commented 1 month ago

Rewrite it as a loop, and the program will run properly for extended periods.