Skip to content

Commit

Permalink
add the multiera block impls
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Nov 18, 2023
1 parent 95a1b0c commit 843da32
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 46 deletions.
2 changes: 1 addition & 1 deletion indexer/tasks/src/byron/byron_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async fn handle_block(
era: Set(EraValue::Byron.into()),
hash: Set(hash),
height: Set(block.1.number() as i32),
epoch: Set(block.1.epoch() as i32),
epoch: Set(block.1.epoch().unwrap() as i32),
slot: Set(block.1.slot() as i32),
payload: Set(Some(block_payload)),
..Default::default()
Expand Down
158 changes: 113 additions & 45 deletions indexer/tasks/src/multiera_block_ext.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::Deref;

use crate::utils::blake2b256;
use cbor_event::{cbor, Serialize as _};
use cml_chain::assets::{AssetName, Mint, PositiveCoin};
Expand All @@ -13,7 +15,7 @@ use cml_crypto::chain_crypto::{hash::Blake2b224, Blake2b256};
use cml_crypto::{DatumHash, Deserialize, Ed25519KeyHash, Serialize as _};
use cml_multi_era::mary::MaryTransactionOutput;
use cml_multi_era::utils::MultiEraTransactionOutput;
use cml_multi_era::MultiEraTransactionBody;
use cml_multi_era::{MultiEraBlock, MultiEraTransactionBody};
use entity::block::EraValue;

pub(crate) trait MultieraBlockExt {
Expand All @@ -31,95 +33,161 @@ pub(crate) trait MultieraBlockExt {

fn has_aux_data(&self) -> bool;

fn epoch(&self) -> u64;
fn epoch(&self) -> Option<u64>;
}

impl MultieraBlockExt for cml_multi_era::MultiEraBlock {
impl MultieraBlockExt for MultiEraBlock {
fn is_empty(&self) -> bool {
match self {
cml_multi_era::MultiEraBlock::Byron(_) => todo!(),
cml_multi_era::MultiEraBlock::Shelley(_) => todo!(),
cml_multi_era::MultiEraBlock::Allegra(_) => todo!(),
cml_multi_era::MultiEraBlock::Mary(_) => todo!(),
cml_multi_era::MultiEraBlock::Alonzo(_) => todo!(),
cml_multi_era::MultiEraBlock::Babbage(_) => todo!(),
cml_multi_era::MultiEraBlock::Conway(_) => todo!(),
MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::EpochBoundary(_)) => true,
MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::Main(block)) => {
block.body.tx_payload.is_empty()
}
MultiEraBlock::Shelley(block) => block.transaction_bodies.is_empty(),
MultiEraBlock::Allegra(block) => block.transaction_bodies.is_empty(),
MultiEraBlock::Mary(block) => block.transaction_bodies.is_empty(),
MultiEraBlock::Alonzo(block) => block.transaction_bodies.is_empty(),
MultiEraBlock::Babbage(block) => block.transaction_bodies.is_empty(),
MultiEraBlock::Conway(block) => block.transaction_bodies.is_empty(),
}
}

fn tx_len(&self) -> usize {
todo!()
match self {
MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::EpochBoundary(_)) => 0,
MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::Main(block)) => {
block.body.tx_payload.len()
}
MultiEraBlock::Shelley(block) => block.transaction_bodies.len(),
MultiEraBlock::Allegra(block) => block.transaction_bodies.len(),
MultiEraBlock::Mary(block) => block.transaction_bodies.len(),
MultiEraBlock::Alonzo(block) => block.transaction_bodies.len(),
MultiEraBlock::Babbage(block) => block.transaction_bodies.len(),
MultiEraBlock::Conway(block) => block.transaction_bodies.len(),
}
}

// TODO: avoid cloning? but I'm not sure it can be done easily with the current api
fn txs(&self) -> Vec<MultiEraTransactionBody> {
todo!()
match self {
MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::EpochBoundary(_)) => {
vec![]
}
MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::Main(block)) => block
.body
.tx_payload
.iter()
.map(|tx| MultiEraTransactionBody::Byron(tx.byron_tx.clone()))
.collect(),
MultiEraBlock::Shelley(block) => block
.transaction_bodies
.iter()
.map(|tx| MultiEraTransactionBody::Shelley(tx.clone()))
.collect(),
MultiEraBlock::Allegra(block) => block
.transaction_bodies
.iter()
.map(|tx| MultiEraTransactionBody::Allegra(tx.clone()))
.collect(),
MultiEraBlock::Mary(block) => block
.transaction_bodies
.iter()
.map(|tx| MultiEraTransactionBody::Mary(tx.clone()))
.collect(),
MultiEraBlock::Alonzo(block) => block
.transaction_bodies
.iter()
.map(|tx| MultiEraTransactionBody::Alonzo(tx.clone()))
.collect(),
MultiEraBlock::Babbage(block) => block
.transaction_bodies
.iter()
.map(|tx| MultiEraTransactionBody::Babbage(tx.clone()))
.collect(),
MultiEraBlock::Conway(block) => block
.transaction_bodies
.iter()
.map(|tx| MultiEraTransactionBody::Conway(tx.clone()))
.collect(),
}
}

