Skip to content

Commit

Permalink
use network config for extract proof size
Browse files Browse the repository at this point in the history
  • Loading branch information
olga24912 committed Jan 17, 2024
1 parent a20e81d commit e22cdfb
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 34 deletions.
1 change: 1 addition & 0 deletions eth2near/Cargo.lock

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

7 changes: 5 additions & 2 deletions eth2near/eth2near-block-relay-rs/src/eth2near_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::prometheus_metrics::{
use bitvec::macros::internal::funty::Fundamental;
use contract_wrapper::eth_client_contract_trait::EthClientContractTrait;
use contract_wrapper::near_rpc_client::NearRPCClient;
use eth2_utility::consensus::{EPOCHS_PER_SYNC_COMMITTEE_PERIOD, SLOTS_PER_EPOCH};
use eth2_utility::consensus::{EPOCHS_PER_SYNC_COMMITTEE_PERIOD, Network, NetworkConfig, SLOTS_PER_EPOCH};
use eth_rpc_client::beacon_rpc_client::BeaconRPCClient;
use eth_rpc_client::eth1_rpc_client::Eth1RPCClient;
use eth_rpc_client::hand_made_finality_light_client_update::HandMadeFinalityLightClientUpdate;
Expand All @@ -24,6 +24,7 @@ use std::thread;
use std::thread::sleep;
use std::time::Duration;
use std::vec::Vec;
use std::str::FromStr;
use eth2_utility::types::ClientMode;
use types::Slot;

Expand Down Expand Up @@ -119,11 +120,13 @@ impl Eth2NearRelay {
) -> Self {
info!(target: "relay", "=== Relay initialization === ");

let network_config = NetworkConfig::new(&Network::from_str(config.ethereum_network.as_str()).unwrap());
let beacon_rpc_client = BeaconRPCClient::new(
&config.beacon_endpoint,
config.eth_requests_timeout_seconds,
config.state_requests_timeout_seconds,
Some(config.beacon_rpc_version.clone()),
network_config
);
let next_light_client_update =
Self::get_light_client_update_from_file(config, &beacon_rpc_client)
Expand Down Expand Up @@ -463,7 +466,7 @@ impl Eth2NearRelay {
fn send_light_client_updates(
&mut self,
last_finalized_slot_on_near: u64,
last_finalized_slot_on_eth: u64,
last_finalized_slot_on_eth: u64
) {
info!(target: "relay", "= Sending light client update =");

Expand Down
2 changes: 1 addition & 1 deletion eth2near/eth2near-block-relay-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

let mut eth2near_relay = Eth2NearRelay::init(
&config,
get_eth_client_contract(&config)
get_eth_client_contract(&config),
);

eth2near_relay.run(None);
Expand Down
1 change: 1 addition & 0 deletions eth2near/eth_rpc_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ serde = { version = "1.0", features = ["derive"] }
ethereum-types = { version = "0.14.1", features = ["rlp", "serialize"], default-features = false }
reqwest = { version = "0.11", features = ["blocking"] }
eth-types = { path = "../../contracts/near/eth-types/" }
eth2-utility = { path = "../../contracts/near/eth2-utility/" }
contract_wrapper = { path = "../contract_wrapper" }
clap = { version = "3.1.6", features = ["derive"] }
tokio = { version = "1.1", features = ["macros", "rt", "time"] }
Expand Down
27 changes: 22 additions & 5 deletions eth2near/eth_rpc_client/src/beacon_block_body_merkle_tree.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use eth2_utility::consensus::NetworkConfig;
use eth_types::eth2::Epoch;
use ethereum_types::H256;
use merkle_proof::MerkleTree;
use tree_hash::TreeHash;
Expand Down Expand Up @@ -56,11 +58,15 @@ impl BeaconBlockBodyMerkleTree {
pub struct ExecutionPayloadMerkleTree(pub MerkleTree);

impl ExecutionPayloadMerkleTree {
pub const TREE_NUM_LEAVES: usize = 15;
pub const TREE_DEPTH: usize = 4;
pub fn get_tree_depth(network_config: &NetworkConfig, epoch: Epoch) -> usize {
let proof_size = network_config.compute_proof_size(epoch);
proof_size.l2_execution_payload_proof_size
}

pub fn new(execution_payload: &ExecutionPayload<MainnetEthSpec>) -> Self {
let leaves: [H256; Self::TREE_NUM_LEAVES] = [
pub fn new(execution_payload: &ExecutionPayload<MainnetEthSpec>,
network_config: &NetworkConfig,
epoch: Epoch) -> Self {
let mut leaves: Vec<H256> = vec![
execution_payload.parent_hash().tree_hash_root(),
execution_payload.fee_recipient().tree_hash_root(),
execution_payload.state_root().tree_hash_root(),
Expand All @@ -82,7 +88,18 @@ impl ExecutionPayloadMerkleTree {
},
];

Self(MerkleTree::create(&leaves, Self::TREE_DEPTH))
let tree_depth = Self::get_tree_depth(network_config, epoch);
if tree_depth > 4 {
if let Ok(blob_gas_used) = execution_payload.blob_gas_used() {
leaves.push(blob_gas_used.tree_hash_root());
}

if let Ok(excess_blob_gas) = execution_payload.excess_blob_gas() {
leaves.push(excess_blob_gas.tree_hash_root());
}
}

Self(MerkleTree::create(&leaves, tree_depth))
}
}

Expand Down
24 changes: 23 additions & 1 deletion eth2near/eth_rpc_client/src/beacon_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use eth_types::eth2::SyncAggregate;
use eth_types::eth2::SyncCommittee;
use eth_types::eth2::SyncCommitteeUpdate;
use eth_types::H256;
use eth2_utility::consensus::NetworkConfig;
use log::trace;
use reqwest::blocking::Client;
use serde::Deserialize;
Expand Down Expand Up @@ -81,6 +82,7 @@ pub struct BeaconRPCClient {
client: Client,
client_state_request: Client,
routes: BeaconRPCRoutes,
network_config: NetworkConfig,
}

impl BeaconRPCClient {
Expand All @@ -93,6 +95,7 @@ impl BeaconRPCClient {
timeout_seconds: u64,
timeout_state_seconds: u64,
version: Option<BeaconRPCVersion>,
network_config: NetworkConfig,
) -> Self {
Self {
endpoint_url: endpoint_url.to_string(),
Expand All @@ -105,9 +108,14 @@ impl BeaconRPCClient {
.build()
.expect("Error on building blocking client for state request."),
routes: BeaconRPCRoutes::new(version.unwrap_or(BeaconRPCVersion::V1_1)),
network_config,
}
}

pub fn get_network_config(&self) -> &NetworkConfig {
&self.network_config
}

/// Returns `BeaconBlockBody` struct for the given `block_id`.
///
/// # Arguments
Expand Down Expand Up @@ -501,6 +509,19 @@ impl BeaconRPCClient {
_ => serde_json::to_string(&v["data"][0]["finalized_header"])?,
};

let slot = match self.routes.version {
BeaconRPCVersion::V1_5 => {
let mut res = serde_json::to_value(&v[0]["data"]["attested_header"]["beacon"]["slot"])?;
if res == "null" {
res = serde_json::to_value(&v["data"][0]["attested_header"]["beacon"]["slot"])?;
}
res
}
_ => serde_json::to_value(&v["data"][0]["attested_header"]["slot"])?,
}.as_u64().ok_or(FailOnGettingJson {response: "Error on extraction slot number".to_string()})?;

let epoch = eth2_utility::consensus::compute_epoch_at_slot(slot);

let finalized_header: BeaconBlockHeader = serde_json::from_str(&finalized_header_json_str)?;

let mut finalized_branch_json_str =
Expand All @@ -516,7 +537,8 @@ impl BeaconRPCClient {
let finalized_block_body =
self.get_beacon_block_body_for_block_id(&format!("{}", finalized_block_slot))?;
let finalized_block_eth1data_proof =
ExecutionBlockProof::construct_from_beacon_block_body(&finalized_block_body)?;
ExecutionBlockProof::construct_from_beacon_block_body(&finalized_block_body,
&self.network_config, epoch)?;

Ok(FinalizedHeaderUpdate {
header_update: HeaderUpdate {
Expand Down
48 changes: 25 additions & 23 deletions eth2near/eth_rpc_client/src/execution_block_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use ethereum_types::H256;
use std::error::Error;
use std::fmt;
use std::fmt::Display;
use eth2_utility::consensus::NetworkConfig;
use eth_types::eth2::Epoch;
use types::{BeaconBlockBody, ExecutionPayload, MainnetEthSpec};

/// `ExecutionBlockProof` contains a `block_hash` (execution block) and
Expand All @@ -17,51 +19,47 @@ use types::{BeaconBlockBody, ExecutionPayload, MainnetEthSpec};
/// The proof starts from the leaf.
pub struct ExecutionBlockProof {
block_hash: H256,
proof: [H256; Self::PROOF_SIZE],
proof: Vec<H256>,
}

impl ExecutionBlockProof {
pub const L1_BEACON_BLOCK_BODY_TREE_EXECUTION_PAYLOAD_INDEX: usize = 9;
pub const L2_EXECUTION_PAYLOAD_TREE_EXECUTION_BLOCK_INDEX: usize = 12;

pub const L1_BEACON_BLOCK_BODY_PROOF_SIZE: usize =
BeaconBlockBodyMerkleTree::BEACON_BLOCK_BODY_TREE_DEPTH;
pub const L2_EXECUTION_PAYLOAD_PROOF_SIZE: usize = ExecutionPayloadMerkleTree::TREE_DEPTH;
pub const PROOF_SIZE: usize =
Self::L1_BEACON_BLOCK_BODY_PROOF_SIZE + Self::L2_EXECUTION_PAYLOAD_PROOF_SIZE;

pub fn construct_from_raw_data(block_hash: &H256, proof: &[H256; Self::PROOF_SIZE]) -> Self {
pub fn construct_from_raw_data(block_hash: &H256, proof: Vec<H256>) -> Self {
Self {
block_hash: *block_hash,
proof: *proof,
proof,
}
}

pub fn construct_from_beacon_block_body(
beacon_block_body: &BeaconBlockBody<MainnetEthSpec>,
network_config: &NetworkConfig,
epoch: Epoch
) -> Result<Self, Box<dyn Error>> {
let proof_size = network_config.compute_proof_size(epoch);
let beacon_block_merkle_tree = &BeaconBlockBodyMerkleTree::new(beacon_block_body);

let execution_payload_merkle_tree = &ExecutionPayloadMerkleTree::new(
&beacon_block_body
.execution_payload()
.map_err(|_| MissExecutionPayload)?
.into(),
network_config,
epoch
);

let l1_execution_payload_proof = beacon_block_merkle_tree
.0
.generate_proof(
Self::L1_BEACON_BLOCK_BODY_TREE_EXECUTION_PAYLOAD_INDEX,
Self::L1_BEACON_BLOCK_BODY_PROOF_SIZE,
proof_size.l1_beacon_block_body_tree_execution_payload_index,
proof_size.l1_beacon_block_body_proof_size,
)
.map_err(|err| MerkleTreeError(err))?
.1;
let mut block_proof = execution_payload_merkle_tree
.0
.generate_proof(
Self::L2_EXECUTION_PAYLOAD_TREE_EXECUTION_BLOCK_INDEX,
Self::L2_EXECUTION_PAYLOAD_PROOF_SIZE,
proof_size.l2_execution_payload_tree_execution_block_index,
proof_size.l2_execution_payload_proof_size,
)
.map_err(|err| MerkleTreeError(err))?
.1;
Expand All @@ -77,8 +75,8 @@ impl ExecutionBlockProof {
})
}

pub fn get_proof(&self) -> [H256; Self::PROOF_SIZE] {
self.proof
pub fn get_proof(&self) -> Vec<H256> {
self.proof.clone()
}

pub fn get_execution_block_hash(&self) -> H256 {
Expand All @@ -88,22 +86,26 @@ impl ExecutionBlockProof {
pub fn verify_proof_for_hash(
&self,
beacon_block_body_hash: &H256,
network_config: &NetworkConfig,
epoch: Epoch
) -> Result<bool, IncorrectBranchLength> {
let l2_proof: &[H256] = &self.proof[0..Self::L2_EXECUTION_PAYLOAD_PROOF_SIZE];
let proof_size = network_config.compute_proof_size(epoch);

let l2_proof: &[H256] = &self.proof[0..proof_size.l2_execution_payload_proof_size];
let l1_proof: &[H256] =
&self.proof[Self::L2_EXECUTION_PAYLOAD_PROOF_SIZE..Self::PROOF_SIZE];
&self.proof[proof_size.l2_execution_payload_proof_size..proof_size.execution_proof_size];
let execution_payload_hash = Self::merkle_root_from_branch(
self.block_hash,
l2_proof,
Self::L2_EXECUTION_PAYLOAD_PROOF_SIZE,
Self::L2_EXECUTION_PAYLOAD_TREE_EXECUTION_BLOCK_INDEX,
proof_size.l2_execution_payload_proof_size,
proof_size.l2_execution_payload_tree_execution_block_index,
)?;

Ok(merkle_proof::verify_merkle_proof(
execution_payload_hash,
l1_proof,
BeaconBlockBodyMerkleTree::BEACON_BLOCK_BODY_TREE_DEPTH,
Self::L1_BEACON_BLOCK_BODY_TREE_EXECUTION_PAYLOAD_INDEX,
proof_size.l1_beacon_block_body_tree_execution_payload_index,
*beacon_block_body_hash,
))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use log::trace;
use serde_json::Value;
use ssz::Encode;
use std::error::Error;
use eth2_utility::consensus::NetworkConfig;
use tree_hash::TreeHash;
use types::{BeaconBlockBody, BeaconBlockHeader, BeaconState, MainnetEthSpec};
use types::{BeaconBlockBody, BeaconBlockHeader, BeaconState, Epoch, MainnetEthSpec};

pub struct HandMadeFinalityLightClientUpdate {}

Expand Down Expand Up @@ -162,6 +163,7 @@ impl HandMadeFinalityLightClientUpdate {
beacon_state: BeaconState<MainnetEthSpec>,
include_next_sync_committee: bool,
) -> Result<LightClientUpdate, Box<dyn Error>> {
let epoch = eth2_utility::consensus::compute_epoch_at_slot(attested_slot);
let signature_beacon_body =
beacon_rpc_client.get_beacon_block_body_for_block_id(&format!("{}", signature_slot))?;
let sync_aggregate = signature_beacon_body
Expand Down Expand Up @@ -192,6 +194,8 @@ impl HandMadeFinalityLightClientUpdate {
&finality_header,
&beacon_state,
&finalized_block_body,
beacon_rpc_client.get_network_config(),
epoch.into()
)?,
sync_committee_update: if include_next_sync_committee {
Some(Self::get_next_sync_committee(&beacon_state)?)
Expand Down Expand Up @@ -301,10 +305,12 @@ impl HandMadeFinalityLightClientUpdate {
finality_header: &BeaconBlockHeader,
beacon_state: &BeaconState<MainnetEthSpec>,
finalized_block_body: &BeaconBlockBody<MainnetEthSpec>,
network_config: &NetworkConfig,
epoch: Epoch
) -> Result<FinalizedHeaderUpdate, Box<dyn Error>> {
let finality_branch = Self::get_finality_branch(beacon_state)?;
let finalized_block_eth1data_proof =
ExecutionBlockProof::construct_from_beacon_block_body(finalized_block_body)?;
ExecutionBlockProof::construct_from_beacon_block_body(finalized_block_body, network_config, epoch.into())?;

Ok(FinalizedHeaderUpdate {
header_update: HeaderUpdate {
Expand Down

0 comments on commit e22cdfb

Please sign in to comment.