diff --git a/client/src/client.rs b/client/src/client.rs index 4cc7d45..1095dda 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -135,11 +135,17 @@ impl ClientBuilder { }); let checkpoint = if let Some(checkpoint) = self.checkpoint { - 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"))] @@ -184,6 +190,7 @@ impl ClientBuilder { consensus_rpc, execution_rpc, checkpoint, + default_checkpoint, #[cfg(not(target_arch = "wasm32"))] rpc_port, #[cfg(target_arch = "wasm32")] @@ -216,8 +223,10 @@ pub struct Client { impl Client { fn new(mut config: Config) -> Result { let db = DB::new(&config)?; - let checkpoint = db.load_checkpoint()?; - config.checkpoint = checkpoint; + if config.checkpoint.is_none() { + let checkpoint = db.load_checkpoint()?; + config.checkpoint = Some(checkpoint); + } let config = Arc::new(config); let node = Node::new(config.clone())?; @@ -250,7 +259,15 @@ impl Client { ConsensusError::CheckpointTooOld => { warn!( "failed to sync consensus node with checkpoint: 0x{}", - hex::encode(&self.node.read().await.config.checkpoint), + hex::encode( + self.node + .read() + .await + .config + .checkpoint + .clone() + .unwrap_or_default() + ), ); let fallback = self.boot_from_fallback().await; diff --git a/client/src/database.rs b/client/src/database.rs index 43186c9..335ebc8 100644 --- a/client/src/database.rs +++ b/client/src/database.rs @@ -28,7 +28,7 @@ impl Database for FileDB { if let Some(data_dir) = &config.data_dir { return Ok(FileDB { data_dir: data_dir.to_path_buf(), - default_checkpoint: config.checkpoint.clone(), + default_checkpoint: config.default_checkpoint.clone(), }); } @@ -72,7 +72,10 @@ pub struct ConfigDB { impl Database for ConfigDB { fn new(config: &Config) -> Result { Ok(Self { - checkpoint: config.checkpoint.clone(), + checkpoint: config + .checkpoint + .clone() + .unwrap_or(config.default_checkpoint.clone()), }) } diff --git a/client/src/node.rs b/client/src/node.rs index 5b1dd3b..93d6b41 100644 --- a/client/src/node.rs +++ b/client/src/node.rs @@ -31,7 +31,7 @@ pub struct Node { impl Node { pub fn new(config: Arc) -> Result { let consensus_rpc = &config.consensus_rpc; - let checkpoint_hash = &config.checkpoint; + let checkpoint_hash = &config.checkpoint.as_ref().unwrap(); let execution_rpc = &config.execution_rpc; let consensus = ConsensusClient::new(consensus_rpc, checkpoint_hash, config.clone()) diff --git a/config/src/base.rs b/config/src/base.rs index e89f08a..2ab2c32 100644 --- a/config/src/base.rs +++ b/config/src/base.rs @@ -12,7 +12,7 @@ pub struct BaseConfig { deserialize_with = "bytes_deserialize", serialize_with = "bytes_serialize" )] - pub checkpoint: Vec, + pub default_checkpoint: Vec, pub chain: ChainConfig, pub forks: Forks, pub max_checkpoint_age: u64, diff --git a/config/src/config.rs b/config/src/config.rs index c19aee6..990cbf4 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -2,25 +2,25 @@ use figment::{ providers::{Format, Serialized, Toml}, Figment, }; -use serde::{Deserialize, Serialize}; +use serde::Deserialize; use std::{path::PathBuf, process::exit}; use crate::base::BaseConfig; use crate::cli::CliConfig; use crate::networks; use crate::types::{ChainConfig, Forks}; -use crate::utils::{bytes_deserialize, bytes_serialize}; +use crate::utils::{bytes_deserialize, bytes_opt_deserialize}; -#[derive(Serialize, Deserialize, Debug, Default)] +#[derive(Deserialize, Debug, Default)] pub struct Config { pub consensus_rpc: String, pub execution_rpc: String, pub rpc_port: Option, - #[serde( - deserialize_with = "bytes_deserialize", - serialize_with = "bytes_serialize" - )] - pub checkpoint: Vec, + #[serde(deserialize_with = "bytes_deserialize")] + pub default_checkpoint: Vec, + #[serde(default)] + #[serde(deserialize_with = "bytes_opt_deserialize")] + pub checkpoint: Option>, pub data_dir: Option, pub chain: ChainConfig, pub forks: Forks, @@ -86,7 +86,7 @@ impl Config { BaseConfig { rpc_port: self.rpc_port.unwrap_or(8545), consensus_rpc: Some(self.consensus_rpc.clone()), - checkpoint: self.checkpoint.clone(), + default_checkpoint: self.default_checkpoint.clone(), chain: self.chain.clone(), forks: self.forks.clone(), max_checkpoint_age: self.max_checkpoint_age, diff --git a/config/src/networks.rs b/config/src/networks.rs index 72d8bde..ebe4f9e 100644 --- a/config/src/networks.rs +++ b/config/src/networks.rs @@ -35,7 +35,7 @@ impl Network { pub fn mainnet() -> BaseConfig { BaseConfig { - checkpoint: hex_str_to_bytes( + default_checkpoint: hex_str_to_bytes( "0x766647f3c4e1fc91c0db9a9374032ae038778411fbff222974e11f2e3ce7dadf", ) .unwrap(), @@ -69,7 +69,7 @@ pub fn mainnet() -> BaseConfig { pub fn goerli() -> BaseConfig { BaseConfig { - checkpoint: hex_str_to_bytes( + default_checkpoint: hex_str_to_bytes( "0xd4344682866dbede543395ecf5adf9443a27f423a4b00f270458e7932686ced1", ) .unwrap(), diff --git a/config/src/utils.rs b/config/src/utils.rs index 24e680f..b12773b 100644 --- a/config/src/utils.rs +++ b/config/src/utils.rs @@ -15,3 +15,15 @@ where let bytes_string = hex::encode(bytes); serializer.serialize_str(&bytes_string) } + +pub fn bytes_opt_deserialize<'de, D>(deserializer: D) -> Result>, D::Error> +where + D: serde::Deserializer<'de>, +{ + let bytes_opt: Option = serde::Deserialize::deserialize(deserializer)?; + if let Some(bytes) = bytes_opt { + Ok(Some(hex_str_to_bytes(&bytes).unwrap())) + } else { + Ok(None) + } +}