Skip to content

Commit

Permalink
fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ltfschoen committed Aug 18, 2023
2 parents 4feae5b + 0711689 commit 252f2ba
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 204 deletions.
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use dirs::home_dir;
use env_logger::Env;
use eyre::Result;

use client::{database::FileDB, Client, ClientBuilder};
use client::{Client, ClientBuilder};
use config::{CliConfig, Config};
use futures::executor::block_on;
use log::{error, info};
Expand All @@ -39,7 +39,7 @@ async fn main() -> Result<()> {
std::future::pending().await
}

fn register_shutdown_handler(client: Client<FileDB>) {
fn register_shutdown_handler(client: Client) {
let client = Arc::new(client);
let shutdown_counter = Arc::new(Mutex::new(0));

Expand Down
144 changes: 8 additions & 136 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use gloo_timers::callback::Interval;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_futures::spawn_local;

use crate::database::Database;
use crate::node::Node;

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -109,7 +108,7 @@ impl ClientBuilder {
self
}

pub fn build<DB: Database>(self) -> Result<Client<DB>> {
pub fn build(self) -> Result<Client> {
let base_config = if let Some(network) = self.network {
network.to_base_config()
} else {
Expand Down Expand Up @@ -226,23 +225,14 @@ impl ClientBuilder {
}
}

pub struct Client<DB: Database> {
pub struct Client {
node: Arc<RwLock<Node>>,
#[cfg(not(target_arch = "wasm32"))]
rpc: Option<Rpc>,
db: DB,
// fallback: Option<String>,
// load_external_fallback: bool,
}

impl<DB: Database> Client<DB> {
fn new(mut config: Config) -> Result<Self> {
let db = DB::new(&config)?;
if config.checkpoint.is_none() {
let checkpoint = db.load_checkpoint()?;
config.checkpoint = Some(checkpoint);
}

impl Client {
fn new(config: Config) -> Result<Self> {
let config = Arc::new(config);
let node = Node::new(config.clone())?;
let node = Arc::new(RwLock::new(node));
Expand All @@ -257,9 +247,6 @@ impl<DB: Database> Client<DB> {
node,
#[cfg(not(target_arch = "wasm32"))]
rpc,
db,
// fallback: config.fallback.clone(),
// load_external_fallback: config.load_external_fallback,
})
}

Expand All @@ -269,41 +256,6 @@ impl<DB: Database> Client<DB> {
rpc.start().await?;
}

// let sync_res = self.node.write().await.sync().await;

// if let Err(err) = sync_res {
// match err {
// NodeError::ConsensusSyncError(err) => match err.downcast_ref() {
// Some(ConsensusError::CheckpointTooOld) => {
// warn!(
// "failed to sync consensus node with checkpoint: 0x{}",
// hex::encode(
// self.node
// .read()
// .await
// .config
// .checkpoint
// .clone()
// .unwrap_or_default()
// ),
// );

// let fallback = self.boot_from_fallback().await;
// if fallback.is_err() && self.load_external_fallback {
// self.boot_from_external_fallbacks().await?
// } else if fallback.is_err() {
// error!("Invalid checkpoint. Please update your checkpoint too a more recent block. Alternatively, set an explicit checkpoint fallback service url with the `-f` flag or use the configured external fallback services with `-l` (NOT RECOMMENDED). See https://github.com/a16z/helios#additional-options for more information.");
// return Err(err);
// }
// }
// _ => return Err(err),
// },
// _ => return Err(err.into()),
// }
// }

// self.save_last_checkpoint().await;

self.start_advance_thread();

Ok(())
Expand Down Expand Up @@ -337,91 +289,11 @@ impl<DB: Database> Client<DB> {
.forget();
}

// async fn boot_from_fallback(&self) -> eyre::Result<()> {
// if let Some(fallback) = &self.fallback {
// info!(
// "attempting to load checkpoint from fallback \"{}\"",
// fallback
// );

// let checkpoint = CheckpointFallback::fetch_checkpoint_from_api(fallback)
// .await
// .map_err(|_| {
// eyre::eyre!("Failed to fetch checkpoint from fallback \"{}\"", fallback)
// })?;

// info!(
// "external fallbacks responded with checkpoint 0x{:?}",
// checkpoint
// );

// // Try to sync again with the new checkpoint by reconstructing the consensus client
// // We fail fast here since the node is unrecoverable at this point
// let config = self.node.read().await.config.clone();
// let consensus =
// ConsensusClient::new(&config.consensus_rpc, checkpoint.as_bytes(), config.clone())?;
// self.node.write().await.consensus = consensus;
// self.node.write().await.sync().await?;

// Ok(())
// } else {
// Err(eyre::eyre!("no explicit fallback specified"))
// }
// }

// async fn boot_from_external_fallbacks(&self) -> eyre::Result<()> {
// info!("attempting to fetch checkpoint from external fallbacks...");
// // Build the list of external checkpoint fallback services
// let list = CheckpointFallback::new()
// .build()
// .await
// .map_err(|_| eyre::eyre!("Failed to construct external checkpoint sync fallbacks"))?;

// let checkpoint = if self.node.read().await.config.chain.chain_id == 5 {
// list.fetch_latest_checkpoint(&Network::GOERLI)
// .await
// .map_err(|_| {
// eyre::eyre!("Failed to fetch latest goerli checkpoint from external fallbacks")
// })?
// } else {
// list.fetch_latest_checkpoint(&Network::MAINNET)
// .await
// .map_err(|_| {
// eyre::eyre!("Failed to fetch latest mainnet checkpoint from external fallbacks")
// })?
// };

// info!(
// "external fallbacks responded with checkpoint {:?}",
// checkpoint
// );

// // Try to sync again with the new checkpoint by reconstructing the consensus client
// // We fail fast here since the node is unrecoverable at this point
// let config = self.node.read().await.config.clone();
// let consensus =
// ConsensusClient::new(&config.consensus_rpc, checkpoint.as_bytes(), config.clone())?;
// self.node.write().await.consensus = consensus;
// self.node.write().await.sync().await?;
// Ok(())
// }

/// Saves last checkpoint of the node.
async fn save_last_checkpoint(&self) {
let node = self.node.read().await;
let checkpoint = node.consensus.checkpoint_recv.borrow().to_owned();

if let Some(checkpoint) = checkpoint {
info!("saving last checkpoint hash");
let res = self.db.save_checkpoint(checkpoint);
if res.is_err() {
warn!("checkpoint save failed");
}
};
}

pub async fn shutdown(&self) {
self.save_last_checkpoint().await;
info!("shutting down");
if let Err(err) = self.node.read().await.consensus.shutdown() {
warn!("graceful shutdown failed: {}", err);
}
}

pub async fn call(&self, opts: &CallOpts, block: BlockTag) -> Result<Vec<u8>> {
Expand Down
1 change: 0 additions & 1 deletion client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod client;
pub use crate::client::*;

pub mod database;
pub mod errors;

#[cfg(not(target_arch = "wasm32"))]
Expand Down
7 changes: 4 additions & 3 deletions client/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::BTreeMap;
use std::sync::Arc;
use std::time::Duration;

use consensus::database::FileDB;
use ethers::prelude::{Address, U256};
use ethers::types::{
FeeHistory, Filter, Log, SyncProgress, SyncingStatus, Transaction, TransactionReceipt, H256,
Expand All @@ -23,7 +24,7 @@ use execution::ExecutionClient;
use crate::errors::NodeError;

pub struct Node {
pub consensus: ConsensusClient<NimbusRpc>,
pub consensus: ConsensusClient<NimbusRpc, FileDB>,
pub execution: Arc<ExecutionClient<HttpRpc>>,
pub config: Arc<Config>,
pub history_size: usize,
Expand All @@ -34,11 +35,11 @@ pub struct Node {
impl Node {
pub fn new(config: Arc<Config>) -> Result<Self, NodeError> {
let consensus_rpc = &config.consensus_rpc;
let checkpoint_hash = &config.checkpoint.as_ref().unwrap();
let execution_rpc = &config.execution_rpc;

let consensus = ConsensusClient::new(consensus_rpc, checkpoint_hash, config.clone())
let consensus = ConsensusClient::new(consensus_rpc, config.clone())
.map_err(NodeError::ConsensusClientCreationError)?;

let execution = Arc::new(
ExecutionClient::new(execution_rpc).map_err(NodeError::ExecutionClientCreationError)?,
);
Expand Down
9 changes: 9 additions & 0 deletions config/src/networks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use common::utils::hex_str_to_bytes;
use eyre::Result;
use serde::{Deserialize, Serialize};
use strum::{Display, EnumIter};

Expand Down Expand Up @@ -31,6 +32,14 @@ impl Network {
Self::GOERLI => goerli(),
}
}

pub fn from_chain_id(id: u64) -> Result<Self> {
match id {
1 => Ok(Network::MAINNET),
5 => Ok(Network::GOERLI),
_ => Err(eyre::eyre!("chain id not known")),
}
}
}

pub fn mainnet() -> BaseConfig {
Expand Down
Loading

0 comments on commit 252f2ba

Please sign in to comment.