-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.rs
147 lines (125 loc) · 3.99 KB
/
common.rs
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
pub(crate) use crate::common::*;
use nix::unistd;
use ruc::*;
use serde::{Deserialize, Serialize};
use std::{env, fmt, fs, str::FromStr, sync::LazyLock};
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
/// Allocate ports based on this trait
pub trait NodePorts:
Clone + fmt::Debug + Send + Sync + Serialize + for<'a> Deserialize<'a>
{
/// Reserved ports defined both by the Tendermint and the APP
fn reserved() -> Vec<u16> {
let mut ret = Self::app_reserved();
ret.extend_from_slice(&Self::sys_reserved());
ret
}
/// Reserved ports defined by the Tendermint
fn sys_reserved() -> Vec<u16> {
// - p2p, owned by TM
// - rpc, owned by TM
// - abci, owned by APP
[26656, 26657, 26658].to_vec()
}
/// Reserved ports defined by the APP
fn app_reserved() -> Vec<u16>;
/// Check and return the new created port set
fn try_create(ports: &[u16]) -> Result<Self>;
/// Get actual ports from the instance
fn get_port_list(&self) -> Vec<u16>;
/// The p2p listening port in the Tendermint side
fn get_sys_p2p(&self) -> u16;
/// The rpc listening port in the Tendermint side
fn get_sys_rpc(&self) -> u16;
/// The ABCI listening port in the APP side
fn get_sys_abci(&self) -> u16;
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// global shared paths should not be used to avoid confusion
// when multiple users share a same physical machine
pub(crate) static BASE_DIR: LazyLock<String> = LazyLock::new(|| {
let ret = env::var("RUNTIME_CHAIN_DEV_BASE_DIR").unwrap_or_else(|_| {
format!(
"/tmp/__CHAIN_DEV__/tendermint_based/{}/{}/{}",
option_env!("STATIC_CHAIN_DEV_BASE_DIR_SUFFIX").unwrap_or(""),
unistd::gethostname().unwrap().into_string().unwrap(),
unistd::User::from_uid(unistd::getuid())
.unwrap()
.unwrap()
.name,
)
});
pnk!(fs::create_dir_all(&ret));
ret
});
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
pub(crate) const PRESET_POWER: u32 = 1_000_000_000;
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
#[serde(untagged)]
pub enum BlockItv {
Int(u16),
Float(f32),
}
impl BlockItv {
pub(crate) fn to_millisecond(self) -> Result<u32> {
const ITV_MAX: u16 = u16::MAX;
match self {
Self::Int(i) => Ok((i as u32) * 1000),
Self::Float(i) => {
if i > (ITV_MAX as f32) {
Err(eg!("block interval too big, max value: {}s", ITV_MAX))
} else {
Ok((i * 1000.0) as u32)
}
}
}
}
}
impl From<u8> for BlockItv {
fn from(t: u8) -> BlockItv {
Self::Int(t as u16)
}
}
impl From<u16> for BlockItv {
fn from(t: u16) -> BlockItv {
Self::Int(t)
}
}
impl From<f32> for BlockItv {
fn from(t: f32) -> BlockItv {
Self::Float(t)
}
}
impl fmt::Display for BlockItv {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Int(i) => write!(f, "{}", i),
Self::Float(i) => write!(f, "{}", i),
}
}
}
impl FromStr for BlockItv {
type Err = Box<(dyn ruc::RucError)>;
fn from_str(s: &str) -> Result<Self> {
s.parse::<u16>()
.map(Self::Int)
.c(d!())
.or_else(|_| s.parse::<f32>().map(Self::Float).c(d!()))
}
}
impl From<BlockItv> for f32 {
fn from(t: BlockItv) -> Self {
match t {
BlockItv::Int(i) => i as f32,
BlockItv::Float(i) => i,
}
}
}
impl From<BlockItv> for f64 {
fn from(t: BlockItv) -> Self {
f32::from(t) as f64
}
}