Skip to content

Commit

Permalink
Eth2Near-relay: split relay on a few crates, many improvements (#825)
Browse files Browse the repository at this point in the history
* Separate `logger`, `eth_rpc_client`, and `Eth2OnNearClient` contract initialization into separate crates
* Check configuration setup for Mainnet: add a verification on init args that it can't be executed in the trustless mode without `bls` verification on Mainnet (#767). 
* Improve config file usage on contract initialization: `validate_updates`, `verify_bls_signatures`, `hashes_gc_threshold`, `max_submitted_blocks_by_account`, `trusted_signer_account_id` fields are set through the config file instead of hardcoded values (#769). 
* Improve configuration of some args: `network`, `contract_type`, and `near_network_id` now use `enum`s instead of `String`s (#811).
* Add messages for unhandled `unwraps()` on relay start-up (#816).

Co-authored-by: Kirill <[email protected]>
Co-authored-by: Karim <[email protected]>
  • Loading branch information
3 people authored Oct 23, 2022
1 parent bc16598 commit 2230d63
Show file tree
Hide file tree
Showing 57 changed files with 10,913 additions and 479 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/contracts-near.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ jobs:
./test.sh
timeout-minutes: 20

- job-name: test eth2near/eth2-contract-init
cmd: |
cd eth2near/eth2-contract-init
./test.sh
timeout-minutes: 20

- job-name: test eth2near/eth_rpc_client
cmd: |
cd eth2near/eth_rpc_client
./test.sh
timeout-minutes: 20

name: ${{ matrix.job-name }}
steps:
- name: Clone the repository
Expand Down
10 changes: 4 additions & 6 deletions eth2near/contract_wrapper/src/dao_eth_client_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ impl EthClientContractTrait for DaoEthClientContract {
mod tests {
use crate::eth_client_contract_trait::EthClientContractTrait;
use crate::near_contract_wrapper::NearContractWrapper;
use crate::{
dao_contract, dao_eth_client_contract, eth_client_contract, near_contract_wrapper, utils,
};
use crate::{dao_contract, dao_eth_client_contract, eth_client_contract, eth_network, near_contract_wrapper, utils};
use eth_types::eth2::{ExtendedBeaconBlockHeader, LightClientUpdate, SyncCommittee};
use eth_types::BlockHeader;
use std::path::PathBuf;
Expand Down Expand Up @@ -180,8 +178,6 @@ mod tests {
const CONTRACT_ACCOUNT_ID: &str = "dev-1660212590113-35162107482173";
const DAO_CONTRACT_ACCOUNT_ID: &str = "eth2-test.sputnikv2.testnet";

const ETH_NETWORK: &str = "kiln";

let near_contract_wrapper = Box::new(NearContractWrapper::new_with_raw_secret_key(
NEAR_ENDPOINT,
&signer_account_id,
Expand Down Expand Up @@ -232,11 +228,13 @@ mod tests {
}

eth_client.init_contract(
ETH_NETWORK.to_string(),
eth_network::EthNetwork::Kiln,
finalized_execution_header.unwrap(),
finalized_beacon_header,
current_sync_committee,
next_sync_committee,
Some(true),
Some(false),
None,
None,
Some(eth_client.contract_wrapper.get_signer_account_id()),
Expand Down
20 changes: 13 additions & 7 deletions eth2near/contract_wrapper/src/eth_client_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use serde_json::json;
use std::error::Error;
use std::option::Option;
use std::string::String;
use crate::eth_network::EthNetwork;
use serde::Serialize;

/// Implementation for interaction with Ethereum Light Client Contract on NEAR.
Expand Down Expand Up @@ -46,11 +47,13 @@ impl EthClientContract {
/// * `trusted_signer` - the account address of the trusted signer which is allowed to submit light client updates.
pub fn init_contract(
&self,
ethereum_network: String,
ethereum_network: EthNetwork,
finalized_execution_header: BlockHeader,
finalized_beacon_header: ExtendedBeaconBlockHeader,
current_sync_committee: SyncCommittee,
next_sync_committee: SyncCommittee,
validate_updates: Option<bool>,
verify_bls_signatures: Option<bool>,
hashes_gc_threshold: Option<u64>,
max_submitted_blocks_by_account: Option<u32>,
trusted_signer: Option<AccountId>,
Expand All @@ -70,13 +73,13 @@ impl EthClientContract {
}

let init_input = InitInput {
network: ethereum_network,
network: ethereum_network.to_string(),
finalized_execution_header,
finalized_beacon_header,
current_sync_committee,
next_sync_committee,
validate_updates: true,
verify_bls_signatures: false,
validate_updates: validate_updates.unwrap_or(true),
verify_bls_signatures: verify_bls_signatures.unwrap_or(false),
hashes_gc_threshold: hashes_gc_threshold.unwrap_or(51_000),
max_submitted_blocks_by_account: max_submitted_blocks_by_account.unwrap_or(8000),
trusted_signer,
Expand All @@ -102,7 +105,9 @@ impl EthClientContract {
self.contract_wrapper.get_account_id()
}

pub fn get_signature_account_id(&self) -> AccountId { self.contract_wrapper.get_signer_account_id() }
pub fn get_signer_account_id(&self) -> AccountId {
self.contract_wrapper.get_signer_account_id()
}
}

impl EthClientContractTrait for EthClientContract {
Expand Down Expand Up @@ -328,7 +333,6 @@ mod tests {
const PATH_TO_CURRENT_SYNC_COMMITTEE: &str =
"./data/next_sync_committee_kiln_period_133.json";
const PATH_TO_NEXT_SYNC_COMMITTEE: &str = "./data/next_sync_committee_kiln_period_134.json";
const ETH_NETWORK: &str = "kiln";

let current_sync_committee: SyncCommittee = serde_json::from_str(
&std::fs::read_to_string(PATH_TO_CURRENT_SYNC_COMMITTEE).expect("Unable to read file"),
Expand Down Expand Up @@ -361,11 +365,13 @@ mod tests {
}

eth_client_contract.init_contract(
ETH_NETWORK.to_string(),
eth_client_contract::EthNetwork::Kiln,
finalized_execution_header.unwrap(),
finalized_beacon_header,
current_sync_committee,
next_sync_committee,
Some(true),
Some(false),
None,
None,
Option::<AccountId>::Some(trusted_signer.parse().unwrap()),
Expand Down
58 changes: 58 additions & 0 deletions eth2near/contract_wrapper/src/eth_network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 EthNetwork {
Mainnet,
Kiln,
Ropsten,
Goerli,
}

#[derive(Debug, Clone, Deserialize)]
pub struct IncorrectEthNetwork;

impl Display for IncorrectEthNetwork {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Unknown Ethereum network. Possible networks: 'Mainnet', 'Kiln', 'Goerli', 'Ropsten'"
)
}
}

impl Error for IncorrectEthNetwork {}

impl Display for EthNetwork {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}

impl EthNetwork {
pub fn as_str(&self) -> &str {
match self {
EthNetwork::Mainnet => "mainnet",
EthNetwork::Kiln => "kiln",
EthNetwork::Goerli => "goerli",
EthNetwork::Ropsten => "ropsten"
}
}
}

impl FromStr for EthNetwork {
type Err = IncorrectEthNetwork;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"mainnet" => Ok(EthNetwork::Mainnet),
"kiln" => Ok(EthNetwork::Kiln),
"goerli" => Ok(EthNetwork::Goerli),
"ropsten" => Ok(EthNetwork::Ropsten),
_ => Err(IncorrectEthNetwork),
}
}
}
5 changes: 4 additions & 1 deletion eth2near/contract_wrapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ pub mod eth_client_contract_trait;
pub mod file_eth_client_contract;
pub mod near_contract_wrapper;
pub mod sandbox_contract_wrapper;
pub mod utils;
pub mod utils;
pub mod eth_network;
pub mod near_network;
pub mod near_rpc_client;
52 changes: 52 additions & 0 deletions eth2near/contract_wrapper/src/near_network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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 NearNetwork {
Mainnet,
Testnet,
}

#[derive(Debug, Clone, Deserialize)]
pub struct IncorrectNearNetwork;

impl Display for IncorrectNearNetwork {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Unknown NEAR network id. Possible networks: 'Mainnet', 'Testnet'"
)
}
}

impl Error for IncorrectNearNetwork {}

impl Display for NearNetwork {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}

impl NearNetwork {
pub fn as_str(&self) -> &str {
match self {
NearNetwork::Mainnet => "mainnet",
NearNetwork::Testnet => "testnet",
}
}
}

impl FromStr for NearNetwork {
type Err = IncorrectNearNetwork;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"mainnet" => Ok(NearNetwork::Mainnet),
"testnet" => Ok(NearNetwork::Testnet),
_ => Err(IncorrectNearNetwork),
}
}
}
Loading

0 comments on commit 2230d63

Please sign in to comment.