Skip to content

Commit 6ed67f1

Browse files
committed
Add test coverage for TransactionType::Splice
Add parallel `txn_types` vector to `TestBroadcaster` to track `TransactionType` alongside broadcast transactions. Existing `txn_broadcast()` API remains unchanged for backward compatibility. New `txn_broadcast_with_types()` API allows tests to verify transaction types. Also add a `clear()` helper method and update test files to use it instead of directly manipulating `txn_broadcasted`, ensuring the two vectors stay in sync. Update splice tests to use the new API and verify that splice transactions are broadcast with the correct `TransactionType`. Co-Authored-By: HAL 9000 Signed-off-by: Elias Rohrer <dev@tnull.de>
1 parent ff90d20 commit 6ed67f1

File tree

8 files changed

+51
-22
lines changed

8 files changed

+51
-22
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ fn test_monitor_and_persister_update_fail() {
8686
let persister = test_utils::TestPersister::new();
8787
let tx_broadcaster = TestBroadcaster {
8888
txn_broadcasted: Mutex::new(Vec::new()),
89+
txn_types: Mutex::new(Vec::new()),
8990
// Because we will connect a block at height 200 below, we need the TestBroadcaster to know
9091
// that we are at height 200 so that it doesn't think we're violating the time lock
9192
// requirements of transactions broadcasted at that point.

lightning/src/ln/channel_open_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,7 @@ pub fn test_invalid_funding_tx() {
16991699

17001700
assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
17011701
assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0], tx);
1702-
nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
1702+
nodes[0].tx_broadcaster.clear();
17031703

17041704
let expected_err = "funding tx had wrong script/value or output index";
17051705
confirm_transaction_at(&nodes[1], &tx, 1);

lightning/src/ln/functional_test_utils.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
861861
txn_broadcasted: Mutex::new(
862862
self.tx_broadcaster.txn_broadcasted.lock().unwrap().clone(),
863863
),
864+
txn_types: Mutex::new(self.tx_broadcaster.txn_types.lock().unwrap().clone()),
864865
blocks: Arc::new(Mutex::new(self.tx_broadcaster.blocks.lock().unwrap().clone())),
865866
};
866867

@@ -1538,7 +1539,7 @@ pub fn sign_funding_transaction<'a, 'b, 'c>(
15381539

