Skip to content

Commit 52025d1

Browse files
committed
Pass UpdateFulfillHTLC message owned, rather than by ref
Most of the message handlers in LDK pass messages to the handler by reference. This is unnecessarily inefficient and there's no real reason to do so. Because we just tweaked what `UpdateFulfillHTLC` messages look like, this is a good opportunity to make progress here, passing at least one message owned rather than by reference.
1 parent cbe90c5 commit 52025d1

19 files changed

+222
-209
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,9 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
11381138
dest.handle_update_add_htlc(nodes[$node].get_our_node_id(), &new_msg);
11391139
}
11401140
}
1141-
for update_fulfill in update_fulfill_htlcs.iter() {
1141+
let processed_change = !update_add_htlcs.is_empty() || !update_fulfill_htlcs.is_empty() ||
1142+
!update_fail_htlcs.is_empty() || !update_fail_malformed_htlcs.is_empty();
1143+
for update_fulfill in update_fulfill_htlcs {
11421144
out.locked_write(format!("Delivering update_fulfill_htlc from node {} to node {}.\n", $node, idx).as_bytes());
11431145
dest.handle_update_fulfill_htlc(nodes[$node].get_our_node_id(), update_fulfill);
11441146
}
@@ -1154,8 +1156,6 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
11541156
out.locked_write(format!("Delivering update_fee from node {} to node {}.\n", $node, idx).as_bytes());
11551157
dest.handle_update_fee(nodes[$node].get_our_node_id(), &msg);
11561158
}
1157-
let processed_change = !update_add_htlcs.is_empty() || !update_fulfill_htlcs.is_empty() ||
1158-
!update_fail_htlcs.is_empty() || !update_fail_malformed_htlcs.is_empty();
11591159
if $limit_events != ProcessMessages::AllMessages && processed_change {
11601160
// If we only want to process some messages, don't deliver the CS until later.
11611161
extra_ev = Some(MessageSendEvent::UpdateHTLCs { node_id, channel_id, updates: CommitmentUpdate {

lightning-dns-resolver/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,8 @@ mod test {
458458
}
459459

460460
check_added_monitors(&nodes[1], 1);
461-
let updates = get_htlc_update_msgs!(nodes[1], payer_id);
462-
nodes[0].node.handle_update_fulfill_htlc(payee_id, &updates.update_fulfill_htlcs[0]);
461+
let mut updates = get_htlc_update_msgs!(nodes[1], payer_id);
462+
nodes[0].node.handle_update_fulfill_htlc(payee_id, updates.update_fulfill_htlcs.remove(0));
463463
commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
464464

465465
expect_payment_sent(&nodes[0], our_payment_preimage, None, true, true);

lightning-net-tokio/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ mod tests {
721721
#[cfg(simple_close)]
722722
fn handle_closing_sig(&self, _their_node_id: PublicKey, _msg: ClosingSig) {}
723723
fn handle_update_add_htlc(&self, _their_node_id: PublicKey, _msg: &UpdateAddHTLC) {}
724-
fn handle_update_fulfill_htlc(&self, _their_node_id: PublicKey, _msg: &UpdateFulfillHTLC) {}
724+
fn handle_update_fulfill_htlc(&self, _their_node_id: PublicKey, _msg: UpdateFulfillHTLC) {}
725725
fn handle_update_fail_htlc(&self, _their_node_id: PublicKey, _msg: &UpdateFailHTLC) {}
726726
fn handle_update_fail_malformed_htlc(
727727
&self, _their_node_id: PublicKey, _msg: &UpdateFailMalformedHTLC,

lightning/src/chain/chainmonitor.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,27 +1394,27 @@ mod tests {
13941394
// Now manually walk the commitment signed dance - because we claimed two payments
13951395
// back-to-back it doesn't fit into the neat walk commitment_signed_dance does.
13961396

1397-
let updates = get_htlc_update_msgs!(nodes[1], node_a_id);
1398-
nodes[0].node.handle_update_fulfill_htlc(node_b_id, &updates.update_fulfill_htlcs[0]);
1397+
let mut updates = get_htlc_update_msgs!(nodes[1], node_a_id);
1398+
nodes[0].node.handle_update_fulfill_htlc(node_b_id, updates.update_fulfill_htlcs.remove(0));
13991399
expect_payment_sent(&nodes[0], payment_preimage_1, None, false, false);
14001400
nodes[0].node.handle_commitment_signed_batch_test(node_b_id, &updates.commitment_signed);
14011401
check_added_monitors!(nodes[0], 1);
14021402
let (as_first_raa, as_first_update) = get_revoke_commit_msgs!(nodes[0], node_b_id);
14031403

14041404
nodes[1].node.handle_revoke_and_ack(node_a_id, &as_first_raa);
14051405
check_added_monitors!(nodes[1], 1);
1406-
let bs_second_updates = get_htlc_update_msgs!(nodes[1], node_a_id);
1406+
let mut bs_2nd_updates = get_htlc_update_msgs!(nodes[1], node_a_id);
14071407
nodes[1].node.handle_commitment_signed_batch_test(node_a_id, &as_first_update);
14081408
check_added_monitors!(nodes[1], 1);
14091409
let bs_first_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, node_a_id);
14101410

14111411
nodes[0]
14121412
.node
1413-
.handle_update_fulfill_htlc(node_b_id, &bs_second_updates.update_fulfill_htlcs[0]);
1413+
.handle_update_fulfill_htlc(node_b_id, bs_2nd_updates.update_fulfill_htlcs.remove(0));
14141414
expect_payment_sent(&nodes[0], payment_preimage_2, None, false, false);
14151415
nodes[0]
14161416
.node
1417-
.handle_commitment_signed_batch_test(node_b_id, &bs_second_updates.commitment_signed);
1417+
.handle_commitment_signed_batch_test(node_b_id, &bs_2nd_updates.commitment_signed);
14181418
check_added_monitors!(nodes[0], 1);
14191419
nodes[0].node.handle_revoke_and_ack(node_b_id, &bs_first_raa);
14201420
expect_payment_path_successful!(nodes[0]);

lightning/src/ln/async_signer_tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -825,20 +825,20 @@ fn do_test_async_commitment_signature_ordering(monitor_update_failure: bool) {
825825

826826
// Handle the update_fulfill_htlc, but fail to persist the monitor update when handling the
827827
// commitment_signed.
828-
let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
828+
let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
829829
assert_eq!(events_2.len(), 1);
830-
match events_2[0] {
830+
match events_2.remove(0) {
831831
MessageSendEvent::UpdateHTLCs {
832832
node_id: _,
833833
channel_id: _,
834-
updates: msgs::CommitmentUpdate { ref update_fulfill_htlcs, ref commitment_signed, .. },
834+
updates: msgs::CommitmentUpdate { mut update_fulfill_htlcs, commitment_signed, .. },
835835
} => {
836-
nodes[0].node.handle_update_fulfill_htlc(node_b_id, &update_fulfill_htlcs[0]);
836+
nodes[0].node.handle_update_fulfill_htlc(node_b_id, update_fulfill_htlcs.remove(0));
837837
expect_payment_sent(&nodes[0], payment_preimage_1, None, false, false);
838838
if monitor_update_failure {
839839
chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
840840
}
841-
nodes[0].node.handle_commitment_signed_batch_test(node_b_id, commitment_signed);
841+
nodes[0].node.handle_commitment_signed_batch_test(node_b_id, &commitment_signed);
842842
if monitor_update_failure {
843843
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
844844
} else {
@@ -1401,8 +1401,8 @@ fn test_no_disconnect_while_async_commitment_signed_expecting_remote_revoke_and_
14011401

14021402
// After processing the `update_fulfill`, they'll only be able to send `revoke_and_ack` until
14031403
// the `commitment_signed` is no longer pending.
1404-
let update = get_htlc_update_msgs!(&nodes[1], node_a_id);
1405-
nodes[0].node.handle_update_fulfill_htlc(node_b_id, &update.update_fulfill_htlcs[0]);
1404+
let mut update = get_htlc_update_msgs!(&nodes[1], node_a_id);
1405+
nodes[0].node.handle_update_fulfill_htlc(node_b_id, update.update_fulfill_htlcs.remove(0));
14061406
nodes[0].node.handle_commitment_signed_batch_test(node_b_id, &update.commitment_signed);
14071407
check_added_monitors(&nodes[0], 1);
14081408

0 commit comments

Comments
 (0)