Skip to content

Commit 135297e

Browse files
author
Ludo Galabru
committed
feat: dry config
1 parent fc17dac commit 135297e

File tree

8 files changed

+49
-25
lines changed

8 files changed

+49
-25
lines changed

components/chainhook-cli/src/archive/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ pub async fn download_stacks_dataset_if_required(config: &mut Config, ctx: &Cont
233233
} else {
234234
info!(
235235
ctx.expect_logger(),
236-
"Streaming blocks from stacks-node {}", config.network.stacks_node_rpc_url
236+
"Streaming blocks from stacks-node {}",
237+
config.network.get_stacks_node_config().rpc_url
237238
);
238239
false
239240
}
@@ -303,7 +304,7 @@ pub async fn download_ordinals_dataset_if_required(config: &Config, ctx: &Contex
303304
} else {
304305
info!(
305306
ctx.expect_logger(),
306-
"Streaming blocks from bitcoind {}", config.network.stacks_node_rpc_url
307+
"Streaming blocks from bitcoind {}", config.network.bitcoind_rpc_url
307308
);
308309
false
309310
}

components/chainhook-cli/src/config/file.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ pub struct NetworkConfigFile {
4848
pub bitcoind_rpc_username: String,
4949
pub bitcoind_rpc_password: String,
5050
pub bitcoind_zmq_url: Option<String>,
51-
pub stacks_node_rpc_url: String,
51+
pub stacks_node_rpc_url: Option<String>,
52+
pub stacks_events_ingestion_port: Option<u16>,
5253
}

