Skip to content

Commit f5b7167

Browse files
committed
refactor: Add state root cache
1 parent 15f8bb0 commit f5b7167

File tree

1 file changed

+19
-9
lines changed
  • packages/testing/src/execution_testing/client_clis

1 file changed

+19
-9
lines changed

packages/testing/src/execution_testing/client_clis/cli_types.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,17 @@ class Result(CamelModel):
287287
opcode_count: OpcodeCount | None = None
288288

289289

290-
JSONDict = Dict[str, Any]
291-
292-
TRaw = TypeVar("TRaw", str, JSONDict)
290+
TRaw = TypeVar("TRaw")
293291

294292

295293
@dataclass(kw_only=True)
296294
class LazyAlloc(Generic[TRaw]):
297295
"""
298-
Allocation that is lazily loaded from a JSON file.
296+
Allocation that is lazily loaded from the transition tool response.
299297
"""
300298

301299
raw: TRaw
300+
_state_root: Hash
302301
alloc: Alloc | None = None
303302

304303
def validate(self) -> Alloc:
@@ -311,6 +310,13 @@ def get(self) -> Alloc:
311310
self.alloc = self.validate()
312311
return self.alloc
313312

313+
def state_root(self) -> Hash:
314+
"""Return state root of the allocation."""
315+
return self._state_root
316+
317+
318+
JSONDict = Dict[str, Any]
319+
314320

315321
class LazyAllocJson(LazyAlloc[JSONDict]):
316322
"""
@@ -326,9 +332,9 @@ def validate(self) -> Alloc:
326332

327333
class LazyAllocStr(LazyAlloc[str]):
328334
"""
329-
Lazy allocation backed by a JSON dict cache.
335+
Lazy allocation backed by a str cache.
330336
331-
Uses Alloc.model_validate on the dict.
337+
Uses Alloc.model_validate_json on the string.
332338
"""
333339

334340
def validate(self) -> Alloc:
@@ -466,7 +472,7 @@ def model_validate_files(
466472
result = Result.model_validate_json(
467473
json_data=result_data, context=context
468474
)
469-
alloc = LazyAllocStr(raw=alloc_data)
475+
alloc = LazyAllocStr(raw=alloc_data, _state_root=result.state_root)
470476
output = cls(result=result, alloc=alloc)
471477
return output
472478

@@ -481,7 +487,9 @@ def model_validate(
481487
result = Result.model_validate(
482488
obj=response_json["result"], context=context
483489
)
484-
alloc = LazyAllocJson(raw=response_json["alloc"])
490+
alloc = LazyAllocJson(
491+
raw=response_json["alloc"], _state_root=result.state_root
492+
)
485493
output = cls(result=result, alloc=alloc)
486494
return output
487495

@@ -498,7 +506,9 @@ def model_validate_json(
498506
result = Result.model_validate(
499507
obj=parsed_json["result"], context=context
500508
)
501-
alloc = LazyAllocStr(raw=json.dumps(parsed_json["alloc"]))
509+
alloc = LazyAllocStr(
510+
raw=json.dumps(parsed_json["alloc"]), _state_root=result.state_root
511+
)
502512
output = cls(result=result, alloc=alloc)
503513
return output
504514

0 commit comments

Comments
 (0)