@@ -55,6 +55,7 @@ const MAX_REPLY_CHANNEL_RANGE_PER_QUERY: usize = 250;
55
55
/// Represents the network as nodes and channels between them
56
56
#[ derive( PartialEq ) ]
57
57
pub struct NetworkGraph {
58
+ genesis_hash : BlockHash ,
58
59
channels : BTreeMap < u64 , ChannelInfo > ,
59
60
nodes : BTreeMap < PublicKey , NodeInfo > ,
60
61
}
@@ -87,13 +88,10 @@ impl<C: Deref, L: Deref> NetGraphMsgHandler<C, L> where C::Target: chain::Access
87
88
/// Chain monitor is used to make sure announced channels exist on-chain,
88
89
/// channel data is correct, and that the announcement is signed with
89
90
/// 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 {
91
92
NetGraphMsgHandler {
92
93
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) ) ,
97
95
full_syncs_requested : AtomicUsize :: new ( 0 ) ,
98
96
chain_access,
99
97
pending_events : Mutex :: new ( vec ! [ ] ) ,
@@ -934,6 +932,7 @@ impl Readable for NodeInfo {
934
932
935
933
impl Writeable for NetworkGraph {
936
934
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
935
+ self . genesis_hash . write ( writer) ?;
937
936
( self . channels . len ( ) as u64 ) . write ( writer) ?;
938
937
for ( ref chan_id, ref chan_info) in self . channels . iter ( ) {
939
938
( * chan_id) . write ( writer) ?;
@@ -950,6 +949,7 @@ impl Writeable for NetworkGraph {
950
949
951
950
impl Readable for NetworkGraph {
952
951
fn read < R : :: std:: io:: Read > ( reader : & mut R ) -> Result < NetworkGraph , DecodeError > {
952
+ let genesis_hash: BlockHash = Readable :: read ( reader) ?;
953
953
let channels_count: u64 = Readable :: read ( reader) ?;
954
954
let mut channels = BTreeMap :: new ( ) ;
955
955
for _ in 0 ..channels_count {
@@ -965,6 +965,7 @@ impl Readable for NetworkGraph {
965
965
nodes. insert ( node_id, node_info) ;
966
966
}
967
967
Ok ( NetworkGraph {
968
+ genesis_hash,
968
969
channels,
969
970
nodes,
970
971
} )
@@ -1010,8 +1011,9 @@ impl NetworkGraph {
1010
1011
}
1011
1012
1012
1013
/// Creates a new, empty, network graph.
1013
- pub fn new ( ) -> NetworkGraph {
1014
+ pub fn new ( genesis_hash : BlockHash ) -> NetworkGraph {
1014
1015
Self {
1016
+ genesis_hash,
1015
1017
channels : BTreeMap :: new ( ) ,
1016
1018
nodes : BTreeMap :: new ( ) ,
1017
1019
}
@@ -1318,7 +1320,8 @@ mod tests {
1318
1320
fn create_net_graph_msg_handler ( ) -> ( Secp256k1 < All > , NetGraphMsgHandler < Arc < test_utils:: TestChainSource > , Arc < test_utils:: TestLogger > > ) {
1319
1321
let secp_ctx = Secp256k1 :: new ( ) ;
1320
1322
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) ) ;
1322
1325
( secp_ctx, net_graph_msg_handler)
1323
1326
}
1324
1327
@@ -1479,7 +1482,7 @@ mod tests {
1479
1482
} ;
1480
1483
1481
1484
// 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) ) ;
1483
1486
match net_graph_msg_handler. handle_channel_announcement ( & valid_announcement) {
1484
1487
Ok ( res) => assert ! ( res) ,
1485
1488
_ => panic ! ( )
@@ -1503,7 +1506,7 @@ mod tests {
1503
1506
// Test if an associated transaction were not on-chain (or not confirmed).
1504
1507
let chain_source = Arc :: new ( test_utils:: TestChainSource :: new ( Network :: Testnet ) ) ;
1505
1508
* 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) ) ;
1507
1510
unsigned_announcement. short_channel_id += 1 ;
1508
1511
1509
1512
msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
@@ -1627,7 +1630,7 @@ mod tests {
1627
1630
let secp_ctx = Secp256k1 :: new ( ) ;
1628
1631
let logger: Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
1629
1632
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) ) ;
1631
1634
1632
1635
let node_1_privkey = & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
1633
1636
let node_2_privkey = & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ;
0 commit comments