Skip to content

feat: implement simulateTransaction#36

Open
RaoulSchaffranek wants to merge 4 commits into
mainfrom
feat/simulate-transaction
Open

feat: implement simulateTransaction#36
RaoulSchaffranek wants to merge 4 commits into
mainfrom
feat/simulate-transaction

Conversation

@RaoulSchaffranek

Copy link
Copy Markdown
Member

What

Implements the simulateTransaction RPC method as a non-committing dry run of a contract invocation, per the Stellar RPC specification.

The server decodes the base64 transaction envelope (which may be unsigned) and requires exactly one InvokeHostFunction operation with an invoke-contract host function. The invocation is dispatched through the K semantics like a regular sendTransaction, but nothing is committed: interpreter.run gains a commit flag that discards the resulting configuration, and no receipt, trace, or ledger bump is written.

On success the response contains:

  • latestLedger as a JSON number,
  • minResourceFee as a stringified constant (the semantics do not meter resources),
  • results[0].xdr — the host function's return value as base64 SCVal XDR, with auth: [],
  • transactionData — a minimal valid SorobanTransactionData (empty footprint, zero resources).

Failures (trapped call, unknown contract, non-invoke transaction, upload/deploy host functions) are reported in the result body as {error, latestLedger}, matching real stellar-rpc; only an undecodable transaction parameter is a JSON-RPC -32602 error. Optional fields are omitted rather than set to null, matching the omitempty serialization of the Go structs.

How

Following the repo architecture, dispatch and response content live in the K semantics (node.md): a simulateTransaction rule runs the invocation and reports the return value from the <hostStack> as a JSON-encoded ScVal, or a result-level error when the call trapped. The final XDR serialization is the one part done in Python, since K has no XDR encoder or base64 hook: json_to_scval (scval.py) converts the JSON ScVal to base64 SCVal XDR, and server.py fills in minResourceFee and transactionData. Docs (server.md, architecture.md, notes.md) describe the method and its current limitations (no auth recording, no resource metering, no events/restorePreamble/stateChanges, ScMap return values not yet encoded).

Testing

  • New integration tests in src/tests/integration/test_server.py cover the response shape (number/string types, base64 XDR round-trips), the no-commit guarantee (no ledger bump, no receipt, no trace), result-level errors for failed simulations and non-invoke transactions, and -32602 for missing/malformed params.
  • Full test_server.py suite passes (32 tests), and make check is clean (including two mypy findings that predate this branch).

Cover the spec-required response shape: latestLedger as a JSON number,
minResourceFee as a stringified number, results[0].xdr carrying the host
function return value as base64 SCVal XDR, results[0].auth as an array of
base64 strings, and transactionData as valid SorobanTransactionData XDR.
Optional fields must be omitted rather than null, matching the omitempty
serialization of real stellar-rpc.

Also assert that simulation never commits (no ledger bump, no receipt, no
trace), that failures are reported as a result-level {error, latestLedger}
object without the success-only fields, that non-InvokeHostFunction
transactions are rejected with a result-level error, and that missing or
malformed transaction params return JSON-RPC error -32602.

All six tests currently fail with -32601 because the method is not
implemented yet.
Dispatch a simulateTransaction request through the K semantics like a
regular invocation, but discard the resulting configuration
(interpreter.run gains a commit flag) and write no receipt, trace, or
ledger bump. The semantics report the invocation's return value as a
JSON-encoded ScVal; the server serializes it to base64 SCVal XDR
(json_to_scval) and fills in a synthetic minResourceFee and an
empty-footprint SorobanTransactionData, since K cannot build XDR.
Failures (trapped call, unknown contract, non-invoke transaction) are
reported in the result body as {error, latestLedger}, matching real
stellar-rpc; only undecodable transaction XDR is a -32602 error.
Both errors predate this branch: komet's SCVec.val annotation is
tuple[SCValue] (a 1-tuple) rather than tuple[SCValue, ...], so the
call in scvalue_from_xdr needs a targeted ignore, and the server test
fixture was missing its return type annotation. make check now passes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant