Skip to content

Commit

Permalink
Generate v1 and v2 dummy snapshots
Browse files Browse the repository at this point in the history
Previously, our dummy snapshots were unversioned. This commit ensures
that we generate no-op serializations for both currently supported
versions, and introduces unit tests to ascertain their validity.
  • Loading branch information
arik-so committed Jan 31, 2025
1 parent f37e149 commit 9346b63
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ pub(crate) async fn connect_to_db() -> Client {
/// of our granularity constant. Note that for that purpose, this method could be very dangerous,
/// because if consumed, the `timestamp` value calculated here will overwrite the timestamp that
/// the client previously had, which could result in duplicated or omitted gossip down the line.
fn serialize_empty_blob(current_timestamp: u64) -> Vec<u8> {
fn serialize_empty_blob(current_timestamp: u64, serialization_version: u8) -> Vec<u8> {
let mut blob = GOSSIP_PREFIX.to_vec();
serialization_version.write(&mut blob).unwrap();

let network = config::network();
let chain_hash = ChainHash::using_genesis_block(network);
Expand All @@ -170,6 +171,10 @@ fn serialize_empty_blob(current_timestamp: u64) -> Vec<u8> {
let blob_timestamp = Snapshotter::<Arc<RGSSLogger>>::round_down_to_nearest_multiple(current_timestamp, SYMLINK_GRANULARITY_INTERVAL as u64) as u32;
blob_timestamp.write(&mut blob).unwrap();

if serialization_version >= 2 {
0u8.write(&mut blob).unwrap(); // default node feature count
}

0u32.write(&mut blob).unwrap(); // node count
0u32.write(&mut blob).unwrap(); // announcement count
0u32.write(&mut blob).unwrap(); // update count
Expand Down
9 changes: 6 additions & 3 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,12 @@ impl<L: Deref + Clone> Snapshotter<L> where L::Target: Logger {
{
// create dummy symlink
let dummy_filename = "empty_delta.lngossip";
let dummy_snapshot = super::serialize_empty_blob(reference_timestamp);
let dummy_snapshot_path = format!("{}/{}", pending_snapshot_directory, dummy_filename);
fs::write(&dummy_snapshot_path, dummy_snapshot).unwrap();
let dummy_snapshot_v1 = super::serialize_empty_blob(reference_timestamp, 1);
let dummy_snapshot_v2 = super::serialize_empty_blob(reference_timestamp, 2);
let dummy_snapshot_path_v1 = format!("{}/{}", pending_snapshot_directory, dummy_filename);
let dummy_snapshot_path_v2 = format!("{}/v2/{}", pending_snapshot_directory, dummy_filename);
fs::write(&dummy_snapshot_path_v1, dummy_snapshot_v1).unwrap();
fs::write(&dummy_snapshot_path_v2, dummy_snapshot_v2).unwrap();

let dummy_symlink_path = format!("{}/{}.bin", pending_symlink_directory, reference_timestamp);
let relative_dummy_snapshot_path = format!("{}/{}", relative_symlink_to_snapshot_path, dummy_filename);
Expand Down
15 changes: 14 additions & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use lightning::ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement,
use lightning::routing::gossip::{NetworkGraph, NodeAlias, NodeId};
use lightning::util::ser::Writeable;
use lightning_rapid_gossip_sync::RapidGossipSync;
use crate::{calculate_delta, config, serialize_delta};
use crate::{calculate_delta, config, serialize_delta, serialize_empty_blob};
use crate::persistence::GossipPersister;
use crate::snapshot::Snapshotter;
use crate::types::{GossipMessage, tests::TestLogger};
Expand Down Expand Up @@ -221,6 +221,19 @@ async fn test_persistence_runtime() {
}


#[test]
fn test_no_op() {
let logger = Arc::new(TestLogger::with_id("test_no_op".to_string()));
for serialization_version in 1..3 {
let serialization = serialize_empty_blob(current_time() as u64, serialization_version);

let client_graph = NetworkGraph::new(Network::Bitcoin, logger.clone());
let client_graph_arc = Arc::new(client_graph);
let rgs = RapidGossipSync::new(client_graph_arc.clone(), logger.clone());
rgs.update_network_graph(&serialization).unwrap();
}
}

#[tokio::test]
async fn test_trivial_setup() {
let _sanitizer = SchemaSanitizer::new();
Expand Down

0 comments on commit 9346b63

Please sign in to comment.