Skip to content

Commit a7b7134

Browse files
committed
Return more detail when invalid data is found in the DB during startup (#2445)
## Issue Addressed - Resolves #2444 ## Proposed Changes Adds some more detail to the error message returned when the `BeaconChainBuilder` is unable to access or decode block/state objects during startup. ## Additional Info NA
1 parent 371c216 commit a7b7134

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

beacon_node/beacon_chain/src/builder.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use slot_clock::{SlotClock, TestingSlotClock};
2424
use std::marker::PhantomData;
2525
use std::sync::Arc;
2626
use std::time::Duration;
27-
use store::{HotColdDB, ItemStore};
27+
use store::{Error as StoreError, HotColdDB, ItemStore};
2828
use task_executor::ShutdownReason;
2929
use types::{
3030
BeaconBlock, BeaconState, ChainSpec, EthSpec, Graffiti, Hash256, PublicKeyBytes, Signature,
@@ -237,11 +237,11 @@ where
237237

238238
let genesis_block = store
239239
.get_block(&chain.genesis_block_root)
240-
.map_err(|e| format!("DB error when reading genesis block: {:?}", e))?
240+
.map_err(|e| descriptive_db_error("genesis block", &e))?
241241
.ok_or("Genesis block not found in store")?;
242242
let genesis_state = store
243243
.get_state(&genesis_block.state_root(), Some(genesis_block.slot()))
244-
.map_err(|e| format!("DB error when reading genesis state: {:?}", e))?
244+
.map_err(|e| descriptive_db_error("genesis state", &e))?
245245
.ok_or("Genesis block not found in store")?;
246246

247247
self.genesis_time = Some(genesis_state.genesis_time());
@@ -436,12 +436,12 @@ where
436436

437437
let head_block = store
438438
.get_block(&head_block_root)
439-
.map_err(|e| format!("DB error when reading head block: {:?}", e))?
439+
.map_err(|e| descriptive_db_error("head block", &e))?
440440
.ok_or("Head block not found in store")?;
441441
let head_state_root = head_block.state_root();
442442
let head_state = store
443443
.get_state(&head_state_root, Some(head_block.slot()))
444-
.map_err(|e| format!("DB error when reading head state: {:?}", e))?
444+
.map_err(|e| descriptive_db_error("head state", &e))?
445445
.ok_or("Head state not found in store")?;
446446

447447
let mut canonical_head = BeaconSnapshot {
@@ -653,6 +653,21 @@ fn genesis_block<T: EthSpec>(
653653
))
654654
}
655655

656+
// Helper function to return more useful errors when reading from the database.
657+
fn descriptive_db_error(item: &str, error: &StoreError) -> String {
658+
let additional_info = if let StoreError::SszDecodeError(_) = error {
659+
"Ensure the data directory is not initialized for a different network. The \
660+
--purge-db flag can be used to permanently delete the existing data directory."
661+
} else {
662+
"Database corruption may be present. If the issue persists, use \
663+
--purge-db to permanently delete the existing data directory."
664+
};
665+
format!(
666+
"DB error when reading {}: {:?}. {}",
667+
item, error, additional_info
668+
)
669+
}
670+
656671
#[cfg(not(debug_assertions))]
657672
#[cfg(test)]
658673
mod test {

0 commit comments

Comments
 (0)