Skip to content

Commit 787e160

Browse files
committed
Add a flag to enable/disable auto CONSTR_ID
1 parent c4e06d6 commit 787e160

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

pycardano/plutus.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from dataclasses import dataclass, field, fields
88
from enum import Enum
99
from hashlib import sha256
10-
from typing import Any, Optional, Type, Union
10+
from typing import Any, ClassVar, Optional, Type, Union
1111

1212
import cbor2
1313
from cbor2 import CBORTag
@@ -468,6 +468,9 @@ class will reduce the complexity of serialization and deserialization tremendous
468468
>>> assert test == Test.from_cbor("d87a9f187b43333231ff")
469469
"""
470470

471+
AUTO_ID: ClassVar[bool] = False
472+
"""Enables automatic assignment of a deterministic constructor id (CONSTR_ID) for the Plutus data if set to True."""
473+
471474
@classproperty
472475
def CONSTR_ID(cls):
473476
"""
@@ -476,6 +479,9 @@ def CONSTR_ID(cls):
476479
The default implementation is an almost unique, deterministic constructor ID in the range 1 - 2^32 based
477480
on class attributes, types and class name.
478481
"""
482+
if not cls.AUTO_ID:
483+
return 0
484+
479485
k = f"_CONSTR_ID_{cls.__name__}"
480486
if not hasattr(cls, k):
481487
det_string = (

test/pycardano/test_plutus.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def test_plutus_data_from_json_wrong_data_structure_type():
207207
def test_plutus_data_hash():
208208
assert (
209209
bytes.fromhex(
210-
"19d31e4f3aa9b03ad93b64c8dd2cc822d247c21e2c22762b7b08e6cadfeddb47"
210+
"923918e403bf43c34b4ef6b48eb2ee04babed17320d8d1b9ff9ad086e86f44ec"
211211
)
212212
== PlutusData().hash().payload
213213
)
@@ -324,10 +324,12 @@ def test_clone_plutus_data():
324324
def test_unique_constr_ids():
325325
@dataclass
326326
class A(PlutusData):
327+
AUTO_ID = True
327328
pass
328329

329330
@dataclass
330331
class B(PlutusData):
332+
AUTO_ID = True
331333
pass
332334

333335
assert (
@@ -337,6 +339,7 @@ class B(PlutusData):
337339

338340
@dataclass
339341
class B(PlutusData):
342+
AUTO_ID = True
340343
a: int
341344
b: bytes
342345

@@ -348,6 +351,7 @@ class B(PlutusData):
348351

349352
@dataclass
350353
class B(PlutusData):
354+
AUTO_ID = True
351355
a: bytes
352356
b: bytes
353357

@@ -359,13 +363,15 @@ class B(PlutusData):
359363
def test_deterministic_constr_ids_local():
360364
@dataclass
361365
class A(PlutusData):
366+
AUTO_ID = True
362367
a: int
363368
b: bytes
364369

365370
A_tmp = A
366371

367372
@dataclass
368373
class A(PlutusData):
374+
AUTO_ID = True
369375
a: int
370376
b: bytes
371377

@@ -381,6 +387,7 @@ def test_deterministic_constr_ids_global():
381387
382388
@dataclass
383389
class A(PlutusData):
390+
AUTO_ID = True
384391
a: int
385392
b: bytes
386393

0 commit comments

Comments
 (0)