refactor(backend): restructure config to support both networks#570
refactor(backend): restructure config to support both networks#570silent-cipher wants to merge 27 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Refactors the backend configuration system to support running against multiple Filecoin networks (e.g., calibration + mainnet) from a single deployment, and threads network through chain- and provider-dependent services so jobs/metrics/DB operations are correctly network-scoped.
Changes:
- Replaces the legacy single-network config (
app.config.ts+validationSchema) with a new multi-network config loader + Joi env validation + legacy env compatibility shim. - Updates wallet/chain integration and check services (deal, retrieval, pull-check, piece-cleanup, data-retention, metrics collectors, dev-tools) to accept and use an explicit
networkparameter and per-network config. - Extends provider API responses and filtering to support network scoping (controller/service/DTO + blocklists per network).
Reviewed changes
Copilot reviewed 58 out of 58 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| apps/backend/src/worker.module.ts | Switches Nest config wiring to new loadConfig/validateConfig. |
| apps/backend/src/wallet-sdk/wallet-sdk.service.ts | Refactors wallet SDK to maintain per-network Synapse + provider caches/state. |
| apps/backend/src/wallet-sdk/wallet-sdk.service.spec.ts | Updates wallet-sdk tests for multi-network state and config shape. |
| apps/backend/src/retrieval/retrieval.service.ts | Threads network through retrieval flow and deal selection/liveness checks. |
| apps/backend/src/retrieval/retrieval.service.spec.ts | Updates retrieval tests for network-scoped config/labels/entities. |
| apps/backend/src/retrieval-addons/strategies/ipfs-block.strategy.ts | Looks up provider info using the deal’s network. |
| apps/backend/src/pull-check/pull-piece-stream-tracker.service.ts | Updates config typing/imports for new config module. |
| apps/backend/src/pull-check/pull-piece-stream-tracker.service.spec.ts | Aligns pull-piece stream tracker tests with new config types. |
| apps/backend/src/pull-check/pull-check.service.ts | Makes pull-check execution explicitly network-scoped (config + synapse client). |
| apps/backend/src/pull-check/pull-check.service.spec.ts | Updates pull-check tests to pass network and validate new labels/context. |
| apps/backend/src/providers/providers.service.ts | Adds optional network filter + per-network blocklist application in queries. |
| apps/backend/src/providers/providers.service.spec.ts | Updates providers service tests for per-network blocklist query clauses. |
| apps/backend/src/providers/providers.controller.ts | Adds network query param with validation for providers listing endpoint. |
| apps/backend/src/providers/dto/provider-list-response.dto.ts | Adds network to provider DTO for API responses/docs. |
| apps/backend/src/piece-cleanup/piece-cleanup.service.ts | Scopes cleanup quotas, DB queries, provider lookups, and Synapse access by network. |
| apps/backend/src/piece-cleanup/piece-cleanup.service.spec.ts | Updates piece-cleanup tests for network-scoped methods and Synapse sourcing. |
| apps/backend/src/metrics-prometheus/wallet-balance.collector.ts | Collects wallet balances per active network and labels metrics with network. |
| apps/backend/src/metrics-prometheus/wallet-balance.collector.spec.ts | Updates wallet-balance collector tests for per-network metric labels. |
| apps/backend/src/jobs/data-set-creation.handler.ts | Threads network into dataset provisioning/repair calls. |
| apps/backend/src/http-client/http-client.service.ts | Updates config import to new module. |
| apps/backend/src/dev-tools/dto/trigger-retrieval.dto.ts | Adds optional network query param validation for retrieval trigger. |
| apps/backend/src/dev-tools/dto/trigger-deal.dto.ts | Adds optional network query param validation for deal trigger. |
| apps/backend/src/dev-tools/dev-tools.service.ts | Adds default network behavior and passes network through deal/retrieval operations. |
| apps/backend/src/dev-tools/dev-tools.controller.ts | Exposes optional network query params for dev-tools endpoints. |
| apps/backend/src/deal/deal.service.ts | Makes deal creation, dataset liveness/repair, and wallet address/config network-scoped. |
| apps/backend/src/deal/deal.service.spec.ts | Updates deal service tests for new signatures and per-network config. |
| apps/backend/src/deal-addons/strategies/ipni.strategy.ts | Updates config import to new module. |
| apps/backend/src/dataSource/dataSource.service.ts | Updates config import to new module. |
| apps/backend/src/dataSource/dataSource.service.spec.ts | Updates config import to new module. |
| apps/backend/src/dataset-liveness/dataset-liveness.service.ts | Makes liveness probes network-aware (synapse client + provider registry access). |
| apps/backend/src/dataset-liveness/dataset-liveness.service.spec.ts | Updates liveness tests to pass network argument. |
| apps/backend/src/database/database.module.ts | Updates config import to new module. |
| apps/backend/src/data-retention/data-retention.service.ts | Makes data-retention polling network-scoped and uses per-network blocklists/providers. |
| apps/backend/src/config/types.ts | Introduces new typed config model including per-network slices and active networks. |
| apps/backend/src/config/loader.ts | Implements env-based config loader, including per-network prefixed vars. |
| apps/backend/src/config/loader.spec.ts | Adds tests for config loader behavior across networks and env layouts. |
| apps/backend/src/config/legacy-env-compat.ts | Adds compatibility shim translating legacy single-network env vars to prefixed form. |
| apps/backend/src/config/legacy-env-compat.spec.ts | Adds tests for legacy env translation behavior. |
| apps/backend/src/config/index.ts | Exposes config entry points (loadConfig, validateConfig) and types. |
| apps/backend/src/config/env.schema.ts | Adds Joi env validation including dynamic per-network requirements + legacy shim hook. |
| apps/backend/src/config/env.schema.spec.ts | Adds extensive tests for env schema validation rules. |
| apps/backend/src/config/env.parsers.ts | Adds domain-level env parsing helpers (networks, run mode, lists). |
| apps/backend/src/config/env.helpers.ts | Adds low-level env getters/coercion helpers. |
| apps/backend/src/config/constants.ts | Defines per-network default values and supported env prefixes. |
| apps/backend/src/config/app.config.ts | Removes legacy single-network config loader and schema. |
| apps/backend/src/common/synapse-factory.ts | Updates Synapse factory to accept INetworkConfig (wallet-key vs session-key variants). |
| apps/backend/src/common/sp-blocklist.ts | Updates blocklist helper to accept per-network blocklist fields. |
| apps/backend/src/common/sp-blocklist.spec.ts | Updates blocklist tests for new config shape. |
| apps/backend/src/common/logging.ts | Extends job/provider log context types to include network. |
| apps/backend/src/clickhouse/clickhouse.service.ts | Updates config import to new module. |
| apps/backend/src/app.module.ts | Switches Nest config wiring to new loadConfig/validateConfig. |
| apps/backend/src/app.controller.ts | Updates config endpoint to return per-active-network rates/intervals. |
| apps/backend/.env.example | Documents and demonstrates multi-network env layout and per-network overrides. |
| .env.example | Updates secrets-only example to use per-network prefixed wallet/session keys. |
…iew findings Per-network env vars now resolve `<NETWORK>_<VAR>` -> unprefixed `<VAR>` -> default, so shared tuning is set once and overridden per network only where it differs. A single source of truth (network-fields.ts) classifies chain-specific vars (credentials, endpoints, blocklists) that never inherit, and drives both the loader resolver and the Joi schema. Validation: shared unprefixed keys are validated against the same rules as their prefixed overrides (not waved through by allowUnknown). Per-network defaults moved out of Joi into the loader so NestJS-injected prefixed defaults can't shadow a shared value. Also resolves prior-PR review findings on this branch: - TARGET<MAX is asserted post-resolution in the loader (the Joi .custom read the wrong, unprefixed key and never fired) - legacy var renames (DEALBOT_MAINTENANCE_WINDOWS_UTC, DEALBOT_MAINTENANCE_WINDOW_MINUTES, JOB_PIECE_CLEANUP_PER_SP_PER_HOUR) are promoted to their current names - empty/degenerate NETWORKS is rejected instead of booting idle - getBooleanEnv no longer treats "0"/"no"/"False" as true - tryGetSynapse() replaces the dead getSynapse() ?? createSynapseInstance fallback - retrieval provider lookup filters by (address, network) - shutdown drain timeout includes maxPieceCleanupRuntimeSeconds - dropped the import-time process.env.NETWORK default Prometheus label
SgtPooki
left a comment
There was a problem hiding this comment.
This is a huge PR.. I threw a few AIs at it to pick it apart and they came up with some flaws in this approach that I think are made simpler by #621. This works as is, but I think #621 makes the env loading story more consistent and understandable.
Approving under the assumption that #621 will also get merged.
Built on top of #569
Major changes