From 9ea18e249e144b2bfebdf0920d35270ddc5d7709 Mon Sep 17 00:00:00 2001 From: Arik Sosman Date: Tue, 4 Apr 2023 16:20:58 -0700 Subject: [PATCH] =?utf8?q?Create=20a=20utility=20method=20to=20generate=20?= =?utf8?q?no=C3=B6p=20RGS=20blobs.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 2 +- src/lib.rs | 39 ++++++++++++++++++++++++++++++++++++++- src/snapshot.rs | 16 ++++++++++++++-- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c2c689b..729c080 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ lightning = { version = "0.0.114" } lightning-block-sync = { version = "0.0.114", features=["rest-client"] } lightning-net-tokio = { version = "0.0.114" } tokio = { version = "1.14.1", features = ["full"] } -tokio-postgres = { version="0.7.5" } +tokio-postgres = { version="=0.7.5" } futures = "0.3" [profile.release] diff --git a/src/lib.rs b/src/lib.rs index f8a4231..b655c56 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,6 +36,12 @@ mod config; mod hex_utils; mod verifier; +/// The purpose of this prefix is to identify the serialization format, should other rapid gossip +/// sync formats arise in the future. +/// +/// The fourth byte is the protocol version in case our format gets updated. +const GOSSIP_PREFIX: [u8; 4] = [76, 68, 75, 1]; + pub struct RapidSyncProcessor { network_graph: Arc>, } @@ -100,6 +106,37 @@ impl RapidSyncProcessor { } } +/// This method generates a no-op blob that can be used as a delta where none exists. +/// +/// The primary purpose of this method is the scenario of a client retrieving and processing a +/// given snapshot, and then immediately retrieving the would-be next snapshot at the timestamp +/// indicated by the one that was just processed. +/// Previously, there would not be a new snapshot to be processed for that particular timestamp yet, +/// and the server would return a 404 error. +/// +/// In principle, this method could also be used to address another unfortunately all too common +/// pitfall: requesting snapshots from intermediate timestamps, i. e. those that are not multiples +/// 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 { + let mut blob = GOSSIP_PREFIX.to_vec(); + + let network = config::network(); + let genesis_block = bitcoin::blockdata::constants::genesis_block(network); + let chain_hash = genesis_block.block_hash(); + chain_hash.write(&mut blob).unwrap(); + + let blob_timestamp = Snapshotter::round_down_to_nearest_multiple(current_timestamp, config::SNAPSHOT_CALCULATION_INTERVAL as u64) as u32; + blob_timestamp.write(&mut blob).unwrap(); + + 0u32.write(&mut blob).unwrap(); // node count + 0u32.write(&mut blob).unwrap(); // announcement count + 0u32.write(&mut blob).unwrap(); // update count + + blob +} + async fn serialize_delta(network_graph: Arc>, last_sync_timestamp: u32, consider_intermediate_updates: bool) -> SerializedResponse { let (client, connection) = lookup::connect_to_db().await; @@ -191,7 +228,7 @@ async fn serialize_delta(network_graph: Arc>, last_sync // some stats let message_count = announcement_count + update_count; - let mut prefixed_output = vec![76, 68, 75, 1]; + let mut prefixed_output = GOSSIP_PREFIX.to_vec(); // always write the chain hash serialization_details.chain_hash.write(&mut prefixed_output).unwrap(); diff --git a/src/snapshot.rs b/src/snapshot.rs index ae28233..6894dbb 100644 --- a/src/snapshot.rs +++ b/src/snapshot.rs @@ -87,8 +87,20 @@ impl Snapshotter { } } + { + // 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_symlink_path = format!("{}/{}.bin", pending_symlink_directory, reference_timestamp); + println!("Symlinking dummy: {} -> {}", dummy_symlink_path, dummy_snapshot_path); + symlink(&dummy_snapshot_path, &dummy_symlink_path).unwrap(); + } + for i in 0..10_001u64 { - // let's create symlinks + // let's create non-dummy-symlinks // first, determine which snapshot range should be referenced let referenced_day_range = if i == 0 { @@ -141,7 +153,7 @@ impl Snapshotter { } } - fn round_down_to_nearest_multiple(number: u64, multiple: u64) -> u64 { + pub(super) fn round_down_to_nearest_multiple(number: u64, multiple: u64) -> u64 { let round_multiple_delta = number % multiple; number - round_multiple_delta } -- 2.39.5