Update to latest upstream rust-bitcoin
[rust-lightning] / lightning / src / routing / network_graph.rs
index 44f2ed237bf9ef9730ed5c8a0ea9da7db133a1fc..6e51b7d092ab92b86067d71f45703213b31829f3 100644 (file)
@@ -27,7 +27,7 @@ use util::ser::{Writeable, Readable, Writer};
 use util::logger::Logger;
 
 use std::{cmp, fmt};
-use std::sync::RwLock;
+use std::sync::{RwLock, RwLockReadGuard};
 use std::sync::atomic::{AtomicUsize, Ordering};
 use std::collections::BTreeMap;
 use std::collections::btree_map::Entry as BtreeEntry;
@@ -41,6 +41,11 @@ pub struct NetworkGraph {
        nodes: BTreeMap<PublicKey, NodeInfo>,
 }
 
+/// A simple newtype for RwLockReadGuard<'a, NetworkGraph>.
+/// This exists only to make accessing a RwLock<NetworkGraph> possible from
+/// the C bindings, as it can be done directly in Rust code.
+pub struct LockedNetworkGraph<'a>(pub RwLockReadGuard<'a, NetworkGraph>);
+
 /// Receives and validates network updates from peers,
 /// stores authentic and relevant data as a network graph.
 /// This network graph is then used for routing payments.
@@ -85,6 +90,21 @@ impl<C: Deref, L: Deref> NetGraphMsgHandler<C, L> where C::Target: ChainWatchInt
                        logger,
                }
        }
+
+       /// Take a read lock on the network_graph and return it in the C-bindings
+       /// newtype helper. This is likely only useful when called via the C
+       /// bindings as you can call `self.network_graph.read().unwrap()` in Rust
+       /// yourself.
+       pub fn read_locked_graph<'a>(&'a self) -> LockedNetworkGraph<'a> {
+               LockedNetworkGraph(self.network_graph.read().unwrap())
+       }
+}
+
+impl<'a> LockedNetworkGraph<'a> {
+       /// Get a reference to the NetworkGraph which this read-lock contains.
+       pub fn graph(&self) -> &NetworkGraph {
+               &*self.0
+       }
 }
 
 
@@ -818,7 +838,6 @@ mod tests {
        use bitcoin::blockdata::constants::genesis_block;
        use bitcoin::blockdata::script::Builder;
        use bitcoin::blockdata::opcodes;
-       use bitcoin::util::hash::BitcoinHash;
 
        use hex;
 
@@ -886,7 +905,7 @@ mod tests {
                        // Announce a channel to add a corresponding node.
                        let unsigned_announcement = UnsignedChannelAnnouncement {
                                features: ChannelFeatures::known(),
-                               chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
+                               chain_hash: genesis_block(Network::Testnet).header.block_hash(),
                                short_channel_id: 0,
                                node_id_1,
                                node_id_2,
@@ -976,7 +995,7 @@ mod tests {
 
                let mut unsigned_announcement = UnsignedChannelAnnouncement {
                        features: ChannelFeatures::known(),
-                       chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
+                       chain_hash: genesis_block(Network::Testnet).header.block_hash(),
                        short_channel_id: 0,
                        node_id_1,
                        node_id_2,
@@ -1156,7 +1175,7 @@ mod tests {
 
                let zero_hash = Sha256dHash::hash(&[0; 32]);
                let short_channel_id = 0;
-               let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash();
+               let chain_hash = genesis_block(Network::Testnet).header.block_hash();
                let amount_sats = 1000_000;
 
                {
@@ -1319,7 +1338,7 @@ mod tests {
                let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
 
                let short_channel_id = 0;
-               let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash();
+               let chain_hash = genesis_block(Network::Testnet).header.block_hash();
 
                {
                        // There is no nodes in the table at the beginning.
@@ -1434,7 +1453,7 @@ mod tests {
                let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
 
                let short_channel_id = 1;
-               let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash();
+               let chain_hash = genesis_block(Network::Testnet).header.block_hash();
 
                // Channels were not announced yet.
                let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(0, 1);
@@ -1568,7 +1587,7 @@ mod tests {
                let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap();
 
                let short_channel_id = 1;
-               let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash();
+               let chain_hash = genesis_block(Network::Testnet).header.block_hash();
 
                // No nodes yet.
                let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 10);
@@ -1688,7 +1707,7 @@ mod tests {
                let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
                let unsigned_announcement = UnsignedChannelAnnouncement {
                        features: ChannelFeatures::known(),
-                       chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
+                       chain_hash: genesis_block(Network::Testnet).header.block_hash(),
                        short_channel_id: 0,
                        node_id_1,
                        node_id_2,