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
use crate::{emitter::query_sequence_numbers, instance::Instance, ClusterArgs};
use anyhow::{anyhow, bail, format_err, Result};
use aptos_crypto::{
ed25519::{Ed25519PrivateKey, Ed25519PublicKey},
test_utils::KeyPair,
Uniform,
};
use aptos_logger::{info, warn};
use aptos_rest_client::Client as RestClient;
use aptos_sdk::{
move_types::account_address::AccountAddress,
types::{account_config::aptos_root_address, chain_id::ChainId, AccountKey, LocalAccount},
};
use rand::seq::SliceRandom;
use std::convert::TryFrom;
use url::Url;
#[derive(Debug)]
pub struct Cluster {
instances: Vec<Instance>,
mint_key_pair: KeyPair<Ed25519PrivateKey, Ed25519PublicKey>,
pub chain_id: ChainId,
}
fn clone(key: &Ed25519PrivateKey) -> Ed25519PrivateKey {
let serialized: &[u8] = &(key.to_bytes());
Ed25519PrivateKey::try_from(serialized).unwrap()
}
impl Cluster {
pub async fn from_host_port(
peers: Vec<Url>,
mint_key: Ed25519PrivateKey,
chain_id: ChainId,
vasp: bool,
) -> Result<Self> {
let num_peers = peers.len();
let mut instances = Vec::new();
let mut errors = Vec::new();
for url in &peers {
let instance = Instance::new(
format!(
"{}:{}",
url.host().unwrap(),
url.port_or_known_default().unwrap()
),
url.clone(),
None,
);
match instance.rest_client().get_ledger_information().await {
Ok(_) => instances.push(instance),
Err(err) => errors.push(err),
}
}
if !errors.is_empty() {
warn!(
"Failed to build some endpoints for the cluster: {:?}",
errors
);
}
if instances.is_empty() {
return Err(anyhow!(
"None of the rest endpoints provided are reachable: {:?}",
errors
));
}
info!(
"Creating the cluster with {}/{} end points",
instances.len(),
num_peers
);
let mint_key_pair = if vasp {
dummy_key_pair()
} else {
KeyPair::from(mint_key)
};
Ok(Self {
instances,
mint_key_pair,
chain_id,
})
}
pub async fn try_from_cluster_args(args: &ClusterArgs) -> Result<Self> {
let mut urls = Vec::new();
for url in &args.targets {
if !url.has_host() {
bail!("No host found in URL: {}", url);
}
let mut url = url.clone();
if url.port_or_known_default().is_none() {
url.set_port(Some(8080))
.map_err(|_| format_err!("Failed to set port unexpectedly"))?;
}
urls.push(url);
}
let mint_key = args.mint_args.get_mint_key()?;
let cluster = Cluster::from_host_port(urls, mint_key, args.chain_id, args.vasp)
.await
.map_err(|e| format_err!("failed to create a cluster from host and port: {}", e))?;
Ok(cluster)
}
fn account_key(&self) -> AccountKey {
AccountKey::from_private_key(clone(&self.mint_key_pair.private_key))
}
async fn load_account_with_mint_key(
&self,
client: &RestClient,
address: AccountAddress,
) -> Result<LocalAccount> {
let sequence_number = query_sequence_numbers(client, &[address])
.await
.map_err(|e| {
format_err!(
"query_sequence_numbers on {:?} for account {} failed: {}",
client,
address,
e
)
})?[0];
Ok(LocalAccount::new(
address,
self.account_key(),
sequence_number,
))
}
pub async fn load_aptos_root_account(&self, client: &RestClient) -> Result<LocalAccount> {
self.load_account_with_mint_key(client, aptos_root_address())
.await
}
pub async fn load_faucet_account(&self, client: &RestClient) -> Result<LocalAccount> {
self.load_account_with_mint_key(client, aptos_root_address())
.await
}
pub fn random_instance(&self) -> Instance {
let mut rnd = rand::thread_rng();
self.instances
.choose(&mut rnd)
.expect("random_validator_instance requires non-empty validator_instances")
.clone()
}
pub fn all_instances(&self) -> impl Iterator<Item = &Instance> {
self.instances.iter()
}
}
pub fn dummy_key_pair() -> KeyPair<Ed25519PrivateKey, Ed25519PublicKey> {
Ed25519PrivateKey::generate_for_testing().into()
}