refactor: invert server dependencies onto interfaces (DIP)#45
Merged
Conversation
…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
changed the base branch from
refactor/architecture-store-types
to
main
July 23, 2026 10:07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses the dependency-inversion gap:
StellarRpcServerhard-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 tomainas #43 → #44 → this merge in order).make checkclean, all 90 tests pass.The inversion
New
interfaces.py— the abstraction layer. Holds the collaboratorProtocols (Interpreter,Encoder,Store) plus the shared data shapes (TxRequest,SimulateRequest,SimulateResult,LedgerRecord,EventRecord), moved out oftransaction.py/store.py/server.pyso both the protocols and the concretes can share them without importing each other.server.pydepends only on abstractions.__init__now takesinterpreter/encoder/storeinjected, typed against the protocols; the module imports none of the concrete classes. Verified:Concretes declare conformance.
NodeInterpreter(Interpreter),TransactionEncoder(Encoder),ChainStore(Store)— somypyflags 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.serverfixture inconftest.py— test root, wiring concretes explicitly ontmp_path.The server's own
network_passphrase/io_dirconstructor params are gone — those now belong to the encoder and store that the composition root builds.Dependency graph after
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.serverwires the concretes; added a sharedcontract_address_from_deployerhelper so the two sites that reached throughserver.encoderfor it no longer depend on the concrete encoder type.test_default_io_dir_is_a_fresh_temp_dirdrivesbuild_server(the temp-dir default moved there).Deliberately out of scope
NodeInterpreterstill constructs its ownsimbolik_definition()internally. With theInterpreterprotocol that's no longer a testability blocker (fake the whole interpreter), so I left it; easy follow-up to inject the definition if wanted.