fix: handle checkpoint overrides correctly (#187)

* fix: handle checkpoint overrides correctly

* fix optional deserialize

* clippy

* fmt
This commit is contained in:
Noah Citron 2023-02-01 17:10:26 -05:00 committed by GitHub
parent 1fa2dede25
commit 4a84ecabed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 20 deletions

View File

@ -135,11 +135,17 @@ impl ClientBuilder {
}); });
let checkpoint = if let Some(checkpoint) = self.checkpoint { let checkpoint = if let Some(checkpoint) = self.checkpoint {
checkpoint Some(checkpoint)
} else if let Some(config) = &self.config { } else if let Some(config) = &self.config {
config.checkpoint.clone() config.checkpoint.clone()
} else { } 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"))] #[cfg(not(target_arch = "wasm32"))]
@ -184,6 +190,7 @@ impl ClientBuilder {
consensus_rpc, consensus_rpc,
execution_rpc, execution_rpc,
checkpoint, checkpoint,
default_checkpoint,
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
rpc_port, rpc_port,
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
@ -216,8 +223,10 @@ pub struct Client<DB: Database> {
impl<DB: Database> Client<DB> { impl<DB: Database> Client<DB> {
fn new(mut config: Config) -> Result<Self> { fn new(mut config: Config) -> Result<Self> {
let db = DB::new(&config)?; let db = DB::new(&config)?;
let checkpoint = db.load_checkpoint()?; if config.checkpoint.is_none() {
config.checkpoint = checkpoint; let checkpoint = db.load_checkpoint()?;
config.checkpoint = Some(checkpoint);
}
let config = Arc::new(config); let config = Arc::new(config);
let node = Node::new(config.clone())?; let node = Node::new(config.clone())?;
@ -250,7 +259,15 @@ impl<DB: Database> Client<DB> {
ConsensusError::CheckpointTooOld => { ConsensusError::CheckpointTooOld => {
warn!( warn!(
"failed to sync consensus node with checkpoint: 0x{}", "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; let fallback = self.boot_from_fallback().await;

View File

@ -28,7 +28,7 @@ impl Database for FileDB {
if let Some(data_dir) = &config.data_dir { if let Some(data_dir) = &config.data_dir {
return Ok(FileDB { return Ok(FileDB {
data_dir: data_dir.to_path_buf(), 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 { impl Database for ConfigDB {
fn new(config: &Config) -> Result<Self> { fn new(config: &Config) -> Result<Self> {
Ok(Self { Ok(Self {
checkpoint: config.checkpoint.clone(), checkpoint: config
.checkpoint
.clone()
.unwrap_or(config.default_checkpoint.clone()),
}) })
} }

View File

@ -31,7 +31,7 @@ pub struct Node {
impl Node { impl Node {
pub fn new(config: Arc<Config>) -> Result<Self, NodeError> { pub fn new(config: Arc<Config>) -> Result<Self, NodeError> {
let consensus_rpc = &config.consensus_rpc; 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 execution_rpc = &config.execution_rpc;
let consensus = ConsensusClient::new(consensus_rpc, checkpoint_hash, config.clone()) let consensus = ConsensusClient::new(consensus_rpc, checkpoint_hash, config.clone())

View File

@ -12,7 +12,7 @@ pub struct BaseConfig {
deserialize_with = "bytes_deserialize", deserialize_with = "bytes_deserialize",
serialize_with = "bytes_serialize" serialize_with = "bytes_serialize"
)] )]
pub checkpoint: Vec<u8>, pub default_checkpoint: Vec<u8>,
pub chain: ChainConfig, pub chain: ChainConfig,
pub forks: Forks, pub forks: Forks,
pub max_checkpoint_age: u64, pub max_checkpoint_age: u64,

View File

@ -2,25 +2,25 @@ use figment::{
providers::{Format, Serialized, Toml}, providers::{Format, Serialized, Toml},
Figment, Figment,
}; };
use serde::{Deserialize, Serialize}; use serde::Deserialize;
use std::{path::PathBuf, process::exit}; use std::{path::PathBuf, process::exit};
use crate::base::BaseConfig; use crate::base::BaseConfig;
use crate::cli::CliConfig; use crate::cli::CliConfig;
use crate::networks; use crate::networks;
use crate::types::{ChainConfig, Forks}; 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 struct Config {
pub consensus_rpc: String, pub consensus_rpc: String,
pub execution_rpc: String, pub execution_rpc: String,
pub rpc_port: Option<u16>, pub rpc_port: Option<u16>,
#[serde( #[serde(deserialize_with = "bytes_deserialize")]
deserialize_with = "bytes_deserialize", pub default_checkpoint: Vec<u8>,
serialize_with = "bytes_serialize" #[serde(default)]
)] #[serde(deserialize_with = "bytes_opt_deserialize")]
pub checkpoint: Vec<u8>, pub checkpoint: Option<Vec<u8>>,
pub data_dir: Option<PathBuf>, pub data_dir: Option<PathBuf>,
pub chain: ChainConfig, pub chain: ChainConfig,
pub forks: Forks, pub forks: Forks,
@ -86,7 +86,7 @@ impl Config {
BaseConfig { BaseConfig {
rpc_port: self.rpc_port.unwrap_or(8545), rpc_port: self.rpc_port.unwrap_or(8545),
consensus_rpc: Some(self.consensus_rpc.clone()), consensus_rpc: Some(self.consensus_rpc.clone()),
checkpoint: self.checkpoint.clone(), default_checkpoint: self.default_checkpoint.clone(),
chain: self.chain.clone(), chain: self.chain.clone(),
forks: self.forks.clone(), forks: self.forks.clone(),
max_checkpoint_age: self.max_checkpoint_age, max_checkpoint_age: self.max_checkpoint_age,

View File

@ -35,7 +35,7 @@ impl Network {
pub fn mainnet() -> BaseConfig { pub fn mainnet() -> BaseConfig {
BaseConfig { BaseConfig {
checkpoint: hex_str_to_bytes( default_checkpoint: hex_str_to_bytes(
"0x766647f3c4e1fc91c0db9a9374032ae038778411fbff222974e11f2e3ce7dadf", "0x766647f3c4e1fc91c0db9a9374032ae038778411fbff222974e11f2e3ce7dadf",
) )
.unwrap(), .unwrap(),
@ -69,7 +69,7 @@ pub fn mainnet() -> BaseConfig {
pub fn goerli() -> BaseConfig { pub fn goerli() -> BaseConfig {
BaseConfig { BaseConfig {
checkpoint: hex_str_to_bytes( default_checkpoint: hex_str_to_bytes(
"0xd4344682866dbede543395ecf5adf9443a27f423a4b00f270458e7932686ced1", "0xd4344682866dbede543395ecf5adf9443a27f423a4b00f270458e7932686ced1",
) )
.unwrap(), .unwrap(),

View File

@ -15,3 +15,15 @@ where
let bytes_string = hex::encode(bytes); let bytes_string = hex::encode(bytes);
serializer.serialize_str(&bytes_string) serializer.serialize_str(&bytes_string)
} }
pub fn bytes_opt_deserialize<'de, D>(deserializer: D) -> Result<Option<Vec<u8>>, D::Error>
where
D: serde::Deserializer<'de>,
{
let bytes_opt: Option<String> = serde::Deserialize::deserialize(deserializer)?;
if let Some(bytes) = bytes_opt {
Ok(Some(hex_str_to_bytes(&bytes).unwrap()))
} else {
Ok(None)
}
}