Skip to content

Commit de55339

Browse files
Merge pull request #44 from runtimeverification/refactor/architecture-store-types
refactor: ChainStore, RpcError, typed boundaries, errors module
2 parents 386f483 + cc298a0 commit de55339

7 files changed

Lines changed: 405 additions & 198 deletions

File tree

docs/notes.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
| Module | Role |
1212
|---|---|
13-
| [`server.py`](server.md)`StellarRpcServer` | Long-running HTTP/JSON-RPC server wrapping the one-shot K interpreter; `handle_rpc` dispatch; owns the io-dir files. Holds no ledger or receipt state. |
14-
| [`transaction.py`](transaction.md)`TransactionEncoder` | XDR → request envelope + (for wasm uploads) kasmer steps; address/contract-id helpers. |
13+
| [`server.py`](server.md)`StellarRpcServer` | Long-running HTTP/JSON-RPC server wrapping the one-shot K interpreter; `handle_rpc``_dispatch` routing (bad params raise `RpcError`). Holds no ledger or receipt state; delegates the io-dir to `ChainStore`. |
14+
| `store.py``ChainStore` | Owns the io-dir layout: the sole reader/writer of `state.kore`, `metadata.json`, and the `receipts/` `ledgers/` `events/` `requests/` `wasms/` files. |
15+
| [`transaction.py`](transaction.md)`TransactionEncoder` | XDR → request envelope (`TxRequest`/`SimulateRequest`) + (for wasm uploads) kasmer steps; address/contract-id helpers. |
1516
| [`interpreter.py`](interpreter.md)`NodeInterpreter` | Runs request envelopes through `llvm_interpret`; persists `state.kore`. No `kast``kore` whole-config conversions. |
1617
| `scval.py` | XDR `SCVal` ↔ request/response JSON (`scval_to_json`, `scval_from_json`). |
1718
| `ledger_entries.py` | `getLedgerEntries` XDR translation: base64 `LedgerKey` → key descriptors for the semantics, intermediate entries → base64 `LedgerEntryData`. |
19+
| `errors.py` | The internal exceptions: `NodeInterpreterError`, `TransactionEncodingError`, and `RpcError` (a JSON-RPC error with its spec code). |
1820
| [`kdist/node.md`](node-semantics.md) | The K RPC layer: reads `request.json`, dispatches, updates `metadata.json` and the per-transaction `receipts/` files, writes `response.json`. |
1921

2022
State lives in the io dir as `state.kore` (KORE world state) and `metadata.json` (ledger counter), with per-transaction receipts and traces under `receipts/` and `traces/`. See [architecture.md](architecture.md).

src/komet_node/errors.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""The exceptions komet-node raises internally.
2+
3+
Collected in one module so no layer has to reach into another purely for an error type
4+
(the encoder, for instance, must not depend on the interpreter just to signal a bad input).
5+
6+
- :class:`NodeInterpreterError` — the K interpreter subprocess failed.
7+
- :class:`TransactionEncodingError` — a Stellar transaction could not be encoded.
8+
- :class:`RpcError` — a JSON-RPC error to hand back to the client.
9+
"""
10+
11+
from __future__ import annotations
12+
13+
14+
class NodeError(RuntimeError):
15+
"""Base class for komet-node's own errors."""
16+
17+
18+
class NodeInterpreterError(NodeError):
19+
"""The K interpreter subprocess failed or produced no usable output."""
20+
21+
22+
class TransactionEncodingError(NodeError):
23+
"""A Stellar transaction could not be encoded into a node request envelope.
24+
25+
Raised by :class:`~komet_node.transaction.TransactionEncoder` for inputs it cannot
26+
translate (sub-stroop XLM amounts, malformed strkey addresses). On the admission path
27+
the server catches it and turns it into a ``txMALFORMED`` status response.
28+
"""
29+
30+
31+
class RpcError(Exception):
32+
"""A JSON-RPC error to return to the client, carrying its spec code and message.
33+
34+
Raised by request validation and dispatch in the server so the envelope builders can
35+
return a single value type instead of threading a pre-formatted error string back
36+
through their return type. :meth:`~komet_node.server.StellarRpcServer.handle_rpc`
37+
catches it and formats the error envelope. The classmethods name the JSON-RPC 2.0
38+
error codes; ``invalid_params`` prepends the conventional ``Invalid params:`` prefix.
39+
"""
40+
41+
code: int
42+
message: str
43+
44+
def __init__(self, code: int, message: str) -> None:
45+
super().__init__(code, message)
46+
self.code = code
47+
self.message = message
48+
49+
@classmethod
50+
def invalid_request(cls, message: str) -> RpcError:
51+
return cls(-32600, message)
52+
53+
@classmethod
54+
def invalid_params(cls, detail: str) -> RpcError:
55+
return cls(-32602, f'Invalid params: {detail}')
56+
57+
@classmethod
58+
def method_not_found(cls, message: str = 'Method not found') -> RpcError:
59+
return cls(-32601, message)
60+
61+
@classmethod
62+
def internal(cls, message: str = 'Internal error') -> RpcError:
63+
return cls(-32603, message)

src/komet_node/interpreter.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
from pyk.kore.syntax import App, SortApp
1414
from pyk.utils import check_file_path, run_process_2
1515

16+
from .errors import NodeInterpreterError
1617
from .utils import simbolik_definition
1718

1819
if TYPE_CHECKING:
20+
from collections.abc import Mapping
1921
from pathlib import Path
2022
from typing import Any
2123

@@ -122,7 +124,7 @@ def run(
122124
self,
123125
state_file: Path,
124126
io_dir: Path,
125-
request: dict[str, Any],
127+
request: Mapping[str, Any],
126128
program_steps: list[KInner] | None = None,
127129
*,
128130
commit: bool = True,
@@ -180,7 +182,3 @@ def _inject_program(self, pattern: Pattern, steps: list[KInner]) -> Pattern:
180182
"""
181183
steps_kore = kast_to_kore(self.definition.kdefinition, steps_of(steps), KSort('Steps'))
182184
return _set_cell(pattern, _PROGRAM_CELL, steps_kore)
183-
184-
185-
class NodeInterpreterError(RuntimeError):
186-
pass

0 commit comments

Comments
 (0)