Developing with SAGE

SAGE is built to be extended at two seams: the middleware chain for request handling, and QoS plugins for chains. Neither requires touching the protocol code.

Building and Testing

bash
git clone https://github.com/pokt-network/sage.git
cd sage
make sage_build       # → bin/sagegw
make test_unit        # go test ./... -short -count=1 -race
make go_lint          # golangci-lint, then a dead-code check
make docs             # regenerate the config, metrics and admin API reference

A single test: go test ./relay/middleware/ -run TestRetry -race -count=1 -v.

bench/mock-config.yaml runs the whole chain against an in-process mock backend, for load tests without a network. make e2e_test runs the end-to-end suite against a running gateway at SAGE_URL.

Adding a Middleware

Each middleware wraps the next handler and holds one concern. The recipe, in docs/middleware.md:

  1. Write the middleware in relay/middleware/, with its test beside it.
  2. Give it a name in relay/chain_order.go and a place in the default order. If it must run before something else, add an ordering rule there; the default order alone is not a guarantee.
  3. Register it in cmd/sagegw/wire.go.
  4. If it should be switchable at runtime, gate it on a feature flag defined in featureflag/defaults.go.
  5. Give any new config field a doc comment; the configuration reference is generated from them.

Adding a Chain

The core plugin interface has two methods, ParseRequest and SelectEndpoints. Everything else (block height, archival detection, caching, coalescing) is an optional extension interface: a new chain implements only what it needs and reuses the shared height consensus and selection code. A chain that reports its height in JSON can often be declared in the JSON-height plugin instead of written as a new plugin. See docs/qos-plugins.md.

Conventions the Codebase Enforces

  • No bare goroutines. Every goroutine recovers its own panics through internal/safego; a test fails on one that does not.
  • A control must control. Every feature flag needs code that reads it, and every parsed config key needs a reader or an entry saying why it is inert. Tests fail otherwise.
  • Anything countable is generated. Config keys, metrics and routes are documented from the source; a golden test fails when the committed reference is stale.
  • Redis stays optional. Nothing on the relay path may require it.

Read CONTRIBUTING.md before opening a pull request, and ARCHITECTURE.md for why the code is shaped the way it is.