fix(primitives): Force CBOR null primitive for missing aux data (#159)
This commit is contained in:
parent
97d4fad2b7
commit
8f38834a1d
5 changed files with 82 additions and 9 deletions
|
|
@ -574,3 +574,67 @@ impl<C, T> minicbor::Encode<C> for KeepRaw<'_, T> {
|
||||||
.map_err(minicbor::encode::Error::write)
|
.map_err(minicbor::encode::Error::write)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum Nullable<T> {
|
||||||
|
Some(T),
|
||||||
|
Null,
|
||||||
|
Undefined,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b, C, T> minicbor::Decode<'b, C> for Nullable<T>
|
||||||
|
where
|
||||||
|
T: minicbor::Decode<'b, C>,
|
||||||
|
{
|
||||||
|
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
|
||||||
|
match d.datatype()? {
|
||||||
|
minicbor::data::Type::Null => {
|
||||||
|
d.null()?;
|
||||||
|
Ok(Self::Null)
|
||||||
|
}
|
||||||
|
minicbor::data::Type::Undefined => {
|
||||||
|
d.undefined()?;
|
||||||
|
Ok(Self::Undefined)
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let x = d.decode_with(ctx)?;
|
||||||
|
Ok(Self::Some(x))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<C, T> minicbor::Encode<C> for Nullable<T>
|
||||||
|
where
|
||||||
|
T: minicbor::Encode<C>,
|
||||||
|
{
|
||||||
|
fn encode<W: minicbor::encode::Write>(
|
||||||
|
&self,
|
||||||
|
e: &mut minicbor::Encoder<W>,
|
||||||
|
ctx: &mut C,
|
||||||
|
) -> Result<(), minicbor::encode::Error<W::Error>> {
|
||||||
|
match self {
|
||||||
|
Nullable::Some(x) => {
|
||||||
|
e.encode_with(x, ctx)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Nullable::Null => {
|
||||||
|
e.null()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Nullable::Undefined => {
|
||||||
|
e.undefined()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> From<Option<T>> for Nullable<T> {
|
||||||
|
fn from(x: Option<T>) -> Self {
|
||||||
|
match x {
|
||||||
|
Some(x) => Nullable::Some(x),
|
||||||
|
None => Nullable::Null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
use pallas_codec::minicbor::{bytes::ByteVec, data::Int, data::Tag, Decode, Encode};
|
use pallas_codec::minicbor::{bytes::ByteVec, data::Int, data::Tag, Decode, Encode};
|
||||||
use pallas_crypto::hash::Hash;
|
use pallas_crypto::hash::Hash;
|
||||||
|
|
||||||
use pallas_codec::utils::{AnyUInt, KeepRaw, KeyValuePairs, MaybeIndefArray};
|
use pallas_codec::utils::{AnyUInt, KeepRaw, KeyValuePairs, MaybeIndefArray, Nullable};
|
||||||
|
|
||||||
// required for derive attrs to work
|
// required for derive attrs to work
|
||||||
use pallas_codec::minicbor;
|
use pallas_codec::minicbor;
|
||||||
|
|
@ -1373,7 +1373,7 @@ pub struct Tx {
|
||||||
pub success: bool,
|
pub success: bool,
|
||||||
|
|
||||||
#[n(3)]
|
#[n(3)]
|
||||||
pub auxiliary_data: Option<AuxiliaryData>,
|
pub auxiliary_data: Nullable<AuxiliaryData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Encode, Decode, Debug, Clone)]
|
#[derive(Encode, Decode, Debug, Clone)]
|
||||||
|
|
@ -1388,7 +1388,7 @@ pub struct MintedTx<'b> {
|
||||||
pub success: bool,
|
pub success: bool,
|
||||||
|
|
||||||
#[n(3)]
|
#[n(3)]
|
||||||
pub auxiliary_data: Option<KeepRaw<'b, AuxiliaryData>>,
|
pub auxiliary_data: Nullable<KeepRaw<'b, AuxiliaryData>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
use pallas_codec::minicbor::{bytes::ByteVec, Decode, Encode};
|
use pallas_codec::minicbor::{bytes::ByteVec, Decode, Encode};
|
||||||
use pallas_crypto::hash::Hash;
|
use pallas_crypto::hash::Hash;
|
||||||
|
|
||||||
use pallas_codec::utils::{CborWrap, KeepRaw, KeyValuePairs, MaybeIndefArray};
|
use pallas_codec::utils::{CborWrap, KeepRaw, KeyValuePairs, MaybeIndefArray, Nullable};
|
||||||
|
|
||||||
// required for derive attrs to work
|
// required for derive attrs to work
|
||||||
use pallas_codec::minicbor;
|
use pallas_codec::minicbor;
|
||||||
|
|
@ -561,7 +561,7 @@ pub struct Tx {
|
||||||
pub success: bool,
|
pub success: bool,
|
||||||
|
|
||||||
#[n(3)]
|
#[n(3)]
|
||||||
pub auxiliary_data: Option<AuxiliaryData>,
|
pub auxiliary_data: Nullable<AuxiliaryData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Encode, Decode, Debug, Clone)]
|
#[derive(Encode, Decode, Debug, Clone)]
|
||||||
|
|
@ -576,7 +576,7 @@ pub struct MintedTx<'b> {
|
||||||
pub success: bool,
|
pub success: bool,
|
||||||
|
|
||||||
#[n(3)]
|
#[n(3)]
|
||||||
pub auxiliary_data: Option<KeepRaw<'b, AuxiliaryData>>,
|
pub auxiliary_data: Nullable<KeepRaw<'b, AuxiliaryData>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,8 @@ macro_rules! clone_tx_fn {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.cloned();
|
.cloned()
|
||||||
|
.into();
|
||||||
|
|
||||||
let x = $era::MintedTx {
|
let x = $era::MintedTx {
|
||||||
transaction_body,
|
transaction_body,
|
||||||
|
|
|
||||||
|
|
@ -219,8 +219,16 @@ impl<'b> MultiEraTx<'b> {
|
||||||
|
|
||||||
fn aux_data(&self) -> Option<&KeepRaw<'_, AuxiliaryData>> {
|
fn aux_data(&self) -> Option<&KeepRaw<'_, AuxiliaryData>> {
|
||||||
match self {
|
match self {
|
||||||
MultiEraTx::AlonzoCompatible(x, _) => x.auxiliary_data.as_ref(),
|
MultiEraTx::AlonzoCompatible(x, _) => match &x.auxiliary_data {
|
||||||
MultiEraTx::Babbage(x) => x.auxiliary_data.as_ref(),
|
pallas_codec::utils::Nullable::Some(x) => Some(x),
|
||||||
|
pallas_codec::utils::Nullable::Null => None,
|
||||||
|
pallas_codec::utils::Nullable::Undefined => None,
|
||||||
|
},
|
||||||
|
MultiEraTx::Babbage(x) => match &x.auxiliary_data {
|
||||||
|
pallas_codec::utils::Nullable::Some(x) => Some(x),
|
||||||
|
pallas_codec::utils::Nullable::Null => None,
|
||||||
|
pallas_codec::utils::Nullable::Undefined => None,
|
||||||
|
},
|
||||||
MultiEraTx::Byron(_) => None,
|
MultiEraTx::Byron(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue