Skip to content

Commit ed8c448

Browse files
committed
fix plutus map serialization for multiple keys
1 parent ad4b118 commit ed8c448

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

rust/src/protocol_types/plutus/plutus_data.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ impl PlutusMap {
150150
.or_insert_with(PlutusMapValues::new);
151151
values.add_move(value);
152152
}
153+
154+
pub(crate) fn total_len(&self) -> usize {
155+
self.0.iter().map(|(_k, v)| v.len()).sum()
156+
}
153157
}
154158

155159
#[wasm_bindgen]

rust/src/serialization/plutus/plutus_data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl cbor_event::se::Serialize for PlutusMap {
6969
&self,
7070
serializer: &'se mut Serializer<W>,
7171
) -> cbor_event::Result<&'se mut Serializer<W>> {
72-
serializer.write_map(cbor_event::Len::Len(self.0.len() as u64))?;
72+
serializer.write_map(cbor_event::Len::Len(self.total_len() as u64))?;
7373
for (key, values) in &self.0 {
7474
for value in &values.elems {
7575
key.serialize(serializer)?;

rust/src/tests/general.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,4 +818,37 @@ fn test_multiple_tiers() {
818818
let result = min_ref_script_fee(*size, &new_uinternal(1, 1000)).unwrap();
819819
assert_eq!(result.to_str(), *expected, "Failed for size {}", size);
820820
}
821+
}
822+
823+
#[test]
824+
fn plutus_map_keys_duplication_test() {
825+
let mut map = PlutusMap::new();
826+
let key1 = PlutusData::new_integer(&BigInt::from(1));
827+
let key2 = PlutusData::new_integer(&BigInt::from(2));
828+
let value1 = PlutusData::new_integer(&BigInt::from(3));
829+
let value2 = PlutusData::new_integer(&BigInt::from(4));
830+
let value3 = PlutusData::new_integer(&BigInt::from(5));
831+
832+
assert_eq!(map.len(), 0);
833+
assert_eq!(map.total_len(), 0);
834+
835+
let mut plutus_map_value1 = PlutusMapValues::new();
836+
plutus_map_value1.add(&value1);
837+
plutus_map_value1.add(&value2);
838+
839+
let mut plutus_map_value2 = PlutusMapValues::new();
840+
plutus_map_value2.add(&value3);
841+
842+
map.insert(&key1, &plutus_map_value1);
843+
map.insert(&key2, &plutus_map_value2);
844+
845+
assert_eq!(map.len(), 2);
846+
assert_eq!(map.total_len(), 3);
847+
848+
let map_bytes = map.to_bytes();
849+
let map_from_bytes = PlutusMap::from_bytes(map_bytes).unwrap();
850+
assert_eq!(map_from_bytes.len(), 2);
851+
assert_eq!(map_from_bytes.total_len(), 3);
852+
853+
assert_eq!(map, map_from_bytes)
821854
}

0 commit comments

Comments
 (0)