Skip to content

Commit

Permalink
fix: ensure that accumulated orphan chain data is committed before he…
Browse files Browse the repository at this point in the history
…ader validation

- commit orphan chain accumulated data before orphan header validation so that
  accumulated target difficulty is available for chain comparison
- use hash for reorg chain rewind because height may potentially be
  manipulated to cause a large rewind
  • Loading branch information
sdbondi committed Oct 16, 2021
1 parent 1d11ae4 commit 8975825
Show file tree
Hide file tree
Showing 16 changed files with 635 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,10 @@ impl<'a, B: BlockchainBackend + 'static> HorizonStateSynchronization<'a, B> {
.sync_validators
.final_horizon_state
.validate(
&*self.db().clone().into_inner().db_read_access()?,
header.height(),
&pruned_utxo_sum,
&pruned_kernel_sum,
&*self.db().clone().into_inner().db_read_access()?,
)
.map_err(HorizonSyncError::FinalStateValidationFailed)?;

Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/chain_storage/accumulated_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ impl BlockHeaderAccumulatedDataBuilder<'_> {
.hash
.ok_or_else(|| ChainStorageError::InvalidOperation("hash not provided".to_string()))?;

if hash == self.previous_accum.hash {
if hash == previous_accum.hash {
return Err(ChainStorageError::InvalidOperation(
"Hash was set to the same hash that is contained in previous accumulated data".to_string(),
));
Expand Down
13 changes: 9 additions & 4 deletions base_layer/core/src/chain_storage/block_add_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ impl BlockAddResult {
matches!(self, BlockAddResult::Ok(_))
}

pub fn is_chain_reorg(&self) -> bool {
matches!(self, BlockAddResult::ChainReorg { .. })
}

pub fn is_orphaned(&self) -> bool {
matches!(self, BlockAddResult::OrphanBlock)
}

pub fn assert_added(&self) -> ChainBlock {
match self {
BlockAddResult::ChainReorg { added, removed } => panic!(
Expand All @@ -60,10 +68,7 @@ impl BlockAddResult {
}

pub fn assert_orphaned(&self) {
match self {
BlockAddResult::OrphanBlock => (),
_ => panic!("Result was not orphaned"),
}
assert!(self.is_orphaned(), "Result was not orphaned");
}

pub fn assert_reorg(&self, num_added: usize, num_removed: usize) {
Expand Down
Loading

0 comments on commit 8975825

Please sign in to comment.