Skip to content

Commit

Permalink
Merge branch 'master' into add-near-plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
karim-en committed Mar 14, 2023
2 parents 63bff44 + aef9364 commit ebff1b5
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 59 deletions.
3 changes: 2 additions & 1 deletion contracts/eth/nearbridge/.catalog-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions contracts/near/eth-client/.catalog-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions eth2near/eth2-contract-init/src/config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -51,6 +52,9 @@ pub struct Config {
/// The trusted block root for checkpoint for contract initialization
/// e.g.: 0x9cd0c5a8392d0659426b12384e8440c147510ab93eeaeccb08435a462d7bb1c7
pub init_block_root: Option<String>,

// Beacon rpc version (V1_1, V1_2)
pub beacon_rpc_version: beacon_rpc_client::BeaconRPCVersion,
}

impl Config {
Expand Down
5 changes: 4 additions & 1 deletion eth2near/eth2-contract-init/src/init_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion eth2near/eth2near-block-relay-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ thread = "*"

[dev-dependencies]
workspaces = "0.5.0"
eth2-contract-init = { path = "../eth2-contract-init" }
eth2-contract-init = { path = "../eth2-contract-init" }
4 changes: 4 additions & 0 deletions eth2near/eth2near-block-relay-rs/src/config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<u32>,

// Beacon rpc version (V1_1, V1_2)
pub beacon_rpc_version: BeaconRPCVersion,
}

impl Config {
Expand Down
6 changes: 3 additions & 3 deletions eth2near/eth2near-block-relay-rs/src/contract_type.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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",
}
}
Expand Down
26 changes: 15 additions & 11 deletions eth2near/eth2near-block-relay-rs/src/eth2near_relay.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
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,
CHAIN_FINALIZED_EXECUTION_BLOCK_HEIGHT_ON_ETH, CHAIN_FINALIZED_EXECUTION_BLOCK_HEIGHT_ON_NEAR,
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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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::<NoBlockForSlotError>().is_some() {
Expand Down Expand Up @@ -1191,6 +1194,7 @@ mod tests {
"http://httpstat.us/504/",
TIMEOUT_SECONDS,
TIMEOUT_STATE_SECONDS,
None,
);

relay
Expand Down
17 changes: 17 additions & 0 deletions eth2near/eth2near-block-relay-rs/src/last_slot_searcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -814,6 +820,7 @@ mod tests {
"http://httpstat.us/504/",
TIMEOUT_SECONDS,
TIMEOUT_STATE_SECONDS,
None,
);
last_slot_searcher
.linear_slot_search(
Expand All @@ -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);
Expand Down Expand Up @@ -911,6 +919,7 @@ mod tests {
"http://httpstat.us/504/",
TIMEOUT_SECONDS,
TIMEOUT_STATE_SECONDS,
None,
);
if last_slot_searcher
.binsearch_slot_range(
Expand All @@ -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);
Expand Down Expand Up @@ -1014,6 +1024,7 @@ mod tests {
"http://httpstat.us/504/",
TIMEOUT_SECONDS,
TIMEOUT_STATE_SECONDS,
None,
);
if last_slot_searcher
.binsearch_slot_forward(
Expand All @@ -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);
Expand Down Expand Up @@ -1127,6 +1139,7 @@ mod tests {
"http://httpstat.us/504/",
TIMEOUT_SECONDS,
TIMEOUT_STATE_SECONDS,
None,
);
if last_slot_searcher
.binary_slot_search(
Expand All @@ -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);
Expand Down Expand Up @@ -1196,6 +1210,7 @@ mod tests {
"http://httpstat.us/504/",
TIMEOUT_SECONDS,
TIMEOUT_STATE_SECONDS,
None,
);
if last_slot_searcher
.get_last_slot(
Expand All @@ -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);
Expand Down Expand Up @@ -1264,6 +1280,7 @@ mod tests {
"http://httpstat.us/504/",
TIMEOUT_SECONDS,
TIMEOUT_STATE_SECONDS,
None,
);
if last_slot_searcher
.get_last_slot(
Expand Down
26 changes: 18 additions & 8 deletions eth2near/eth2near-block-relay-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,29 +48,33 @@ fn get_dao_contract_wrapper(config: &Config) -> Box<dyn ContractWrapper> {
&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"),
))
}

fn get_eth_client_contract(config: &Config) -> Box<dyn EthClientContractTrait> {
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,
Expand All @@ -90,7 +95,12 @@ fn init_log(args: &Arguments, config: &Config) {

fn main() -> Result<(), Box<dyn std::error::Error>> {
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(
Expand Down
Loading

0 comments on commit ebff1b5

Please sign in to comment.