Skip to content

Commit 71b54a9

Browse files
committed
Cache CONSTR_ID
1 parent 167d473 commit 71b54a9

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

pycardano/plutus.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
from __future__ import annotations
44

5+
import functools
56
import inspect
67
import json
78
from dataclasses import dataclass, field, fields
89
from enum import Enum
10+
from functools import lru_cache
911
from typing import Any, Optional, Type, Union
1012

1113
import cbor2
@@ -476,11 +478,14 @@ def CONSTR_ID(cls):
476478
The default implementation is an almost unique, deterministic constructor ID in the range 1 - 2^32 based
477479
on class attributes, types and class name.
478480
"""
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
481+
if not hasattr(cls, "_CONSTR_ID"):
482+
det_string = (
483+
cls.__name__ + "*" + "*".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
484489

485490
def __post_init__(self):
486491
valid_types = (PlutusData, dict, IndefiniteList, int, bytes)

0 commit comments

Comments
 (0)