projects
/
rust-lightning
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge pull request #1685 from wpaulino/anchors-prep
[rust-lightning]
/
lightning-block-sync
/
src
/
poll.rs
diff --git
a/lightning-block-sync/src/poll.rs
b/lightning-block-sync/src/poll.rs
index c59652ea51716db9c7d24c3d9015c2e73f13833b..2bb2f4a07df9e4a0aa79759249d795cc81cd833d 100644
(file)
--- a/
lightning-block-sync/src/poll.rs
+++ b/
lightning-block-sync/src/poll.rs
@@
-1,12
+1,11
@@
//! Adapters that make one or more [`BlockSource`]s simpler to poll for new chain tip transitions.
//! 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, Block
Data, Block
HeaderData, BlockSource, BlockSourceError, BlockSourceResult};
-use bitcoin::blockdata::block::Block;
use bitcoin::hash_types::BlockHash;
use bitcoin::network::constants::Network;
use bitcoin::hash_types::BlockHash;
use bitcoin::network::constants::Network;
-use std::ops::Deref
Mut
;
+use std::ops::Deref;
/// The `Poll` trait defines behavior for polling block sources for a chain tip and retrieving
/// related chain data. It serves as an adapter for `BlockSource`.
/// The `Poll` trait defines behavior for polling block sources for a chain tip and retrieving
/// related chain data. It serves as an adapter for `BlockSource`.
@@
-17,15
+16,15
@@
use std::ops::DerefMut;
/// [`ChainPoller`]: ../struct.ChainPoller.html
pub trait Poll {
/// Returns a chain tip in terms of its relationship to the provided chain tip.
/// [`ChainPoller`]: ../struct.ChainPoller.html
pub trait Poll {
/// Returns a chain tip in terms of its relationship to the provided chain tip.
- fn poll_chain_tip<'a>(&'a
mut
self, best_known_chain_tip: ValidatedBlockHeader) ->
+ fn poll_chain_tip<'a>(&'a self, best_known_chain_tip: ValidatedBlockHeader) ->
AsyncBlockSourceResult<'a, ChainTip>;
/// Returns the header that preceded the given header in the chain.
AsyncBlockSourceResult<'a, ChainTip>;
/// Returns the header that preceded the given header in the chain.
- fn look_up_previous_header<'a>(&'a
mut
self, header: &'a ValidatedBlockHeader) ->
+ fn look_up_previous_header<'a>(&'a self, header: &'a ValidatedBlockHeader) ->
AsyncBlockSourceResult<'a, ValidatedBlockHeader>;
/// Returns the block associated with the given header.
AsyncBlockSourceResult<'a, ValidatedBlockHeader>;
/// Returns the block associated with the given header.
- fn fetch_block<'a>(&'a
mut
self, header: &'a ValidatedBlockHeader) ->
+ fn fetch_block<'a>(&'a self, header: &'a ValidatedBlockHeader) ->
AsyncBlockSourceResult<'a, ValidatedBlock>;
}
AsyncBlockSourceResult<'a, ValidatedBlock>;
}
@@
-59,12
+58,11
@@
impl Validate for BlockHeaderData {
type T = ValidatedBlockHeader;
fn validate(self, block_hash: BlockHash) -> BlockSourceResult<Self::T> {
type T = ValidatedBlockHeader;
fn validate(self, block_hash: BlockHash) -> BlockSourceResult<Self::T> {
- self.header
+
let pow_valid_block_hash =
self.header
.validate_pow(&self.header.target())
.or_else(|e| Err(BlockSourceError::persistent(e)))?;
.validate_pow(&self.header.target())
.or_else(|e| Err(BlockSourceError::persistent(e)))?;
- // TODO: Use the result of validate_pow instead of recomputing the block hash once upstream.
- if self.header.block_hash() != block_hash {
+ if pow_valid_block_hash != block_hash {
return Err(BlockSourceError::persistent("invalid block hash"));
}
return Err(BlockSourceError::persistent("invalid block hash"));
}
@@
-72,25
+70,31
@@
impl Validate for BlockHeaderData {
}
}
}
}
-impl Validate for Block {
+impl Validate for Block
Data
{
type T = ValidatedBlock;
fn validate(self, block_hash: BlockHash) -> BlockSourceResult<Self::T> {
type T = ValidatedBlock;
fn validate(self, block_hash: BlockHash) -> BlockSourceResult<Self::T> {
- self.header
- .validate_pow(&self.header.target())
+ let header = match &self {
+ BlockData::FullBlock(block) => &block.header,
+ BlockData::HeaderOnly(header) => header,
+ };
+
+ let pow_valid_block_hash = header
+ .validate_pow(&header.target())
.or_else(|e| Err(BlockSourceError::persistent(e)))?;
.or_else(|e| Err(BlockSourceError::persistent(e)))?;
- // TODO: Use the result of validate_pow instead of recomputing the block hash once upstream.
- if self.block_hash() != block_hash {
+ if pow_valid_block_hash != block_hash {
return Err(BlockSourceError::persistent("invalid 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 })
}
Ok(ValidatedBlock { block_hash, inner: self })
@@
-147,11
+151,11
@@
impl ValidatedBlockHeader {
/// A block with validated data against its transaction list and corresponding block hash.
pub struct ValidatedBlock {
pub(crate) block_hash: BlockHash,
/// A block with validated data against its transaction list and corresponding block hash.
pub struct ValidatedBlock {
pub(crate) block_hash: BlockHash,
- inner: Block,
+ inner: Block
Data
,
}
impl std::ops::Deref for ValidatedBlock {
}
impl std::ops::Deref for ValidatedBlock {
- type Target = Block;
+ type Target = Block
Data
;
fn deref(&self) -> &Self::Target {
&self.inner
fn deref(&self) -> &Self::Target {
&self.inner
@@
-163,19
+167,19
@@
mod sealed {
pub trait Validate {}
impl Validate for crate::BlockHeaderData {}
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`.
///
/// Other `Poll` implementations should be built using `ChainPoller` as it provides the simplest way
/// of validating chain data and checking consistency.
}
/// The canonical `Poll` implementation used for a single `BlockSource`.
///
/// Other `Poll` implementations should be built using `ChainPoller` as it provides the simplest way
/// of validating chain data and checking consistency.
-pub struct ChainPoller<B: Deref
Mut<Target=T> + Sized, T: BlockSource
> {
+pub struct ChainPoller<B: Deref
<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized
> {
block_source: B,
network: Network,
}
block_source: B,
network: Network,
}
-impl<B: Deref
Mut<Target=T> + Sized, T: BlockSource
> ChainPoller<B, T> {
+impl<B: Deref
<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized
> ChainPoller<B, T> {
/// Creates a new poller for the given block source.
///
/// If the `network` parameter is mainnet, then the difficulty between blocks is checked for
/// Creates a new poller for the given block source.
///
/// If the `network` parameter is mainnet, then the difficulty between blocks is checked for
@@
-185,8
+189,8
@@
impl<B: DerefMut<Target=T> + Sized, T: BlockSource> ChainPoller<B, T> {
}
}
}
}
-impl<B: Deref
Mut<Target=T> + Sized + Send + Sync, T: BlockSource
> Poll for ChainPoller<B, T> {
- fn poll_chain_tip<'a>(&'a
mut
self, best_known_chain_tip: ValidatedBlockHeader) ->
+impl<B: Deref
<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized
> Poll for ChainPoller<B, T> {
+ fn poll_chain_tip<'a>(&'a self, best_known_chain_tip: ValidatedBlockHeader) ->
AsyncBlockSourceResult<'a, ChainTip>
{
Box::pin(async move {
AsyncBlockSourceResult<'a, ChainTip>
{
Box::pin(async move {
@@
-206,7
+210,7
@@
impl<B: DerefMut<Target=T> + Sized + Send + Sync, T: BlockSource> Poll for Chain
})
}
})
}
- fn look_up_previous_header<'a>(&'a
mut
self, header: &'a ValidatedBlockHeader) ->
+ fn look_up_previous_header<'a>(&'a self, header: &'a ValidatedBlockHeader) ->
AsyncBlockSourceResult<'a, ValidatedBlockHeader>
{
Box::pin(async move {
AsyncBlockSourceResult<'a, ValidatedBlockHeader>
{
Box::pin(async move {
@@
-225,7
+229,7
@@
impl<B: DerefMut<Target=T> + Sized + Send + Sync, T: BlockSource> Poll for Chain
})
}
})
}
- fn fetch_block<'a>(&'a
mut
self, header: &'a ValidatedBlockHeader) ->
+ fn fetch_block<'a>(&'a self, header: &'a ValidatedBlockHeader) ->
AsyncBlockSourceResult<'a, ValidatedBlock>
{
Box::pin(async move {
AsyncBlockSourceResult<'a, ValidatedBlock>
{
Box::pin(async move {
@@
-249,7
+253,7
@@
mod tests {
let best_known_chain_tip = chain.tip();
chain.disconnect_tip();
let best_known_chain_tip = chain.tip();
chain.disconnect_tip();
- let
mut poller = ChainPoller::new(&mut
chain, Network::Bitcoin);
+ let
poller = ChainPoller::new(&
chain, Network::Bitcoin);
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => {
assert_eq!(e.kind(), BlockSourceErrorKind::Transient);
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => {
assert_eq!(e.kind(), BlockSourceErrorKind::Transient);
@@
-261,10
+265,10
@@
mod tests {
#[tokio::test]
async fn poll_chain_without_headers() {
#[tokio::test]
async fn poll_chain_without_headers() {
- let
mut
chain = Blockchain::default().with_height(1).without_headers();
+ let chain = Blockchain::default().with_height(1).without_headers();
let best_known_chain_tip = chain.at_height(0);
let best_known_chain_tip = chain.at_height(0);
- let
mut poller = ChainPoller::new(&mut
chain, Network::Bitcoin);
+ let
poller = ChainPoller::new(&
chain, Network::Bitcoin);
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => {
assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => {
assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
@@
-283,7
+287,7
@@
mod tests {
chain.blocks.last_mut().unwrap().header.bits =
BlockHeader::compact_target_from_u256(&Uint256::from_be_bytes([0; 32]));
chain.blocks.last_mut().unwrap().header.bits =
BlockHeader::compact_target_from_u256(&Uint256::from_be_bytes([0; 32]));
- let
mut poller = ChainPoller::new(&mut
chain, Network::Bitcoin);
+ let
poller = ChainPoller::new(&
chain, Network::Bitcoin);
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => {
assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => {
assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
@@
-295,10
+299,10
@@
mod tests {
#[tokio::test]
async fn poll_chain_with_malformed_headers() {
#[tokio::test]
async fn poll_chain_with_malformed_headers() {
- let
mut
chain = Blockchain::default().with_height(1).malformed_headers();
+ let chain = Blockchain::default().with_height(1).malformed_headers();
let best_known_chain_tip = chain.at_height(0);
let best_known_chain_tip = chain.at_height(0);
- let
mut poller = ChainPoller::new(&mut
chain, Network::Bitcoin);
+ let
poller = ChainPoller::new(&
chain, Network::Bitcoin);
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => {
assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => {
assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
@@
-310,10
+314,10
@@
mod tests {
#[tokio::test]
async fn poll_chain_with_common_tip() {
#[tokio::test]
async fn poll_chain_with_common_tip() {
- let
mut
chain = Blockchain::default().with_height(0);
+ let chain = Blockchain::default().with_height(0);
let best_known_chain_tip = chain.tip();
let best_known_chain_tip = chain.tip();
- let
mut poller = ChainPoller::new(&mut
chain, Network::Bitcoin);
+ let
poller = ChainPoller::new(&
chain, Network::Bitcoin);
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => panic!("Unexpected error: {:?}", e),
Ok(tip) => assert_eq!(tip, ChainTip::Common),
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => panic!("Unexpected error: {:?}", e),
Ok(tip) => assert_eq!(tip, ChainTip::Common),
@@
-330,7
+334,7
@@
mod tests {
let worse_chain_tip = chain.tip();
assert_eq!(best_known_chain_tip.chainwork, worse_chain_tip.chainwork);
let worse_chain_tip = chain.tip();
assert_eq!(best_known_chain_tip.chainwork, worse_chain_tip.chainwork);
- let
mut poller = ChainPoller::new(&mut
chain, Network::Bitcoin);
+ let
poller = ChainPoller::new(&
chain, Network::Bitcoin);
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => panic!("Unexpected error: {:?}", e),
Ok(tip) => assert_eq!(tip, ChainTip::Worse(worse_chain_tip)),
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => panic!("Unexpected error: {:?}", e),
Ok(tip) => assert_eq!(tip, ChainTip::Worse(worse_chain_tip)),
@@
-345,7
+349,7
@@
mod tests {
chain.disconnect_tip();
let worse_chain_tip = chain.tip();
chain.disconnect_tip();
let worse_chain_tip = chain.tip();
- let
mut poller = ChainPoller::new(&mut
chain, Network::Bitcoin);
+ let
poller = ChainPoller::new(&
chain, Network::Bitcoin);
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => panic!("Unexpected error: {:?}", e),
Ok(tip) => assert_eq!(tip, ChainTip::Worse(worse_chain_tip)),
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => panic!("Unexpected error: {:?}", e),
Ok(tip) => assert_eq!(tip, ChainTip::Worse(worse_chain_tip)),
@@
-354,12
+358,12
@@
mod tests {
#[tokio::test]
async fn poll_chain_with_better_tip() {
#[tokio::test]
async fn poll_chain_with_better_tip() {
- let
mut
chain = Blockchain::default().with_height(1);
+ let chain = Blockchain::default().with_height(1);
let best_known_chain_tip = chain.at_height(0);
let better_chain_tip = chain.tip();
let best_known_chain_tip = chain.at_height(0);
let better_chain_tip = chain.tip();
- let
mut poller = ChainPoller::new(&mut
chain, Network::Bitcoin);
+ let
poller = ChainPoller::new(&
chain, Network::Bitcoin);
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => panic!("Unexpected error: {:?}", e),
Ok(tip) => assert_eq!(tip, ChainTip::Better(better_chain_tip)),
match poller.poll_chain_tip(best_known_chain_tip).await {
Err(e) => panic!("Unexpected error: {:?}", e),
Ok(tip) => assert_eq!(tip, ChainTip::Better(better_chain_tip)),