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
use crate::{auto_validate::AutoValidate, json_rpc::JsonRpcClientWrapper, TransactionContext};
use diem_crypto::{ed25519::Ed25519PublicKey, x25519};
use diem_global_constants::{
CONSENSUS_KEY, FULLNODE_NETWORK_KEY, OPERATOR_ACCOUNT, OWNER_ACCOUNT, VALIDATOR_NETWORK_KEY,
};
use diem_management::{error::Error, secure_backend::ValidatorBackend, storage::to_x25519};
use diem_network_address_encryption::Encryptor;
use diem_secure_storage::Storage;
use diem_types::{
account_address::AccountAddress,
network_address::{NetworkAddress, Protocol},
};
use serde::Serialize;
use std::{convert::TryFrom, str::FromStr};
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
pub struct SetValidatorConfig {
#[structopt(long, required_unless = "config")]
json_server: Option<String>,
#[structopt(flatten)]
validator_config: diem_management::validator_config::ValidatorConfig,
#[structopt(
long,
required_unless = "fullnode-address",
help = "Validator Network Address"
)]
validator_address: Option<NetworkAddress>,
#[structopt(
long,
required_unless = "validator-address",
help = "Full Node Network Address"
)]
fullnode_address: Option<NetworkAddress>,
#[structopt(flatten)]
auto_validate: AutoValidate,
#[structopt(long, help = "Disables network address validation")]
disable_address_validation: bool,
}
impl SetValidatorConfig {
pub fn execute(self) -> Result<TransactionContext, Error> {
let config = self
.validator_config
.config
.load()?
.override_chain_id(self.validator_config.chain_id)
.override_json_server(&self.json_server)
.override_validator_backend(
&self.validator_config.validator_backend.validator_backend,
)?;
let storage = config.validator_backend();
let operator_account = storage.account_address(OPERATOR_ACCOUNT)?;
let owner_account = storage.account_address(OWNER_ACCOUNT)?;
let encryptor = config.validator_backend().encryptor();
let client = JsonRpcClientWrapper::new(config.json_server.clone());
let sequence_number = client.sequence_number(operator_account)?;
let in_set = client
.validator_set(None)?
.iter()
.any(|vi| vi.account_address() == &owner_account);
let validator_config = if in_set {
let vc = client.validator_config(owner_account)?;
DecryptedValidatorConfig::from_validator_config_resource(&vc, owner_account, &encryptor)
.map(Some)
.map_err(|e| {
Error::UnexpectedError(format!("Error parsing validator config: {}", e))
})?
} else {
None
};
let validator_address = if let Some(validator_address) = &self.validator_address {
validator_address.clone()
} else if let Some(vc) = &validator_config {
strip_address(&vc.validator_network_address)
} else {
return Err(Error::UnexpectedError(
"Missing validator-network-address".to_string(),
));
};
let fullnode_address = if let Some(fullnode_address) = &self.fullnode_address {
fullnode_address.clone()
} else if let Some(vc) = &validator_config {
strip_address(&vc.fullnode_network_address)
} else {
return Err(Error::UnexpectedError(
"Missing fullnode-network-address".to_string(),
));
};
let txn = self.validator_config.build_transaction(
sequence_number,
fullnode_address,
validator_address,
validator_config.is_some(),
self.disable_address_validation,
)?;
let mut transaction_context =
client.submit_transaction(txn.as_signed_user_txn().unwrap().clone())?;
transaction_context = self
.auto_validate
.execute(config.json_server, transaction_context)?;
Ok(transaction_context)
}
}
#[derive(Debug, StructOpt)]
pub struct RotateKey {
#[structopt(long, required_unless = "config")]
json_server: Option<String>,
#[structopt(flatten)]
validator_config: diem_management::validator_config::ValidatorConfig,
#[structopt(flatten)]
auto_validate: AutoValidate,
}
impl RotateKey {
pub fn execute(
self,
key_name: &'static str,
) -> Result<(TransactionContext, Ed25519PublicKey), Error> {
let config = self
.validator_config
.config()?
.override_json_server(&self.json_server);
let mut storage = config.validator_backend();
let encryptor = config.validator_backend().encryptor();
let client = JsonRpcClientWrapper::new(config.json_server.clone());
let owner_account = storage.account_address(OWNER_ACCOUNT)?;
let validator_config = client.validator_config(owner_account).and_then(|vc| {
DecryptedValidatorConfig::from_validator_config_resource(&vc, owner_account, &encryptor)
})?;
let mut storage_key = storage.ed25519_public_from_private(key_name)?;
let keys_match = match key_name {
CONSENSUS_KEY => storage_key == validator_config.consensus_public_key,
VALIDATOR_NETWORK_KEY => {
Some(to_x25519(storage_key.clone())?)
== validator_config
.validator_network_address
.find_noise_proto()
}
FULLNODE_NETWORK_KEY => {
Some(to_x25519(storage_key.clone())?)
== validator_config.fullnode_network_address.find_noise_proto()
}
_ => {
return Err(Error::UnexpectedError(
"Rotate key was called with an unknown key name!".into(),
));
}
};
if keys_match {
storage_key = storage.rotate_key(key_name)?;
}
let set_validator_config = SetValidatorConfig {
json_server: self.json_server.clone(),
validator_config: self.validator_config.clone(),
validator_address: None,
fullnode_address: None,
auto_validate: self.auto_validate.clone(),
disable_address_validation: true,
};
let mut transaction_context = set_validator_config.execute()?;
transaction_context = self
.auto_validate
.execute(config.json_server, transaction_context)?;
Ok((transaction_context, storage_key))
}
}
#[derive(Debug, StructOpt)]
pub struct RotateConsensusKey {
#[structopt(flatten)]
rotate_key: RotateKey,
}
impl RotateConsensusKey {
pub fn execute(self) -> Result<(TransactionContext, Ed25519PublicKey), Error> {
self.rotate_key.execute(CONSENSUS_KEY)
}
}
#[derive(Debug, StructOpt)]
pub struct RotateValidatorNetworkKey {
#[structopt(flatten)]
rotate_key: RotateKey,
}
impl RotateValidatorNetworkKey {
pub fn execute(self) -> Result<(TransactionContext, x25519::PublicKey), Error> {
let (txn_ctx, key) = self.rotate_key.execute(VALIDATOR_NETWORK_KEY)?;
Ok((txn_ctx, to_x25519(key)?))
}
}
#[derive(Debug, StructOpt)]
pub struct RotateFullNodeNetworkKey {
#[structopt(flatten)]
rotate_key: RotateKey,
}
impl RotateFullNodeNetworkKey {
pub fn execute(self) -> Result<(TransactionContext, x25519::PublicKey), Error> {
let (txn_ctx, key) = self.rotate_key.execute(FULLNODE_NETWORK_KEY)?;
Ok((txn_ctx, to_x25519(key)?))
}
}
pub fn strip_address(address: &NetworkAddress) -> NetworkAddress {
let protocols = address
.as_slice()
.iter()
.filter(|protocol| {
matches!(
protocol,
Protocol::Dns(_)
| Protocol::Dns4(_)
| Protocol::Dns6(_)
| Protocol::Ip4(_)
| Protocol::Ip6(_)
| Protocol::Memory(_)
| Protocol::Tcp(_)
)
})
.cloned()
.collect::<Vec<_>>();
NetworkAddress::try_from(protocols).unwrap()
}
#[derive(Debug, StructOpt)]
pub struct ValidatorConfig {
#[structopt(long, help = "Validator account address to display the config")]
account_address: AccountAddress,
#[structopt(flatten)]
config: diem_management::config::ConfigPath,
#[structopt(long, required_unless = "config")]
json_server: Option<String>,
#[structopt(
long,
help = "The secure backend that contains the network address encryption keys"
)]
validator_backend: Option<ValidatorBackend>,
}
impl ValidatorConfig {
pub fn execute(self) -> Result<DecryptedValidatorConfig, Error> {
let mut config = self.config.load()?.override_json_server(&self.json_server);
let client = JsonRpcClientWrapper::new(config.clone().json_server);
let encryptor = if let Some(backend) = &self.validator_backend {
config = config.override_validator_backend(&backend.validator_backend)?;
config.validator_backend().encryptor()
} else {
Encryptor::empty()
};
client
.validator_config(self.account_address)
.and_then(|vc| {
DecryptedValidatorConfig::from_validator_config_resource(
&vc,
self.account_address,
&encryptor,
)
})
}
}
#[derive(Serialize)]
pub struct DecryptedValidatorConfig {
pub name: String,
pub consensus_public_key: Ed25519PublicKey,
pub validator_network_address: NetworkAddress,
pub fullnode_network_address: NetworkAddress,
}
impl DecryptedValidatorConfig {
pub fn from_validator_config_resource(
config_resource: &diem_types::validator_config::ValidatorConfigResource,
account_address: AccountAddress,
encryptor: &Encryptor<Storage>,
) -> Result<Self, Error> {
let config = config_resource.validator_config.as_ref().ok_or_else(|| {
Error::JsonRpcReadError("validator-config", "not present".to_string())
})?;
let mut value = Self::from_validator_config(config, account_address, encryptor)?;
value.name = Self::human_name(&config_resource.human_name);
Ok(value)
}
pub fn from_validator_config(
config: &diem_types::validator_config::ValidatorConfig,
account_address: AccountAddress,
encryptor: &Encryptor<Storage>,
) -> Result<Self, Error> {
let fullnode_network_addresses = fullnode_addresses(config)?;
let validator_network_addresses = validator_addresses(config, account_address, encryptor)
.unwrap_or_else(|error| {
println!("{}: Using a dummy validator network address!", error);
vec![NetworkAddress::from_str("/dns4/could-not-decrypt").unwrap()]
});
Ok(DecryptedValidatorConfig {
name: "".to_string(),
consensus_public_key: config.consensus_public_key.clone(),
fullnode_network_address: fullnode_network_addresses[0].clone(),
validator_network_address: validator_network_addresses[0].clone(),
})
}
pub fn human_name(name: &[u8]) -> String {
std::str::from_utf8(name)
.map(|v| v.to_string())
.unwrap_or_else(|_| hex::encode(name))
}
}
pub fn fullnode_addresses(
config: &diem_types::validator_config::ValidatorConfig,
) -> Result<Vec<NetworkAddress>, Error> {
config
.fullnode_network_addresses()
.map_err(|e| Error::NetworkAddressDecodeError(e.to_string()))
}
pub fn validator_addresses(
config: &diem_types::validator_config::ValidatorConfig,
account_address: AccountAddress,
encryptor: &Encryptor<Storage>,
) -> Result<Vec<NetworkAddress>, Error> {
encryptor
.decrypt(&config.validator_network_addresses, account_address)
.map_err(|error| {
Error::CommandArgumentError(format!(
"Unable to decode network address for account {}: {}",
account_address, error
))
})
}