Skip to content

Commit

Permalink
parse the genesis inside the task to keep things working well without…
Browse files Browse the repository at this point in the history
… the new task
  • Loading branch information
ecioppettini committed Oct 1, 2024
1 parent d505f02 commit 0badb1b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
16 changes: 1 addition & 15 deletions indexer/src/sinks/cardano.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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(),
)
Expand Down
9 changes: 6 additions & 3 deletions indexer/tasks/src/dsl/database_task.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion indexer/tasks/src/dsl/task_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ macro_rules! era_to_block {
GenesisData
};
(shelley_genesis) => {
ShelleyGenesisData
PathBuf
};
(byron) => {
cml_multi_era::MultiEraBlock
Expand Down
4 changes: 2 additions & 2 deletions indexer/tasks/src/genesis/genesis_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Mutex<TaskPerfAggregator>>,
) -> Result<(), DbErr> {
Expand Down
30 changes: 23 additions & 7 deletions indexer/tasks/src/genesis/shelley_genesis.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,6 +16,7 @@ use entity::{
};
use hex::ToHex;
use sea_orm::{QueryOrder, QuerySelect as _};
use tokio::io::AsyncReadExt as _;

carp_task! {
name ShelleyGenesisBlockTask;
Expand All @@ -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)))
Expand All @@ -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)
Expand All @@ -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;
Expand All @@ -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()),
Expand All @@ -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())
Expand Down

0 comments on commit 0badb1b

Please sign in to comment.