From 4279c65cdaa568d06d7c581e8a26f171fa7475bb Mon Sep 17 00:00:00 2001 From: Andreas Bigger Date: Fri, 17 Mar 2023 09:51:40 -0400 Subject: [PATCH] builder refactor --- client/src/builder.rs | 50 +++++++-- client/src/client.rs | 199 ++---------------------------------- execution/src/rpc/ws_rpc.rs | 35 ++++++- 3 files changed, 84 insertions(+), 200 deletions(-) diff --git a/client/src/builder.rs b/client/src/builder.rs index fbf087c..ba098f8 100644 --- a/client/src/builder.rs +++ b/client/src/builder.rs @@ -1,21 +1,27 @@ -use std::path::PathBuf; +use eyre::{eyre, Result}; use config::{Config, Network}; use execution::rpc::WsRpc; use crate::{database::FileDB, Client}; +#[cfg(not(target_arch = "wasm32"))] +use std::path::PathBuf; + #[derive(Default)] pub struct ClientBuilder { pub network: Option, pub consensus_rpc: Option, pub execution_rpc: Option, pub checkpoint: Option>, + #[cfg(not(target_arch = "wasm32"))] pub rpc_port: Option, + #[cfg(not(target_arch = "wasm32"))] pub data_dir: Option, pub config: Option, pub fallback: Option, pub load_external_fallback: bool, + pub strict_checkpoint_age: bool, pub with_ws: bool, pub with_http: bool, } @@ -56,6 +62,7 @@ impl ClientBuilder { /// client_builder = client_builder.with_ws(false); /// assert_eq!(client_builder.with_ws, false); /// ``` + #[cfg(not(target_arch = "wasm32"))] pub fn with_ws(mut self, option: bool) -> Self { self.with_ws = option; self @@ -70,16 +77,20 @@ impl ClientBuilder { /// client_builder = client_builder.with_http(false); /// assert_eq!(client_builder.with_http, false); /// ``` + #[cfg(not(target_arch = "wasm32"))] pub fn with_http(mut self, option: bool) -> Self { self.with_http = option; self } + /// Sets the port for the client to serve an RPC server. + #[cfg(not(target_arch = "wasm32"))] pub fn rpc_port(mut self, port: u16) -> Self { self.rpc_port = Some(port); self } + #[cfg(not(target_arch = "wasm32"))] pub fn data_dir(mut self, data_dir: PathBuf) -> Self { self.data_dir = Some(data_dir); self @@ -100,14 +111,19 @@ impl ClientBuilder { self } - fn build_base_config(&self) -> eyre::Result { + pub fn strict_checkpoint_age(mut self) -> Self { + self.strict_checkpoint_age = true; + self + } + + fn build_config(&self) -> Result { let base_config = if let Some(network) = self.network { network.to_base_config() } else { let config = self .config .as_ref() - .ok_or(eyre::eyre!("missing network config"))?; + .ok_or(eyre!("missing network config"))?; config.to_base_config() }; @@ -127,14 +143,21 @@ impl ClientBuilder { .clone() }); - let checkpoint = if let Some(checkpoint) = &self.checkpoint { - checkpoint.clone() + let checkpoint = if let Some(checkpoint) = self.checkpoint { + Some(checkpoint) } else if let Some(config) = &self.config { config.checkpoint.clone() } else { - base_config.checkpoint + None }; + let default_checkpoint = if let Some(config) = &self.config { + config.default_checkpoint.clone() + } else { + base_config.default_checkpoint.clone() + }; + + #[cfg(not(target_arch = "wasm32"))] let rpc_port = if self.rpc_port.is_some() { self.rpc_port } else if let Some(config) = &self.config { @@ -143,6 +166,7 @@ impl ClientBuilder { None }; + #[cfg(not(target_arch = "wasm32"))] let data_dir = if self.data_dir.is_some() { self.data_dir.clone() } else if let Some(config) = &self.config { @@ -177,17 +201,27 @@ impl ClientBuilder { self.with_http }; + let strict_checkpoint_age = if let Some(config) = &self.config { + self.strict_checkpoint_age || config.strict_checkpoint_age + } else { + self.strict_checkpoint_age + }; + Ok(Config { consensus_rpc, execution_rpc, checkpoint, + default_checkpoint, + #[cfg(not(target_arch = "wasm32"))] rpc_port, + #[cfg(not(target_arch = "wasm32"))] data_dir, chain: base_config.chain, forks: base_config.forks, max_checkpoint_age: base_config.max_checkpoint_age, fallback, load_external_fallback, + strict_checkpoint_age, with_ws, with_http, }) @@ -195,8 +229,8 @@ impl ClientBuilder { } impl ClientBuilder { - pub fn build(self) -> eyre::Result> { - let config = self.build_base_config()?; + pub fn build(self) -> Result> { + let config = self.build_config()?; Client::new(config) } } diff --git a/client/src/client.rs b/client/src/client.rs index 68e5140..64a94a4 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -1,5 +1,4 @@ -use std::process::exit; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use config::networks::Network; use consensus::errors::ConsensusError; @@ -7,20 +6,17 @@ use ethers::prelude::{Address, U256}; use ethers::types::{ FeeHistory, Filter, Log, SyncingStatus, Transaction, TransactionReceipt, H256, }; -use eyre::{eyre, Result}; +use eyre::Result; use common::types::BlockTag; use config::{CheckpointFallback, Config}; use consensus::{types::Header, ConsensusClient}; -use execution::rpc::{ExecutionRpc, WsRpc}; +use execution::rpc::ExecutionRpc; use execution::types::{CallOpts, ExecutionBlock}; -use futures::executor::block_on; use log::{error, info, warn}; use tokio::spawn; use tokio::sync::RwLock; -#[cfg(not(target_arch = "wasm32"))] -use std::path::PathBuf; #[cfg(not(target_arch = "wasm32"))] use tokio::spawn; #[cfg(not(target_arch = "wasm32"))] @@ -38,189 +34,10 @@ use crate::node::Node; #[cfg(not(target_arch = "wasm32"))] use crate::rpc::Rpc; -#[derive(Default)] -pub struct ClientBuilder { - network: Option, - consensus_rpc: Option, - execution_rpc: Option, - checkpoint: Option>, +pub struct Client { + node: Arc>>, #[cfg(not(target_arch = "wasm32"))] - rpc_port: Option, - #[cfg(not(target_arch = "wasm32"))] - data_dir: Option, - config: Option, - fallback: Option, - load_external_fallback: bool, - strict_checkpoint_age: bool, -} - -impl ClientBuilder { - pub fn new() -> Self { - Self::default() - } - - pub fn network(mut self, network: Network) -> Self { - self.network = Some(network); - self - } - - pub fn consensus_rpc(mut self, consensus_rpc: &str) -> Self { - self.consensus_rpc = Some(consensus_rpc.to_string()); - self - } - - pub fn execution_rpc(mut self, execution_rpc: &str) -> Self { - self.execution_rpc = Some(execution_rpc.to_string()); - self - } - - pub fn checkpoint(mut self, checkpoint: &str) -> Self { - let checkpoint = hex::decode(checkpoint.strip_prefix("0x").unwrap_or(checkpoint)) - .expect("cannot parse checkpoint"); - self.checkpoint = Some(checkpoint); - self - } - - #[cfg(not(target_arch = "wasm32"))] - pub fn rpc_port(mut self, port: u16) -> Self { - self.rpc_port = Some(port); - self - } - - #[cfg(not(target_arch = "wasm32"))] - pub fn data_dir(mut self, data_dir: PathBuf) -> Self { - self.data_dir = Some(data_dir); - self - } - - pub fn config(mut self, config: Config) -> Self { - self.config = Some(config); - self - } - - pub fn fallback(mut self, fallback: &str) -> Self { - self.fallback = Some(fallback.to_string()); - self - } - - pub fn load_external_fallback(mut self) -> Self { - self.load_external_fallback = true; - self - } - - pub fn strict_checkpoint_age(mut self) -> Self { - self.strict_checkpoint_age = true; - self - } - - pub fn build(self) -> Result> { - let base_config = if let Some(network) = self.network { - network.to_base_config() - } else { - let config = self - .config - .as_ref() - .ok_or(eyre!("missing network config"))?; - config.to_base_config() - }; - - let consensus_rpc = self.consensus_rpc.unwrap_or_else(|| { - self.config - .as_ref() - .expect("missing consensus rpc") - .consensus_rpc - .clone() - }); - - let execution_rpc = self.execution_rpc.unwrap_or_else(|| { - self.config - .as_ref() - .expect("missing execution rpc") - .execution_rpc - .clone() - }); - - let checkpoint = if let Some(checkpoint) = self.checkpoint { - Some(checkpoint) - } else if let Some(config) = &self.config { - config.checkpoint.clone() - } else { - None - }; - - let default_checkpoint = if let Some(config) = &self.config { - config.default_checkpoint.clone() - } else { - base_config.default_checkpoint.clone() - }; - - #[cfg(not(target_arch = "wasm32"))] - let rpc_port = if self.rpc_port.is_some() { - self.rpc_port - } else if let Some(config) = &self.config { - config.rpc_port - } else { - None - }; - - #[cfg(not(target_arch = "wasm32"))] - let data_dir = if self.data_dir.is_some() { - self.data_dir - } else if let Some(config) = &self.config { - config.data_dir.clone() - } else { - None - }; - - let fallback = if self.fallback.is_some() { - self.fallback - } else if let Some(config) = &self.config { - config.fallback.clone() - } else { - None - }; - - let load_external_fallback = if let Some(config) = &self.config { - self.load_external_fallback || config.load_external_fallback - } else { - self.load_external_fallback - }; - - let strict_checkpoint_age = if let Some(config) = &self.config { - self.strict_checkpoint_age || config.strict_checkpoint_age - } else { - self.strict_checkpoint_age - }; - - let config = Config { - consensus_rpc, - execution_rpc, - checkpoint, - default_checkpoint, - #[cfg(not(target_arch = "wasm32"))] - rpc_port, - #[cfg(target_arch = "wasm32")] - rpc_port: None, - #[cfg(not(target_arch = "wasm32"))] - data_dir, - #[cfg(target_arch = "wasm32")] - data_dir: None, - chain: base_config.chain, - forks: base_config.forks, - max_checkpoint_age: base_config.max_checkpoint_age, - fallback, - load_external_fallback, - strict_checkpoint_age, - }; - - Client::new(config) - } -} - -pub struct Client { - node: Arc>, - #[cfg(not(target_arch = "wasm32"))] - rpc: Option, + rpc: Option>, db: DB, fallback: Option, load_external_fallback: bool, @@ -228,8 +45,8 @@ pub struct Client { http: bool, } -impl Client { - fn new(mut config: Config) -> Result { +impl Client { + pub fn new(mut config: Config) -> Result { let db = DB::new(&config)?; if config.checkpoint.is_none() { let checkpoint = db.load_checkpoint()?; diff --git a/execution/src/rpc/ws_rpc.rs b/execution/src/rpc/ws_rpc.rs index 07754b4..ee1abd9 100644 --- a/execution/src/rpc/ws_rpc.rs +++ b/execution/src/rpc/ws_rpc.rs @@ -61,7 +61,7 @@ impl ExecutionRpc for WsRpc { let block = Some(BlockId::from(block)); let mut raw_tx = Eip1559TransactionRequest::new(); - raw_tx.to = Some(opts.to.into()); + raw_tx.to = opts.to.map(Into::into); raw_tx.from = opts.from; raw_tx.value = opts.value; raw_tx.gas = Some(opts.gas.unwrap_or(U256::from(100_000_000))); @@ -159,4 +159,37 @@ impl ExecutionRpc for WsRpc { .await .map_err(|e| RpcError::new("get_logs", e))?) } + + async fn chain_id(&self) -> Result { + Ok(self + .provider + .as_ref() + .ok_or(RpcError::new( + "get_chainid", + eyre::eyre!("Provider not connected!"), + ))? + .get_chainid() + .await + .map_err(|e| RpcError::new("chain_id", e))? + .as_u64()) + } + + async fn get_fee_history( + &self, + block_count: u64, + last_block: u64, + reward_percentiles: &[f64], + ) -> Result { + let block = BlockNumber::from(last_block); + Ok(self + .provider + .as_ref() + .ok_or(RpcError::new( + "fee_history", + eyre::eyre!("Provider not connected!"), + ))? + .fee_history(block_count, block, reward_percentiles) + .await + .map_err(|e| RpcError::new("fee_history", e))?) + } }