Skip to content

refactor: invert server dependencies onto interfaces (DIP)#45

Merged
RaoulSchaffranek merged 2 commits into
mainfrom
refactor/dependency-inversion
Jul 23, 2026
Merged

refactor: invert server dependencies onto interfaces (DIP)#45
RaoulSchaffranek merged 2 commits into
mainfrom
refactor/dependency-inversion

Conversation

@RaoulSchaffranek

Copy link
Copy Markdown
Member

Addresses the dependency-inversion gap: StellarRpcServer hard-constructed its collaborators (NodeInterpreter(), TransactionEncoder(...), ChainStore(...)) in __init__, so there was no seam to substitute fakes — the server's dispatch/validation/response logic couldn't be exercised without the compiled K interpreter. Stacked on #44 (retarget to main as #43#44 → this merge in order). make check clean, all 90 tests pass.

The inversion

New interfaces.py — the abstraction layer. Holds the collaborator Protocols (Interpreter, Encoder, Store) plus the shared data shapes (TxRequest, SimulateRequest, SimulateResult, LedgerRecord, EventRecord), moved out of transaction.py/store.py/server.py so both the protocols and the concretes can share them without importing each other.

server.py depends only on abstractions. __init__ now takes interpreter / encoder / store injected, typed against the protocols; the module imports none of the concrete classes. Verified:

$ grep -E 'import (NodeInterpreter|TransactionEncoder|ChainStore)' server.py
(nothing)

Concretes declare conformance. NodeInterpreter(Interpreter), TransactionEncoder(Encoder), ChainStore(Store) — so mypy flags any drift between a concrete and the protocol the server relies on. This is the correct DIP direction: the low-level details depend on the abstraction.

Composition roots wire the concretes:

  • build_server() in __main__.py — production root; owns the default temp io-dir behavior that used to live in the server.
  • the server fixture in conftest.py — test root, wiring concretes explicitly on tmp_path.

The server's own network_passphrase / io_dir constructor params are gone — those now belong to the encoder and store that the composition root builds.

Dependency graph after

interfaces  ←  server, interpreter, transaction, store   (all depend on the abstractions)
__main__    →  server + the three concretes              (composition root)

The server no longer has a construction edge to any collaborator.

Testability payoff

A unit test can now drive StellarRpcServer(interpreter=Fake(), encoder=..., store=...) with a K-free fake interpreter and exercise dispatch/validation/error-mapping without the compiled semantics. Per our discussion I did not add such a test here (refactor-only); the seam is in place for follow-up.

Test changes (consequences of the refactor, not new coverage)

  • conftest.server wires the concretes; added a shared contract_address_from_deployer helper so the two sites that reached through server.encoder for it no longer depend on the concrete encoder type.
  • test_default_io_dir_is_a_fresh_temp_dir drives build_server (the temp-dir default moved there).

Deliberately out of scope

NodeInterpreter still constructs its own simbolik_definition() internally. With the Interpreter protocol that's no longer a testability blocker (fake the whole interpreter), so I left it; easy follow-up to inject the definition if wanted.

…n root

StellarRpcServer hard-constructed NodeInterpreter, TransactionEncoder, and
ChainStore in __init__, so there was no seam to substitute fakes — the server's
dispatch/validation/response logic could not be exercised without the compiled K
interpreter.

- interfaces.py: a new abstractions module holding the shared data shapes
  (TxRequest, SimulateRequest, SimulateResult, LedgerRecord, EventRecord, moved
  out of transaction.py/store.py) and the Interpreter / Encoder / Store protocols.
- StellarRpcServer.__init__ now takes interpreter/encoder/store injected and typed
  against those protocols; server.py imports none of the concretes. The three
  collaborators declare conformance (NodeInterpreter(Interpreter), etc.), so mypy
  catches drift between a concrete and its protocol.
- Composition roots wire the concretes: build_server() in __main__ (production,
  owns the default temp io-dir), and the conftest server fixture (tests). The
  server's own network_passphrase/io_dir params are gone — those belong to the
  encoder and store the root builds.

Tests: the two sites reaching through server.encoder for a helper now use a
shared conftest.contract_address_from_deployer; the default-io-dir test drives
build_server. make check clean, 90 tests pass.
@RaoulSchaffranek
RaoulSchaffranek changed the base branch from refactor/architecture-store-types to main July 23, 2026 10:07
@RaoulSchaffranek
RaoulSchaffranek merged commit b5b0642 into main Jul 23, 2026
1 check passed
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