Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
| [`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. |
| [`transaction.py`](transaction.md) — `TransactionEncoder` | XDR → request envelope + (for wasm uploads) kasmer steps; address/contract-id helpers. |
| [`interpreter.py`](interpreter.md) — `NodeInterpreter` | Runs request envelopes through `llvm_interpret`; persists `state.kore`. No `kast`↔`kore` whole-config conversions. |
| `scval.py` | XDR `SCVal` ↔ Komet `SCValue` (`scvalue_from_xdr`), XDR `SCVal` ↔ request/response JSON (`scval_to_json`, `scval_from_json`). |
| `scval.py` | XDR `SCVal` ↔ request/response JSON (`scval_to_json`, `scval_from_json`). |
| `ledger_entries.py` | `getLedgerEntries` XDR translation: base64 `LedgerKey` → key descriptors for the semantics, intermediate entries → base64 `LedgerEntryData`. |
| [`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`. |

Expand Down
123 changes: 0 additions & 123 deletions src/komet_node/scval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,13 @@

from typing import TYPE_CHECKING

from komet.scval import (
SCI32,
SCI64,
SCI128,
SCI256,
SCU32,
SCU64,
SCU128,
SCU256,
AccountId,
ContractId,
SCAddress,
SCBool,
SCBytes,
SCMap,
SCSymbol,
SCVec,
)
from stellar_sdk import xdr as stellar_xdr
from stellar_sdk.xdr.sc_address_type import SCAddressType
from stellar_sdk.xdr.sc_val_type import SCValType

_UINT64_MASK = (1 << 64) - 1

if TYPE_CHECKING:
from komet.scval import SCValue
from stellar_sdk.xdr.sc_address import SCAddress as XDRSCAddress
from stellar_sdk.xdr.sc_val import SCVal


Expand Down Expand Up @@ -176,106 +156,3 @@ def _map_entry(pair: object) -> tuple[dict, dict]:
if isinstance(pair, (list, tuple)) and len(pair) == 2:
return pair[0], pair[1]
raise NotImplementedError(f'Unsupported SCMap entry in JSON encoding: {pair!r}')


def sc_address_from_xdr(xdr: XDRSCAddress) -> SCAddress:
"""Convert an XDR SCAddress to a Komet SCAddress."""
match xdr.type:
case SCAddressType.SC_ADDRESS_TYPE_ACCOUNT:
assert xdr.account_id is not None
# The account_id is a PublicKey XDR — extract the raw 32-byte ed25519 key
assert xdr.account_id.account_id.ed25519 is not None
raw_bytes = xdr.account_id.account_id.ed25519.uint256
return SCAddress(AccountId(raw_bytes))
case SCAddressType.SC_ADDRESS_TYPE_CONTRACT:
assert xdr.contract_id is not None
return SCAddress(ContractId(xdr.contract_id.contract_id.hash))
case _:
raise NotImplementedError(f'Unsupported SCAddress type: {xdr.type}')


def scvalue_from_xdr(xdr: SCVal) -> SCValue:
"""
Convert a Stellar XDR SCVal to a Komet SCValue.

The XDR SCVal is a large union type — each case maps directly to one of
the Komet SCValue dataclasses. Unsupported types (void, error, timepoint,
duration, contract instance, ledger keys) raise NotImplementedError.
"""
match xdr.type:

case SCValType.SCV_BOOL:
assert xdr.b is not None
return SCBool(xdr.b)

case SCValType.SCV_I32:
assert xdr.i32 is not None
return SCI32(xdr.i32.int32)

case SCValType.SCV_I64:
assert xdr.i64 is not None
return SCI64(xdr.i64.int64)

case SCValType.SCV_I128:
assert xdr.i128 is not None
# i128 is stored as (hi: int64, lo: uint64) parts
val = (xdr.i128.hi.int64 << 64) | xdr.i128.lo.uint64
return SCI128(val)

case SCValType.SCV_I256:
assert xdr.i256 is not None
# i256 is stored as (hi_hi, hi_lo, lo_hi, lo_lo) parts
val = (
(xdr.i256.hi_hi.int64 << 192)
| (xdr.i256.hi_lo.uint64 << 128)
| (xdr.i256.lo_hi.uint64 << 64)
| xdr.i256.lo_lo.uint64
)
return SCI256(val)

case SCValType.SCV_U32:
assert xdr.u32 is not None
return SCU32(xdr.u32.uint32)

case SCValType.SCV_U64:
assert xdr.u64 is not None
return SCU64(xdr.u64.uint64)

case SCValType.SCV_U128:
assert xdr.u128 is not None
val = (xdr.u128.hi.uint64 << 64) | xdr.u128.lo.uint64
return SCU128(val)

case SCValType.SCV_U256:
assert xdr.u256 is not None
val = (
(xdr.u256.hi_hi.uint64 << 192)
| (xdr.u256.hi_lo.uint64 << 128)
| (xdr.u256.lo_hi.uint64 << 64)
| xdr.u256.lo_lo.uint64
)
return SCU256(val)

case SCValType.SCV_SYMBOL:
assert xdr.sym is not None
return SCSymbol(xdr.sym.sc_symbol.decode())

case SCValType.SCV_BYTES:
assert xdr.bytes is not None
return SCBytes(xdr.bytes.sc_bytes)

case SCValType.SCV_ADDRESS:
assert xdr.address is not None
return sc_address_from_xdr(xdr.address)

case SCValType.SCV_VEC:
assert xdr.vec is not None
# komet annotates SCVec.val as tuple[SCValue] instead of tuple[SCValue, ...]
return SCVec(tuple(scvalue_from_xdr(v) for v in xdr.vec.sc_vec)) # type: ignore[arg-type]

case SCValType.SCV_MAP:
assert xdr.map is not None
return SCMap({scvalue_from_xdr(entry.key): scvalue_from_xdr(entry.val) for entry in xdr.map.sc_map})

case _:
raise NotImplementedError(f'Unsupported SCVal type: {xdr.type}')
Loading
Loading