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
use aptos_config::config::HANDSHAKE_VERSION;
use aptos_crypto::{bls12381, ed25519::Ed25519PublicKey, x25519};
use aptos_types::{
account_address::AccountAddress,
chain_id::ChainId,
network_address::{DnsName, NetworkAddress, Protocol},
transaction::authenticator::AuthenticationKey,
};
use serde::{Deserialize, Serialize};
use std::{
convert::TryFrom,
fs::File,
io::Read,
net::{Ipv4Addr, Ipv6Addr, ToSocketAddrs},
path::Path,
str::FromStr,
};
use vm_genesis::Validator;
#[derive(Debug, Deserialize, Serialize)]
pub struct Layout {
pub root_key: Ed25519PublicKey,
pub users: Vec<String>,
pub chain_id: ChainId,
#[serde(default)]
pub allow_new_validators: bool,
pub min_stake: u64,
pub max_stake: u64,
pub min_lockup_duration_secs: u64,
pub max_lockup_duration_secs: u64,
pub epoch_duration_secs: u64,
pub initial_lockup_timestamp: u64,
pub min_price_per_gas_unit: u64,
}
impl Layout {
pub fn from_disk(path: &Path) -> anyhow::Result<Self> {
let mut file = File::open(&path).map_err(|e| {
anyhow::Error::msg(format!("Failed to open file {}, {}", path.display(), e))
})?;
let mut contents = String::new();
file.read_to_string(&mut contents).map_err(|e| {
anyhow::Error::msg(format!("Failed to read file {}, {}", path.display(), e))
})?;
Ok(serde_yaml::from_str(&contents)?)
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ValidatorConfiguration {
pub account_address: AccountAddress,
pub consensus_public_key: bls12381::PublicKey,
pub proof_of_possession: bls12381::ProofOfPossession,
pub account_public_key: Ed25519PublicKey,
pub validator_network_public_key: x25519::PublicKey,
pub validator_host: HostAndPort,
#[serde(skip_serializing_if = "Option::is_none")]
pub full_node_network_public_key: Option<x25519::PublicKey>,
#[serde(skip_serializing_if = "Option::is_none")]
pub full_node_host: Option<HostAndPort>,
pub stake_amount: u64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StringValidatorConfiguration {
pub account_address: String,
pub consensus_public_key: String,
pub proof_of_possession: String,
pub account_public_key: String,
pub validator_network_public_key: String,
pub validator_host: HostAndPort,
pub full_node_network_public_key: Option<String>,
pub full_node_host: Option<HostAndPort>,
pub stake_amount: u64,
}
impl TryFrom<ValidatorConfiguration> for Validator {
type Error = anyhow::Error;
fn try_from(config: ValidatorConfiguration) -> Result<Self, Self::Error> {
let auth_key = AuthenticationKey::ed25519(&config.account_public_key);
let validator_addresses = vec![config
.validator_host
.as_network_address(config.validator_network_public_key)
.unwrap()];
let full_node_addresses = if let Some(full_node_host) = config.full_node_host {
if let Some(full_node_network_key) = config.full_node_network_public_key {
vec![full_node_host
.as_network_address(full_node_network_key)
.unwrap()]
} else {
return Err(anyhow::Error::msg(
"Full node host specified, but not full node network key",
));
}
} else {
vec![]
};
let derived_address = auth_key.derived_address();
if config.account_address != derived_address {
return Err(anyhow::Error::msg(format!(
"AccountAddress {} does not match account key derived one {}",
config.account_address, derived_address
)));
}
Ok(Validator {
address: derived_address,
consensus_pubkey: config.consensus_public_key.to_bytes().to_vec(),
proof_of_possession: config.proof_of_possession.to_bytes().to_vec(),
operator_address: auth_key.derived_address(),
network_addresses: bcs::to_bytes(&validator_addresses).unwrap(),
full_node_network_addresses: bcs::to_bytes(&full_node_addresses).unwrap(),
stake_amount: config.stake_amount,
})
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HostAndPort {
pub host: DnsName,
pub port: u16,
}
impl HostAndPort {
pub fn as_network_address(&self, key: x25519::PublicKey) -> anyhow::Result<NetworkAddress> {
let host = self.host.to_string();
let host_protocol = if let Ok(ip) = Ipv4Addr::from_str(&host) {
Protocol::Ip4(ip)
} else if let Ok(ip) = Ipv6Addr::from_str(&host) {
Protocol::Ip6(ip)
} else {
Protocol::Dns(self.host.clone())
};
let port_protocol = Protocol::Tcp(self.port);
let noise_protocol = Protocol::NoiseIK(key);
let handshake_protocol = Protocol::Handshake(HANDSHAKE_VERSION);
Ok(NetworkAddress::try_from(vec![
host_protocol,
port_protocol,
noise_protocol,
handshake_protocol,
])?)
}
}
impl TryFrom<&NetworkAddress> for HostAndPort {
type Error = anyhow::Error;
fn try_from(address: &NetworkAddress) -> Result<Self, Self::Error> {
let socket_addr = address.to_socket_addrs()?.next().unwrap();
HostAndPort::from_str(&socket_addr.to_string())
}
}
impl FromStr for HostAndPort {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<_> = s.split(':').collect();
if parts.len() != 2 {
Err(anyhow::Error::msg(
"Invalid host and port, must be of the form 'host:port` e.g. '127.0.0.1:6180'",
))
} else {
let host = DnsName::from_str(*parts.get(0).unwrap())?;
let port = u16::from_str(parts.get(1).unwrap())?;
Ok(HostAndPort { host, port })
}
}
}