Skip to content

Commit

Permalink
WIP: Fix block height witnesser bugs (#5498)
Browse files Browse the repository at this point in the history
Fix BHW bugs, now reorgs are processed correctly.
  • Loading branch information
MxmUrw authored and kylezs committed Dec 17, 2024
1 parent 057eac0 commit 2683778
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
23 changes: 16 additions & 7 deletions engine/src/witness/btc_e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
},
witness::btc::deposits::{deposit_witnesses, map_script_addresses},
};
use anyhow::Result;
use anyhow::{anyhow, Result};

use std::sync::Arc;

Expand Down Expand Up @@ -118,34 +118,43 @@ impl VoterApi<BitcoinBlockHeightTracking> for BitcoinBlockHeightTrackingVoter {
let get_header = |index: BlockNumber| {
async move {
let header = self.client.block_header(index).await?;
tracing::info!("Voting for block height tracking: {:?}", header.height);
// tracing::info!("bht: Voting for block height tracking: {:?}", header.height);
// Order from lowest to highest block index.
Ok::<Header<sp_core::H256, u64>, anyhow::Error>(header_from_btc_header(header)?)
}
};

let best_block_header = header_from_btc_header(self.client.best_block_header().await?)?;

if witness_from_index == 0 {
if best_block_header.block_height <= witness_from_index {
Err(anyhow::anyhow!("btc: no new blocks found since best block height is {} for witness_from={witness_from_index}", best_block_header.block_height))
} else if witness_from_index == 0 {
headers.push_back(best_block_header);
Ok(headers)
} else {
// fetch the headers we haven't got yet
for index in witness_from_index..best_block_header.block_height {
let header = self.client.block_header(index).await?;
tracing::info!("Voting for block height tracking: {:?}", header.height);
// let header = self.client.block_header(index).await?;
// tracing::info!("bht: Voting for block height tracking: {:?}", header.height);
// Order from lowest to highest block index.
headers.push_back(get_header(index).await?);
}

headers.push_back(best_block_header);
tracing::info!(
"bht: Voting for block height tracking: {:?}",
headers.iter().map(|h| h.block_height)
);

// We should have a chain of hashees.
if headers.iter().zip(headers.iter().skip(1)).all(|(a, b)| a.hash == b.parent_hash) {
tracing::info!("Submitting vote with {} headers", headers.len());
tracing::info!(
"bht: Submitting vote for (witness_from={witness_from_index})with {} headers",
headers.len()
);
Ok(headers)
} else {
Err(anyhow::anyhow!("Headers do not form a chain"))
Err(anyhow::anyhow!("bht: Headers do not form a chain"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ impl<
unsynchronised_state
.headers
.current_state_as_no_chain_progress(),
unsynchronised_state.headers.next_height,
unsynchronised_state
.headers
.first_height()
.unwrap_or(0u32.into()), /* If we have no first height
* recorded, we have to restart
* the election?! */
))
},
Err(MergeFailure::InternalError(reason)) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ impl<H, N: Copy> ChainBlocks<H, N> {
ChainProgress::WaitingForFirstConsensus
}
}

pub fn first_height(&self) -> Option<N> {
self.headers.front().map(|h| h.block_height)
}
}

pub fn validate_continous_headers<H: PartialEq + Clone, N: PartialEq>(
Expand Down

0 comments on commit 2683778

Please sign in to comment.