From: Wilmer Paulino Date: Mon, 31 Oct 2022 17:36:12 +0000 (-0700) Subject: Consume events by value in EventHandler's handle_event X-Git-Tag: v0.0.113~45^2~4 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=05cb46723489a91cf14e6e1ada27589a30bd9040;p=rust-lightning Consume events by value in EventHandler's handle_event --- diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index a8102c370..b394a2311 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -227,7 +227,7 @@ impl< L: Deref, > EventHandler for DecoratingEventHandler<'a, E, PGS, RGS, G, A, L> where A::Target: chain::Access, L::Target: Logger { - fn handle_event(&self, event: &Event) { + fn handle_event(&self, event: Event) { if let Some(network_graph) = self.gossip_sync.network_graph() { handle_network_graph_update(network_graph, &event) } @@ -779,7 +779,7 @@ mod tests { begin_open_channel!($node_a, $node_b, $channel_value); let events = $node_a.node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); - let (temporary_channel_id, tx) = handle_funding_generation_ready!(&events[0], $channel_value); + let (temporary_channel_id, tx) = handle_funding_generation_ready!(events[0], $channel_value); end_open_channel!($node_a, $node_b, temporary_channel_id, tx); tx }} @@ -796,7 +796,7 @@ mod tests { macro_rules! handle_funding_generation_ready { ($event: expr, $channel_value: expr) => {{ match $event { - &Event::FundingGenerationReady { temporary_channel_id, channel_value_satoshis, ref output_script, user_channel_id, .. } => { + Event::FundingGenerationReady { temporary_channel_id, channel_value_satoshis, ref output_script, user_channel_id, .. } => { assert_eq!(channel_value_satoshis, $channel_value); assert_eq!(user_channel_id, 42); @@ -857,7 +857,7 @@ mod tests { // Initiate the background processors to watch each node. let data_dir = nodes[0].persister.get_data_dir(); let persister = Arc::new(Persister::new(data_dir)); - let event_handler = |_: &_| {}; + let event_handler = |_: _| {}; let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].p2p_gossip_sync(), nodes[0].peer_manager.clone(), nodes[0].logger.clone(), Some(nodes[0].scorer.clone())); macro_rules! check_persisted_data { @@ -919,7 +919,7 @@ mod tests { let nodes = create_nodes(1, "test_timer_tick_called".to_string()); let data_dir = nodes[0].persister.get_data_dir(); let persister = Arc::new(Persister::new(data_dir)); - let event_handler = |_: &_| {}; + let event_handler = |_: _| {}; let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].no_gossip_sync(), nodes[0].peer_manager.clone(), nodes[0].logger.clone(), Some(nodes[0].scorer.clone())); loop { let log_entries = nodes[0].logger.lines.lock().unwrap(); @@ -942,7 +942,7 @@ mod tests { let data_dir = nodes[0].persister.get_data_dir(); let persister = Arc::new(Persister::new(data_dir).with_manager_error(std::io::ErrorKind::Other, "test")); - let event_handler = |_: &_| {}; + let event_handler = |_: _| {}; let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].no_gossip_sync(), nodes[0].peer_manager.clone(), nodes[0].logger.clone(), Some(nodes[0].scorer.clone())); match bg_processor.join() { Ok(_) => panic!("Expected error persisting manager"), @@ -959,7 +959,7 @@ mod tests { let nodes = create_nodes(2, "test_persist_network_graph_error".to_string()); let data_dir = nodes[0].persister.get_data_dir(); let persister = Arc::new(Persister::new(data_dir).with_graph_error(std::io::ErrorKind::Other, "test")); - let event_handler = |_: &_| {}; + let event_handler = |_: _| {}; let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].p2p_gossip_sync(), nodes[0].peer_manager.clone(), nodes[0].logger.clone(), Some(nodes[0].scorer.clone())); match bg_processor.stop() { @@ -977,7 +977,7 @@ mod tests { let nodes = create_nodes(2, "test_persist_scorer_error".to_string()); let data_dir = nodes[0].persister.get_data_dir(); let persister = Arc::new(Persister::new(data_dir).with_scorer_error(std::io::ErrorKind::Other, "test")); - let event_handler = |_: &_| {}; + let event_handler = |_: _| {}; let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].no_gossip_sync(), nodes[0].peer_manager.clone(), nodes[0].logger.clone(), Some(nodes[0].scorer.clone())); match bg_processor.stop() { @@ -998,7 +998,7 @@ mod tests { // Set up a background event handler for FundingGenerationReady events. let (sender, receiver) = std::sync::mpsc::sync_channel(1); - let event_handler = move |event: &Event| match event { + let event_handler = move |event: Event| match event { Event::FundingGenerationReady { .. } => sender.send(handle_funding_generation_ready!(event, channel_value)).unwrap(), Event::ChannelReady { .. } => {}, _ => panic!("Unexpected event: {:?}", event), @@ -1027,7 +1027,7 @@ mod tests { // Set up a background event handler for SpendableOutputs events. let (sender, receiver) = std::sync::mpsc::sync_channel(1); - let event_handler = move |event: &Event| match event { + let event_handler = move |event: Event| match event { Event::SpendableOutputs { .. } => sender.send(event.clone()).unwrap(), Event::ChannelReady { .. } => {}, Event::ChannelClosed { .. } => {}, @@ -1057,7 +1057,7 @@ mod tests { let nodes = create_nodes(2, "test_scorer_persistence".to_string()); let data_dir = nodes[0].persister.get_data_dir(); let persister = Arc::new(Persister::new(data_dir)); - let event_handler = |_: &_| {}; + let event_handler = |_: _| {}; let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].no_gossip_sync(), nodes[0].peer_manager.clone(), nodes[0].logger.clone(), Some(nodes[0].scorer.clone())); loop { @@ -1085,7 +1085,7 @@ mod tests { assert!(original_graph_description.contains("42: features: 0000, node_one:")); assert_eq!(network_graph.read_only().channels().len(), 1); - let event_handler = |_: &_| {}; + let event_handler = |_: _| {}; let background_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].rapid_gossip_sync(), nodes[0].peer_manager.clone(), nodes[0].logger.clone(), Some(nodes[0].scorer.clone())); loop { @@ -1138,7 +1138,7 @@ mod tests { let data_dir = nodes[0].persister.get_data_dir(); let persister = Arc::new(Persister::new(data_dir)); let router = DefaultRouter::new(Arc::clone(&nodes[0].network_graph), Arc::clone(&nodes[0].logger), random_seed_bytes, Arc::clone(&nodes[0].scorer)); - let invoice_payer = Arc::new(InvoicePayer::new(Arc::clone(&nodes[0].node), router, Arc::clone(&nodes[0].logger), |_: &_| {}, Retry::Attempts(2))); + let invoice_payer = Arc::new(InvoicePayer::new(Arc::clone(&nodes[0].node), router, Arc::clone(&nodes[0].logger), |_: _| {}, Retry::Attempts(2))); let event_handler = Arc::clone(&invoice_payer); let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].no_gossip_sync(), nodes[0].peer_manager.clone(), nodes[0].logger.clone(), Some(nodes[0].scorer.clone())); assert!(bg_processor.stop().is_ok()); diff --git a/lightning-invoice/src/payment.rs b/lightning-invoice/src/payment.rs index 08000b4dc..cbd760c37 100644 --- a/lightning-invoice/src/payment.rs +++ b/lightning-invoice/src/payment.rs @@ -105,7 +105,7 @@ //! # } //! # //! # fn main() { -//! let event_handler = |event: &Event| { +//! let event_handler = |event: Event| { //! match event { //! Event::PaymentPathFailed { .. } => println!("payment failed after retries"), //! Event::PaymentSent { .. } => println!("payment successful"), @@ -841,10 +841,10 @@ where P::Target: Payer, L::Target: Logger, { - fn handle_event(&self, event: &Event) { + fn handle_event(&self, event: Event) { let should_forward = self.handle_event_internal(&event); if should_forward { - self.event_handler.handle_event(&event) + self.event_handler.handle_event(event) } } } @@ -960,7 +960,7 @@ mod tests { #[test] fn pays_invoice_on_first_attempt() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -976,7 +976,7 @@ mod tests { let payment_id = Some(invoice_payer.pay_invoice(&invoice).unwrap()); assert_eq!(*payer.attempts.borrow(), 1); - invoice_payer.handle_event(&Event::PaymentSent { + invoice_payer.handle_event(Event::PaymentSent { payment_id, payment_preimage, payment_hash, fee_paid_msat: None }); assert_eq!(*event_handled.borrow(), true); @@ -986,7 +986,7 @@ mod tests { #[test] fn pays_invoice_on_retry() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -1014,11 +1014,11 @@ mod tests { short_channel_id: None, retry: Some(TestRouter::retry_for_invoice(&invoice)), }; - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event); assert_eq!(*event_handled.borrow(), false); assert_eq!(*payer.attempts.borrow(), 2); - invoice_payer.handle_event(&Event::PaymentSent { + invoice_payer.handle_event(Event::PaymentSent { payment_id, payment_preimage, payment_hash, fee_paid_msat: None }); assert_eq!(*event_handled.borrow(), true); @@ -1027,7 +1027,7 @@ mod tests { #[test] fn pays_invoice_on_partial_failure() { - let event_handler = |_: &Event| { panic!() }; + let event_handler = |_: Event| { panic!() }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -1051,7 +1051,7 @@ mod tests { #[test] fn retries_payment_path_for_unknown_payment() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -1077,15 +1077,15 @@ mod tests { short_channel_id: None, retry: Some(TestRouter::retry_for_invoice(&invoice)), }; - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event.clone()); assert_eq!(*event_handled.borrow(), false); assert_eq!(*payer.attempts.borrow(), 1); - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event.clone()); assert_eq!(*event_handled.borrow(), false); assert_eq!(*payer.attempts.borrow(), 2); - invoice_payer.handle_event(&Event::PaymentSent { + invoice_payer.handle_event(Event::PaymentSent { payment_id, payment_preimage, payment_hash, fee_paid_msat: None }); assert_eq!(*event_handled.borrow(), true); @@ -1095,7 +1095,7 @@ mod tests { #[test] fn fails_paying_invoice_after_max_retry_counts() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -1123,7 +1123,7 @@ mod tests { short_channel_id: None, retry: Some(TestRouter::retry_for_invoice(&invoice)), }; - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event); assert_eq!(*event_handled.borrow(), false); assert_eq!(*payer.attempts.borrow(), 2); @@ -1139,11 +1139,11 @@ mod tests { final_value_msat: final_value_msat / 2, ..TestRouter::retry_for_invoice(&invoice) }), }; - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event.clone()); assert_eq!(*event_handled.borrow(), false); assert_eq!(*payer.attempts.borrow(), 3); - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event.clone()); assert_eq!(*event_handled.borrow(), true); assert_eq!(*payer.attempts.borrow(), 3); } @@ -1152,7 +1152,7 @@ mod tests { #[test] fn fails_paying_invoice_after_max_retry_timeout() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -1182,13 +1182,13 @@ mod tests { short_channel_id: None, retry: Some(TestRouter::retry_for_invoice(&invoice)), }; - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event.clone()); assert_eq!(*event_handled.borrow(), false); assert_eq!(*payer.attempts.borrow(), 2); SinceEpoch::advance(Duration::from_secs(121)); - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event.clone()); assert_eq!(*event_handled.borrow(), true); assert_eq!(*payer.attempts.borrow(), 2); } @@ -1196,7 +1196,7 @@ mod tests { #[test] fn fails_paying_invoice_with_missing_retry_params() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -1221,7 +1221,7 @@ mod tests { short_channel_id: None, retry: None, }; - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event); assert_eq!(*event_handled.borrow(), true); assert_eq!(*payer.attempts.borrow(), 1); } @@ -1231,7 +1231,7 @@ mod tests { #[test] fn fails_paying_invoice_after_expiration() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payer = TestPayer::new(); let router = TestRouter::new(TestScorer::new()); @@ -1251,7 +1251,7 @@ mod tests { #[test] fn fails_retrying_invoice_after_expiration() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -1280,7 +1280,7 @@ mod tests { short_channel_id: None, retry: Some(retry_data), }; - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event); assert_eq!(*event_handled.borrow(), true); assert_eq!(*payer.attempts.borrow(), 1); } @@ -1288,7 +1288,7 @@ mod tests { #[test] fn fails_paying_invoice_after_retry_error() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -1316,7 +1316,7 @@ mod tests { short_channel_id: None, retry: Some(TestRouter::retry_for_invoice(&invoice)), }; - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event); assert_eq!(*event_handled.borrow(), true); assert_eq!(*payer.attempts.borrow(), 2); } @@ -1324,7 +1324,7 @@ mod tests { #[test] fn fails_paying_invoice_after_rejected_by_payee() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -1349,7 +1349,7 @@ mod tests { short_channel_id: None, retry: Some(TestRouter::retry_for_invoice(&invoice)), }; - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event); assert_eq!(*event_handled.borrow(), true); assert_eq!(*payer.attempts.borrow(), 1); } @@ -1357,7 +1357,7 @@ mod tests { #[test] fn fails_repaying_invoice_with_pending_payment() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -1397,7 +1397,7 @@ mod tests { short_channel_id: None, retry: Some(TestRouter::retry_for_invoice(&invoice)), }; - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event); assert_eq!(*event_handled.borrow(), true); } @@ -1407,7 +1407,7 @@ mod tests { let router = FailingRouter {}; let logger = TestLogger::new(); let invoice_payer = - InvoicePayer::new(&payer, router, &logger, |_: &Event| {}, Retry::Attempts(0)); + InvoicePayer::new(&payer, router, &logger, |_: Event| {}, Retry::Attempts(0)); let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -1430,7 +1430,7 @@ mod tests { let router = TestRouter::new(TestScorer::new()); let logger = TestLogger::new(); let invoice_payer = - InvoicePayer::new(&payer, router, &logger, |_: &Event| {}, Retry::Attempts(0)); + InvoicePayer::new(&payer, router, &logger, |_: Event| {}, Retry::Attempts(0)); match invoice_payer.pay_invoice(&invoice) { Err(PaymentError::Sending(_)) => {}, @@ -1442,7 +1442,7 @@ mod tests { #[test] fn pays_zero_value_invoice_using_amount() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = zero_value_invoice(payment_preimage); @@ -1459,7 +1459,7 @@ mod tests { Some(invoice_payer.pay_zero_value_invoice(&invoice, final_value_msat).unwrap()); assert_eq!(*payer.attempts.borrow(), 1); - invoice_payer.handle_event(&Event::PaymentSent { + invoice_payer.handle_event(Event::PaymentSent { payment_id, payment_preimage, payment_hash, fee_paid_msat: None }); assert_eq!(*event_handled.borrow(), true); @@ -1469,7 +1469,7 @@ mod tests { #[test] fn fails_paying_zero_value_invoice_with_amount() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payer = TestPayer::new(); let router = TestRouter::new(TestScorer::new()); @@ -1491,7 +1491,7 @@ mod tests { #[test] fn pays_pubkey_with_amount() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let pubkey = pubkey(); let payment_preimage = PaymentPreimage([1; 32]); @@ -1527,11 +1527,11 @@ mod tests { short_channel_id: None, retry: Some(retry), }; - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event); assert_eq!(*event_handled.borrow(), false); assert_eq!(*payer.attempts.borrow(), 2); - invoice_payer.handle_event(&Event::PaymentSent { + invoice_payer.handle_event(Event::PaymentSent { payment_id, payment_preimage, payment_hash, fee_paid_msat: None }); assert_eq!(*event_handled.borrow(), true); @@ -1541,7 +1541,7 @@ mod tests { #[test] fn scores_failed_channel() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -1573,13 +1573,13 @@ mod tests { short_channel_id, retry: Some(TestRouter::retry_for_invoice(&invoice)), }; - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event); } #[test] fn scores_successful_channels() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -1601,17 +1601,17 @@ mod tests { let event = Event::PaymentPathSuccessful { payment_id, payment_hash, path: route.paths[0].clone() }; - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event); let event = Event::PaymentPathSuccessful { payment_id, payment_hash, path: route.paths[1].clone() }; - invoice_payer.handle_event(&event); + invoice_payer.handle_event(event); } #[test] fn generates_correct_inflight_map_data() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice = invoice(payment_preimage); @@ -1638,7 +1638,7 @@ mod tests { assert_eq!(inflight_map.0.get(&(3, false)).unwrap().clone(), 74); assert_eq!(inflight_map.0.get(&(4, false)).unwrap().clone(), 64); - invoice_payer.handle_event(&Event::PaymentPathSuccessful { + invoice_payer.handle_event(Event::PaymentPathSuccessful { payment_id, payment_hash, path: route.paths[0].clone() }); @@ -1657,7 +1657,7 @@ mod tests { fn considers_inflight_htlcs_between_invoice_payments_when_path_succeeds() { // First, let's just send a payment through, but only make sure one of the path completes let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let payment_invoice = invoice(payment_preimage); @@ -1691,7 +1691,7 @@ mod tests { // Succeed 1st path, leave 2nd path inflight let payment_id = invoice_payer.pay_invoice(&payment_invoice).unwrap(); - invoice_payer.handle_event(&Event::PaymentPathSuccessful { + invoice_payer.handle_event(Event::PaymentPathSuccessful { payment_id, payment_hash, path: route.paths[0].clone() }); @@ -1708,7 +1708,7 @@ mod tests { fn considers_inflight_htlcs_between_retries() { // First, let's just send a payment through, but only make sure one of the path completes let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let payment_invoice = invoice(payment_preimage); @@ -1749,7 +1749,7 @@ mod tests { // Fail 1st path, leave 2nd path inflight let payment_id = Some(invoice_payer.pay_invoice(&payment_invoice).unwrap()); - invoice_payer.handle_event(&Event::PaymentPathFailed { + invoice_payer.handle_event(Event::PaymentPathFailed { payment_id, payment_hash, network_update: None, @@ -1761,7 +1761,7 @@ mod tests { }); // Fails again the 1st path of our retry - invoice_payer.handle_event(&Event::PaymentPathFailed { + invoice_payer.handle_event(Event::PaymentPathFailed { payment_id, payment_hash, network_update: None, @@ -1779,7 +1779,7 @@ mod tests { #[test] fn accounts_for_some_inflight_htlcs_sent_during_partial_failure() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice_to_pay = invoice(payment_preimage); @@ -1810,7 +1810,7 @@ mod tests { #[test] fn accounts_for_all_inflight_htlcs_sent_during_partial_failure() { let event_handled = core::cell::RefCell::new(false); - let event_handler = |_: &Event| { *event_handled.borrow_mut() = true; }; + let event_handler = |_: Event| { *event_handled.borrow_mut() = true; }; let payment_preimage = PaymentPreimage([1; 32]); let invoice_to_pay = invoice(payment_preimage); @@ -2307,7 +2307,7 @@ mod tests { route.paths[1][0].fee_msat = 50_000_000; router.expect_find_route(Ok(route.clone())); - let event_handler = |_: &Event| { panic!(); }; + let event_handler = |_: Event| { panic!(); }; let invoice_payer = InvoicePayer::new(nodes[0].node, router, nodes[0].logger, event_handler, Retry::Attempts(1)); assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager_and_duration_since_epoch( @@ -2352,7 +2352,7 @@ mod tests { route.paths[1][0].fee_msat = 50_000_001; router.expect_find_route(Ok(route.clone())); - let event_handler = |_: &Event| { panic!(); }; + let event_handler = |_: Event| { panic!(); }; let invoice_payer = InvoicePayer::new(nodes[0].node, router, nodes[0].logger, event_handler, Retry::Attempts(1)); assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager_and_duration_since_epoch( @@ -2429,8 +2429,8 @@ mod tests { route.paths.remove(1); router.expect_find_route(Ok(route.clone())); - let expected_events: RefCell> = RefCell::new(VecDeque::new()); - let event_handler = |event: &Event| { + let expected_events: RefCell> = RefCell::new(VecDeque::new()); + let event_handler = |event: Event| { let event_checker = expected_events.borrow_mut().pop_front().unwrap(); event_checker(event); }; @@ -2505,7 +2505,7 @@ mod tests { // `PaymentPathFailed` being passed up to the user (us, in this case). Previously, we'd // treated this as "HTLC complete" and dropped the retry counter, causing us to retry again // if the final HTLC failed. - expected_events.borrow_mut().push_back(&|ev: &Event| { + expected_events.borrow_mut().push_back(&|ev: Event| { if let Event::PaymentPathFailed { payment_failed_permanently, all_paths_failed, .. } = ev { assert!(!payment_failed_permanently); assert!(all_paths_failed); @@ -2523,13 +2523,13 @@ mod tests { nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_update.update_fail_htlcs[0]); commitment_signed_dance!(nodes[0], nodes[1], &bs_fail_update.commitment_signed, false, true); - expected_events.borrow_mut().push_back(&|ev: &Event| { + expected_events.borrow_mut().push_back(&|ev: Event| { if let Event::PaymentPathFailed { payment_failed_permanently, all_paths_failed, .. } = ev { assert!(!payment_failed_permanently); assert!(all_paths_failed); } else { panic!("Unexpected event"); } }); - expected_events.borrow_mut().push_back(&|ev: &Event| { + expected_events.borrow_mut().push_back(&|ev: Event| { if let Event::PaymentFailed { .. } = ev { } else { panic!("Unexpected event"); } }); diff --git a/lightning-net-tokio/src/lib.rs b/lightning-net-tokio/src/lib.rs index 3fbe6aab9..7a7cc4bb0 100644 --- a/lightning-net-tokio/src/lib.rs +++ b/lightning-net-tokio/src/lib.rs @@ -43,7 +43,7 @@ //! async fn connect_to_node(peer_manager: PeerManager, chain_monitor: Arc, channel_manager: ChannelManager, their_node_id: PublicKey, addr: SocketAddr) { //! lightning_net_tokio::connect_outbound(peer_manager, their_node_id, addr).await; //! loop { -//! let event_handler = |event: &Event| { +//! let event_handler = |event: Event| { //! // Handle the event! //! }; //! channel_manager.await_persistable_update(); @@ -56,7 +56,7 @@ //! async fn accept_socket(peer_manager: PeerManager, chain_monitor: Arc, channel_manager: ChannelManager, socket: TcpStream) { //! lightning_net_tokio::setup_inbound(peer_manager, socket); //! loop { -//! let event_handler = |event: &Event| { +//! let event_handler = |event: Event| { //! // Handle the event! //! }; //! channel_manager.await_persistable_update(); diff --git a/lightning/src/chain/chainmonitor.rs b/lightning/src/chain/chainmonitor.rs index fc4caef8a..c70eed1a1 100644 --- a/lightning/src/chain/chainmonitor.rs +++ b/lightning/src/chain/chainmonitor.rs @@ -492,7 +492,7 @@ where C::Target: chain::Filter, pub fn get_and_clear_pending_events(&self) -> Vec { use crate::util::events::EventsProvider; let events = core::cell::RefCell::new(Vec::new()); - let event_handler = |event: &events::Event| events.borrow_mut().push(event.clone()); + let event_handler = |event: events::Event| events.borrow_mut().push(event); self.process_pending_events(&event_handler); events.into_inner() } @@ -736,8 +736,8 @@ impl even for monitor_state in self.monitors.read().unwrap().values() { pending_events.append(&mut monitor_state.monitor.get_and_clear_pending_events()); } - for event in pending_events.drain(..) { - handler.handle_event(&event); + for event in pending_events { + handler.handle_event(event); } } #[cfg(anchors)] @@ -759,8 +759,8 @@ impl even for monitor_state in self.monitors.read().unwrap().values() { pending_events.append(&mut monitor_state.monitor.get_and_clear_pending_events()); } - for event in pending_events.drain(..) { - handler.handle_event(&event); + for event in pending_events { + handler.handle_event(event); } } } diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 1458d0c50..7f8c82b41 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -5714,7 +5714,7 @@ impl ChannelManager Vec { let events = core::cell::RefCell::new(Vec::new()); - let event_handler = |event: &events::Event| events.borrow_mut().push(event.clone()); + let event_handler = |event: events::Event| events.borrow_mut().push(event); self.process_pending_events(&event_handler); events.into_inner() } @@ -5791,13 +5791,13 @@ where result = NotifyOption::DoPersist; } - let mut pending_events = mem::replace(&mut *self.pending_events.lock().unwrap(), vec![]); + let pending_events = mem::replace(&mut *self.pending_events.lock().unwrap(), vec![]); if !pending_events.is_empty() { result = NotifyOption::DoPersist; } - for event in pending_events.drain(..) { - handler.handle_event(&event); + for event in pending_events { + handler.handle_event(event); } result diff --git a/lightning/src/util/events.rs b/lightning/src/util/events.rs index a9687d288..c369a376a 100644 --- a/lightning/src/util/events.rs +++ b/lightning/src/util/events.rs @@ -1438,17 +1438,17 @@ pub trait EventHandler { /// Handles the given [`Event`]. /// /// See [`EventsProvider`] for details that must be considered when implementing this method. - fn handle_event(&self, event: &Event); + fn handle_event(&self, event: Event); } -impl EventHandler for F where F: Fn(&Event) { - fn handle_event(&self, event: &Event) { +impl EventHandler for F where F: Fn(Event) { + fn handle_event(&self, event: Event) { self(event) } } impl EventHandler for Arc { - fn handle_event(&self, event: &Event) { + fn handle_event(&self, event: Event) { self.deref().handle_event(event) } }