Skip to content

Commit a9c88c6

Browse files
committed
Add genesis block hash to NetworkGraph
This changes adds the genesis block hash as a BlockHash to the NetworkGraph struct. Making the NetworkGraph aware allows the message handler to validate the chain_hash for received messages. This change also adds the hash value to the Writeable and Readable methods.
1 parent 6a59483 commit a9c88c6

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
11681168
let payment_count = Rc::new(RefCell::new(0));
11691169

11701170
for i in 0..node_count {
1171-
let net_graph_msg_handler = NetGraphMsgHandler::new(None, cfgs[i].logger);
1171+
let net_graph_msg_handler = NetGraphMsgHandler::new(cfgs[i].chain_source.genesis_hash, None, cfgs[i].logger);
11721172
nodes.push(Node{ chain_source: cfgs[i].chain_source,
11731173
tx_broadcaster: cfgs[i].tx_broadcaster, chain_monitor: &cfgs[i].chain_monitor,
11741174
keys_manager: &cfgs[i].keys_manager, node: &chan_mgrs[i], net_graph_msg_handler,

lightning/src/routing/network_graph.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const MAX_REPLY_CHANNEL_RANGE_PER_QUERY: usize = 250;
5555
/// Represents the network as nodes and channels between them
5656
#[derive(PartialEq)]
5757
pub struct NetworkGraph {
58+
genesis_hash: BlockHash,
5859
channels: BTreeMap<u64, ChannelInfo>,
5960
nodes: BTreeMap<PublicKey, NodeInfo>,
6061
}
@@ -87,13 +88,10 @@ impl<C: Deref, L: Deref> NetGraphMsgHandler<C, L> where C::Target: chain::Access
8788
/// Chain monitor is used to make sure announced channels exist on-chain,
8889
/// channel data is correct, and that the announcement is signed with
8990
/// channel owners' keys.
90-
pub fn new(chain_access: Option<C>, logger: L) -> Self {
91+
pub fn new(genesis_hash: BlockHash, chain_access: Option<C>, logger: L) -> Self {
9192
NetGraphMsgHandler {
9293
secp_ctx: Secp256k1::verification_only(),
93-
network_graph: RwLock::new(NetworkGraph {
94-
channels: BTreeMap::new(),
95-
nodes: BTreeMap::new(),
96-
}),
94+
network_graph: RwLock::new(NetworkGraph::new(genesis_hash)),
9795
full_syncs_requested: AtomicUsize::new(0),
9896
chain_access,
9997
pending_events: Mutex::new(vec![]),
@@ -934,6 +932,7 @@ impl Readable for NodeInfo {
934932

935933
impl Writeable for NetworkGraph {
936934
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
935+
self.genesis_hash.write(writer)?;
937936
(self.channels.len() as u64).write(writer)?;
938937
for (ref chan_id, ref chan_info) in self.channels.iter() {
939938
(*chan_id).write(writer)?;
@@ -950,6 +949,7 @@ impl Writeable for NetworkGraph {
950949

951950
impl Readable for NetworkGraph {
952951
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NetworkGraph, DecodeError> {
952+
let genesis_hash: BlockHash = Readable::read(reader)?;
953953
let channels_count: u64 = Readable::read(reader)?;
954954
let mut channels = BTreeMap::new();
955955
for _ in 0..channels_count {
@@ -965,6 +965,7 @@ impl Readable for NetworkGraph {
965965
nodes.insert(node_id, node_info);
966966
}
967967
Ok(NetworkGraph {
968+
genesis_hash,
968969
channels,
969970
nodes,
970971
})
@@ -1010,8 +1011,9 @@ impl NetworkGraph {
10101011
}
10111012

10121013
/// Creates a new, empty, network graph.
1013-
pub fn new() -> NetworkGraph {
1014+
pub fn new(genesis_hash: BlockHash) -> NetworkGraph {
10141015
Self {
1016+
genesis_hash,
10151017
channels: BTreeMap::new(),
10161018
nodes: BTreeMap::new(),
10171019
}
@@ -1318,7 +1320,8 @@ mod tests {
13181320
fn create_net_graph_msg_handler() -> (Secp256k1<All>, NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>) {
13191321
let secp_ctx = Secp256k1::new();
13201322
let logger = Arc::new(test_utils::TestLogger::new());
1321-
let net_graph_msg_handler = NetGraphMsgHandler::new(None, Arc::clone(&logger));
1323+
let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
1324+
let net_graph_msg_handler = NetGraphMsgHandler::new(genesis_hash, None, Arc::clone(&logger));
13221325
(secp_ctx, net_graph_msg_handler)
13231326
}
13241327

@@ -1479,7 +1482,7 @@ mod tests {
14791482
};
14801483

14811484
// Test if the UTXO lookups were not supported
1482-
let mut net_graph_msg_handler = NetGraphMsgHandler::new(None, Arc::clone(&logger));
1485+
let mut net_graph_msg_handler = NetGraphMsgHandler::new(genesis_block(Network::Testnet).header.block_hash(), None, Arc::clone(&logger));
14831486
match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
14841487
Ok(res) => assert!(res),
14851488
_ => panic!()
@@ -1503,7 +1506,7 @@ mod tests {
15031506
// Test if an associated transaction were not on-chain (or not confirmed).
15041507
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
15051508
*chain_source.utxo_ret.lock().unwrap() = Err(chain::AccessError::UnknownTx);
1506-
net_graph_msg_handler = NetGraphMsgHandler::new(Some(chain_source.clone()), Arc::clone(&logger));
1509+
net_graph_msg_handler = NetGraphMsgHandler::new(chain_source.clone().genesis_hash, Some(chain_source.clone()), Arc::clone(&logger));
15071510
unsigned_announcement.short_channel_id += 1;
15081511

15091512
msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
@@ -1627,7 +1630,7 @@ mod tests {
16271630
let secp_ctx = Secp256k1::new();
16281631
let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
16291632
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
1630-
let net_graph_msg_handler = NetGraphMsgHandler::new(Some(chain_source.clone()), Arc::clone(&logger));
1633+
let net_graph_msg_handler = NetGraphMsgHandler::new(genesis_block(Network::Testnet).header.block_hash(), Some(chain_source.clone()), Arc::clone(&logger));
16311634

16321635
let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
16331636
let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();

lightning/src/routing/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ mod tests {
533533
fn build_graph() -> (Secp256k1<All>, NetGraphMsgHandler<std::sync::Arc<crate::util::test_utils::TestChainSource>, std::sync::Arc<crate::util::test_utils::TestLogger>>, std::sync::Arc<test_utils::TestLogger>) {
534534
let secp_ctx = Secp256k1::new();
535535
let logger = Arc::new(test_utils::TestLogger::new());
536-
let net_graph_msg_handler = NetGraphMsgHandler::new(None, Arc::clone(&logger));
536+
let net_graph_msg_handler = NetGraphMsgHandler::new(genesis_block(Network::Testnet).header.block_hash(), None, Arc::clone(&logger));
537537
// Build network from our_id to node7:
538538
//
539539
// -1(1)2- node0 -1(3)2-

0 commit comments

Comments
 (0)