Skip to content

Commit 6d75f99

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 6fd11bd commit 6d75f99

1 file changed

Lines changed: 42 additions & 40 deletions

File tree

lightning/src/ln/splicing_tests.rs

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -354,50 +354,50 @@ pub fn complete_interactive_funding_negotiation_for_both<'a, 'b, 'c, 'd>(
354354
(Vec::new(), Vec::new())
355355
};
356356

357-
let mut acceptor_sent_tx_complete = false;
358357
let mut initiator_sent_tx_complete;
358+
let mut acceptor_sent_tx_complete = false;
359359
loop {
360360
// Initiator's turn: send TxAddInput, TxAddOutput, or TxComplete
361-
if !expected_initiator_inputs.is_empty() {
362-
let tx_add_input =
363-
get_event_msg!(initiator, MessageSendEvent::SendTxAddInput, node_id_acceptor);
364-
let input_prevout = BitcoinOutPoint {
365-
txid: tx_add_input
366-
.prevtx
367-
.as_ref()
368-
.map(|prevtx| prevtx.compute_txid())
369-
.or(tx_add_input.shared_input_txid)
370-
.unwrap(),
371-
vout: tx_add_input.prevtx_out,
372-
};
373-
expected_initiator_inputs.remove(
374-
expected_initiator_inputs.iter().position(|input| *input == input_prevout).unwrap(),
375-
);
376-
acceptor.node.handle_tx_add_input(node_id_initiator, &tx_add_input);
377-
initiator_sent_tx_complete = false;
378-
} else if !expected_initiator_scripts.is_empty() {
379-
let tx_add_output =
380-
get_event_msg!(initiator, MessageSendEvent::SendTxAddOutput, node_id_acceptor);
381-
expected_initiator_scripts.remove(
382-
expected_initiator_scripts
383-
.iter()
384-
.position(|script| *script == tx_add_output.script)
385-
.unwrap(),
386-
);
387-
acceptor.node.handle_tx_add_output(node_id_initiator, &tx_add_output);
388-
initiator_sent_tx_complete = false;
389-
} else {
390-
let msg_events = initiator.node.get_and_clear_pending_msg_events();
391-
assert_eq!(msg_events.len(), 1, "{msg_events:?}");
392-
if let MessageSendEvent::SendTxComplete { ref msg, .. } = &msg_events[0] {
361+
let msg_events = initiator.node.get_and_clear_pending_msg_events();
362+
assert_eq!(msg_events.len(), 1, "{msg_events:?}");
363+
match &msg_events[0] {
364+
MessageSendEvent::SendTxAddInput { msg, .. } => {
365+
let input_prevout = BitcoinOutPoint {
366+
txid: msg
367+
.prevtx
368+
.as_ref()
369+
.map(|prevtx| prevtx.compute_txid())
370+
.or(msg.shared_input_txid)
371+
.unwrap(),
372+
vout: msg.prevtx_out,
373+
};
374+
expected_initiator_inputs.remove(
375+
expected_initiator_inputs
376+
.iter()
377+
.position(|input| *input == input_prevout)
378+
.unwrap(),
379+
);
380+
acceptor.node.handle_tx_add_input(node_id_initiator, msg);
381+
initiator_sent_tx_complete = false;
382+
},
383+
MessageSendEvent::SendTxAddOutput { msg, .. } => {
384+
expected_initiator_scripts.remove(
385+
expected_initiator_scripts
386+
.iter()
387+
.position(|script| *script == msg.script)
388+
.unwrap(),
389+
);
390+
acceptor.node.handle_tx_add_output(node_id_initiator, msg);
391+
initiator_sent_tx_complete = false;
392+
},
393+
MessageSendEvent::SendTxComplete { msg, .. } => {
393394
acceptor.node.handle_tx_complete(node_id_initiator, msg);
394-
} else {
395-
panic!();
396-
}
397-
initiator_sent_tx_complete = true;
398-
if acceptor_sent_tx_complete {
399-
break;
400-
}
395+
initiator_sent_tx_complete = true;
396+
if acceptor_sent_tx_complete {
397+
break;
398+
}
399+
},
400+
_ => panic!("Unexpected message event: {:?}", msg_events[0]),
401401
}
402402

403403
// Acceptor's turn: send TxAddInput, TxAddOutput, or TxComplete
@@ -444,6 +444,8 @@ pub fn complete_interactive_funding_negotiation_for_both<'a, 'b, 'c, 'd>(
444444
}
445445
}
446446

447+
assert!(expected_initiator_inputs.is_empty(), "Not all initiator inputs were sent");
448+
assert!(expected_initiator_scripts.is_empty(), "Not all initiator outputs were sent");
447449
assert!(expected_acceptor_inputs.is_empty(), "Not all acceptor inputs were sent");
448450
assert!(expected_acceptor_scripts.is_empty(), "Not all acceptor outputs were sent");
449451
}

0 commit comments

Comments
 (0)