Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Prevent lost update of block details
Browse files Browse the repository at this point in the history
  • Loading branch information
AtkinsChang committed Sep 2, 2019
1 parent 784e159 commit 4bb8d5d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
8 changes: 5 additions & 3 deletions ethcore/blockchain/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ impl BlockChain {

self.prepare_update(batch, ExtrasUpdate {
block_hashes: self.prepare_block_hashes_update(&info),
block_details: self.prepare_block_details_update(block_parent_hash, &info, false),
block_details: self.prepare_block_details_update(block_parent_hash, &info, false, false),
block_receipts: self.prepare_block_receipts_update(receipts, &info),
blocks_blooms: self.prepare_block_blooms_update(block.header_view().log_bloom(), &info),
transactions_addresses: self.prepare_transaction_addresses_update(block.view().transaction_hashes(), &info),
Expand Down Expand Up @@ -1103,7 +1103,7 @@ impl BlockChain {

self.prepare_update(batch, ExtrasUpdate {
block_hashes: self.prepare_block_hashes_update(&info),
block_details: self.prepare_block_details_update(parent_hash, &info, extras.is_finalized),
block_details: self.prepare_block_details_update(parent_hash, &info, extras.is_finalized, extras.is_parent_finalized),
block_receipts: self.prepare_block_receipts_update(receipts, &info),
blocks_blooms: self.prepare_block_blooms_update(block.header_view().log_bloom(), &info),
transactions_addresses: self.prepare_transaction_addresses_update(block.view().transaction_hashes(), &info),
Expand Down Expand Up @@ -1342,9 +1342,10 @@ impl BlockChain {

/// This function returns modified block details.
/// Uses the given parent details or attempts to load them from the database.
fn prepare_block_details_update(&self, parent_hash: H256, info: &BlockInfo, is_finalized: bool) -> HashMap<H256, BlockDetails> {
fn prepare_block_details_update(&self, parent_hash: H256, info: &BlockInfo, is_finalized: bool, is_parent_finalized: bool) -> HashMap<H256, BlockDetails> {
// update parent
let mut parent_details = self.block_details(&parent_hash).unwrap_or_else(|| panic!("Invalid parent hash: {:?}", parent_hash));
parent_details.is_finalized = is_parent_finalized;
parent_details.children.push(info.hash);

// create current block details.
Expand Down Expand Up @@ -1662,6 +1663,7 @@ mod tests {
bc.insert_block(batch, block, receipts, ExtrasInsert {
fork_choice: fork_choice,
is_finalized: false,
is_parent_finalized: false,
})
}

Expand Down
2 changes: 2 additions & 0 deletions ethcore/blockchain/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ pub struct ExtrasInsert {
pub fork_choice: ForkChoice,
/// Is the inserted block considered finalized.
pub is_finalized: bool,
/// Is the inserted block parent considered finalized.
pub is_parent_finalized: bool,
}
12 changes: 9 additions & 3 deletions ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ impl Importer {
let parent = header.parent_hash();
let chain = client.chain.read();
let mut is_finalized = false;
let mut is_parent_finalized = false;

// Commit results
let block = block.drain();
Expand Down Expand Up @@ -523,11 +524,14 @@ impl Importer {
let finalized: Vec<_> = ancestry_actions.into_iter().map(|ancestry_action| {
let AncestryAction::MarkFinalized(a) = ancestry_action;

if a != header.hash() {
chain.mark_finalized(&mut batch, a).expect("Engine's ancestry action must be known blocks; qed");
} else {
if a == header.hash() {
// we're finalizing the current block
is_finalized = true;
} else if a == *header.parent_hash() {
// we're finalizing the parent block
is_parent_finalized = true;
} else {
chain.mark_finalized(&mut batch, a).expect("Engine's ancestry action must be known blocks; qed");
}

a
Expand All @@ -536,6 +540,7 @@ impl Importer {
let route = chain.insert_block(&mut batch, block_data, receipts.clone(), ExtrasInsert {
fork_choice,
is_finalized,
is_parent_finalized,
});

client.tracedb.read().import(&mut batch, TraceImportRequest {
Expand Down Expand Up @@ -2667,6 +2672,7 @@ mod tests {
another_client.chain.read().insert_block(&mut batch, encoded::Block::new(new_block), Vec::new(), ExtrasInsert {
fork_choice: ForkChoice::New,
is_finalized: false,
is_parent_finalized: false,
});
go_thread.store(true, Ordering::SeqCst);
});
Expand Down
1 change: 1 addition & 0 deletions ethcore/src/snapshot/tests/proof_of_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn chunk_and_restore(amount: u64) {
bc.insert_block(&mut batch, block.encoded(), vec![], ExtrasInsert {
fork_choice: ForkChoice::New,
is_finalized: false,
is_parent_finalized: false,
});
bc.commit();
}
Expand Down
2 changes: 2 additions & 0 deletions ethcore/src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ pub fn generate_dummy_blockchain(block_number: u32) -> BlockChain {
bc.insert_block(&mut batch, encoded::Block::new(create_unverifiable_block(block_order, bc.best_block_hash())), vec![], ExtrasInsert {
fork_choice: ForkChoice::New,
is_finalized: false,
is_parent_finalized: false,
});
bc.commit();
}
Expand All @@ -396,6 +397,7 @@ pub fn generate_dummy_blockchain_with_extra(block_number: u32) -> BlockChain {
bc.insert_block(&mut batch, encoded::Block::new(create_unverifiable_block_with_extra(block_order, bc.best_block_hash(), None)), vec![], ExtrasInsert {
fork_choice: ForkChoice::New,
is_finalized: false,
is_parent_finalized: false,
});
bc.commit();
}
Expand Down

0 comments on commit 4bb8d5d

Please sign in to comment.