-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathlib.rs
159 lines (140 loc) · 4.3 KB
/
lib.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
148
149
150
151
152
153
154
155
156
157
158
159
use std::{
ffi::OsString,
fs,
net::{IpAddr, Ipv4Addr},
path::{Path, PathBuf},
};
use anyhow::anyhow;
use clap::{Args, Parser, Subcommand};
use edr_eth::Address;
use tracing::{event, Level};
pub mod config;
use config::ConfigFile;
#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
struct Cli {
#[clap(subcommand)]
command: Command,
}
#[derive(Subcommand)]
#[allow(clippy::large_enum_variant)]
enum Command {
/// Start the local Ethereum development node to serve JSON-RPC requests
/// over HTTP.
Node(NodeArgs),
/// Write default configuration values to edr.toml, overwriting any existing
/// file.
InitConfigFile,
}
const DEFAULT_CONFIG_FILE_NAME: &str = "edr.toml";
#[derive(Args)]
pub struct NodeArgs {
#[clap(long, default_value_t = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)))]
host: IpAddr,
#[clap(long, default_value = "8545")]
port: u16,
#[clap(long, default_value = DEFAULT_CONFIG_FILE_NAME)]
config_file: String,
#[clap(long, action = clap::ArgAction::SetTrue)]
allow_blocks_with_same_timestamp: bool,
#[clap(long, action = clap::ArgAction::SetTrue)]
allow_unlimited_contract_size: bool,
#[clap(long)]
fork_url: Option<String>,
#[clap(long)]
fork_block_number: Option<u64>,
#[clap(long)]
chain_id: Option<u64>,
#[clap(long)]
coinbase: Option<Address>,
#[clap(long)]
network_id: Option<u64>,
#[clap(long)]
cache_dir: Option<PathBuf>,
#[clap(short, long, action = clap::ArgAction::Count)]
verbose: u8,
}
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
pub enum ExitStatus {
Success,
Error,
}
impl From<bool> for ExitStatus {
fn from(value: bool) -> Self {
if value {
ExitStatus::Success
} else {
ExitStatus::Error
}
}
}
pub async fn run_with_args<T, I>(args: I) -> Result<ExitStatus, anyhow::Error>
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
async fn await_signal() {
use tokio::signal;
let ctrl_c = async {
signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
use signal::unix::{signal, SignalKind};
signal(SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
event!(Level::INFO, "Shutting down");
}
let args = Cli::parse_from(args);
match args.command {
Command::Node(node_args) => {
tracing_subscriber::fmt::Subscriber::builder()
.with_max_level(match node_args.verbose {
0 => Level::ERROR,
1 => Level::WARN,
2 => Level::INFO,
3 => Level::DEBUG,
4 => Level::TRACE,
_ => Err(anyhow!(
"Specifying --verbose more than 4 times is unsupported"
))?,
})
.init();
let config_file = if Path::new(&node_args.config_file).exists() {
let mut contents = String::new();
fs::read_to_string(&mut contents)?;
toml::from_str(&contents)?
} else if node_args.config_file != DEFAULT_CONFIG_FILE_NAME {
Err(anyhow!(
"Failed to open config file {}",
node_args.config_file
))?
} else {
ConfigFile::default()
};
let config = config_file.into_server_config(node_args)?;
let server = edr_rpc_server::Server::new(&config).await?;
Ok(server
.serve_with_shutdown_signal(await_signal())
.await
.map(|_| ExitStatus::Success)?)
}
Command::InitConfigFile => fs::write(
DEFAULT_CONFIG_FILE_NAME,
toml::to_string(&ConfigFile::default())?,
)
.map_err(|e| anyhow!("failed to write config file: {e}"))
.map(|_| ExitStatus::Success),
}
}