1
1
use {
2
- crate :: { admin_rpc_service, cli :: DefaultArgs } ,
2
+ crate :: { admin_rpc_service, commands :: FromClapArgMatches } ,
3
3
clap:: { App , Arg , ArgGroup , ArgMatches , SubCommand } ,
4
4
std:: { net:: SocketAddr , path:: Path } ,
5
5
} ;
6
6
7
- pub fn command ( _default_args : & DefaultArgs ) -> App < ' _ , ' _ > {
8
- SubCommand :: with_name ( "set-public-address" )
7
+ const COMMAND : & str = "set-public-address" ;
8
+
9
+ #[ derive( Debug , PartialEq ) ]
10
+ pub struct SetPublicAddressArgs {
11
+ pub tpu_addr : Option < SocketAddr > ,
12
+ pub tpu_forwards_addr : Option < SocketAddr > ,
13
+ }
14
+
15
+ impl FromClapArgMatches for SetPublicAddressArgs {
16
+ fn from_clap_arg_match ( matches : & ArgMatches ) -> Result < Self , String > {
17
+ let parse_arg_addr = |arg_name : & str ,
18
+ arg_long : & str |
19
+ -> Result < Option < SocketAddr > , String > {
20
+ matches. value_of ( arg_name) . map ( |host_port| {
21
+ solana_net_utils:: parse_host_port ( host_port) . map_err ( |err| {
22
+ format ! (
23
+ "failed to parse --{arg_long} address. It must be in the HOST:PORT format. {err}"
24
+ )
25
+ } )
26
+ } )
27
+ . transpose ( )
28
+ } ;
29
+ Ok ( SetPublicAddressArgs {
30
+ tpu_addr : parse_arg_addr ( "tpu_addr" , "tpu" ) ?,
31
+ tpu_forwards_addr : parse_arg_addr ( "tpu_forwards_addr" , "tpu-forwards" ) ?,
32
+ } )
33
+ }
34
+ }
35
+
36
+ pub fn command < ' a > ( ) -> App < ' a , ' a > {
37
+ SubCommand :: with_name ( COMMAND )
9
38
. about ( "Specify addresses to advertise in gossip" )
10
39
. arg (
11
40
Arg :: with_name ( "tpu_addr" )
@@ -33,18 +62,7 @@ pub fn command(_default_args: &DefaultArgs) -> App<'_, '_> {
33
62
}
34
63
35
64
pub fn execute ( matches : & ArgMatches , ledger_path : & Path ) -> Result < ( ) , String > {
36
- let parse_arg_addr = |arg_name : & str , arg_long : & str | -> Result < Option < SocketAddr > , String > {
37
- matches. value_of ( arg_name) . map ( |host_port| {
38
- solana_net_utils:: parse_host_port ( host_port) . map_err ( |err| {
39
- format ! (
40
- "failed to parse --{arg_long} address. It must be in the HOST:PORT format. {err}"
41
- )
42
- } )
43
- } )
44
- . transpose ( )
45
- } ;
46
- let tpu_addr = parse_arg_addr ( "tpu_addr" , "tpu" ) ?;
47
- let tpu_forwards_addr = parse_arg_addr ( "tpu_forwards_addr" , "tpu-forwards" ) ?;
65
+ let set_public_address_args = SetPublicAddressArgs :: from_clap_arg_match ( matches) ?;
48
66
49
67
macro_rules! set_public_address {
50
68
( $public_addr: expr, $set_public_address: ident, $request: literal) => {
@@ -60,10 +78,72 @@ pub fn execute(matches: &ArgMatches, ledger_path: &Path) -> Result<(), String> {
60
78
}
61
79
} ;
62
80
}
63
- set_public_address ! ( tpu_addr, set_public_tpu_address, "set public tpu address" ) ?;
64
81
set_public_address ! (
65
- tpu_forwards_addr,
82
+ set_public_address_args. tpu_addr,
83
+ set_public_tpu_address,
84
+ "setPublicTpuAddress"
85
+ ) ?;
86
+ set_public_address ! (
87
+ set_public_address_args. tpu_forwards_addr,
66
88
set_public_tpu_forwards_address,
67
89
"set public tpu forwards address"
68
- )
90
+ ) ?;
91
+ Ok ( ( ) )
92
+ }
93
+
94
+ #[ cfg( test) ]
95
+ mod tests {
96
+ use {
97
+ super :: * ,
98
+ crate :: commands:: tests:: {
99
+ verify_args_struct_by_command, verify_args_struct_by_command_is_error,
100
+ } ,
101
+ } ;
102
+
103
+ #[ test]
104
+ fn verify_args_struct_by_command_set_public_default ( ) {
105
+ verify_args_struct_by_command_is_error :: < SetPublicAddressArgs > ( command ( ) , vec ! [ COMMAND ] ) ;
106
+ }
107
+
108
+ #[ test]
109
+ fn verify_args_struct_by_command_set_public_address_tpu ( ) {
110
+ verify_args_struct_by_command (
111
+ command ( ) ,
112
+ vec ! [ COMMAND , "--tpu" , "127.0.0.1:8080" ] ,
113
+ SetPublicAddressArgs {
114
+ tpu_addr : Some ( SocketAddr :: from ( ( [ 127 , 0 , 0 , 1 ] , 8080 ) ) ) ,
115
+ tpu_forwards_addr : None ,
116
+ } ,
117
+ ) ;
118
+ }
119
+
120
+ #[ test]
121
+ fn verify_args_struct_by_command_set_public_address_tpu_forwards ( ) {
122
+ verify_args_struct_by_command (
123
+ command ( ) ,
124
+ vec ! [ COMMAND , "--tpu-forwards" , "127.0.0.1:8081" ] ,
125
+ SetPublicAddressArgs {
126
+ tpu_addr : None ,
127
+ tpu_forwards_addr : Some ( SocketAddr :: from ( ( [ 127 , 0 , 0 , 1 ] , 8081 ) ) ) ,
128
+ } ,
129
+ ) ;
130
+ }
131
+
132
+ #[ test]
133
+ fn verify_args_struct_by_command_set_public_address_tpu_and_tpu_forwards ( ) {
134
+ verify_args_struct_by_command (
135
+ command ( ) ,
136
+ vec ! [
137
+ COMMAND ,
138
+ "--tpu" ,
139
+ "127.0.0.1:8080" ,
140
+ "--tpu-forwards" ,
141
+ "127.0.0.1:8081" ,
142
+ ] ,
143
+ SetPublicAddressArgs {
144
+ tpu_addr : Some ( SocketAddr :: from ( ( [ 127 , 0 , 0 , 1 ] , 8080 ) ) ) ,
145
+ tpu_forwards_addr : Some ( SocketAddr :: from ( ( [ 127 , 0 , 0 , 1 ] , 8081 ) ) ) ,
146
+ } ,
147
+ ) ;
148
+ }
69
149
}
0 commit comments