Skip to content

Commit 0b77008

Browse files
authored
Merge pull request #1500 from arik-so/2022-05-network-graph-rapid-sync-timestamp
Add optional last_rapid_gossip_sync_timestamp field to NetworkGraph to enable optimized differential rapid syncing.
2 parents 994fa07 + c3bbfe5 commit 0b77008

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

lightning/src/routing/network_graph.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ impl Readable for NodeId {
123123

124124
/// Represents the network as nodes and channels between them
125125
pub struct NetworkGraph {
126+
/// The unix timestamp in UTC provided by the most recent rapid gossip sync
127+
/// It will be set by the rapid sync process after every sync completion
128+
pub last_rapid_gossip_sync_timestamp: Option<u32>,
126129
genesis_hash: BlockHash,
127130
// Lock order: channels -> nodes
128131
channels: RwLock<BTreeMap<u64, ChannelInfo>>,
@@ -137,6 +140,7 @@ impl Clone for NetworkGraph {
137140
genesis_hash: self.genesis_hash.clone(),
138141
channels: RwLock::new(channels.clone()),
139142
nodes: RwLock::new(nodes.clone()),
143+
last_rapid_gossip_sync_timestamp: self.last_rapid_gossip_sync_timestamp.clone(),
140144
}
141145
}
142146
}
@@ -990,7 +994,9 @@ impl Writeable for NetworkGraph {
990994
node_info.write(writer)?;
991995
}
992996

993-
write_tlv_fields!(writer, {});
997+
write_tlv_fields!(writer, {
998+
(1, self.last_rapid_gossip_sync_timestamp, option),
999+
});
9941000
Ok(())
9951001
}
9961002
}
@@ -1014,12 +1020,17 @@ impl Readable for NetworkGraph {
10141020
let node_info = Readable::read(reader)?;
10151021
nodes.insert(node_id, node_info);
10161022
}
1017-
read_tlv_fields!(reader, {});
1023+
1024+
let mut last_rapid_gossip_sync_timestamp: Option<u32> = None;
1025+
read_tlv_fields!(reader, {
1026+
(1, last_rapid_gossip_sync_timestamp, option),
1027+
});
10181028

10191029
Ok(NetworkGraph {
10201030
genesis_hash,
10211031
channels: RwLock::new(channels),
10221032
nodes: RwLock::new(nodes),
1033+
last_rapid_gossip_sync_timestamp,
10231034
})
10241035
}
10251036
}
@@ -1053,6 +1064,7 @@ impl NetworkGraph {
10531064
genesis_hash,
10541065
channels: RwLock::new(BTreeMap::new()),
10551066
nodes: RwLock::new(BTreeMap::new()),
1067+
last_rapid_gossip_sync_timestamp: None,
10561068
}
10571069
}
10581070

@@ -2359,6 +2371,18 @@ mod tests {
23592371
assert!(<NetworkGraph>::read(&mut io::Cursor::new(&w.0)).unwrap() == network_graph);
23602372
}
23612373

2374+
#[test]
2375+
fn network_graph_tlv_serialization() {
2376+
let mut network_graph = create_network_graph();
2377+
network_graph.last_rapid_gossip_sync_timestamp.replace(42);
2378+
2379+
let mut w = test_utils::TestVecWriter(Vec::new());
2380+
network_graph.write(&mut w).unwrap();
2381+
let reassembled_network_graph: NetworkGraph = Readable::read(&mut io::Cursor::new(&w.0)).unwrap();
2382+
assert!(reassembled_network_graph == network_graph);
2383+
assert_eq!(reassembled_network_graph.last_rapid_gossip_sync_timestamp.unwrap(), 42);
2384+
}
2385+
23622386
#[test]
23632387
#[cfg(feature = "std")]
23642388
fn calling_sync_routing_table() {

0 commit comments

Comments
 (0)