Skip to content

Commit

Permalink
feat(primitives): Preserve order of map structures (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega authored Sep 14, 2022
1 parent 9232c20 commit 2f7d108
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
45 changes: 36 additions & 9 deletions pallas-codec/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,32 @@ impl<C, const N: usize> minicbor::Encode<C> for SkipCbor<N> {
/// canonicalization for isomorphic decoding / encoding operators, we use a Vec
/// as the underlaying struct for storage of the items (as opposed to a BTreeMap
/// or HashMap).
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum KeyValuePairs<K, V> {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[serde(from = "Vec::<(K, V)>", into = "Vec::<(K, V)>")]
pub enum KeyValuePairs<K, V>
where
K: Clone,
V: Clone,
{
Def(Vec<(K, V)>),
Indef(Vec<(K, V)>),
}

impl<K, V> KeyValuePairs<K, V> {
impl<K, V> KeyValuePairs<K, V>
where
K: Clone,
V: Clone,
{
pub fn to_vec(self) -> Vec<(K, V)> {
self.into()
}
}

impl<K, V> From<KeyValuePairs<K, V>> for Vec<(K, V)> {
impl<K, V> From<KeyValuePairs<K, V>> for Vec<(K, V)>
where
K: Clone,
V: Clone,
{
fn from(other: KeyValuePairs<K, V>) -> Self {
match other {
KeyValuePairs::Def(x) => x,
Expand All @@ -58,7 +71,21 @@ impl<K, V> From<KeyValuePairs<K, V>> for Vec<(K, V)> {
}
}

impl<K, V> Deref for KeyValuePairs<K, V> {
impl<K, V> From<Vec<(K, V)>> for KeyValuePairs<K, V>
where
K: Clone,
V: Clone,
{
fn from(other: Vec<(K, V)>) -> Self {
KeyValuePairs::Def(other)
}
}

impl<K, V> Deref for KeyValuePairs<K, V>
where
K: Clone,
V: Clone,
{
type Target = Vec<(K, V)>;

fn deref(&self) -> &Self::Target {
Expand All @@ -71,8 +98,8 @@ impl<K, V> Deref for KeyValuePairs<K, V> {

impl<'b, C, K, V> minicbor::decode::Decode<'b, C> for KeyValuePairs<K, V>
where
K: Encode<C> + Decode<'b, C>,
V: Encode<C> + Decode<'b, C>,
K: Encode<C> + Decode<'b, C> + Clone,
V: Encode<C> + Decode<'b, C> + Clone,
{
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
let datatype = d.datatype()?;
Expand All @@ -92,8 +119,8 @@ where

impl<C, K, V> minicbor::encode::Encode<C> for KeyValuePairs<K, V>
where
K: Encode<C>,
V: Encode<C>,
K: Encode<C> + Clone,
V: Encode<C> + Clone,
{
fn encode<W: minicbor::encode::Write>(
&self,
Expand Down
23 changes: 11 additions & 12 deletions pallas-primitives/src/alonzo/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//!
//! Handcrafted, idiomatic rust artifacts based on based on the [Alonzo CDDL](https://github.com/input-output-hk/cardano-ledger/blob/master/eras/alonzo/test-suite/cddl-files/alonzo.cddl) file in IOHK repo.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use pallas_codec::minicbor::{data::Tag, Decode, Encode};
Expand Down Expand Up @@ -115,7 +113,7 @@ pub type PolicyId = Hash<28>;

pub type AssetName = Bytes;

pub type Multiasset<A> = BTreeMap<PolicyId, BTreeMap<AssetName, A>>;
pub type Multiasset<A> = KeyValuePairs<PolicyId, KeyValuePairs<AssetName, A>>;

pub type Mint = Multiasset<i64>;

Expand Down Expand Up @@ -232,7 +230,7 @@ impl<C> minicbor::encode::Encode<C> for InstantaneousRewardSource {

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub enum InstantaneousRewardTarget {
StakeCredentials(BTreeMap<StakeCredential, i64>),
StakeCredentials(KeyValuePairs<StakeCredential, i64>),
OtherAccountingPot(Coin),
}

Expand Down Expand Up @@ -284,7 +282,7 @@ pub struct MoveInstantaneousReward {

pub type RewardAccount = Bytes;

pub type Withdrawals = BTreeMap<RewardAccount, Coin>;
pub type Withdrawals = KeyValuePairs<RewardAccount, Coin>;

pub type RequiredSigners = Vec<AddrKeyhash>;

Expand Down Expand Up @@ -642,7 +640,7 @@ pub enum Language {

pub type CostModel = Vec<i64>;

pub type CostMdls = BTreeMap<Language, CostModel>;
pub type CostMdls = KeyValuePairs<Language, CostModel>;

#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
#[cbor(map)]
Expand Down Expand Up @@ -700,7 +698,7 @@ pub struct ProtocolParamUpdate {
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
pub struct Update {
#[n(0)]
pub proposed_protocol_parameter_updates: BTreeMap<Genesishash, ProtocolParamUpdate>,
pub proposed_protocol_parameter_updates: KeyValuePairs<Genesishash, ProtocolParamUpdate>,

#[n(1)]
pub epoch: Epoch,
Expand Down Expand Up @@ -916,7 +914,7 @@ impl<C> minicbor::encode::Encode<C> for BigInt {
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub enum PlutusData {
Constr(Constr<PlutusData>),
Map(BTreeMap<PlutusData, PlutusData>),
Map(KeyValuePairs<PlutusData, PlutusData>),
BigInt(BigInt),
BoundedBytes(Bytes),
Array(Vec<PlutusData>),
Expand Down Expand Up @@ -1235,7 +1233,7 @@ pub enum Metadatum {
Bytes(Bytes),
Text(String),
Array(Vec<Metadatum>),
Map(BTreeMap<Metadatum, Metadatum>),
Map(KeyValuePairs<Metadatum, Metadatum>),
}

impl<'b, C> minicbor::Decode<'b, C> for Metadatum {
Expand Down Expand Up @@ -1294,7 +1292,7 @@ impl<C> minicbor::Encode<C> for Metadatum {

pub type MetadatumLabel = u64;

pub type Metadata = BTreeMap<MetadatumLabel, Metadatum>;
pub type Metadata = KeyValuePairs<MetadatumLabel, Metadatum>;

#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Clone)]
pub struct ShelleyMaAuxiliaryData {
Expand Down Expand Up @@ -1368,7 +1366,7 @@ pub struct Block {
pub transaction_witness_sets: Vec<WitnessSet>,

#[n(3)]
pub auxiliary_data_set: BTreeMap<TransactionIndex, AuxiliaryData>,
pub auxiliary_data_set: KeyValuePairs<TransactionIndex, AuxiliaryData>,

#[n(4)]
pub invalid_transactions: Option<Vec<TransactionIndex>>,
Expand Down Expand Up @@ -1419,7 +1417,8 @@ impl<'b> From<MintedBlock<'b>> for Block {
.to_vec()
.into_iter()
.map(|(k, v)| (k, v.unwrap()))
.collect(),
.collect::<Vec<_>>()
.into(),
invalid_transactions: x.invalid_transactions.map(|x| x.into()),
}
}
Expand Down
13 changes: 6 additions & 7 deletions pallas-primitives/src/babbage/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//!
//! Handcrafted, idiomatic rust artifacts based on based on the [Babbage CDDL](https://github.com/input-output-hk/cardano-ledger/blob/master/eras/babbage/test-suite/cddl-files/babbage.cddl) file in IOHK repo.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use pallas_codec::minicbor::{Decode, Encode};
Expand Down Expand Up @@ -117,7 +115,7 @@ pub use crate::alonzo::MoveInstantaneousReward;

pub use crate::alonzo::RewardAccount;

pub type Withdrawals = BTreeMap<RewardAccount, Coin>;
pub type Withdrawals = KeyValuePairs<RewardAccount, Coin>;

pub type RequiredSigners = Vec<AddrKeyhash>;

Expand Down Expand Up @@ -226,7 +224,7 @@ pub struct ProtocolParamUpdate {
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
pub struct Update {
#[n(0)]
pub proposed_protocol_parameter_updates: BTreeMap<Genesishash, ProtocolParamUpdate>,
pub proposed_protocol_parameter_updates: KeyValuePairs<Genesishash, ProtocolParamUpdate>,

#[n(1)]
pub epoch: Epoch,
Expand All @@ -251,7 +249,7 @@ pub struct TransactionBody {
pub certificates: Option<Vec<Certificate>>,

#[n(5)]
pub withdrawals: Option<BTreeMap<RewardAccount, Coin>>,
pub withdrawals: Option<KeyValuePairs<RewardAccount, Coin>>,

#[n(6)]
pub update: Option<Update>,
Expand Down Expand Up @@ -565,7 +563,7 @@ pub struct Block {
pub transaction_witness_sets: Vec<WitnessSet>,

#[n(3)]
pub auxiliary_data_set: BTreeMap<TransactionIndex, AuxiliaryData>,
pub auxiliary_data_set: KeyValuePairs<TransactionIndex, AuxiliaryData>,

#[n(4)]
pub invalid_transactions: Option<Vec<TransactionIndex>>,
Expand Down Expand Up @@ -616,7 +614,8 @@ impl<'b> From<MintedBlock<'b>> for Block {
.to_vec()
.into_iter()
.map(|(k, v)| (k, v.unwrap()))
.collect(),
.collect::<Vec<_>>()
.into(),
invalid_transactions: x.invalid_transactions.map(|x| x.into()),
}
}
Expand Down

0 comments on commit 2f7d108

Please sign in to comment.