diff --git a/contracts/eth/nearbridge/.catalog-info.yaml b/contracts/eth/nearbridge/.catalog-info.yaml index cddc1774c..5ed91d74e 100644 --- a/contracts/eth/nearbridge/.catalog-info.yaml +++ b/contracts/eth/nearbridge/.catalog-info.yaml @@ -42,7 +42,8 @@ spec: lifecycle: production system: bridge-protocol deployedAt: - - contract-deprecated:ethereum/mainnet/0x3be7df8db39996a837041bb8ee0dadf60f767038 - contract:ethereum/mainnet/0x3FEFc5A4B1c02f21cBc8D3613643ba0635b9a873 interactsWith: - relayer:ethereum/mainnet/0x015e634c7c1311a9034220c28d3d12b7f710a3b1 + deprecated: + - contract:ethereum/mainnet/0x3be7df8db39996a837041bb8ee0dadf60f767038 diff --git a/contracts/near/eth-client/.catalog-info.yaml b/contracts/near/eth-client/.catalog-info.yaml index 860d853cd..c718efb16 100644 --- a/contracts/near/eth-client/.catalog-info.yaml +++ b/contracts/near/eth-client/.catalog-info.yaml @@ -17,5 +17,6 @@ spec: system: bridge-protocol deployedAt: - contract:near/mainnet/client.bridge.near + - contract:near/mainnet/client-eth2.bridge.near interactsWith: - relayer:near/mainnet/relayer.bridge.near diff --git a/eth2near/eth2-contract-init/src/config.rs b/eth2near/eth2-contract-init/src/config.rs index 7e1ff3150..0dc82674c 100644 --- a/eth2near/eth2-contract-init/src/config.rs +++ b/eth2near/eth2-contract-init/src/config.rs @@ -1,6 +1,7 @@ use contract_wrapper::eth_network::EthNetwork; use contract_wrapper::near_network::NearNetwork; use contract_wrapper::near_rpc_client::NearRPCClient; +use eth_rpc_client::beacon_rpc_client; use reqwest::Url; use serde::Deserialize; use std::io::Read; @@ -51,6 +52,9 @@ pub struct Config { /// The trusted block root for checkpoint for contract initialization /// e.g.: 0x9cd0c5a8392d0659426b12384e8440c147510ab93eeaeccb08435a462d7bb1c7 pub init_block_root: Option, + + // Beacon rpc version (V1_1, V1_2) + pub beacon_rpc_version: beacon_rpc_client::BeaconRPCVersion, } impl Config { diff --git a/eth2near/eth2-contract-init/src/init_contract.rs b/eth2near/eth2-contract-init/src/init_contract.rs index 6b8d92b91..4aba0ca48 100644 --- a/eth2near/eth2-contract-init/src/init_contract.rs +++ b/eth2near/eth2-contract-init/src/init_contract.rs @@ -55,6 +55,7 @@ pub fn init_contract( &config.beacon_endpoint, config.eth_requests_timeout_seconds.unwrap_or(10), config.eth_requests_timeout_seconds.unwrap_or(10), + Some(config.beacon_rpc_version.clone()), ); let eth1_rpc_client = Eth1RPCClient::new(&config.eth1_endpoint); @@ -148,7 +149,7 @@ mod tests { use contract_wrapper::eth_client_contract_trait::EthClientContractTrait; use crate::init_contract::init_contract; use contract_wrapper::near_network::NearNetwork; - use eth_rpc_client::beacon_rpc_client::BeaconRPCClient; + use eth_rpc_client::beacon_rpc_client::{BeaconRPCClient, BeaconRPCVersion}; use crate::config_for_tests::ConfigForTests; const ONE_EPOCH_IN_SLOTS: u64 = 32; @@ -184,6 +185,7 @@ mod tests { max_submitted_blocks_by_account: Some(8000), trusted_signer_account_id: Some(eth_client_contract.get_signer_account_id().to_string()), init_block_root: None, + beacon_rpc_version: BeaconRPCVersion::V1_1, } } @@ -240,6 +242,7 @@ mod tests { &init_config.beacon_endpoint, init_config.eth_requests_timeout_seconds.unwrap_or(10), init_config.eth_requests_timeout_seconds.unwrap_or(10), + None, ); let last_finalized_slot_eth_network = beacon_rpc_client diff --git a/eth2near/eth2near-block-relay-rs/Cargo.toml b/eth2near/eth2near-block-relay-rs/Cargo.toml index 3fa35d8f1..db607ea2d 100644 --- a/eth2near/eth2near-block-relay-rs/Cargo.toml +++ b/eth2near/eth2near-block-relay-rs/Cargo.toml @@ -48,4 +48,4 @@ thread = "*" [dev-dependencies] workspaces = "0.5.0" -eth2-contract-init = { path = "../eth2-contract-init" } \ No newline at end of file +eth2-contract-init = { path = "../eth2-contract-init" } diff --git a/eth2near/eth2near-block-relay-rs/src/config.rs b/eth2near/eth2near-block-relay-rs/src/config.rs index 96fc43516..6a946ed7d 100644 --- a/eth2near/eth2near-block-relay-rs/src/config.rs +++ b/eth2near/eth2near-block-relay-rs/src/config.rs @@ -1,4 +1,5 @@ use crate::contract_type::ContractType; +use eth_rpc_client::beacon_rpc_client::BeaconRPCVersion; use contract_wrapper::eth_network::EthNetwork; use contract_wrapper::near_rpc_client::NearRPCClient; use contract_wrapper::near_network::NearNetwork; @@ -82,6 +83,9 @@ pub struct Config { /// Max number of unfinalized blocks allowed to be stored by one submitter account. /// It is used on initialization of the Eth2 client. pub max_submitted_blocks_by_account: Option, + + // Beacon rpc version (V1_1, V1_2) + pub beacon_rpc_version: BeaconRPCVersion, } impl Config { diff --git a/eth2near/eth2near-block-relay-rs/src/contract_type.rs b/eth2near/eth2near-block-relay-rs/src/contract_type.rs index 443df6922..4533029d3 100644 --- a/eth2near/eth2near-block-relay-rs/src/contract_type.rs +++ b/eth2near/eth2near-block-relay-rs/src/contract_type.rs @@ -1,8 +1,8 @@ +use serde::Deserialize; use std::error::Error; use std::fmt; use std::fmt::{Display, Formatter}; use std::str::FromStr; -use serde::Deserialize; #[derive(Debug, Clone, Deserialize)] pub enum ContractType { @@ -34,8 +34,8 @@ impl Display for ContractType { impl ContractType { pub fn as_str(&self) -> &str { match self { - ContractType::Near => "NEAR", - ContractType::Dao => "DAO", + ContractType::Near => "Near", + ContractType::Dao => "Dao", ContractType::File => "File", } } diff --git a/eth2near/eth2near-block-relay-rs/src/eth2near_relay.rs b/eth2near/eth2near-block-relay-rs/src/eth2near_relay.rs index db6d177e7..d6cc444c0 100644 --- a/eth2near/eth2near-block-relay-rs/src/eth2near_relay.rs +++ b/eth2near/eth2near-block-relay-rs/src/eth2near_relay.rs @@ -1,10 +1,5 @@ -use std::cmp; -use eth_rpc_client::beacon_rpc_client::BeaconRPCClient; use crate::config::Config; -use eth_rpc_client::eth1_rpc_client::Eth1RPCClient; -use eth_rpc_client::hand_made_finality_light_client_update::HandMadeFinalityLightClientUpdate; use crate::last_slot_searcher::LastSlotSearcher; -use contract_wrapper::near_rpc_client::NearRPCClient; use crate::prometheus_metrics; use crate::prometheus_metrics::{ CHAIN_EXECUTION_BLOCK_HEIGHT_ON_ETH, CHAIN_EXECUTION_BLOCK_HEIGHT_ON_NEAR, @@ -12,18 +7,23 @@ use crate::prometheus_metrics::{ FAILS_ON_HEADERS_SUBMISSION, FAILS_ON_UPDATES_SUBMISSION, LAST_ETH_SLOT, LAST_ETH_SLOT_ON_NEAR, LAST_FINALIZED_ETH_SLOT, LAST_FINALIZED_ETH_SLOT_ON_NEAR, }; -use eth_rpc_client::errors::NoBlockForSlotError; +use bitvec::macros::internal::funty::Fundamental; use contract_wrapper::eth_client_contract_trait::EthClientContractTrait; +use contract_wrapper::near_rpc_client::NearRPCClient; +use eth_rpc_client::beacon_rpc_client::BeaconRPCClient; +use eth_rpc_client::errors::NoBlockForSlotError; +use eth_rpc_client::eth1_rpc_client::Eth1RPCClient; +use eth_rpc_client::hand_made_finality_light_client_update::HandMadeFinalityLightClientUpdate; use eth_types::eth2::LightClientUpdate; use eth_types::BlockHeader; use log::{debug, info, trace, warn}; use near_primitives::views::FinalExecutionStatus; +use std::cmp; use std::error::Error; use std::thread; use std::thread::sleep; use std::time::Duration; use std::vec::Vec; -use bitvec::macros::internal::funty::Fundamental; use types::Slot; const ONE_EPOCH_IN_SLOTS: u64 = 32; @@ -112,9 +112,11 @@ impl Eth2NearRelay { &config.beacon_endpoint, config.eth_requests_timeout_seconds, config.state_requests_timeout_seconds, + Some(config.beacon_rpc_version.clone()), ); let next_light_client_update = - Self::get_light_client_update_from_file(config, &beacon_rpc_client).expect("Error on parsing light client update"); + Self::get_light_client_update_from_file(config, &beacon_rpc_client) + .expect("Error on parsing light client update"); let max_submitted_blocks_by_account = eth_contract .get_max_submitted_blocks_by_account() @@ -715,12 +717,12 @@ impl Eth2NearRelay { #[cfg(test)] mod tests { - use eth_rpc_client::beacon_rpc_client::BeaconRPCClient; use crate::config_for_tests::ConfigForTests; use crate::eth2near_relay::{Eth2NearRelay, ONE_EPOCH_IN_SLOTS}; - use eth_rpc_client::hand_made_finality_light_client_update::HandMadeFinalityLightClientUpdate; - use eth_rpc_client::errors::NoBlockForSlotError; use crate::test_utils::{get_relay, get_relay_from_slot, get_relay_with_update_from_file}; + use eth_rpc_client::beacon_rpc_client::BeaconRPCClient; + use eth_rpc_client::errors::NoBlockForSlotError; + use eth_rpc_client::hand_made_finality_light_client_update::HandMadeFinalityLightClientUpdate; use eth_types::eth2::LightClientUpdate; use eth_types::BlockHeader; use std::thread::sleep; @@ -1010,6 +1012,7 @@ mod tests { "http://httpstat.us/504/", TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); if let Err(err) = relay.get_execution_block_by_slot(config_for_test.slot_without_block) { if err.downcast_ref::().is_some() { @@ -1191,6 +1194,7 @@ mod tests { "http://httpstat.us/504/", TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); relay diff --git a/eth2near/eth2near-block-relay-rs/src/last_slot_searcher.rs b/eth2near/eth2near-block-relay-rs/src/last_slot_searcher.rs index 71d09947c..e59095c64 100644 --- a/eth2near/eth2near-block-relay-rs/src/last_slot_searcher.rs +++ b/eth2near/eth2near-block-relay-rs/src/last_slot_searcher.rs @@ -471,6 +471,7 @@ mod tests { &config_for_test.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let eth1_rpc_client = Eth1RPCClient::new(&config_for_test.eth1_endpoint); let last_slot_searcher = LastSlotSearcher::new(true); @@ -527,6 +528,7 @@ mod tests { &config_for_test.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let eth1_rpc_client = Eth1RPCClient::new(&config_for_test.eth1_endpoint); let last_slot_searcher = LastSlotSearcher::new(true); @@ -603,6 +605,7 @@ mod tests { &config_for_test.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let eth1_rpc_client = Eth1RPCClient::new(&config_for_test.eth1_endpoint); let last_slot_searcher = LastSlotSearcher::new(true); @@ -656,6 +659,7 @@ mod tests { &config_for_test.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let eth1_rpc_client = Eth1RPCClient::new(&config_for_test.eth1_endpoint); let last_slot_searcher = LastSlotSearcher::new(true); @@ -719,6 +723,7 @@ mod tests { &config_for_test.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let eth1_rpc_client = Eth1RPCClient::new(&config_for_test.eth1_endpoint); let last_slot_searcher = LastSlotSearcher::new(true); @@ -794,6 +799,7 @@ mod tests { &config_for_test.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let eth1_rpc_client = Eth1RPCClient::new(&config_for_test.eth1_endpoint); let last_slot_searcher = LastSlotSearcher::new(true); @@ -814,6 +820,7 @@ mod tests { "http://httpstat.us/504/", TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); last_slot_searcher .linear_slot_search( @@ -835,6 +842,7 @@ mod tests { &config_for_test.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let eth1_rpc_client = Eth1RPCClient::new(&config_for_test.eth1_endpoint); let last_slot_searcher = LastSlotSearcher::new(true); @@ -911,6 +919,7 @@ mod tests { "http://httpstat.us/504/", TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); if last_slot_searcher .binsearch_slot_range( @@ -937,6 +946,7 @@ mod tests { &config_for_test.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let eth1_rpc_client = Eth1RPCClient::new(&config_for_test.eth1_endpoint); let last_slot_searcher = LastSlotSearcher::new(true); @@ -1014,6 +1024,7 @@ mod tests { "http://httpstat.us/504/", TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); if last_slot_searcher .binsearch_slot_forward( @@ -1040,6 +1051,7 @@ mod tests { &config_for_test.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let eth1_rpc_client = Eth1RPCClient::new(&config_for_test.eth1_endpoint); let last_slot_searcher = LastSlotSearcher::new(true); @@ -1127,6 +1139,7 @@ mod tests { "http://httpstat.us/504/", TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); if last_slot_searcher .binary_slot_search( @@ -1151,6 +1164,7 @@ mod tests { &config_for_test.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let eth1_rpc_client = Eth1RPCClient::new(&config_for_test.eth1_endpoint); let mut last_slot_searcher = LastSlotSearcher::new(true); @@ -1196,6 +1210,7 @@ mod tests { "http://httpstat.us/504/", TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); if last_slot_searcher .get_last_slot( @@ -1218,6 +1233,7 @@ mod tests { &config_for_test.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let eth1_rpc_client = Eth1RPCClient::new(&config_for_test.eth1_endpoint); let mut last_slot_searcher = LastSlotSearcher::new(true); @@ -1264,6 +1280,7 @@ mod tests { "http://httpstat.us/504/", TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); if last_slot_searcher .get_last_slot( diff --git a/eth2near/eth2near-block-relay-rs/src/main.rs b/eth2near/eth2near-block-relay-rs/src/main.rs index 0b3b85cf0..f7ec8f42b 100644 --- a/eth2near/eth2near-block-relay-rs/src/main.rs +++ b/eth2near/eth2near-block-relay-rs/src/main.rs @@ -6,6 +6,7 @@ use contract_wrapper::{ dao_contract, dao_eth_client_contract, eth_client_contract, file_eth_client_contract, }; use eth2_to_near_relay::config::Config; +use eth2_to_near_relay::contract_type::ContractType; use eth2_to_near_relay::eth2near_relay::Eth2NearRelay; use eth2near_logger::SimpleLogger; use log::LevelFilter; @@ -47,7 +48,8 @@ fn get_dao_contract_wrapper(config: &Config) -> Box { &config.near_endpoint, &config.signer_account_id, &config.path_to_signer_secret_key, - &dao_contract_account_id.expect("No DAO contract account ID provided for relay running in DAO mode"), + &dao_contract_account_id + .expect("No DAO contract account ID provided for relay running in DAO mode"), )) } @@ -55,21 +57,24 @@ fn get_eth_client_contract(config: &Config) -> Box { let eth_contract_wrapper = get_eth_contract_wrapper(config); let eth_client = eth_client_contract::EthClientContract::new(eth_contract_wrapper); - match config.contract_type.as_str() { - "dao" => Box::new(dao_eth_client_contract::DaoEthClientContract::new( + match config.contract_type { + ContractType::Dao => Box::new(dao_eth_client_contract::DaoEthClientContract::new( eth_client, dao_contract::DAOContract::new(get_dao_contract_wrapper(config)), )), - "file" => Box::new(file_eth_client_contract::FileEthClientContract::new( + ContractType::File => Box::new(file_eth_client_contract::FileEthClientContract::new( eth_client, - config.output_dir.clone().expect("No output dir provided for relay running in FILE mode"), + config + .output_dir + .clone() + .expect("No output dir provided for relay running in FILE mode"), )), - _ => Box::new(eth_client), + ContractType::Near => Box::new(eth_client), } } fn init_log(args: &Arguments, config: &Config) { - let log_level_filter = match args.log_level.as_str() { + let log_level_filter = match args.log_level.to_lowercase().as_str() { "trace" => LevelFilter::Trace, "debug" => LevelFilter::Debug, "warn" => LevelFilter::Warn, @@ -90,7 +95,12 @@ fn init_log(args: &Arguments, config: &Config) { fn main() -> Result<(), Box> { let args = Arguments::parse(); - let config = Config::load_from_toml(args.config.clone().try_into().expect("Error on config parsing")); + let config = Config::load_from_toml( + args.config + .clone() + .try_into() + .expect("Error on config parsing"), + ); init_log(&args, &config); let mut eth2near_relay = Eth2NearRelay::init( diff --git a/eth2near/eth2near-block-relay-rs/src/test_utils.rs b/eth2near/eth2near-block-relay-rs/src/test_utils.rs index 5488afcd7..0835b7733 100644 --- a/eth2near/eth2near-block-relay-rs/src/test_utils.rs +++ b/eth2near/eth2near-block-relay-rs/src/test_utils.rs @@ -1,4 +1,4 @@ -use eth_rpc_client::beacon_rpc_client::BeaconRPCClient; +use eth_rpc_client::beacon_rpc_client::{BeaconRPCClient, BeaconRPCVersion}; use crate::config::Config; use crate::config_for_tests::ConfigForTests; use eth_rpc_client::eth1_rpc_client::Eth1RPCClient; @@ -108,7 +108,7 @@ pub fn init_contract_from_specific_slot( .unwrap(); let beacon_rpc_client = - BeaconRPCClient::new(&config_for_test.beacon_endpoint, TIMEOUT, TIMEOUT_STATE); + BeaconRPCClient::new(&config_for_test.beacon_endpoint, TIMEOUT, TIMEOUT_STATE, None); let eth1_rpc_client = Eth1RPCClient::new(&config_for_test.eth1_endpoint); let finality_header = beacon_rpc_client @@ -203,6 +203,7 @@ fn get_config(config_for_test: &ConfigForTests) -> Config { sleep_time_after_submission_secs: 5, hashes_gc_threshold: None, max_submitted_blocks_by_account: None, + beacon_rpc_version: BeaconRPCVersion::V1_1, } } @@ -227,6 +228,7 @@ fn get_init_config( max_submitted_blocks_by_account: Some(8000), trusted_signer_account_id: Some(eth_client_contract.get_signer_account_id().to_string()), init_block_root: None, + beacon_rpc_version: BeaconRPCVersion::V1_1, } } diff --git a/eth2near/eth_rpc_client/src/beacon_rpc_client.rs b/eth2near/eth_rpc_client/src/beacon_rpc_client.rs index 0611baa01..537a73704 100644 --- a/eth2near/eth_rpc_client/src/beacon_rpc_client.rs +++ b/eth2near/eth_rpc_client/src/beacon_rpc_client.rs @@ -1,7 +1,10 @@ +use crate::errors::{ + ErrorOnJsonParse, ExecutionPayloadError, FailOnGettingJson, MissSyncAggregationError, + NoBlockForSlotError, SignatureSlotNotFoundError, +}; use crate::execution_block_proof::ExecutionBlockProof; -use crate::errors::{ErrorOnJsonParse, ExecutionPayloadError, FailOnGettingJson, MissSyncAggregationError, NoBlockForSlotError, SignatureSlotNotFoundError}; -use crate::utils; use crate::light_client_snapshot_with_proof::LightClientSnapshotWithProof; +use crate::utils; use eth_types::eth2::BeaconBlockHeader; use eth_types::eth2::FinalizedHeaderUpdate; use eth_types::eth2::HeaderUpdate; @@ -13,6 +16,7 @@ use eth_types::eth2::SyncCommitteeUpdate; use eth_types::H256; use log::trace; use reqwest::blocking::Client; +use serde::Deserialize; use serde_json::{json, Value}; use std::error::Error; use std::string::String; @@ -20,6 +24,46 @@ use std::time::Duration; use types::MainnetEthSpec; use types::{BeaconBlockBody, BeaconState}; +#[derive(Debug, Clone, Deserialize)] +pub enum BeaconRPCVersion { + V1_1, + V1_2, +} + +struct BeaconRPCRoutes { + pub get_block_header: String, + pub get_block: String, + pub get_light_client_update: String, + pub get_light_client_finality_update: String, + pub get_bootstrap: String, + pub get_state: String, +} + +impl BeaconRPCRoutes { + pub fn new(version: BeaconRPCVersion) -> Self { + match version { + BeaconRPCVersion::V1_1 => Self { + get_block_header: "eth/v1/beacon/headers".to_string(), + get_block: "eth/v2/beacon/blocks".to_string(), + get_light_client_update: "eth/v1/beacon/light_client/updates".to_string(), + get_light_client_finality_update: "eth/v1/beacon/light_client/finality_update/" + .to_string(), + get_bootstrap: "eth/v1/beacon/light_client/bootstrap".to_string(), + get_state: "eth/v2/debug/beacon/states".to_string(), + }, + BeaconRPCVersion::V1_2 => Self { + get_block_header: "eth/v1/beacon/headers".to_string(), + get_block: "eth/v2/beacon/blocks".to_string(), + get_light_client_update: "eth/v1/beacon/light_client/updates".to_string(), + get_light_client_finality_update: "eth/v1/beacon/light_client/finality_update" + .to_string(), + get_bootstrap: "eth/v1/beacon/light_client/bootstrap".to_string(), + get_state: "eth/v2/debug/beacon/states".to_string(), + }, + } + } +} + /// `BeaconRPCClient` allows getting beacon block body, beacon block header /// and light client updates /// using Beacon RPC API (https://ethereum.github.io/beacon-APIs/) @@ -27,22 +71,20 @@ pub struct BeaconRPCClient { endpoint_url: String, client: Client, client_state_request: Client, + routes: BeaconRPCRoutes, } impl BeaconRPCClient { - const URL_HEADER_PATH: &'static str = "eth/v1/beacon/headers"; - const URL_BODY_PATH: &'static str = "eth/v2/beacon/blocks"; - const URL_GET_LIGHT_CLIENT_UPDATE_API: &'static str = "eth/v1/beacon/light_client/updates"; - const URL_FINALITY_LIGHT_CLIENT_UPDATE_PATH: &'static str = - "eth/v1/beacon/light_client/finality_update/"; - const URL_GET_BOOTSTRAP: &'static str = "eth/v1/beacon/light_client/bootstrap"; - const URL_STATE_PATH: &'static str = "eth/v2/debug/beacon/states"; - const SLOTS_PER_EPOCH: u64 = 32; const EPOCHS_PER_PERIOD: u64 = 256; /// Creates `BeaconRPCClient` for the given BeaconAPI `endpoint_url` - pub fn new(endpoint_url: &str, timeout_seconds: u64, timeout_state_seconds: u64) -> Self { + pub fn new( + endpoint_url: &str, + timeout_seconds: u64, + timeout_state_seconds: u64, + version: Option, + ) -> Self { Self { endpoint_url: endpoint_url.to_string(), client: reqwest::blocking::Client::builder() @@ -53,6 +95,7 @@ impl BeaconRPCClient { .timeout(Duration::from_secs(timeout_state_seconds)) .build() .expect("Error on building blocking client for state request."), + routes: BeaconRPCRoutes::new(version.unwrap_or(BeaconRPCVersion::V1_1)), } } @@ -67,7 +110,10 @@ impl BeaconRPCClient { &self, block_id: &str, ) -> Result, Box> { - let url = format!("{}/{}/{}", self.endpoint_url, Self::URL_BODY_PATH, block_id); + let url = format!( + "{}/{}/{}", + self.endpoint_url, self.routes.get_block, block_id + ); let json_str = &self.get_json_from_raw_request(&url)?; @@ -90,9 +136,7 @@ impl BeaconRPCClient { ) -> Result> { let url = format!( "{}/{}/{}", - self.endpoint_url, - Self::URL_HEADER_PATH, - block_id + self.endpoint_url, self.routes.get_block_header, block_id ); let json_str = &self.get_json_from_raw_request(&url)?; @@ -113,9 +157,7 @@ impl BeaconRPCClient { ) -> Result> { let url = format!( "{}/{}?start_period={}&count=1", - self.endpoint_url, - Self::URL_GET_LIGHT_CLIENT_UPDATE_API, - period + self.endpoint_url, self.routes.get_light_client_update, period ); let light_client_update_json_str = self.get_json_from_raw_request(&url)?; @@ -147,9 +189,7 @@ impl BeaconRPCClient { ) -> Result> { let url = format!( "{}/{}/{}", - self.endpoint_url, - Self::URL_GET_BOOTSTRAP, - block_root + self.endpoint_url, self.routes.get_bootstrap, block_root ); let light_client_snapshot_json_str = self.get_json_from_raw_request(&url)?; @@ -200,9 +240,7 @@ impl BeaconRPCClient { let url = format!( "{}/{}/{}", - self.endpoint_url, - Self::URL_BODY_PATH, - beacon_block_hash_str + self.endpoint_url, self.routes.get_block, beacon_block_hash_str ); let block_json_str = &self.get_json_from_raw_request(&url)?; let v: Value = serde_json::from_str(block_json_str)?; @@ -223,8 +261,7 @@ impl BeaconRPCClient { pub fn get_finality_light_client_update(&self) -> Result> { let url = format!( "{}/{}", - self.endpoint_url, - Self::URL_FINALITY_LIGHT_CLIENT_UPDATE_PATH + self.endpoint_url, self.routes.get_light_client_finality_update, ); let light_client_update_json_str = self.get_json_from_raw_request(&url)?; @@ -265,9 +302,7 @@ impl BeaconRPCClient { ) -> Result, Box> { let url_request = format!( "{}/{}/{}", - self.endpoint_url, - Self::URL_STATE_PATH, - state_id + self.endpoint_url, self.routes.get_state, state_id ); let json_str = Self::get_json_from_client(&self.client_state_request, &url_request)?; @@ -486,11 +521,11 @@ mod tests { use crate::beacon_rpc_client::BeaconRPCClient; use crate::config_for_tests::ConfigForTests; use crate::utils::read_json_file_from_data_dir; + use crate::utils::trim_quotes; use serde_json::Value; use types::BeaconBlockBody; use types::BeaconBlockHeader; use types::MainnetEthSpec; - use crate::utils::trim_quotes; const TIMEOUT_SECONDS: u64 = 30; const TIMEOUT_STATE_SECONDS: u64 = 1000; @@ -553,7 +588,8 @@ mod tests { "{}/eth/v2/beacon/blocks/{}", config.beacon_endpoint, config.first_slot ); - let beacon_rpc_client = BeaconRPCClient::new(&url, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS); + let beacon_rpc_client = + BeaconRPCClient::new(&url, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, None); let rpc_json_str = beacon_rpc_client.get_json_from_raw_request(&url); assert_eq!(rpc_json_str.unwrap(), file_json_str.trim()); } @@ -566,6 +602,7 @@ mod tests { &config.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ) .get_beacon_block_body_for_block_id(&config.first_slot.to_string()) .unwrap(); @@ -573,6 +610,7 @@ mod tests { &config.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ) .get_beacon_block_header_for_block_id(&config.first_slot.to_string()) .unwrap(); @@ -585,6 +623,7 @@ mod tests { &config.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ) .get_beacon_block_header_for_block_id(&format!("{}", config.first_slot)) .unwrap(); @@ -627,6 +666,7 @@ mod tests { &config.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ) .get_beacon_block_body_for_block_id(&config.first_slot.to_string()) .unwrap(); @@ -652,7 +692,8 @@ mod tests { assert!(!BeaconRPCClient::new( "https://lodestar-goerli.chainsafe.io", TIMEOUT_SECONDS, - TIMEOUT_STATE_SECONDS + TIMEOUT_STATE_SECONDS, + None ) .is_syncing() .unwrap()); @@ -699,6 +740,7 @@ mod tests { &config.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let file_json_str = std::fs::read_to_string(&config.path_to_light_client_update) .expect("Unable to read file"); diff --git a/eth2near/eth_rpc_client/src/execution_block_proof.rs b/eth2near/eth_rpc_client/src/execution_block_proof.rs index 7ee19d96f..51cb9e24a 100644 --- a/eth2near/eth_rpc_client/src/execution_block_proof.rs +++ b/eth2near/eth_rpc_client/src/execution_block_proof.rs @@ -218,6 +218,7 @@ mod tests { &config.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let beacon_block_body = beacon_rpc_client diff --git a/eth2near/eth_rpc_client/src/hand_made_finality_light_client_update.rs b/eth2near/eth_rpc_client/src/hand_made_finality_light_client_update.rs index bf85821de..74aa4e617 100644 --- a/eth2near/eth_rpc_client/src/hand_made_finality_light_client_update.rs +++ b/eth2near/eth_rpc_client/src/hand_made_finality_light_client_update.rs @@ -373,6 +373,7 @@ mod tests { &config.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let light_client_period = @@ -409,6 +410,7 @@ mod tests { &config.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let hand_made_light_client_update = HandMadeFinalityLightClientUpdate::get_finality_light_client_update_from_file( @@ -435,6 +437,7 @@ mod tests { &config.beacon_endpoint, TIMEOUT_SECONDS, TIMEOUT_STATE_SECONDS, + None, ); let hand_made_light_client_update = HandMadeFinalityLightClientUpdate::get_light_client_update_from_file_with_next_sync_committee(