components/chainhook-cli/src/config/generator.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
pub fn generate_config() -> String {
1+
use chainhook_types::BitcoinNetwork;
2+
3+
pub fn generate_config(network: &BitcoinNetwork) -> String {
4+
let network = format!("{:?}", network);
25
let conf = format!(
36
r#"[storage]
47
working_dir = "cache"
@@ -12,7 +15,7 @@ working_dir = "cache"
1215
# database_uri = "redis://localhost:6379/"
1316
1417
[network]
15-
mode = "mainnet"
18+
mode = "{network}"
1619
bitcoind_rpc_url = "http://localhost:8332"
1720
bitcoind_rpc_username = "devnet"
1821
bitcoind_rpc_password = "devnet"
@@ -34,8 +37,9 @@ max_number_of_networking_threads = 16
3437
max_caching_memory_size_mb = 32000
3538
3639
[[event_source]]
37-
tsv_file_url = "https://archive.hiro.so/mainnet/stacks-blockchain-api/mainnet-stacks-blockchain-api-latest"
38-
"#
40+
tsv_file_url = "https://archive.hiro.so/{network}/stacks-blockchain-api/{network}-stacks-blockchain-api-latest"
41+
"#,
42+
network = network.to_lowercase(),
3943
);
4044
return conf;
4145
}

components/chainhook-cli/src/config/mod.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ impl Config {
131131
bitcoind_rpc_username: self.network.bitcoind_rpc_username.clone(),
132132
bitcoind_rpc_password: self.network.bitcoind_rpc_password.clone(),
133133
bitcoind_rpc_url: self.network.bitcoind_rpc_url.clone(),
134-
stacks_node_rpc_url: self.network.stacks_node_rpc_url.clone(),
135134
bitcoin_block_signaling: self.network.bitcoin_block_signaling.clone(),
136135
display_logs: false,
137136
cache_path: self.storage.working_dir.clone(),
@@ -212,15 +211,21 @@ impl Config {
212211
.unwrap_or(2048),
213212
},
214213
network: IndexerConfig {
215-
stacks_node_rpc_url: config_file.network.stacks_node_rpc_url.to_string(),
216214
bitcoind_rpc_url: config_file.network.bitcoind_rpc_url.to_string(),
217215
bitcoind_rpc_username: config_file.network.bitcoind_rpc_username.to_string(),
218216
bitcoind_rpc_password: config_file.network.bitcoind_rpc_password.to_string(),
219217
bitcoin_block_signaling: match config_file.network.bitcoind_zmq_url {
220218
Some(ref zmq_url) => BitcoinBlockSignaling::ZeroMQ(zmq_url.clone()),
221-
None => BitcoinBlockSignaling::Stacks(
222-
config_file.network.stacks_node_rpc_url.clone(),
223-
),
219+
None => BitcoinBlockSignaling::Stacks(StacksNodeConfig {
220+
rpc_url: config_file
221+
.network
222+
.stacks_node_rpc_url
223+
.unwrap_or("http://localhost:20443".to_string()),
224+
ingestion_port: config_file
225+
.network
226+
.stacks_events_ingestion_port
227+
.unwrap_or(DEFAULT_INGESTION_PORT),
228+
}),
224229
},
225230
stacks_network,
226231
bitcoin_network,
@@ -401,7 +406,6 @@ impl Config {
401406
max_caching_memory_size_mb: 2048,
402407
},
403408
network: IndexerConfig {
404-
stacks_node_rpc_url: "http://0.0.0.0:20443".into(),
405409
bitcoind_rpc_url: "http://0.0.0.0:18443".into(),
406410
bitcoind_rpc_username: "devnet".into(),
407411
bitcoind_rpc_password: "devnet".into(),
@@ -433,7 +437,6 @@ impl Config {
433437
max_caching_memory_size_mb: 2048,
434438
},
435439
network: IndexerConfig {
436-
stacks_node_rpc_url: "http://0.0.0.0:20443".into(),
437440
bitcoind_rpc_url: "http://0.0.0.0:18332".into(),
438441
bitcoind_rpc_username: "devnet".into(),
439442
bitcoind_rpc_password: "devnet".into(),
@@ -470,7 +473,6 @@ impl Config {
470473
max_caching_memory_size_mb: 2048,
471474
},
472475
network: IndexerConfig {
473-
stacks_node_rpc_url: "http://0.0.0.0:20443".into(),
474476
bitcoind_rpc_url: "http://0.0.0.0:8332".into(),
475477
bitcoind_rpc_username: "devnet".into(),
476478
bitcoind_rpc_password: "devnet".into(),

components/chainhook-sdk/src/indexer/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::utils::{AbstractBlock, Context};
66

77
use chainhook_types::{
88
BitcoinBlockSignaling, BitcoinNetwork, BlockHeader, BlockIdentifier, BlockchainEvent,
9-
StacksChainEvent, StacksNetwork,
9+
StacksChainEvent, StacksNetwork, StacksNodeConfig,
1010
};
1111
use hiro_system_kit::slog;
1212
use rocket::serde::json::Value as JsonValue;
@@ -53,13 +53,21 @@ impl BitcoinChainContext {
5353
pub struct IndexerConfig {
5454
pub bitcoin_network: BitcoinNetwork,
5555
pub stacks_network: StacksNetwork,
56-
pub stacks_node_rpc_url: String,
5756
pub bitcoind_rpc_url: String,
5857
pub bitcoind_rpc_username: String,
5958
pub bitcoind_rpc_password: String,
6059
pub bitcoin_block_signaling: BitcoinBlockSignaling,
6160
}
6261

62+
impl IndexerConfig {
63+
pub fn get_stacks_node_config(&self) -> &StacksNodeConfig {
64+
match self.bitcoin_block_signaling {
65+
BitcoinBlockSignaling::Stacks(ref config) => config,
66+
_ => unreachable!(),
67+
}
68+
}
69+
}
70+
6371
pub struct Indexer {
6472
pub config: IndexerConfig,
6573
stacks_blocks_pool: StacksBlockPool,

components/chainhook-sdk/src/indexer/stacks/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ pub fn standardize_stacks_block(
288288
&tx.txid,
289289
events,
290290
&mut chain_ctx.asset_class_map,
291-
&indexer_config.stacks_node_rpc_url,
291+
&indexer_config.get_stacks_node_config().rpc_url.clone(),
292292
true,
293293
);
294294

@@ -409,7 +409,7 @@ pub fn standardize_stacks_microblock_trail(
409409
&tx.txid,
410410
events,
411411
&mut chain_ctx.asset_class_map,
412-
&indexer_config.stacks_node_rpc_url,
412+
&indexer_config.get_stacks_node_config().rpc_url.clone(),
413413
true,
414414
);
415415

components/chainhook-sdk/src/observer/mod.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ pub struct EventObserverConfig {
129129
pub bitcoind_rpc_password: String,
130130
pub bitcoind_rpc_url: String,
131131
pub bitcoin_block_signaling: BitcoinBlockSignaling,
132-
pub stacks_node_rpc_url: String,
133132
pub display_logs: bool,
134133
pub cache_path: String,
135134
pub bitcoin_network: BitcoinNetwork,
@@ -170,6 +169,13 @@ impl EventObserverConfig {
170169
bitcoin_config
171170
}
172171

172+
pub fn get_stacks_node_config(&self) -> &StacksNodeConfig {
173+
match self.bitcoin_block_signaling {
174+
BitcoinBlockSignaling::Stacks(ref config) => config,
175+
_ => unreachable!(),
176+
}
177+
}
178+
173179
pub fn new_using_overrides(
174180
overrides: Option<&EventObserverConfigOverrides>,
175181
) -> Result<EventObserverConfig, String> {
@@ -212,8 +218,12 @@ impl EventObserverConfig {
212218
Some(url) => Some(BitcoinBlockSignaling::ZeroMQ(url.clone())),
213219
None => Some(BitcoinBlockSignaling::Stacks(stacks_node_rpc_url.clone())),
214220
})
215-
.unwrap_or(BitcoinBlockSignaling::Stacks(stacks_node_rpc_url.clone())),
216-
stacks_node_rpc_url,
221+
.unwrap_or(BitcoinBlockSignaling::Stacks(StacksNodeConfig {
222+
rpc_url: stacks_node_rpc_url.clone(),
223+
ingestion_port: overrides
224+
.and_then(|c| c.ingestion_port)
225+
.unwrap_or(DEFAULT_INGESTION_PORT),
226+
})),
217227
display_logs: overrides.and_then(|c| c.display_logs).unwrap_or(false),
218228
cache_path: overrides
219229
.and_then(|c| c.cache_path.clone())
@@ -396,7 +406,6 @@ pub async fn start_event_observer(
396406
ctx: Context,
397407
) -> Result<(), Box<dyn Error>> {
398408
let indexer_config = IndexerConfig {
399-
stacks_node_rpc_url: config.stacks_node_rpc_url.clone(),
400409
bitcoind_rpc_url: config.bitcoind_rpc_url.clone(),
401410
bitcoind_rpc_username: config.bitcoind_rpc_username.clone(),
402411
bitcoind_rpc_password: config.bitcoind_rpc_password.clone(),
@@ -417,7 +426,7 @@ pub async fn start_event_observer(
417426
LogLevel::Off
418427
};
419428

420-
let ingestion_port = config.ingestion_port;
429+
let ingestion_port = config.get_stacks_node_config().ingestion_port;
421430
let bitcoin_rpc_proxy_enabled = config.bitcoin_rpc_proxy_enabled;
422431
let bitcoin_config = config.get_bitcoin_config();
423432

components/chainhook-sdk/src/observer/tests/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ fn generate_test_config() -> (EventObserverConfig, ChainhookStore) {
3434
bitcoind_rpc_username: "user".into(),
3535
bitcoind_rpc_password: "user".into(),
3636
bitcoind_rpc_url: "http://localhost:18443".into(),
37-
stacks_node_rpc_url: "http://localhost:20443".into(),
3837
display_logs: false,
3938
bitcoin_block_signaling: BitcoinBlockSignaling::Stacks("http://localhost:20443".into()),
4039
cache_path: "cache".into(),

0 commit comments

Comments
 (0)