Skip to content

Commit

Permalink
Add Babbage nonce/leader vrf extension (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewWestberg authored Jun 20, 2022
1 parent 7f8b384 commit fa12e45
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
1 change: 0 additions & 1 deletion pallas-multiplexer/src/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::{
Arc,
},
thread::{spawn, JoinHandle},
time::Duration,
};

pub type StdIngress = Receiver<Payload>;
Expand Down
2 changes: 1 addition & 1 deletion pallas-traverse/src/era.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl TryFrom<u16> for Era {
4 => Ok(Era::Mary),
5 => Ok(Era::Alonzo),
6 => Ok(Era::Babbage),
x => Err(crate::Error::UnkownEra(x)),
x => Err(crate::Error::UnknownEra(x)),
}
}
}
Expand Down
35 changes: 33 additions & 2 deletions pallas-traverse/src/header.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use pallas_codec::minicbor;
use pallas_crypto::hash::Hash;
use pallas_crypto::hash::{Hash, Hasher};
use pallas_primitives::ToHash;

use crate::{Error, MultiEraHeader};
use crate::Era::Byron;

impl<'b> MultiEraHeader<'b> {
pub fn decode(tag: u8, subtag: Option<u8>, cbor: &'b [u8]) -> Result<Self, Error> {
Expand Down Expand Up @@ -45,4 +46,34 @@ impl<'b> MultiEraHeader<'b> {
MultiEraHeader::Byron(x) => x.to_hash(),
}
}
}

pub fn leader_vrf_output(&self) -> Result<Vec<u8>, Error> {
match self {
MultiEraHeader::EpochBoundary(_) => Err(Error::InvalidEra(Byron)),
MultiEraHeader::AlonzoCompatible(x) => {
Ok(x.header_body.leader_vrf.0.to_vec())
}
MultiEraHeader::Babbage(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())
}
MultiEraHeader::Byron(_) => Err(Error::InvalidEra(Byron)),
}
}

pub fn nonce_vrf_output(&self) -> Result<Vec<u8>, Error> {
match self {
MultiEraHeader::EpochBoundary(_) => Err(Error::InvalidEra(Byron)),
MultiEraHeader::AlonzoCompatible(x) => {
Ok(x.header_body.nonce_vrf.0.to_vec())
}
MultiEraHeader::Babbage(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())
}
MultiEraHeader::Byron(_) => Err(Error::InvalidEra(Byron)),
}
}
}
32 changes: 26 additions & 6 deletions pallas-traverse/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Utilities to traverse over multi-era block data

use std::borrow::Cow;
use std::fmt::Display;
use std::fmt::{Display, Formatter};

use thiserror::Error;

use pallas_codec::utils::KeepRaw;
use pallas_crypto::hash::Hash;
use pallas_primitives::{alonzo, babbage, byron};
use thiserror::Error;

pub mod block;
pub mod cert;
Expand All @@ -23,12 +24,28 @@ pub mod tx;
pub enum Era {
Byron,
Shelley,
Allegra, // time-locks
Mary, // multi-assets
Alonzo, // smart-contracts
Allegra,
// time-locks
Mary,
// multi-assets
Alonzo,
// smart-contracts
Babbage, // CIP-31/32/33
}

impl Display for Era {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Era::Byron => { write!(f, "Byron") }
Era::Shelley => { write!(f, "Shelley") }
Era::Allegra => { write!(f, "Allegra") }
Era::Mary => { write!(f, "Mary") }
Era::Alonzo => { write!(f, "Alonzo") }
Era::Babbage => { write!(f, "Babbage") }
}
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Feature {
Expand Down Expand Up @@ -97,7 +114,10 @@ pub enum Error {
UnknownCbor(String),

#[error("Unknown era tag: {0}")]
UnkownEra(u16),
UnknownEra(u16),

#[error("Invalid era for request: {0}")]
InvalidEra(Era),
}

impl Error {
Expand Down

0 comments on commit fa12e45

Please sign in to comment.