@@ -1057,7 +1057,8 @@ mod tests {
1057
1057
use ln:: channelmanager;
1058
1058
use ln:: router:: { Router , NodeInfo , NetworkMap , ChannelInfo , DirectionalChannelInfo , RouteHint } ;
1059
1059
use ln:: features:: { ChannelFeatures , InitFeatures , NodeFeatures } ;
1060
- use ln:: msgs:: { ErrorAction , LightningError , RoutingMessageHandler , UnsignedNodeAnnouncement , NodeAnnouncement } ;
1060
+ use ln:: msgs:: { ErrorAction , LightningError , RoutingMessageHandler , UnsignedNodeAnnouncement , NodeAnnouncement ,
1061
+ UnsignedChannelAnnouncement , ChannelAnnouncement } ;
1061
1062
use util:: test_utils;
1062
1063
use util:: test_utils:: TestVecWriter ;
1063
1064
use util:: logger:: Logger ;
@@ -1066,6 +1067,8 @@ mod tests {
1066
1067
use bitcoin_hashes:: sha256d:: Hash as Sha256dHash ;
1067
1068
use bitcoin_hashes:: Hash ;
1068
1069
use bitcoin:: network:: constants:: Network ;
1070
+ use bitcoin:: blockdata:: constants:: genesis_block;
1071
+ use bitcoin:: util:: hash:: BitcoinHash ;
1069
1072
1070
1073
use hex;
1071
1074
@@ -1074,6 +1077,7 @@ mod tests {
1074
1077
use secp256k1:: Secp256k1 ;
1075
1078
1076
1079
use std:: sync:: Arc ;
1080
+ use std:: collections:: btree_map:: Entry as BtreeEntry ;
1077
1081
1078
1082
fn create_router ( ) -> ( Secp256k1 < All > , PublicKey , Router ) {
1079
1083
let secp_ctx = Secp256k1 :: new ( ) ;
@@ -1949,4 +1953,130 @@ mod tests {
1949
1953
Err ( e) => assert_eq ! ( e. err, "Update older than last processed update" )
1950
1954
} ;
1951
1955
}
1956
+
1957
+ #[ test]
1958
+ fn handling_channel_announcements ( ) {
1959
+ let ( secp_ctx, _, router) = create_router ( ) ;
1960
+ let node_1_privkey = & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
1961
+ let node_2_privkey = & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ;
1962
+ let node_id_1 = PublicKey :: from_secret_key ( & secp_ctx, node_1_privkey) ;
1963
+ let node_id_2 = PublicKey :: from_secret_key ( & secp_ctx, node_2_privkey) ;
1964
+ let node_1_btckey = & SecretKey :: from_slice ( & [ 40 ; 32 ] ) . unwrap ( ) ;
1965
+ let node_2_btckey = & SecretKey :: from_slice ( & [ 39 ; 32 ] ) . unwrap ( ) ;
1966
+
1967
+ let mut unsigned_announcement = UnsignedChannelAnnouncement {
1968
+ features : ChannelFeatures :: supported ( ) ,
1969
+ chain_hash : genesis_block ( Network :: Testnet ) . header . bitcoin_hash ( ) ,
1970
+ short_channel_id : 0 ,
1971
+ node_id_1,
1972
+ node_id_2,
1973
+ bitcoin_key_1 : PublicKey :: from_secret_key ( & secp_ctx, node_1_btckey) ,
1974
+ bitcoin_key_2 : PublicKey :: from_secret_key ( & secp_ctx, node_2_btckey) ,
1975
+ excess_data : Vec :: new ( ) ,
1976
+ } ;
1977
+
1978
+ let channel_key = NetworkMap :: get_key ( unsigned_announcement. short_channel_id ,
1979
+ unsigned_announcement. chain_hash ) ;
1980
+
1981
+ let mut msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
1982
+ let valid_announcement = ChannelAnnouncement {
1983
+ node_signature_1 : secp_ctx. sign ( & msghash, node_1_privkey) ,
1984
+ node_signature_2 : secp_ctx. sign ( & msghash, node_2_privkey) ,
1985
+ bitcoin_signature_1 : secp_ctx. sign ( & msghash, node_1_btckey) ,
1986
+ bitcoin_signature_2 : secp_ctx. sign ( & msghash, node_2_btckey) ,
1987
+ contents : unsigned_announcement. clone ( ) ,
1988
+ } ;
1989
+
1990
+ // So the channel doesn't exist on chain here. Lookup returns ChainError::NotSupported
1991
+ // instead of UnknownTx, chain lookup seems to be not implemented yet.
1992
+ // match router.handle_channel_announcement(&valid_announcement) {
1993
+ // Ok(_) => panic!(),
1994
+ // Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry")
1995
+ // };
1996
+
1997
+
1998
+ match router. handle_channel_announcement ( & valid_announcement) {
1999
+ Ok ( res) => assert ! ( res) ,
2000
+ _ => panic ! ( )
2001
+ } ;
2002
+ {
2003
+ let mut network = router. network_map . write ( ) . unwrap ( ) ;
2004
+ match network. nodes . entry ( node_id_1) {
2005
+ BtreeEntry :: Occupied ( node_entry) => {
2006
+ assert_eq ! ( node_entry. into_mut( ) . channels[ 0 ] , channel_key) ;
2007
+ } ,
2008
+ _ => panic ! ( )
2009
+ }
2010
+ match network. nodes . entry ( node_id_2) {
2011
+ BtreeEntry :: Occupied ( node_entry) => {
2012
+ assert_eq ! ( node_entry. into_mut( ) . channels[ 0 ] , channel_key) ;
2013
+ } ,
2014
+ _ => panic ! ( )
2015
+ }
2016
+ }
2017
+
2018
+ match router. handle_channel_announcement ( & valid_announcement) {
2019
+ Ok ( _) => panic ! ( ) ,
2020
+ Err ( e) => assert_eq ! ( e. err, "Already have knowledge of channel" )
2021
+ } ;
2022
+
2023
+ unsigned_announcement. short_channel_id += 1 ;
2024
+
2025
+ unsigned_announcement. excess_data . push ( 1 ) ;
2026
+ msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
2027
+ let valid_announcement = ChannelAnnouncement {
2028
+ node_signature_1 : secp_ctx. sign ( & msghash, node_1_privkey) ,
2029
+ node_signature_2 : secp_ctx. sign ( & msghash, node_2_privkey) ,
2030
+ bitcoin_signature_1 : secp_ctx. sign ( & msghash, node_1_btckey) ,
2031
+ bitcoin_signature_2 : secp_ctx. sign ( & msghash, node_2_btckey) ,
2032
+ contents : unsigned_announcement. clone ( ) ,
2033
+ } ;
2034
+ // Returns false because contains excess data.
2035
+ match router. handle_channel_announcement ( & valid_announcement) {
2036
+ Ok ( res) => assert ! ( !res) ,
2037
+ _ => panic ! ( )
2038
+ } ;
2039
+ unsigned_announcement. excess_data = Vec :: new ( ) ;
2040
+
2041
+ let invalid_sig_announcement = ChannelAnnouncement {
2042
+ node_signature_1 : secp_ctx. sign ( & msghash, node_1_privkey) ,
2043
+ node_signature_2 : secp_ctx. sign ( & msghash, node_2_privkey) ,
2044
+ bitcoin_signature_1 : secp_ctx. sign ( & msghash, node_1_btckey) ,
2045
+ bitcoin_signature_2 : secp_ctx. sign ( & msghash, node_1_btckey) ,
2046
+ contents : unsigned_announcement. clone ( ) ,
2047
+ } ;
2048
+ match router. handle_channel_announcement ( & invalid_sig_announcement) {
2049
+ Ok ( _) => panic ! ( ) ,
2050
+ Err ( e) => assert_eq ! ( e. err, "Invalid signature from remote node" )
2051
+ } ;
2052
+
2053
+ unsigned_announcement. chain_hash = genesis_block ( Network :: Regtest ) . header . bitcoin_hash ( ) ;
2054
+ msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
2055
+ let invalid_chainhash_announcement = ChannelAnnouncement {
2056
+ node_signature_1 : secp_ctx. sign ( & msghash, node_1_privkey) ,
2057
+ node_signature_2 : secp_ctx. sign ( & msghash, node_2_privkey) ,
2058
+ bitcoin_signature_1 : secp_ctx. sign ( & msghash, node_1_btckey) ,
2059
+ bitcoin_signature_2 : secp_ctx. sign ( & msghash, node_2_btckey) ,
2060
+ contents : unsigned_announcement. clone ( ) ,
2061
+ } ;
2062
+ match router. handle_channel_announcement ( & invalid_chainhash_announcement) {
2063
+ Ok ( _) => panic ! ( ) ,
2064
+ Err ( e) => assert_eq ! ( e. err, "Channel announced on an unknown chain" )
2065
+ } ;
2066
+ unsigned_announcement. chain_hash = genesis_block ( Network :: Testnet ) . header . bitcoin_hash ( ) ;
2067
+
2068
+ unsigned_announcement. node_id_1 = PublicKey :: from_secret_key ( & secp_ctx, node_2_privkey) ;
2069
+ msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
2070
+ let channel_to_itself_announcement = ChannelAnnouncement {
2071
+ node_signature_1 : secp_ctx. sign ( & msghash, node_1_privkey) ,
2072
+ node_signature_2 : secp_ctx. sign ( & msghash, node_1_privkey) ,
2073
+ bitcoin_signature_1 : secp_ctx. sign ( & msghash, node_1_btckey) ,
2074
+ bitcoin_signature_2 : secp_ctx. sign ( & msghash, node_2_btckey) ,
2075
+ contents : unsigned_announcement. clone ( ) ,
2076
+ } ;
2077
+ match router. handle_channel_announcement ( & channel_to_itself_announcement) {
2078
+ Ok ( _) => panic ! ( ) ,
2079
+ Err ( e) => assert_eq ! ( e. err, "Channel announcement node had a channel with itself" )
2080
+ } ;
2081
+ }
1952
2082
}
0 commit comments