agave_validator/commands/run/args/
rpc_bootstrap_config.rs

1use {
2    crate::{
3        bootstrap::RpcBootstrapConfig,
4        commands::{FromClapArgMatches, Result},
5    },
6    clap::{value_t, ArgMatches},
7};
8
9#[cfg(test)]
10impl Default for RpcBootstrapConfig {
11    fn default() -> Self {
12        Self {
13            no_genesis_fetch: false,
14            no_snapshot_fetch: false,
15            check_vote_account: None,
16            only_known_rpc: false,
17            max_genesis_archive_unpacked_size: 10485760,
18            incremental_snapshot_fetch: true,
19        }
20    }
21}
22
23impl FromClapArgMatches for RpcBootstrapConfig {
24    fn from_clap_arg_match(matches: &ArgMatches) -> Result<Self> {
25        let no_genesis_fetch = matches.is_present("no_genesis_fetch");
26
27        let no_snapshot_fetch = matches.is_present("no_snapshot_fetch");
28
29        let check_vote_account = matches
30            .value_of("check_vote_account")
31            .map(|url| url.to_string());
32
33        let only_known_rpc = matches.is_present("only_known_rpc");
34
35        let max_genesis_archive_unpacked_size =
36            value_t!(matches, "max_genesis_archive_unpacked_size", u64).map_err(|err| {
37                Box::<dyn std::error::Error>::from(format!(
38                    "failed to parse max_genesis_archive_unpacked_size: {err}"
39                ))
40            })?;
41
42        let no_incremental_snapshots = matches.is_present("no_incremental_snapshots");
43
44        Ok(Self {
45            no_genesis_fetch,
46            no_snapshot_fetch,
47            check_vote_account,
48            only_known_rpc,
49            max_genesis_archive_unpacked_size,
50            incremental_snapshot_fetch: !no_incremental_snapshots,
51        })
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use {
58        super::*,
59        crate::commands::run::args::{
60            tests::verify_args_struct_by_command_run_with_identity_setup, RunArgs,
61        },
62        solana_pubkey::Pubkey,
63        std::{
64            collections::HashSet,
65            net::{IpAddr, Ipv4Addr, SocketAddr},
66        },
67    };
68
69    #[test]
70    fn verify_args_struct_by_command_run_with_no_genesis_fetch() {
71        // long arg
72        {
73            let default_run_args = RunArgs::default();
74            let expected_args = RunArgs {
75                rpc_bootstrap_config: RpcBootstrapConfig {
76                    no_genesis_fetch: true,
77                    ..RpcBootstrapConfig::default()
78                },
79                ..default_run_args.clone()
80            };
81            verify_args_struct_by_command_run_with_identity_setup(
82                default_run_args.clone(),
83                vec!["--no-genesis-fetch"],
84                expected_args,
85            );
86        }
87    }
88
89    #[test]
90    fn verify_args_struct_by_command_run_with_no_snapshot_fetch() {
91        // long arg
92        {
93            let default_run_args = RunArgs::default();
94            let expected_args = RunArgs {
95                rpc_bootstrap_config: RpcBootstrapConfig {
96                    no_snapshot_fetch: true,
97                    ..RpcBootstrapConfig::default()
98                },
99                ..default_run_args.clone()
100            };
101            verify_args_struct_by_command_run_with_identity_setup(
102                default_run_args.clone(),
103                vec!["--no-snapshot-fetch"],
104                expected_args,
105            );
106        }
107    }
108
109    #[test]
110    fn verify_args_struct_by_command_run_with_check_vote_account() {
111        // long arg
112        {
113            let default_run_args = RunArgs::default();
114            let expected_args = RunArgs {
115                entrypoints: vec![SocketAddr::new(
116                    IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
117                    8000,
118                )],
119                rpc_bootstrap_config: RpcBootstrapConfig {
120                    check_vote_account: Some("https://api.mainnet-beta.solana.com".to_string()),
121                    ..RpcBootstrapConfig::default()
122                },
123                ..default_run_args.clone()
124            };
125            verify_args_struct_by_command_run_with_identity_setup(
126                default_run_args,
127                vec![
128                    // required by --check-vote-account
129                    "--entrypoint",
130                    "127.0.0.1:8000",
131                    "--check-vote-account",
132                    "https://api.mainnet-beta.solana.com",
133                ],
134                expected_args,
135            );
136        }
137    }
138
139    #[test]
140    fn verify_args_struct_by_command_run_with_only_known_rpc() {
141        // long arg
142        {
143            let default_run_args = RunArgs::default();
144            let known_validators_pubkey = Pubkey::new_unique();
145            let known_validators = Some(HashSet::from([known_validators_pubkey]));
146            let expected_args = RunArgs {
147                known_validators,
148                rpc_bootstrap_config: RpcBootstrapConfig {
149                    only_known_rpc: true,
150                    ..RpcBootstrapConfig::default()
151                },
152                ..default_run_args.clone()
153            };
154            verify_args_struct_by_command_run_with_identity_setup(
155                default_run_args,
156                vec![
157                    // required by --only-known-rpc
158                    "--known-validator",
159                    &known_validators_pubkey.to_string(),
160                    "--only-known-rpc",
161                ],
162                expected_args,
163            );
164        }
165
166        // alias
167        {
168            let default_run_args = RunArgs::default();
169            let known_validators_pubkey = Pubkey::new_unique();
170            let known_validators = Some(HashSet::from([known_validators_pubkey]));
171            let expected_args = RunArgs {
172                known_validators,
173                rpc_bootstrap_config: RpcBootstrapConfig {
174                    only_known_rpc: true,
175                    ..RpcBootstrapConfig::default()
176                },
177                ..default_run_args.clone()
178            };
179            verify_args_struct_by_command_run_with_identity_setup(
180                default_run_args,
181                vec![
182                    // required by --no-untrusted-rpc
183                    "--known-validator",
184                    &known_validators_pubkey.to_string(),
185                    "--no-untrusted-rpc",
186                ],
187                expected_args,
188            );
189        }
190    }
191
192    #[test]
193    fn verify_args_struct_by_command_run_with_incremental_snapshot_fetch() {
194        // long arg
195        {
196            let default_run_args = RunArgs::default();
197            let expected_args = RunArgs {
198                rpc_bootstrap_config: RpcBootstrapConfig {
199                    incremental_snapshot_fetch: false,
200                    ..RpcBootstrapConfig::default()
201                },
202                ..default_run_args.clone()
203            };
204            verify_args_struct_by_command_run_with_identity_setup(
205                default_run_args,
206                vec!["--no-incremental-snapshots"],
207                expected_args,
208            );
209        }
210    }
211}