forked from AleoNet/snarkOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
187 lines (157 loc) · 6.46 KB
/
main.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
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
// Copyright (C) 2019-2020 Aleo Systems Inc.
// This file is part of the snarkOS library.
// The snarkOS library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The snarkOS library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.
#[macro_use]
extern crate tracing;
use snarkos::{
cli::CLI,
config::{Config, ConfigCli},
display::render_init,
miner::MinerInstance,
};
use snarkos_consensus::{ConsensusParameters, MemoryPool, MerkleTreeLedger};
use snarkos_dpc::base_dpc::{instantiated::Components, parameters::PublicParameters, BaseDPCComponents};
use snarkos_errors::node::NodeError;
use snarkos_models::algorithms::{CRH, SNARK};
use snarkos_network::{external::protocol::SyncHandler, internal::context::Context, Server};
use snarkos_objects::{AccountAddress, Network};
use snarkos_posw::PoswMarlin;
use snarkos_rpc::start_rpc_server;
use snarkos_utilities::{to_bytes, ToBytes};
use std::{net::SocketAddr, str::FromStr, sync::Arc};
use tokio::{runtime::Runtime, sync::Mutex};
/// Builds a node from configuration parameters.
/// 1. Creates new storage database or uses existing.
/// 2. Creates new memory pool or uses existing from storage.
/// 3. Creates consensus parameters.
/// 4. Creates network server.
/// 5. Starts rpc server thread.
/// 6. Starts miner thread.
/// 7. Starts network server listener.
async fn start_server(config: Config) -> Result<(), NodeError> {
match config.node.verbose {
0 => {}
verbosity => {
match verbosity {
1 => std::env::set_var("RUST_LOG", "info"),
2 => std::env::set_var("RUST_LOG", "debug"),
_ => std::env::set_var("RUST_LOG", "info"),
};
tracing_subscriber::fmt::init();
println!("{}", render_init(&config));
}
}
let address = format! {"{}:{}", config.node.ip, config.node.port};
let socket_address = address.parse::<SocketAddr>()?;
let mut path = config.node.dir;
path.push(&config.node.db);
let storage = Arc::new(MerkleTreeLedger::open_at_path(path.clone())?);
let memory_pool = MemoryPool::from_storage(&storage.clone())?;
let memory_pool_lock = Arc::new(Mutex::new(memory_pool.clone()));
let bootnode = match config.p2p.bootnodes.len() {
0 => socket_address,
_ => config.p2p.bootnodes[0].parse::<SocketAddr>()?,
};
let sync_handler = SyncHandler::new(bootnode);
let sync_handler_lock = Arc::new(Mutex::new(sync_handler));
info!("Loading Aleo parameters...");
let parameters = PublicParameters::<Components>::load(!config.miner.is_miner)?;
info!("Loading complete.");
// Fetch the valid inner snark ids
let inner_snark_vk: <<Components as BaseDPCComponents>::InnerSNARK as SNARK>::VerificationParameters =
parameters.inner_snark_parameters.1.clone().into();
let inner_snark_id = parameters
.system_parameters
.inner_snark_verification_key_crh
.hash(&to_bytes![inner_snark_vk]?)?;
let authorized_inner_snark_ids = vec![to_bytes![inner_snark_id]?];
// Set the initial consensus parameters.
let consensus = ConsensusParameters {
max_block_size: 1_000_000_000usize,
max_nonce: u32::max_value(),
target_block_time: 10i64,
network: Network::from_network_id(config.aleo.network_id),
verifier: PoswMarlin::verify_only().expect("could not instantiate PoSW verifier"),
authorized_inner_snark_ids,
};
let mut context = Arc::new(Context::new(
socket_address,
config.p2p.mempool_interval,
config.p2p.min_peers,
config.p2p.max_peers,
config.node.is_bootnode,
config.p2p.bootnodes.clone(),
false,
));
// Start the miner task, if the mining configuration is enabled.
if config.miner.is_miner {
match AccountAddress::<Components>::from_str(&config.miner.miner_address) {
Ok(miner_address) => {
if let Some(mutable_context) = Arc::get_mut(&mut context) {
mutable_context.is_miner = true;
}
MinerInstance::new(
miner_address,
consensus.clone(),
parameters.clone(),
storage.clone(),
memory_pool_lock.clone(),
context.clone(),
)
.spawn();
}
Err(_) => info!(
"Miner not started. Please specify a valid miner address in your ~/.snarkOS/config.toml file or by using the --miner-address option in the CLI."
),
}
}
// Construct the server instance. Note this does not start the server.
let server = Server::new(
context,
consensus.clone(),
storage.clone(),
parameters,
memory_pool_lock.clone(),
sync_handler_lock.clone(),
15000, // 15 seconds
);
// Start RPC thread, if the RPC configuration is enabled.
if config.rpc.json_rpc {
info!("Loading Aleo parameters for RPC...");
let proving_parameters = PublicParameters::<Components>::load(!config.miner.is_miner)?;
info!("Loading complete.");
// Open a secondary storage instance to prevent resource sharing and bottle-necking.
let secondary_storage = Arc::new(MerkleTreeLedger::open_secondary_at_path(path.clone())?);
start_rpc_server(
config.rpc.port,
secondary_storage.clone(),
path,
proving_parameters,
server.context.clone(),
consensus.clone(),
memory_pool_lock.clone(),
sync_handler_lock.clone(),
config.rpc.username,
config.rpc.password,
)
.await?;
}
// Start the main server thread.
server.listen().await?;
Ok(())
}
fn main() -> Result<(), NodeError> {
let arguments = ConfigCli::new();
let config: Config = ConfigCli::parse(&arguments)?;
Runtime::new()?.block_on(start_server(config))?;
Ok(())
}