forked from jito-foundation/jito-solana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.rs
444 lines (410 loc) · 12.8 KB
/
cli.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use {
clap::{crate_description, crate_name, crate_version, ArgEnum, Args, Parser},
serde::{Deserialize, Serialize},
solana_sdk::pubkey::Pubkey,
std::{net::SocketAddr, process::exit, str::FromStr},
};
#[derive(Parser, Debug, PartialEq, Eq)]
#[clap(name = crate_name!(),
version = crate_version!(),
about = crate_description!(),
rename_all = "kebab-case"
)]
pub struct DosClientParameters {
#[clap(long, arg_enum, help = "Interface to DoS")]
pub mode: Mode,
#[clap(long, arg_enum, help = "Type of data to send")]
pub data_type: DataType,
#[clap(
long = "entrypoint",
parse(try_from_str = addr_parser),
default_value = "127.0.0.1:8001",
help = "Gossip entrypoint address. Usually <ip>:8001"
)]
pub entrypoint_addr: SocketAddr,
#[clap(
long,
default_value = "128",
required_if_eq("data-type", "random"),
help = "Size of packet to DoS with, relevant only for data-type=random"
)]
pub data_size: usize,
#[clap(
long,
parse(try_from_str = pubkey_parser),
required_if_eq("mode", "rpc"),
help = "Pubkey for rpc-mode calls"
)]
pub data_input: Option<Pubkey>,
#[clap(long, help = "Just use entrypoint address directly")]
pub skip_gossip: bool,
#[clap(long, help = "Allow contacting private ip addresses")]
pub allow_private_addr: bool,
#[clap(
long,
default_value = "1",
help = "Number of threads generating transactions"
)]
pub num_gen_threads: usize,
#[clap(flatten)]
pub transaction_params: TransactionParams,
#[clap(
long,
conflicts_with("skip-gossip"),
help = "Submit transactions via QUIC"
)]
pub tpu_use_quic: bool,
#[clap(long, default_value = "16384", help = "Size of the transactions batch")]
pub send_batch_size: usize,
}
#[derive(Args, Clone, Serialize, Deserialize, Debug, Default, PartialEq, Eq)]
#[clap(rename_all = "kebab-case")]
pub struct TransactionParams {
#[clap(
long,
conflicts_with("valid-blockhash"),
help = "Number of signatures in transaction"
)]
pub num_signatures: Option<usize>,
#[clap(
long,
requires("transaction-type"),
conflicts_with("skip-gossip"),
help = "Generate a valid blockhash for transaction"
)]
pub valid_blockhash: bool,
#[clap(
long,
requires("num-signatures"),
help = "Generate valid signature(s) for transaction"
)]
pub valid_signatures: bool,
#[clap(long, help = "Generate unique transactions")]
pub unique_transactions: bool,
#[clap(
long,
arg_enum,
requires("valid-blockhash"),
help = "Type of transaction to be sent"
)]
pub transaction_type: Option<TransactionType>,
#[clap(
long,
required_if_eq("transaction-type", "transfer"),
help = "Number of instructions in transfer transaction"
)]
pub num_instructions: Option<usize>,
}
#[derive(ArgEnum, Clone, Copy, Debug, Eq, PartialEq)]
pub enum Mode {
Gossip,
Tvu,
Tpu,
TpuForwards,
Repair,
ServeRepair,
Rpc,
}
#[derive(ArgEnum, Clone, Copy, Debug, Eq, PartialEq)]
pub enum DataType {
RepairHighest,
RepairShred,
RepairOrphan,
Random,
GetAccountInfo,
GetProgramAccounts,
Transaction,
}
#[derive(ArgEnum, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum TransactionType {
Transfer,
AccountCreation,
}
fn addr_parser(addr: &str) -> Result<SocketAddr, &'static str> {
match solana_net_utils::parse_host_port(addr) {
Ok(v) => Ok(v),
Err(_) => Err("failed to parse address"),
}
}
fn pubkey_parser(pubkey: &str) -> Result<Pubkey, &'static str> {
match Pubkey::from_str(pubkey) {
Ok(v) => Ok(v),
Err(_) => Err("failed to parse pubkey"),
}
}
/// input checks which are not covered by Clap
fn validate_input(params: &DosClientParameters) {
if params.mode == Mode::Rpc
&& (params.data_type != DataType::GetAccountInfo
&& params.data_type != DataType::GetProgramAccounts)
{
eprintln!("unsupported data type");
exit(1);
}
if params.data_type != DataType::Transaction {
let tp = ¶ms.transaction_params;
if tp.valid_blockhash || tp.valid_signatures || tp.unique_transactions {
eprintln!("Arguments valid-blockhash, valid-sign, unique-transactions are ignored if data-type != transaction");
exit(1);
}
}
}
pub fn build_cli_parameters() -> DosClientParameters {
let cmd_params = DosClientParameters::parse();
validate_input(&cmd_params);
cmd_params
}
#[cfg(test)]
mod tests {
use {super::*, clap::Parser, solana_sdk::pubkey::Pubkey};
#[test]
fn test_cli_parse_rpc_no_data_input() {
let result = DosClientParameters::try_parse_from(vec![
"solana-dos",
"--mode",
"rpc",
"--data-type",
"get-account-info",
//--data-input is required for `--mode rpc` but it is not specified
]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
clap::error::ErrorKind::MissingRequiredArgument
);
}
#[test]
fn test_cli_parse_rpc_data_input() {
let entrypoint_addr: SocketAddr = "127.0.0.1:8001".parse().unwrap();
let pubkey = Pubkey::default();
let pubkey_str: String = pubkey.to_string();
let params = DosClientParameters::try_parse_from(vec![
"solana-dos",
"--mode",
"rpc",
"--data-type",
"get-account-info",
"--data-input",
&pubkey_str,
])
.unwrap();
assert_eq!(
params,
DosClientParameters {
entrypoint_addr,
mode: Mode::Rpc,
data_size: 128, // default value
data_type: DataType::GetAccountInfo,
data_input: Some(pubkey),
skip_gossip: false,
allow_private_addr: false,
transaction_params: TransactionParams::default(),
tpu_use_quic: false,
num_gen_threads: 1,
send_batch_size: 16384,
},
);
}
#[test]
fn test_cli_parse_dos_valid_signatures() {
let entrypoint_addr: SocketAddr = "127.0.0.1:8001".parse().unwrap();
let params = DosClientParameters::try_parse_from(vec![
"solana-dos",
"--mode",
"tpu",
"--data-type",
"transaction",
"--unique-transactions",
"--valid-signatures",
"--num-signatures",
"8",
"--tpu-use-quic",
"--send-batch-size",
"1",
])
.unwrap();
assert_eq!(
params,
DosClientParameters {
entrypoint_addr,
mode: Mode::Tpu,
data_size: 128,
data_type: DataType::Transaction,
data_input: None,
skip_gossip: false,
allow_private_addr: false,
num_gen_threads: 1,
transaction_params: TransactionParams {
num_signatures: Some(8),
valid_blockhash: false,
valid_signatures: true,
unique_transactions: true,
transaction_type: None,
num_instructions: None,
},
tpu_use_quic: true,
send_batch_size: 1,
},
);
}
#[test]
fn test_cli_parse_dos_transfer() {
let entrypoint_addr: SocketAddr = "127.0.0.1:8001".parse().unwrap();
let params = DosClientParameters::try_parse_from(vec![
"solana-dos",
"--mode",
"tpu",
"--data-type",
"transaction",
"--unique-transactions",
"--valid-blockhash",
"--transaction-type",
"transfer",
"--num-instructions",
"1",
"--send-batch-size",
"1",
])
.unwrap();
assert_eq!(
params,
DosClientParameters {
entrypoint_addr,
mode: Mode::Tpu,
data_size: 128, // irrelevant if not random
data_type: DataType::Transaction,
data_input: None,
skip_gossip: false,
allow_private_addr: false,
num_gen_threads: 1,
transaction_params: TransactionParams {
num_signatures: None,
valid_blockhash: true,
valid_signatures: false,
unique_transactions: true,
transaction_type: Some(TransactionType::Transfer),
num_instructions: Some(1),
},
tpu_use_quic: false,
send_batch_size: 1,
},
);
let result = DosClientParameters::try_parse_from(vec![
"solana-dos",
"--mode",
"tpu",
"--data-type",
"transaction",
"--unique-transactions",
"--transaction-type",
"transfer",
"--num-instructions",
"8",
]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
clap::error::ErrorKind::MissingRequiredArgument
);
let entrypoint_addr: SocketAddr = "127.0.0.1:8001".parse().unwrap();
let params = DosClientParameters::try_parse_from(vec![
"solana-dos",
"--mode",
"tpu",
"--data-type",
"transaction",
"--unique-transactions",
"--valid-blockhash",
"--transaction-type",
"transfer",
"--num-instructions",
"8",
"--send-batch-size",
"1",
])
.unwrap();
assert_eq!(
params,
DosClientParameters {
entrypoint_addr,
mode: Mode::Tpu,
data_size: 128, // irrelevant if not random
data_type: DataType::Transaction,
data_input: None,
skip_gossip: false,
allow_private_addr: false,
num_gen_threads: 1,
transaction_params: TransactionParams {
num_signatures: None,
valid_blockhash: true,
valid_signatures: false,
unique_transactions: true,
transaction_type: Some(TransactionType::Transfer),
num_instructions: Some(8),
},
tpu_use_quic: false,
send_batch_size: 1,
},
);
}
#[test]
fn test_cli_parse_dos_create_account() {
let entrypoint_addr: SocketAddr = "127.0.0.1:8001".parse().unwrap();
let params = DosClientParameters::try_parse_from(vec![
"solana-dos",
"--mode",
"tpu",
"--data-type",
"transaction",
"--unique-transactions",
"--valid-blockhash",
"--transaction-type",
"account-creation",
"--send-batch-size",
"1",
])
.unwrap();
assert_eq!(
params,
DosClientParameters {
entrypoint_addr,
mode: Mode::Tpu,
data_size: 128, // irrelevant if not random
data_type: DataType::Transaction,
data_input: None,
skip_gossip: false,
allow_private_addr: false,
num_gen_threads: 1,
transaction_params: TransactionParams {
num_signatures: None,
valid_blockhash: true,
valid_signatures: false,
unique_transactions: true,
transaction_type: Some(TransactionType::AccountCreation),
num_instructions: None,
},
tpu_use_quic: false,
send_batch_size: 1,
},
);
}
#[test]
#[should_panic]
fn test_cli_parse_dos_conflicting_sign_instruction() {
// check conflicting args num-signatures and num-instructions
let result = DosClientParameters::try_parse_from(vec![
"solana-dos",
"--mode",
"tpu",
"--data-type",
"transaction",
"--unique-transactions",
"--valid-signatures",
"--num-signatures",
"8",
"--num-instructions",
"1",
]);
assert!(result.is_err());
}
}