feat(primitives): Preserve order of map structures (#192)

This commit is contained in:
Santiago Carmuega 2022-09-14 15:00:53 -03:00 committed by GitHub
parent 4ebeb60119
commit 4dc373335f
3 changed files with 53 additions and 28 deletions

View file

@ -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 /// canonicalization for isomorphic decoding / encoding operators, we use a Vec
/// as the underlaying struct for storage of the items (as opposed to a BTreeMap /// as the underlaying struct for storage of the items (as opposed to a BTreeMap
/// or HashMap). /// or HashMap).
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum KeyValuePairs<K, V> { #[serde(from = "Vec::<(K, V)>", into = "Vec::<(K, V)>")]
pub enum KeyValuePairs<K, V>
where
K: Clone,
V: Clone,
{
Def(Vec<(K, V)>), Def(Vec<(K, V)>),
Indef(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)> { pub fn to_vec(self) -> Vec<(K, V)> {
self.into() 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 { fn from(other: KeyValuePairs<K, V>) -> Self {
match other { match other {
KeyValuePairs::Def(x) => x, KeyValuePairs::Def(x) => x,
@ -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)>; type Target = Vec<(K, V)>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
@ -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> impl<'b, C, K, V> minicbor::decode::Decode<'b, C> for KeyValuePairs<K, V>
where where
K: Encode<C> + Decode<'b, C>, K: Encode<C> + Decode<'b, C> + Clone,
V: Encode<C> + Decode<'b, C>, V: Encode<C> + Decode<'b, C> + Clone,
{ {
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> { fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
let datatype = d.datatype()?; let datatype = d.datatype()?;
@ -92,8 +119,8 @@ where
impl<C, K, V> minicbor::encode::Encode<C> for KeyValuePairs<K, V> impl<C, K, V> minicbor::encode::Encode<C> for KeyValuePairs<K, V>
where where
K: Encode<C>, K: Encode<C> + Clone,
V: Encode<C>, V: Encode<C> + Clone,
{ {
fn encode<W: minicbor::encode::Write>( fn encode<W: minicbor::encode::Write>(
&self, &self,

View file

@ -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. //! 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 serde::{Deserialize, Serialize};
use pallas_codec::minicbor::{data::Tag, Decode, Encode}; use pallas_codec::minicbor::{data::Tag, Decode, Encode};
@ -115,7 +113,7 @@ pub type PolicyId = Hash<28>;
pub type AssetName = Bytes; 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>; pub type Mint = Multiasset<i64>;
@ -232,7 +230,7 @@ impl<C> minicbor::encode::Encode<C> for InstantaneousRewardSource {
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Clone)] #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub enum InstantaneousRewardTarget { pub enum InstantaneousRewardTarget {
StakeCredentials(BTreeMap<StakeCredential, i64>), StakeCredentials(KeyValuePairs<StakeCredential, i64>),
OtherAccountingPot(Coin), OtherAccountingPot(Coin),
} }
@ -284,7 +282,7 @@ pub struct MoveInstantaneousReward {
pub type RewardAccount = Bytes; pub type RewardAccount = Bytes;
pub type Withdrawals = BTreeMap<RewardAccount, Coin>; pub type Withdrawals = KeyValuePairs<RewardAccount, Coin>;
pub type RequiredSigners = Vec<AddrKeyhash>; pub type RequiredSigners = Vec<AddrKeyhash>;
@ -642,7 +640,7 @@ pub enum Language {
pub type CostModel = Vec<i64>; 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)] #[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
#[cbor(map)] #[cbor(map)]
@ -700,7 +698,7 @@ pub struct ProtocolParamUpdate {
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)] #[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
pub struct Update { pub struct Update {
#[n(0)] #[n(0)]
pub proposed_protocol_parameter_updates: BTreeMap<Genesishash, ProtocolParamUpdate>, pub proposed_protocol_parameter_updates: KeyValuePairs<Genesishash, ProtocolParamUpdate>,
#[n(1)] #[n(1)]
pub epoch: Epoch, pub epoch: Epoch,
@ -916,7 +914,7 @@ impl<C> minicbor::encode::Encode<C> for BigInt {
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Clone)] #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub enum PlutusData { pub enum PlutusData {
Constr(Constr<PlutusData>), Constr(Constr<PlutusData>),
Map(BTreeMap<PlutusData, PlutusData>), Map(KeyValuePairs<PlutusData, PlutusData>),
BigInt(BigInt), BigInt(BigInt),
BoundedBytes(Bytes), BoundedBytes(Bytes),
Array(Vec<PlutusData>), Array(Vec<PlutusData>),
@ -1235,7 +1233,7 @@ pub enum Metadatum {
Bytes(Bytes), Bytes(Bytes),
Text(String), Text(String),
Array(Vec<Metadatum>), Array(Vec<Metadatum>),
Map(BTreeMap<Metadatum, Metadatum>), Map(KeyValuePairs<Metadatum, Metadatum>),
} }
impl<'b, C> minicbor::Decode<'b, C> for Metadatum { impl<'b, C> minicbor::Decode<'b, C> for Metadatum {
@ -1294,7 +1292,7 @@ impl<C> minicbor::Encode<C> for Metadatum {
pub type MetadatumLabel = u64; pub type MetadatumLabel = u64;
pub type Metadata = BTreeMap<MetadatumLabel, Metadatum>; pub type Metadata = KeyValuePairs<MetadatumLabel, Metadatum>;
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Clone)] #[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Clone)]
pub struct ShelleyMaAuxiliaryData { pub struct ShelleyMaAuxiliaryData {
@ -1368,7 +1366,7 @@ pub struct Block {
pub transaction_witness_sets: Vec<WitnessSet>, pub transaction_witness_sets: Vec<WitnessSet>,
#[n(3)] #[n(3)]
pub auxiliary_data_set: BTreeMap<TransactionIndex, AuxiliaryData>, pub auxiliary_data_set: KeyValuePairs<TransactionIndex, AuxiliaryData>,
#[n(4)] #[n(4)]
pub invalid_transactions: Option<Vec<TransactionIndex>>, pub invalid_transactions: Option<Vec<TransactionIndex>>,
@ -1419,7 +1417,8 @@ impl<'b> From<MintedBlock<'b>> for Block {
.to_vec() .to_vec()
.into_iter() .into_iter()
.map(|(k, v)| (k, v.unwrap())) .map(|(k, v)| (k, v.unwrap()))
.collect(), .collect::<Vec<_>>()
.into(),
invalid_transactions: x.invalid_transactions.map(|x| x.into()), invalid_transactions: x.invalid_transactions.map(|x| x.into()),
} }
} }

View file

@ -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. //! 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 serde::{Deserialize, Serialize};
use pallas_codec::minicbor::{Decode, Encode}; use pallas_codec::minicbor::{Decode, Encode};
@ -117,7 +115,7 @@ pub use crate::alonzo::MoveInstantaneousReward;
pub use crate::alonzo::RewardAccount; pub use crate::alonzo::RewardAccount;
pub type Withdrawals = BTreeMap<RewardAccount, Coin>; pub type Withdrawals = KeyValuePairs<RewardAccount, Coin>;
pub type RequiredSigners = Vec<AddrKeyhash>; pub type RequiredSigners = Vec<AddrKeyhash>;
@ -226,7 +224,7 @@ pub struct ProtocolParamUpdate {
#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)] #[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
pub struct Update { pub struct Update {
#[n(0)] #[n(0)]
pub proposed_protocol_parameter_updates: BTreeMap<Genesishash, ProtocolParamUpdate>, pub proposed_protocol_parameter_updates: KeyValuePairs<Genesishash, ProtocolParamUpdate>,
#[n(1)] #[n(1)]
pub epoch: Epoch, pub epoch: Epoch,
@ -251,7 +249,7 @@ pub struct TransactionBody {
pub certificates: Option<Vec<Certificate>>, pub certificates: Option<Vec<Certificate>>,
#[n(5)] #[n(5)]
pub withdrawals: Option<BTreeMap<RewardAccount, Coin>>, pub withdrawals: Option<KeyValuePairs<RewardAccount, Coin>>,
#[n(6)] #[n(6)]
pub update: Option<Update>, pub update: Option<Update>,
@ -565,7 +563,7 @@ pub struct Block {
pub transaction_witness_sets: Vec<WitnessSet>, pub transaction_witness_sets: Vec<WitnessSet>,
#[n(3)] #[n(3)]
pub auxiliary_data_set: BTreeMap<TransactionIndex, AuxiliaryData>, pub auxiliary_data_set: KeyValuePairs<TransactionIndex, AuxiliaryData>,
#[n(4)] #[n(4)]
pub invalid_transactions: Option<Vec<TransactionIndex>>, pub invalid_transactions: Option<Vec<TransactionIndex>>,
@ -616,7 +614,8 @@ impl<'b> From<MintedBlock<'b>> for Block {
.to_vec() .to_vec()
.into_iter() .into_iter()
.map(|(k, v)| (k, v.unwrap())) .map(|(k, v)| (k, v.unwrap()))
.collect(), .collect::<Vec<_>>()
.into(),
invalid_transactions: x.invalid_transactions.map(|x| x.into()), invalid_transactions: x.invalid_transactions.map(|x| x.into()),
} }
} }