From 7275f8d9227287e9f863cb53deaaf023fce7ea01 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Mon, 27 Feb 2023 13:08:20 +0000 Subject: [PATCH] chore: check genesis mismatch in `init_genesis` (#1560) --- bin/reth/src/chain/init.rs | 11 +----- crates/staged-sync/Cargo.toml | 3 +- crates/staged-sync/src/utils/init.rs | 53 +++++++++++++++++++++++----- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/bin/reth/src/chain/init.rs b/bin/reth/src/chain/init.rs index e8eb32afeb..82d7d1f434 100644 --- a/bin/reth/src/chain/init.rs +++ b/bin/reth/src/chain/init.rs @@ -49,16 +49,7 @@ impl InitCommand { info!(target: "reth::cli", "Database opened"); info!(target: "reth::cli", "Writing genesis block"); - let genesis_hash = init_genesis(db, self.chain.clone())?; - - if genesis_hash != self.chain.genesis_hash() { - // TODO: better error text - return Err(eyre::eyre!( - "Genesis hash mismatch: expected {}, got {}", - self.chain.genesis_hash(), - genesis_hash - )) - } + init_genesis(db, self.chain.clone())?; Ok(()) } diff --git a/crates/staged-sync/Cargo.toml b/crates/staged-sync/Cargo.toml index dc440eaa63..5521c66634 100644 --- a/crates/staged-sync/Cargo.toml +++ b/crates/staged-sync/Cargo.toml @@ -41,7 +41,7 @@ tracing = "0.1.37" rand = { version = "0.8", optional = true } # errors -thiserror = { version = "1", optional = true } +thiserror = "1" # enr enr = { version = "0.7.0", features = ["serde", "rust-secp256k1"], optional = true } @@ -89,7 +89,6 @@ test-utils = [ "dep:enr", "dep:ethers-core", "dep:tempfile", - "dep:thiserror", "dep:hex", "dep:rand", "dep:tokio", diff --git a/crates/staged-sync/src/utils/init.rs b/crates/staged-sync/src/utils/init.rs index 5551ee7d9d..a43514881c 100644 --- a/crates/staged-sync/src/utils/init.rs +++ b/crates/staged-sync/src/utils/init.rs @@ -21,14 +21,37 @@ pub fn init_db>(path: P) -> eyre::Result> { Ok(db) } +/// Database initialization error type. +#[derive(Debug, thiserror::Error, PartialEq, Eq, Clone)] +pub enum InitDatabaseError { + /// Attempted to reinitialize database with inconsistent genesis block + #[error("Genesis hash mismatch: expected {expected}, got {actual}")] + GenesisHashMismatch { expected: H256, actual: H256 }, + + /// Low-level database error. + #[error(transparent)] + DBError(#[from] reth_db::Error), +} + /// Write the genesis block if it has not already been written #[allow(clippy::field_reassign_with_default)] -pub fn init_genesis(db: Arc, chain: ChainSpec) -> Result { +pub fn init_genesis( + db: Arc, + chain: ChainSpec, +) -> Result { let genesis = chain.genesis(); + + let header = chain.genesis_header(); + let hash = header.hash_slow(); + let tx = db.tx()?; - if let Some((_, hash)) = tx.cursor_read::()?.first()? { - debug!("Genesis already written, skipping."); - return Ok(hash) + if let Some((_, db_hash)) = tx.cursor_read::()?.first()? { + if db_hash == hash { + debug!("Genesis already written, skipping."); + return Ok(hash) + } + + return Err(InitDatabaseError::GenesisHashMismatch { expected: hash, actual: db_hash }) } drop(tx); @@ -48,9 +71,6 @@ pub fn init_genesis(db: Arc, chain: ChainSpec) -> Result(0, hash)?; tx.put::(hash, 0)?; tx.put::(0, Default::default())?; @@ -65,7 +85,7 @@ pub fn init_genesis(db: Arc, chain: ChainSpec) -> Result