Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 480
Support intercepting onion messages for offline peers#2973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
arik-so
merged 9 commits into
lightningdevkit:main
from
valentinewallace:2024-03-om-mailboxMay 9, 2024
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e8f7fe1
Support generating events when an OM for an offline peer is received.
valentinewallace 1c28cc0
OnionMessenger: support generating peer connection events.
valentinewallace 7213458
Support forwarding prebuilt onion messages in OnionMessenger.
valentinewallace 1fc8f11
Refactor MessengerNode test util construction to take config.
valentinewallace 4c7ecaa
Fix outdated comment in onion message functional test.
valentinewallace be31e63
Test offline peer onion message interception.
valentinewallace 6613f1f
Manually implement Debug for onion message packets.
valentinewallace edc86e3
Limit OnionMessenger event buffer size.
valentinewallace a5ada64
Fill in top-level docs for onion message offline peer interception.
valentinewallace File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -59,6 +59,17 @@ struct MessengerNode { | ||
| >> | ||
| } | ||
| impl Drop for MessengerNode { | ||
| fn drop(&mut self) { | ||
| #[cfg(feature = "std")] { | ||
| if std::thread::panicking() { | ||
| return; | ||
| } | ||
| } | ||
| assert!(release_events(self).is_empty()); | ||
| } | ||
| } | ||
| struct TestOffersMessageHandler {} | ||
| impl OffersMessageHandler for TestOffersMessageHandler { | ||
| @@ -155,22 +166,41 @@ impl CustomOnionMessageHandler for TestCustomMessageHandler { | ||
| } | ||
| fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> { | ||
| let secrets = (1..=num_messengers) | ||
| let cfgs = (1..=num_messengers) | ||
| .into_iter() | ||
| .map(|i| SecretKey::from_slice(&[i; 32]).unwrap()) | ||
| .map(|_| MessengerCfg::new()) | ||
| .collect(); | ||
| create_nodes_using_secrets(secrets) | ||
| create_nodes_using_cfgs(cfgs) | ||
| } | ||
| fn create_nodes_using_secrets(secrets: Vec<SecretKey>) -> Vec<MessengerNode> { | ||
| struct MessengerCfg { | ||
| secret_override: Option<SecretKey>, | ||
| intercept_offline_peer_oms: bool, | ||
| } | ||
| impl MessengerCfg { | ||
| fn new() -> Self { | ||
| Self { secret_override: None, intercept_offline_peer_oms: false } | ||
| } | ||
| fn with_node_secret(mut self, secret: SecretKey) -> Self { | ||
| self.secret_override = Some(secret); | ||
| self | ||
| } | ||
| fn with_offline_peer_interception(mut self) -> Self { | ||
| self.intercept_offline_peer_oms = true; | ||
| self | ||
| } | ||
| } | ||
| fn create_nodes_using_cfgs(cfgs: Vec<MessengerCfg>) -> Vec<MessengerNode> { | ||
| let gossip_logger = Arc::new(test_utils::TestLogger::with_id("gossip".to_string())); | ||
| let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, gossip_logger.clone())); | ||
| let gossip_sync = Arc::new( | ||
| P2PGossipSync::new(network_graph.clone(), None, gossip_logger) | ||
| ); | ||
| let mut nodes = Vec::new(); | ||
| for (i, secret_key) in secrets.into_iter().enumerate() { | ||
| for (i, cfg) in cfgs.into_iter().enumerate() { | ||
| let secret_key = cfg.secret_override.unwrap_or(SecretKey::from_slice(&[(i + 1) as u8; 32]).unwrap()); | ||
| let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i))); | ||
| let seed = [i as u8; 32]; | ||
| let entropy_source = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet)); | ||
| @@ -182,14 +212,24 @@ fn create_nodes_using_secrets(secrets: Vec<SecretKey>) -> Vec<MessengerNode> { | ||
| ); | ||
| let offers_message_handler = Arc::new(TestOffersMessageHandler {}); | ||
| let custom_message_handler = Arc::new(TestCustomMessageHandler::new()); | ||
| let messenger = if cfg.intercept_offline_peer_oms { | ||
| OnionMessenger::new_with_offline_peer_interception( | ||
| entropy_source.clone(), node_signer.clone(), logger.clone(), | ||
| node_id_lookup, message_router, offers_message_handler, | ||
| custom_message_handler.clone() | ||
| ) | ||
| } else { | ||
| OnionMessenger::new( | ||
| entropy_source.clone(), node_signer.clone(), logger.clone(), | ||
| node_id_lookup, message_router, offers_message_handler, | ||
| custom_message_handler.clone() | ||
| ) | ||
| }; | ||
valentinewallace marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| nodes.push(MessengerNode { | ||
| privkey: secret_key, | ||
| node_id: node_signer.get_node_id(Recipient::Node).unwrap(), | ||
| entropy_source: entropy_source.clone(), | ||
| messenger: OnionMessenger::new( | ||
| entropy_source, node_signer, logger.clone(), node_id_lookup, message_router, | ||
| offers_message_handler, custom_message_handler.clone() | ||
| ), | ||
| entropy_source, | ||
| messenger, | ||
| custom_message_handler, | ||
| gossip_sync: gossip_sync.clone(), | ||
| }); | ||
| @@ -362,11 +402,10 @@ fn we_are_intro_node() { | ||
| #[test] | ||
| fn invalid_blinded_path_error() { | ||
| // Make sure we error as expected if a provided blinded path has 0 or 1 hops. | ||
| // Make sure we error as expected if a provided blinded path has 0 hops. | ||
| let nodes = create_nodes(3); | ||
| let test_msg = TestCustomMessage::Response; | ||
| // 0 hops | ||
| let secp_ctx = Secp256k1::new(); | ||
| let mut blinded_path = BlindedPath::new_for_message(&[nodes[1].node_id, nodes[2].node_id], &*nodes[2].entropy_source, &secp_ctx).unwrap(); | ||
| blinded_path.blinded_hops.clear(); | ||
| @@ -539,18 +578,83 @@ fn drops_buffered_messages_waiting_for_peer_connection() { | ||
| assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_none()); | ||
| } | ||
| #[test] | ||
| fn intercept_offline_peer_oms() { | ||
| // Ensure that if OnionMessenger is initialized with | ||
| // new_with_offline_peer_interception, we will intercept OMs for offline | ||
| // peers, generate the right events, and forward OMs when they are re-injected | ||
| // by the user. | ||
| let node_cfgs = vec![MessengerCfg::new(), MessengerCfg::new().with_offline_peer_interception(), MessengerCfg::new()]; | ||
| let mut nodes = create_nodes_using_cfgs(node_cfgs); | ||
| let peer_conn_evs = release_events(&nodes[1]); | ||
| assert_eq!(peer_conn_evs.len(), 2); | ||
| for (i, ev) in peer_conn_evs.iter().enumerate() { | ||
| match ev { | ||
| Event::OnionMessagePeerConnected { peer_node_id } => { | ||
| let node_idx = if i == 0 { 0 } else { 2 }; | ||
| assert_eq!(peer_node_id, &nodes[node_idx].node_id); | ||
| }, | ||
| _ => panic!() | ||
| } | ||
| } | ||
| let message = TestCustomMessage::Response; | ||
| let secp_ctx = Secp256k1::new(); | ||
| let blinded_path = BlindedPath::new_for_message( | ||
| &[nodes[1].node_id, nodes[2].node_id], &*nodes[2].entropy_source, &secp_ctx | ||
| ).unwrap(); | ||
| let destination = Destination::BlindedPath(blinded_path); | ||
| // Disconnect the peers to ensure we intercept the OM. | ||
| disconnect_peers(&nodes[1], &nodes[2]); | ||
| nodes[0].messenger.send_onion_message(message, destination, None).unwrap(); | ||
| let mut final_node_vec = nodes.split_off(2); | ||
| pass_along_path(&nodes); | ||
| let mut events = release_events(&nodes[1]); | ||
| assert_eq!(events.len(), 1); | ||
| let onion_message = match events.remove(0) { | ||
| Event::OnionMessageIntercepted { peer_node_id, message } => { | ||
| assert_eq!(peer_node_id, final_node_vec[0].node_id); | ||
| message | ||
| }, | ||
| _ => panic!() | ||
| }; | ||
| // Ensure that we'll refuse to forward the re-injected OM until after the | ||
| // outbound peer comes back online. | ||
| let err = nodes[1].messenger.forward_onion_message(onion_message.clone(), &final_node_vec[0].node_id).unwrap_err(); | ||
| assert_eq!(err, SendError::InvalidFirstHop(final_node_vec[0].node_id)); | ||
| connect_peers(&nodes[1], &final_node_vec[0]); | ||
| let peer_conn_ev = release_events(&nodes[1]); | ||
| assert_eq!(peer_conn_ev.len(), 1); | ||
| match peer_conn_ev[0] { | ||
| Event::OnionMessagePeerConnected { peer_node_id } => { | ||
| assert_eq!(peer_node_id, final_node_vec[0].node_id); | ||
| }, | ||
| _ => panic!() | ||
| } | ||
| nodes[1].messenger.forward_onion_message(onion_message, &final_node_vec[0].node_id).unwrap(); | ||
| final_node_vec[0].custom_message_handler.expect_message(TestCustomMessage::Response); | ||
| pass_along_path(&vec![nodes.remove(1), final_node_vec.remove(0)]); | ||
| } | ||
| #[test] | ||
| fn spec_test_vector() { | ||
| let secret_keys = [ | ||
| let node_cfgs = [ | ||
| "4141414141414141414141414141414141414141414141414141414141414141", // Alice | ||
| "4242424242424242424242424242424242424242424242424242424242424242", // Bob | ||
| "4343434343434343434343434343434343434343434343434343434343434343", // Carol | ||
| "4444444444444444444444444444444444444444444444444444444444444444", // Dave | ||
| ] | ||
| .iter() | ||
| .map(|secret| SecretKey::from_slice(&<Vec<u8>>::from_hex(secret).unwrap()).unwrap()) | ||
| .map(|secret_hex| SecretKey::from_slice(&<Vec<u8>>::from_hex(secret_hex).unwrap()).unwrap()) | ||
| .map(|secret| MessengerCfg::new().with_node_secret(secret)) | ||
| .collect(); | ||
| let nodes = create_nodes_using_secrets(secret_keys); | ||
| let nodes = create_nodes_using_cfgs(node_cfgs); | ||
| // Hardcode the sender->Alice onion message, because it includes an unknown TLV of type 1, which | ||
| // LDK doesn't support constructing. | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, these are never persisted by LDK, but this lets us check the buffer size. Clients could persist them, I suppose, but would probably want to persists the parts rather than the enum.