Skip to content

Commit

Permalink
feat(traverse): Decode Conway block headers properly (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewWestberg authored May 24, 2024
1 parent 69de222 commit 0c36f38
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/n2n-miniprotocols/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async fn do_chainsync(
tracing::info!("epoch boundary");
None
}
MultiEraHeader::AlonzoCompatible(_) | MultiEraHeader::Babbage(_) => {
MultiEraHeader::ShelleyCompatible(_) | MultiEraHeader::BabbageCompatible(_) => {
if next_log.elapsed().as_secs() > 1 {
tracing::info!("chainsync block header: {}", number);
next_log = Instant::now();
Expand Down
4 changes: 2 additions & 2 deletions pallas-network/src/facades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::net::SocketAddr;
use std::path::Path;

Check warning on line 2 in pallas-network/src/facades.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest, stable)

unused import: `std::path::Path`
use std::time::Duration;
use thiserror::Error;
use tracing::{debug, error, warn};
use tracing::{debug, error};

use tokio::net::{TcpListener, ToSocketAddrs};

Expand Down Expand Up @@ -67,7 +67,7 @@ impl KeepAliveLoop {

loop {
interval.tick().await;
warn!("sending keepalive request");
debug!("sending keepalive request");

client
.keepalive_roundtrip()
Expand Down
6 changes: 3 additions & 3 deletions pallas-traverse/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ impl<'b> MultiEraBlock<'b> {
}
MultiEraBlock::Byron(x) => MultiEraHeader::Byron(Cow::Borrowed(&x.header)),
MultiEraBlock::AlonzoCompatible(x, _) => {
MultiEraHeader::AlonzoCompatible(Cow::Borrowed(&x.header))
MultiEraHeader::ShelleyCompatible(Cow::Borrowed(&x.header))
}
MultiEraBlock::Babbage(x) => MultiEraHeader::Babbage(Cow::Borrowed(&x.header)),
MultiEraBlock::Conway(x) => MultiEraHeader::Babbage(Cow::Borrowed(&x.header)),
MultiEraBlock::Babbage(x) => MultiEraHeader::BabbageCompatible(Cow::Borrowed(&x.header)),
MultiEraBlock::Conway(x) => MultiEraHeader::BabbageCompatible(Cow::Borrowed(&x.header)),
}
}

Expand Down
46 changes: 23 additions & 23 deletions pallas-traverse/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ impl<'b> MultiEraHeader<'b> {
Ok(MultiEraHeader::Byron(Cow::Owned(header)))
}
},
5 => {
1..=4 => {
let header = minicbor::decode(cbor).map_err(Error::invalid_cbor)?;
Ok(MultiEraHeader::Babbage(Cow::Owned(header)))
Ok(MultiEraHeader::ShelleyCompatible(Cow::Owned(header)))
}
_ => {
let header = minicbor::decode(cbor).map_err(Error::invalid_cbor)?;
Ok(MultiEraHeader::AlonzoCompatible(Cow::Owned(header)))
Ok(MultiEraHeader::BabbageCompatible(Cow::Owned(header)))
}
}
}

pub fn cbor(&self) -> &'b [u8] {
match self {
MultiEraHeader::EpochBoundary(x) => x.raw_cbor(),
MultiEraHeader::AlonzoCompatible(x) => x.raw_cbor(),
MultiEraHeader::Babbage(x) => x.raw_cbor(),
MultiEraHeader::ShelleyCompatible(x) => x.raw_cbor(),
MultiEraHeader::BabbageCompatible(x) => x.raw_cbor(),
MultiEraHeader::Byron(x) => x.raw_cbor(),
}
}
Expand All @@ -48,16 +48,16 @@ impl<'b> MultiEraHeader<'b> {
.first()
.cloned()
.unwrap_or_default(),
MultiEraHeader::AlonzoCompatible(x) => x.header_body.block_number,
MultiEraHeader::Babbage(x) => x.header_body.block_number,
MultiEraHeader::ShelleyCompatible(x) => x.header_body.block_number,
MultiEraHeader::BabbageCompatible(x) => x.header_body.block_number,
MultiEraHeader::Byron(x) => x.consensus_data.2.first().cloned().unwrap_or_default(),
}
}

