From c55f79ace1c2932199d903a6ad54890dda78974a Mon Sep 17 00:00:00 2001 From: Karim Date: Thu, 22 Sep 2022 02:40:51 +0100 Subject: [PATCH] Eth2-to-Near-relay: improve client initialization (#819) * Improve init of the Eth2Client contract * Move the default init to the client wrapper method * Fix tests * Print init eth2 client input Co-authored-by: Kirill --- .../src/dao_eth_client_contract.rs | 3 +++ .../src/eth_client_contract.rs | 23 ++++++++++++++----- .../eth2near-block-relay-rs/src/config.rs | 9 ++++++++ .../src/init_contract.rs | 10 ++++++++ .../eth2near-block-relay-rs/src/test_utils.rs | 8 +++++++ 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/eth2near/contract_wrapper/src/dao_eth_client_contract.rs b/eth2near/contract_wrapper/src/dao_eth_client_contract.rs index c26e3b1da..164919cac 100644 --- a/eth2near/contract_wrapper/src/dao_eth_client_contract.rs +++ b/eth2near/contract_wrapper/src/dao_eth_client_contract.rs @@ -224,6 +224,9 @@ mod tests { finalized_beacon_header, current_sync_committee, next_sync_committee, + None, + None, + Some(eth_client.contract_wrapper.get_signer_account_id()), ); let dao_contract_wrapper = diff --git a/eth2near/contract_wrapper/src/eth_client_contract.rs b/eth2near/contract_wrapper/src/eth_client_contract.rs index 20144af4c..3e8c550b4 100644 --- a/eth2near/contract_wrapper/src/eth_client_contract.rs +++ b/eth2near/contract_wrapper/src/eth_client_contract.rs @@ -14,10 +14,10 @@ use std::error::Error; use std::option::Option; use std::string::String; use std::vec::Vec; - +use serde::Serialize; pub struct EthClientContract { last_slot: u64, - contract_wrapper: Box, + pub contract_wrapper: Box, } impl EthClientContract { @@ -35,8 +35,11 @@ impl EthClientContract { finalized_beacon_header: ExtendedBeaconBlockHeader, current_sync_committee: SyncCommittee, next_sync_committee: SyncCommittee, + hashes_gc_threshold: Option, + max_submitted_blocks_by_account: Option, + trusted_signer: Option, ) { - #[derive(BorshSerialize)] + #[derive(BorshSerialize, Serialize)] pub struct InitInput { pub network: String, pub finalized_execution_header: eth_types::BlockHeader, @@ -58,11 +61,16 @@ impl EthClientContract { next_sync_committee, validate_updates: true, verify_bls_signatures: false, - hashes_gc_threshold: 51000, - max_submitted_blocks_by_account: 8000, - trusted_signer: Option::::None, + 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, }; + println!( + "Init eth2 client input: \n {}", + serde_json::to_string_pretty(&init_input).unwrap() + ); + self.contract_wrapper .call_change_method( "init".to_string(), @@ -319,6 +327,9 @@ mod tests { finalized_beacon_header, current_sync_committee, next_sync_committee, + None, + None, + None, ); eth_state.current_light_client_update = 1; } diff --git a/eth2near/eth2near-block-relay-rs/src/config.rs b/eth2near/eth2near-block-relay-rs/src/config.rs index 49153c33a..01319232f 100644 --- a/eth2near/eth2near-block-relay-rs/src/config.rs +++ b/eth2near/eth2near-block-relay-rs/src/config.rs @@ -70,6 +70,15 @@ pub struct Config { // Sleep time in seconds after blocks/light_client_update submission to client pub sleep_time_after_submission_secs: u64, + + /// Max number of stored blocks in the storage of the eth2 client contract. + /// Events that happen past this threshold cannot be verified by the client. + /// It is used on initialization of the Eth2 client. + pub hashes_gc_threshold: Option, + + /// 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, } impl Config { diff --git a/eth2near/eth2near-block-relay-rs/src/init_contract.rs b/eth2near/eth2near-block-relay-rs/src/init_contract.rs index 11c75997a..c6c58c07b 100644 --- a/eth2near/eth2near-block-relay-rs/src/init_contract.rs +++ b/eth2near/eth2near-block-relay-rs/src/init_contract.rs @@ -65,6 +65,16 @@ pub fn init_contract( finalized_header, current_sync_committee, next_sync_committee, + config.hashes_gc_threshold, + config.max_submitted_blocks_by_account, + Some( + config + .dao_contract_account_id + .as_ref() + .unwrap_or(&config.signer_account_id) + .parse() + .unwrap(), + ), ); thread::sleep(time::Duration::from_secs(30)); diff --git a/eth2near/eth2near-block-relay-rs/src/test_utils.rs b/eth2near/eth2near-block-relay-rs/src/test_utils.rs index 35791faca..72259ce34 100644 --- a/eth2near/eth2near-block-relay-rs/src/test_utils.rs +++ b/eth2near/eth2near-block-relay-rs/src/test_utils.rs @@ -78,6 +78,9 @@ pub fn init_contract_from_files(eth_client_contract: &mut EthClientContract) { finalized_beacon_header, current_sync_committee, next_sync_committee, + None, + None, + Some(eth_client_contract.contract_wrapper.get_signer_account_id()), ); thread::sleep(time::Duration::from_secs(30)); } @@ -154,6 +157,9 @@ pub fn init_contract_from_specific_slot( finalized_beacon_header, current_sync_committee, next_sync_committee, + None, + None, + Some(eth_client_contract.contract_wrapper.get_signer_account_id()), ); thread::sleep(time::Duration::from_secs(30)); @@ -207,6 +213,8 @@ fn get_config() -> Config { state_requests_timeout_seconds: 1000, sleep_time_after_submission_secs: 5, sleep_time_on_sync_secs: 30, + hashes_gc_threshold: None, + max_submitted_blocks_by_account: None, } }