Skip to content

Commit 4ccf445

Browse files
committed
Implement EventHandler for NetworkGraph
Instead of implementing EventHandler for P2PGossipSync, implement it on NetworkGraph. This allows RapidGossipSync to handle events, too, by delegating to its NetworkGraph.
1 parent 67736b7 commit 4ccf445

File tree

3 files changed

+40
-46
lines changed

3 files changed

+40
-46
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ impl<
112112
> EventHandler for DecoratingEventHandler<E, P, G, A, L>
113113
where A::Target: chain::Access, L::Target: Logger {
114114
fn handle_event(&self, event: &Event) {
115-
if let Some(event_handler) = &self.p2p_gossip_sync {
116-
event_handler.handle_event(event);
115+
if let Some(gossip_sync) = &self.p2p_gossip_sync {
116+
gossip_sync.network_graph().handle_event(event);
117117
}
118118
self.event_handler.handle_event(event);
119119
}
@@ -211,7 +211,10 @@ impl BackgroundProcessor {
211211
let stop_thread = Arc::new(AtomicBool::new(false));
212212
let stop_thread_clone = stop_thread.clone();
213213
let handle = thread::spawn(move || -> Result<(), std::io::Error> {
214-
let event_handler = DecoratingEventHandler { event_handler, p2p_gossip_sync: p2p_gossip_sync.as_ref().map(|t| t.deref()) };
214+
let event_handler = DecoratingEventHandler {
215+
event_handler,
216+
p2p_gossip_sync: p2p_gossip_sync.as_ref().map(|t| t.deref()),
217+
};
215218

216219
log_trace!(logger, "Calling ChannelManager's timer_tick_occurred on startup");
217220
channel_manager.timer_tick_occurred();

lightning/src/routing/gossip.rs

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub struct NetworkGraph<L: Deref> where L::Target: Logger {
126126
secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
127127
last_rapid_gossip_sync_timestamp: Mutex<Option<u32>>,
128128
genesis_hash: BlockHash,
129-
_logger: L,
129+
logger: L,
130130
// Lock order: channels -> nodes
131131
channels: RwLock<BTreeMap<u64, ChannelInfo>>,
132132
nodes: RwLock<BTreeMap<NodeId, NodeInfo>>,
@@ -184,17 +184,6 @@ impl_writeable_tlv_based_enum_upgradable!(NetworkUpdate,
184184
},
185185
);
186186

187-
impl<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref> EventHandler for P2PGossipSync<G, C, L>
188-
where C::Target: chain::Access, L::Target: Logger {
189-
fn handle_event(&self, event: &Event) {
190-
if let Event::PaymentPathFailed { payment_hash: _, rejected_by_dest: _, network_update, .. } = event {
191-
if let Some(network_update) = network_update {
192-
self.handle_network_update(network_update);
193-
}
194-
}
195-
}
196-
}
197-
198187
/// Receives and validates network updates from peers,
199188
/// stores authentic and relevant data as a network graph.
200189
/// This network graph is then used for routing payments.
@@ -257,27 +246,32 @@ where C::Target: chain::Access, L::Target: Logger
257246
false
258247
}
259248
}
249+
}
260250

261-
/// Applies changes to the [`NetworkGraph`] from the given update.
262-
fn handle_network_update(&self, update: &NetworkUpdate) {
263-
match *update {
264-
NetworkUpdate::ChannelUpdateMessage { ref msg } => {
265-
let short_channel_id = msg.contents.short_channel_id;
266-
let is_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
267-
let status = if is_enabled { "enabled" } else { "disabled" };
268-
log_debug!(self.logger, "Updating channel with channel_update from a payment failure. Channel {} is {}.", short_channel_id, status);
269-
let _ = self.network_graph.update_channel(msg);
270-
},
271-
NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
272-
let action = if is_permanent { "Removing" } else { "Disabling" };
273-
log_debug!(self.logger, "{} channel graph entry for {} due to a payment failure.", action, short_channel_id);
274-
self.network_graph.channel_failed(short_channel_id, is_permanent);
275-
},
276-
NetworkUpdate::NodeFailure { ref node_id, is_permanent } => {
277-
let action = if is_permanent { "Removing" } else { "Disabling" };
278-
log_debug!(self.logger, "{} node graph entry for {} due to a payment failure.", action, node_id);
279-
self.network_graph.node_failed(node_id, is_permanent);
280-
},
251+
impl<L: Deref> EventHandler for NetworkGraph<L> where L::Target: Logger {
252+
fn handle_event(&self, event: &Event) {
253+
if let Event::PaymentPathFailed { payment_hash: _, rejected_by_dest: _, network_update, .. } = event {
254+
if let Some(network_update) = network_update {
255+
match *network_update {
256+
NetworkUpdate::ChannelUpdateMessage { ref msg } => {
257+
let short_channel_id = msg.contents.short_channel_id;
258+
let is_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
259+
let status = if is_enabled { "enabled" } else { "disabled" };
260+
log_debug!(self.logger, "Updating channel with channel_update from a payment failure. Channel {} is {}.", short_channel_id, status);
261+
let _ = self.update_channel(msg);
262+
},
263+
NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
264+
let action = if is_permanent { "Removing" } else { "Disabling" };
265+
log_debug!(self.logger, "{} channel graph entry for {} due to a payment failure.", action, short_channel_id);
266+
self.channel_failed(short_channel_id, is_permanent);
267+
},
268+
NetworkUpdate::NodeFailure { ref node_id, is_permanent } => {
269+
let action = if is_permanent { "Removing" } else { "Disabling" };
270+
log_debug!(self.logger, "{} node graph entry for {} due to a payment failure.", action, node_id);
271+
self.node_failed(node_id, is_permanent);
272+
},
273+
}
274+
}
281275
}
282276
}
283277
}
@@ -988,7 +982,7 @@ impl<L: Deref> Writeable for NetworkGraph<L> where L::Target: Logger {
988982
}
989983

990984
impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
991-
fn read<R: io::Read>(reader: &mut R, _logger: L) -> Result<NetworkGraph<L>, DecodeError> {
985+
fn read<R: io::Read>(reader: &mut R, logger: L) -> Result<NetworkGraph<L>, DecodeError> {
992986
let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
993987

994988
let genesis_hash: BlockHash = Readable::read(reader)?;
@@ -1015,7 +1009,7 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
10151009
Ok(NetworkGraph {
10161010
secp_ctx: Secp256k1::verification_only(),
10171011
genesis_hash,
1018-
_logger,
1012+
logger,
10191013
channels: RwLock::new(channels),
10201014
nodes: RwLock::new(nodes),
10211015
last_rapid_gossip_sync_timestamp: Mutex::new(last_rapid_gossip_sync_timestamp),
@@ -1047,11 +1041,11 @@ impl<L: Deref> PartialEq for NetworkGraph<L> where L::Target: Logger {
10471041

10481042
impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
10491043
/// Creates a new, empty, network graph.
1050-
pub fn new(genesis_hash: BlockHash, _logger: L) -> NetworkGraph<L> {
1044+
pub fn new(genesis_hash: BlockHash, logger: L) -> NetworkGraph<L> {
10511045
Self {
10521046
secp_ctx: Secp256k1::verification_only(),
10531047
genesis_hash,
1054-
_logger,
1048+
logger,
10551049
channels: RwLock::new(BTreeMap::new()),
10561050
nodes: RwLock::new(BTreeMap::new()),
10571051
last_rapid_gossip_sync_timestamp: Mutex::new(None),
@@ -2055,10 +2049,8 @@ mod tests {
20552049
#[test]
20562050
fn handling_network_update() {
20572051
let logger = test_utils::TestLogger::new();
2058-
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
20592052
let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
20602053
let network_graph = NetworkGraph::new(genesis_hash, &logger);
2061-
let gossip_sync = P2PGossipSync::new(&network_graph, Some(chain_source.clone()), &logger);
20622054
let secp_ctx = Secp256k1::new();
20632055

20642056
let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
@@ -2081,7 +2073,7 @@ mod tests {
20812073
let valid_channel_update = get_signed_channel_update(|_| {}, node_1_privkey, &secp_ctx);
20822074
assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_none());
20832075

2084-
gossip_sync.handle_event(&Event::PaymentPathFailed {
2076+
network_graph.handle_event(&Event::PaymentPathFailed {
20852077
payment_id: None,
20862078
payment_hash: PaymentHash([0; 32]),
20872079
rejected_by_dest: false,
@@ -2108,7 +2100,7 @@ mod tests {
21082100
}
21092101
};
21102102

2111-
gossip_sync.handle_event(&Event::PaymentPathFailed {
2103+
network_graph.handle_event(&Event::PaymentPathFailed {
21122104
payment_id: None,
21132105
payment_hash: PaymentHash([0; 32]),
21142106
rejected_by_dest: false,
@@ -2133,7 +2125,7 @@ mod tests {
21332125
}
21342126

21352127
// Permanent closing deletes a channel
2136-
gossip_sync.handle_event(&Event::PaymentPathFailed {
2128+
network_graph.handle_event(&Event::PaymentPathFailed {
21372129
payment_id: None,
21382130
payment_hash: PaymentHash([0; 32]),
21392131
rejected_by_dest: false,

lightning/src/util/events.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,9 @@ pub enum Event {
337337
/// payment route.
338338
///
339339
/// Should be applied to the [`NetworkGraph`] so that routing decisions can take into
340-
/// account the update. [`P2PGossipSync`] is capable of doing this.
340+
/// account the update.
341341
///
342342
/// [`NetworkGraph`]: crate::routing::gossip::NetworkGraph
343-
/// [`P2PGossipSync`]: crate::routing::gossip::P2PGossipSync
344343
network_update: Option<NetworkUpdate>,
345344
/// For both single-path and multi-path payments, this is set if all paths of the payment have
346345
/// failed. This will be set to false if (1) this is an MPP payment and (2) other parts of the

0 commit comments

Comments
 (0)