Skip to content

Commit

Permalink
Merge branch 'master' into eth2near-relay-separate-init
Browse files Browse the repository at this point in the history
  • Loading branch information
olga24912 committed Oct 12, 2022
2 parents a2dc229 + f015f8f commit e191f98
Show file tree
Hide file tree
Showing 29 changed files with 3,971 additions and 420 deletions.
3,515 changes: 3,515 additions & 0 deletions eth2near/contract_wrapper/Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion eth2near/contract_wrapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ borsh = "0.9.3"
futures = "0.3.21"
async-std = "1.12.0"
near-sdk = "4.0.0"
near-jsonrpc-client = "0.4.0-beta.0"
near-jsonrpc-client = "=0.4.0-beta.0"
near-crypto = "0.14.0"
near-primitives = "0.14.0"
near-chain-configs = "0.14.0"
Expand Down
2 changes: 2 additions & 0 deletions eth2near/contract_wrapper/rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "1.64.0"
16 changes: 8 additions & 8 deletions eth2near/contract_wrapper/src/dao_contract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::dao_types::*;
use crate::dao_types;
use eth_types::eth2::LightClientUpdate;
use near_primitives::views::FinalExecutionOutcomeView;
use near_sdk::borsh::BorshSerialize;
Expand Down Expand Up @@ -30,7 +30,7 @@ impl DAOContract {
}