pub fn slot(&self) -> u64 {
match self {
MultiEraHeader::AlonzoCompatible(x) => x.header_body.slot,
MultiEraHeader::Babbage(x) => x.header_body.slot,
MultiEraHeader::ShelleyCompatible(x) => x.header_body.slot,
MultiEraHeader::BabbageCompatible(x) => x.header_body.slot,
MultiEraHeader::EpochBoundary(x) => {
let genesis = GenesisValues::default();
genesis.relative_slot_to_absolute(x.consensus_data.epoch_id, 0)
Expand All @@ -72,34 +72,34 @@ impl<'b> MultiEraHeader<'b> {
pub fn hash(&self) -> Hash<32> {
match self {
MultiEraHeader::EpochBoundary(x) => x.original_hash(),
MultiEraHeader::AlonzoCompatible(x) => x.original_hash(),
MultiEraHeader::Babbage(x) => x.original_hash(),
MultiEraHeader::ShelleyCompatible(x) => x.original_hash(),
MultiEraHeader::BabbageCompatible(x) => x.original_hash(),
MultiEraHeader::Byron(x) => x.original_hash(),
}
}

pub fn previous_hash(&self) -> Option<Hash<32>> {
match self {
MultiEraHeader::AlonzoCompatible(x) => x.header_body.prev_hash,
MultiEraHeader::Babbage(x) => x.header_body.prev_hash,
MultiEraHeader::ShelleyCompatible(x) => x.header_body.prev_hash,
MultiEraHeader::BabbageCompatible(x) => x.header_body.prev_hash,
MultiEraHeader::EpochBoundary(x) => Some(x.prev_block),
MultiEraHeader::Byron(x) => Some(x.prev_block),
}
}

pub fn vrf_vkey(&self) -> Option<&[u8]> {
match self {
MultiEraHeader::AlonzoCompatible(x) => Some(x.header_body.vrf_vkey.as_ref()),
MultiEraHeader::Babbage(x) => Some(x.header_body.vrf_vkey.as_ref()),
MultiEraHeader::ShelleyCompatible(x) => Some(x.header_body.vrf_vkey.as_ref()),
MultiEraHeader::BabbageCompatible(x) => Some(x.header_body.vrf_vkey.as_ref()),
MultiEraHeader::EpochBoundary(_) => None,
MultiEraHeader::Byron(_) => None,
}
}

pub fn issuer_vkey(&self) -> Option<&[u8]> {
match self {
MultiEraHeader::AlonzoCompatible(x) => Some(x.header_body.issuer_vkey.as_ref()),
MultiEraHeader::Babbage(x) => Some(x.header_body.issuer_vkey.as_ref()),
MultiEraHeader::ShelleyCompatible(x) => Some(x.header_body.issuer_vkey.as_ref()),
MultiEraHeader::BabbageCompatible(x) => Some(x.header_body.issuer_vkey.as_ref()),
MultiEraHeader::EpochBoundary(_) => None,
MultiEraHeader::Byron(_) => None,
}
Expand All @@ -108,8 +108,8 @@ impl<'b> MultiEraHeader<'b> {
pub fn leader_vrf_output(&self) -> Result<Vec<u8>, Error> {
match self {
MultiEraHeader::EpochBoundary(_) => Err(Error::InvalidEra(Era::Byron)),
MultiEraHeader::AlonzoCompatible(x) => Ok(x.header_body.leader_vrf.0.to_vec()),
MultiEraHeader::Babbage(x) => {
MultiEraHeader::ShelleyCompatible(x) => Ok(x.header_body.leader_vrf.0.to_vec()),
MultiEraHeader::BabbageCompatible(x) => {
let mut leader_tagged_vrf: Vec<u8> = vec![0x4C_u8]; /* "L" */
leader_tagged_vrf.extend(&*x.header_body.vrf_result.0);
Ok(Hasher::<256>::hash(&leader_tagged_vrf).to_vec())
Expand All @@ -121,8 +121,8 @@ impl<'b> MultiEraHeader<'b> {
pub fn nonce_vrf_output(&self) -> Result<Vec<u8>, Error> {
match self {
MultiEraHeader::EpochBoundary(_) => Err(Error::InvalidEra(Era::Byron)),
MultiEraHeader::AlonzoCompatible(x) => Ok(x.header_body.nonce_vrf.0.to_vec()),
MultiEraHeader::Babbage(x) => {
MultiEraHeader::ShelleyCompatible(x) => Ok(x.header_body.nonce_vrf.0.to_vec()),
MultiEraHeader::BabbageCompatible(x) => {
let mut nonce_tagged_vrf: Vec<u8> = vec![0x4E_u8]; /* "N" */
nonce_tagged_vrf.extend(&*x.header_body.vrf_result.0);
Ok(Hasher::<256>::hash(&nonce_tagged_vrf).to_vec())
Expand All @@ -147,14 +147,14 @@ impl<'b> MultiEraHeader<'b> {

pub fn as_alonzo(&self) -> Option<&alonzo::Header> {
match self {
MultiEraHeader::AlonzoCompatible(x) => Some(x.deref().deref()),
MultiEraHeader::ShelleyCompatible(x) => Some(x.deref().deref()),
_ => None,
}
}

pub fn as_babbage(&self) -> Option<&babbage::Header> {
match self {
MultiEraHeader::Babbage(x) => Some(x.deref().deref()),
MultiEraHeader::BabbageCompatible(x) => Some(x.deref().deref()),
_ => None,
}
}
Expand Down
4 changes: 2 additions & 2 deletions pallas-traverse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ pub enum Feature {
#[derive(Debug)]
pub enum MultiEraHeader<'b> {
EpochBoundary(Cow<'b, KeepRaw<'b, byron::EbbHead>>),
AlonzoCompatible(Cow<'b, KeepRaw<'b, alonzo::Header>>),
Babbage(Cow<'b, KeepRaw<'b, babbage::Header>>),
ShelleyCompatible(Cow<'b, KeepRaw<'b, alonzo::Header>>),
BabbageCompatible(Cow<'b, KeepRaw<'b, babbage::Header>>),
Byron(Cow<'b, KeepRaw<'b, byron::BlockHead>>),
}

Expand Down

0 comments on commit 0c36f38

Please sign in to comment.