//! Adapters that make one or more [`BlockSource`]s simpler to poll for new chain tip transitions.
-use crate::{AsyncBlockSourceResult, BlockHeaderData, BlockSource, BlockSourceError, BlockSourceResult};
+use crate::{AsyncBlockSourceResult, BlockData, BlockHeaderData, BlockSource, BlockSourceError, BlockSourceResult};
-use bitcoin::blockdata::block::Block;
use bitcoin::hash_types::BlockHash;
use bitcoin::network::constants::Network;
use lightning::chain::BestBlock;
}
/// A chain tip relative to another chain tip in terms of block hash and chainwork.
-#[derive(Clone, Debug, PartialEq)]
+#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ChainTip {
/// A chain tip with the same hash as another chain's tip.
Common,
fn validate(self, block_hash: BlockHash) -> BlockSourceResult<Self::T> {
let pow_valid_block_hash = self.header
- .validate_pow(&self.header.target())
- .or_else(|e| Err(BlockSourceError::persistent(e)))?;
+ .validate_pow(self.header.target())
+ .map_err(BlockSourceError::persistent)?;
if pow_valid_block_hash != block_hash {
return Err(BlockSourceError::persistent("invalid block hash"));
}
}
-impl Validate for Block {
+impl Validate for BlockData {
type T = ValidatedBlock;
fn validate(self, block_hash: BlockHash) -> BlockSourceResult<Self::T> {
- let pow_valid_block_hash = self.header
- .validate_pow(&self.header.target())
- .or_else(|e| Err(BlockSourceError::persistent(e)))?;
+ let header = match &self {
+ BlockData::FullBlock(block) => &block.header,
+ BlockData::HeaderOnly(header) => header,
+ };
+
+ let pow_valid_block_hash = header
+ .validate_pow(header.target())
+ .map_err(BlockSourceError::persistent)?;
if pow_valid_block_hash != block_hash {
return Err(BlockSourceError::persistent("invalid block hash"));
}
- if !self.check_merkle_root() {
- return Err(BlockSourceError::persistent("invalid merkle root"));
- }
+ if let BlockData::FullBlock(block) = &self {
+ if !block.check_merkle_root() {
+ return Err(BlockSourceError::persistent("invalid merkle root"));
+ }
- if !self.check_witness_commitment() {
- return Err(BlockSourceError::persistent("invalid witness commitment"));
+ if !block.check_witness_commitment() {
+ return Err(BlockSourceError::persistent("invalid witness commitment"));
+ }
}
Ok(ValidatedBlock { block_hash, inner: self })
}
/// A block header with validated proof of work and corresponding block hash.
-#[derive(Clone, Copy, Debug, PartialEq)]
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ValidatedBlockHeader {
pub(crate) block_hash: BlockHash,
inner: BlockHeaderData,
if let Network::Bitcoin = network {
if self.height % 2016 == 0 {
- let previous_work = previous_header.header.work();
- if work > (previous_work << 2) || work < (previous_work >> 2) {
+ let target = self.header.target();
+ let previous_target = previous_header.header.target();
+ let min_target = previous_target.min_difficulty_transition_threshold();
+ let max_target = previous_target.max_difficulty_transition_threshold();
+ if target > max_target || target < min_target {
return Err(BlockSourceError::persistent("invalid difficulty transition"))
}
} else if self.header.bits != previous_header.header.bits {
/// A block with validated data against its transaction list and corresponding block hash.
pub struct ValidatedBlock {
pub(crate) block_hash: BlockHash,
- inner: Block,
+ inner: BlockData,
}
impl std::ops::Deref for ValidatedBlock {
- type Target = Block;
+ type Target = BlockData;
fn deref(&self) -> &Self::Target {
&self.inner
pub trait Validate {}
impl Validate for crate::BlockHeaderData {}
- impl Validate for bitcoin::blockdata::block::Block {}
+ impl Validate for crate::BlockData {}
}
/// The canonical `Poll` implementation used for a single `BlockSource`.
use crate::*;
use crate::test_utils::Blockchain;
use super::*;
- use bitcoin::util::uint::Uint256;
#[tokio::test]
async fn poll_empty_chain() {
// Invalidate the tip by changing its target.
chain.blocks.last_mut().unwrap().header.bits =
- BlockHeader::compact_target_from_u256(&Uint256::from_be_bytes([0; 32]));
+ bitcoin::Target::from_be_bytes([0x01; 32]).to_compact_lossy();
let poller = ChainPoller::new(&chain, Network::Bitcoin);
match poller.poll_chain_tip(best_known_chain_tip).await {