Skip to content

Commit 8e4ab25

Browse files
ADD. adding ogmios parsing integration test
UPDATE. improve NativeScript.from_dict() by pulling out primitive list building step
1 parent 713cad9 commit 8e4ab25

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

integration-test/test/test_ogmios.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from retry import retry
2-
import pytest
32

43
from .base import TEST_RETRIES, TestBase
54

65

7-
@pytest.mark.single
86
class TestProtocolParam(TestBase):
97
@retry(tries=TEST_RETRIES, backoff=1.5, delay=6, jitter=(0, 4))
108
def test_protocol_param_cost_models(self):

pycardano/backend/blockfrost.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
UTxO,
3030
Value,
3131
)
32+
from pycardano.types import JSON
3233

3334
__all__ = ["BlockFrostChainContext"]
3435

@@ -147,9 +148,10 @@ def _get_script(
147148
cbor2.loads(bytes.fromhex(self.api.script_cbor(script_hash).cbor))
148149
)
149150
else:
150-
return NativeScript.from_dict(
151-
self.api.script_json(script_hash, return_type="json")["json"]
152-
)
151+
script_json: JSON = self.api.script_json(script_hash, return_type="json")[
152+
"json"
153+
]
154+
return NativeScript.from_dict(script_json)
153155

154156
def utxos(self, address: str) -> List[UTxO]:
155157
results = self.api.address_utxos(address, gather_pages=True)

pycardano/nativescript.py

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pycardano.exception import DeserializeException
1212
from pycardano.hash import SCRIPT_HASH_SIZE, ScriptHash, VerificationKeyHash
1313
from pycardano.serialization import ArrayCBORSerializable, Primitive, list_hook
14+
from pycardano.types import JSON
1415

1516
__all__ = [
1617
"NativeScript",
@@ -25,6 +26,9 @@
2526

2627
@dataclass
2728
class NativeScript(ArrayCBORSerializable):
29+
json_tag: ClassVar[str]
30+
json_field: ClassVar[str]
31+
2832
@classmethod
2933
def from_primitive(
3034
cls: Type[NativeScript], value: Primitive
@@ -54,7 +58,7 @@ def hash(self) -> ScriptHash:
5458

5559
@classmethod
5660
def from_dict(
57-
cls: NativeScript, script: dict, top_level: bool = True
61+
cls: Type[NativeScript], script_json: JSON
5862
) -> Union[
5963
ScriptPubkey, ScriptAll, ScriptAny, ScriptNofK, InvalidBefore, InvalidHereAfter
6064
]:
@@ -71,27 +75,48 @@ def from_dict(
7175
InvalidHereAfter,
7276
]
7377
}
78+
script_type = script_json["type"]
79+
target_class = types[script_type]
80+
script_primitive = cls._script_dict_to_primitive(script_json)
81+
return super(NativeScript, target_class).from_primitive(script_primitive[1:])
7482

75-
native_script = []
76-
if isinstance(script, dict):
83+
@classmethod
84+
def _script_dict_to_primitive(
85+
cls: Type[NativeScript], script_json: JSON
86+
) -> List[Primitive]:
87+
"""Serialize a standard JSON native script into a primitive array"""
7788

78-
for key, value in script.items():
79-
if key == "type":
80-
native_script.insert(0, list(types.keys()).index(value))
81-
elif key == "scripts":
82-
native_script.append(cls.from_dict(value, top_level=False))
83-
else:
84-
native_script.append(value)
89+
types = {
90+
p.json_tag: p
91+
for p in [
92+
ScriptPubkey,
93+
ScriptAll,
94+
ScriptAny,
95+
ScriptNofK,
96+
InvalidBefore,
97+
InvalidHereAfter,
98+
]
99+
}
85100

86-
elif isinstance(script, list): # list
87-
native_script = [cls.from_dict(i, top_level=False) for i in script]
101+
script_type: str = script_json["type"]
102+
native_script = [types[script_type]._TYPE]
88103

89-
if not top_level:
90-
return native_script
91-
else:
92-
return super(NativeScript, types[script["type"]]).from_primitive(
93-
native_script[1:]
94-
)
104+
for key, value in script_json.items():
105+
if key == "type":
106+
continue
107+
elif key == "scripts":
108+
native_script.append(cls._script_list_to_primitive(value))
109+
else:
110+
native_script.append(value)
111+
return native_script
112+
113+
@classmethod
114+
def _script_list_to_primitive(
115+
cls: Type[NativeScript], script_jsons: List[JSON]
116+
) -> List[List[Primitive]]:
117+
"""Parse a list of JSON scripts into a list of primitive arrays"""
118+
native_script = [cls._script_dict_to_primitive(i) for i in script_jsons]
119+
return native_script
95120

96121
def to_dict(self) -> dict:
97122
"""Export to standard native script dictionary (potentially to dump to a JSON file)."""

0 commit comments

Comments
 (0)