lightning_block_sync/
poll.rs

1//! Adapters that make one or more [`BlockSource`]s simpler to poll for new chain tip transitions.
2
3use crate::{
4	AsyncBlockSourceResult, BlockData, BlockHeaderData, BlockSource, BlockSourceError,
5	BlockSourceResult,
6};
7
8use bitcoin::hash_types::BlockHash;
9use bitcoin::network::Network;
10use lightning::chain::BestBlock;
11
12use std::ops::Deref;
13
14/// The `Poll` trait defines behavior for polling block sources for a chain tip and retrieving
15/// related chain data. It serves as an adapter for `BlockSource`.
16///
17/// [`ChainPoller`] adapts a single `BlockSource`, while any other implementations of `Poll` are
18/// required to be built in terms of it to ensure chain data validity.
19///
20/// [`ChainPoller`]: ../struct.ChainPoller.html
21pub trait Poll {
22	/// Returns a chain tip in terms of its relationship to the provided chain tip.
23	fn poll_chain_tip<'a>(
24		&'a self, best_known_chain_tip: ValidatedBlockHeader,
25	) -> AsyncBlockSourceResult<'a, ChainTip>;
26
27	/// Returns the header that preceded the given header in the chain.
28	fn look_up_previous_header<'a>(
29		&'a self, header: &'a ValidatedBlockHeader,
30	) -> AsyncBlockSourceResult<'a, ValidatedBlockHeader>;
31
32	/// Returns the block associated with the given header.
33	fn fetch_block<'a>(
34		&'a self, header: &'a ValidatedBlockHeader,
35	) -> AsyncBlockSourceResult<'a, ValidatedBlock>;
36}
37
38/// A chain tip relative to another chain tip in terms of block hash and chainwork.
39#[derive(Clone, Debug, PartialEq, Eq)]
40pub enum ChainTip {
41	/// A chain tip with the same hash as another chain's tip.
42	Common,
43
44	/// A chain tip with more chainwork than another chain's tip.
45	Better(ValidatedBlockHeader),
46
47	/// A chain tip with less or equal chainwork than another chain's tip. In either case, the
48	/// hashes of each tip will be different.
49	Worse(ValidatedBlockHeader),
50}
51
52/// The `Validate` trait defines behavior for validating chain data.
53///
54/// This trait is sealed and not meant to be implemented outside of this crate.
55pub trait Validate: sealed::Validate {
56	/// The validated data wrapper which can be dereferenced to obtain the validated data.
57	type T: std::ops::Deref<Target = Self>;
58
59	/// Validates the chain data against the given block hash and any criteria needed to ensure that
60	/// it is internally consistent.
61	fn validate(self, block_hash: BlockHash) -> BlockSourceResult<Self::T>;
62}
63
64impl Validate for BlockHeaderData {
65	type T = ValidatedBlockHeader;
66
67	fn validate(self, block_hash: BlockHash) -> BlockSourceResult<Self::T> {
68		let pow_valid_block_hash =
69			self.header.validate_pow(self.header.target()).map_err(BlockSourceError::persistent)?;
70
71		if pow_valid_block_hash != block_hash {
72			return Err(BlockSourceError::persistent("invalid block hash"));
73		}
74
75		Ok(ValidatedBlockHeader { block_hash, inner: self })
76	}
77}
78
79impl Validate for BlockData {
80	type T = ValidatedBlock;
81
82	fn validate(self, block_hash: BlockHash) -> BlockSourceResult<Self::T> {
83		let header = match &self {
84			BlockData::FullBlock(block) => &block.header,
85			BlockData::HeaderOnly(header) => header,
86		};
87
88		let pow_valid_block_hash =
89			header.validate_pow(header.target()).map_err(BlockSourceError::persistent)?;
90
91		if pow_valid_block_hash != block_hash {
92			return Err(BlockSourceError::persistent("invalid block hash"));
93		}
94
95		if let BlockData::FullBlock(block) = &self {
96			if !block.check_merkle_root() {
97				return Err(BlockSourceError::persistent("invalid merkle root"));
98			}
99
100			if !block.check_witness_commitment() {
101				return Err(BlockSourceError::persistent("invalid witness commitment"));
102			}
103		}
104
105		Ok(ValidatedBlock { block_hash, inner: self })
106	}
107}
108
109/// A block header with validated proof of work and corresponding block hash.
110#[derive(Clone, Copy, Debug, PartialEq, Eq)]
111pub struct ValidatedBlockHeader {
112	pub(crate) block_hash: BlockHash,
113	inner: BlockHeaderData,
114}
115
116impl std::ops::Deref for ValidatedBlockHeader {
117	type Target = BlockHeaderData;
118
119	fn deref(&self) -> &Self::Target {
120		&self.inner
121	}
122}
123
124impl ValidatedBlockHeader {
125	/// Checks that the header correctly builds on previous_header: the claimed work differential
126	/// matches the actual PoW and the difficulty transition is possible, i.e., within 4x.
127	fn check_builds_on(
128		&self, previous_header: &ValidatedBlockHeader, network: Network,
129	) -> BlockSourceResult<()> {
130		if self.header.prev_blockhash != previous_header.block_hash {
131			return Err(BlockSourceError::persistent("invalid previous block hash"));
132		}
133
134		if self.height != previous_header.height + 1 {
135			return Err(BlockSourceError::persistent("invalid block height"));
136		}
137
138		let work = self.header.work();
139		if self.chainwork != previous_header.chainwork + work {
140			return Err(BlockSourceError::persistent("invalid chainwork"));
141		}
142
143		if let Network::Bitcoin = network {
144			if self.height % 2016 == 0 {
145				let target = self.header.target();
146				let previous_target = previous_header.header.target();
147				let min_target = previous_target.min_transition_threshold();
148				let max_target = previous_target.max_transition_threshold_unchecked();
149				if target > max_target || target < min_target {
150					return Err(BlockSourceError::persistent("invalid difficulty transition"));
151				}
152			} else if self.header.bits != previous_header.header.bits {
153				return Err(BlockSourceError::persistent("invalid difficulty"));
154			}
155		}
156
157		Ok(())
158	}
159
160	/// Returns the [`BestBlock`] corresponding to this validated block header, which can be passed
161	/// into [`ChannelManager::new`] as part of its [`ChainParameters`]. Useful for ensuring that
162	/// the [`SpvClient`] and [`ChannelManager`] are initialized to the same block during a fresh
163	/// start.
164	///
165	/// [`SpvClient`]: crate::SpvClient
166	/// [`ChainParameters`]: lightning::ln::channelmanager::ChainParameters
167	/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
168	/// [`ChannelManager::new`]: lightning::ln::channelmanager::ChannelManager::new
169	pub fn to_best_block(&self) -> BestBlock {
170		BestBlock::new(self.block_hash, self.inner.height)
171	}
172}
173
174/// A block with validated data against its transaction list and corresponding block hash.
175pub struct ValidatedBlock {
176	pub(crate) block_hash: BlockHash,
177	inner: BlockData,
178}
179
180impl std::ops::Deref for ValidatedBlock {
181	type Target = BlockData;
182
183	fn deref(&self) -> &Self::Target {
184		&self.inner
185	}
186}
187
188mod sealed {
189	/// Used to prevent implementing [`super::Validate`] outside the crate but still allow its use.
190	pub trait Validate {}
191
192	impl Validate for crate::BlockHeaderData {}
193	impl Validate for crate::BlockData {}
194}
195
196/// The canonical `Poll` implementation used for a single `BlockSource`.
197///
198/// Other `Poll` implementations should be built using `ChainPoller` as it provides the simplest way
199/// of validating chain data and checking consistency.
200pub struct ChainPoller<B: Deref<Target = T> + Sized + Send + Sync, T: BlockSource + ?Sized> {
201	block_source: B,
202	network: Network,
203}
204
205impl<B: Deref<Target = T> + Sized + Send + Sync, T: BlockSource + ?Sized> ChainPoller<B, T> {
206	/// Creates a new poller for the given block source.
207	///
208	/// If the `network` parameter is mainnet, then the difficulty between blocks is checked for
209	/// validity.
210	pub fn new(block_source: B, network: Network) -> Self {
211		Self { block_source, network }
212	}
213}
214
215impl<B: Deref<Target = T> + Sized + Send + Sync, T: BlockSource + ?Sized> Poll
216	for ChainPoller<B, T>
217{
218	fn poll_chain_tip<'a>(
219		&'a self, best_known_chain_tip: ValidatedBlockHeader,
220	) -> AsyncBlockSourceResult<'a, ChainTip> {
221		Box::pin(async move {
222			let (block_hash, height) = self.block_source.get_best_block().await?;
223			if block_hash == best_known_chain_tip.header.block_hash() {
224				return Ok(ChainTip::Common);
225			}
226
227			let chain_tip =
228				self.block_source.get_header(&block_hash, height).await?.validate(block_hash)?;
229			if chain_tip.chainwork > best_known_chain_tip.chainwork {
230				Ok(ChainTip::Better(chain_tip))
231			} else {
232				Ok(ChainTip::Worse(chain_tip))
233			}
234		})
235	}
236
237	fn look_up_previous_header<'a>(
238		&'a self, header: &'a ValidatedBlockHeader,
239	) -> AsyncBlockSourceResult<'a, ValidatedBlockHeader> {
240		Box::pin(async move {
241			if header.height == 0 {
242				return Err(BlockSourceError::persistent("genesis block reached"));
243			}
244
245			let previous_hash = &header.header.prev_blockhash;
246			let height = header.height - 1;
247			let previous_header = self
248				.block_source
249				.get_header(previous_hash, Some(height))
250				.await?
251				.validate(*previous_hash)?;
252			header.check_builds_on(&previous_header, self.network)?;
253
254			Ok(previous_header)
255		})
256	}
257
258	fn fetch_block<'a>(
259		&'a self, header: &'a ValidatedBlockHeader,
260	) -> AsyncBlockSourceResult<'a, ValidatedBlock> {
261		Box::pin(async move {
262			self.block_source.get_block(&header.block_hash).await?.validate(header.block_hash)
263		})
264	}
265}
266
267#[cfg(test)]
268mod tests {
269	use super::*;
270	use crate::test_utils::Blockchain;
271	use crate::*;
272
273	#[tokio::test]
274	async fn poll_empty_chain() {
275		let mut chain = Blockchain::default().with_height(0);
276		let best_known_chain_tip = chain.tip();
277		chain.disconnect_tip();
278
279		let poller = ChainPoller::new(&chain, Network::Bitcoin);
280		match poller.poll_chain_tip(best_known_chain_tip).await {
281			Err(e) => {
282				assert_eq!(e.kind(), BlockSourceErrorKind::Transient);
283				assert_eq!(e.into_inner().as_ref().to_string(), "empty chain");
284			},
285			Ok(_) => panic!("Expected error"),
286		}
287	}
288
289	#[tokio::test]
290	async fn poll_chain_without_headers() {
291		let chain = Blockchain::default().with_height(1).without_headers();
292		let best_known_chain_tip = chain.at_height(0);
293
294		let poller = ChainPoller::new(&chain, Network::Bitcoin);
295		match poller.poll_chain_tip(best_known_chain_tip).await {
296			Err(e) => {
297				assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
298				assert_eq!(e.into_inner().as_ref().to_string(), "header not found");
299			},
300			Ok(_) => panic!("Expected error"),
301		}
302	}
303
304	#[tokio::test]
305	async fn poll_chain_with_invalid_pow() {
306		let mut chain = Blockchain::default().with_height(1);
307		let best_known_chain_tip = chain.at_height(0);
308
309		// Invalidate the tip by changing its target.
310		chain.blocks.last_mut().unwrap().header.bits =
311			bitcoin::Target::from_be_bytes([0x01; 32]).to_compact_lossy();
312
313		let poller = ChainPoller::new(&chain, Network::Bitcoin);
314		match poller.poll_chain_tip(best_known_chain_tip).await {
315			Err(e) => {
316				assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
317				assert_eq!(
318					e.into_inner().as_ref().to_string(),
319					"block target correct but not attained"
320				);
321			},
322			Ok(_) => panic!("Expected error"),
323		}
324	}
325
326	#[tokio::test]
327	async fn poll_chain_with_malformed_headers() {
328		let chain = Blockchain::default().with_height(1).malformed_headers();
329		let best_known_chain_tip = chain.at_height(0);
330
331		let poller = ChainPoller::new(&chain, Network::Bitcoin);
332		match poller.poll_chain_tip(best_known_chain_tip).await {
333			Err(e) => {
334				assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
335				assert_eq!(e.into_inner().as_ref().to_string(), "invalid block hash");
336			},
337			Ok(_) => panic!("Expected error"),
338		}
339	}
340
341	#[tokio::test]
342	async fn poll_chain_with_common_tip() {
343		let chain = Blockchain::default().with_height(0);
344		let best_known_chain_tip = chain.tip();
345
346		let poller = ChainPoller::new(&chain, Network::Bitcoin);
347		match poller.poll_chain_tip(best_known_chain_tip).await {
348			Err(e) => panic!("Unexpected error: {:?}", e),
349			Ok(tip) => assert_eq!(tip, ChainTip::Common),
350		}
351	}
352
353	#[tokio::test]
354	async fn poll_chain_with_uncommon_tip_but_equal_chainwork() {
355		let mut chain = Blockchain::default().with_height(1);
356		let best_known_chain_tip = chain.tip();
357
358		// Change the nonce to get a different block hash with the same chainwork.
359		chain.blocks.last_mut().unwrap().header.nonce += 1;
360		let worse_chain_tip = chain.tip();
361		assert_eq!(best_known_chain_tip.chainwork, worse_chain_tip.chainwork);
362
363		let poller = ChainPoller::new(&chain, Network::Bitcoin);
364		match poller.poll_chain_tip(best_known_chain_tip).await {
365			Err(e) => panic!("Unexpected error: {:?}", e),
366			Ok(tip) => assert_eq!(tip, ChainTip::Worse(worse_chain_tip)),
367		}
368	}
369
370	#[tokio::test]
371	async fn poll_chain_with_worse_tip() {
372		let mut chain = Blockchain::default().with_height(1);
373		let best_known_chain_tip = chain.tip();
374
375		chain.disconnect_tip();
376		let worse_chain_tip = chain.tip();
377
378		let poller = ChainPoller::new(&chain, Network::Bitcoin);
379		match poller.poll_chain_tip(best_known_chain_tip).await {
380			Err(e) => panic!("Unexpected error: {:?}", e),
381			Ok(tip) => assert_eq!(tip, ChainTip::Worse(worse_chain_tip)),
382		}
383	}
384
385	#[tokio::test]
386	async fn poll_chain_with_better_tip() {
387		let chain = Blockchain::default().with_height(1);
388		let best_known_chain_tip = chain.at_height(0);
389
390		let better_chain_tip = chain.tip();
391
392		let poller = ChainPoller::new(&chain, Network::Bitcoin);
393		match poller.poll_chain_tip(best_known_chain_tip).await {
394			Err(e) => panic!("Unexpected error: {:?}", e),
395			Ok(tip) => assert_eq!(tip, ChainTip::Better(better_chain_tip)),
396		}
397	}
398}