Skip to content

Commit a0adc51

Browse files
Don't clone the Vec in remove_first_msg_event_to_node
1 parent b852df5 commit a0adc51

File tree

6 files changed

+30
-34
lines changed

6 files changed

+30
-34
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
935935

936936
// Note that the ordering of the events for different nodes is non-prescriptive, though the
937937
// ordering of the two events that both go to nodes[2] have to stay in the same order.
938-
let (nodes_0_event, events_3) = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &events_3);
938+
let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut events_3);
939939
let messages_a = match nodes_0_event {
940940
MessageSendEvent::UpdateHTLCs { node_id, mut updates } => {
941941
assert_eq!(node_id, nodes[0].node.get_our_node_id());
@@ -949,12 +949,12 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
949949
_ => panic!("Unexpected event type!"),
950950
};
951951

952-
let (nodes_2_event, events_3) = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &events_3);
952+
let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events_3);
953953
let send_event_b = SendEvent::from_event(nodes_2_event);
954954
assert_eq!(send_event_b.node_id, nodes[2].node.get_our_node_id());
955955

956956
let raa = if test_ignore_second_cs {
957-
let (nodes_2_event, _events_3) = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &events_3);
957+
let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events_3);
958958
match nodes_2_event {
959959
MessageSendEvent::SendRevokeAndACK { node_id, msg } => {
960960
assert_eq!(node_id, nodes[2].node.get_our_node_id());

lightning/src/ln/functional_test_utils.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -572,12 +572,12 @@ macro_rules! get_htlc_update_msgs {
572572
}
573573

574574
/// Fetches the first `msg_event` to the passed `node_id` in the passed `msg_events` vec.
575-
/// Returns the `msg_event`, along with an updated `msg_events` vec with the message removed.
575+
/// Returns the `msg_event`.
576576
///
577577
/// Note that even though `BroadcastChannelAnnouncement` and `BroadcastChannelUpdate`
578578
/// `msg_events` are stored under specific peers, this function does not fetch such `msg_events` as
579579
/// such messages are intended to all peers.
580-
pub fn remove_first_msg_event_to_node(msg_node_id: &PublicKey, msg_events: &Vec<MessageSendEvent>) -> (MessageSendEvent, Vec<MessageSendEvent>) {
580+
pub fn remove_first_msg_event_to_node(msg_node_id: &PublicKey, msg_events: &mut Vec<MessageSendEvent>) -> MessageSendEvent {
581581
let ev_index = msg_events.iter().position(|e| { match e {
582582
MessageSendEvent::SendAcceptChannel { node_id, .. } => {
583583
node_id == msg_node_id
@@ -641,9 +641,7 @@ pub fn remove_first_msg_event_to_node(msg_node_id: &PublicKey, msg_events: &Vec<
641641
},
642642
}});
643643
if ev_index.is_some() {
644-
let mut updated_msg_events = msg_events.to_vec();
645-
let ev = updated_msg_events.remove(ev_index.unwrap());
646-
(ev, updated_msg_events)
644+
msg_events.remove(ev_index.unwrap())
647645
} else {
648646
panic!("Couldn't find any MessageSendEvent to the node!")
649647
}
@@ -1482,9 +1480,9 @@ pub fn do_main_commitment_signed_dance(node_a: &Node<'_, '_, '_>, node_b: &Node<
14821480
check_added_monitors!(node_b, 1);
14831481
node_b.node.handle_commitment_signed(&node_a.node.get_our_node_id(), &as_commitment_signed);
14841482
let (bs_revoke_and_ack, extra_msg_option) = {
1485-
let events = node_b.node.get_and_clear_pending_msg_events();
1483+
let mut events = node_b.node.get_and_clear_pending_msg_events();
14861484
assert!(events.len() <= 2);
1487-
let (node_a_event, events) = remove_first_msg_event_to_node(&node_a.node.get_our_node_id(), &events);
1485+
let node_a_event = remove_first_msg_event_to_node(&node_a.node.get_our_node_id(), &mut events);
14881486
(match node_a_event {
14891487
MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
14901488
assert_eq!(*node_id, node_a.node.get_our_node_id());
@@ -1939,8 +1937,7 @@ pub fn pass_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_rou
19391937
let mut events = origin_node.node.get_and_clear_pending_msg_events();
19401938
assert_eq!(events.len(), expected_route.len());
19411939
for (path_idx, expected_path) in expected_route.iter().enumerate() {
1942-
let (ev, updated_events) = remove_first_msg_event_to_node(&expected_path[0].node.get_our_node_id(), &events);
1943-
events = updated_events;
1940+
let ev = remove_first_msg_event_to_node(&expected_path[0].node.get_our_node_id(), &mut events);
19441941
// Once we've gotten through all the HTLCs, the last one should result in a
19451942
// PaymentClaimable (but each previous one should not!), .
19461943
let expect_payment = path_idx == expected_route.len() - 1;
@@ -1999,9 +1996,8 @@ pub fn do_claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>,
19991996
} else {
20001997
for expected_path in expected_paths.iter() {
20011998
// For MPP payments, we always want the message to the first node in the path.
2002-
let (ev, updated_events) = remove_first_msg_event_to_node(&expected_path[0].node.get_our_node_id(), &events);
1999+
let ev = remove_first_msg_event_to_node(&expected_path[0].node.get_our_node_id(), &mut events);
20032000
per_path_msgs.push(msgs_from_ev!(&ev));
2004-
events = updated_events;
20052001
}
20062002
}
20072003

lightning/src/ln/functional_tests.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,7 +2747,7 @@ fn test_htlc_on_chain_success() {
27472747
},
27482748
_ => panic!()
27492749
}
2750-
let events = nodes[1].node.get_and_clear_pending_msg_events();
2750+
let mut events = nodes[1].node.get_and_clear_pending_msg_events();
27512751
{
27522752
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
27532753
assert_eq!(added_monitors.len(), 2);
@@ -2757,8 +2757,8 @@ fn test_htlc_on_chain_success() {
27572757
}
27582758
assert_eq!(events.len(), 3);
27592759

2760-
let (nodes_2_event, events) = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &events);
2761-
let (nodes_0_event, events) = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &events);
2760+
let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
2761+
let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut events);
27622762

27632763
match nodes_2_event {
27642764
MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id: _ } => {},
@@ -3196,14 +3196,15 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use
31963196
_ => panic!("Unexpected event"),
31973197
}
31983198
}
3199+
31993200
nodes[1].node.process_pending_htlc_forwards();
32003201
check_added_monitors!(nodes[1], 1);
32013202

32023203
let mut events = nodes[1].node.get_and_clear_pending_msg_events();
32033204
assert_eq!(events.len(), if deliver_bs_raa { 4 } else { 3 });
32043205

3205-
let events = if deliver_bs_raa {
3206-
let (nodes_2_event, events) = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &events);
3206+
if deliver_bs_raa {
3207+
let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
32073208
match nodes_2_event {
32083209
MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
32093210
assert_eq!(nodes[2].node.get_our_node_id(), *node_id);
@@ -3214,10 +3215,9 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use
32143215
},
32153216
_ => panic!("Unexpected event"),
32163217
}
3217-
events
3218-
} else { events };
3218+
}
32193219

3220-
let (nodes_2_event, events) = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &events);
3220+
let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
32213221
match nodes_2_event {
32223222
MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { msg: msgs::ErrorMessage { channel_id, ref data } }, node_id: _ } => {
32233223
assert_eq!(channel_id, chan_2.2);
@@ -3226,7 +3226,7 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use
32263226
_ => panic!("Unexpected event"),
32273227
}
32283228

3229-
let (nodes_0_event, events) = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &events);
3229+
let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut events);
32303230
match nodes_0_event {
32313231
MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed, .. } } => {
32323232
assert!(update_add_htlcs.is_empty());
@@ -4642,10 +4642,10 @@ fn test_onchain_to_onchain_claim() {
46424642
_ => panic!("Unexpected event"),
46434643
}
46444644
check_added_monitors!(nodes[1], 1);
4645-
let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
4645+
let mut msg_events = nodes[1].node.get_and_clear_pending_msg_events();
46464646
assert_eq!(msg_events.len(), 3);
4647-
let (nodes_2_event, msg_events) = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &msg_events);
4648-
let (nodes_0_event, msg_events) = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &msg_events);
4647+
let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut msg_events);
4648+
let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut msg_events);
46494649