15391540
assert_eq!(node_a.tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
15401541
assert_eq!(node_a.tx_broadcaster.txn_broadcasted.lock().unwrap()[0], tx);
1541-
node_a.tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
1542+
node_a.tx_broadcaster.clear();
15421543

15431544
// Ensure that funding_transaction_generated is idempotent.
15441545
assert!(node_a
@@ -1641,10 +1642,8 @@ pub fn open_zero_conf_channel_with_value<'a, 'b, 'c, 'd>(
16411642
check_added_monitors(&initiator, 1);
16421643

16431644
assert_eq!(initiator.tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
1644-
assert_eq!(
1645-
initiator.tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0)[0],
1646-
tx
1647-
);
1645+
assert_eq!(initiator.tx_broadcaster.txn_broadcasted.lock().unwrap()[0], tx);
1646+
initiator.tx_broadcaster.clear();
16481647

16491648
as_channel_ready =
16501649
get_event_msg!(initiator, MessageSendEvent::SendChannelReady, receiver_node_id);
@@ -2014,7 +2013,7 @@ pub fn create_unannounced_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(
20142013

20152014
assert_eq!(nodes[a].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
20162015
assert_eq!(nodes[a].tx_broadcaster.txn_broadcasted.lock().unwrap()[0], tx);
2017-
nodes[a].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
2016+
nodes[a].tx_broadcaster.clear();
20182017

20192018
let conf_height =
20202019
core::cmp::max(nodes[a].best_block_info().1 + 1, nodes[b].best_block_info().1 + 1);

lightning/src/ln/monitor_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ fn test_no_preimage_inbound_htlc_balances() {
12081208
}, a_received_htlc_balance.clone(), a_sent_htlc_balance.clone()]);
12091209

12101210
mine_transaction(&nodes[0], &as_txn[0]);
1211-
nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
1211+
nodes[0].tx_broadcaster.clear();
12121212
check_closed_broadcast!(nodes[0], true);
12131213
check_added_monitors(&nodes[0], 1);
12141214
check_closed_event(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, &[nodes[1].node.get_our_node_id()], 1000000);
@@ -1255,7 +1255,7 @@ fn test_no_preimage_inbound_htlc_balances() {
12551255
bs_pre_spend_claims.retain(|e| if let Balance::ClaimableAwaitingConfirmations { .. } = e { false } else { true });
12561256

12571257
// The next few blocks for B look the same as for A, though for the opposite HTLC
1258-
nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
1258+
nodes[1].tx_broadcaster.clear();
12591259
connect_blocks(&nodes[1], TEST_FINAL_CLTV - (ANTI_REORG_DELAY - 1));
12601260
expect_htlc_failure_conditions(nodes[1].node.get_and_clear_pending_events(), &[HTLCHandlingFailureType::Receive { payment_hash: to_b_failed_payment_hash }]);
12611261
nodes[1].node.process_pending_htlc_forwards();

lightning/src/ln/payment_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
838838
let as_commitment_tx = get_local_commitment_txn!(nodes[0], chan_id)[0].clone();
839839
if confirm_before_reload {
840840
mine_transaction(&nodes[0], &as_commitment_tx);
841-
nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
841+
nodes[0].tx_broadcaster.clear();
842842
}
843843

844844
// The ChannelMonitor should always be the latest version, as we're required to persist it
@@ -893,7 +893,7 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
893893
&node_b_id)) }, &[node_a_id], 100000);
894894
check_added_monitors(&nodes[1], 1);
895895
assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
896-
nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
896+
nodes[1].tx_broadcaster.clear();
897897
},
898898
_ => panic!("Unexpected event"),
899899
}
@@ -954,7 +954,7 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
954954
} else {
955955
confirm_transaction(&nodes[0], &first_htlc_timeout_tx);
956956
}
957-
nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
957+
nodes[0].tx_broadcaster.clear();
958958
let conditions = PaymentFailedConditions::new().from_mon_update();
959959
expect_payment_failed_conditions(&nodes[0], payment_hash, false, conditions);
960960

