From 0badb1b41383486829f099e44682168cab2fe16b Mon Sep 17 00:00:00 2001 From: Enzo Cioppettini <48031343+ecioppettini@users.noreply.github.com> Date: Tue, 1 Oct 2024 03:23:45 -0300 Subject: [PATCH] parse the genesis inside the task to keep things working well without the new task --- indexer/src/sinks/cardano.rs | 16 +--------- indexer/tasks/src/dsl/database_task.rs | 9 ++++-- indexer/tasks/src/dsl/task_macro.rs | 2 +- indexer/tasks/src/genesis/genesis_executor.rs | 4 +-- indexer/tasks/src/genesis/shelley_genesis.rs | 30 ++++++++++++++----- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/indexer/src/sinks/cardano.rs b/indexer/src/sinks/cardano.rs index 88fbac33..78938d5b 100644 --- a/indexer/src/sinks/cardano.rs +++ b/indexer/src/sinks/cardano.rs @@ -18,7 +18,6 @@ use entity::{ prelude::{Block, BlockColumn}, sea_orm::{DatabaseConnection, EntityTrait, QueryOrder, QuerySelect}, }; -use std::io::Cursor; use std::path::PathBuf; use std::sync::Arc; use std::sync::Mutex; @@ -27,7 +26,6 @@ use tasks::dsl::database_task::BlockGlobalInfo; use tasks::execution_plan::ExecutionPlan; use tasks::multiera::multiera_executor::process_multiera_block; use tasks::utils::TaskPerfAggregator; -use tokio::io::AsyncReadExt; #[derive(Clone)] pub enum Network { @@ -398,21 +396,9 @@ async fn insert_block( .context("Couldn't get the shelley genesis file from the filesystem") .unwrap(); - let mut buffer = Vec::new(); - - tokio::fs::File::open(genesis_file_path) - .await - .unwrap() - .read_to_end(&mut buffer) - .await - .unwrap(); - - let genesis = cml_chain::genesis::shelley::parse::parse_genesis_data(Cursor::new(buffer)) - .expect("Failed to parse genesis"); - tasks::genesis::genesis_executor::process_shelley_genesis_block( txn, - ("", &genesis, &block_global_info), + ("", &genesis_file_path, &block_global_info), &exec_plan, task_perf_aggregator.clone(), ) diff --git a/indexer/tasks/src/dsl/database_task.rs b/indexer/tasks/src/dsl/database_task.rs index 093124d8..d4145830 100644 --- a/indexer/tasks/src/dsl/database_task.rs +++ b/indexer/tasks/src/dsl/database_task.rs @@ -1,8 +1,11 @@ use crate::utils::TaskPerfAggregator; -use cml_chain::genesis::{byron::config::GenesisData, shelley::config::ShelleyGenesisData}; +use cml_chain::genesis::byron::config::GenesisData; use entity::{block::EraValue, prelude::*, sea_orm::DatabaseTransaction}; use shred::DispatcherBuilder; -use std::sync::{Arc, Mutex}; +use std::{ + path::PathBuf, + sync::{Arc, Mutex}, +}; /// Misc information about blocks that can't be computed from just the block data itself pub struct BlockGlobalInfo { @@ -67,7 +70,7 @@ pub struct GenesisTaskRegistryEntry { #[derive(Copy, Clone)] pub struct ShelleyGenesisTaskRegistryEntry { - pub builder: &'static (dyn for<'a> TaskBuilder<'a, ShelleyGenesisData, BlockGlobalInfo> + Sync), + pub builder: &'static (dyn for<'a> TaskBuilder<'a, PathBuf, BlockGlobalInfo> + Sync), } #[derive(Copy, Clone)] diff --git a/indexer/tasks/src/dsl/task_macro.rs b/indexer/tasks/src/dsl/task_macro.rs index ce9370c6..25122fc4 100644 --- a/indexer/tasks/src/dsl/task_macro.rs +++ b/indexer/tasks/src/dsl/task_macro.rs @@ -19,7 +19,7 @@ macro_rules! era_to_block { GenesisData }; (shelley_genesis) => { - ShelleyGenesisData + PathBuf }; (byron) => { cml_multi_era::MultiEraBlock diff --git a/indexer/tasks/src/genesis/genesis_executor.rs b/indexer/tasks/src/genesis/genesis_executor.rs index 1af6d6b7..8b89f3e2 100644 --- a/indexer/tasks/src/genesis/genesis_executor.rs +++ b/indexer/tasks/src/genesis/genesis_executor.rs @@ -4,9 +4,9 @@ use crate::execution_plan::ExecutionPlan; use crate::utils::find_task_registry_entry; use crate::utils::TaskPerfAggregator; use cml_chain::genesis::byron::config::GenesisData; -use cml_chain::genesis::shelley::config::ShelleyGenesisData; use entity::sea_orm::{prelude::*, DatabaseTransaction}; use shred::{DispatcherBuilder, World}; +use std::path::PathBuf; use std::sync::{Arc, Mutex}; use tokio::runtime::Handle; @@ -63,7 +63,7 @@ pub async fn process_genesis_block( pub async fn process_shelley_genesis_block( txn: &DatabaseTransaction, - block: BlockInfo<'_, ShelleyGenesisData, BlockGlobalInfo>, + block: BlockInfo<'_, PathBuf, BlockGlobalInfo>, exec_plan: &ExecutionPlan, perf_aggregator: Arc>, ) -> Result<(), DbErr> { diff --git a/indexer/tasks/src/genesis/shelley_genesis.rs b/indexer/tasks/src/genesis/shelley_genesis.rs index 83b34670..1dfafbf7 100644 --- a/indexer/tasks/src/genesis/shelley_genesis.rs +++ b/indexer/tasks/src/genesis/shelley_genesis.rs @@ -1,3 +1,6 @@ +use std::io::Cursor; +use std::path::PathBuf; + use crate::config::EmptyConfig::EmptyConfig; use crate::dsl::task_macro::*; use cml_chain::genesis::shelley::config::ShelleyGenesisData; @@ -13,6 +16,7 @@ use entity::{ }; use hex::ToHex; use sea_orm::{QueryOrder, QuerySelect as _}; +use tokio::io::AsyncReadExt as _; carp_task! { name ShelleyGenesisBlockTask; @@ -35,7 +39,7 @@ carp_task! { async fn handle_block( db_tx: &DatabaseTransaction, - block: BlockInfo<'_, ShelleyGenesisData, BlockGlobalInfo>, + block: BlockInfo<'_, PathBuf, BlockGlobalInfo>, ) -> Result<(), DbErr> { if Genesis::find() .filter(GenesisColumn::Era.eq(i32::from(EraValue::Shelley))) @@ -56,6 +60,18 @@ async fn handle_block( return Ok(()); } + let mut buffer = Vec::new(); + + tokio::fs::File::open(block.1) + .await + .unwrap() + .read_to_end(&mut buffer) + .await + .unwrap(); + + let genesis = cml_chain::genesis::shelley::parse::parse_genesis_data(Cursor::new(buffer)) + .expect("Failed to parse genesis"); + let (latest_block_height, latest_block_epoch) = Block::find() .order_by_desc(block::Column::Height) .limit(1) @@ -73,7 +89,7 @@ async fn handle_block( // potentially we may want to add an entry in the era table with values for these though? // or we could read the genesis file here. let byron_slot_duration = 20; - let epoch_length_in_byron_slots = block.1.epoch_length / byron_slot_duration; + let epoch_length_in_byron_slots = genesis.epoch_length / byron_slot_duration; let first_slot = (block.2.epoch_slot.unwrap() / epoch_length_in_byron_slots * epoch_length_in_byron_slots) as i64; @@ -83,7 +99,7 @@ async fn handle_block( height: Set(latest_block_height + 1), epoch: Set(start_epoch), payload: Set(None), - tx_count: Set(block.1.initial_funds.len().try_into().unwrap()), + tx_count: Set(genesis.initial_funds.len().try_into().unwrap()), // TODO: what should we hash? hash: Set(b"shelley-genesis".to_vec()), slot: Set(first_slot.try_into().unwrap()), @@ -99,15 +115,15 @@ async fn handle_block( block_height: Set(latest_block_height + 1), first_slot: Set(first_slot), start_epoch: Set(start_epoch.into()), - epoch_length_seconds: Set(block.1.epoch_length as i64), + epoch_length_seconds: Set(genesis.epoch_length as i64), }) .exec(db_tx) .await?; - let stake_credentials = handle_initial_funds(block, inserted_block, db_tx).await?; + let stake_credentials = + handle_initial_funds((block.0, &genesis, block.2), inserted_block, db_tx).await?; - if let Some(staking) = block - .1 + if let Some(staking) = genesis .staking .as_ref() .filter(|staking| !staking.stake.is_empty())