46504650
match nodes_2_event {
46514651
MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id: _ } => {},
@@ -9284,7 +9284,7 @@ fn test_double_partial_claim() {
92849284

92859285
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
92869286
assert_eq!(events.len(), 2);
9287-
let (node_1_msgs, _events) = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &events);
9287+
let node_1_msgs = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
92889288
pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 15_000_000, payment_hash, Some(payment_secret), node_1_msgs, false, None);
92899289

92909290
// At this point nodes[3] has received one half of the payment, and the user goes to handle

lightning/src/ln/payment_tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ fn mpp_retry() {
153153
assert_eq!(events.len(), 2);
154154

155155
// Pass half of the payment along the success path.
156-
let (success_path_msgs, mut events) = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &events);
156+
let success_path_msgs = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
157157
pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 2_000_000, payment_hash, Some(payment_secret), success_path_msgs, false, None);
158158

159159
// Add the HTLC along the first hop.
160-
let (fail_path_msgs_1, _events) = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &events);
160+
let fail_path_msgs_1 = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
161161
let (update_add, commitment_signed) = match fail_path_msgs_1 {
162162
MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
163163
assert_eq!(update_add_htlcs.len(), 1);
@@ -237,7 +237,7 @@ fn do_mpp_receive_timeout(send_partial_mpp: bool) {
237237
assert_eq!(events.len(), 2);
238238

239239
// Pass half of the payment along the first path.
240-
let (node_1_msgs, mut events) = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &events);
240+
let node_1_msgs = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
241241
pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 200_000, payment_hash, Some(payment_secret), node_1_msgs, false, None);
242242

