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]]
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",

View File

@ -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

View File

@ -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) {

View File

@ -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<u64, NodeError> {
@ -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<U256> {

View File

@ -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

View File

@ -32,10 +32,24 @@ pub struct ExecutionClient<R: ExecutionRpc> {
impl<R: ExecutionRpc> ExecutionClient<R> {
pub fn new(rpc: &str) -> Result<Self> {
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,

View File

@ -128,4 +128,13 @@ impl ExecutionRpc for HttpRpc {
.await
.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"))?;
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(&self, tx_hash: &H256) -> Result<Option<Transaction>>;
async fn get_logs(&self, filter: &Filter) -> Result<Vec<Log>>;
async fn chain_id(&self) -> Result<u64>;
}