mirror of
https://github.com/luxfi/explorer.git
synced 2026-09-24 00:25:12 +00:00
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]>
108 lines
3.9 KiB
Go
108 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// /v1/stats is the canonical flat summary. Values must be plain numbers so a
|
|
// caller can compare total_blocks to eth_blockNumber without unwrapping an
|
|
// envelope — that comparison is the only real check that the index is current.
|
|
func TestV1Stats(t *testing.T) {
|
|
body := get(t, newStats(t, 7).handleStats, "api-explore.lux.network", "/v1/stats")
|
|
|
|
if got := body["total_blocks"].(float64); got != 7 {
|
|
t.Errorf("total_blocks = %v, want 7", got)
|
|
}
|
|
if got := body["head_block"].(float64); got != 7 {
|
|
t.Errorf("head_block = %v, want 7", got)
|
|
}
|
|
if got := body["average_block_time"].(float64); got != 60 {
|
|
t.Errorf("average_block_time = %v, want 60", got)
|
|
}
|
|
if body["indexed"] != true {
|
|
t.Errorf("indexed = %v, want true", body["indexed"])
|
|
}
|
|
}
|
|
|
|
// A missing DB is a real state (fresh chain, indexer starting), not an error:
|
|
// it must answer 200 with indexed=false so a health check can tell the two
|
|
// apart, rather than 500ing.
|
|
func TestV1StatsNoDB(t *testing.T) {
|
|
s := NewStatsService(t.TempDir(), statsChains)
|
|
body := get(t, s.handleStats, "api-explore.lux.network", "/v1/stats")
|
|
if body["indexed"] != false {
|
|
t.Errorf("indexed = %v, want false", body["indexed"])
|
|
}
|
|
}
|
|
|
|
func TestV1Blocks(t *testing.T) {
|
|
s := newStats(t, 10)
|
|
const host = "api-explore.lux.network"
|
|
|
|
t.Run("newest first, default page", func(t *testing.T) {
|
|
blocks := get(t, s.handleBlocks, host, "/v1/blocks")["blocks"].([]any)
|
|
if len(blocks) != 10 {
|
|
t.Fatalf("got %d blocks, want 10", len(blocks))
|
|
}
|
|
first := blocks[0].(map[string]any)
|
|
if n := first["number"].(float64); n != 10 {
|
|
t.Errorf("first block = %v, want 10 (newest first)", n)
|
|
}
|
|
if h := first["hash"].(string); h == "" {
|
|
t.Error("hash must be populated")
|
|
}
|
|
// The timestamp is a unix epoch within a minute of the fixture's head.
|
|
// It read 2026 on mainnet — SQLite casting the leading digits of the
|
|
// stored datetime string — and 2026 is a plausible-looking number, which
|
|
// is why nothing caught it.
|
|
ts := int64(first["timestamp"].(float64))
|
|
if delta := time.Since(time.Unix(ts, 0)); delta < 0 || delta > 2*time.Minute {
|
|
t.Errorf("timestamp = %d (%s), want the head block's real time", ts, time.Unix(ts, 0))
|
|
}
|
|
})
|
|
|
|
t.Run("limit is honoured", func(t *testing.T) {
|
|
if got := len(get(t, s.handleBlocks, host, "/v1/blocks?limit=3")["blocks"].([]any)); got != 3 {
|
|
t.Fatalf("got %d blocks, want 3", got)
|
|
}
|
|
})
|
|
|
|
t.Run("limit is bounded so a huge index cannot be dumped", func(t *testing.T) {
|
|
got := len(get(t, s.handleBlocks, host, "/v1/blocks?limit=99999")["blocks"].([]any))
|
|
// Assert the cap AND that rows actually came back — `got > 200` alone
|
|
// passes trivially when the handler returns nothing, which is how the
|
|
// scan-type bug slipped through the first run of this suite.
|
|
if got != 10 {
|
|
t.Fatalf("returned %d blocks, want all 10 (capped at 200)", got)
|
|
}
|
|
})
|
|
|
|
t.Run("before pages backwards", func(t *testing.T) {
|
|
blocks := get(t, s.handleBlocks, host, "/v1/blocks?before=5&limit=2")["blocks"].([]any)
|
|
if len(blocks) != 2 {
|
|
t.Fatalf("got %d blocks, want 2", len(blocks))
|
|
}
|
|
if n := blocks[0].(map[string]any)["number"].(float64); n != 4 {
|
|
t.Errorf("first block = %v, want 4 (strictly before 5)", n)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Every route this service mounts lives under /v1 and none under /api.
|
|
func TestStatsRoutesAreV1Only(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
NewStatsService(t.TempDir(), statsChains).Mount(mux)
|
|
for _, p := range []string{"/v1/stats", "/v1/blocks", "/v1/counters", "/v1/pages/main"} {
|
|
if _, pattern := mux.Handler(httptest.NewRequest(http.MethodGet, p, nil)); pattern == "" {
|
|
t.Errorf("%s is not mounted", p)
|
|
}
|
|
}
|
|
// /api/v2 is Blockscout's shape and must not exist here.
|
|
if _, pattern := mux.Handler(httptest.NewRequest(http.MethodGet, "/api/v2/stats", nil)); pattern != "" {
|
|
t.Errorf("/api/v2/stats must NOT be mounted, got pattern %q", pattern)
|
|
}
|
|
}
|