feat(traverse): Introduce new accessor methods (#152)

This commit is contained in:
Santiago Carmuega 2022-07-16 19:31:27 -03:00 committed by GitHub
parent fbea30aafe
commit 33c6152f19
17 changed files with 507 additions and 238 deletions

View file

@ -280,6 +280,8 @@ pub struct MoveInstantaneousReward {
pub type RewardAccount = ByteVec;
pub type Withdrawals = KeyValuePairs<RewardAccount, Coin>;
pub type Port = u32;
pub type IPv4 = ByteVec;
pub type IPv6 = ByteVec;
@ -719,7 +721,7 @@ pub struct TransactionBody {
pub certificates: Option<MaybeIndefArray<Certificate>>,
#[n(5)]
pub withdrawals: Option<KeyValuePairs<RewardAccount, Coin>>,
pub withdrawals: Option<Withdrawals>,
#[n(6)]
pub update: Option<Update>,

View file

@ -6,8 +6,7 @@ use pallas_codec::minicbor::{bytes::ByteVec, Decode, Encode};
use pallas_crypto::hash::Hash;
use pallas_codec::utils::{
CborWrap, EmptyMap, KeepRaw, KeyValuePairs, MaybeIndefArray, OrderPreservingProperties,
TagWrap, ZeroOrOneArray,
CborWrap, EmptyMap, KeepRaw, KeyValuePairs, MaybeIndefArray, TagWrap, ZeroOrOneArray,
};
// required for derive attrs to work
@ -49,161 +48,14 @@ pub type Signature = ByteVec;
// attributes = {* any => any}
pub type Attributes = EmptyMap;
// Addresses
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum AddrDistr {
Variant0(StakeholderId),
Variant1,
}
impl<'b, C> minicbor::Decode<'b, C> for AddrDistr {
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
d.array()?;
let variant = d.u32()?;
match variant {
0 => Ok(AddrDistr::Variant0(d.decode_with(ctx)?)),
1 => Ok(AddrDistr::Variant1),
_ => Err(minicbor::decode::Error::message(
"invalid variant for addrdstr",
)),
}
}
}
impl minicbor::Encode<()> for AddrDistr {
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
_ctx: &mut (),
) -> Result<(), minicbor::encode::Error<W::Error>> {
match self {
AddrDistr::Variant0(x) => {
e.array(2)?;
e.u32(0)?;
e.encode(x)?;
Ok(())
}
AddrDistr::Variant1 => {
e.array(1)?;
e.u32(1)?;
Ok(())
}
}
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum AddrType {
PubKey,
Script,
Redeem,
Other(u64),
}
impl<'b, C> minicbor::Decode<'b, C> for AddrType {
fn decode(
d: &mut minicbor::Decoder<'b>,
_ctx: &mut C,
) -> Result<Self, minicbor::decode::Error> {
let variant = d.u64()?;
match variant {
0 => Ok(AddrType::PubKey),
1 => Ok(AddrType::Script),
2 => Ok(AddrType::Redeem),
x => Ok(AddrType::Other(x)),
}
}
}
impl<C> minicbor::Encode<C> for AddrType {
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
_ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
match self {
AddrType::PubKey => e.u64(0)?,
AddrType::Script => e.u64(1)?,
AddrType::Redeem => e.u64(2)?,
AddrType::Other(x) => e.u64(*x)?,
};
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum AddrAttrProperty {
AddrDistr(AddrDistr),
Bytes(ByteVec),
Unparsed(u8, ByteVec),
}
impl<'b, C> minicbor::Decode<'b, C> for AddrAttrProperty {
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
let key = d.u8()?;
match key {
0 => Ok(AddrAttrProperty::AddrDistr(d.decode_with(ctx)?)),
1 => Ok(AddrAttrProperty::Bytes(d.decode_with(ctx)?)),
x => Ok(AddrAttrProperty::Unparsed(x, d.decode_with(ctx)?)),
}
}
}
impl<C> minicbor::Encode<C> for AddrAttrProperty {
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
_ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
match self {
AddrAttrProperty::AddrDistr(x) => {
e.u32(0)?;
e.encode(x)?;
Ok(())
}
AddrAttrProperty::Bytes(x) => {
e.u32(1)?;
e.encode(x)?;
Ok(())
}
AddrAttrProperty::Unparsed(a, b) => {
e.encode(a)?;
e.encode(b)?;
Ok(())
}
}
}
}
pub type AddrAttr = OrderPreservingProperties<AddrAttrProperty>;
#[derive(Debug, Encode, Decode, Clone, PartialEq, PartialOrd)]
pub struct AddressPayload {
#[n(0)]
pub root: AddressId,
#[n(1)]
pub attributes: AddrAttr,
#[n(2)]
pub addrtype: AddrType,
}
// The cbor struct of the address payload is now defined in pallas-addresses.
// The primitives crate will treat addresses as a black-box vec of bytes.
// address = [ #6.24(bytes .cbor ([addressid, addrattr, addrtype])), u64 ]
#[derive(Debug, Encode, Decode, Clone, PartialEq, PartialOrd)]
pub struct Address {
#[n(0)]
pub payload: CborWrap<AddressPayload>,
pub payload: TagWrap<ByteVec, 24>,
#[n(1)]
pub crc: u64,
@ -954,6 +806,18 @@ pub struct EbBlock {
pub extra: MaybeIndefArray<Attributes>,
}
#[derive(Encode, Decode, Debug, Clone)]
pub struct MintedEbBlock<'b> {
#[b(0)]
pub header: KeepRaw<'b, EbbHead>,
#[n(1)]
pub body: MaybeIndefArray<StakeholderId>,
#[n(2)]
pub extra: MaybeIndefArray<Attributes>,
}
#[cfg(test)]
mod tests {
use super::{BlockHead, EbBlock, MintedBlock};