Skip to content

Commit

Permalink
empty MultiAsset eq fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lisicky committed Dec 30, 2024
1 parent a2a19db commit e8f750f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
10 changes: 10 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,16 @@ impl MultiAsset {
}
lhs_ma
}

pub(crate) fn reduce_empty_to_none(&self) -> Option<&MultiAsset> {
for (_policy, assets) in self.0.iter() {
if assets.len() > 0 {
return Some(self);
}
}

None
}
}

// deriving PartialOrd doesn't work in a way that's useful , as the
Expand Down
39 changes: 39 additions & 0 deletions rust/src/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,3 +772,42 @@ fn has_transaction_set_tag_mixed() {
let tx_sets = has_transaction_set_tag(hex::decode(hex).unwrap()).unwrap();
assert_eq!(tx_sets, TransactionSetsState::MixedSets);
}


#[test]
fn value_empty_asset_equal() {
let a = Value {
coin: BigNum(0),
multiasset: None,
};
let b = Value {
coin: BigNum(0),
multiasset: Some(MultiAsset::new()),
};
let c = Value {
coin: BigNum(0),
multiasset: None,
};

assert_eq!(a, b);
assert_eq!(a, c);
}

#[test]
fn value_empty_asset_not_equal() {
let a = Value {
coin: BigNum(1),
multiasset: None,
};
let b = Value {
coin: BigNum(2),
multiasset: Some(MultiAsset::new()),
};
let c = Value {
coin: BigNum(3),
multiasset: None,
};

assert_ne!(a, b);
assert_ne!(a, c);
}
28 changes: 17 additions & 11 deletions rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ impl<'a> IntoIterator for &'a TransactionUnspentOutputs {
#[derive(
Clone,
Debug,
Eq,
/*Hash,*/ Ord,
PartialEq,
serde::Serialize,
serde::Deserialize,
JsonSchema,
Expand Down Expand Up @@ -298,6 +296,16 @@ impl Value {
}
}

impl PartialEq for Value {
fn eq(&self, other: &Self) -> bool {
let self_ma = self.multiasset.as_ref().map(|ma| ma.reduce_empty_to_none()).flatten();
let other_ma = other.multiasset.as_ref().map(|ma| ma.reduce_empty_to_none()).flatten();
self.coin == other.coin && self_ma == other_ma
}
}

impl Eq for Value {}

impl PartialOrd for Value {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
use std::cmp::Ordering::*;
Expand Down Expand Up @@ -334,19 +342,17 @@ impl cbor_event::se::Serialize for Value {
&self,
serializer: &'se mut Serializer<W>,
) -> cbor_event::Result<&'se mut Serializer<W>> {
let multiasset_len = match &self.multiasset {
Some(multiasset) => multiasset.len(),
None => 0,
};
let multiasset = self.multiasset
.as_ref()
.map(|ma| ma.reduce_empty_to_none())
.flatten();

if multiasset_len == 0 {
if let Some(multiasset) = multiasset {
serializer.write_array(cbor_event::Len::Len(2))?;
self.coin.serialize(serializer)?;
multiasset.serialize(serializer)?;
} else {
serializer.write_array(cbor_event::Len::Len(2))?;
self.coin.serialize(serializer)?;
if let Some(multiasset) = &self.multiasset {
multiasset.serialize(serializer)?;
}
}

Ok(serializer)
Expand Down

0 comments on commit e8f750f

Please sign in to comment.