Skip to content

Commit

Permalink
Feature I need for playing around
Browse files Browse the repository at this point in the history
  • Loading branch information
fiksn committed Aug 10, 2023
1 parent 6f58072 commit c6dbeca
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions lightning/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ regex = { version = "1.5.6", optional = true }
backtrace = { version = "0.3", optional = true }

core2 = { version = "0.3.0", optional = true, default-features = false }
rand = { verson = "0.8" }

[dev-dependencies]
hex = "0.4"
Expand Down
9 changes: 7 additions & 2 deletions lightning/src/ln/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
//! (see [BOLT-4](https://github.com/lightning/bolts/blob/master/04-onion-routing.md) for more information).
//! - `BasicMPP` - requires/supports that a node can receive basic multi-part payments
//! (see [BOLT-4](https://github.com/lightning/bolts/blob/master/04-onion-routing.md#basic-multi-part-payments) for more information).
//! - `GossipQueriesEx` - requires/supports more sophisticated gossip control
//! (see [BOLT-7](https://github.com/lightning/bolts/blob/master/07-routing-gossip.md) for more information).
//! - `Wumbo` - requires/supports that a node create large channels. Called `option_support_large_channel` in the spec.
//! (see [BOLT-2](https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#the-open_channel-message) for more information).
//! - `ShutdownAnySegwit` - requires/supports that future segwit versions are allowed in `shutdown`
Expand Down Expand Up @@ -139,7 +141,7 @@ mod sealed {
// Byte 0
DataLossProtect | InitialRoutingSync | UpfrontShutdownScript | GossipQueries,
// Byte 1
VariableLengthOnion | StaticRemoteKey | PaymentSecret,
VariableLengthOnion | StaticRemoteKey | PaymentSecret | GossipQueriesEx,
// Byte 2
BasicMPP | Wumbo | AnchorsNonzeroFeeHtlcTx | AnchorsZeroFeeHtlcTx,
// Byte 3
Expand All @@ -155,7 +157,7 @@ mod sealed {
// Byte 0
DataLossProtect | UpfrontShutdownScript | GossipQueries,
// Byte 1
VariableLengthOnion | StaticRemoteKey | PaymentSecret,
VariableLengthOnion | StaticRemoteKey | PaymentSecret | GossipQueriesEx,
// Byte 2
BasicMPP | Wumbo | AnchorsNonzeroFeeHtlcTx | AnchorsZeroFeeHtlcTx,
// Byte 3
Expand Down Expand Up @@ -373,6 +375,9 @@ mod sealed {
"Feature flags for `var_onion_optin`.", set_variable_length_onion_optional,
set_variable_length_onion_required, supports_variable_length_onion,
requires_variable_length_onion);
define_feature!(11, GossipQueriesEx, [InitContext, NodeContext],
"Feature flags for `gossip_queries_ex`.", set_gossip_queries_ex_optional, set_gossip_queries_ex_required,
supports_gossip_queries_ex, requires_gossip_queries_ex);
define_feature!(13, StaticRemoteKey, [InitContext, NodeContext, ChannelTypeContext],
"Feature flags for `option_static_remotekey`.", set_static_remote_key_optional,
set_static_remote_key_required, supports_static_remote_key, requires_static_remote_key);
Expand Down
35 changes: 35 additions & 0 deletions lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
//! call into the provided message handlers (probably a ChannelManager and P2PGossipSync) with
//! messages they should handle, and encoding/sending response messages.

use rand::seq::SliceRandom;

use bitcoin::blockdata::constants::ChainHash;
use bitcoin::secp256k1::{self, Secp256k1, SecretKey, PublicKey};

Expand Down Expand Up @@ -2279,6 +2281,39 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
}
}

// TODO: those two methods are ugly AF, but I need them

/// Send data to a node given its node id.
///
pub fn send_to_node<M: wire::Type>(&self, node_id: PublicKey, message: &M) {
let descriptors = self.node_id_to_descriptor.lock().unwrap();
if let Some(descriptor) = descriptors.get(&node_id) {
let peers = self.peers.read().unwrap();

if let Some(peer) = peers.get(descriptor) {
let mut p = peer.lock().unwrap();
self.enqueue_message(&mut p, message);
}
}
}

/// Send data to a random node.
///
pub fn send_to_random_node<M: wire::Type>(&self, message: &M) {
let peers = self.peers.read().unwrap();

let keys: Vec<&Descriptor> = peers.keys().collect();
let mut rng = rand::thread_rng();

if let Some(random_key) = keys.choose(&mut rng) {
if let Some(peer) = peers.get(*random_key) {
let mut p = peer.lock().unwrap();
log_trace!(self.logger, "!!!!Enqueueing message {:?} to {}", message, log_pubkey!(p.their_node_id.unwrap().0));
self.enqueue_message(&mut p, message);
}
}
}

/// Disconnects all currently-connected peers. This is useful on platforms where there may be
/// an indication that TCP sockets have stalled even if we weren't around to time them out
/// using regular ping/pongs.
Expand Down

0 comments on commit c6dbeca

Please sign in to comment.