Add block_disconnecting tests to cancel HTLC failure updates
[rust-lightning] / src / ln / functional_test_utils.rs
index b8f5e5835a359f5a30bc9a821685f5bd5be26577..15c372ddeb5e190144591299962e152a98cb319b 100644 (file)
@@ -15,7 +15,7 @@ use util::logger::Logger;
 use util::config::UserConfig;
 
 use bitcoin::util::hash::BitcoinHash;
-use bitcoin::blockdata::block::BlockHeader;
+use bitcoin::blockdata::block::{BlockHeader, Block};
 use bitcoin::blockdata::transaction::{Transaction, TxOut};
 use bitcoin::network::constants::Network;
 
@@ -47,13 +47,28 @@ pub fn confirm_transaction(chain: &chaininterface::ChainWatchInterfaceUtil, tx:
        }
 }
 
-pub fn connect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) {
+pub fn connect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) -> Sha256d {
        let mut header = BlockHeader { version: 0x2000000, prev_blockhash: if parent { prev_blockhash } else { Default::default() }, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
        chain.block_connected_checked(&header, height + 1, &Vec::new(), &Vec::new());
        for i in 2..depth + 1 {
                header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
                chain.block_connected_checked(&header, height + i, &Vec::new(), &Vec::new());
        }
+       header.bitcoin_hash()
+}
+
+pub fn disconnect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) {
+       let mut header = BlockHeader { version: 0x2000000, prev_blockhash: if parent { prev_blockhash } else { Default::default() }, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       let mut blocks = Vec::new();
+       for _ in 0..depth {
+               blocks.push(Block { header, txdata: Vec::new() });
+               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+       }
+       let mut height = height;
+       for block in blocks.pop() {
+               chain.block_disconnected(&block.header, height);
+               height -= 1;
+       }
 }
 
 pub struct Node {