From 83cad682a1e0d7f32d7e5c28ecc23cb12eaadee8 Mon Sep 17 00:00:00 2001 From: Martin Stefcek Date: Mon, 31 Jan 2022 12:25:11 +0700 Subject: [PATCH] chore: check genesis block during startup --- .../src/chain_storage/blockchain_database.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/base_layer/core/src/chain_storage/blockchain_database.rs b/base_layer/core/src/chain_storage/blockchain_database.rs index 29b62b8a96..b47c0787ab 100644 --- a/base_layer/core/src/chain_storage/blockchain_database.rs +++ b/base_layer/core/src/chain_storage/blockchain_database.rs @@ -214,9 +214,9 @@ where B: BlockchainBackend difficulty_calculator: Arc::new(difficulty_calculator), disable_add_block_flag: Arc::new(AtomicBool::new(false)), }; + let genesis_block = Arc::new(blockchain_db.consensus_manager.get_genesis_block()); if is_empty { info!(target: LOG_TARGET, "Blockchain db is empty. Adding genesis block."); - let genesis_block = Arc::new(blockchain_db.consensus_manager.get_genesis_block()); blockchain_db.insert_block(genesis_block.clone())?; let mut txn = DbTransaction::new(); let body = &genesis_block.block().body; @@ -230,6 +230,20 @@ where B: BlockchainBackend txn.set_horizon_data(kernel_sum, utxo_sum); blockchain_db.write(txn)?; blockchain_db.store_pruning_horizon(config.pruning_horizon)?; + } else if !blockchain_db.block_exists(genesis_block.accumulated_data().hash.clone())? { + // Check the genesis block in the DB. + error!( + target: LOG_TARGET, + "Genesis block in database does not match the supplied genesis block in the code! Hash in the code \ + {:?}, hash in the database {:?}", + blockchain_db.fetch_chain_header(0)?.hash(), + genesis_block.accumulated_data().hash + ); + return Err(ChainStorageError::CorruptedDatabase( + "Genesis block in database does not match the supplied genesis block in the code! Please delete and \ + resync your blockchain database." + .into(), + )); } if cleanup_orphans_at_startup { match blockchain_db.cleanup_all_orphans() {