Skip to content

Commit

Permalink
[feat] update block RLP fetching for Dencun (#30)
Browse files Browse the repository at this point in the history
* feat: update block rlp fetching for Dencun

No ZK circuit changes were made.

We update the `get_block_rlp` function in `axiom_eth::providers::block`
so it will correctly calculate the RLP of blocks with the new fields
from EIP-4844 and EIP-4788. We must fetch these fields from the
`block.other` BTreeMap since `ethers-rs` has also not updated to handle
these.

* chore: add first sepolia dencun block to test
  • Loading branch information
jonathanpwang authored Mar 12, 2024
1 parent 11ea6d8 commit 17a52a8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
4 changes: 2 additions & 2 deletions axiom-codec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "axiom-codec"
version = "0.2.0"
version = "0.2.1"
authors = ["Intrinsic Technologies"]
license = "MIT"
edition = "2021"
Expand All @@ -19,7 +19,7 @@ serde_with = { version = "2.2", optional = true }
anyhow = "1.0"

# halo2, features turned on by axiom-eth
axiom-eth = { version = "=0.4.0", path = "../axiom-eth", default-features = false }
axiom-eth = { version = "0.4.1", path = "../axiom-eth", default-features = false }

# ethereum
ethers-core = { version = "2.0.10" }
Expand Down
4 changes: 2 additions & 2 deletions axiom-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "axiom-core"
version = "2.0.12"
version = "2.0.13"
authors = ["Intrinsic Technologies"]
license = "MIT"
edition = "2021"
Expand Down Expand Up @@ -29,7 +29,7 @@ anyhow = "1.0"
hex = "0.4.3"

# halo2, features turned on by axiom-eth
axiom-eth = { version = "=0.4.0", path = "../axiom-eth", default-features = false, features = ["providers", "aggregation", "evm"] }
axiom-eth = { version = "=0.4.1", path = "../axiom-eth", default-features = false, features = ["providers", "aggregation", "evm"] }

# crypto
ethers-core = { version = "=2.0.10" }
Expand Down
6 changes: 3 additions & 3 deletions axiom-eth/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "axiom-eth"
version = "0.4.0"
version = "0.4.1"
authors = ["Intrinsic Technologies"]
license = "MIT"
edition = "2021"
Expand Down Expand Up @@ -35,7 +35,7 @@ zkevm-hashes = { git = "https://github.com/axiom-crypto/halo2-lib.git", tag = "v

# crypto
rlp = "=0.5.2"
ethers-core = { version = "2.0.10" }
ethers-core = { version = "=2.0.10" } # fix the version as it affects what fields are included in the `Block` struct
# mpt implementation
hasher = { version = "=0.1", features = ["hash-keccak"] }
cita_trie = "=5.0.0"
Expand All @@ -53,7 +53,7 @@ snark-verifier = { git = "https://github.com/axiom-crypto/snark-verifier.git", t
snark-verifier-sdk = { git = "https://github.com/axiom-crypto/snark-verifier.git", tag = "v0.1.7-git", default-features = false }

# generating circuit inputs from blockchain
ethers-providers = { version = "2.0.10", optional = true }
ethers-providers = { version = "=2.0.10", optional = true }
tokio = { version = "1.28", default-features = false, features = ["rt", "rt-multi-thread", "macros"], optional = true }
futures = { version = "0.3", optional = true }

Expand Down
48 changes: 45 additions & 3 deletions axiom-eth/src/providers/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ethers_core::{
types::{Block, H256},
types::{Block, H256, U64},
utils::keccak256,
};
use ethers_providers::{JsonRpcClient, Middleware, Provider, ProviderError};
Expand Down Expand Up @@ -33,9 +33,36 @@ pub fn get_block_rlp_from_num<P: JsonRpcClient>(
}

pub fn get_block_rlp<TX>(block: &Block<TX>) -> Vec<u8> {
let withdrawals_root: Option<H256> = block.withdrawals_root;
let base_fee = block.base_fee_per_gas;
let rlp_len = 15 + usize::from(base_fee.is_some()) + usize::from(withdrawals_root.is_some());
let withdrawals_root: Option<H256> = block.withdrawals_root;
// EIP-4844:
let other = &block.other;
let mut blob_gas_used = other.get("blobGasUsed"); // EIP-4844 spec
if blob_gas_used.is_none() {
blob_gas_used = other.get("dataGasUsed"); // EIP-4788 spec
}
let blob_gas_used: Option<U64> =
blob_gas_used.map(|v| serde_json::from_value(v.clone()).unwrap());
let mut excess_blob_gas = other.get("excessBlobGas"); // EIP-4844 spec
if excess_blob_gas.is_none() {
excess_blob_gas = other.get("excessDataGas"); // EIP-4788 spec
}
let excess_blob_gas: Option<U64> =
excess_blob_gas.map(|v| serde_json::from_value(v.clone()).unwrap());
// EIP-4788:
let parent_beacon_block_root: Option<H256> =
other.get("parentBeaconBlockRoot").map(|v| serde_json::from_value(v.clone()).unwrap());

let mut rlp_len = 15;
for opt in [
base_fee.is_some(),
withdrawals_root.is_some(),
blob_gas_used.is_some(),
excess_blob_gas.is_some(),
parent_beacon_block_root.is_some(),
] {
rlp_len += opt as usize;
}
let mut rlp = RlpStream::new_list(rlp_len);
rlp.append(&block.parent_hash);
rlp.append(&block.uncles_hash);
Expand All @@ -54,6 +81,9 @@ pub fn get_block_rlp<TX>(block: &Block<TX>) -> Vec<u8> {
rlp.append(&block.nonce.unwrap());
base_fee.map(|base_fee| rlp.append(&base_fee));
withdrawals_root.map(|withdrawals_root| rlp.append(&withdrawals_root));
blob_gas_used.map(|blob_gas_used| rlp.append(&blob_gas_used));
excess_blob_gas.map(|excess_blob_gas| rlp.append(&excess_blob_gas));
parent_beacon_block_root.map(|parent_beacon_block_root| rlp.append(&parent_beacon_block_root));
let encoding: Vec<u8> = rlp.out().into();
assert_eq!(keccak256(&encoding), block.hash.unwrap().0);
encoding
Expand Down Expand Up @@ -96,4 +126,16 @@ mod tests {
get_block_rlp(&block);
}
}

#[test]
fn test_retry_provider_sepolia() {
let provider = setup_provider(Chain::Sepolia);

let rt = Runtime::new().unwrap();
let latest = rt.block_on(provider.get_block_number()).unwrap();
for block_num in [0, 5000050, 5187023, 5187810, 5187814, latest.as_u64()] {
let block = rt.block_on(provider.get_block(block_num)).unwrap().unwrap();
get_block_rlp(&block);
}
}
}
1 change: 1 addition & 0 deletions axiom-eth/src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn setup_provider(chain: Chain) -> Provider<RetryClient<Http>> {
Provider::new_client(&provider_uri, 10, 500).expect("could not instantiate HTTP Provider")
}

/// Version of `Vec::from_hex` that works with odd-length strings. Assumes string does not start with `0x` and only has hex characters.
pub fn from_hex(s: &str) -> Vec<u8> {
let s = if s.len() % 2 == 1 { format!("0{s}") } else { s.to_string() };
Vec::from_hex(s).unwrap()
Expand Down
6 changes: 3 additions & 3 deletions axiom-query/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "axiom-query"
version = "2.0.14"
version = "2.0.15"
authors = ["Intrinsic Technologies"]
license = "MIT"
edition = "2021"
Expand Down Expand Up @@ -35,8 +35,8 @@ rand = "0.8"
rand_core = { version = "0.6", default-features = false, features = ["getrandom"] }

# halo2, features turned on by axiom-eth
axiom-eth = { version = "=0.4.0", path = "../axiom-eth", default-features = false, features = ["providers", "aggregation", "evm"] }
axiom-codec = { version = "0.2.0", path = "../axiom-codec", default-features = false }
axiom-eth = { version = "=0.4.1", path = "../axiom-eth", default-features = false, features = ["providers", "aggregation", "evm"] }
axiom-codec = { version = "0.2.1", path = "../axiom-codec", default-features = false }

# crypto
rlp = "0.5.2"
Expand Down

0 comments on commit 17a52a8

Please sign in to comment.