-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathconfiguration_command.rs
More file actions
536 lines (501 loc) · 20.7 KB
/
configuration_command.rs
File metadata and controls
536 lines (501 loc) · 20.7 KB
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// Copyright (c) 2019, MASQ (https://masq.ai) and/or its affiliates. All rights reserved.
use crate::command_context::CommandContext;
use crate::commands::commands_common::CommandError::Payload;
use crate::commands::commands_common::{
dump_parameter_line, transaction, Command, CommandError, STANDARD_COMMAND_TIMEOUT_MILLIS,
};
use clap::{App, Arg, SubCommand};
use masq_lib::as_any_ref_in_trait_impl;
use masq_lib::constants::NODE_NOT_RUNNING_ERROR;
use masq_lib::messages::{UiConfigurationRequest, UiConfigurationResponse};
use masq_lib::short_writeln;
use masq_lib::utils::to_string;
use std::fmt::{Debug, Display};
use std::io::Write;
use std::iter::once;
use thousands::Separable;
const COLUMN_WIDTH: usize = 33;
#[derive(Debug, PartialEq, Eq)]
pub struct ConfigurationCommand {
pub db_password: Option<String>,
}
const CONFIGURATION_ABOUT: &str = "Displays a running Node's current configuration.";
const CONFIGURATION_ARG_HELP: &str =
"Password of the database from which the configuration will be read.";
pub fn configuration_subcommand() -> App<'static, 'static> {
SubCommand::with_name("configuration")
.about(CONFIGURATION_ABOUT)
.arg(
Arg::with_name("db-password")
.help(CONFIGURATION_ARG_HELP)
.index(1)
.required(false),
)
}
impl Command for ConfigurationCommand {
fn execute(&self, context: &mut dyn CommandContext) -> Result<(), CommandError> {
let input = UiConfigurationRequest {
db_password_opt: self.db_password.clone(),
};
let output: Result<UiConfigurationResponse, CommandError> =
transaction(input, context, STANDARD_COMMAND_TIMEOUT_MILLIS);
match output {
Ok(response) => {
Self::dump_configuration(context.stdout(), response);
Ok(())
}
Err(Payload(code, message)) if code == NODE_NOT_RUNNING_ERROR => {
short_writeln!(
context.stderr(),
"MASQNode is not running; therefore its configuration cannot be displayed."
);
Err(Payload(code, message))
}
Err(e) => {
short_writeln!(context.stderr(), "Configuration retrieval failed: {:?}", e);
Err(e)
}
}
}
as_any_ref_in_trait_impl!();
}
impl ConfigurationCommand {
pub fn new(pieces: &[String]) -> Result<Self, String> {
let matches = match configuration_subcommand().get_matches_from_safe(pieces) {
Ok(matches) => matches,
Err(e) => return Err(format!("{}", e)),
};
Ok(ConfigurationCommand {
db_password: matches.value_of("db-password").map(to_string),
})
}
fn dump_configuration(stream: &mut dyn Write, configuration: UiConfigurationResponse) {
dump_parameter_line(stream, "NAME", "VALUE");
dump_parameter_line(
stream,
"Blockchain service URL:",
&configuration
.blockchain_service_url_opt
.unwrap_or_else(|| "[?]".to_string()),
);
dump_parameter_line(stream, "Chain:", &configuration.chain_name);
dump_parameter_line(
stream,
"Clandestine port:",
&configuration.clandestine_port.to_string(),
);
dump_parameter_line(
stream,
"Consuming wallet private key:",
&Self::interpret_option(&configuration.consuming_wallet_private_key_opt),
);
dump_parameter_line(
stream,
"Current schema version:",
&configuration.current_schema_version,
);
dump_parameter_line(
stream,
"Earning wallet address:",
&Self::interpret_option(&configuration.earning_wallet_address_opt),
);
dump_parameter_line(stream, "Gas price:", &configuration.gas_price.to_string());
dump_parameter_line(
stream,
"Max block count:",
&configuration
.max_block_count_opt
.map(|m| m.separate_with_commas())
.unwrap_or_else(|| "[Unlimited]".to_string()),
);
dump_parameter_line(
stream,
"Neighborhood mode:",
&configuration.neighborhood_mode,
);
dump_parameter_line(
stream,
"Port mapping protocol:",
&Self::interpret_option(&configuration.port_mapping_protocol_opt),
);
dump_parameter_line(
stream,
"Start block:",
&configuration
.start_block_opt
.map(|m| m.separate_with_commas())
.unwrap_or_else(|| "[Latest]".to_string()),
);
Self::dump_value_list(stream, "Past neighbors:", &configuration.past_neighbors);
let payment_thresholds = Self::preprocess_combined_parameters({
let p_c = &configuration.payment_thresholds;
&[
("Debt threshold:", &p_c.debt_threshold_gwei, "gwei"),
("Maturity threshold:", &p_c.maturity_threshold_sec, "s"),
("Payment grace period:", &p_c.payment_grace_period_sec, "s"),
(
"Permanent debt allowed:",
&p_c.permanent_debt_allowed_gwei,
"gwei",
),
("Threshold interval:", &p_c.threshold_interval_sec, "s"),
("Unban below:", &p_c.unban_below_gwei, "gwei"),
]
});
Self::dump_value_list(stream, "Payment thresholds:", &payment_thresholds);
let rate_pack = Self::preprocess_combined_parameters({
let r_p = &configuration.rate_pack;
&[
("Routing byte rate:", &r_p.routing_byte_rate, "wei"),
("Routing service rate:", &r_p.routing_service_rate, "wei"),
("Exit byte rate:", &r_p.exit_byte_rate, "wei"),
("Exit service rate:", &r_p.exit_service_rate, "wei"),
]
});
Self::dump_value_list(stream, "Rate pack:", &rate_pack);
let scan_intervals = Self::preprocess_combined_parameters({
let s_i = &configuration.scan_intervals;
&[
("Payable:", &s_i.payable_sec, "s"),
("Pending payable:", &s_i.pending_payable_sec, "s"),
("Receivable:", &s_i.receivable_sec, "s"),
]
});
Self::dump_value_list(stream, "Scan intervals:", &scan_intervals);
}
fn dump_value_list(stream: &mut dyn Write, name: &str, values: &[String]) {
if values.is_empty() {
dump_parameter_line(stream, name, "[?]");
return;
}
let mut name_row = true;
values.iter().for_each(|value| {
if name_row {
dump_parameter_line(stream, name, value);
name_row = false;
} else {
dump_parameter_line(stream, "", value);
}
})
}
fn interpret_option(value_opt: &Option<String>) -> String {
match value_opt {
None => "[?]".to_string(),
Some(s) => s.clone(),
}
}
fn preprocess_combined_parameters(
parameters: &[(&str, &dyn DisplaySeparable, &str)],
) -> Vec<String> {
let iter_of_strings = parameters.iter().map(|(description, value, unit)| {
format!(
"{:width$} {} {}",
description,
value.separate_with_commas(),
unit,
width = COLUMN_WIDTH
)
});
once(String::from("")).chain(iter_of_strings).collect()
}
}
trait DisplaySeparable: Display + Separable {}
impl DisplaySeparable for u64 {}
impl DisplaySeparable for String {}
#[cfg(test)]
mod tests {
use super::*;
use crate::command_context::ContextError;
use crate::command_context::ContextError::ConnectionDropped;
use crate::command_factory::{CommandFactory, CommandFactoryReal};
use crate::commands::commands_common::CommandError::ConnectionProblem;
use crate::test_utils::mocks::CommandContextMock;
use masq_lib::constants::NODE_NOT_RUNNING_ERROR;
use masq_lib::messages::{
ToMessageBody, UiConfigurationResponse, UiPaymentThresholds, UiRatePack, UiScanIntervals,
};
use masq_lib::utils::AutomapProtocol;
use std::sync::{Arc, Mutex};
#[test]
fn constants_have_correct_values() {
assert_eq!(
CONFIGURATION_ABOUT,
"Displays a running Node's current configuration."
);
assert_eq!(
CONFIGURATION_ARG_HELP,
"Password of the database from which the configuration will be read."
);
}
#[test]
fn command_factory_works_with_password() {
let subject = CommandFactoryReal::new();
let command = subject
.make(&["configuration".to_string(), "password".to_string()])
.unwrap();
let configuration_command = command
.as_any()
.downcast_ref::<ConfigurationCommand>()
.unwrap();
assert_eq!(
*configuration_command,
ConfigurationCommand {
db_password: Some("password".to_string())
}
);
}
#[test]
fn command_factory_works_without_password() {
let subject = CommandFactoryReal::new();
let command = subject.make(&["configuration".to_string()]).unwrap();
let configuration_command = command
.as_any()
.downcast_ref::<ConfigurationCommand>()
.unwrap();
assert_eq!(
configuration_command,
&ConfigurationCommand { db_password: None }
);
}
#[test]
fn doesnt_work_if_node_is_not_running() {
let mut context = CommandContextMock::new().transact_result(Err(
ContextError::PayloadError(NODE_NOT_RUNNING_ERROR, "irrelevant".to_string()),
));
let stdout_arc = context.stdout_arc();
let stderr_arc = context.stderr_arc();
let subject = ConfigurationCommand::new(&["configuration".to_string()]).unwrap();
let result = subject.execute(&mut context);
assert_eq!(
result,
Err(CommandError::Payload(
NODE_NOT_RUNNING_ERROR,
"irrelevant".to_string()
))
);
assert_eq!(
stderr_arc.lock().unwrap().get_string(),
"MASQNode is not running; therefore its configuration cannot be displayed.\n"
);
assert_eq!(stdout_arc.lock().unwrap().get_string(), String::new());
}
#[test]
fn configuration_command_happy_path_with_secrets() {
let transact_params_arc = Arc::new(Mutex::new(vec![]));
let expected_response = UiConfigurationResponse {
blockchain_service_url_opt: Some("https://infura.io/ID".to_string()),
current_schema_version: "schema version".to_string(),
clandestine_port: 1234,
chain_name: "ropsten".to_string(),
gas_price: 2345,
neighborhood_mode: "standard".to_string(),
max_block_count_opt: None,
consuming_wallet_private_key_opt: Some("consuming wallet private key".to_string()),
consuming_wallet_address_opt: Some("consuming wallet address".to_string()),
earning_wallet_address_opt: Some("earning address".to_string()),
port_mapping_protocol_opt: Some(AutomapProtocol::Pcp.to_string()),
past_neighbors: vec!["neighbor 1".to_string(), "neighbor 2".to_string()],
payment_thresholds: UiPaymentThresholds {
threshold_interval_sec: 11111,
debt_threshold_gwei: 1201412000,
payment_grace_period_sec: 4578,
permanent_debt_allowed_gwei: 112000,
maturity_threshold_sec: 3333,
unban_below_gwei: 120000,
},
rate_pack: UiRatePack {
routing_byte_rate: 99025000,
routing_service_rate: 138000000,
exit_byte_rate: 129000000,
exit_service_rate: 160000000,
},
start_block_opt: None,
scan_intervals: UiScanIntervals {
pending_payable_sec: 150500,
payable_sec: 155000,
receivable_sec: 250666,
},
};
let mut context = CommandContextMock::new()
.transact_params(&transact_params_arc)
.transact_result(Ok(expected_response.tmb(42)));
let stdout_arc = context.stdout_arc();
let stderr_arc = context.stderr_arc();
let subject =
ConfigurationCommand::new(&["configuration".to_string(), "password".to_string()])
.unwrap();
let result = subject.execute(&mut context);
assert_eq!(result, Ok(()));
let transact_params = transact_params_arc.lock().unwrap();
assert_eq!(
*transact_params,
vec![(
UiConfigurationRequest {
db_password_opt: Some("password".to_string())
}
.tmb(0),
STANDARD_COMMAND_TIMEOUT_MILLIS
)]
);
assert_eq!(
stdout_arc.lock().unwrap().get_string(),
format!(
"\
|NAME VALUE\n\
|Blockchain service URL: https://infura.io/ID\n\
|Chain: ropsten\n\
|Clandestine port: 1234\n\
|Consuming wallet private key: consuming wallet private key\n\
|Current schema version: schema version\n\
|Earning wallet address: earning address\n\
|Gas price: 2345\n\
|Max block count: [Unlimited]\n\
|Neighborhood mode: standard\n\
|Port mapping protocol: PCP\n\
|Start block: [Latest]\n\
|Past neighbors: neighbor 1\n\
| neighbor 2\n\
|Payment thresholds: \n\
| Debt threshold: 1,201,412,000 gwei\n\
| Maturity threshold: 3,333 s\n\
| Payment grace period: 4,578 s\n\
| Permanent debt allowed: 112,000 gwei\n\
| Threshold interval: 11,111 s\n\
| Unban below: 120,000 gwei\n\
|Rate pack: \n\
| Routing byte rate: 99,025,000 wei\n\
| Routing service rate: 138,000,000 wei\n\
| Exit byte rate: 129,000,000 wei\n\
| Exit service rate: 160,000,000 wei\n\
|Scan intervals: \n\
| Payable: 155,000 s\n\
| Pending payable: 150,500 s\n\
| Receivable: 250,666 s\n"
)
.replace('|', "")
);
assert_eq!(stderr_arc.lock().unwrap().get_string(), "");
}
#[test]
fn configuration_command_happy_path_without_secrets() {
let transact_params_arc = Arc::new(Mutex::new(vec![]));
let expected_response = UiConfigurationResponse {
blockchain_service_url_opt: Some("https://infura.io/ID".to_string()),
current_schema_version: "schema version".to_string(),
clandestine_port: 1234,
chain_name: "amoy".to_string(),
gas_price: 2345,
max_block_count_opt: Some(100_000),
neighborhood_mode: "zero-hop".to_string(),
consuming_wallet_address_opt: None,
consuming_wallet_private_key_opt: None,
earning_wallet_address_opt: Some("earning wallet".to_string()),
port_mapping_protocol_opt: Some(AutomapProtocol::Pcp.to_string()),
past_neighbors: vec![],
payment_thresholds: UiPaymentThresholds {
threshold_interval_sec: 1000,
debt_threshold_gwei: 2500,
payment_grace_period_sec: 666,
permanent_debt_allowed_gwei: 1200,
maturity_threshold_sec: 500,
unban_below_gwei: 1400,
},
rate_pack: UiRatePack {
routing_byte_rate: 15,
routing_service_rate: 17,
exit_byte_rate: 20,
exit_service_rate: 30,
},
start_block_opt: Some(1234567890u64),
scan_intervals: UiScanIntervals {
pending_payable_sec: 1000,
payable_sec: 1000,
receivable_sec: 1000,
},
};
let mut context = CommandContextMock::new()
.transact_params(&transact_params_arc)
.transact_result(Ok(expected_response.tmb(42)));
let stdout_arc = context.stdout_arc();
let stderr_arc = context.stderr_arc();
let subject = ConfigurationCommand::new(&["configuration".to_string()]).unwrap();
let result = subject.execute(&mut context);
assert_eq!(result, Ok(()));
let transact_params = transact_params_arc.lock().unwrap();
assert_eq!(
*transact_params,
vec![(
UiConfigurationRequest {
db_password_opt: None
}
.tmb(0),
STANDARD_COMMAND_TIMEOUT_MILLIS
)]
);
assert_eq!(
stdout_arc.lock().unwrap().get_string(),
format!(
"\
|NAME VALUE\n\
|Blockchain service URL: https://infura.io/ID\n\
|Chain: amoy\n\
|Clandestine port: 1234\n\
|Consuming wallet private key: [?]\n\
|Current schema version: schema version\n\
|Earning wallet address: earning wallet\n\
|Gas price: 2345\n\
|Max block count: 100,000\n\
|Neighborhood mode: zero-hop\n\
|Port mapping protocol: PCP\n\
|Start block: 1,234,567,890\n\
|Past neighbors: [?]\n\
|Payment thresholds: \n\
| Debt threshold: 2,500 gwei\n\
| Maturity threshold: 500 s\n\
| Payment grace period: 666 s\n\
| Permanent debt allowed: 1,200 gwei\n\
| Threshold interval: 1,000 s\n\
| Unban below: 1,400 gwei\n\
|Rate pack: \n\
| Routing byte rate: 15 wei\n\
| Routing service rate: 17 wei\n\
| Exit byte rate: 20 wei\n\
| Exit service rate: 30 wei\n\
|Scan intervals: \n\
| Payable: 1,000 s\n\
| Pending payable: 1,000 s\n\
| Receivable: 1,000 s\n",
)
.replace('|', "")
);
assert_eq!(stderr_arc.lock().unwrap().get_string(), "");
}
#[test]
fn configuration_command_sad_path() {
let transact_params_arc = Arc::new(Mutex::new(vec![]));
let mut context = CommandContextMock::new()
.transact_params(&transact_params_arc)
.transact_result(Err(ConnectionDropped("Booga".to_string())));
let stdout_arc = context.stdout_arc();
let stderr_arc = context.stderr_arc();
let subject = ConfigurationCommand::new(&["configuration".to_string()]).unwrap();
let result = subject.execute(&mut context);
assert_eq!(result, Err(ConnectionProblem("Booga".to_string())));
let transact_params = transact_params_arc.lock().unwrap();
assert_eq!(
*transact_params,
vec![(
UiConfigurationRequest {
db_password_opt: None
}
.tmb(0),
STANDARD_COMMAND_TIMEOUT_MILLIS
)]
);
assert_eq!(stdout_arc.lock().unwrap().get_string(), String::new());
assert_eq!(
stderr_arc.lock().unwrap().get_string(),
"Configuration retrieval failed: ConnectionProblem(\"Booga\")\n"
);
}
}