-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathmod.rs
440 lines (406 loc) · 17.2 KB
/
mod.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
pub mod file;
pub mod generator;
pub use chainhook_sdk::indexer::IndexerConfig;
use chainhook_sdk::observer::EventObserverConfig;
use chainhook_sdk::types::{
BitcoinBlockSignaling, BitcoinNetwork, StacksNetwork, StacksNodeConfig,
};
pub use file::ConfigFile;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::PathBuf;
const DEFAULT_MAINNET_STACKS_TSV_ARCHIVE: &str =
"https://archive.hiro.so/mainnet/stacks-blockchain-api/mainnet-stacks-blockchain-api-latest";
const DEFAULT_TESTNET_STACKS_TSV_ARCHIVE: &str =
"https://archive.hiro.so/testnet/stacks-blockchain-api/testnet-stacks-blockchain-api-latest";
pub const DEFAULT_REDIS_URI: &str = "redis://localhost:6379/";
pub const DEFAULT_INGESTION_PORT: u16 = 20455;
pub const DEFAULT_CONTROL_PORT: u16 = 20456;
pub const STACKS_SCAN_THREAD_POOL_SIZE: usize = 10;
pub const BITCOIN_SCAN_THREAD_POOL_SIZE: usize = 10;
pub const STACKS_MAX_PREDICATE_REGISTRATION: usize = 50;
pub const BITCOIN_MAX_PREDICATE_REGISTRATION: usize = 50;
#[derive(Clone, Debug, PartialEq)]
pub struct Config {
pub storage: StorageConfig,
pub http_api: PredicatesApi,
pub event_sources: Vec<EventSourceConfig>,
pub limits: LimitsConfig,
pub network: IndexerConfig,
pub monitoring: MonitoringConfig,
}
#[derive(Clone, Debug, PartialEq)]
pub struct StorageConfig {
pub working_dir: String,
}
#[derive(Clone, Debug, PartialEq)]
pub enum PredicatesApi {
Off,
On(PredicatesApiConfig),
}
#[derive(Clone, Debug, PartialEq)]
pub struct PredicatesApiConfig {
pub http_port: u16,
pub database_uri: String,
pub display_logs: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub enum EventSourceConfig {
StacksTsvPath(PathConfig),
StacksTsvUrl(UrlConfig),
OrdinalsSqlitePath(PathConfig),
OrdinalsSqliteUrl(UrlConfig),
}
#[derive(Clone, Debug, PartialEq)]
pub struct PathConfig {
pub file_path: PathBuf,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UrlConfig {
pub file_url: String,
}
#[derive(Clone, Debug, PartialEq)]
pub struct LimitsConfig {
pub max_number_of_bitcoin_predicates: usize,
pub max_number_of_concurrent_bitcoin_scans: usize,
pub max_number_of_stacks_predicates: usize,
pub max_number_of_concurrent_stacks_scans: usize,
pub max_number_of_processing_threads: usize,
pub max_number_of_networking_threads: usize,
pub max_caching_memory_size_mb: usize,
}
#[derive(Clone, Debug, PartialEq)]
pub struct MonitoringConfig {
pub prometheus_monitoring_port: Option<u16>,
}
impl Config {
pub fn from_file_path(file_path: &str) -> Result<Config, String> {
let file = File::open(file_path)
.map_err(|e| format!("unable to read file {}\n{:?}", file_path, e))?;
let mut file_reader = BufReader::new(file);
let mut file_buffer = vec![];
file_reader
.read_to_end(&mut file_buffer)
.map_err(|e| format!("unable to read file {}\n{:?}", file_path, e))?;
let config_file: ConfigFile = match toml::from_slice(&file_buffer) {
Ok(s) => s,
Err(e) => {
return Err(format!("Config file malformatted {}", e.to_string()));
}
};
Config::from_config_file(config_file)
}
pub fn is_http_api_enabled(&self) -> bool {
match self.http_api {
PredicatesApi::Off => false,
PredicatesApi::On(_) => true,
}
}
pub fn get_event_observer_config(&self) -> EventObserverConfig {
EventObserverConfig {
bitcoin_rpc_proxy_enabled: true,
chainhook_config: None,
ingestion_port: DEFAULT_INGESTION_PORT,
bitcoind_rpc_username: self.network.bitcoind_rpc_username.clone(),
bitcoind_rpc_password: self.network.bitcoind_rpc_password.clone(),
bitcoind_rpc_url: self.network.bitcoind_rpc_url.clone(),
bitcoin_block_signaling: self.network.bitcoin_block_signaling.clone(),
display_logs: false,
cache_path: self.storage.working_dir.clone(),
bitcoin_network: self.network.bitcoin_network.clone(),
stacks_network: self.network.stacks_network.clone(),
data_handler_tx: None,
prometheus_monitoring_port: self.monitoring.prometheus_monitoring_port,
}
}
pub fn from_config_file(config_file: ConfigFile) -> Result<Config, String> {
let (stacks_network, bitcoin_network) = match config_file.network.mode.as_str() {
"devnet" => (StacksNetwork::Devnet, BitcoinNetwork::Regtest),
"testnet" => (StacksNetwork::Testnet, BitcoinNetwork::Testnet),
"mainnet" => (StacksNetwork::Mainnet, BitcoinNetwork::Mainnet),
_ => return Err("network.mode not supported".to_string()),
};
let mut event_sources = vec![];
for source in config_file.event_source.unwrap_or(vec![]).iter_mut() {
if let Some(dst) = source.tsv_file_path.take() {
let mut file_path = PathBuf::new();
file_path.push(dst);
event_sources.push(EventSourceConfig::StacksTsvPath(PathConfig { file_path }));
continue;
}
if let Some(file_url) = source.tsv_file_url.take() {
event_sources.push(EventSourceConfig::StacksTsvUrl(UrlConfig { file_url }));
continue;
}
}
let prometheus_monitoring_port = if let Some(monitoring) = config_file.monitoring {
monitoring.prometheus_monitoring_port
} else {
None
};
let config = Config {
storage: StorageConfig {
working_dir: config_file.storage.working_dir.unwrap_or("cache".into()),
},
http_api: match config_file.http_api {
None => PredicatesApi::Off,
Some(http_api) => match http_api.disabled {
Some(true) => PredicatesApi::Off,
_ => PredicatesApi::On(PredicatesApiConfig {
http_port: http_api.http_port.unwrap_or(DEFAULT_CONTROL_PORT),
display_logs: http_api.display_logs.unwrap_or(true),
database_uri: http_api
.database_uri
.unwrap_or(DEFAULT_REDIS_URI.to_string()),
}),
},
},
event_sources,
limits: LimitsConfig {
max_number_of_stacks_predicates: config_file
.limits
.max_number_of_stacks_predicates
.unwrap_or(STACKS_MAX_PREDICATE_REGISTRATION),
max_number_of_bitcoin_predicates: config_file
.limits
.max_number_of_bitcoin_predicates
.unwrap_or(BITCOIN_MAX_PREDICATE_REGISTRATION),
max_number_of_concurrent_stacks_scans: config_file
.limits
.max_number_of_concurrent_stacks_scans
.unwrap_or(STACKS_SCAN_THREAD_POOL_SIZE),
max_number_of_concurrent_bitcoin_scans: config_file
.limits
.max_number_of_concurrent_bitcoin_scans
.unwrap_or(BITCOIN_SCAN_THREAD_POOL_SIZE),
max_number_of_processing_threads: config_file
.limits
.max_number_of_processing_threads
.unwrap_or(1.max(num_cpus::get().saturating_sub(1))),
max_number_of_networking_threads: config_file
.limits
.max_number_of_networking_threads
.unwrap_or(1.max(num_cpus::get().saturating_sub(1))),
max_caching_memory_size_mb: config_file
.limits
.max_caching_memory_size_mb
.unwrap_or(2048),
},
network: IndexerConfig {
bitcoind_rpc_url: config_file.network.bitcoind_rpc_url.to_string(),
bitcoind_rpc_username: config_file.network.bitcoind_rpc_username.to_string(),
bitcoind_rpc_password: config_file.network.bitcoind_rpc_password.to_string(),
bitcoin_block_signaling: match config_file.network.bitcoind_zmq_url {
Some(ref zmq_url) => BitcoinBlockSignaling::ZeroMQ(zmq_url.clone()),
None => BitcoinBlockSignaling::Stacks(StacksNodeConfig::default_localhost(
config_file
.network
.stacks_events_ingestion_port
.unwrap_or(DEFAULT_INGESTION_PORT),
)),
},
stacks_network,
bitcoin_network,
},
monitoring: MonitoringConfig {
prometheus_monitoring_port,
},
};
Ok(config)
}
pub fn is_initial_ingestion_required(&self) -> bool {
for source in self.event_sources.iter() {
match source {
EventSourceConfig::StacksTsvUrl(_) | EventSourceConfig::StacksTsvPath(_) => {
return true
}
_ => {}
}
}
return false;
}
pub fn add_local_stacks_tsv_source(&mut self, file_path: &PathBuf) {
self.event_sources
.push(EventSourceConfig::StacksTsvPath(PathConfig {
file_path: file_path.clone(),
}));
}
pub fn expected_api_database_uri(&self) -> &str {
&self.expected_api_config().database_uri
}
pub fn expected_api_config(&self) -> &PredicatesApiConfig {
match self.http_api {
PredicatesApi::On(ref config) => config,
_ => unreachable!(),
}
}
pub fn expected_local_stacks_tsv_file(&self) -> Result<&PathBuf, String> {
for source in self.event_sources.iter() {
if let EventSourceConfig::StacksTsvPath(config) = source {
return Ok(&config.file_path);
}
}
Err("could not find expected local tsv source")?
}
pub fn expected_cache_path(&self) -> PathBuf {
let mut destination_path = PathBuf::new();
destination_path.push(&self.storage.working_dir);
destination_path
}
fn expected_remote_stacks_tsv_base_url(&self) -> Result<&String, String> {
for source in self.event_sources.iter() {
if let EventSourceConfig::StacksTsvUrl(config) = source {
return Ok(&config.file_url);
}
}
Err("could not find expected remote tsv source")?
}
pub fn expected_remote_stacks_tsv_sha256(&self) -> Result<String, String> {
self.expected_remote_stacks_tsv_base_url()
.map(|url| format!("{}.sha256", url))
}
pub fn expected_remote_stacks_tsv_url(&self) -> Result<String, String> {
self.expected_remote_stacks_tsv_base_url()
.map(|url| format!("{}.gz", url))
}
pub fn rely_on_remote_stacks_tsv(&self) -> bool {
for source in self.event_sources.iter() {
if let EventSourceConfig::StacksTsvUrl(_config) = source {
return true;
}
}
false
}
pub fn should_download_remote_stacks_tsv(&self) -> bool {
let mut rely_on_remote_tsv = false;
let mut remote_tsv_present_locally = false;
for source in self.event_sources.iter() {
if let EventSourceConfig::StacksTsvUrl(_config) = source {
rely_on_remote_tsv = true;
}
if let EventSourceConfig::StacksTsvPath(_config) = source {
remote_tsv_present_locally = true;
}
}
rely_on_remote_tsv == true && remote_tsv_present_locally == false
}
pub fn default(
devnet: bool,
testnet: bool,
mainnet: bool,
config_path: &Option<String>,
) -> Result<Config, String> {
let config = match (devnet, testnet, mainnet, config_path) {
(true, false, false, _) => Config::devnet_default(),
(false, true, false, _) => Config::testnet_default(),
(false, false, true, _) => Config::mainnet_default(),
(false, false, false, Some(config_path)) => Config::from_file_path(&config_path)?,
_ => Err("Invalid combination of arguments".to_string())?,
};
Ok(config)
}
pub fn devnet_default() -> Config {
Config {
storage: StorageConfig {
working_dir: default_cache_path(),
},
http_api: PredicatesApi::Off,
event_sources: vec![],
limits: LimitsConfig {
max_number_of_bitcoin_predicates: BITCOIN_MAX_PREDICATE_REGISTRATION,
max_number_of_concurrent_bitcoin_scans: BITCOIN_SCAN_THREAD_POOL_SIZE,
max_number_of_stacks_predicates: STACKS_MAX_PREDICATE_REGISTRATION,
max_number_of_concurrent_stacks_scans: STACKS_SCAN_THREAD_POOL_SIZE,
max_number_of_processing_threads: 1.max(num_cpus::get().saturating_sub(1)),
max_number_of_networking_threads: 1.max(num_cpus::get().saturating_sub(1)),
max_caching_memory_size_mb: 2048,
},
network: IndexerConfig {
bitcoind_rpc_url: "http://0.0.0.0:18443".into(),
bitcoind_rpc_username: "devnet".into(),
bitcoind_rpc_password: "devnet".into(),
bitcoin_block_signaling: BitcoinBlockSignaling::Stacks(
StacksNodeConfig::default_localhost(DEFAULT_INGESTION_PORT),
),
stacks_network: StacksNetwork::Devnet,
bitcoin_network: BitcoinNetwork::Regtest,
},
monitoring: MonitoringConfig {
prometheus_monitoring_port: None,
},
}
}
pub fn testnet_default() -> Config {
Config {
storage: StorageConfig {
working_dir: default_cache_path(),
},
http_api: PredicatesApi::Off,
event_sources: vec![EventSourceConfig::StacksTsvUrl(UrlConfig {
file_url: DEFAULT_TESTNET_STACKS_TSV_ARCHIVE.into(),
})],
limits: LimitsConfig {
max_number_of_bitcoin_predicates: BITCOIN_MAX_PREDICATE_REGISTRATION,
max_number_of_concurrent_bitcoin_scans: BITCOIN_SCAN_THREAD_POOL_SIZE,
max_number_of_stacks_predicates: STACKS_MAX_PREDICATE_REGISTRATION,
max_number_of_concurrent_stacks_scans: STACKS_SCAN_THREAD_POOL_SIZE,
max_number_of_processing_threads: 1.max(num_cpus::get().saturating_sub(1)),
max_number_of_networking_threads: 1.max(num_cpus::get().saturating_sub(1)),
max_caching_memory_size_mb: 2048,
},
network: IndexerConfig {
bitcoind_rpc_url: "http://0.0.0.0:18332".into(),
bitcoind_rpc_username: "devnet".into(),
bitcoind_rpc_password: "devnet".into(),
bitcoin_block_signaling: BitcoinBlockSignaling::Stacks(
StacksNodeConfig::default_localhost(DEFAULT_INGESTION_PORT),
),
stacks_network: StacksNetwork::Testnet,
bitcoin_network: BitcoinNetwork::Testnet,
},
monitoring: MonitoringConfig {
prometheus_monitoring_port: None,
},
}
}
pub fn mainnet_default() -> Config {
Config {
storage: StorageConfig {
working_dir: default_cache_path(),
},
http_api: PredicatesApi::Off,
event_sources: vec![EventSourceConfig::StacksTsvUrl(UrlConfig {
file_url: DEFAULT_MAINNET_STACKS_TSV_ARCHIVE.into(),
})],
limits: LimitsConfig {
max_number_of_bitcoin_predicates: BITCOIN_MAX_PREDICATE_REGISTRATION,
max_number_of_concurrent_bitcoin_scans: BITCOIN_SCAN_THREAD_POOL_SIZE,
max_number_of_stacks_predicates: STACKS_MAX_PREDICATE_REGISTRATION,
max_number_of_concurrent_stacks_scans: STACKS_SCAN_THREAD_POOL_SIZE,
max_number_of_processing_threads: 1.max(num_cpus::get().saturating_sub(1)),
max_number_of_networking_threads: 1.max(num_cpus::get().saturating_sub(1)),
max_caching_memory_size_mb: 2048,
},
network: IndexerConfig {
bitcoind_rpc_url: "http://0.0.0.0:8332".into(),
bitcoind_rpc_username: "devnet".into(),
bitcoind_rpc_password: "devnet".into(),
bitcoin_block_signaling: BitcoinBlockSignaling::Stacks(
StacksNodeConfig::default_localhost(DEFAULT_INGESTION_PORT),
),
stacks_network: StacksNetwork::Mainnet,
bitcoin_network: BitcoinNetwork::Mainnet,
},
monitoring: MonitoringConfig {
prometheus_monitoring_port: None,
},
}
}
}
pub fn default_cache_path() -> String {
let mut cache_path = std::env::current_dir().expect("unable to get current dir");
cache_path.push("cache");
format!("{}", cache_path.display())
}
#[cfg(test)]
pub mod tests;