243243
if send_partial_mpp {
@@ -265,7 +265,7 @@ fn do_mpp_receive_timeout(send_partial_mpp: bool) {
265265
expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain().expected_htlc_error_data(23, &[][..]));
266266
} else {
267267
// Pass half of the payment along the second path.
268-
let (node_2_msgs, _events) = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &events);
268+
let node_2_msgs = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
269269
pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 200_000, payment_hash, Some(payment_secret), node_2_msgs, true, None);
270270

271271
// Even after MPP_TIMEOUT_TICKS we should not timeout the MPP if we have all the parts

lightning/src/ln/reload_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,8 @@ fn do_test_partial_claim_before_restart(persist_both_monitors: bool) {
701701
// Send the payment through to nodes[3] *without* clearing the PaymentClaimable event
702702
let mut send_events = nodes[0].node.get_and_clear_pending_msg_events();
703703
assert_eq!(send_events.len(), 2);
704-
let (node_1_msgs, mut send_events) = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &send_events);
705-
let (node_2_msgs, _send_events) = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &send_events);
704+
let node_1_msgs = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut send_events);
705+
let node_2_msgs = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut send_events);
706706
do_pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 15_000_000, payment_hash, Some(payment_secret), node_1_msgs, true, false, None);
707707
do_pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 15_000_000, payment_hash, Some(payment_secret), node_2_msgs, true, false, None);
708708

lightning/src/ln/reorg_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::chain::transaction::OutPoint;
1414
use crate::chain::Confirm;
1515
use crate::ln::channelmanager::ChannelManager;
1616
use crate::ln::msgs::ChannelMessageHandler;
17-
use crate::util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
17+
use crate::util::events::{Event, MessageSendEventsProvider, ClosureReason, HTLCDestination};
1818
use crate::util::test_utils;
1919
use crate::util::ser::Writeable;
2020

0 commit comments

Comments
 (0)