lightning/src/ln/shutdown_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ fn updates_shutdown_wait() {
495495
assert!(nodes[0].node.list_channels().is_empty());
496496

497497
assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
498-
nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
498+
nodes[1].tx_broadcaster.clear();
499499
close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
500500

501501
assert!(nodes[1].node.list_channels().is_empty());
@@ -625,7 +625,7 @@ fn do_htlc_fail_async_shutdown(blinded_recipient: bool) {
625625
assert!(nodes[0].node.list_channels().is_empty());
626626

627627
assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
628-
nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
628+
nodes[1].tx_broadcaster.clear();
629629
close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
630630
assert!(nodes[1].node.list_channels().is_empty());
631631
assert!(nodes[2].node.list_channels().is_empty());
@@ -842,7 +842,7 @@ fn do_test_shutdown_rebroadcast(recv_count: u8) {
842842
assert!(nodes[0].node.list_channels().is_empty());
843843

844844
assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
845-
nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
845+
nodes[1].tx_broadcaster.clear();
846846
close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
847847

848848
assert!(nodes[1].node.list_channels().is_empty());

lightning/src/ln/splicing_tests.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#![cfg_attr(not(test), allow(unused_imports))]
1111

12-
use crate::chain::chaininterface::FEERATE_FLOOR_SATS_PER_KW;
12+
use crate::chain::chaininterface::{TransactionType, FEERATE_FLOOR_SATS_PER_KW};
1313
use crate::chain::channelmonitor::{ANTI_REORG_DELAY, LATENCY_GRACE_PERIOD_BLOCKS};
1414
use crate::chain::transaction::OutPoint;
1515
use crate::chain::ChannelMonitorUpdateStatus;
@@ -333,11 +333,18 @@ pub fn sign_interactive_funding_tx<'a, 'b, 'c, 'd>(
333333
check_added_monitors(&acceptor, 1);
334334

335335
let tx = {
336-
let mut initiator_txn = initiator.tx_broadcaster.txn_broadcast();
336+
let mut initiator_txn = initiator.tx_broadcaster.txn_broadcast_with_types();
337337
assert_eq!(initiator_txn.len(), 1);
338-
let acceptor_txn = acceptor.tx_broadcaster.txn_broadcast();
339-
assert_eq!(initiator_txn, acceptor_txn,);
340-
initiator_txn.remove(0)
338+
let acceptor_txn = acceptor.tx_broadcaster.txn_broadcast_with_types();
339+
assert_eq!(initiator_txn, acceptor_txn);
340+
let (tx, tx_type) = initiator_txn.remove(0);
341+
// Verify transaction type is Splice
342+
assert!(
343+
matches!(tx_type, TransactionType::Splice { .. }),
344+
"Expected TransactionType::Splice, got {:?}",
345+
tx_type
346+
);
347+
tx
341348
};
342349
(tx, splice_locked)
343350
}

lightning/src/util/test_utils.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,31 +1126,50 @@ unsafe impl Send for TestStore {}
11261126

11271127
pub struct TestBroadcaster {
11281128
pub txn_broadcasted: Mutex<Vec<Transaction>>,
1129+
pub txn_types: Mutex<Vec<TransactionType>>,
11291130
pub blocks: Arc<Mutex<Vec<(Block, u32)>>>,
11301131
}
11311132

11321133
impl TestBroadcaster {
11331134
pub fn new(network: Network) -> Self {
11341135
let txn_broadcasted = Mutex::new(Vec::new());
1136+
let txn_types = Mutex::new(Vec::new());
11351137
let blocks = Arc::new(Mutex::new(vec![(genesis_block(network), 0)]));
1136-
Self { txn_broadcasted, blocks }
1138+
Self { txn_broadcasted, txn_types, blocks }
11371139
}
11381140

11391141
pub fn with_blocks(blocks: Arc<Mutex<Vec<(Block, u32)>>>) -> Self {
11401142
let txn_broadcasted = Mutex::new(Vec::new());
1141-
Self { txn_broadcasted, blocks }
1143+
let txn_types = Mutex::new(Vec::new());
1144+
Self { txn_broadcasted, txn_types, blocks }
11421145
}
11431146

11441147
pub fn txn_broadcast(&self) -> Vec<Transaction> {
1148+
self.txn_types.lock().unwrap().clear();
11451149
self.txn_broadcasted.lock().unwrap().split_off(0)
11461150
}
11471151

11481152
pub fn unique_txn_broadcast(&self) -> Vec<Transaction> {
11491153
let mut txn = self.txn_broadcasted.lock().unwrap().split_off(0);
1154+
self.txn_types.lock().unwrap().clear();
11501155
let mut seen = new_hash_set();
11511156
txn.retain(|tx| seen.insert(tx.compute_txid()));
11521157
txn
11531158
}
1159+
1160+
/// Returns all broadcast transactions with their types, clearing both internal lists.
1161+
pub fn txn_broadcast_with_types(&self) -> Vec<(Transaction, TransactionType)> {
1162+
let txn = self.txn_broadcasted.lock().unwrap().split_off(0);
1163+
let types = self.txn_types.lock().unwrap().split_off(0);
1164+
assert_eq!(txn.len(), types.len(), "Transaction and type vectors out of sync");
1165+
txn.into_iter().zip(types.into_iter()).collect()
1166+
}
1167+
1168+
/// Clears both the transaction and type vectors.
1169+
pub fn clear(&self) {
1170+
self.txn_broadcasted.lock().unwrap().clear();
1171+
self.txn_types.lock().unwrap().clear();
1172+
}
11541173
}
11551174

11561175
impl chaininterface::BroadcasterInterface for TestBroadcaster {
@@ -1198,7 +1217,10 @@ impl chaininterface::BroadcasterInterface for TestBroadcaster {
11981217
}
11991218
}
12001219
let owned_txs: Vec<Transaction> = txs.iter().map(|(tx, _)| (*tx).clone()).collect();
1220+
let owned_types: Vec<TransactionType> =
1221+
txs.iter().map(|(_, tx_type)| tx_type.clone()).collect();
12011222
self.txn_broadcasted.lock().unwrap().extend(owned_txs);
1223+
self.txn_types.lock().unwrap().extend(owned_types);
12021224
}
12031225
}
12041226

0 commit comments

Comments
 (0)