Skip to content

Commit 42f9aee

Browse files
committed
f add test util
1 parent c3aabce commit 42f9aee

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

lightning/src/ln/functional_test_utils.rs

+25-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::chain::transaction::OutPoint;
1717
use crate::events::{ClaimedHTLC, ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, PaymentFailureReason};
1818
use crate::events::bump_transaction::{BumpTransactionEventHandler, Wallet, WalletSource};
1919
use crate::ln::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret};
20-
use crate::ln::channelmanager::{self, AChannelManager, ChainParameters, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure, RecipientOnionFields, PaymentId, MIN_CLTV_EXPIRY_DELTA};
20+
use crate::ln::channelmanager::{AChannelManager, ChainParameters, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure, RecipientOnionFields, PaymentId, MIN_CLTV_EXPIRY_DELTA};
2121
use crate::routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate};
2222
use crate::routing::router::{self, PaymentParameters, Route};
2323
use crate::ln::features::InitFeatures;
@@ -73,6 +73,20 @@ pub fn mine_transactions<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, txn: &[&Tra
7373
let height = node.best_block_info().1 + 1;
7474
confirm_transactions_at(node, txn, height);
7575
}
76+
/// Mine a single block containing the given transaction without extra consistency checks which may
77+
/// impact ChannelManager state.
78+
pub fn mine_transaction_without_consistency_checks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, tx: &Transaction) {
79+
let height = node.best_block_info().1 + 1;
80+
let mut block = Block {
81+
header: BlockHeader { version: 0x20000000, prev_blockhash: node.best_block_hash(), merkle_root: TxMerkleNode::all_zeros(), time: height, bits: 42, nonce: 42 },
82+
txdata: Vec::new(),
83+
};
84+
for _ in 0..*node.network_chan_count.borrow() { // Make sure we don't end up with channels at the same short id by offsetting by chan_count
85+
block.txdata.push(Transaction { version: 0, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: Vec::new() });
86+
}
87+
block.txdata.push((*tx).clone());
88+
do_connect_block_without_consistency_checks(node, block, false);
89+
}
7690
/// Mine the given transaction at the given height, mining blocks as required to build to that
7791
/// height
7892
///
@@ -211,16 +225,16 @@ pub fn connect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, depth: u32) ->
211225
assert!(depth >= 1);
212226
for i in 1..depth {
213227
let prev_blockhash = block.header.block_hash();
214-
do_connect_block(node, block, skip_intermediaries);
228+
do_connect_block_with_consistency_checks(node, block, skip_intermediaries);
215229
block = create_dummy_block(prev_blockhash, height + i, Vec::new());
216230
}
217231
let hash = block.header.block_hash();
218-
do_connect_block(node, block, false);
232+
do_connect_block_with_consistency_checks(node, block, false);
219233
hash
220234
}
221235

222236
pub fn connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: &Block) {
223-
do_connect_block(node, block.clone(), false);
237+
do_connect_block_with_consistency_checks(node, block.clone(), false);
224238
}
225239

226240
fn call_claimable_balances<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>) {
@@ -230,8 +244,14 @@ fn call_claimable_balances<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>) {
230244
}
231245
}
232246

233-
fn do_connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: Block, skip_intermediaries: bool) {
247+
fn do_connect_block_with_consistency_checks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: Block, skip_intermediaries: bool) {
248+
call_claimable_balances(node);
249+
do_connect_block_without_consistency_checks(node, block, skip_intermediaries);
234250
call_claimable_balances(node);
251+
node.node.test_process_background_events();
252+
}
253+
254+
fn do_connect_block_without_consistency_checks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: Block, skip_intermediaries: bool) {
235255
let height = node.best_block_info().1 + 1;
236256
#[cfg(feature = "std")] {
237257
eprintln!("Connecting block using Block Connection Style: {:?}", *node.connect_style.borrow());
@@ -286,8 +306,6 @@ fn do_connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: Block, sk
286306
}
287307
}
288308
}
289-
call_claimable_balances(node);
290-
node.node.test_process_background_events();
291309

292310
for tx in &block.txdata {
293311
for input in &tx.input {

0 commit comments

Comments
 (0)