@@ -1231,6 +1231,18 @@ pub struct NodeInfo {
1231
1231
pub announcement_info : Option < NodeAnnouncementInfo >
1232
1232
}
1233
1233
1234
+ impl NodeInfo {
1235
+ /// Returns whether the node has only announced Tor addresses.
1236
+ pub fn is_tor_only ( & self ) -> bool {
1237
+ self . announcement_info
1238
+ . as_ref ( )
1239
+ . map ( |info| info. addresses ( ) )
1240
+ . and_then ( |addresses| ( !addresses. is_empty ( ) ) . then ( || addresses) )
1241
+ . map ( |addresses| addresses. iter ( ) . all ( |address| address. is_tor ( ) ) )
1242
+ . unwrap_or ( false )
1243
+ }
1244
+ }
1245
+
1234
1246
impl fmt:: Display for NodeInfo {
1235
1247
fn fmt ( & self , f : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
1236
1248
write ! ( f, " channels: {:?}, announcement_info: {:?}" ,
@@ -2089,14 +2101,15 @@ pub(crate) mod tests {
2089
2101
use crate :: ln:: chan_utils:: make_funding_redeemscript;
2090
2102
#[ cfg( feature = "std" ) ]
2091
2103
use crate :: ln:: features:: InitFeatures ;
2104
+ use crate :: ln:: msgs:: SocketAddress ;
2092
2105
use crate :: routing:: gossip:: { P2PGossipSync , NetworkGraph , NetworkUpdate , NodeAlias , MAX_EXCESS_BYTES_FOR_RELAY , NodeId , RoutingFees , ChannelUpdateInfo , ChannelInfo , NodeAnnouncementInfo , NodeInfo } ;
2093
2106
use crate :: routing:: utxo:: { UtxoLookupError , UtxoResult } ;
2094
2107
use crate :: ln:: msgs:: { RoutingMessageHandler , UnsignedNodeAnnouncement , NodeAnnouncement ,
2095
2108
UnsignedChannelAnnouncement , ChannelAnnouncement , UnsignedChannelUpdate , ChannelUpdate ,
2096
2109
ReplyChannelRange , QueryChannelRange , QueryShortChannelIds , MAX_VALUE_MSAT } ;
2097
2110
use crate :: util:: config:: UserConfig ;
2098
2111
use crate :: util:: test_utils;
2099
- use crate :: util:: ser:: { ReadableArgs , Readable , Writeable } ;
2112
+ use crate :: util:: ser:: { Hostname , ReadableArgs , Readable , Writeable } ;
2100
2113
use crate :: util:: scid_utils:: scid_from_parts;
2101
2114
2102
2115
use crate :: routing:: gossip:: REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS ;
@@ -3474,6 +3487,112 @@ pub(crate) mod tests {
3474
3487
let node_id = NodeId ( [ 42 ; 33 ] ) ;
3475
3488
assert_eq ! ( format!( "{}" , & node_id) , "2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a" ) ;
3476
3489
}
3490
+
3491
+ #[ test]
3492
+ fn is_tor_only_node ( ) {
3493
+ let network_graph = create_network_graph ( ) ;
3494
+ let ( secp_ctx, gossip_sync) = create_gossip_sync ( & network_graph) ;
3495
+
3496
+ let node_1_privkey = & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
3497
+ let node_2_privkey = & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ;
3498
+ let node_1_id = NodeId :: from_pubkey ( & PublicKey :: from_secret_key ( & secp_ctx, node_1_privkey) ) ;
3499
+
3500
+ let announcement = get_signed_channel_announcement ( |_| { } , node_1_privkey, node_2_privkey, & secp_ctx) ;
3501
+ gossip_sync. handle_channel_announcement ( & announcement) . unwrap ( ) ;
3502
+
3503
+ let tcp_ip_v4 = SocketAddress :: TcpIpV4 {
3504
+ addr : [ 255 , 254 , 253 , 252 ] ,
3505
+ port : 9735
3506
+ } ;
3507
+ let tcp_ip_v6 = SocketAddress :: TcpIpV6 {
3508
+ addr : [ 255 , 254 , 253 , 252 , 251 , 250 , 249 , 248 , 247 , 246 , 245 , 244 , 243 , 242 , 241 , 240 ] ,
3509
+ port : 9735
3510
+ } ;
3511
+ let onion_v2 = SocketAddress :: OnionV2 ( [ 255 , 254 , 253 , 252 , 251 , 250 , 249 , 248 , 247 , 246 , 38 , 7 ] ) ;
3512
+ let onion_v3 = SocketAddress :: OnionV3 {
3513
+ ed25519_pubkey : [ 255 , 254 , 253 , 252 , 251 , 250 , 249 , 248 , 247 , 246 , 245 , 244 , 243 , 242 , 241 , 240 , 239 , 238 , 237 , 236 , 235 , 234 , 233 , 232 , 231 , 230 , 229 , 228 , 227 , 226 , 225 , 224 ] ,
3514
+ checksum : 32 ,
3515
+ version : 16 ,
3516
+ port : 9735
3517
+ } ;
3518
+ let hostname = SocketAddress :: Hostname {
3519
+ hostname : Hostname :: try_from ( String :: from ( "host" ) ) . unwrap ( ) ,
3520
+ port : 9735 ,
3521
+ } ;
3522
+
3523
+ assert ! ( !network_graph. read_only( ) . node( & node_1_id) . unwrap( ) . is_tor_only( ) ) ;
3524
+
3525
+ let announcement = get_signed_node_announcement ( |_| { } , node_1_privkey, & secp_ctx) ;
3526
+ gossip_sync. handle_node_announcement ( & announcement) . unwrap ( ) ;
3527
+ assert ! ( !network_graph. read_only( ) . node( & node_1_id) . unwrap( ) . is_tor_only( ) ) ;
3528
+
3529
+ let announcement = get_signed_node_announcement (
3530
+ |announcement| {
3531
+ announcement. addresses = vec ! [
3532
+ tcp_ip_v4. clone( ) , tcp_ip_v6. clone( ) , onion_v2. clone( ) , onion_v3. clone( ) ,
3533
+ hostname. clone( )
3534
+ ] ;
3535
+ announcement. timestamp += 1000 ;
3536
+ } ,
3537
+ node_1_privkey, & secp_ctx
3538
+ ) ;
3539
+ gossip_sync. handle_node_announcement ( & announcement) . unwrap ( ) ;
3540
+ assert ! ( !network_graph. read_only( ) . node( & node_1_id) . unwrap( ) . is_tor_only( ) ) ;
3541
+
3542
+ let announcement = get_signed_node_announcement (
3543
+ |announcement| {
3544
+ announcement. addresses = vec ! [
3545
+ tcp_ip_v4. clone( ) , tcp_ip_v6. clone( ) , onion_v2. clone( ) , onion_v3. clone( )
3546
+ ] ;
3547
+ announcement. timestamp += 2000 ;
3548
+ } ,
3549
+ node_1_privkey, & secp_ctx
3550
+ ) ;
3551
+ gossip_sync. handle_node_announcement ( & announcement) . unwrap ( ) ;
3552
+ assert ! ( !network_graph. read_only( ) . node( & node_1_id) . unwrap( ) . is_tor_only( ) ) ;
3553
+
3554
+ let announcement = get_signed_node_announcement (
3555
+ |announcement| {
3556
+ announcement. addresses = vec ! [
3557
+ tcp_ip_v6. clone( ) , onion_v2. clone( ) , onion_v3. clone( )
3558
+ ] ;
3559
+ announcement. timestamp += 3000 ;
3560
+ } ,
3561
+ node_1_privkey, & secp_ctx
3562
+ ) ;
3563
+ gossip_sync. handle_node_announcement ( & announcement) . unwrap ( ) ;
3564
+ assert ! ( !network_graph. read_only( ) . node( & node_1_id) . unwrap( ) . is_tor_only( ) ) ;
3565
+
3566
+ let announcement = get_signed_node_announcement (
3567
+ |announcement| {
3568
+ announcement. addresses = vec ! [ onion_v2. clone( ) , onion_v3. clone( ) ] ;
3569
+ announcement. timestamp += 4000 ;
3570
+ } ,
3571
+ node_1_privkey, & secp_ctx
3572
+ ) ;
3573
+ gossip_sync. handle_node_announcement ( & announcement) . unwrap ( ) ;
3574
+ assert ! ( network_graph. read_only( ) . node( & node_1_id) . unwrap( ) . is_tor_only( ) ) ;
3575
+
3576
+ let announcement = get_signed_node_announcement (
3577
+ |announcement| {
3578
+ announcement. addresses = vec ! [ onion_v2. clone( ) ] ;
3579
+ announcement. timestamp += 5000 ;
3580
+ } ,
3581
+ node_1_privkey, & secp_ctx
3582
+ ) ;
3583
+ gossip_sync. handle_node_announcement ( & announcement) . unwrap ( ) ;
3584
+ assert ! ( network_graph. read_only( ) . node( & node_1_id) . unwrap( ) . is_tor_only( ) ) ;
3585
+
3586
+ let announcement = get_signed_node_announcement (
3587
+ |announcement| {
3588
+ announcement. addresses = vec ! [ tcp_ip_v4. clone( ) ] ;
3589
+ announcement. timestamp += 6000 ;
3590
+ } ,
3591
+ node_1_privkey, & secp_ctx
3592
+ ) ;
3593
+ gossip_sync. handle_node_announcement ( & announcement) . unwrap ( ) ;
3594
+ assert ! ( !network_graph. read_only( ) . node( & node_1_id) . unwrap( ) . is_tor_only( ) ) ;
3595
+ }
3477
3596
}
3478
3597
3479
3598
#[ cfg( ldk_bench) ]
0 commit comments