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

Eth JSON-RPC incompatibility - eth_getBlock* is incompatible with rust web3 library #1565

Closed
shleshg opened this issue Jan 25, 2023 · 1 comment

Comments

@shleshg
Copy link

shleshg commented Jan 25, 2023

Right now I'm participating in FVM Space Warp hackathon in Mark3d team with FileMarket project.

I've faced an issue with eth_getBlockByNumber and eth_getBlockByHash methods. Block format in response is not compatible with Rust web3 library. Trying to fetch block returns following error:

thread 'main' panicked at 'Decoder error: Error("invalid value: string \"\", expected 0x prefix", line: 0, column: 0)', src/bin/get_block.rs:16:19
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Code to represent the issue is following:

src/bin/get_block.rs file:

use web3::{
    types::{BlockId, BlockNumber},
};
use env_logger;

#[tokio::main]
async fn main() {
    env_logger::init();

    let url = "https://api.hyperspace.node.glif.io/rpc/v1";
    let transport = web3::transports::Http::new(url).unwrap();
    let web3 = web3::Web3::new(transport);

    let block = match web3.eth().block(BlockId::Number(BlockNumber::Latest)).await {
        Ok(b) => b,
        Err(e) => panic!("{:}", e),
    };
    let block = match block {
        Some(b) => b,
        None => panic!("none"),
    };
    println!("{:?}", block);
}

Cargo.toml file:

[package]
name = "backend"
version = "0.1.0"
edition = "2021"

[dependencies]
web3 = "0.18.0"
tokio = "1.21.2"
log = "0.4.6"
env_logger = "0.10.0"

Running get_block.rs with trace log level gives following log:

[2023-01-25T13:08:27Z DEBUG web3::transports::http] [id:0] sending request: "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getBlockByNumber\",\"params\":[\"latest\",false],\"id\":0}"
[2023-01-25T13:08:28Z DEBUG web3::transports::http] [id:0] received response: "{\"jsonrpc\":\"2.0\",\"result\":{\"hash\":\"0xe0f85c0ae6fe10cf099d29039dd083a5fb2be8b9a409b42512312dbf5e4699e8\",\"parentHash\":\"0x932f50fec9b5e67fa225cb0604c9d1e28e06a29b8baf8bf4225ff7c2e1ced42a\",\"sha3Uncles\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"miner\":\"0x0000000000000000000000000000000000000000\",\"stateRoot\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"transactionsRoot\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"receiptsRoot\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"logsBloom\":\"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001\",\"difficulty\":\"0x0\",\"totalDifficulty\":\"0x0\",\"number\":\"0x6897\",\"gasLimit\":\"0x2540be400\",\"gasUsed\":\"0x0\",\"timestamp\":\"0x63d12992\",\"extraData\":\"\",\"mixHash\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"nonce\":\"0x0000000000000000\",\"baseFeePerGas\":\"0x64\",\"size\":\"0x0\",\"transactions\":[],\"uncles\":[]},\"id\":0}\n"
thread 'main' panicked at 'Decoder error: Error("invalid value: string \"\", expected 0x prefix", line: 0, column: 0)', src/bin/get_block.rs:16:19
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

So, there is no issue with the connection, it's just about the block structure.

I believe the issue is with extraData field. In web3 library it defined as Bytes. This requires 0x prefix for the field value and in response extraData is just an empty string.

Defining self-made block structure without extraData field and fetching it with web3::Transport execute method directly is a workaround (this can help other hackathon participants facing the same issue until it will be fixed).

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