Skip to content

Commit

Permalink
feat: Add Babbage primitives (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega authored Jun 18, 2022
1 parent da44e02 commit 79c037a
Show file tree
Hide file tree
Showing 6 changed files with 564 additions and 37 deletions.
2 changes: 1 addition & 1 deletion pallas-codec/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ where
}

/// Wraps a struct so that it is encoded/decoded as a cbor bytes
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct CborWrap<T>(pub T);

impl<'b, C, T> minicbor::Decode<'b, C> for CborWrap<T>
Expand Down
70 changes: 34 additions & 36 deletions pallas-primitives/src/alonzo/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,29 @@ pub struct HeaderBody {
pub block_body_hash: Hash<32>,

#[n(9)]
pub operational_cert: ByteVec,
pub operational_cert: OperationalCert,

#[n(10)]
pub unknown_0: u64,
pub protocol: ProtocolVersion,
}

#[n(11)]
pub unknown_1: u64,
#[derive(Encode, Decode, Debug, PartialEq, Clone)]
pub struct OperationalCert {
#[n(0)]
hot_vkey: ByteVec,

#[n(12)]
pub unknown_2: ByteVec,
#[n(1)]
sequence_number: u64,

#[n(13)]
pub protocol_version_major: u64,
#[n(2)]
kes_period: u64,

#[n(14)]
pub protocol_version_minor: u64,
#[n(3)]
sigma: ByteVec,
}

pub type ProtocolVersion = (u64, u64);

#[derive(Encode, Decode, Debug, PartialEq)]
pub struct KesSignature {}

Expand Down Expand Up @@ -634,8 +639,6 @@ pub type CostModel = MaybeIndefArray<i32>;

pub type CostMdls = KeyValuePairs<Language, CostModel>;

pub type ProtocolVersion = (u32, u32);

#[derive(Encode, Decode, Debug, PartialEq, Clone)]
#[cbor(map)]
pub struct ProtocolParamUpdate {
Expand Down Expand Up @@ -1146,11 +1149,13 @@ pub struct TransactionWitnessSet {

#[derive(Encode, Decode, Debug, PartialEq, Clone)]
#[cbor(map)]
pub struct AlonzoAuxiliaryData {
pub struct PostAlonzoAuxiliaryData {
#[n(0)]
pub metadata: Option<Metadata>,

#[n(1)]
pub native_scripts: Option<MaybeIndefArray<NativeScript>>,

#[n(2)]
pub plutus_scripts: Option<MaybeIndefArray<PlutusScript>>,
}
Expand Down Expand Up @@ -1246,14 +1251,20 @@ pub type MetadatumLabel = AnyUInt;

pub type Metadata = KeyValuePairs<MetadatumLabel, Metadatum>;

#[derive(Encode, Decode, Debug, PartialEq, Clone)]
pub struct ShelleyMaAuxiliaryDAta {
#[n(0)]
transaction_metadata: Metadata,

#[n(1)]
auxiliary_scripts: Option<MaybeIndefArray<NativeScript>>,
}

#[derive(Debug, PartialEq, Clone)]
pub enum AuxiliaryData {
Shelley(Metadata),
ShelleyMa {
transaction_metadata: Metadata,
auxiliary_scripts: Option<MaybeIndefArray<NativeScript>>,
},
Alonzo(AlonzoAuxiliaryData),
ShelleyMa(ShelleyMaAuxiliaryDAta),
PostAlonzo(PostAlonzoAuxiliaryData),
}

impl<'b, C> minicbor::Decode<'b, C> for AuxiliaryData {
Expand All @@ -1262,18 +1273,10 @@ impl<'b, C> minicbor::Decode<'b, C> for AuxiliaryData {
minicbor::data::Type::Map | minicbor::data::Type::MapIndef => {
Ok(AuxiliaryData::Shelley(d.decode_with(ctx)?))
}
minicbor::data::Type::Array => {
d.array()?;
let transaction_metadata = d.decode_with(ctx)?;
let auxiliary_scripts = d.decode_with(ctx)?;
Ok(AuxiliaryData::ShelleyMa {
transaction_metadata,
auxiliary_scripts,
})
}
minicbor::data::Type::Array => Ok(AuxiliaryData::ShelleyMa(d.decode_with(ctx)?)),
minicbor::data::Type::Tag => {
d.tag()?;
Ok(AuxiliaryData::Alonzo(d.decode_with(ctx)?))
Ok(AuxiliaryData::PostAlonzo(d.decode_with(ctx)?))
}
_ => Err(minicbor::decode::Error::message(
"Can't infer variant from data type for AuxiliaryData",
Expand All @@ -1292,15 +1295,10 @@ impl<C> minicbor::Encode<C> for AuxiliaryData {
AuxiliaryData::Shelley(m) => {
e.encode_with(m, ctx)?;
}
AuxiliaryData::ShelleyMa {
transaction_metadata,
auxiliary_scripts,
} => {
e.array(2)?;
e.encode_with(transaction_metadata, ctx)?;
e.encode_with(auxiliary_scripts, ctx)?;
AuxiliaryData::ShelleyMa(m) => {
e.encode_with(m, ctx)?;
}
AuxiliaryData::Alonzo(v) => {
AuxiliaryData::PostAlonzo(v) => {
// TODO: check if this is the correct tag
e.tag(Tag::Unassigned(259))?;
e.encode_with(v, ctx)?;
Expand Down
3 changes: 3 additions & 0 deletions pallas-primitives/src/babbage/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod model;

pub use model::*;
Loading

0 comments on commit 79c037a

Please sign in to comment.