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

cannot get earlier transactions and blocks #319

Open
yuvalelmo opened this issue Jan 30, 2024 · 8 comments
Open

cannot get earlier transactions and blocks #319

yuvalelmo opened this issue Jan 30, 2024 · 8 comments

Comments

@yuvalelmo
Copy link

yuvalelmo commented Jan 30, 2024

I am using helios as library (0.5.2)
for some reason, it seems like i cannot get the data of blocks that are not the latest block.
i use the get_block_by_number function and it returns None, unless it is the latest block in the chain(using BlockTag::Number with the block number).

the same goes with transactions - cannot fetch information about transactions using transaction hash.
the RPC provider is alchemy.

for example, this code would work and return the block:

let block_number = client.get_block_number().await?;
    let block = client
        .get_block_by_number(BlockTag::Number(block_number.as_u64()), true)
        .await?;
    info!("block: {:?}", block);

but this code would return None, even though there is a block with this block number:

let block_number: u64 = 19075597;

    let block = client
        .get_block_by_number(BlockTag::Number(block_number), true)
        .await?;
    info!("block: {:?}", block);

am i using it wrong?

@ncitron
Copy link
Collaborator

ncitron commented Jan 30, 2024

Can you please provide the full code example so I can help you debug?

@yuvalelmo
Copy link
Author

sure, there's the whole function:

#[tokio::main]
async fn main() -> Result<()> {
    dotenv().ok();
    set_logger();

    let network = Network::GOERLI;
    let cf = checkpoints::CheckpointFallback::new()
        .build()
        .await
        .unwrap();
    info!("Checkpoint fallback services built");

    let checkpoint = cf.fetch_latest_checkpoint(&network).await.unwrap();
    let checkpoint = format!("{:?}", checkpoint);

    let execution_rpc_url = get_env_variable("EXECUTION_RPC")?;
    let consensus_rpc_url = get_env_variable("CONSENSUS_RPC")?;

    let mut client: Client<FileDB> = ClientBuilder::new()
        .network(network)
        .execution_rpc(&execution_rpc_url)
        .consensus_rpc(&consensus_rpc_url)
        .load_external_fallback()
        .checkpoint(&checkpoint)
        .data_dir("/tmp/helios".parse()?)
        .build()?;
    info!("client created");

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

    let block_number: u64 = 19075597;

    let block = client
        .get_block_by_number(BlockTag::Number(block_number), true)
        .await?;
    info!("block: {:?}", block);
    Ok(())
}

@ncitron
Copy link
Collaborator

ncitron commented Jan 30, 2024

Oh I see what is happening here. Helios only stores block data for blocks that start either after helios starts or within 64 blocks of the current block number (whichever is higher).

This is mostly a limitation due to efficiency as well as how the light client protocol works.

@ncitron ncitron closed this as completed Jan 30, 2024
@yuvalelmo
Copy link
Author

yuvalelmo commented Feb 1, 2024

@ncitron first, thanks for your help!

I have a similar problem now but with transactions.
I use the get_transaction_receipt function in order to get the receipt of a transaction that is created after the start of the client, but I keep getting a None result for some reason.

I inject the hash of the transaction to the tx_hash_str variable using a breakpoint, and i am taking a transaction hash from etherscan explorer.
I tried both pending transactions and approved transactions, but both gives the same result of None, when the transaction is clearly created after the client.
I use the same code as above, but with changing the block fetching part with the following:

	let tx_hash_str = "";
	//here i use a breakpoint and put the hash string into tx_hash_str
    let tx_hash = H256::from_str(tx_hash_str).unwrap();
    let tx = client
        .get_transaction_receipt(&tx_hash)
        .await?;

@ncitron
Copy link
Collaborator

ncitron commented Feb 2, 2024

Can you confirm that you can fetch a block for the block number of the transaction?

@ncitron ncitron reopened this Feb 2, 2024
@yuvalelmo
Copy link
Author

Yes, I can do this.

@ncitron
Copy link
Collaborator

ncitron commented Feb 5, 2024

Hmm it seems I am able to fetch a receipt using this simple example:

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;
let tx_hash = head_block.unwrap().unwrap().transactions.hashes()[0];
let receipt = client.get_transaction_receipt(&tx_hash).await.unwrap().unwrap();
println!("{:?}", receipt);

I wonder if the breakpoints are causing it to act up. If helios can fetch the block it should always be able to get a receipt from within that block.

@yuvalelmo
Copy link
Author

Thanks for all the help!
It seems I did not understand the light client's logics just right. I now understand that I would be able to fetch only transactions from already fetched blocks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants