Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Return more detail when invalid data is found in the DB during startup #2445

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions beacon_node/beacon_chain/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use slot_clock::{SlotClock, TestingSlotClock};
use std::marker::PhantomData;
use std::sync::Arc;
use std::time::Duration;
use store::{HotColdDB, ItemStore};
use store::{Error as StoreError, HotColdDB, ItemStore};
use task_executor::ShutdownReason;
use types::{
BeaconBlock, BeaconState, ChainSpec, EthSpec, Graffiti, Hash256, PublicKeyBytes, Signature,
Expand Down Expand Up @@ -237,11 +237,11 @@ where

let genesis_block = store
.get_block(&chain.genesis_block_root)
.map_err(|e| format!("DB error when reading genesis block: {:?}", e))?
.map_err(|e| descriptive_db_error("genesis block", &e))?
.ok_or("Genesis block not found in store")?;
let genesis_state = store
.get_state(&genesis_block.state_root(), Some(genesis_block.slot()))
.map_err(|e| format!("DB error when reading genesis state: {:?}", e))?
.map_err(|e| descriptive_db_error("genesis state", &e))?
.ok_or("Genesis block not found in store")?;

self.genesis_time = Some(genesis_state.genesis_time());
Expand Down Expand Up @@ -436,12 +436,12 @@ where

let head_block = store
.get_block(&head_block_root)
.map_err(|e| format!("DB error when reading head block: {:?}", e))?
.map_err(|e| descriptive_db_error("head block", &e))?
.ok_or("Head block not found in store")?;
let head_state_root = head_block.state_root();
let head_state = store
.get_state(&head_state_root, Some(head_block.slot()))
.map_err(|e| format!("DB error when reading head state: {:?}", e))?
.map_err(|e| descriptive_db_error("head state", &e))?
.ok_or("Head state not found in store")?;

let mut canonical_head = BeaconSnapshot {
Expand Down Expand Up @@ -653,6 +653,21 @@ fn genesis_block<T: EthSpec>(
))
}

// Helper function to return more useful errors when reading from the database.
fn descriptive_db_error(item: &str, error: &StoreError) -> String {
let additional_info = if let StoreError::SszDecodeError(_) = error {
"Ensure the data directory is not initialized for a different network. The \
--purge-db flag can be used to permanently delete the existing data directory."
} else {
"Database corruption may be present. If the issue persists, use \
--purge-db to permanently delete the existing data directory."
};
format!(
"DB error when reading {}: {:?}. {}",
item, error, additional_info
)
}

#[cfg(not(debug_assertions))]
#[cfg(test)]
mod test {
Expand Down