Skip to content

Commit 3e8a87b

Browse files
jkczyzclaude
andcommitted
Refactor complete_interactive_funding_negotiation_for_both
Use a single get_and_clear_pending_msg_events() + match pattern for the initiator's turn, matching the existing acceptor code path. Also add assertions that all expected initiator inputs and outputs were sent. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 042798b commit 3e8a87b

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

lightning/src/ln/splicing_tests.rs

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -374,53 +374,52 @@ pub fn complete_interactive_funding_negotiation_for_both<'a, 'b, 'c, 'd>(
374374
(Vec::new(), Vec::new())
375375
};
376376

377-
let mut acceptor_sent_tx_complete = false;
378377
let mut initiator_sent_tx_complete;
378+
let mut acceptor_sent_tx_complete = false;
379379
loop {
380380
// Initiator's turn: send TxAddInput, TxAddOutput, or TxComplete
381-
if !expected_initiator_inputs.is_empty() {
382-
let tx_add_input =
383-
get_event_msg!(initiator, MessageSendEvent::SendTxAddInput, node_id_acceptor);
384-
let input_prevout = BitcoinOutPoint {
385-
txid: tx_add_input
386-
.prevtx
387-
.as_ref()
388-
.map(|prevtx| prevtx.compute_txid())
389-
.or(tx_add_input.shared_input_txid)
390-
.unwrap(),
391-
vout: tx_add_input.prevtx_out,
392-
};
393-
expected_initiator_inputs.remove(
394-
expected_initiator_inputs.iter().position(|input| *input == input_prevout).unwrap(),
395-
);
396-
acceptor.node.handle_tx_add_input(node_id_initiator, &tx_add_input);
397-
initiator_sent_tx_complete = false;
398-
} else if !expected_initiator_outputs.is_empty() {
399-
let tx_add_output =
400-
get_event_msg!(initiator, MessageSendEvent::SendTxAddOutput, node_id_acceptor);
401-
expected_initiator_outputs.remove(
402-
expected_initiator_outputs
403-
.iter()
404-
.position(|output| {
405-
*output.script_pubkey == tx_add_output.script
406-
&& output.value.to_sat() == tx_add_output.sats
407-
})
408-
.unwrap(),
409-
);
410-
acceptor.node.handle_tx_add_output(node_id_initiator, &tx_add_output);
411-
initiator_sent_tx_complete = false;
412-
} else {
413-
let msg_events = initiator.node.get_and_clear_pending_msg_events();
414-
assert_eq!(msg_events.len(), 1, "{msg_events:?}");
415-
if let MessageSendEvent::SendTxComplete { ref msg, .. } = &msg_events[0] {
381+
let msg_events = initiator.node.get_and_clear_pending_msg_events();
382+
assert_eq!(msg_events.len(), 1, "{msg_events:?}");
383+
match &msg_events[0] {
384+
MessageSendEvent::SendTxAddInput { msg, .. } => {
385+
let input_prevout = BitcoinOutPoint {
386+
txid: msg
387+
.prevtx
388+
.as_ref()
389+
.map(|prevtx| prevtx.compute_txid())
390+
.or(msg.shared_input_txid)
391+
.unwrap(),
392+
vout: msg.prevtx_out,
393+
};
394+
expected_initiator_inputs.remove(
395+
expected_initiator_inputs
396+
.iter()
397+
.position(|input| *input == input_prevout)
398+
.unwrap(),
399+
);
400+
acceptor.node.handle_tx_add_input(node_id_initiator, msg);
401+
initiator_sent_tx_complete = false;
402+
},
403+
MessageSendEvent::SendTxAddOutput { msg, .. } => {
404+
expected_initiator_outputs.remove(
405+
expected_initiator_outputs
406+
.iter()
407+
.position(|output| {
408+
*output.script_pubkey == msg.script && output.value.to_sat() == msg.sats
409+
})
410+
.unwrap(),
411+
);
412+
acceptor.node.handle_tx_add_output(node_id_initiator, msg);
413+
initiator_sent_tx_complete = false;
414+
},
415+
MessageSendEvent::SendTxComplete { msg, .. } => {
416416
acceptor.node.handle_tx_complete(node_id_initiator, msg);
417-
} else {
418-
panic!();
419-
}
420-
initiator_sent_tx_complete = true;
421-
if acceptor_sent_tx_complete {
422-
break;
423-
}
417+
initiator_sent_tx_complete = true;
418+
if acceptor_sent_tx_complete {
419+
break;
420+
}
421+
},
422+
_ => panic!("Unexpected message event: {:?}", msg_events[0]),
424423
}
425424

426425
// Acceptor's turn: send TxAddInput, TxAddOutput, or TxComplete
@@ -467,6 +466,8 @@ pub fn complete_interactive_funding_negotiation_for_both<'a, 'b, 'c, 'd>(
467466
}
468467
}
469468

469+
assert!(expected_initiator_inputs.is_empty(), "Not all initiator inputs were sent");
470+
assert!(expected_initiator_outputs.is_empty(), "Not all initiator outputs were sent");
470471
assert!(expected_acceptor_inputs.is_empty(), "Not all acceptor inputs were sent");
471472
assert!(expected_acceptor_scripts.is_empty(), "Not all acceptor outputs were sent");
472473
}

0 commit comments

Comments
 (0)