fn hash(&self) -> [u8; 32] {
let header = match self {
cml_multi_era::MultiEraBlock::Byron(
MultiEraBlock::Byron(
// TODO: this may be missing the prefix
cml_multi_era::byron::block::ByronBlock::EpochBoundary(block),
) => cbor!(block.header).unwrap(),

// TODO: this may be missing the prefix
cml_multi_era::MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::Main(
block,
)) => cbor!(block.header).unwrap(),
cml_multi_era::MultiEraBlock::Shelley(block) => block.header.to_canonical_cbor_bytes(),
cml_multi_era::MultiEraBlock::Allegra(block) => block.header.to_canonical_cbor_bytes(),
cml_multi_era::MultiEraBlock::Mary(block) => block.header.to_canonical_cbor_bytes(),
cml_multi_era::MultiEraBlock::Alonzo(block) => block.header.to_canonical_cbor_bytes(),
cml_multi_era::MultiEraBlock::Babbage(block) => block.header.to_canonical_cbor_bytes(),
cml_multi_era::MultiEraBlock::Conway(block) => block.header.to_canonical_cbor_bytes(),
MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::Main(block)) => {
cbor!(block.header).unwrap()
}
MultiEraBlock::Shelley(block) => block.header.to_canonical_cbor_bytes(),
MultiEraBlock::Allegra(block) => block.header.to_canonical_cbor_bytes(),
MultiEraBlock::Mary(block) => block.header.to_canonical_cbor_bytes(),
MultiEraBlock::Alonzo(block) => block.header.to_canonical_cbor_bytes(),
MultiEraBlock::Babbage(block) => block.header.to_canonical_cbor_bytes(),
MultiEraBlock::Conway(block) => block.header.to_canonical_cbor_bytes(),
};

blake2b256(&header)
}

fn number(&self) -> u64 {
match self {
cml_multi_era::MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::Main(
block,
)) => block.header.consensus_data.byron_difficulty.u64,
MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::Main(block)) => {
block.header.consensus_data.byron_difficulty.u64
}

cml_multi_era::MultiEraBlock::Byron(
cml_multi_era::byron::block::ByronBlock::EpochBoundary(block),
) => block.header.consensus_data.byron_difficulty.u64,
MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::EpochBoundary(block)) => {
block.header.consensus_data.byron_difficulty.u64
}
_ => todo!(),
}
}

fn slot(&self) -> u64 {
match self {
cml_multi_era::MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::Main(
block,
)) => {
MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::Main(block)) => {
let byron_slot_id = &block.header.consensus_data.byron_slot_id;
epoch_slot_to_absolute_byron(byron_slot_id.epoch, byron_slot_id.slot)
}

cml_multi_era::MultiEraBlock::Byron(
cml_multi_era::byron::block::ByronBlock::EpochBoundary(block),
) => epoch_slot_to_absolute_byron(block.header.consensus_data.epoch_id, 0),
_ => todo!(),
MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::EpochBoundary(block)) => {
epoch_slot_to_absolute_byron(block.header.consensus_data.epoch_id, 0)
}
MultiEraBlock::Shelley(block) => block.header.body.slot,
MultiEraBlock::Allegra(block) => block.header.body.slot,
MultiEraBlock::Mary(block) => block.header.body.slot,
MultiEraBlock::Alonzo(block) => block.header.body.slot,
MultiEraBlock::Babbage(block) => block.header.header_body.slot,
MultiEraBlock::Conway(block) => block.header.header_body.slot,
}
}

fn has_aux_data(&self) -> bool {
todo!()
match self {
MultiEraBlock::Byron(_) => false,
// TODO: why is this different?
MultiEraBlock::Shelley(_) => false,
MultiEraBlock::Allegra(block) => !block.auxiliary_data_set.deref().is_empty(),
MultiEraBlock::Mary(block) => !block.auxiliary_data_set.deref().is_empty(),
MultiEraBlock::Alonzo(block) => !block.auxiliary_data_set.deref().is_empty(),
MultiEraBlock::Babbage(block) => !block.auxiliary_data_set.deref().is_empty(),
MultiEraBlock::Conway(block) => !block.auxiliary_data_set.deref().is_empty(),
}
}

fn epoch(&self) -> u64 {
fn epoch(&self) -> Option<u64> {
match self {
cml_multi_era::MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::Main(
block,
)) => block.header.consensus_data.byron_slot_id.epoch,
MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::Main(block)) => {
Some(block.header.consensus_data.byron_slot_id.epoch)
}

cml_multi_era::MultiEraBlock::Byron(
cml_multi_era::byron::block::ByronBlock::EpochBoundary(block),
) => block.header.consensus_data.epoch_id,
_ => todo!(),
MultiEraBlock::Byron(cml_multi_era::byron::block::ByronBlock::EpochBoundary(block)) => {
Some(block.header.consensus_data.epoch_id)
}
_ => None,
}
}
}
Expand Down

0 comments on commit 843da32

Please sign in to comment.