From 59dddc47a6f48a4d04df62a11a01f15defda87b1 Mon Sep 17 00:00:00 2001 From: rooooooooob Date: Tue, 6 Aug 2024 23:45:08 -0700 Subject: [PATCH] Dynamically load custom network configs --- indexer/Cargo.toml | 10 ++++--- indexer/configs/custom.yml | 38 ++++++++++++++++++++++++++ indexer/src/main.rs | 52 +++++++++++------------------------- indexer/src/sinks/cardano.rs | 1 + 4 files changed, 60 insertions(+), 41 deletions(-) create mode 100644 indexer/configs/custom.yml diff --git a/indexer/Cargo.toml b/indexer/Cargo.toml index 049cd77b..45dbb979 100644 --- a/indexer/Cargo.toml +++ b/indexer/Cargo.toml @@ -9,10 +9,12 @@ strip = true [dependencies] # [core] -dcspark-core = { git = "https://github.com/dcSpark/dcspark-core.git", rev = "572af17e3e22101dee64e0999049a571aea26e0f" } -dcspark-blockchain-source = { git = "https://github.com/dcSpark/dcspark-core.git", rev = "572af17e3e22101dee64e0999049a571aea26e0f" } -multiverse = { git = "https://github.com/dcSpark/dcspark-core.git", rev = "572af17e3e22101dee64e0999049a571aea26e0f" } - +dcspark-core = { git = "https://github.com/dcSpark/dcspark-core.git", rev = "98c21b8d6c4f72a1c1813eaec3b9ccf1ff67106e" } +dcspark-blockchain-source = { git = "https://github.com/dcSpark/dcspark-core.git", rev = "98c21b8d6c4f72a1c1813eaec3b9ccf1ff67106e" } +#dcspark-core = { path = "../../dcspark-core/core" } +#dcspark-blockchain-source = { path = "../../dcspark-core/blockchain-source" } +#multiverse = { path = "../../dcspark-core/multiverse" } +multiverse = { git = "https://github.com/dcSpark/dcspark-core.git", rev = "98c21b8d6c4f72a1c1813eaec3b9ccf1ff67106e" } # [local] entity = { path = "entity" } migration = { path = "migration" } diff --git a/indexer/configs/custom.yml b/indexer/configs/custom.yml new file mode 100644 index 00000000..9ab0870f --- /dev/null +++ b/indexer/configs/custom.yml @@ -0,0 +1,38 @@ +source: + type: cardano_net + relay: + - localhost + - 3001 + +sink: + type: cardano + db: + type: postgres + database_url: postgresql://carp:1234@localhost:5432/carp_custom5 + network: custom # preview / preprod / testnet / custom + genesis_folder: /home/user/Cardano/carp/indexer/genesis/ + custom_config: + chain_info: + network_id: 1 + protocol_magic: 42 + relay: + - "localhost" + - 3001 + from: + BlockHeader: + slot_nb: 1 + hash: "ba8066f73eb9cf4ad9adf38051c54d3a51d92cb98561cffc1f202b1b97739cd5" + genesis_parent: "0ded594a3411f6d3236228abc1e2ef8c2a21e09d859ea23bfc2182f92853cba8" + genesis: + BlockHeader: + slot_nb: 0 + hash: "7a32184d9e0068b0fa75fd0ecaad798f9bc573d4921c519b12968e26ff0747a3" + shelley_era_config: + first_slot: 0 + start_epoch: 0 + known_time: 1722355346 + slot_length: 1 + epoch_length_seconds: 500 + +start_block: + diff --git a/indexer/src/main.rs b/indexer/src/main.rs index a9b12f2f..3e6f444d 100644 --- a/indexer/src/main.rs +++ b/indexer/src/main.rs @@ -55,6 +55,8 @@ pub enum SinkConfig { Cardano { db: DbConfig, network: String, + /// Custom configuration. If not present it will be inferred from the network name + custom_config: Option, genesis_folder: Option, }, } @@ -158,10 +160,20 @@ async fn main() -> anyhow::Result<()> { config }; - let (network, mut sink) = match config.sink { - SinkConfig::Cardano { ref network, .. } => ( + let (network, base_config, mut sink) = match &config.sink { + SinkConfig::Cardano { network, custom_config, .. } => ( network.clone(), - CardanoSink::new(config.sink, exec_plan) + match custom_config { + Some(custom_config) => custom_config.clone(), + None => match network.as_ref() { + "mainnet" => dcspark_blockchain_source::cardano::NetworkConfiguration::mainnet(), + "preprod" => dcspark_blockchain_source::cardano::NetworkConfiguration::preprod(), + "preview" => dcspark_blockchain_source::cardano::NetworkConfiguration::preview(), + "custom" => panic!("sink.custom_config is mandatory when setting network to custom"), + unknown_network => return Err(anyhow::anyhow!("network {unknown_network} not supported by source")), + } + }, + CardanoSink::new(config.sink.clone(), exec_plan) .await .context("Can't create cardano sink")?, ), @@ -184,40 +196,6 @@ async fn main() -> anyhow::Result<()> { main_loop(source, sink, start_from, running, processing_finished).await } SourceConfig::CardanoNet { relay } => { - let base_config = match network.as_ref() { - "mainnet" => dcspark_blockchain_source::cardano::NetworkConfiguration::mainnet(), - "preprod" => dcspark_blockchain_source::cardano::NetworkConfiguration::preprod(), - "preview" => dcspark_blockchain_source::cardano::NetworkConfiguration::preview(), - "custom" => dcspark_blockchain_source::cardano::NetworkConfiguration { - // TODO: dynamically fill in all of this - chain_info: dcspark_blockchain_source::cardano::ChainInfo::Custom { - protocol_magic: 0, - network_id: 0, - }, - relay: (Cow::Borrowed("preprod-node.world.dev.cardano.org."), 30000), - from: dcspark_blockchain_source::cardano::Point::BlockHeader { - slot_nb: dcspark_core::SlotNumber::new(86400), - hash: dcspark_core::BlockId::new( - "c4a1595c5cc7a31eda9e544986fe9387af4e3491afe0ca9a80714f01951bbd5c", - ), - }, - genesis_parent: dcspark_core::BlockId::new( - "d4b8de7a11d929a323373cbab6c1a9bdc931beffff11db111cf9d57356ee1937", - ), - genesis: dcspark_core::BlockId::new( - "9ad7ff320c9cf74e0f5ee78d22a85ce42bb0a487d0506bf60cfb5a91ea4497d2", - ), - shelley_era_config: dcspark_blockchain_source::cardano::time::Era { - first_slot: 4492800, - start_epoch: 208, - known_time: 1596059091, - slot_length: 1, - epoch_length_seconds: 432000, - }, - }, - _ => return Err(anyhow::anyhow!("network not supported by source")), - }; - // try to find a confirmed point. // // this way the multiverse can be temporary, which saves setting up the extra db diff --git a/indexer/src/sinks/cardano.rs b/indexer/src/sinks/cardano.rs index 31689c0a..cbffbe39 100644 --- a/indexer/src/sinks/cardano.rs +++ b/indexer/src/sinks/cardano.rs @@ -44,6 +44,7 @@ impl CardanoSink { SinkConfig::Cardano { db, network, + custom_config: _, genesis_folder, } => (db, network, genesis_folder), _ => todo!("Invalid sink config provided"),