Skip to content

Commit baf2b57

Browse files
committed
Test list and map type in datum serialization
1 parent b19dd6c commit baf2b57

File tree

6 files changed

+29
-10
lines changed

6 files changed

+29
-10
lines changed

pycardano/plutus.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class PlutusData(ArrayCBORSerializable):
270270
It is primarily used by Plutus core to reconstruct a data structure from serialized CBOR bytes."""
271271

272272
def __post_init__(self):
273-
valid_types = (PlutusData, dict, list, int, bytes)
273+
valid_types = (PlutusData, dict, IndefiniteList, int, bytes)
274274
for f in fields(self):
275275
if inspect.isclass(f.type) and not issubclass(f.type, valid_types):
276276
raise TypeError(

pycardano/serialization.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ class IndefiniteList:
3737
def __init__(self, items):
3838
self.items = items
3939

40+
def __eq__(self, other):
41+
if isinstance(other, IndefiniteList):
42+
return self.items == other.items
43+
else:
44+
return False
45+
4046

4147
Primitive = TypeVar(
4248
"Primitive",
@@ -318,6 +324,8 @@ def _restore_dataclass_field(
318324
return f.metadata["object_hook"](v)
319325
elif isclass(f.type) and issubclass(f.type, CBORSerializable):
320326
return f.type.from_primitive(v)
327+
elif isclass(f.type) and issubclass(f.type, IndefiniteList):
328+
return IndefiniteList(v)
321329
elif hasattr(f.type, "__origin__") and f.type.__origin__ is Union:
322330
t_args = f.type.__args__
323331
for t in t_args:

test/pycardano/test_plutus.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dataclasses import dataclass
22
from test.pycardano.util import check_two_way_cbor
3-
from typing import Union
3+
from typing import List, Union
44

55
from pycardano.plutus import (
66
COST_MODELS,
@@ -9,6 +9,7 @@
99
Redeemer,
1010
RedeemerTag,
1111
)
12+
from pycardano.serialization import IndefiniteList
1213

1314

1415
@dataclass
@@ -17,6 +18,8 @@ class MyTest(PlutusData):
1718

1819
a: int
1920
b: bytes
21+
c: IndefiniteList
22+
d: dict
2023

2124

2225
@dataclass
@@ -50,15 +53,15 @@ def test_plutus_data():
5053
"""Ground truth of this test is generated by test/resources/haskell/PlutusData. See its README for more details."""
5154
key_hash = bytes.fromhex("c2ff616e11299d9094ce0a7eb5b7284b705147a822f4ffbd471f971a")
5255
deadline = 1643235300000
53-
testa = BigTest(MyTest(123, b"1234"))
56+
testa = BigTest(MyTest(123, b"1234", IndefiniteList([4, 5, 6]), {1: b"1", 2: b"2"}))
5457
testb = LargestTest()
5558

5659
my_vesting = VestingParam(
5760
beneficiary=key_hash, deadline=deadline, testa=testa, testb=testb
5861
)
5962
assert (
6063
"d87a9f581cc2ff616e11299d9094ce0a7eb5b7284b705147a822f4ffbd471f971a1b0000017e9"
61-
"874d2a0d905019fd8668218829f187b4431323334ffffd9050280ff"
64+
"874d2a0d905019fd8668218829f187b44313233349f040506ffa2014131024132ffffd9050280ff"
6265
== my_vesting.to_cbor()
6366
)
6467
check_two_way_cbor(my_vesting)
@@ -74,10 +77,11 @@ def test_plutus_data_hash():
7477

7578

7679
def test_redeemer():
77-
data = MyTest(123, b"234")
80+
data = MyTest(123, b"234", IndefiniteList([4, 5, 6]), {1: b"1", 2: b"2"})
7881
redeemer = MyRedeemer(RedeemerTag.SPEND, 0, data, ExecutionUnits(1000000, 1000000))
7982
assert (
80-
"840000d8668218829f187b43323334ff821a000f42401a000f4240" == redeemer.to_cbor()
83+
"840000d8668218829f187b433233349f040506ffa2014131024132ff821a000f42401a000f4240"
84+
== redeemer.to_cbor()
8185
)
8286
check_two_way_cbor(redeemer)
8387

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"d87a9f581cc2ff616e11299d9094ce0a7eb5b7284b705147a822f4ffbd471f971a1b0000017e9874d2a0d905019fd8668218829f187b4431323334ffffd9050280ff"
1+
"d87a9f581cc2ff616e11299d9094ce0a7eb5b7284b705147a822f4ffbd471f971a1b0000017e9874d2a0d905019fd8668218829f187b44313233349f040506ffa2014131024132ffffd9050280ff"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"constructor":1,"fields":[{"bytes":"c2ff616e11299d9094ce0a7eb5b7284b705147a822f4ffbd471f971a"},{"int":1643235300000},{"constructor":8,"fields":[{"constructor":130,"fields":[{"int":123},{"bytes":"31323334"}]}]},{"constructor":9,"fields":[]}]}
1+
{"constructor":1,"fields":[{"bytes":"c2ff616e11299d9094ce0a7eb5b7284b705147a822f4ffbd471f971a"},{"int":1643235300000},{"constructor":8,"fields":[{"constructor":130,"fields":[{"int":123},{"bytes":"31323334"},{"list":[{"int":4},{"int":5},{"int":6}]},{"map":[{"v":{"bytes":"31"},"k":{"int":1}},{"v":{"bytes":"32"},"k":{"int":2}}]}]}]},{"constructor":9,"fields":[]}]}

test/resources/haskell/PlutusData/src/PlutusData.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import qualified Data.ByteString.Lazy as LBS
1313
import qualified Data.ByteString.Lazy.Char8 as C
1414
import PlutusTx (Data (..))
1515
import qualified PlutusTx
16+
import qualified PlutusTx.AssocMap as AssocMap
1617
import PlutusTx.Prelude ( BuiltinByteString, )
1718
import Ledger ( PaymentPubKeyHash(PaymentPubKeyHash), POSIXTime )
1819

@@ -42,8 +43,11 @@ writeJSON file = LBS.writeFile file . toJSONByteString
4243
data Test = Test
4344
{
4445
a :: !Integer,
45-
b :: !BuiltinByteString
46-
} deriving (Show)
46+
b :: !BuiltinByteString,
47+
c :: !([Integer]),
48+
d :: !(AssocMap.Map Integer BuiltinByteString)
49+
}
50+
deriving (Show)
4751

4852
PlutusTx.makeLift ''Test
4953
PlutusTx.makeIsDataIndexed ''Test [('Test, 130)]
@@ -65,10 +69,13 @@ PlutusTx.makeLift ''VestingParam
6569

6670
PlutusTx.makeIsDataIndexed ''VestingParam [('VestingParam, 1)]
6771

72+
6873
test :: Test
6974
test = Test
7075
{ a = 123
7176
, b = "1234"
77+
, c = [4, 5, 6]
78+
, d = AssocMap.fromList [(1, "1"), (2, "2")]
7279
}
7380

7481
param :: VestingParam

0 commit comments

Comments
 (0)