feat: check execution rpc network (#176)

* feat: check execution rpc network

* clippy
This commit is contained in:
Noah Citron 2023-01-22 11:58:55 -05:00 committed by GitHub
parent 10e39eb35a
commit de90eb9158
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 67 additions and 16 deletions

14
Cargo.lock generated
View File

@ -526,7 +526,7 @@ dependencies = [
[[package]] [[package]]
name = "cli" name = "cli"
version = "0.1.0" version = "0.1.3"
dependencies = [ dependencies = [
"clap", "clap",
"client", "client",
@ -543,7 +543,7 @@ dependencies = [
[[package]] [[package]]
name = "client" name = "client"
version = "0.1.1" version = "0.1.3"
dependencies = [ dependencies = [
"common", "common",
"config", "config",
@ -645,7 +645,7 @@ checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]] [[package]]
name = "common" name = "common"
version = "0.1.0" version = "0.1.3"
dependencies = [ dependencies = [
"ethers", "ethers",
"eyre", "eyre",
@ -657,7 +657,7 @@ dependencies = [
[[package]] [[package]]
name = "config" name = "config"
version = "0.1.0" version = "0.1.3"
dependencies = [ dependencies = [
"common", "common",
"ethers", "ethers",
@ -677,7 +677,7 @@ dependencies = [
[[package]] [[package]]
name = "consensus" name = "consensus"
version = "0.1.0" version = "0.1.3"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"blst", "blst",
@ -1461,7 +1461,7 @@ checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
[[package]] [[package]]
name = "execution" name = "execution"
version = "0.1.0" version = "0.1.3"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"bytes", "bytes",
@ -1979,7 +1979,7 @@ checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]] [[package]]
name = "helios" name = "helios"
version = "0.1.2" version = "0.1.3"
dependencies = [ dependencies = [
"client", "client",
"common", "common",

View File

@ -15,16 +15,25 @@ use eyre::Result;
use client::{database::FileDB, Client, ClientBuilder}; use client::{database::FileDB, Client, ClientBuilder};
use config::{CliConfig, Config}; use config::{CliConfig, Config};
use futures::executor::block_on; use futures::executor::block_on;
use log::info; use log::{error, info};
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let config = get_config(); 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); register_shutdown_handler(client);
std::future::pending().await std::future::pending().await

View File

@ -1,5 +1,5 @@
use common::errors::BlockNotFoundError; use common::errors::BlockNotFoundError;
use execution::errors::EvmError; use execution::errors::{EvmError, ExecutionError};
use eyre::Report; use eyre::Report;
use thiserror::Error; use thiserror::Error;
@ -7,7 +7,10 @@ use thiserror::Error;
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum NodeError { pub enum NodeError {
#[error(transparent)] #[error(transparent)]
ExecutionError(#[from] EvmError), ExecutionEvmError(#[from] EvmError),
#[error(transparent)]
ExecutionError(#[from] ExecutionError),
#[error("out of sync: {0} slots behind")] #[error("out of sync: {0} slots behind")]
OutOfSync(u64), OutOfSync(u64),
@ -37,7 +40,7 @@ pub enum NodeError {
impl NodeError { impl NodeError {
pub fn to_json_rpsee_error(self) -> jsonrpsee::core::Error { pub fn to_json_rpsee_error(self) -> jsonrpsee::core::Error {
match self { match self {
NodeError::ExecutionError(evm_err) => match evm_err { NodeError::ExecutionEvmError(evm_err) => match evm_err {
EvmError::Revert(data) => { EvmError::Revert(data) => {
let mut msg = "execution reverted".to_string(); let mut msg = "execution reverted".to_string();
if let Some(reason) = data.as_ref().and_then(EvmError::decode_revert_reason) { if let Some(reason) = data.as_ref().and_then(EvmError::decode_revert_reason) {

View File

@ -54,10 +54,17 @@ impl Node {
} }
pub async fn sync(&mut self) -> Result<(), NodeError> { 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 self.consensus
.sync() .sync()
.await .await
.map_err(NodeError::ConsensusSyncError)?; .map_err(NodeError::ConsensusSyncError)?;
self.update_payloads().await self.update_payloads().await
} }
@ -121,7 +128,7 @@ impl Node {
&self.payloads, &self.payloads,
self.chain_id(), 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<u64, NodeError> { pub async fn estimate_gas(&self, opts: &CallOpts) -> Result<u64, NodeError> {
@ -136,7 +143,7 @@ impl Node {
); );
evm.estimate_gas(opts) evm.estimate_gas(opts)
.await .await
.map_err(NodeError::ExecutionError) .map_err(NodeError::ExecutionEvmError)
} }
pub async fn get_balance(&self, address: &Address, block: BlockTag) -> Result<U256> { pub async fn get_balance(&self, address: &Address, block: BlockTag) -> Result<U256> {

View File

@ -24,6 +24,10 @@ pub enum ExecutionError {
MissingLog(String, U256), MissingLog(String, U256),
#[error("too many logs to prove: {0}, current limit is: {1}")] #[error("too many logs to prove: {0}, current limit is: {1}")]
TooManyLogsToProve(usize, usize), 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 /// Errors that can occur during evm.rs calls

View File

@ -32,10 +32,24 @@ pub struct ExecutionClient<R: ExecutionRpc> {
impl<R: ExecutionRpc> ExecutionClient<R> { impl<R: ExecutionRpc> ExecutionClient<R> {
pub fn new(rpc: &str) -> Result<Self> { pub fn new(rpc: &str) -> Result<Self> {
let rpc = ExecutionRpc::new(rpc)?; let rpc: R = ExecutionRpc::new(rpc)?;
Ok(ExecutionClient { 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( pub async fn get_account(
&self, &self,
address: &Address, address: &Address,

View File

@ -128,4 +128,13 @@ impl ExecutionRpc for HttpRpc {
.await .await
.map_err(|e| RpcError::new("get_logs", e))?) .map_err(|e| RpcError::new("get_logs", e))?)
} }
async fn chain_id(&self) -> Result<u64> {
Ok(self
.provider
.get_chainid()
.await
.map_err(|e| RpcError::new("chain_id", e))?
.as_u64())
}
} }

View File

@ -61,4 +61,8 @@ impl ExecutionRpc for MockRpc {
let logs = read_to_string(self.path.join("logs.json"))?; let logs = read_to_string(self.path.join("logs.json"))?;
Ok(serde_json::from_str(&logs)?) Ok(serde_json::from_str(&logs)?)
} }
async fn chain_id(&self) -> Result<u64> {
Err(eyre!("not implemented"))
}
} }

View File

@ -29,4 +29,5 @@ pub trait ExecutionRpc: Send + Clone + Sync + 'static {
async fn get_transaction_receipt(&self, tx_hash: &H256) -> Result<Option<TransactionReceipt>>; async fn get_transaction_receipt(&self, tx_hash: &H256) -> Result<Option<TransactionReceipt>>;
async fn get_transaction(&self, tx_hash: &H256) -> Result<Option<Transaction>>; async fn get_transaction(&self, tx_hash: &H256) -> Result<Option<Transaction>>;
async fn get_logs(&self, filter: &Filter) -> Result<Vec<Log>>; async fn get_logs(&self, filter: &Filter) -> Result<Vec<Log>>;
async fn chain_id(&self) -> Result<u64>;
} }