Merge pull request #1610 from ethcore/db-repair

Fix bloomchain on blockchain repair
This commit is contained in:
Nikolay Volf 2016-07-14 01:14:57 +02:00 committed by GitHub
commit 50c43bd819

View File

@ -298,16 +298,28 @@ impl BlockChain {
Some(best) => { Some(best) => {
let best = H256::from_slice(&best); let best = H256::from_slice(&best);
let mut b = best.clone(); let mut b = best.clone();
let mut removed = 0;
let mut best_num = 0;
while !bc.blocks_db.get(&b).unwrap().is_some() { while !bc.blocks_db.get(&b).unwrap().is_some() {
// track back to the best block we have in the blocks database // track back to the best block we have in the blocks database
let extras: BlockDetails = bc.extras_db.read(&b).unwrap(); let extras: BlockDetails = bc.extras_db.read(&b).unwrap();
type DetailsKey = Key<BlockDetails, Target=H264>; type DetailsKey = Key<BlockDetails, Target=H264>;
bc.extras_db.delete(&(DetailsKey::key(&b))).unwrap(); bc.extras_db.delete(&(DetailsKey::key(&b))).unwrap();
b = extras.parent; b = extras.parent;
best_num = extras.number;
removed += 1;
} }
if b != best { if b != best {
let batch = DBTransaction::new();
let range = (best_num + 1) as bc::Number .. (best_num + removed) as bc::Number;
let chain = bc::group::BloomGroupChain::new(bc.blooms_config, &bc);
let changes = chain.replace(&range, vec![]);
for (k, v) in changes.into_iter() {
batch.write(&LogGroupPosition::from(k), &BloomGroup::from(v));
}
batch.put(b"best", &b).unwrap();
bc.extras_db.write(batch).unwrap();
info!("Restored mismatched best block. Was: {}, new: {}", best.hex(), b.hex()); info!("Restored mismatched best block. Was: {}, new: {}", best.hex(), b.hex());
bc.extras_db.put(b"best", &b).unwrap();
} }
b b
} }