From de90eb91584be096e4a87ec35db5ca9daa60622e Mon Sep 17 00:00:00 2001 From: Noah Citron Date: Sun, 22 Jan 2023 11:58:55 -0500 Subject: [PATCH] feat: check execution rpc network (#176) * feat: check execution rpc network * clippy --- Cargo.lock | 14 +++++++------- cli/src/main.rs | 15 ++++++++++++--- client/src/errors.rs | 9 ++++++--- client/src/node.rs | 11 +++++++++-- execution/src/errors.rs | 4 ++++ execution/src/execution.rs | 16 +++++++++++++++- execution/src/rpc/http_rpc.rs | 9 +++++++++ execution/src/rpc/mock_rpc.rs | 4 ++++ execution/src/rpc/mod.rs | 1 + 9 files changed, 67 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 88ad67b..0818a8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -526,7 +526,7 @@ dependencies = [ [[package]] name = "cli" -version = "0.1.0" +version = "0.1.3" dependencies = [ "clap", "client", @@ -543,7 +543,7 @@ dependencies = [ [[package]] name = "client" -version = "0.1.1" +version = "0.1.3" dependencies = [ "common", "config", @@ -645,7 +645,7 @@ checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" [[package]] name = "common" -version = "0.1.0" +version = "0.1.3" dependencies = [ "ethers", "eyre", @@ -657,7 +657,7 @@ dependencies = [ [[package]] name = "config" -version = "0.1.0" +version = "0.1.3" dependencies = [ "common", "ethers", @@ -677,7 +677,7 @@ dependencies = [ [[package]] name = "consensus" -version = "0.1.0" +version = "0.1.3" dependencies = [ "async-trait", "blst", @@ -1461,7 +1461,7 @@ checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" [[package]] name = "execution" -version = "0.1.0" +version = "0.1.3" dependencies = [ "async-trait", "bytes", @@ -1979,7 +1979,7 @@ checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" [[package]] name = "helios" -version = "0.1.2" +version = "0.1.3" dependencies = [ "client", "common", diff --git a/cli/src/main.rs b/cli/src/main.rs index cee757d..270f777 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -15,16 +15,25 @@ use eyre::Result; use client::{database::FileDB, Client, ClientBuilder}; use config::{CliConfig, Config}; use futures::executor::block_on; -use log::info; +use log::{error, info}; #[tokio::main] async fn main() -> Result<()> { env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); let config = get_config(); - let mut client = ClientBuilder::new().config(config).build()?; + let mut client = match ClientBuilder::new().config(config).build() { + Ok(client) => client, + Err(err) => { + error!("{}", err); + exit(1); + } + }; - client.start().await?; + if let Err(err) = client.start().await { + error!("{}", err); + exit(1); + } register_shutdown_handler(client); std::future::pending().await diff --git a/client/src/errors.rs b/client/src/errors.rs index b0b0018..84959fd 100644 --- a/client/src/errors.rs +++ b/client/src/errors.rs @@ -1,5 +1,5 @@ use common::errors::BlockNotFoundError; -use execution::errors::EvmError; +use execution::errors::{EvmError, ExecutionError}; use eyre::Report; use thiserror::Error; @@ -7,7 +7,10 @@ use thiserror::Error; #[derive(Debug, Error)] pub enum NodeError { #[error(transparent)] - ExecutionError(#[from] EvmError), + ExecutionEvmError(#[from] EvmError), + + #[error(transparent)] + ExecutionError(#[from] ExecutionError), #[error("out of sync: {0} slots behind")] OutOfSync(u64), @@ -37,7 +40,7 @@ pub enum NodeError { impl NodeError { pub fn to_json_rpsee_error(self) -> jsonrpsee::core::Error { match self { - NodeError::ExecutionError(evm_err) => match evm_err { + NodeError::ExecutionEvmError(evm_err) => match evm_err { EvmError::Revert(data) => { let mut msg = "execution reverted".to_string(); if let Some(reason) = data.as_ref().and_then(EvmError::decode_revert_reason) { diff --git a/client/src/node.rs b/client/src/node.rs index 9987413..48bc0dc 100644 --- a/client/src/node.rs +++ b/client/src/node.rs @@ -54,10 +54,17 @@ impl Node { } pub async fn sync(&mut self) -> Result<(), NodeError> { + let chain_id = self.config.chain.chain_id; + self.execution + .check_rpc(chain_id) + .await + .map_err(NodeError::ExecutionError)?; + self.consensus .sync() .await .map_err(NodeError::ConsensusSyncError)?; + self.update_payloads().await } @@ -121,7 +128,7 @@ impl Node { &self.payloads, self.chain_id(), ); - evm.call(opts).await.map_err(NodeError::ExecutionError) + evm.call(opts).await.map_err(NodeError::ExecutionEvmError) } pub async fn estimate_gas(&self, opts: &CallOpts) -> Result { @@ -136,7 +143,7 @@ impl Node { ); evm.estimate_gas(opts) .await - .map_err(NodeError::ExecutionError) + .map_err(NodeError::ExecutionEvmError) } pub async fn get_balance(&self, address: &Address, block: BlockTag) -> Result { diff --git a/execution/src/errors.rs b/execution/src/errors.rs index 415fe91..0512d10 100644 --- a/execution/src/errors.rs +++ b/execution/src/errors.rs @@ -24,6 +24,10 @@ pub enum ExecutionError { MissingLog(String, U256), #[error("too many logs to prove: {0}, current limit is: {1}")] TooManyLogsToProve(usize, usize), + #[error("rpc error: {0:?}")] + RpcError(Report), + #[error("execution rpc is for the incorect network")] + IncorrectRpcNetwork(), } /// Errors that can occur during evm.rs calls diff --git a/execution/src/execution.rs b/execution/src/execution.rs index 07858cc..0f310f8 100644 --- a/execution/src/execution.rs +++ b/execution/src/execution.rs @@ -32,10 +32,24 @@ pub struct ExecutionClient { impl ExecutionClient { pub fn new(rpc: &str) -> Result { - let rpc = ExecutionRpc::new(rpc)?; + let rpc: R = ExecutionRpc::new(rpc)?; Ok(ExecutionClient { rpc }) } + pub async fn check_rpc(&self, chain_id: u64) -> Result<(), ExecutionError> { + if self + .rpc + .chain_id() + .await + .map_err(ExecutionError::RpcError)? + != chain_id + { + Err(ExecutionError::IncorrectRpcNetwork()) + } else { + Ok(()) + } + } + pub async fn get_account( &self, address: &Address, diff --git a/execution/src/rpc/http_rpc.rs b/execution/src/rpc/http_rpc.rs index 4b02ee4..d395b4b 100644 --- a/execution/src/rpc/http_rpc.rs +++ b/execution/src/rpc/http_rpc.rs @@ -128,4 +128,13 @@ impl ExecutionRpc for HttpRpc { .await .map_err(|e| RpcError::new("get_logs", e))?) } + + async fn chain_id(&self) -> Result { + Ok(self + .provider + .get_chainid() + .await + .map_err(|e| RpcError::new("chain_id", e))? + .as_u64()) + } } diff --git a/execution/src/rpc/mock_rpc.rs b/execution/src/rpc/mock_rpc.rs index 4527c68..3a4fd1e 100644 --- a/execution/src/rpc/mock_rpc.rs +++ b/execution/src/rpc/mock_rpc.rs @@ -61,4 +61,8 @@ impl ExecutionRpc for MockRpc { let logs = read_to_string(self.path.join("logs.json"))?; Ok(serde_json::from_str(&logs)?) } + + async fn chain_id(&self) -> Result { + Err(eyre!("not implemented")) + } } diff --git a/execution/src/rpc/mod.rs b/execution/src/rpc/mod.rs index 1a47c16..0c1b799 100644 --- a/execution/src/rpc/mod.rs +++ b/execution/src/rpc/mod.rs @@ -29,4 +29,5 @@ pub trait ExecutionRpc: Send + Clone + Sync + 'static { async fn get_transaction_receipt(&self, tx_hash: &H256) -> Result>; async fn get_transaction(&self, tx_hash: &H256) -> Result>; async fn get_logs(&self, filter: &Filter) -> Result>; + async fn chain_id(&self) -> Result; }