|
6 | 6 | import json
|
7 | 7 | from dataclasses import dataclass, field, fields
|
8 | 8 | from enum import Enum
|
| 9 | +from hashlib import sha256 |
9 | 10 | from typing import Any, Optional, Type, Union
|
10 | 11 |
|
11 | 12 | import cbor2
|
12 | 13 | from cbor2 import CBORTag
|
13 | 14 | from nacl.encoding import RawEncoder
|
14 | 15 | from nacl.hash import blake2b
|
15 |
| -from hashlib import sha256 |
16 | 16 |
|
17 | 17 | from pycardano.exception import DeserializeException
|
18 | 18 | from pycardano.hash import DATUM_HASH_SIZE, SCRIPT_HASH_SIZE, DatumHash, ScriptHash
|
@@ -476,11 +476,16 @@ def CONSTR_ID(cls):
|
476 | 476 | The default implementation is an almost unique, deterministic constructor ID in the range 1 - 2^32 based
|
477 | 477 | on class attributes, types and class name.
|
478 | 478 | """
|
479 |
| - det_string = ( |
480 |
| - cls.__name__ + "*" + "*".join([f"{f.name}~{f.type}" for f in fields(cls)]) |
481 |
| - ) |
482 |
| - det_hash = sha256(det_string.encode("utf8")).hexdigest() |
483 |
| - return int(det_hash, 16) % 2**32 |
| 479 | + if not hasattr(cls, "_CONSTR_ID"): |
| 480 | + det_string = ( |
| 481 | + cls.__name__ |
| 482 | + + "*" |
| 483 | + + "*".join([f"{f.name}~{f.type}" for f in fields(cls)]) |
| 484 | + ) |
| 485 | + det_hash = sha256(det_string.encode("utf8")).hexdigest() |
| 486 | + setattr(cls, "_CONSTR_ID", int(det_hash, 16) % 2**32) |
| 487 | + |
| 488 | + return cls._CONSTR_ID |
484 | 489 |
|
485 | 490 | def __post_init__(self):
|
486 | 491 | valid_types = (PlutusData, dict, IndefiniteList, int, bytes)
|
|
0 commit comments