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
use crate::storage::Storage;
use std::{net::SocketAddr, path::PathBuf};
use anyhow::Context;
use clap::Parser;
use serde::{Deserialize, Serialize};
use melstf::GenesisConfig;
use melstructs::Address;
use tap::Tap;
use tmelcrypt::Ed25519SK;
#[derive(Debug, Parser)]
#[command(author, version, about, long_about = None)]
pub struct MainArgs {
#[arg(long, default_value = "0.0.0.0:41814")]
listen: SocketAddr,
#[arg(long)]
legacy_listen: Option<SocketAddr>,
#[arg(long)]
advertise: Option<SocketAddr>,
#[arg(long, default_value = "auto")]
bootstrap: Vec<String>,
#[arg(long)]
database: Option<PathBuf>,
#[arg(long)]
staker_cfg: Option<PathBuf>,
#[arg(long)]
override_genesis: Option<PathBuf>,
#[arg(long)]
testnet: bool,
#[arg(long)]
pub self_test: bool,
#[arg(long)]
pub index_coins: bool,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct StakerConfig {
#[serde(with = "serde_with::rust::display_fromstr")]
pub signing_secret: Ed25519SK,
#[serde(with = "serde_with::rust::display_fromstr")]
pub listen: SocketAddr,
#[serde(with = "serde_with::rust::display_fromstr")]
pub bootstrap: SocketAddr,
#[serde(with = "serde_with::rust::display_fromstr")]
pub payout_addr: Address,
pub target_fee_multiplier: u128,
}
impl MainArgs {
pub fn advertise_addr(&self) -> Option<SocketAddr> {
self.advertise
}
pub async fn genesis_config(&self) -> anyhow::Result<GenesisConfig> {
if let Some(path) = &self.override_genesis {
let genesis_yaml: Vec<u8> = smol::fs::read(&path)
.await
.context("cannot read genesis config")?;
Ok(serde_yaml::from_slice(&genesis_yaml)
.context("error while parsing genesis config")?)
} else if self.testnet {
Ok(GenesisConfig::std_testnet())
} else {
Ok(GenesisConfig::std_mainnet())
}
}
pub async fn storage(&self) -> anyhow::Result<Storage> {
let genesis = self.genesis_config().await?;
let database_default_path = dirs::home_dir().expect("no home dir?!").tap_mut(|p| {
p.push(".melnode/");
});
let database_base_path = self.database.clone().unwrap_or(database_default_path);
let _history_path = database_base_path
.clone()
.tap_mut(|path| path.push("history"));
let _smt_path = database_base_path
.clone()
.tap_mut(|path| path.push("smt.db"));
std::fs::create_dir_all(&database_base_path)?;
let storage = Storage::open(database_base_path, genesis)
.await
.context("cannot make storage")?;
log::debug!("node storage opened");
Ok(storage)
}
pub async fn bootstrap(&self) -> anyhow::Result<Vec<SocketAddr>> {
if !self.bootstrap.is_empty() {
let mut bootstrap = vec![];
for name in self.bootstrap.iter() {
let addrs = if name == "auto" {
melbootstrap::bootstrap_routes(self.genesis_config().await?.network)
} else {
smol::net::resolve(&name)
.await
.context("cannot resolve DNS bootstrap")?
};
bootstrap.extend(addrs);
}
Ok(bootstrap)
} else {
Ok(melbootstrap::bootstrap_routes(
self.genesis_config().await?.network,
))
}
}
pub fn listen_addr(&self) -> SocketAddr {
self.listen
}
pub fn legacy_listen_addr(&self) -> Option<SocketAddr> {
self.legacy_listen
}
pub async fn staker_cfg(&self) -> anyhow::Result<Option<StakerConfig>> {
if let Some(path) = self.staker_cfg.as_ref() {
let s = std::fs::read_to_string(path)?;
let lele: StakerConfig = serde_yaml::from_str(&s)?;
Ok(Some(lele))
} else {
Ok(None)
}
}
}