/// Gets the proposal for a given proposal ID
pub fn get_proposal(&self, id: u64) -> Result<ProposalOutput, Box<dyn Error>> {
pub fn get_proposal(&self, id: u64) -> Result<dao_types::ProposalOutput, Box<dyn Error>> {
let response = self.contract_wrapper.call_view_function(
"get_proposal".to_string(),
json!({ "id": id }).to_string().into_bytes(),
Expand All @@ -40,7 +40,7 @@ impl DAOContract {
}

/// Gets policy of the DAO contract
pub fn get_policy(&self) -> Result<Policy, Box<dyn Error>> {
pub fn get_policy(&self) -> Result<dao_types::Policy, Box<dyn Error>> {
let response = self
.contract_wrapper
.call_view_function("get_policy".to_string(), json!({}).to_string().into_bytes())?;
Expand All @@ -51,7 +51,7 @@ impl DAOContract {
/// Submits a new proposal to the DAO contract
pub fn add_proposal(
&mut self,
proposal: ProposalInput,
proposal: dao_types::ProposalInput,
) -> Result<(u64, FinalExecutionOutcomeView), Box<dyn Error>> {
let policy = self.get_policy()?;
let response = self.contract_wrapper.call_change_method(
Expand Down Expand Up @@ -80,7 +80,7 @@ impl DAOContract {
pub fn act_proposal(
&self,
id: u64,
action: Action,
action: dao_types::Action,
) -> Result<FinalExecutionOutcomeView, Box<dyn Error>> {
self.contract_wrapper.call_change_method(
"act_proposal".to_string(),
Expand All @@ -107,20 +107,20 @@ impl DAOContract {
let args = Base64VecU8::from(raw_update);

const GAS_FOR_SUBMIT_LIGHT_CLIENT_UPDATE: u64 = 270 * Gas::ONE_TERA.0;
let action = ActionCall {
let action = dao_types::ActionCall {
method_name: "submit_beacon_chain_light_client_update".to_string(),
args,
deposit: 0.into(),
gas: GAS_FOR_SUBMIT_LIGHT_CLIENT_UPDATE.into(),
};

let proposal_input = ProposalInput {
let proposal_input = dao_types::ProposalInput {
description: json!({
"finalized slot": update.finality_update.header_update.beacon_header.slot,
"update_hash": update_hash.to_string()
})
.to_string(),
kind: ProposalKind::FunctionCall {
kind: dao_types::ProposalKind::FunctionCall {
receiver_id,
actions: vec![action],
},
Expand Down
13 changes: 10 additions & 3 deletions eth2near/contract_wrapper/src/dao_eth_client_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::error::Error;
use std::str::FromStr;
use std::thread;
use std::time::Duration;
use std::vec::Vec;

/// Implementation of Ethereum Light Client Contract interaction on NEAR
/// having intermediate submission of Light Client Updates to the DAO contract.
Expand Down Expand Up @@ -71,7 +70,7 @@ impl EthClientContractTrait for DaoEthClientContract {
let (proposal_id, execution_outcome) =
self.dao_contract.submit_light_client_update_proposal(
near_sdk::AccountId::from_str(
&self.eth_client_contract.get_account_id().to_string(),
&self.eth_client_contract.get_account_id(),
)?,
light_client_update,
)?;
Expand Down Expand Up @@ -100,7 +99,7 @@ impl EthClientContractTrait for DaoEthClientContract {

fn send_headers(
&mut self,
headers: &Vec<BlockHeader>,
headers: &[BlockHeader],
end_slot: u64,
) -> Result<FinalExecutionOutcomeView, Box<dyn std::error::Error>> {
self.eth_client_contract.send_headers(headers, end_slot)
Expand All @@ -121,6 +120,14 @@ impl EthClientContractTrait for DaoEthClientContract {
fn get_light_client_state(&self) -> Result<LightClientState, Box<dyn Error>> {
self.eth_client_contract.get_light_client_state()
}

fn get_num_of_submitted_blocks_by_account(&self) -> Result<u32, Box<dyn Error>> {
self.eth_client_contract.get_num_of_submitted_blocks_by_account()
}

fn get_max_submitted_blocks_by_account(&self) -> Result<u32, Box<dyn Error>> {
self.eth_client_contract.get_max_submitted_blocks_by_account()
}
}

#[cfg(test)]
Expand Down
25 changes: 21 additions & 4 deletions eth2near/contract_wrapper/src/eth_client_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use serde_json::json;
use std::error::Error;
use std::option::Option;
use std::string::String;
use std::vec::Vec;
use crate::eth_network_enum::EthNetwork;
use serde::Serialize;

Expand Down Expand Up @@ -48,7 +47,7 @@ impl EthClientContract {
/// * `trusted_signer` - the account address of the trusted signer which is allowed to submit light client updates.
pub fn init_contract(
&self,
network: EthNetwork,
ethereum_network: EthNetwork,
finalized_execution_header: BlockHeader,
finalized_beacon_header: ExtendedBeaconBlockHeader,
current_sync_committee: SyncCommittee,
Expand All @@ -74,7 +73,7 @@ impl EthClientContract {
}

let init_input = InitInput {
network: network.to_string(),
network: ethereum_network.to_string(),
finalized_execution_header,
finalized_beacon_header,
current_sync_committee,
Expand Down Expand Up @@ -157,7 +156,7 @@ impl EthClientContractTrait for EthClientContract {

fn send_headers(
&mut self,
headers: &Vec<BlockHeader>,
headers: &[BlockHeader],
end_slot: u64,
) -> Result<FinalExecutionOutcomeView, Box<dyn std::error::Error>> {
self.last_slot = end_slot;
Expand Down Expand Up @@ -213,6 +212,24 @@ impl EthClientContractTrait for EthClientContract {

Ok(LightClientState::try_from_slice(result.as_slice())?)
}

fn get_num_of_submitted_blocks_by_account(&self) -> Result<u32, Box<dyn Error>> {
let response = self.contract_wrapper.call_view_function(
"get_num_of_submitted_blocks_by_account".to_string(),
json!({"account_id": self.contract_wrapper.get_signer_account_id()}).to_string().into_bytes(),
)?;

Ok(serde_json::from_slice(response.as_slice())?)
}

fn get_max_submitted_blocks_by_account(&self) -> Result<u32, Box<dyn Error>> {
let response = self.contract_wrapper.call_view_function(
"get_max_submitted_blocks_by_account".to_string(),
json!({}).to_string().into_bytes(),
)?;

Ok(serde_json::from_slice(response.as_slice())?)
}
}

#[cfg(test)]
Expand Down
9 changes: 7 additions & 2 deletions eth2near/contract_wrapper/src/eth_client_contract_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use near_primitives::views::FinalExecutionOutcomeView;
use near_primitives::types::AccountId;
use near_sdk::Balance;
use std::error::Error;
use std::vec::Vec;

/// Interface for using Ethereum Light Client
pub trait EthClientContractTrait {
Expand Down Expand Up @@ -34,7 +33,7 @@ pub trait EthClientContractTrait {
/// * `end_slot` - the slot of the last header in list
fn send_headers(
&mut self,
headers: &Vec<BlockHeader>,
headers: &[BlockHeader],
end_slot: u64,
) -> Result<FinalExecutionOutcomeView, Box<dyn std::error::Error>>;

Expand All @@ -49,4 +48,10 @@ pub trait EthClientContractTrait {

/// Gets the Light Client State of the Ethereum Light Client on NEAR
fn get_light_client_state(&self) -> Result<LightClientState, Box<dyn Error>>;

/// Get number of unfinalized blocks submitted by current relay and currently stored on contract
fn get_num_of_submitted_blocks_by_account(&self) -> Result<u32, Box<dyn Error>>;

/// Get max possible number of unfinalized blocks which can be stored on contract for one account
fn get_max_submitted_blocks_by_account(&self) -> Result<u32, Box<dyn Error>>;
}
11 changes: 9 additions & 2 deletions eth2near/contract_wrapper/src/file_eth_client_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::error::Error;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::vec::Vec;

/// Implementation for Ethereum Light Client Contract interaction on NEAR
/// which saves to the file all the submitted headers and light client updates.
Expand Down Expand Up @@ -78,7 +77,7 @@ impl EthClientContractTrait for FileEthClientContract {

fn send_headers(
&mut self,
headers: &Vec<BlockHeader>,
headers: &[BlockHeader],
end_slot: u64,
) -> Result<FinalExecutionOutcomeView, Box<dyn std::error::Error>> {
for header in headers {
Expand Down Expand Up @@ -106,4 +105,12 @@ impl EthClientContractTrait for FileEthClientContract {
fn is_submitter_registered(&self, account_id: Option<AccountId>) -> Result<bool, Box<dyn Error>> {
self.eth_client_contract.is_submitter_registered(account_id)
}

fn get_num_of_submitted_blocks_by_account(&self) -> Result<u32, Box<dyn Error>> {
self.eth_client_contract.get_num_of_submitted_blocks_by_account()
}

fn get_max_submitted_blocks_by_account(&self) -> Result<u32, Box<dyn Error>> {
self.eth_client_contract.get_max_submitted_blocks_by_account()
}
}
4 changes: 2 additions & 2 deletions eth2near/contract_wrapper/src/near_contract_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::contract_wrapper_trait::ContractWrapper;
use crate::utils::trim_quotes;
use crate::utils;
use near_crypto::InMemorySigner;
use near_jsonrpc_client::{methods, JsonRpcClient};
use near_jsonrpc_primitives::types::query::QueryResponseKind;
Expand Down Expand Up @@ -75,7 +75,7 @@ impl NearContractWrapper {
&std::fs::read_to_string(path_to_signer_secret_key).expect("Unable to read file"),
)
.expect("Error on parsing file with secret key during contract initialization");
let signer_secret_key = trim_quotes(serde_json::to_string(&v["private_key"]).expect("Error during trim quotes of signature secret key"));
let signer_secret_key = utils::trim_quotes(serde_json::to_string(&v["private_key"]).expect("Error during trim quotes of signature secret key"));

Self::new_with_raw_secret_key(
near_endpoint,
Expand Down
2 changes: 2 additions & 0 deletions eth2near/eth2-contract-init/Cargo.lock

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

Loading

0 comments on commit e191f98

Please sign in to comment.