From: bmancini55 Date: Sun, 29 Nov 2020 20:20:35 +0000 (-0500) Subject: Add genesis block hash to NetworkGraph X-Git-Tag: v0.0.13~50^2~6 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=d183b975da73c40c513feca92e09f8295b70b13f;p=rust-lightning 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. --- diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index 3aeb3d233..6375d0765 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -19,6 +19,7 @@ use bitcoin::blockdata::script::{Builder, Script}; use bitcoin::blockdata::opcodes; use bitcoin::consensus::encode::deserialize; use bitcoin::network::constants::Network; +use bitcoin::blockdata::constants::genesis_block; use bitcoin::hashes::Hash as TraitImport; use bitcoin::hashes::HashEngine as TraitImportEngine; @@ -343,7 +344,7 @@ pub fn do_test(data: &[u8], logger: &Arc) { config.peer_channel_config_limits.min_dust_limit_satoshis = 0; let channelmanager = Arc::new(ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone(), broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0)); let our_id = PublicKey::from_secret_key(&Secp256k1::signing_only(), &keys_manager.get_node_secret()); - let net_graph_msg_handler = Arc::new(NetGraphMsgHandler::new(None, Arc::clone(&logger))); + let net_graph_msg_handler = Arc::new(NetGraphMsgHandler::new(genesis_block(Network::Bitcoin).header.block_hash(), None, Arc::clone(&logger))); let peers = RefCell::new([false; 256]); let mut loss_detector = MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), PeerManager::new(MessageHandler { @@ -609,7 +610,7 @@ mod tests { // What each byte represents is broken down below, and then everything is concatenated into // one large test at the end (you want %s/ -.*//g %s/\n\| \|\t\|\///g). - // Following BOLT 8, lightning message on the wire are: 2-byte encrypted message length + + // Following BOLT 8, lightning message on the wire are: 2-byte encrypted message length + // 16-byte MAC of the encrypted message length + encrypted Lightning message + 16-byte MAC // of the Lightning message // I.e 2nd inbound read, len 18 : 0006 (encrypted message length) + 03000000000000000000000000000000 (MAC of the encrypted message length) diff --git a/fuzz/src/router.rs b/fuzz/src/router.rs index 671adcfda..4eb85714f 100644 --- a/fuzz/src/router.rs +++ b/fuzz/src/router.rs @@ -21,6 +21,8 @@ use lightning::util::ser::Readable; use lightning::routing::network_graph::{NetworkGraph, RoutingFees}; use bitcoin::secp256k1::key::PublicKey; +use bitcoin::network::constants::Network; +use bitcoin::blockdata::constants::genesis_block; use utils::test_logger; @@ -155,7 +157,7 @@ pub fn do_test(data: &[u8], out: Out) { let logger: Arc = Arc::new(test_logger::TestLogger::new("".to_owned(), out)); let our_pubkey = get_pubkey!(); - let mut net_graph = NetworkGraph::new(); + let mut net_graph = NetworkGraph::new(genesis_block(Network::Bitcoin).header.block_hash()); let mut node_pks = HashSet::new(); let mut scid = 42; diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 8ff3d4eac..d54045f8e 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -1171,7 +1171,7 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec, nodes: BTreeMap, } @@ -87,13 +88,10 @@ impl NetGraphMsgHandler where C::Target: chain::Access /// Chain monitor is used to make sure announced channels exist on-chain, /// channel data is correct, and that the announcement is signed with /// channel owners' keys. - pub fn new(chain_access: Option, logger: L) -> Self { + pub fn new(genesis_hash: BlockHash, chain_access: Option, logger: L) -> Self { NetGraphMsgHandler { secp_ctx: Secp256k1::verification_only(), - network_graph: RwLock::new(NetworkGraph { - channels: BTreeMap::new(), - nodes: BTreeMap::new(), - }), + network_graph: RwLock::new(NetworkGraph::new(genesis_hash)), full_syncs_requested: AtomicUsize::new(0), chain_access, pending_events: Mutex::new(vec![]), @@ -903,6 +901,7 @@ impl Readable for NodeInfo { impl Writeable for NetworkGraph { fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + self.genesis_hash.write(writer)?; (self.channels.len() as u64).write(writer)?; for (ref chan_id, ref chan_info) in self.channels.iter() { (*chan_id).write(writer)?; @@ -919,6 +918,7 @@ impl Writeable for NetworkGraph { impl Readable for NetworkGraph { fn read(reader: &mut R) -> Result { + let genesis_hash: BlockHash = Readable::read(reader)?; let channels_count: u64 = Readable::read(reader)?; let mut channels = BTreeMap::new(); for _ in 0..channels_count { @@ -934,6 +934,7 @@ impl Readable for NetworkGraph { nodes.insert(node_id, node_info); } Ok(NetworkGraph { + genesis_hash, channels, nodes, }) @@ -979,8 +980,9 @@ impl NetworkGraph { } /// Creates a new, empty, network graph. - pub fn new() -> NetworkGraph { + pub fn new(genesis_hash: BlockHash) -> NetworkGraph { Self { + genesis_hash, channels: BTreeMap::new(), nodes: BTreeMap::new(), } @@ -1365,7 +1367,8 @@ mod tests { fn create_net_graph_msg_handler() -> (Secp256k1, NetGraphMsgHandler, Arc>) { let secp_ctx = Secp256k1::new(); let logger = Arc::new(test_utils::TestLogger::new()); - let net_graph_msg_handler = NetGraphMsgHandler::new(None, Arc::clone(&logger)); + let genesis_hash = genesis_block(Network::Testnet).header.block_hash(); + let net_graph_msg_handler = NetGraphMsgHandler::new(genesis_hash, None, Arc::clone(&logger)); (secp_ctx, net_graph_msg_handler) } @@ -1526,7 +1529,7 @@ mod tests { }; // Test if the UTXO lookups were not supported - let mut net_graph_msg_handler = NetGraphMsgHandler::new(None, Arc::clone(&logger)); + let mut net_graph_msg_handler = NetGraphMsgHandler::new(genesis_block(Network::Testnet).header.block_hash(), None, Arc::clone(&logger)); match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) { Ok(res) => assert!(res), _ => panic!() @@ -1550,7 +1553,7 @@ mod tests { // 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); - net_graph_msg_handler = NetGraphMsgHandler::new(Some(chain_source.clone()), Arc::clone(&logger)); + net_graph_msg_handler = NetGraphMsgHandler::new(chain_source.clone().genesis_hash, Some(chain_source.clone()), Arc::clone(&logger)); unsigned_announcement.short_channel_id += 1; msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); @@ -1674,7 +1677,7 @@ mod tests { let secp_ctx = Secp256k1::new(); let logger: Arc = Arc::new(test_utils::TestLogger::new()); let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet)); - let net_graph_msg_handler = NetGraphMsgHandler::new(Some(chain_source.clone()), Arc::clone(&logger)); + let net_graph_msg_handler = NetGraphMsgHandler::new(genesis_block(Network::Testnet).header.block_hash(), Some(chain_source.clone()), Arc::clone(&logger)); let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 490b6b404..f73a9a501 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -538,7 +538,7 @@ mod tests { fn build_graph() -> (Secp256k1, NetGraphMsgHandler, std::sync::Arc>, std::sync::Arc) { let secp_ctx = Secp256k1::new(); let logger = Arc::new(test_utils::TestLogger::new()); - let net_graph_msg_handler = NetGraphMsgHandler::new(None, Arc::clone(&logger)); + let net_graph_msg_handler = NetGraphMsgHandler::new(genesis_block(Network::Testnet).header.block_hash(), None, Arc::clone(&logger)); // Build network from our_id to node7: // // -1(1)2- node0 -1(3)2- @@ -1258,7 +1258,7 @@ mod tests { inbound_capacity_msat: 100000, is_live: true, }]; - let route = get_route(&source_node_id, &NetworkGraph::new(), &target_node_id, Some(&our_chans.iter().collect::>()), &last_hops.iter().collect::>(), 100, 42, Arc::new(test_utils::TestLogger::new())).unwrap(); + let route = get_route(&source_node_id, &NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash()), &target_node_id, Some(&our_chans.iter().collect::>()), &last_hops.iter().collect::>(), 100, 42, Arc::new(test_utils::TestLogger::new())).unwrap(); assert_eq!(route.paths[0].len(), 2);