@@ -28,7 +28,7 @@ use std::time::Instant;
28
28
#[ derive( PartialEq , Clone , Debug ) ]
29
29
pub struct NodeReference {
30
30
pub public_key : PublicKey ,
31
- pub node_addr : NodeAddr ,
31
+ pub node_addr_opt : Option < NodeAddr > ,
32
32
}
33
33
34
34
impl FromStr for NodeReference {
@@ -49,14 +49,19 @@ impl FromStr for NodeReference {
49
49
impl fmt:: Display for NodeReference {
50
50
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
51
51
let public_key_string = base64:: encode_config ( & self . public_key . as_slice ( ) , STANDARD_NO_PAD ) ;
52
- let ip_addr_string = format ! ( "{}" , self . node_addr. ip_addr( ) ) ;
53
- let port_list_string = self
54
- . node_addr
55
- . ports ( )
56
- . iter ( )
57
- . map ( |port| port. to_string ( ) )
58
- . collect :: < Vec < String > > ( )
59
- . join ( "," ) ;
52
+ let ip_addr_string = match & self . node_addr_opt {
53
+ Some ( node_addr) => format ! ( "{}" , node_addr. ip_addr( ) ) ,
54
+ None => String :: new ( ) ,
55
+ } ;
56
+ let port_list_string = match & self . node_addr_opt {
57
+ Some ( node_addr) => node_addr
58
+ . ports ( )
59
+ . iter ( )
60
+ . map ( |port| port. to_string ( ) )
61
+ . collect :: < Vec < String > > ( )
62
+ . join ( "," ) ,
63
+ None => String :: new ( ) ,
64
+ } ;
60
65
write ! (
61
66
f,
62
67
"{}:{}:{}" ,
@@ -68,10 +73,20 @@ impl fmt::Display for NodeReference {
68
73
}
69
74
70
75
impl NodeReference {
71
- pub fn new ( public_key : PublicKey , ip_addr : IpAddr , ports : Vec < u16 > ) -> NodeReference {
72
- NodeReference {
73
- public_key,
74
- node_addr : NodeAddr :: new ( & ip_addr, & ports) ,
76
+ pub fn new (
77
+ public_key : PublicKey ,
78
+ ip_addr_opt : Option < IpAddr > ,
79
+ ports : Vec < u16 > ,
80
+ ) -> NodeReference {
81
+ match ip_addr_opt {
82
+ Some ( ip_addr) => NodeReference {
83
+ public_key,
84
+ node_addr_opt : Some ( NodeAddr :: new ( & ip_addr, & ports) ) ,
85
+ } ,
86
+ None => NodeReference {
87
+ public_key,
88
+ node_addr_opt : None ,
89
+ } ,
75
90
}
76
91
}
77
92
@@ -82,14 +97,18 @@ impl NodeReference {
82
97
}
83
98
}
84
99
85
- fn extract_ip_addr ( slice : & str ) -> Result < IpAddr , String > {
86
- match IpAddr :: from_str ( slice) {
87
- Ok ( ip_addr) => Ok ( ip_addr) ,
88
- Err ( _) => {
89
- return Err ( format ! (
90
- "The IP address of a NodeReference must be valid, not '{}'" ,
91
- slice
92
- ) ) ;
100
+ fn extract_ip_addr ( slice : & str ) -> Result < Option < IpAddr > , String > {
101
+ if slice. is_empty ( ) {
102
+ Ok ( None )
103
+ } else {
104
+ match IpAddr :: from_str ( slice) {
105
+ Ok ( ip_addr) => Ok ( Some ( ip_addr) ) ,
106
+ Err ( _) => {
107
+ return Err ( format ! (
108
+ "The IP address of a NodeReference must be valid, not '{}'" ,
109
+ slice
110
+ ) ) ;
111
+ }
93
112
}
94
113
}
95
114
}
@@ -159,6 +178,8 @@ pub trait SubstratumNode: Any {
159
178
fn rate_pack ( & self ) -> RatePack ;
160
179
// Valid values are "dev, "ropsten" for now. Add "mainnet" when it's time.
161
180
fn chain ( & self ) -> Option < String > ;
181
+ fn accepts_connections ( & self ) -> bool ;
182
+ fn routes_data ( & self ) -> bool ;
162
183
}
163
184
164
185
pub struct SubstratumNodeUtils { }
@@ -319,8 +340,11 @@ mod tests {
319
340
320
341
assert_eq ! ( result. public_key, key) ;
321
342
assert_eq ! (
322
- result. node_addr,
323
- NodeAddr :: new( & IpAddr :: from_str( "12.34.56.78" ) . unwrap( ) , & vec!( 1234 , 2345 ) )
343
+ result. node_addr_opt,
344
+ Some ( NodeAddr :: new(
345
+ & IpAddr :: from_str( "12.34.56.78" ) . unwrap( ) ,
346
+ & vec!( 1234 , 2345 )
347
+ ) )
324
348
) ;
325
349
}
326
350
@@ -333,16 +357,19 @@ mod tests {
333
357
334
358
assert_eq ! ( result. public_key, key) ;
335
359
assert_eq ! (
336
- result. node_addr,
337
- NodeAddr :: new( & IpAddr :: from_str( "12.34.56.78" ) . unwrap( ) , & vec!( ) )
360
+ result. node_addr_opt,
361
+ Some ( NodeAddr :: new(
362
+ & IpAddr :: from_str( "12.34.56.78" ) . unwrap( ) ,
363
+ & vec!( )
364
+ ) )
338
365
) ;
339
366
}
340
367
341
368
#[ test]
342
369
fn node_reference_can_display_itself ( ) {
343
370
let subject = NodeReference :: new (
344
371
PublicKey :: new ( & b"Booga" [ ..] ) ,
345
- IpAddr :: from_str ( "12.34.56.78" ) . unwrap ( ) ,
372
+ Some ( IpAddr :: from_str ( "12.34.56.78" ) . unwrap ( ) ) ,
346
373
vec ! [ 1234 , 5678 ] ,
347
374
) ;
348
375
0 commit comments