EnforcingSigner,
Arc<chainmonitor::ChainMonitor<EnforcingSigner, Arc<dyn chain::Filter>, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<TestPersister>>>,
Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>, Arc<dyn Logger>>;
-type PeerMan<'a> = PeerManager<Peer<'a>, Arc<ChannelMan>, Arc<P2PGossipSync<Arc<NetworkGraph>, Arc<dyn chain::Access>, Arc<dyn Logger>>>, Arc<dyn Logger>, IgnoringMessageHandler>;
+type PeerMan<'a> = PeerManager<Peer<'a>, Arc<ChannelMan>, Arc<P2PGossipSync<Arc<NetworkGraph<Arc<dyn Logger>>>, Arc<dyn chain::Access>, Arc<dyn Logger>>>, Arc<dyn Logger>, IgnoringMessageHandler>;
struct MoneyLossDetector<'a> {
manager: Arc<ChannelMan>,
// it's easier to just increment the counter here so the keys don't change.
keys_manager.counter.fetch_sub(1, Ordering::AcqRel);
let our_id = PublicKey::from_secret_key(&Secp256k1::signing_only(), &keys_manager.get_node_secret(Recipient::Node).unwrap());
- let network_graph = Arc::new(NetworkGraph::new(genesis_block(network).block_hash()));
+ let network_graph = Arc::new(NetworkGraph::new(genesis_block(network).block_hash(), Arc::clone(&logger)));
let gossip_sync = Arc::new(P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger)));
let scorer = FixedPenaltyScorer::with_penalty(0);
final_cltv_expiry_delta: 42,
};
let random_seed_bytes: [u8; 32] = keys_manager.get_secure_random_bytes();
- let route = match find_route(&our_id, ¶ms, &network_graph, None, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ let route = match find_route(&our_id, ¶ms, &network_graph.read_only(), None, Arc::clone(&logger), &scorer, &random_seed_bytes) {
Ok(route) => route,
Err(_) => return,
};
final_cltv_expiry_delta: 42,
};
let random_seed_bytes: [u8; 32] = keys_manager.get_secure_random_bytes();
- let mut route = match find_route(&our_id, ¶ms, &network_graph, None, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ let mut route = match find_route(&our_id, ¶ms, &network_graph.read_only(), None, Arc::clone(&logger), &scorer, &random_seed_bytes) {
Ok(route) => route,
Err(_) => return,
};
// Imports that need to be added manually
+use lightning::util::logger::Logger;
use lightning_rapid_gossip_sync::RapidGossipSync;
+
use utils::test_logger;
+use std::sync::Arc;
+
/// Actual fuzz test, method signature and name are fixed
-fn do_test(data: &[u8]) {
+fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
let block_hash = bitcoin::BlockHash::default();
- let network_graph = lightning::routing::gossip::NetworkGraph::new(block_hash);
+ let logger: Arc<dyn Logger> = Arc::new(test_logger::TestLogger::new("".to_owned(), out));
+ let network_graph = lightning::routing::gossip::NetworkGraph::new(block_hash, logger);
let rapid_sync = RapidGossipSync::new(&network_graph);
let _ = rapid_sync.update_network_graph(data);
}
/// Method that needs to be added manually, {name}_test
-pub fn process_network_graph_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
- do_test(data);
+pub fn process_network_graph_test<Out: test_logger::Output>(data: &[u8], out: Out) {
+ do_test(data, out);
}
/// Method that needs to be added manually, {name}_run
#[no_mangle]
pub extern "C" fn process_network_graph_run(data: *const u8, datalen: usize) {
- do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
+ do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});
}
let logger: Arc<dyn Logger> = Arc::new(test_logger::TestLogger::new("".to_owned(), out));
let our_pubkey = get_pubkey!();
- let net_graph = NetworkGraph::new(genesis_block(Network::Bitcoin).header.block_hash());
+ let net_graph = NetworkGraph::new(genesis_block(Network::Bitcoin).header.block_hash(), Arc::clone(&logger));
let mut node_pks = HashSet::new();
let mut scid = 42;
final_value_msat: slice_to_be64(get_slice!(8)),
final_cltv_expiry_delta: slice_to_be32(get_slice!(4)),
};
- let _ = find_route(&our_pubkey, &route_params, &net_graph,
+ let _ = find_route(&our_pubkey, &route_params, &net_graph.read_only(),
first_hops.map(|c| c.iter().collect::<Vec<_>>()).as_ref().map(|a| a.as_slice()),
Arc::clone(&logger), &scorer, &random_seed_bytes);
}
struct DecoratingEventHandler<
E: EventHandler,
P: Deref<Target = P2PGossipSync<G, A, L>>,
- G: Deref<Target = NetworkGraph>,
+ G: Deref<Target = NetworkGraph<L>>,
A: Deref,
L: Deref,
>
impl<
E: EventHandler,
P: Deref<Target = P2PGossipSync<G, A, L>>,
- G: Deref<Target = NetworkGraph>,
+ G: Deref<Target = NetworkGraph<L>>,
A: Deref,
L: Deref,
> EventHandler for DecoratingEventHandler<E, P, G, A, L>
T: 'static + Deref + Send + Sync,
K: 'static + Deref + Send + Sync,
F: 'static + Deref + Send + Sync,
- G: 'static + Deref<Target = NetworkGraph> + Send + Sync,
+ G: 'static + Deref<Target = NetworkGraph<L>> + Send + Sync,
L: 'static + Deref + Send + Sync,
P: 'static + Deref + Send + Sync,
Descriptor: 'static + SocketDescriptor + Send + Sync,
PM: 'static + Deref<Target = PeerManager<Descriptor, CMH, RMH, L, UMH>> + Send + Sync,
S: 'static + Deref<Target = SC> + Send + Sync,
SC: WriteableScore<'a>,
- RGS: 'static + Deref<Target = RapidGossipSync<G>> + Send
+ RGS: 'static + Deref<Target = RapidGossipSync<G, L>> + Send
>(
persister: PS, event_handler: EH, chain_monitor: M, channel_manager: CM,
p2p_gossip_sync: Option<PGS>, peer_manager: PM, logger: L, scorer: Option<S>,
struct Node {
node: Arc<SimpleArcChannelManager<ChainMonitor, test_utils::TestBroadcaster, test_utils::TestFeeEstimator, test_utils::TestLogger>>,
- p2p_gossip_sync: Option<Arc<P2PGossipSync<Arc<NetworkGraph>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>>>,
+ p2p_gossip_sync: Option<Arc<P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>>>,
peer_manager: Arc<PeerManager<TestDescriptor, Arc<test_utils::TestChannelMessageHandler>, Arc<test_utils::TestRoutingMessageHandler>, Arc<test_utils::TestLogger>, IgnoringMessageHandler>>,
chain_monitor: Arc<ChainMonitor>,
persister: Arc<FilesystemPersister>,
tx_broadcaster: Arc<test_utils::TestBroadcaster>,
- network_graph: Arc<NetworkGraph>,
+ network_graph: Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
logger: Arc<test_utils::TestLogger>,
best_block: BestBlock,
scorer: Arc<Mutex<FixedPenaltyScorer>>,
- rapid_gossip_sync: Option<Arc<RapidGossipSync<Arc<NetworkGraph>>>>
+ rapid_gossip_sync: Option<Arc<RapidGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestLogger>>>>,
}
impl Drop for Node {
let best_block = BestBlock::from_genesis(network);
let params = ChainParameters { network, best_block };
let manager = Arc::new(ChannelManager::new(fee_estimator.clone(), chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), keys_manager.clone(), UserConfig::default(), params));
- let network_graph = Arc::new(NetworkGraph::new(genesis_block.header.block_hash()));
+ let network_graph = Arc::new(NetworkGraph::new(genesis_block.header.block_hash(), logger.clone()));
let p2p_gossip_sync = Some(Arc::new(P2PGossipSync::new(network_graph.clone(), Some(chain_source.clone()), logger.clone())));
let msg_handler = MessageHandler { chan_handler: Arc::new(test_utils::TestChannelMessageHandler::new()), route_handler: Arc::new(test_utils::TestRoutingMessageHandler::new() )};
let peer_manager = Arc::new(PeerManager::new(msg_handler, keys_manager.get_node_secret(Recipient::Node).unwrap(), &seed, logger.clone(), IgnoringMessageHandler{}));
}
/// A [`Router`] implemented using [`find_route`].
-pub struct DefaultRouter<G: Deref<Target = NetworkGraph>, L: Deref> where L::Target: Logger {
+pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref> where L::Target: Logger {
network_graph: G,
logger: L,
random_seed_bytes: Mutex<[u8; 32]>,
}
-impl<G: Deref<Target = NetworkGraph>, L: Deref> DefaultRouter<G, L> where L::Target: Logger {
+impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> DefaultRouter<G, L> where L::Target: Logger {
/// Creates a new router using the given [`NetworkGraph`], a [`Logger`], and a randomness source
/// `random_seed_bytes`.
pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32]) -> Self {
}
}
-impl<G: Deref<Target = NetworkGraph>, L: Deref, S: Score> Router<S> for DefaultRouter<G, L>
+impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Score> Router<S> for DefaultRouter<G, L>
where L::Target: Logger {
fn find_route(
&self, payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash,
first_hops: Option<&[&ChannelDetails]>, scorer: &S
) -> Result<Route, LightningError> {
+ let network_graph = self.network_graph.read_only();
let random_seed_bytes = {
let mut locked_random_seed_bytes = self.random_seed_bytes.lock().unwrap();
*locked_random_seed_bytes = sha256::Hash::hash(&*locked_random_seed_bytes).into_inner();
*locked_random_seed_bytes
};
- find_route(payer, params, &*self.network_graph, first_hops, &*self.logger, scorer, &random_seed_bytes)
+ find_route(payer, params, &network_graph, first_hops, &*self.logger, scorer, &random_seed_bytes)
}
}
final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
};
let first_hops = nodes[0].node.list_usable_channels();
- let network_graph = node_cfgs[0].network_graph;
+ let network_graph = &node_cfgs[0].network_graph;
let logger = test_utils::TestLogger::new();
let scorer = test_utils::TestScorer::with_penalty(0);
let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
let route = find_route(
- &nodes[0].node.get_our_node_id(), &route_params, network_graph,
+ &nodes[0].node.get_our_node_id(), &route_params, &network_graph.read_only(),
Some(&first_hops.iter().collect::<Vec<_>>()), &logger, &scorer, &random_seed_bytes
).unwrap();
final_cltv_expiry_delta: invoice.min_final_cltv_expiry() as u32,
};
let first_hops = nodes[0].node.list_usable_channels();
- let network_graph = node_cfgs[0].network_graph;
+ let network_graph = &node_cfgs[0].network_graph;
let logger = test_utils::TestLogger::new();
let scorer = test_utils::TestScorer::with_penalty(0);
let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
let route = find_route(
- &nodes[0].node.get_our_node_id(), ¶ms, network_graph,
+ &nodes[0].node.get_our_node_id(), ¶ms, &network_graph.read_only(),
Some(&first_hops.iter().collect::<Vec<_>>()), &logger, &scorer, &random_seed_bytes
).unwrap();
let (payment_event, fwd_idx) = {
//! use lightning::routing::gossip::NetworkGraph;
//! use lightning_rapid_gossip_sync::RapidGossipSync;
//!
+//! # use lightning::util::logger::{Logger, Record};
+//! # struct FakeLogger {}
+//! # impl Logger for FakeLogger {
+//! # fn log(&self, record: &Record) { unimplemented!() }
+//! # }
+//! # let logger = FakeLogger {};
+//!
//! let block_hash = genesis_block(Network::Bitcoin).header.block_hash();
-//! let network_graph = NetworkGraph::new(block_hash);
+//! let network_graph = NetworkGraph::new(block_hash, &logger);
//! let rapid_sync = RapidGossipSync::new(&network_graph);
//! let new_last_sync_timestamp_result = rapid_sync.sync_network_graph_with_file_path("./rapid_sync.lngossip");
//! ```
use std::sync::atomic::{AtomicBool, Ordering};
use lightning::routing::gossip::NetworkGraph;
+use lightning::util::logger::Logger;
use crate::error::GraphSyncError;
/// See [crate-level documentation] for usage.
///
/// [crate-level documentation]: crate
-pub struct RapidGossipSync<NG: Deref<Target=NetworkGraph>> {
+pub struct RapidGossipSync<NG: Deref<Target=NetworkGraph<L>>, L: Deref>
+where L::Target: Logger {
network_graph: NG,
is_initial_sync_complete: AtomicBool
}
-impl<NG: Deref<Target=NetworkGraph>> RapidGossipSync<NG> {
+impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L::Target: Logger {
/// Instantiate a new [`RapidGossipSync`] instance
pub fn new(network_graph: NG) -> Self {
Self {
use lightning::ln::msgs::DecodeError;
use lightning::routing::gossip::NetworkGraph;
+ use lightning::util::test_utils::TestLogger;
use crate::RapidGossipSync;
#[test]
let graph_sync_test_file = sync_test.get_test_file_path();
let block_hash = genesis_block(Network::Bitcoin).block_hash();
- let network_graph = NetworkGraph::new(block_hash);
+ let logger = TestLogger::new();
+ let network_graph = NetworkGraph::new(block_hash, &logger);
assert_eq!(network_graph.read_only().channels().len(), 0);
#[test]
fn measure_native_read_from_file() {
let block_hash = genesis_block(Network::Bitcoin).block_hash();
- let network_graph = NetworkGraph::new(block_hash);
+ let logger = TestLogger::new();
+ let network_graph = NetworkGraph::new(block_hash, &logger);
assert_eq!(network_graph.read_only().channels().len(), 0);
use lightning::ln::msgs::DecodeError;
use lightning::routing::gossip::NetworkGraph;
+ use lightning::util::test_utils::TestLogger;
use crate::RapidGossipSync;
#[bench]
fn bench_reading_full_graph_from_file(b: &mut Bencher) {
let block_hash = genesis_block(Network::Bitcoin).block_hash();
+ let logger = TestLogger::new();
b.iter(|| {
- let network_graph = NetworkGraph::new(block_hash);
+ let network_graph = NetworkGraph::new(block_hash, &logger);
let rapid_sync = RapidGossipSync::new(&network_graph);
let sync_result = rapid_sync.sync_network_graph_with_file_path("./res/full_graph.lngossip");
if let Err(crate::error::GraphSyncError::DecodeError(DecodeError::Io(io_error))) = &sync_result {
DecodeError, ErrorAction, LightningError, OptionalField, UnsignedChannelUpdate,
};
use lightning::routing::gossip::NetworkGraph;
+use lightning::util::logger::Logger;
use lightning::util::ser::{BigSize, Readable};
use crate::error::GraphSyncError;
/// avoid malicious updates being able to trigger excessive memory allocation.
const MAX_INITIAL_NODE_ID_VECTOR_CAPACITY: u32 = 50_000;
-impl<NG: Deref<Target=NetworkGraph>> RapidGossipSync<NG> {
+impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L::Target: Logger {
/// Update network graph from binary data.
/// Returns the last sync timestamp to be used the next time rapid sync data is queried.
///
use lightning::ln::msgs::DecodeError;
use lightning::routing::gossip::NetworkGraph;
+ use lightning::util::test_utils::TestLogger;
use crate::error::GraphSyncError;
use crate::RapidGossipSync;
#[test]
fn network_graph_fails_to_update_from_clipped_input() {
let block_hash = genesis_block(Network::Bitcoin).block_hash();
- let network_graph = NetworkGraph::new(block_hash);
+ let logger = TestLogger::new();
+ let network_graph = NetworkGraph::new(block_hash, &logger);
let example_input = vec![
76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
];
let block_hash = genesis_block(Network::Bitcoin).block_hash();
- let network_graph = NetworkGraph::new(block_hash);
+ let logger = TestLogger::new();
+ let network_graph = NetworkGraph::new(block_hash, &logger);
assert_eq!(network_graph.read_only().channels().len(), 0);
];
let block_hash = genesis_block(Network::Bitcoin).block_hash();
- let network_graph = NetworkGraph::new(block_hash);
+ let logger = TestLogger::new();
+ let network_graph = NetworkGraph::new(block_hash, &logger);
assert_eq!(network_graph.read_only().channels().len(), 0);
];
let block_hash = genesis_block(Network::Bitcoin).block_hash();
- let network_graph = NetworkGraph::new(block_hash);
+ let logger = TestLogger::new();
+ let network_graph = NetworkGraph::new(block_hash, &logger);
assert_eq!(network_graph.read_only().channels().len(), 0);
];
let block_hash = genesis_block(Network::Bitcoin).block_hash();
- let network_graph = NetworkGraph::new(block_hash);
+ let logger = TestLogger::new();
+ let network_graph = NetworkGraph::new(block_hash, &logger);
assert_eq!(network_graph.read_only().channels().len(), 0);
];
let block_hash = genesis_block(Network::Bitcoin).block_hash();
- let network_graph = NetworkGraph::new(block_hash);
+ let logger = TestLogger::new();
+ let network_graph = NetworkGraph::new(block_hash, &logger);
assert_eq!(network_graph.read_only().channels().len(), 0);
final_cltv_expiry_delta: TEST_FINAL_CLTV,
};
let route = find_route(
- &nodes[0].node.get_our_node_id(), &route_params, nodes[0].network_graph, None,
- nodes[0].logger, &scorer, &random_seed_bytes
+ &nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph.read_only(),
+ None, nodes[0].logger, &scorer, &random_seed_bytes
).unwrap();
nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage)).unwrap();
check_added_monitors!(nodes[0], 1);
// To start (2), send a keysend payment but don't claim it.
let payment_preimage = PaymentPreimage([42; 32]);
let route = find_route(
- &nodes[0].node.get_our_node_id(), &route_params, nodes[0].network_graph, None,
- nodes[0].logger, &scorer, &random_seed_bytes
+ &nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph.read_only(),
+ None, nodes[0].logger, &scorer, &random_seed_bytes
).unwrap();
let (payment_hash, _) = nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage)).unwrap();
check_added_monitors!(nodes[0], 1);
let scorer = test_utils::TestScorer::with_penalty(0);
let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
let route = find_route(
- &payer_pubkey, &route_params, network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
- nodes[0].logger, &scorer, &random_seed_bytes
+ &payer_pubkey, &route_params, &network_graph.read_only(),
+ Some(&first_hops.iter().collect::<Vec<_>>()), nodes[0].logger, &scorer,
+ &random_seed_bytes
).unwrap();
let test_preimage = PaymentPreimage([42; 32]);
let scorer = test_utils::TestScorer::with_penalty(0);
let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
let route = find_route(
- &payer_pubkey, &route_params, network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
- nodes[0].logger, &scorer, &random_seed_bytes
+ &payer_pubkey, &route_params, &network_graph.read_only(),
+ Some(&first_hops.iter().collect::<Vec<_>>()), nodes[0].logger, &scorer,
+ &random_seed_bytes
).unwrap();
let test_preimage = PaymentPreimage([42; 32]);
_ => panic!(),
}
- let dummy_graph = NetworkGraph::new(genesis_hash);
+ let dummy_graph = NetworkGraph::new(genesis_hash, &logger_a);
let mut payment_count: u64 = 0;
macro_rules! send_payment {
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose};
use util::errors::APIError;
use util::config::UserConfig;
-use util::ser::{ReadableArgs, Writeable, Readable};
+use util::ser::{ReadableArgs, Writeable};
use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::constants::genesis_block;
pub persister: test_utils::TestPersister,
pub logger: test_utils::TestLogger,
pub keys_manager: test_utils::TestKeysInterface,
- pub network_graph: NetworkGraph,
}
pub struct NodeCfg<'a> {
pub chain_monitor: test_utils::TestChainMonitor<'a>,
pub keys_manager: &'a test_utils::TestKeysInterface,
pub logger: &'a test_utils::TestLogger,
- pub network_graph: &'a NetworkGraph,
+ pub network_graph: NetworkGraph<&'a test_utils::TestLogger>,
pub node_seed: [u8; 32],
pub features: InitFeatures,
}
pub chain_monitor: &'b test_utils::TestChainMonitor<'c>,
pub keys_manager: &'b test_utils::TestKeysInterface,
pub node: &'a ChannelManager<EnforcingSigner, &'b TestChainMonitor<'c>, &'c test_utils::TestBroadcaster, &'b test_utils::TestKeysInterface, &'c test_utils::TestFeeEstimator, &'c test_utils::TestLogger>,
- pub network_graph: &'c NetworkGraph,
- pub gossip_sync: P2PGossipSync<&'c NetworkGraph, &'c test_utils::TestChainSource, &'c test_utils::TestLogger>,
+ pub network_graph: &'b NetworkGraph<&'c test_utils::TestLogger>,
+ pub gossip_sync: P2PGossipSync<&'b NetworkGraph<&'c test_utils::TestLogger>, &'c test_utils::TestChainSource, &'c test_utils::TestLogger>,
pub node_seed: [u8; 32],
pub network_payment_count: Rc<RefCell<u8>>,
pub network_chan_count: Rc<RefCell<u32>>,
{
let mut w = test_utils::TestVecWriter(Vec::new());
self.network_graph.write(&mut w).unwrap();
- let network_graph_deser = <NetworkGraph>::read(&mut io::Cursor::new(&w.0)).unwrap();
+ let network_graph_deser = <NetworkGraph<_>>::read(&mut io::Cursor::new(&w.0), self.logger).unwrap();
assert!(network_graph_deser == *self.network_graph);
let gossip_sync = P2PGossipSync::new(
&network_graph_deser, Some(self.chain_source), self.logger
let persister = test_utils::TestPersister::new();
let seed = [i as u8; 32];
let keys_manager = test_utils::TestKeysInterface::new(&seed, Network::Testnet);
- let network_graph = NetworkGraph::new(chain_source.genesis_hash);
- chan_mon_cfgs.push(TestChanMonCfg{ tx_broadcaster, fee_estimator, chain_source, logger, persister, keys_manager, network_graph });
+ chan_mon_cfgs.push(TestChanMonCfg{ tx_broadcaster, fee_estimator, chain_source, logger, persister, keys_manager });
}
chan_mon_cfgs
keys_manager: &chanmon_cfgs[i].keys_manager,
node_seed: seed,
features: InitFeatures::known(),
- network_graph: &chanmon_cfgs[i].network_graph,
+ network_graph: NetworkGraph::new(chanmon_cfgs[i].chain_source.genesis_hash, &chanmon_cfgs[i].logger),
});
}
let connect_style = Rc::new(RefCell::new(ConnectStyle::random_style()));
for i in 0..node_count {
- let gossip_sync = P2PGossipSync::new(cfgs[i].network_graph, None, cfgs[i].logger);
+ let gossip_sync = P2PGossipSync::new(&cfgs[i].network_graph, None, cfgs[i].logger);
nodes.push(Node{
chain_source: cfgs[i].chain_source, tx_broadcaster: cfgs[i].tx_broadcaster,
chain_monitor: &cfgs[i].chain_monitor, keys_manager: &cfgs[i].keys_manager,
use ln::channel::{Channel, ChannelError};
use ln::{chan_utils, onion_utils};
use ln::chan_utils::{htlc_success_tx_weight, htlc_timeout_tx_weight, HTLCOutputInCommitment};
+use routing::gossip::NetworkGraph;
use routing::router::{PaymentParameters, Route, RouteHop, RouteParameters, find_route, get_route};
use ln::features::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
use ln::msgs;
let seed = [42; 32];
let keys_manager = test_utils::TestKeysInterface::new(&seed, Network::Testnet);
let chain_monitor = test_utils::TestChainMonitor::new(Some(&chanmon_cfgs[0].chain_source), &chanmon_cfgs[0].tx_broadcaster, &chanmon_cfgs[0].logger, &chanmon_cfgs[0].fee_estimator, &chanmon_cfgs[0].persister, &keys_manager);
- let node = NodeCfg { chain_source: &chanmon_cfgs[0].chain_source, logger: &chanmon_cfgs[0].logger, tx_broadcaster: &chanmon_cfgs[0].tx_broadcaster, fee_estimator: &chanmon_cfgs[0].fee_estimator, chain_monitor, keys_manager: &keys_manager, network_graph: &chanmon_cfgs[0].network_graph, node_seed: seed, features: InitFeatures::known() };
+ let network_graph = NetworkGraph::new(chanmon_cfgs[0].chain_source.genesis_hash, &chanmon_cfgs[0].logger);
+ let node = NodeCfg { chain_source: &chanmon_cfgs[0].chain_source, logger: &chanmon_cfgs[0].logger, tx_broadcaster: &chanmon_cfgs[0].tx_broadcaster, fee_estimator: &chanmon_cfgs[0].fee_estimator, chain_monitor, keys_manager: &keys_manager, network_graph, node_seed: seed, features: InitFeatures::known() };
let mut node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
node_cfgs.remove(0);
node_cfgs.insert(0, node);
};
let scorer = test_utils::TestScorer::with_penalty(0);
let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
- let route = find_route(&payer_pubkey, &route_params, network_graph, None, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
+ let route = find_route(&payer_pubkey, &route_params, &network_graph.read_only(), None, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
let test_preimage = PaymentPreimage([42; 32]);
let (payment_hash, _) = nodes[0].node.send_spontaneous_payment(&route, Some(test_preimage)).unwrap();
let scorer = test_utils::TestScorer::with_penalty(0);
let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
let route = find_route(
- &payer_pubkey, &route_params, network_graph, Some(&first_hops.iter().collect::<Vec<_>>()),
- nodes[0].logger, &scorer, &random_seed_bytes
+ &payer_pubkey, &route_params, &network_graph.read_only(),
+ Some(&first_hops.iter().collect::<Vec<_>>()), nodes[0].logger, &scorer, &random_seed_bytes
).unwrap();
let test_preimage = PaymentPreimage([42; 32]);
/// issues such as overly long function definitions.
///
/// (C-not exported) as Arcs don't make sense in bindings
-pub type SimpleArcPeerManager<SD, M, T, F, C, L> = PeerManager<SD, Arc<SimpleArcChannelManager<M, T, F, L>>, Arc<P2PGossipSync<Arc<NetworkGraph>, Arc<C>, Arc<L>>>, Arc<L>, Arc<IgnoringMessageHandler>>;
+pub type SimpleArcPeerManager<SD, M, T, F, C, L> = PeerManager<SD, Arc<SimpleArcChannelManager<M, T, F, L>>, Arc<P2PGossipSync<Arc<NetworkGraph<Arc<L>>>, Arc<C>, Arc<L>>>, Arc<L>, Arc<IgnoringMessageHandler>>;
/// SimpleRefPeerManager is a type alias for a PeerManager reference, and is the reference
/// counterpart to the SimpleArcPeerManager type alias. Use this type by default when you don't
/// helps with issues such as long function definitions.
///
/// (C-not exported) as Arcs don't make sense in bindings
-pub type SimpleRefPeerManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, SD, M, T, F, C, L> = PeerManager<SD, SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L>, &'e P2PGossipSync<&'g NetworkGraph, &'h C, &'f L>, &'f L, IgnoringMessageHandler>;
+pub type SimpleRefPeerManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, SD, M, T, F, C, L> = PeerManager<SD, SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L>, &'e P2PGossipSync<&'g NetworkGraph<&'f L>, &'h C, &'f L>, &'f L, IgnoringMessageHandler>;
/// A PeerManager manages a set of peers, described by their [`SocketDescriptor`] and marshalls
/// socket events into messages which it passes on to its [`MessageHandler`].
use ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, OptionalField, GossipTimestampFilter};
use ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds, ReplyShortChannelIdsEnd};
use ln::msgs;
-use util::ser::{Writeable, Readable, Writer};
+use util::ser::{Readable, ReadableArgs, Writeable, Writer};
use util::logger::{Logger, Level};
use util::events::{Event, EventHandler, MessageSendEvent, MessageSendEventsProvider};
use util::scid_utils::{block_from_scid, scid_from_parts, MAX_SCID_BLOCK};
}
/// Represents the network as nodes and channels between them
-pub struct NetworkGraph {
+pub struct NetworkGraph<L: Deref> where L::Target: Logger {
secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
last_rapid_gossip_sync_timestamp: Mutex<Option<u32>>,
genesis_hash: BlockHash,
+ _logger: L,
// Lock order: channels -> nodes
channels: RwLock<BTreeMap<u64, ChannelInfo>>,
nodes: RwLock<BTreeMap<NodeId, NodeInfo>>,
}
-impl Clone for NetworkGraph {
- fn clone(&self) -> Self {
- let channels = self.channels.read().unwrap();
- let nodes = self.nodes.read().unwrap();
- let last_rapid_gossip_sync_timestamp = self.get_last_rapid_gossip_sync_timestamp();
- Self {
- secp_ctx: Secp256k1::verification_only(),
- genesis_hash: self.genesis_hash.clone(),
- channels: RwLock::new(channels.clone()),
- nodes: RwLock::new(nodes.clone()),
- last_rapid_gossip_sync_timestamp: Mutex::new(last_rapid_gossip_sync_timestamp)
- }
- }
-}
-
/// A read-only view of [`NetworkGraph`].
pub struct ReadOnlyNetworkGraph<'a> {
channels: RwLockReadGuard<'a, BTreeMap<u64, ChannelInfo>>,
},
);
-impl<G: Deref<Target=NetworkGraph>, C: Deref, L: Deref> EventHandler for P2PGossipSync<G, C, L>
+impl<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref> EventHandler for P2PGossipSync<G, C, L>
where C::Target: chain::Access, L::Target: Logger {
fn handle_event(&self, event: &Event) {
if let Event::PaymentPathFailed { payment_hash: _, rejected_by_dest: _, network_update, .. } = event {
///
/// Serves as an [`EventHandler`] for applying updates from [`Event::PaymentPathFailed`] to the
/// [`NetworkGraph`].
-pub struct P2PGossipSync<G: Deref<Target=NetworkGraph>, C: Deref, L: Deref>
+pub struct P2PGossipSync<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref>
where C::Target: chain::Access, L::Target: Logger
{
network_graph: G,
logger: L,
}
-impl<G: Deref<Target=NetworkGraph>, C: Deref, L: Deref> P2PGossipSync<G, C, L>
+impl<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref> P2PGossipSync<G, C, L>
where C::Target: chain::Access, L::Target: Logger
{
/// Creates a new tracker of the actual state of the network of channels and nodes,
};
}
-impl<G: Deref<Target=NetworkGraph>, C: Deref, L: Deref> RoutingMessageHandler for P2PGossipSync<G, C, L>
+impl<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref> RoutingMessageHandler for P2PGossipSync<G, C, L>
where C::Target: chain::Access, L::Target: Logger
{
fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
}
}
-impl<G: Deref<Target=NetworkGraph>, C: Deref, L: Deref> MessageSendEventsProvider for P2PGossipSync<G, C, L>
+impl<G: Deref<Target=NetworkGraph<L>>, C: Deref, L: Deref> MessageSendEventsProvider for P2PGossipSync<G, C, L>
where
C::Target: chain::Access,
L::Target: Logger,
const SERIALIZATION_VERSION: u8 = 1;
const MIN_SERIALIZATION_VERSION: u8 = 1;
-impl Writeable for NetworkGraph {
+impl<L: Deref> Writeable for NetworkGraph<L> where L::Target: Logger {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
}
}
-impl Readable for NetworkGraph {
- fn read<R: io::Read>(reader: &mut R) -> Result<NetworkGraph, DecodeError> {
+impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
+ fn read<R: io::Read>(reader: &mut R, _logger: L) -> Result<NetworkGraph<L>, DecodeError> {
let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
let genesis_hash: BlockHash = Readable::read(reader)?;
Ok(NetworkGraph {
secp_ctx: Secp256k1::verification_only(),
genesis_hash,
+ _logger,
channels: RwLock::new(channels),
nodes: RwLock::new(nodes),
last_rapid_gossip_sync_timestamp: Mutex::new(last_rapid_gossip_sync_timestamp),
}
}
-impl fmt::Display for NetworkGraph {
+impl<L: Deref> fmt::Display for NetworkGraph<L> where L::Target: Logger {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
writeln!(f, "Network map\n[Channels]")?;
for (key, val) in self.channels.read().unwrap().iter() {
}
}
-impl PartialEq for NetworkGraph {
+impl<L: Deref> PartialEq for NetworkGraph<L> where L::Target: Logger {
fn eq(&self, other: &Self) -> bool {
self.genesis_hash == other.genesis_hash &&
*self.channels.read().unwrap() == *other.channels.read().unwrap() &&
}
}
-impl NetworkGraph {
+impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
/// Creates a new, empty, network graph.
- pub fn new(genesis_hash: BlockHash) -> NetworkGraph {
+ pub fn new(genesis_hash: BlockHash, _logger: L) -> NetworkGraph<L> {
Self {
secp_ctx: Secp256k1::verification_only(),
genesis_hash,
+ _logger,
channels: RwLock::new(BTreeMap::new()),
nodes: RwLock::new(BTreeMap::new()),
last_rapid_gossip_sync_timestamp: Mutex::new(None),
ReplyChannelRange, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
use util::test_utils;
use util::logger::Logger;
- use util::ser::{Readable, Writeable};
+ use util::ser::{ReadableArgs, Writeable};
use util::events::{Event, EventHandler, MessageSendEvent, MessageSendEventsProvider};
use util::scid_utils::scid_from_parts;
use prelude::*;
use sync::Arc;
- fn create_network_graph() -> NetworkGraph {
+ fn create_network_graph() -> NetworkGraph<Arc<test_utils::TestLogger>> {
let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
- NetworkGraph::new(genesis_hash)
+ let logger = Arc::new(test_utils::TestLogger::new());
+ NetworkGraph::new(genesis_hash, logger)
}
- fn create_gossip_sync(network_graph: &NetworkGraph) -> (
- Secp256k1<All>, P2PGossipSync<&NetworkGraph, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>
+ fn create_gossip_sync(network_graph: &NetworkGraph<Arc<test_utils::TestLogger>>) -> (
+ Secp256k1<All>, P2PGossipSync<&NetworkGraph<Arc<test_utils::TestLogger>>,
+ Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>
) {
let secp_ctx = Secp256k1::new();
let logger = Arc::new(test_utils::TestLogger::new());
let valid_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx);
// Test if the UTXO lookups were not supported
- let network_graph = NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash());
+ let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
+ let network_graph = NetworkGraph::new(genesis_hash, Arc::clone(&logger));
let mut gossip_sync = P2PGossipSync::new(&network_graph, None, Arc::clone(&logger));
match gossip_sync.handle_channel_announcement(&valid_announcement) {
Ok(res) => assert!(res),
// Test if an associated transaction were not on-chain (or not confirmed).
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
*chain_source.utxo_ret.lock().unwrap() = Err(chain::AccessError::UnknownTx);
- let network_graph = NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash());
+ let network_graph = NetworkGraph::new(genesis_hash, Arc::clone(&logger));
gossip_sync = P2PGossipSync::new(&network_graph, Some(chain_source.clone()), Arc::clone(&logger));
let valid_announcement = get_signed_channel_announcement(|unsigned_announcement| {
let secp_ctx = Secp256k1::new();
let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
- let network_graph = NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash());
+ let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
+ let network_graph = NetworkGraph::new(genesis_hash, Arc::clone(&logger));
let gossip_sync = P2PGossipSync::new(&network_graph, Some(chain_source.clone()), Arc::clone(&logger));
let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
let logger = test_utils::TestLogger::new();
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
- let network_graph = NetworkGraph::new(genesis_hash);
+ let network_graph = NetworkGraph::new(genesis_hash, &logger);
let gossip_sync = P2PGossipSync::new(&network_graph, Some(chain_source.clone()), &logger);
let secp_ctx = Secp256k1::new();
let logger = test_utils::TestLogger::new();
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
- let network_graph = NetworkGraph::new(genesis_hash);
+ let network_graph = NetworkGraph::new(genesis_hash, &logger);
let gossip_sync = P2PGossipSync::new(&network_graph, Some(chain_source.clone()), &logger);
let secp_ctx = Secp256k1::new();
assert!(!network_graph.read_only().nodes().is_empty());
assert!(!network_graph.read_only().channels().is_empty());
network_graph.write(&mut w).unwrap();
- assert!(<NetworkGraph>::read(&mut io::Cursor::new(&w.0)).unwrap() == network_graph);
+
+ let logger = Arc::new(test_utils::TestLogger::new());
+ assert!(<NetworkGraph<_>>::read(&mut io::Cursor::new(&w.0), logger).unwrap() == network_graph);
}
#[test]
let mut w = test_utils::TestVecWriter(Vec::new());
network_graph.write(&mut w).unwrap();
- let reassembled_network_graph: NetworkGraph = Readable::read(&mut io::Cursor::new(&w.0)).unwrap();
+
+ let logger = Arc::new(test_utils::TestLogger::new());
+ let reassembled_network_graph: NetworkGraph<_> = ReadableArgs::read(&mut io::Cursor::new(&w.0), logger).unwrap();
assert!(reassembled_network_graph == network_graph);
assert_eq!(reassembled_network_graph.get_last_rapid_gossip_sync_timestamp().unwrap(), 42);
}
}
fn do_handling_query_channel_range(
- gossip_sync: &P2PGossipSync<&NetworkGraph, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
+ gossip_sync: &P2PGossipSync<&NetworkGraph<Arc<test_utils::TestLogger>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
test_node_id: &PublicKey,
msg: QueryChannelRange,
expected_ok: bool,
#[bench]
fn read_network_graph(bench: &mut Bencher) {
+ let logger = ::util::test_utils::TestLogger::new();
let mut d = ::routing::router::test_utils::get_route_file().unwrap();
let mut v = Vec::new();
d.read_to_end(&mut v).unwrap();
bench.iter(|| {
- let _ = NetworkGraph::read(&mut std::io::Cursor::new(&v)).unwrap();
+ let _ = NetworkGraph::read(&mut std::io::Cursor::new(&v), &logger).unwrap();
});
}
#[bench]
fn write_network_graph(bench: &mut Bencher) {
+ let logger = ::util::test_utils::TestLogger::new();
let mut d = ::routing::router::test_utils::get_route_file().unwrap();
- let net_graph = NetworkGraph::read(&mut d).unwrap();
+ let net_graph = NetworkGraph::read(&mut d, &logger).unwrap();
bench.iter(|| {
let _ = net_graph.encode();
});
use ln::channelmanager::ChannelDetails;
use ln::features::{ChannelFeatures, InvoiceFeatures, NodeFeatures};
use ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT};
-use routing::gossip::{DirectedChannelInfoWithUpdate, EffectiveCapacity, NetworkGraph, ReadOnlyNetworkGraph, NodeId, RoutingFees};
+use routing::gossip::{DirectedChannelInfoWithUpdate, EffectiveCapacity, ReadOnlyNetworkGraph, NodeId, RoutingFees};
use routing::scoring::{ChannelUsage, Score};
use util::ser::{Writeable, Readable, Writer};
use util::logger::{Level, Logger};
FirstHop {
details: &'a ChannelDetails,
},
- /// A hop found in the [`NetworkGraph`], where the channel capacity may or may not be known.
+ /// A hop found in the [`ReadOnlyNetworkGraph`], where the channel capacity may be unknown.
PublicHop {
info: DirectedChannelInfoWithUpdate<'a>,
short_channel_id: u64,
/// Private routing paths between a public node and the target may be included in `params.payee`.
///
/// If some channels aren't announced, it may be useful to fill in `first_hops` with the results
-/// from [`ChannelManager::list_usable_channels`]. If it is filled in, the view of our local
-/// channels from [`NetworkGraph`] will be ignored, and only those in `first_hops` will be used.
+/// from [`ChannelManager::list_usable_channels`]. If it is filled in, the view of these channels
+/// from `network_graph` will be ignored, and only those in `first_hops` will be used.
///
/// The fees on channels from us to the next hop are ignored as they are assumed to all be equal.
/// However, the enabled/disabled bit on such channels as well as the `htlc_minimum_msat` /
///
/// [`ChannelManager::list_usable_channels`]: crate::ln::channelmanager::ChannelManager::list_usable_channels
/// [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed
+/// [`NetworkGraph`]: crate::routing::gossip::NetworkGraph
pub fn find_route<L: Deref, S: Score>(
- our_node_pubkey: &PublicKey, route_params: &RouteParameters, network: &NetworkGraph,
- first_hops: Option<&[&ChannelDetails]>, logger: L, scorer: &S, random_seed_bytes: &[u8; 32]
+ our_node_pubkey: &PublicKey, route_params: &RouteParameters,
+ network_graph: &ReadOnlyNetworkGraph, first_hops: Option<&[&ChannelDetails]>, logger: L,
+ scorer: &S, random_seed_bytes: &[u8; 32]
) -> Result<Route, LightningError>
where L::Target: Logger {
- let network_graph = network.read_only();
- let mut route = get_route(our_node_pubkey, &route_params.payment_params, &network_graph, first_hops,
+ let mut route = get_route(our_node_pubkey, &route_params.payment_params, network_graph, first_hops,
route_params.final_value_msat, route_params.final_cltv_expiry_delta, logger, scorer,
random_seed_bytes)?;
- add_random_cltv_offset(&mut route, &route_params.payment_params, &network_graph, random_seed_bytes);
+ add_random_cltv_offset(&mut route, &route_params.payment_params, network_graph, random_seed_bytes);
Ok(route)
}
///
/// Re-uses logic from `find_route`, so the restrictions described there also apply here.
pub fn build_route_from_hops<L: Deref>(
- our_node_pubkey: &PublicKey, hops: &[PublicKey], route_params: &RouteParameters, network: &NetworkGraph,
- logger: L, random_seed_bytes: &[u8; 32]
+ our_node_pubkey: &PublicKey, hops: &[PublicKey], route_params: &RouteParameters,
+ network_graph: &ReadOnlyNetworkGraph, logger: L, random_seed_bytes: &[u8; 32]
) -> Result<Route, LightningError>
where L::Target: Logger {
- let network_graph = network.read_only();
let mut route = build_route_from_hops_internal(
our_node_pubkey, hops, &route_params.payment_params, &network_graph,
route_params.final_value_msat, route_params.final_cltv_expiry_delta, logger, random_seed_bytes)?;
// Using the same keys for LN and BTC ids
fn add_channel(
- gossip_sync: &P2PGossipSync<Arc<NetworkGraph>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
+ gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
secp_ctx: &Secp256k1<All>, node_1_privkey: &SecretKey, node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64
) {
let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
}
fn update_channel(
- gossip_sync: &P2PGossipSync<Arc<NetworkGraph>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
+ gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, update: UnsignedChannelUpdate
) {
let msghash = hash_to_message!(&Sha256dHash::hash(&update.encode()[..])[..]);
}
fn add_or_update_node(
- gossip_sync: &P2PGossipSync<Arc<NetworkGraph>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
+ gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, features: NodeFeatures, timestamp: u32
) {
let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
}
fn build_line_graph() -> (
- Secp256k1<All>, sync::Arc<NetworkGraph>, P2PGossipSync<sync::Arc<NetworkGraph>,
- sync::Arc<test_utils::TestChainSource>, sync::Arc<crate::util::test_utils::TestLogger>>,
+ Secp256k1<All>, sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
+ P2PGossipSync<sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>>,
sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>,
) {
let secp_ctx = Secp256k1::new();
let logger = Arc::new(test_utils::TestLogger::new());
let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
- let network_graph = Arc::new(NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash()));
+ let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
+ let network_graph = Arc::new(NetworkGraph::new(genesis_hash, Arc::clone(&logger)));
let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger));
// Build network from our_id to node 19:
fn build_graph() -> (
Secp256k1<All>,
- sync::Arc<NetworkGraph>,
- P2PGossipSync<sync::Arc<NetworkGraph>, sync::Arc<test_utils::TestChainSource>, sync::Arc<crate::util::test_utils::TestLogger>>,
+ sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
+ P2PGossipSync<sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>>,
sync::Arc<test_utils::TestChainSource>,
sync::Arc<test_utils::TestLogger>,
) {
let secp_ctx = Secp256k1::new();
let logger = Arc::new(test_utils::TestLogger::new());
let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
- let network_graph = Arc::new(NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash()));
+ let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
+ let network_graph = Arc::new(NetworkGraph::new(genesis_hash, Arc::clone(&logger)));
let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger));
// Build network from our_id to node6:
//
let scorer = test_utils::TestScorer::with_penalty(0);
let keys_manager = test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
let random_seed_bytes = keys_manager.get_secure_random_bytes();
- get_route(&source_node_id, &payment_params, &NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash()).read_only(),
- Some(&our_chans.iter().collect::<Vec<_>>()), route_val, 42, &test_utils::TestLogger::new(), &scorer, &random_seed_bytes)
+ let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
+ let logger = test_utils::TestLogger::new();
+ let network_graph = NetworkGraph::new(genesis_hash, &logger);
+ let route = get_route(&source_node_id, &payment_params, &network_graph.read_only(),
+ Some(&our_chans.iter().collect::<Vec<_>>()), route_val, 42, &logger, &scorer, &random_seed_bytes);
+ route
}
#[test]
// payment) htlc_minimum_msat. In the original algorithm, this resulted in node4's
// "previous hop" being set to node 3, creating a loop in the path.
let secp_ctx = Secp256k1::new();
+ let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
let logger = Arc::new(test_utils::TestLogger::new());
- let network = Arc::new(NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash()));
+ let network = Arc::new(NetworkGraph::new(genesis_hash, Arc::clone(&logger)));
let gossip_sync = P2PGossipSync::new(Arc::clone(&network), None, Arc::clone(&logger));
let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx);
let scorer = test_utils::TestScorer::with_penalty(0);
// route over multiple channels with the same first hop.
let secp_ctx = Secp256k1::new();
let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
+ let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
let logger = Arc::new(test_utils::TestLogger::new());
- let network_graph = NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash());
+ let network_graph = NetworkGraph::new(genesis_hash, Arc::clone(&logger));
let scorer = test_utils::TestScorer::with_penalty(0);
let payment_params = PaymentParameters::from_node_id(nodes[0]).with_features(InvoiceFeatures::known());
let keys_manager = test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
seed
}
#[cfg(not(feature = "no-std"))]
- use util::ser::Readable;
+ use util::ser::ReadableArgs;
#[test]
#[cfg(not(feature = "no-std"))]
return;
},
};
- let graph = NetworkGraph::read(&mut d).unwrap();
+ let logger = test_utils::TestLogger::new();
+ let graph = NetworkGraph::read(&mut d, &logger).unwrap();
let keys_manager = test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
let random_seed_bytes = keys_manager.get_secure_random_bytes();
let payment_params = PaymentParameters::from_node_id(dst);
let amt = seed as u64 % 200_000_000;
let params = ProbabilisticScoringParameters::default();
- let logger = test_utils::TestLogger::new();
let scorer = ProbabilisticScorer::new(params, &graph, &logger);
if get_route(src, &payment_params, &graph.read_only(), None, amt, 42, &logger, &scorer, &random_seed_bytes).is_ok() {
continue 'load_endpoints;
return;
},
};
- let graph = NetworkGraph::read(&mut d).unwrap();
+ let logger = test_utils::TestLogger::new();
+ let graph = NetworkGraph::read(&mut d, &logger).unwrap();
let keys_manager = test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
let random_seed_bytes = keys_manager.get_secure_random_bytes();
let payment_params = PaymentParameters::from_node_id(dst).with_features(InvoiceFeatures::known());
let amt = seed as u64 % 200_000_000;
let params = ProbabilisticScoringParameters::default();
- let logger = test_utils::TestLogger::new();
let scorer = ProbabilisticScorer::new(params, &graph, &logger);
if get_route(src, &payment_params, &graph.read_only(), None, amt, 42, &logger, &scorer, &random_seed_bytes).is_ok() {
continue 'load_endpoints;
use chain::keysinterface::{KeysManager,KeysInterface};
use ln::channelmanager::{ChannelCounterparty, ChannelDetails};
use ln::features::{InitFeatures, InvoiceFeatures};
+ use routing::gossip::NetworkGraph;
use routing::scoring::{FixedPenaltyScorer, ProbabilisticScorer, ProbabilisticScoringParameters};
use util::logger::{Logger, Record};
- use util::test_utils::TestLogger;
+ use util::ser::ReadableArgs;
use test::Bencher;
fn log(&self, _record: &Record) {}
}
- fn read_network_graph() -> NetworkGraph {
+ fn read_network_graph(logger: &DummyLogger) -> NetworkGraph<&DummyLogger> {
let mut d = test_utils::get_route_file().unwrap();
- NetworkGraph::read(&mut d).unwrap()
+ NetworkGraph::read(&mut d, logger).unwrap()
}
fn payer_pubkey() -> PublicKey {
#[bench]
fn generate_routes_with_zero_penalty_scorer(bench: &mut Bencher) {
- let network_graph = read_network_graph();
+ let logger = DummyLogger {};
+ let network_graph = read_network_graph(&logger);
let scorer = FixedPenaltyScorer::with_penalty(0);
generate_routes(bench, &network_graph, scorer, InvoiceFeatures::empty());
}
#[bench]
fn generate_mpp_routes_with_zero_penalty_scorer(bench: &mut Bencher) {
- let network_graph = read_network_graph();
+ let logger = DummyLogger {};
+ let network_graph = read_network_graph(&logger);
let scorer = FixedPenaltyScorer::with_penalty(0);
generate_routes(bench, &network_graph, scorer, InvoiceFeatures::known());
}
#[bench]
fn generate_routes_with_probabilistic_scorer(bench: &mut Bencher) {
- let logger = TestLogger::new();
- let network_graph = read_network_graph();
+ let logger = DummyLogger {};
+ let network_graph = read_network_graph(&logger);
let params = ProbabilisticScoringParameters::default();
let scorer = ProbabilisticScorer::new(params, &network_graph, &logger);
generate_routes(bench, &network_graph, scorer, InvoiceFeatures::empty());
#[bench]
fn generate_mpp_routes_with_probabilistic_scorer(bench: &mut Bencher) {
- let logger = TestLogger::new();
- let network_graph = read_network_graph();
+ let logger = DummyLogger {};
+ let network_graph = read_network_graph(&logger);
let params = ProbabilisticScoringParameters::default();
let scorer = ProbabilisticScorer::new(params, &network_graph, &logger);
generate_routes(bench, &network_graph, scorer, InvoiceFeatures::known());
}
fn generate_routes<S: Score>(
- bench: &mut Bencher, graph: &NetworkGraph, mut scorer: S, features: InvoiceFeatures
+ bench: &mut Bencher, graph: &NetworkGraph<&DummyLogger>, mut scorer: S,
+ features: InvoiceFeatures
) {
let nodes = graph.read_only().nodes().clone();
let payer = payer_pubkey();
//! # impl Logger for FakeLogger {
//! # fn log(&self, record: &Record) { unimplemented!() }
//! # }
-//! # fn find_scored_route(payer: PublicKey, route_params: RouteParameters, network_graph: NetworkGraph) {
+//! # fn find_scored_route(payer: PublicKey, route_params: RouteParameters, network_graph: NetworkGraph<&FakeLogger>) {
//! # let logger = FakeLogger {};
//! #
//! // Use the default channel penalties.
//! let scorer = ProbabilisticScorer::new(params, &network_graph, &logger);
//! # let random_seed_bytes = [42u8; 32];
//!
-//! let route = find_route(&payer, &route_params, &network_graph, None, &logger, &scorer, &random_seed_bytes);
+//! let route = find_route(&payer, &route_params, &network_graph.read_only(), None, &logger, &scorer, &random_seed_bytes);
//! # }
//! ```
//!
/// Probabilistic [`Score`] implementation.
///
/// (C-not exported) generally all users should use the [`ProbabilisticScorer`] type alias.
-pub struct ProbabilisticScorerUsingTime<G: Deref<Target = NetworkGraph>, L: Deref, T: Time> where L::Target: Logger {
+pub struct ProbabilisticScorerUsingTime<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time>
+where L::Target: Logger {
params: ProbabilisticScoringParameters,
network_graph: G,
logger: L,
half_life: Duration,
}
-impl<G: Deref<Target = NetworkGraph>, L: Deref, T: Time> ProbabilisticScorerUsingTime<G, L, T> where L::Target: Logger {
+impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ProbabilisticScorerUsingTime<G, L, T> where L::Target: Logger {
/// Creates a new scorer using the given scoring parameters for sending payments from a node
/// through a network graph.
pub fn new(params: ProbabilisticScoringParameters, network_graph: G, logger: L) -> Self {
}
}
-impl<G: Deref<Target = NetworkGraph>, L: Deref, T: Time> Score for ProbabilisticScorerUsingTime<G, L, T> where L::Target: Logger {
+impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> Score for ProbabilisticScorerUsingTime<G, L, T> where L::Target: Logger {
fn channel_penalty_msat(
&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage
) -> u64 {
}
}
-impl<G: Deref<Target = NetworkGraph>, L: Deref, T: Time> Writeable for ProbabilisticScorerUsingTime<G, L, T> where L::Target: Logger {
+impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> Writeable for ProbabilisticScorerUsingTime<G, L, T> where L::Target: Logger {
#[inline]
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
write_tlv_fields!(w, {
}
}
-impl<G: Deref<Target = NetworkGraph>, L: Deref, T: Time>
+impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time>
ReadableArgs<(ProbabilisticScoringParameters, G, L)> for ProbabilisticScorerUsingTime<G, L, T> where L::Target: Logger {
#[inline]
fn read<R: Read>(
// `ProbabilisticScorer` tests
/// A probabilistic scorer for testing with time that can be manually advanced.
- type ProbabilisticScorer<'a> = ProbabilisticScorerUsingTime::<&'a NetworkGraph, &'a TestLogger, SinceEpoch>;
+ type ProbabilisticScorer<'a> = ProbabilisticScorerUsingTime::<&'a NetworkGraph<&'a TestLogger>, &'a TestLogger, SinceEpoch>;
fn sender_privkey() -> SecretKey {
SecretKey::from_slice(&[41; 32]).unwrap()
NodeId::from_pubkey(&recipient_pubkey())
}
- fn network_graph() -> NetworkGraph {
+ fn network_graph(logger: &TestLogger) -> NetworkGraph<&TestLogger> {
let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
- let mut network_graph = NetworkGraph::new(genesis_hash);
+ let mut network_graph = NetworkGraph::new(genesis_hash, logger);
add_channel(&mut network_graph, 42, source_privkey(), target_privkey());
add_channel(&mut network_graph, 43, target_privkey(), recipient_privkey());
}
fn add_channel(
- network_graph: &mut NetworkGraph, short_channel_id: u64, node_1_key: SecretKey,
+ network_graph: &mut NetworkGraph<&TestLogger>, short_channel_id: u64, node_1_key: SecretKey,
node_2_key: SecretKey
) {
let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
}
fn update_channel(
- network_graph: &mut NetworkGraph, short_channel_id: u64, node_key: SecretKey, flags: u8
+ network_graph: &mut NetworkGraph<&TestLogger>, short_channel_id: u64, node_key: SecretKey,
+ flags: u8
) {
let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
let secp_ctx = Secp256k1::new();
fn liquidity_bounds_directed_from_lowest_node_id() {
let logger = TestLogger::new();
let last_updated = SinceEpoch::now();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters::default();
let mut scorer = ProbabilisticScorer::new(params, &network_graph, &logger)
.with_channel(42,
fn resets_liquidity_upper_bound_when_crossed_by_lower_bound() {
let logger = TestLogger::new();
let last_updated = SinceEpoch::now();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters::default();
let mut scorer = ProbabilisticScorer::new(params, &network_graph, &logger)
.with_channel(42,
fn resets_liquidity_lower_bound_when_crossed_by_upper_bound() {
let logger = TestLogger::new();
let last_updated = SinceEpoch::now();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters::default();
let mut scorer = ProbabilisticScorer::new(params, &network_graph, &logger)
.with_channel(42,
#[test]
fn increased_penalty_nearing_liquidity_upper_bound() {
let logger = TestLogger::new();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters {
liquidity_penalty_multiplier_msat: 1_000,
..ProbabilisticScoringParameters::zero_penalty()
fn constant_penalty_outside_liquidity_bounds() {
let logger = TestLogger::new();
let last_updated = SinceEpoch::now();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters {
liquidity_penalty_multiplier_msat: 1_000,
..ProbabilisticScoringParameters::zero_penalty()
#[test]
fn does_not_further_penalize_own_channel() {
let logger = TestLogger::new();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters {
liquidity_penalty_multiplier_msat: 1_000,
..ProbabilisticScoringParameters::zero_penalty()
#[test]
fn sets_liquidity_lower_bound_on_downstream_failure() {
let logger = TestLogger::new();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters {
liquidity_penalty_multiplier_msat: 1_000,
..ProbabilisticScoringParameters::zero_penalty()
#[test]
fn sets_liquidity_upper_bound_on_failure() {
let logger = TestLogger::new();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters {
liquidity_penalty_multiplier_msat: 1_000,
..ProbabilisticScoringParameters::zero_penalty()
#[test]
fn reduces_liquidity_upper_bound_along_path_on_success() {
let logger = TestLogger::new();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters {
liquidity_penalty_multiplier_msat: 1_000,
..ProbabilisticScoringParameters::zero_penalty()
#[test]
fn decays_liquidity_bounds_over_time() {
let logger = TestLogger::new();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters {
liquidity_penalty_multiplier_msat: 1_000,
liquidity_offset_half_life: Duration::from_secs(10),
#[test]
fn decays_liquidity_bounds_without_shift_overflow() {
let logger = TestLogger::new();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters {
liquidity_penalty_multiplier_msat: 1_000,
liquidity_offset_half_life: Duration::from_secs(10),
#[test]
fn restricts_liquidity_bounds_after_decay() {
let logger = TestLogger::new();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters {
liquidity_penalty_multiplier_msat: 1_000,
liquidity_offset_half_life: Duration::from_secs(10),
#[test]
fn restores_persisted_liquidity_bounds() {
let logger = TestLogger::new();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters {
liquidity_penalty_multiplier_msat: 1_000,
liquidity_offset_half_life: Duration::from_secs(10),
#[test]
fn decays_persisted_liquidity_bounds() {
let logger = TestLogger::new();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters {
liquidity_penalty_multiplier_msat: 1_000,
liquidity_offset_half_life: Duration::from_secs(10),
// Shows the scores of "realistic" sends of 100k sats over channels of 1-10m sats (with a
// 50k sat reserve).
let logger = TestLogger::new();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters::default();
let scorer = ProbabilisticScorer::new(params, &network_graph, &logger);
let source = source_node_id();
#[test]
fn adds_base_penalty_to_liquidity_penalty() {
let logger = TestLogger::new();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let source = source_node_id();
let target = target_node_id();
let usage = ChannelUsage {
#[test]
fn adds_amount_penalty_to_liquidity_penalty() {
let logger = TestLogger::new();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let source = source_node_id();
let target = target_node_id();
let usage = ChannelUsage {
#[test]
fn calculates_log10_without_overflowing_u64_max_value() {
let logger = TestLogger::new();
- let network_graph = network_graph();
+ let network_graph = network_graph(&logger);
let source = source_node_id();
let target = target_node_id();
let usage = ChannelUsage {
#[test]
fn accounts_for_inflight_htlc_usage() {
- let network_graph = network_graph();
let logger = TestLogger::new();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters::default();
let scorer = ProbabilisticScorer::new(params, &network_graph, &logger);
let source = source_node_id();
#[test]
fn removes_uncertainity_when_exact_liquidity_known() {
- let network_graph = network_graph();
let logger = TestLogger::new();
+ let network_graph = network_graph(&logger);
let params = ProbabilisticScoringParameters::default();
let scorer = ProbabilisticScorer::new(params, &network_graph, &logger);
let source = source_node_id();
fn persist_manager(&self, channel_manager: &ChannelManager<Signer, M, T, K, F, L>) -> Result<(), io::Error>;
/// Persist the given [`NetworkGraph`] to disk, returning an error if persistence failed.
- fn persist_graph(&self, network_graph: &NetworkGraph) -> Result<(), io::Error>;
+ fn persist_graph(&self, network_graph: &NetworkGraph<L>) -> Result<(), io::Error>;
/// Persist the given [`WriteableScore`] to disk, returning an error if persistence failed.
fn persist_scorer(&self, scorer: &S) -> Result<(), io::Error>;
}
/// Persist the given [`NetworkGraph`] to disk with the name "network_graph", returning an error if persistence failed.
- fn persist_graph(&self, network_graph: &NetworkGraph) -> Result<(), io::Error> {
+ fn persist_graph(&self, network_graph: &NetworkGraph<L>) -> Result<(), io::Error> {
self.persist("network_graph", network_graph)
}