Skip to content

Commit

Permalink
add support for sancho net in cardano-net-fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Sep 21, 2023
1 parent 58ef8bb commit 993f367
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 16 deletions.
57 changes: 55 additions & 2 deletions blockchain-source/src/cardano/block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use cardano_sdk::chain::{
AnyCbor, Header, HeaderShelley, HeaderVasil, MetadataSet, TransactionBodies, TxIndexes,
AnyCbor, Coin, Ed25519KeyHashes, Header, HeaderShelley, HeaderVasil, MetadataHash, MetadataSet,
Mint, ScriptDataHash, TransactionInputs, TransactionOutput, TransactionOutputs, TxIndexes,
Withdrawals,
};
use cbored::{CborRepr, DecodeError};
use cbored::{CborDataOf, CborRepr, DecodeError};

// Currently, cardano-sdk can't parse blocks that have Redeemer fields inside the witnesses,
// because of an issue deserializing the data field.
Expand All @@ -13,6 +15,16 @@ use cbored::{CborRepr, DecodeError};
// re-define the block types here, this involves some code duplication, although it also gives us
// better control.

#[derive(Clone, Debug, CborRepr, PartialEq, Eq)]
#[cborrepr(structure = "array")]
pub struct BlockConway {
pub header: HeaderVasil,
pub tx_bodies: TransactionBodies,
pub tx_witnesses: AnyCbor,
pub metadata_set: MetadataSet,
pub invalid_tx: TxIndexes,
}

#[derive(Clone, Debug, CborRepr, PartialEq, Eq)]
#[cborrepr(structure = "array")]
pub struct BlockVasil {
Expand Down Expand Up @@ -50,6 +62,7 @@ pub enum Block {
Block4(BlockShelley),
Alonzo(BlockAlonzo),
Vasil(BlockVasil),
Conway(BlockConway),
}

impl Block {
Expand All @@ -64,6 +77,7 @@ impl Block {
Block::Block4(blk) => blk.header.clone().into(),
Block::Alonzo(blk) => blk.header.clone().into(),
Block::Vasil(blk) => blk.header.clone().into(),
Block::Conway(blk) => blk.header.clone().into(),
}
}

Expand All @@ -74,6 +88,45 @@ impl Block {
Block::Block4(blk) => &blk.tx_bodies,
Block::Alonzo(blk) => &blk.tx_bodies,
Block::Vasil(blk) => &blk.tx_bodies,
Block::Conway(blk) => &blk.tx_bodies,
}
}
}

/// Transaction Body
#[derive(Clone, Debug, CborRepr, PartialEq, Eq)]
#[cborrepr(structure = "mapint", skipkey = 10, skipkey = 12)]
pub struct TransactionBody {
#[cborrepr(mandatory)]
pub inputs: TransactionInputs,
#[cborrepr(mandatory)]
pub outputs: TransactionOutputs,
#[cborrepr(mandatory)]
pub fee: Coin,
pub ttl: Option<u64>,
pub certs: Option<AnyCbor>,
pub withdrawals: Option<Withdrawals>,
pub update: Option<AnyCbor>,
pub metadata_hash: Option<MetadataHash>,
pub validity_start_interval: Option<u64>,
pub mint: Option<Mint>,
pub script_data_hash: Option<ScriptDataHash>,
pub collateral: Option<TransactionInputs>,
pub required_signers: Option<Ed25519KeyHashes>,
pub network_id: Option<u8>,
pub collateral_return: Option<TransactionOutput>,
pub total_collateral: Option<Coin>,
pub reference_inputs: Option<TransactionInputs>,
pub voting_procedures: Option<AnyCbor>,
pub proposal_procedures: Option<AnyCbor>,
pub current_treasury_value: Option<Coin>,
pub donation: Option<AnyCbor>,
}

cardano_sdk::vec_structure!(TransactionBodies, CborDataOf<TransactionBody>, []);

