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
use crate::config::NodeConfig;
use diem_types::{
account_address::AccountAddress,
network_address::{NetworkAddress, Protocol},
transaction::{authenticator::AuthenticationKey, Transaction},
};
use get_if_addrs::get_if_addrs;
use std::net::{TcpListener, TcpStream};
pub fn default_validator_owner_auth_key_from_name(name: &[u8]) -> AuthenticationKey {
let salt = "validator_owner::";
let mut name_in_bytes = salt.as_bytes().to_vec();
name_in_bytes.extend_from_slice(name);
let hash = diem_crypto::HashValue::sha3_256_of(&name_in_bytes);
AuthenticationKey::new(*hash.as_ref())
}
pub fn validator_owner_account_from_name(name: &[u8]) -> AccountAddress {
default_validator_owner_auth_key_from_name(name).derived_address()
}
pub fn get_available_port() -> u16 {
const MAX_PORT_RETRIES: u32 = 1000;
for _ in 0..MAX_PORT_RETRIES {
if let Ok(port) = get_ephemeral_port() {
return port;
}
}
panic!("Error: could not find an available port");
}
fn get_ephemeral_port() -> ::std::io::Result<u16> {
let listener = TcpListener::bind(("localhost", 0))?;
let addr = listener.local_addr()?;
let _sender = TcpStream::connect(addr)?;
let _incoming = listener.accept()?;
Ok(addr.port())
}
pub fn get_local_ip() -> Option<NetworkAddress> {
get_if_addrs().ok().and_then(|if_addrs| {
if_addrs
.iter()
.find(|if_addr| !if_addr.is_loopback())
.map(|if_addr| NetworkAddress::from(Protocol::from(if_addr.ip())))
})
}
pub fn get_available_port_in_multiaddr(is_ipv4: bool) -> NetworkAddress {
let ip_proto = if is_ipv4 {
Protocol::Ip4("0.0.0.0".parse().unwrap())
} else {
Protocol::Ip6("::1".parse().unwrap())
};
NetworkAddress::from(ip_proto).push(Protocol::Tcp(get_available_port()))
}
pub fn get_genesis_txn(config: &NodeConfig) -> Option<&Transaction> {
config.execution.genesis.as_ref()
}