@@ -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 } ;
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
@@ -1864,4 +1867,106 @@ mod tests {
1864
1867
assert ! ( router. should_request_full_sync( & node_id) ) ;
1865
1868
assert ! ( !router. should_request_full_sync( & node_id) ) ;
1866
1869
}
1870
+
1871
+ #[ test]
1872
+ fn handling_node_announcements ( ) {
1873
+ let ( secp_ctx, _, router) = create_router ( ) ;
1874
+
1875
+ let node_1_privkey = & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
1876
+ let node_2_privkey = & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ;
1877
+ let node_id_1 = PublicKey :: from_secret_key ( & secp_ctx, node_1_privkey) ;
1878
+ let node_id_2 = PublicKey :: from_secret_key ( & secp_ctx, node_2_privkey) ;
1879
+ let node_1_btckey = & SecretKey :: from_slice ( & [ 40 ; 32 ] ) . unwrap ( ) ;
1880
+ let node_2_btckey = & SecretKey :: from_slice ( & [ 39 ; 32 ] ) . unwrap ( ) ;
1881
+ let zero_hash = Sha256dHash :: hash ( & [ 0 ; 32 ] ) ;
1882
+ let first_announcement_time = 500 ;
1883
+
1884
+ let mut unsigned_announcement = UnsignedNodeAnnouncement {
1885
+ features : NodeFeatures :: supported ( ) ,
1886
+ timestamp : first_announcement_time + 1000 ,
1887
+ node_id : node_id_1,
1888
+ rgb : [ 0 ; 3 ] ,
1889
+ alias : [ 0 ; 32 ] ,
1890
+ addresses : Vec :: new ( ) ,
1891
+ excess_address_data : Vec :: new ( ) ,
1892
+ excess_data : Vec :: new ( ) ,
1893
+ } ;
1894
+ let mut msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
1895
+ let valid_announcement = NodeAnnouncement {
1896
+ signature : secp_ctx. sign ( & msghash, node_1_privkey) ,
1897
+ contents : unsigned_announcement. clone ( )
1898
+ } ;
1899
+
1900
+ match router. handle_node_announcement ( & valid_announcement) {
1901
+ Ok ( _) => panic ! ( ) ,
1902
+ Err ( e) => assert_eq ! ( "No existing channels for node_announcement" , e. err)
1903
+ } ;
1904
+
1905
+ {
1906
+ // Announce a channel to add a corresponding node.
1907
+ let unsigned_announcement = UnsignedChannelAnnouncement {
1908
+ features : ChannelFeatures :: supported ( ) ,
1909
+ chain_hash : genesis_block ( Network :: Testnet ) . header . bitcoin_hash ( ) ,
1910
+ short_channel_id : 0 ,
1911
+ node_id_1,
1912
+ node_id_2,
1913
+ bitcoin_key_1 : PublicKey :: from_secret_key ( & secp_ctx, node_1_btckey) ,
1914
+ bitcoin_key_2 : PublicKey :: from_secret_key ( & secp_ctx, node_2_btckey) ,
1915
+ excess_data : Vec :: new ( ) ,
1916
+ } ;
1917
+
1918
+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
1919
+ let valid_announcement = ChannelAnnouncement {
1920
+ node_signature_1 : secp_ctx. sign ( & msghash, node_1_privkey) ,
1921
+ node_signature_2 : secp_ctx. sign ( & msghash, node_2_privkey) ,
1922
+ bitcoin_signature_1 : secp_ctx. sign ( & msghash, node_1_btckey) ,
1923
+ bitcoin_signature_2 : secp_ctx. sign ( & msghash, node_2_btckey) ,
1924
+ contents : unsigned_announcement. clone ( ) ,
1925
+ } ;
1926
+ match router. handle_channel_announcement ( & valid_announcement) {
1927
+ Ok ( res) => assert ! ( res) ,
1928
+ _ => panic ! ( )
1929
+ } ;
1930
+ }
1931
+
1932
+ match router. handle_node_announcement ( & valid_announcement) {
1933
+ Ok ( res) => assert ! ( res) ,
1934
+ Err ( _) => panic ! ( )
1935
+ } ;
1936
+
1937
+ let fake_msghash = hash_to_message ! ( & zero_hash) ;
1938
+ match router. handle_node_announcement (
1939
+ & NodeAnnouncement {
1940
+ signature : secp_ctx. sign ( & fake_msghash, node_1_privkey) ,
1941
+ contents : unsigned_announcement. clone ( )
1942
+ } ) {
1943
+ Ok ( _) => panic ! ( ) ,
1944
+ Err ( e) => assert_eq ! ( e. err, "Invalid signature from remote node" )
1945
+ } ;
1946
+
1947
+ unsigned_announcement. timestamp += 1000 ;
1948
+ unsigned_announcement. excess_data . push ( 1 ) ;
1949
+ msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
1950
+ let announcement_with_data = NodeAnnouncement {
1951
+ signature : secp_ctx. sign ( & msghash, node_1_privkey) ,
1952
+ contents : unsigned_announcement. clone ( )
1953
+ } ;
1954
+ // Return false because contains excess data.
1955
+ match router. handle_node_announcement ( & announcement_with_data) {
1956
+ Ok ( res) => assert ! ( !res) ,
1957
+ Err ( _) => panic ! ( )
1958
+ } ;
1959
+ unsigned_announcement. excess_data = Vec :: new ( ) ;
1960
+
1961
+ unsigned_announcement. timestamp = 0 ;
1962
+ msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
1963
+ let outdated_announcement = NodeAnnouncement {
1964
+ signature : secp_ctx. sign ( & msghash, node_1_privkey) ,
1965
+ contents : unsigned_announcement. clone ( )
1966
+ } ;
1967
+ match router. handle_node_announcement ( & outdated_announcement) {
1968
+ Ok ( _) => panic ! ( ) ,
1969
+ Err ( e) => assert_eq ! ( e. err, "Update older than last processed update" )
1970
+ } ;
1971
+ }
1867
1972
}
0 commit comments