impl TransactionBodies {
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
26 changes: 26 additions & 0 deletions blockchain-source/src/cardano/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,30 @@ impl NetworkConfiguration {
shelley_era_config: Era::SHELLEY_PREVIEW,
}
}

pub fn sancho() -> Self {
Self {
chain_info: ChainInfo::Custom {
protocol_magic: 4,
network_id: 1,
},
relay: (
Cow::Borrowed("sanchonet-node.world.dev.cardano.org."),
30004,
),
from: Point::BlockHeader {
slot_nb: SlotNumber::new(20),
hash: BlockId::new(
"6a7d97aae2a65ca790fd14802808b7fce00a3362bd7b21c4ed4ccb4296783b98",
),
},
genesis_parent: BlockId::new(
"6a7d97aae2a65ca790fd14802808b7fce00a3362bd7b21c4ed4ccb4296783b98",
),
genesis: BlockId::new(
"6a7d97aae2a65ca790fd14802808b7fce00a3362bd7b21c4ed4ccb4296783b98",
),
shelley_era_config: Era::SHELLEY_SANCHO,
}
}
}
8 changes: 8 additions & 0 deletions blockchain-source/src/cardano/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ impl Era {
epoch_length_seconds: 86400,
};

pub const SHELLEY_SANCHO: Self = Self {
first_slot: 0,
start_epoch: 0,
known_time: 1686789000,
slot_length: 1,
epoch_length_seconds: 86400,
};

pub const fn compute_timestamp(&self, slot: u64) -> u64 {
self.known_time + (slot - self.first_slot) * self.slot_length
}
Expand Down
25 changes: 11 additions & 14 deletions cardano-cli-tools/cardano-net-fetcher/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use clap::Parser;

use dcspark_blockchain_source::cardano::Point::{BlockHeader, Origin};
use dcspark_blockchain_source::cardano::Point::BlockHeader;
use dcspark_blockchain_source::cardano::{CardanoNetworkEvent, CardanoSource};

use dcspark_blockchain_source::{GetNextFrom, Source};
use dcspark_core::{BlockId, SlotNumber};
use std::borrow::Cow;

use std::time::Duration;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -42,38 +39,38 @@ async fn main() -> anyhow::Result<()> {
"mainnet" => dcspark_blockchain_source::cardano::NetworkConfiguration::mainnet(),
"preprod" => dcspark_blockchain_source::cardano::NetworkConfiguration::preprod(),
"preview" => dcspark_blockchain_source::cardano::NetworkConfiguration::preview(),
"sancho" => dcspark_blockchain_source::cardano::NetworkConfiguration::sancho(),
_ => return Err(anyhow::anyhow!("network not supported by source")),
};

let from = match since {
None => Origin,
let mut pull_from = match since {
None => vec![],
Some(since) => {
let (since_hash, since_slot) = parse_since(since)?;
BlockHeader {
vec![BlockHeader {
slot_nb: since_slot,
hash: since_hash,
}
}]
}
};

let network_config = dcspark_blockchain_source::cardano::NetworkConfiguration {
relay: (Cow::from(relay_host), relay_port),
from: from.clone(),
..base_config
};

let mut source = CardanoSource::connect(&network_config, Duration::from_secs(20)).await?;

let mut pull_from = from;

while let Some(event) = source.pull(&vec![pull_from.clone()]).await? {
while let Some(event) = source.pull(&pull_from).await? {
let block = match &event {
CardanoNetworkEvent::Tip(_) => continue,
CardanoNetworkEvent::Block(block) => block.clone(),
};

let new_from = event.next_from().unwrap_or(pull_from.clone());
pull_from = new_from;
pull_from = event
.next_from()
.map(|point| vec![point])
.unwrap_or(pull_from.clone());

println!(
"Block #{}, point: {}@{}, raw cbor hex: {}",
Expand Down

0 comments on commit 993f367

Please sign in to comment.