-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathgossip.rs
More file actions
102 lines (90 loc) · 3.38 KB
/
gossip.rs
File metadata and controls
102 lines (90 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// This file is Copyright its original authors, visible in version control history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
// accordance with one or both of these licenses.
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use lightning_block_sync::gossip::GossipVerifier;
use crate::chain::ChainSource;
use crate::config::{RGS_SNAPSHOT_MAX_SIZE, RGS_SYNC_TIMEOUT_SECS};
use crate::logger::{log_error, log_trace, LdkLogger, Logger};
use crate::runtime::{Runtime, RuntimeSpawner};
use crate::types::{GossipSync, Graph, P2PGossipSync, RapidGossipSync};
use crate::Error;
pub(crate) enum GossipSource {
P2PNetwork {
gossip_sync: Arc<P2PGossipSync>,
},
RapidGossipSync {
gossip_sync: Arc<RapidGossipSync>,
server_url: String,
latest_sync_timestamp: AtomicU32,
logger: Arc<Logger>,
},
}
impl GossipSource {
pub fn new_p2p(
network_graph: Arc<Graph>, chain_source: Arc<ChainSource>, runtime: Arc<Runtime>,
logger: Arc<Logger>,
) -> Self {
let verifier = chain_source.as_utxo_source().map(|utxo_source| {
Arc::new(GossipVerifier::new(Arc::new(utxo_source), RuntimeSpawner::new(runtime)))
});
let gossip_sync = Arc::new(P2PGossipSync::new(network_graph, verifier, logger));
Self::P2PNetwork { gossip_sync }
}
pub fn new_rgs(
server_url: String, latest_sync_timestamp: u32, network_graph: Arc<Graph>,
logger: Arc<Logger>,
) -> Self {
let gossip_sync = Arc::new(RapidGossipSync::new(network_graph, Arc::clone(&logger)));
let latest_sync_timestamp = AtomicU32::new(latest_sync_timestamp);
Self::RapidGossipSync { gossip_sync, server_url, latest_sync_timestamp, logger }
}
pub fn is_rgs(&self) -> bool {
matches!(self, Self::RapidGossipSync { .. })
}
pub fn as_gossip_sync(&self) -> GossipSync {
match self {
Self::RapidGossipSync { gossip_sync, .. } => GossipSync::Rapid(Arc::clone(gossip_sync)),
Self::P2PNetwork { gossip_sync, .. } => GossipSync::P2P(Arc::clone(gossip_sync)),
}
}
pub async fn update_rgs_snapshot(&self) -> Result<u32, Error> {
match self {
Self::P2PNetwork { gossip_sync: _, .. } => Ok(0),
Self::RapidGossipSync { gossip_sync, server_url, latest_sync_timestamp, logger } => {
let query_timestamp = latest_sync_timestamp.load(Ordering::Acquire);
let query_url = format!("{}/{}", server_url, query_timestamp);
let query = bitreq::get(query_url)
.with_max_body_size(Some(RGS_SNAPSHOT_MAX_SIZE))
.with_timeout(RGS_SYNC_TIMEOUT_SECS);
let response = query.send_async().await.map_err(|e| {
log_error!(logger, "Failed to retrieve RGS gossip update: {e}");
Error::GossipUpdateTimeout
})?;
match response.status_code {
200 => {
let new_latest_sync_timestamp =
gossip_sync.update_network_graph(response.as_bytes()).map_err(|e| {
log_trace!(
logger,
"Failed to update network graph with RGS data: {:?}",
e
);
Error::GossipUpdateFailed
})?;
latest_sync_timestamp.store(new_latest_sync_timestamp, Ordering::Release);
Ok(new_latest_sync_timestamp)
},
code => {
log_trace!(logger, "Failed to retrieve RGS gossip update: HTTP {}", code);
Err(Error::GossipUpdateFailed)
},
}
},
}
}
}