Skip to content

feat(config): a replay: section, and state: decides the state manager (#880, #881) - #907

Merged
scttfrdmn merged 3 commits into
mainfrom
feat/replay-and-state-config
Sep 15, 2026
Merged

scttfrdmn merged 3 commits into
mainfrom
feat/replay-and-state-config

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Two configuration sections that turned nothing, fixed together because they share emulator/config.go and cmd/substrate/main.go.

What was broken

#880 — substrate replay built a zero-value ReplayConfig. Config had no Replay field and there was no ReplayCfg type, so speed_multiplier, stop_on_error, validate_state, use_snapshots and random_seed were unreachable from substrate.yaml. The consequence that matters: state-hash validation — the thing that turns "re-executed 100 events" into "re-executed 100 events and reached the same state" — was off with no way to switch it on.

#881 — Config.State was validated and read by nothing. Both non-test construction sites (cmd/substrate/main.go:234 in the server, :320 in the replay wiring #855 extracted) called substrate.NewMemoryStateManager() unconditionally, while Validate accepted state.backend: sqlite. A config asking for persistence loaded clean and got a manager that forgets everything at exit. state.path was read by nothing at all.

Found while implementing (#880). ReplayResults.StateErrors is documented to carry "descriptions of any state hash mismatches" and was never appended to: a mismatch flipped StateValid to false and left the slice empty, so the CLI could report a state failure it could not describe. Fixed here because #880's acceptance criterion — that a configured validate_state: true "reports StateValid/StateErrors meaningfully" — is not satisfiable without it.

Design decisions

Refuse, don't fall back (#881). emulator.NewStateManager(StateCfg) (StateManager, error) is the single constructor both call sites use, and an unhandled backend is an error. A caller who asked for persistence and was handed memory has been given a wrong answer, and it surfaces much later as absent state rather than here as a refused config. Validate refuses the same set at load time, distinguishing sqlite as not implemented (naming #2) from an unknown value as not valid — asking for the backend this project has an open issue for deserves "unbuilt", not "misspelled". An empty Backend selects memory, matching DefaultConfig: it means the section was not written, not that something else was requested.

This makes state.backend: sqlite — previously accepted — a load-time error. Nothing shipped in the repo sets it (substrate.yaml.example, configs/substrate-local.yaml, deploy/k8s/configmap.yaml all leave state unset or at memory), and anyone who did set it was silently getting memory, so the change is from a wrong answer to an error, never from working persistence to an error.

state.path is kept and refused — a third answer to #881's AC 3. Neither listed option is right yet. No implemented backend keeps files, so Path cannot be honoured until #2. But deleting the field does not make state: {path: …} an error — viper ignores an unknown key — so removal converts today's silent ignore into a permanent one, which is the very defect being fixed. Validate therefore refuses a non-empty state.path, and the field stays for #2 to give meaning to.

ReplayCfg converts by struct conversion, not field-by-field. ToReplayConfig is return ReplayConfig(c), following the TracingCfg → TracingConfig precedent: the two types are identical but for tags, so a field added to ReplayConfig and forgotten here fails to compile instead of silently defaulting to zero — exactly the failure mode #880 is about.

Only fields the engine reads are exposed. All five ReplayConfig fields are genuinely consumed by ReplayEngine; no knob was invented. RandomSeed reaches the engine's RandFloat64/RandInt64, which have no non-test callers today, and the doc comment says so rather than implying plugin-level determinism it does not yet deliver.

Defaults are the previous zero values. This section makes existing behaviour configurable rather than changing it; cfg.Replay.ToReplayConfig() on a default config is asserted equal to substrate.ReplayConfig{} so that stays true. validate_state stays off with the reason written in DefaultConfig: it only means something for a stream recorded with event_store.include_state_hashes: true, so defaulting it on would make the common case fail on absent data. Only speed_multiplier is bounded — refused below zero — since the four booleans and the seed have no invalid values, and validating them would be theatre.

The replay summary now distinguishes three verdicts, not two. It reported valid for a stream carrying no recorded hashes at all, which is the same class of wrong answer as the two issues: with nothing to compare, "not checked (no event in the stream carries a recorded state hash; record with event_store.include_state_hashes: true)" is the honest verdict. streamHasStateHashes decides it.

Wiring stays extracted, per the newFaultController/newCredentialWiring precedent. newReplayEngineWiring, replayStateSummary and streamHasStateHashes are functions a test can call; nothing new went inline into a RunE closure.

Premise corrections

Recorded on the issues (#880, #881) rather than implemented silently:

Files

  • emulator/config.go — ReplayCfg + ToReplayConfig, Config.Replay, DefaultConfig and LoadConfig defaults for all five keys, Validate refusing an unimplemented/unknown state.backend, a non-empty state.path, and a negative replay.speed_multiplier; rewritten StateCfg doc comments.
  • emulator/state.go — NewStateManager.
  • emulator/replay.go — StateErrors populated at both comparison sites via a new stateHashError helper.
  • cmd/substrate/main.go — both state-manager sites go through NewStateManager; newReplayEngineWiring uses cfg.Replay.ToReplayConfig(); streamHasStateHashes; replayStateSummary gains the "not comparable" verdict.
  • substrate.yaml.example — new state: and replay: sections.
  • docs/testing-guide.md — "Replaying from the command line", with the yaml and the four possible state verdicts.
  • CHANGELOG.md, plus tests in emulator/config_test.go, emulator/state_test.go, emulator/replay_reporting_test.go, cmd/substrate/replay_wiring_test.go.

Tests

Added, all table-driven where there is more than one case, none touching the network or the wall clock:

  • TestNewStateManager — memory, an unwritten section, sqlite refused and postgres refused with exact messages; the success cases Put/Get through the returned manager.
  • TestValidate — five new cases: postgres message, sqlite refused rather than falling back, state.path refused, negative replay.speed_multiplier refused, zero multiplier valid.
  • TestReplayCfg_Defaults, TestReplayCfg_ToReplayConfig, TestLoadConfig_ReplaySection (all five keys from yaml, and a bare file keeping defaults), TestLoadConfig_UnsupportedStateBackend.
  • TestReplayReporting_StateErrorsDescribeEveryMismatch — one description per mismatch, each naming the field, the recorded hash and the replayed one.
  • TestNewReplayEngineWiring_UsesTheConfiguredReplaySection, TestStreamHasStateHashes, TestReplayStateSummary (4 verdicts), TestReplayCmd_StateVerdictFollowsTheConfiguredSection (3 subtests through run([]string{"replay", …})), TestNewReplayEngineWiring_RefusesAnUnsupportedStateBackend, TestReplayCmd_RefusesAnUnsupportedStateBackend, TestServerCmd_RefusesAnUnsupportedStateBackend.

Mutation test

Each production change was reverted in place (files copied aside and restored — no git stash) to prove the tests catch the original defect.

A — the #881 silent fallback (NewStateManager returns memory for every backend; Validate back to the old memory/sqlite map; state.path ignored):

--- FAIL: TestValidate/sqlite_state_backend_is_refused_rather_than_falling_back_to_memory
--- FAIL: TestValidate/state_path_is_refused_while_no_backend_reads_it
--- FAIL: TestLoadConfig_UnsupportedStateBackend/sqlite
--- FAIL: TestNewStateManager/an_unknown_backend_names_itself_in_the_error
--- FAIL: TestNewStateManager/sqlite_is_refused_rather_than_silently_answered_with_memory
--- FAIL: TestNewReplayEngineWiring_RefusesAnUnsupportedStateBackend
--- FAIL: TestReplayCmd_RefusesAnUnsupportedStateBackend
--- FAIL: TestServerCmd_RefusesAnUnsupportedStateBackend

B — the #880 zero value (replayCfg := substrate.ReplayConfig{}):

--- FAIL: TestNewReplayEngineWiring_UsesTheConfiguredReplaySection
--- FAIL: TestReplayCmd_StateVerdictFollowsTheConfiguredSection/validate_state_on_reports_the_divergence_and_describes_each_one
--- FAIL: TestReplayCmd_StateVerdictFollowsTheConfiguredSection/validate_state_on_with_no_recorded_hashes_says_so_instead_of_passing

C — StateErrors never appended (both appends removed):

--- FAIL: TestReplayReporting_StateErrorsDescribeEveryMismatch
--- FAIL: TestReplayCmd_StateVerdictFollowsTheConfiguredSection/validate_state_on_reports_the_divergence_and_describes_each_one

All files restored afterwards; go test ./emulator/ ./cmd/substrate/ -count=1 green.

Verification

Run from the worktree root, in this order, all after the final restore:

  • gofmt -l . && go build ./... && go vet ./... — rc=0, no output.
  • golangci-lint run ./... — 0 issues.
  • make test — rc=0 (checked separately from the redirected log, not through a pipe).
  • GOTOOLCHAIN=go1.26.6 make test — rc=0.
  • GOTOOLCHAIN=go1.26.6 make coverage — rc=0; emulator 83.9%, total 83.4% (baseline held), cmd/substrate 47.3%.
  • make docs-reference docs-reference-check docs-versions authz-reference-check — rc=0, docs/services.md already up to date (67 plugins).
  • go vet -C test/e2e ./... && go test -C test/e2e ./... — rc=0, ok … 0.728s.
  • npm --prefix docs run build — rc=0, build complete in 1.90s (node_modules symlinked in for the build, removed before committing).

Compatibility

  • Breaking for one config value: state.backend: sqlite now fails at load with state.backend "sqlite" is not implemented; choose memory. It previously loaded and silently used memory; feat: SQLite StateManager #2 will make it real.
  • Newly refused: a non-empty state.path, and a negative replay.speed_multiplier.
  • Otherwise no behaviour change by default. Every replay: default is the zero value the CLI used before, asserted by test, so an existing config without the section replays identically. A config without a state: section gets memory as before.
  • Visible output change: substrate replay's State: line has two new readings. not checked (state validation is off) is now not checked (replay.validate_state is off), naming the key a user can set, and a stream with validate_state: true but no recorded hashes now reads not checked (no event in the stream carries a recorded state hash; …) where it previously reported valid. valid and MISMATCH (n error(s)) are unchanged — but a MISMATCH is now accompanied by n printed descriptions, where before it printed a count with nothing behind it.

Closes #880.
Closes #881.

`substrate replay` built a zero-value ReplayConfig, so speed_multiplier,
stop_on_error, validate_state, use_snapshots and random_seed were
unreachable from configuration (#880). Config.State was validated and read
by nowhere: both the server and the replay wiring constructed a
MemoryStateManager unconditionally, so `state.backend: sqlite` was accepted
at load and silently answered with memory (#881).

Adds ReplayCfg with a struct conversion to ReplayConfig, NewStateManager as
the one constructor both call sites use, refusal (not fallback) for an
unimplemented or unknown backend, refusal of a state.path no backend reads,
and refusal of a negative replay.speed_multiplier. Also populates
ReplayResults.StateErrors, which was documented and never appended to, and
distinguishes "state not checked" from "state valid" in the replay summary.

Closes #880.
Closes #881.
@scttfrdmn
scttfrdmn force-pushed the feat/replay-and-state-config branch from db07703 to f659a9d Compare September 15, 2026 23:06
@codecov

codecov Bot commented Sep 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.82759% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/substrate/main.go 83.33% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 9517a43 into main Sep 15, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the feat/replay-and-state-config branch September 15, 2026 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant