Files
explorer/supervisor_host_test.go
zeekayandHanzo Dev 216ef5d8bf explorer: answer every request from the chain its host is served
One binary answers explore.lux.network, explore.hanzo.network and
explore.zoo.network. Only /envs.js and the slug-prefixed /v1/indexer/{slug}
routes knew that. Everything else read whichever chain the config called
default, so Hanzo's home page printed 1,107,383 total blocks above its own
Latest blocks list topping out at 29,805 — Lux's number, on Hanzo's page,
overstating Hanzo's chain 37x and Zoo's 49x. Measured on all three brands.

Config.Serves(host) is now the one thing that answers "which chain is this
request about", and the three surfaces that were guessing ask it:

  * StatsService keeps a read handle per chain instead of one pinned at
    startup, so /v1/counters, /v1/lines, /v1/pages/* and /v1/stats report the
    host's chain: 1107259 / 29805 / 22728 on the three brand hosts, where all
    three read 1107259 before.

  * The realtime stream carries the host's chain. It carried all of them: a
    45s read of /v1/base/realtime returned the same 6 cchain + 14 hanzo + 5 zoo
    events on every brand host, and the SPA appends what it is sent, so a
    50-block page rendered 63 rows with 2 Lux on top and 8 Zoo at the bottom.
    ?chain= still overrides; an empty filter no longer means "all chains",
    because no brand host wants another brand's blocks.

  * A slug-less /v1/indexer/* request follows the host, falling back to the
    default only when the host names no chain (api-explore.lux.network) or
    names one that is not up.

Also here, because they were all the same mistake about the same column: the
timestamp in evm_blocks is a Go time.Time bound into a column declared
TIMESTAMP, so SQLite stores a datetime STRING, and everything that read it as
a unix epoch got nothing and said nothing.

  * /stats' daily charts were empty on all three brands. strftime with the
    'unixepoch' modifier returns NULL on a datetime string, the scan failed,
    and handlePagesMain discarded the error into `daily, _ :=` — a nil slice
    marshals to `null`, which the page renders as "No data". Now 22 points.

  * Average block time read 0.0s on a chain producing a block every 10s: the
    three-value Scan failed and its error was never checked, so the zero went
    out as the answer. Now 60.0 on a one-block-per-minute fixture.

  * /v1/blocks reported `"timestamp": 2026` for every block — CAST of a
    datetime string takes its leading digits, and 2026 looks like a number
    rather than a failure. Now a real epoch.

  * Transactions (24h) was the lifetime total. SQLite orders every INTEGER
    below every TEXT, so `timestamp > <epoch>` was true for all 1.1M rows.

blockEpoch and blockDay are the only two spellings allowed to touch that
column now. The fixtures were the reason none of this failed a test: they
declared the column TIMESTAMP and then inserted integers into it, so the suite
exercised a database the indexer never writes. They bind a time.Time now, and
on the old queries they fail with exactly what production showed —
average_block_time "0.0", empty charts, timestamp 2026.

Deleted: the eight per-channel SSE routes at bare /blocks, /transactions,
/tokens, /gas-tracker, /stats, /validators, /token-transfers and /internal-txs.
Every one of those is also an SPA page path, and one mux cannot serve both; the
SPA opens only the multiplexed stream, so they had no subscriber to lose. With
them goes the second envelope, the per-channel framing built on every
broadcast, and the client's channel field — SSE has one endpoint and one shape.

indexer v1.5.3 for the block transaction-count name.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-08 06:37:03 -07:00

79 lines
2.6 KiB
Go

package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
// A slug-less /v1/indexer request is answered by the chain the host names.
// Pinned to the configured default, api-explore.hanzo.network and
// api-explore.zoo.network returned the same window of Lux block heights as
// api-explore.lux.network — to the block, on every fetch.
func TestIndexerDispatchFollowsTheHost(t *testing.T) {
s := NewChainSupervisor(Config{Chains: []ChainConfig{
{Slug: "cchain", Default: true},
{Slug: "hanzo"},
{Slug: "zoo"},
}})
s.SetDefaultSlug("cchain")
for _, slug := range []string{"cchain", "hanzo", "zoo"} {
slug := slug
s.indexerRoutes.Store(slug, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"served": slug, "path": r.URL.Path})
}))
}
mux := http.NewServeMux()
s.MountRoutes(mux)
for _, tc := range []struct{ host, path, want string }{
{"api-explore.lux.network", "/v1/indexer/blocks", "cchain"}, // names none -> default
{"api-explore.hanzo.network", "/v1/indexer/blocks", "hanzo"},
{"api-explore.zoo.network", "/v1/indexer/blocks", "zoo"},
{"explore.zoo.ngo", "/v1/indexer/tokens", "zoo"},
// An explicit slug still wins over the host.
{"api-explore.hanzo.network", "/v1/indexer/zoo/blocks", "zoo"},
} {
req := httptest.NewRequest(http.MethodGet, tc.path, nil)
req.Host = tc.host
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
var body map[string]string
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("%s %s: %v -- %s", tc.host, tc.path, err, rec.Body.String())
}
if body["served"] != tc.want {
t.Errorf("%s %s: served by %q, want %q", tc.host, tc.path, body["served"], tc.want)
}
}
}
// A host naming a chain that is not running falls back rather than 404ing —
// chains are added and removed at runtime.
func TestIndexerDispatchFallsBackWhenTheHostsChainIsDown(t *testing.T) {
s := NewChainSupervisor(Config{Chains: []ChainConfig{
{Slug: "cchain", Default: true},
{Slug: "hanzo"},
}})
s.SetDefaultSlug("cchain")
s.indexerRoutes.Store("cchain", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"served": "cchain"})
}))
req := httptest.NewRequest(http.MethodGet, "/v1/indexer/blocks", nil)
req.Host = "api-explore.hanzo.network"
rec := httptest.NewRecorder()
mux := http.NewServeMux()
s.MountRoutes(mux)
mux.ServeHTTP(rec, req)
var body map[string]string
json.Unmarshal(rec.Body.Bytes(), &body)
if body["served"] != "cchain" {
t.Errorf("served by %q, want cchain", body["served"])
}
}