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

Add eth proof generation utilities #928

Merged
merged 7 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions eth2near/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions eth2near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"eth_rpc_client",
"finality-update-verify",
"logger",
"utilities",
]

[workspace.dependencies]
Expand Down
16 changes: 16 additions & 0 deletions eth2near/utilities/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "utilities"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = { version = "0.11", features = ["blocking", "json"] }
serde_json = "1.0.74"
serde = { version = "1.0", features = ["derive"] }
hex = { version="0.4", features = ["serde"] }
num-traits = "0.2"
cita_trie = "5.0.1"
hasher = "0.1.4"
rlp = "0.5.2"
243 changes: 243 additions & 0 deletions eth2near/utilities/src/eth_proof_generator.rs

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions eth2near/utilities/src/eth_rpc_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use crate::{
primitives::U256,
types::{BlockHeader, TransactionReceipt},
};
use reqwest::blocking::Client;
use serde::Deserialize;
use serde_json::{json, Value};
use std::error::Error;

pub struct EthRPCClient {
endpoint_url: String,
client: Client,
}

impl EthRPCClient {
olga24912 marked this conversation as resolved.
Show resolved Hide resolved
pub fn new(endpoint_url: &str) -> Self {
Self {
endpoint_url: endpoint_url.to_string(),
client: reqwest::blocking::Client::new(),
}
}

pub fn get_transaction_receipt_by_hash(&self, tx_hash: &U256,) -> Result<TransactionReceipt, Box<dyn Error>> {
let json_value = json!({
"id": 1,
"jsonrpc": "2.0",
"method": "eth_getTransactionReceipt",
"params": [tx_hash.0]
});

let res = self
.client
.post(&self.endpoint_url)
.json(&json_value)
.send()?
.text()?;

let val: Value = serde_json::from_str(&res)?;
let receipt = TransactionReceipt::deserialize(&val["result"])?;

Ok(receipt)
}

pub fn get_block_by_number(&self, block_number: u64) -> Result<BlockHeader, Box<dyn Error>> {
let json_value = json!({
"id": 1,
"jsonrpc": "2.0",
"method": "eth_getBlockByNumber",
"params": [format!("0x{:x}", block_number), false]
});

let res = self
.client
.post(&self.endpoint_url)
.json(&json_value)
.send()?
.text()?;

let val: Value = serde_json::from_str(&res)?;
let header = BlockHeader::deserialize(&val["result"])?;

Ok(header)
}

pub fn get_block_receipts(
&self,
block_number: u64,
) -> Result<Vec<TransactionReceipt>, Box<dyn Error>> {
let json_value = json!({
"id": 1,
"jsonrpc": "2.0",
"method": "eth_getBlockReceipts",
"params": [format!("0x{:x}", block_number)]
});

let res = self
.client
.post(&self.endpoint_url)
.json(&json_value)
.send()?
.text()?;

let val: Value = serde_json::from_str(&res)?;
let receipts = Vec::<TransactionReceipt>::deserialize(&val["result"])?;

Ok(receipts)
}
}
5 changes: 5 additions & 0 deletions eth2near/utilities/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod eth_rpc_client;
pub mod eth_proof_generator;
mod primitives;
mod types;
mod serde;
10 changes: 10 additions & 0 deletions eth2near/utilities/src/primitives.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[derive(Debug, Clone)]
pub struct FixedBytes<const N: usize>(pub [u8; N]);
#[derive(Debug, Clone)]
pub struct Bytes(pub Vec<u8>);
#[derive(Debug, Clone)]
pub struct Byte(pub u8);

pub type U256 = FixedBytes<32>;
pub type Address = FixedBytes<20>;
pub type Bloom = FixedBytes<256>;
Loading
Loading