Merge pull request #2780 from wpaulino/2691-follow-ups
[rust-lightning] / lightning-block-sync / src / test_utils.rs
index fe57c0c606d02d5cfbc5bf79f3bba1637904711f..b6fa6617c846e2d57f5288b0a6ecab975425081a 100644 (file)
@@ -1,11 +1,12 @@
-use crate::{AsyncBlockSourceResult, BlockHeaderData, BlockSource, BlockSourceError, UnboundedCache};
+use crate::{AsyncBlockSourceResult, BlockData, BlockHeaderData, BlockSource, BlockSourceError, UnboundedCache};
 use crate::poll::{Validate, ValidatedBlockHeader};
 
-use bitcoin::blockdata::block::{Block, BlockHeader};
+use bitcoin::blockdata::block::{Block, Header, Version};
 use bitcoin::blockdata::constants::genesis_block;
-use bitcoin::hash_types::BlockHash;
+use bitcoin::blockdata::locktime::absolute::LockTime;
+use bitcoin::hash_types::{BlockHash, TxMerkleNode};
 use bitcoin::network::constants::Network;
-use bitcoin::util::uint::Uint256;
+use bitcoin::Transaction;
 
 use lightning::chain;
 
@@ -18,6 +19,7 @@ pub struct Blockchain {
        without_blocks: Option<std::ops::RangeFrom<usize>>,
        without_headers: bool,
        malformed_headers: bool,
+       filtered_blocks: bool,
 }
 
 impl Blockchain {
@@ -32,21 +34,32 @@ impl Blockchain {
 
        pub fn with_height(mut self, height: usize) -> Self {
                self.blocks.reserve_exact(height);
-               let bits = BlockHeader::compact_target_from_u256(&Uint256::from_be_bytes([0xff; 32]));
+               let bits = bitcoin::Target::from_be_bytes([0xff; 32]).to_compact_lossy();
                for i in 1..=height {
                        let prev_block = &self.blocks[i - 1];
                        let prev_blockhash = prev_block.block_hash();
                        let time = prev_block.header.time + height as u32;
+                       // Must have at least one transaction, because the merkle root is not defined for an empty block
+                       // and we would fail when we later checked, as of bitcoin crate 0.28.0.
+                       // Note that elsewhere in tests we assume that the merkle root of an empty block is all zeros,
+                       // but that's OK because those tests don't trigger the check.
+                       let coinbase = Transaction {
+                               version: 0,
+                               lock_time: LockTime::ZERO,
+                               input: vec![],
+                               output: vec![]
+                       };
+                       let merkle_root = TxMerkleNode::from_raw_hash(coinbase.txid().to_raw_hash());
                        self.blocks.push(Block {
-                               header: BlockHeader {
-                                       version: 0,
+                               header: Header {
+                                       version: Version::NO_SOFT_FORK_SIGNALLING,
                                        prev_blockhash,
-                                       merkle_root: Default::default(),
+                                       merkle_root,
                                        time,
                                        bits,
                                        nonce: 0,
                                },
-                               txdata: vec![],
+                               txdata: vec![coinbase],
                        });
                }
                self
@@ -64,6 +77,10 @@ impl Blockchain {
                Self { malformed_headers: true, ..self }
        }
 
+       pub fn filtered_blocks(self) -> Self {
+               Self { filtered_blocks: true, ..self }
+       }
+
        pub fn fork_at_height(&self, height: usize) -> Self {
                assert!(height + 1 < self.blocks.len());
                let mut blocks = self.blocks.clone();
@@ -85,10 +102,14 @@ impl Blockchain {
        fn at_height_unvalidated(&self, height: usize) -> BlockHeaderData {
                assert!(!self.blocks.is_empty());
                assert!(height < self.blocks.len());
+               let mut total_work = self.blocks[0].header.work();
+               for i in 1..=height {
+                       total_work = total_work + self.blocks[i].header.work();
+               }
                BlockHeaderData {
-                       chainwork: self.blocks[0].header.work() + Uint256::from_u64(height as u64).unwrap(),
+                       chainwork: total_work,
                        height: height as u32,
-                       header: self.blocks[height].header.clone(),
+                       header: self.blocks[height].header,
                }
        }
 
@@ -133,7 +154,7 @@ impl BlockSource for Blockchain {
                })
        }
 
-       fn get_block<'a>(&'a self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block> {
+       fn get_block<'a>(&'a self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, BlockData> {
                Box::pin(async move {
                        for (height, block) in self.blocks.iter().enumerate() {
                                if block.header.block_hash() == *header_hash {
@@ -143,7 +164,11 @@ impl BlockSource for Blockchain {
                                                }
                                        }
 
-                                       return Ok(block.clone());
+                                       if self.filtered_blocks {
+                                               return Ok(BlockData::HeaderOnly(block.header));
+                                       } else {
+                                               return Ok(BlockData::FullBlock(block.clone()));
+                                       }
                                }
                        }
                        Err(BlockSourceError::transient("block not found"))
@@ -166,12 +191,13 @@ impl BlockSource for Blockchain {
 pub struct NullChainListener;
 
 impl chain::Listen for NullChainListener {
-       fn block_connected(&self, _block: &Block, _height: u32) {}
-       fn block_disconnected(&self, _header: &BlockHeader, _height: u32) {}
+       fn filtered_block_connected(&self, _header: &Header, _txdata: &chain::transaction::TransactionData, _height: u32) {}
+       fn block_disconnected(&self, _header: &Header, _height: u32) {}
 }
 
 pub struct MockChainListener {
        expected_blocks_connected: RefCell<VecDeque<BlockHeaderData>>,
+       expected_filtered_blocks_connected: RefCell<VecDeque<BlockHeaderData>>,
        expected_blocks_disconnected: RefCell<VecDeque<BlockHeaderData>>,
 }
 
@@ -179,6 +205,7 @@ impl MockChainListener {
        pub fn new() -> Self {
                Self {
                        expected_blocks_connected: RefCell::new(VecDeque::new()),
+                       expected_filtered_blocks_connected: RefCell::new(VecDeque::new()),
                        expected_blocks_disconnected: RefCell::new(VecDeque::new()),
                }
        }
@@ -188,6 +215,11 @@ impl MockChainListener {
                self
        }
 
+       pub fn expect_filtered_block_connected(self, block: BlockHeaderData) -> Self {
+               self.expected_filtered_blocks_connected.borrow_mut().push_back(block);
+               self
+       }
+
        pub fn expect_block_disconnected(self, block: BlockHeaderData) -> Self {
                self.expected_blocks_disconnected.borrow_mut().push_back(block);
                self
@@ -207,7 +239,19 @@ impl chain::Listen for MockChainListener {
                }
        }
 
-       fn block_disconnected(&self, header: &BlockHeader, height: u32) {
+       fn filtered_block_connected(&self, header: &Header, _txdata: &chain::transaction::TransactionData, height: u32) {
+               match self.expected_filtered_blocks_connected.borrow_mut().pop_front() {
+                       None => {
+                               panic!("Unexpected filtered block connected: {:?}", header.block_hash());
+                       },
+                       Some(expected_block) => {
+                               assert_eq!(header.block_hash(), expected_block.header.block_hash());
+                               assert_eq!(height, expected_block.height);
+                       },
+               }
+       }
+
+       fn block_disconnected(&self, header: &Header, height: u32) {
                match self.expected_blocks_disconnected.borrow_mut().pop_front() {
                        None => {
                                panic!("Unexpected block disconnected: {:?}", header.block_hash());
@@ -231,6 +275,11 @@ impl Drop for MockChainListener {
                        panic!("Expected blocks connected: {:?}", expected_blocks_connected);
                }
 
+               let expected_filtered_blocks_connected = self.expected_filtered_blocks_connected.borrow();
+               if !expected_filtered_blocks_connected.is_empty() {
+                       panic!("Expected filtered_blocks connected: {:?}", expected_filtered_blocks_connected);
+               }
+
                let expected_blocks_disconnected = self.expected_blocks_disconnected.borrow();
                if !expected_blocks_disconnected.is_empty() {
                        panic!("Expected blocks disconnected: {:?}", expected_blocks_disconnected);