Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion discovery/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/livepeer/node/discovery/shared"
"github.com/santhosh-tekuri/jsonschema/v6"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -51,16 +53,29 @@ func TestDiscoverySchema(t *testing.T) {
{"mixed-case schemes", strings.ReplaceAll(envelope(runner), "https://", "hTtPs://"), true},
{"mixed-case node scheme", strings.ReplaceAll(metadata, "https://", "HtTp://"), true},
{"runner URL components", envelope(strings.ReplaceAll(runner, "https://orch.example/app", "https://user:pass@orch.example/app?x=1#part")), true},
{"unknown properties", envelope(strings.TrimSuffix(runner, "}") + `,"extension":{"count":9007199254740993}}`), true},
{"unknown properties", envelope(strings.TrimSuffix(runner, "}") + `,"gpu.name":null,"versıon":null,"extension":{"count":9007199254740993}}`), true},
{"extensible price", envelope(strings.TrimSuffix(runner, "}") + `,"mode":"future","price_info":{"price":123456789012345678901234567890,"price_usd":0.123456789012345678901234567890123456,"currency":"credits","unit":"tokens","extension":true}}`), true},
{"USD price omitted", envelope(strings.TrimSuffix(runner, "}") + `,"price_info":{"price":1,"currency":"wei","unit":"fixed"}}`), true},
{"price string", envelope(strings.TrimSuffix(runner, "}") + `,"price_info":{"price":"1"}}`), false},
{"USD price string", envelope(strings.TrimSuffix(runner, "}") + `,"price_info":{"price":1,"price_usd":"0.1"}}`), false},
{"capacity string", envelope(strings.TrimSuffix(runner, "}") + `,"capacity":"4"}`), false},
{"fractional capacity", envelope(strings.TrimSuffix(runner, "}") + `,"capacity_available":1.5}`), false},
{"integer tokens", envelope(strings.TrimSuffix(runner, "}") + `,"capacity":4.0,"capacity_used":1e0,"capacity_available":9007199254740993}`), true},
{"scaled integers", envelope(strings.TrimSuffix(runner, "}") + `,"capacity":1200.0e-2,"capacity_used":1.20E1,"capacity_available":-0.00e-100}`), true},
{"version number", envelope(strings.TrimSuffix(runner, "}") + `,"version":1}`), false},
{"null metadata", envelope(strings.TrimSuffix(runner, "}") + `,"metadata":null}`), false},
{"null GPU", envelope(strings.TrimSuffix(runner, "}") + `,"gpu":null}`), false},
{"GPU name number", envelope(strings.TrimSuffix(runner, "}") + `,"gpu":{"name":1}}`), false},
{"fractional VRAM", envelope(strings.TrimSuffix(runner, "}") + `,"gpu":{"vram_mb":1.5}}`), false},
{"null price", envelope(strings.TrimSuffix(runner, "}") + `,"price_info":{"price":null}}`), false},
{"missing URL", envelope(strings.ReplaceAll(runner, `"url":"https://orch.example/app",`, "")), false},
{"wrong URL field case", envelope(strings.ReplaceAll(runner, `"url":`, `"URL":`)), false},
{"missing app", envelope(strings.ReplaceAll(runner, `"app":"example",`, "")), false},
{"missing observation", envelope(`{"url":"https://orch.example/app","app":"example"}`), false},
{"empty app", envelope(strings.ReplaceAll(runner, `"app":"example"`, `"app":""`)), false},
{"invalid observation", envelope(strings.ReplaceAll(runner, "2026-10-22T18:05:00.123+01:00", "yesterday")), false},
{"comma observation fraction", envelope(strings.ReplaceAll(runner, "00.123", "00,123")), false},
{"invalid observation offset", envelope(strings.ReplaceAll(runner, "+01:00", "+24:00")), false},
{"invalid query time", `[{"nodes":[{"url":"https://orch.example","queried_at":"yesterday"}]}]`, false},
{"missing query time", `[{"nodes":[{"url":"https://orch.example"}]}]`, false},
{"missing node URL", `[{"nodes":[{"queried_at":"2026-10-22T18:05:00Z"}]}]`, false},
Expand Down Expand Up @@ -88,6 +103,41 @@ func TestDiscoverySchema(t *testing.T) {
} else {
require.Error(t, err)
}
entries, err := shared.Decode[discoveryRunner]([]byte(test.body), time.Now())
if test.valid {
require.NoError(t, err)
}
if err == nil {
body, err := json.Marshal(entries)
require.NoError(t, err)
require.NoError(t, validateDiscoveryJSON(t, schema, body))
}
})
}
}

func TestDiscoveryRunnerValidation(t *testing.T) {
for _, test := range []struct {
name, fields string
valid bool
}{
{"large integer exponent", `"gpu":{"vram_mb":1e1000000}`, true},
{"overflow integer exponent", `"gpu":{"vram_mb":1e999999999999999999999}`, true},
{"large fractional exponent", `"gpu":{"vram_mb":1e-1000000}`, false},
{"overflow fractional exponent", `"gpu":{"vram_mb":1e-999999999999999999999}`, false},
{"zero with overflow exponent", `"gpu":{"vram_mb":0e-999999999999999999999}`, true},
{"case alias", `"URL":"https://other.example/app"`, false},
{"Unicode case alias", `"capacity_used":1,"capacity_uſed":9`, false},
{"nested Unicode case alias", `"price_info":{"price_usd":0.1,"price_uſd":0.9}`, false},
{"optional field case", `"Mode":"persistent"`, false},
{"duplicate field", `"capacity":1,"capacity":2`, false},
{"duplicate extension field", `"extension":{"x":1,"x":2}`, false},
{"invalid Unicode", `"extension":"\ud800"`, false},
{"invalid UTF-8", `"extension":"` + "\xff" + `"`, false},
} {
t.Run(test.name, func(t *testing.T) {
_, err := shared.Decode[discoveryRunner]([]byte(`[{"address":"https://orch.example","runners":[{"url":"https://orch.example/app","app":"test","observed_at":"2000-01-01T00:00:00Z",`+test.fields+`}]}]`), time.Now())
require.Equal(t, test.valid, err == nil)
})
}
}
Expand Down
65 changes: 64 additions & 1 deletion discovery/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package discovery
import (
"context"
"encoding/json"
jsonv2 "encoding/json/v2"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -196,10 +198,71 @@ type discoveryRunner struct {
data json.RawMessage
}

// Validate known fields without changing forwarded records or extensions.
func validRunnerFields(data []byte, prefix string) bool {
var fields map[string]json.RawMessage
if jsonv2.Unmarshal(data, &fields) != nil || fields == nil {
return false
}
for name, value := range fields {
key := ""
match := func(canonical string) bool {
if !strings.EqualFold(name, canonical) {
return false
}
key = canonical
return true
}
switch {
case prefix == "" && match("observed_at"):
var stamp time.Time
if value[0] != '"' || jsonv2.Unmarshal(value, &stamp) != nil {
return false
}
case prefix == "" && (match("url") || match("app") || match("version") || match("metadata") || match("mode")) ||
prefix == "gpu" && (match("id") || match("name")) ||
prefix == "price_info" && (match("currency") || match("unit")):
if value[0] != '"' {
return false
}
case prefix == "" && (match("capacity") || match("capacity_used") || match("capacity_available")) || prefix == "gpu" && match("vram_mb"):
if value[0] != '-' && (value[0] < '0' || value[0] > '9') {
return false
}
mantissa, power, _ := strings.Cut(strings.ToLower(string(value)), "e")
// An absent exponent defaults to zero; Atoi saturates on overflow.
exponent, _ := strconv.Atoi(power)
whole, fraction, _ := strings.Cut(mantissa, ".")
digits := whole + fraction
zeros := len(digits) - len(strings.TrimRight(digits, "0"))
if strings.Trim(digits, "-0") != "" && exponent < len(fraction)-zeros {
return false
}
case prefix == "price_info" && (match("price") || match("price_usd")):
if value[0] != '-' && (value[0] < '0' || value[0] > '9') {
return false
}
case prefix == "" && (match("gpu") || match("price_info")):
if !validRunnerFields(value, key) {
return false
}
default:
continue
}
if name != key {
return false
}
}
return true
}

func (r *discoveryRunner) UnmarshalJSON(data []byte) error {
if !validRunnerFields(data, "") {
return errors.New("invalid discovery runner fields")
}
type identity discoveryRunner
var value identity
if err := json.Unmarshal(data, &value); err != nil {
if err := jsonv2.Unmarshal(data, &value); err != nil {
return err
}
*r = discoveryRunner(value)
Expand Down
1 change: 1 addition & 0 deletions discovery/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ func TestDiscoveryPreservesWholeRecords(t *testing.T) {
case "/tie/discovery":
runner = strings.ReplaceAll(runner, `"app":"test"`, `"app":"tie"`)
}
runner += `,{"url":"https://invalid.example/app","app":"test","observed_at":"2000-01-01T00:00:00Z","price_info":{"price":"1"}}`
w.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(w, `[{"address":"`+owner+`","runners":[`+runner+`]}]`)
}))
Expand Down
2 changes: 2 additions & 0 deletions docs/http-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ with no `address`:

Runner records represent ready apps and carry mode, capacity, prices, and optional
metadata/GPU fields.
Known fields must use the documented casing and types. Discovery skips malformed
records, including duplicate keys and invalid Unicode.
Discovery preserves complete runner records. `nodes` lists successfully queried,
non-evicted sources, and `queried_at` records each source's last successful query
time. Empty node and runner lists are omitted. With neither to advertise, discovery
Expand Down
7 changes: 7 additions & 0 deletions signer/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func TestSignerDiscovery(t *testing.T) {
const advertised = `[{"address":"http://localhost:9000","runners":[{"url":"http://localhost:9000/app","observed_at":"2026-01-01T00:00:00Z","app":"retained","version":"v1","metadata":"model-x","mode":"single-shot","gpu":{"id":"0","name":" H100 ","vram_mb":80000},"capacity":3,"capacity_used":1,"capacity_available":2,"price_info":{"price":10,"price_usd":0.1,"currency":"wei","unit":"fixed"}},{"url":"http://localhost:9000/cpu","observed_at":"2026-01-01T00:00:00Z","app":"cpu","mode":"single-shot","capacity":1,"price_info":{"price":1,"currency":"wei","unit":"fixed"}}]}]`
bodies := map[string]string{
"empty": "[]", "good": advertised, "failed": "", "malformed": "not JSON",
"lenient": strings.NewReplacer(
`"price":10,`, `"price":"10",`,
`"price_usd":0.1,`, `"price_usd":"0.1",`,
`"price":1,`, `"price":"1","price_usd":null,`,
`"app":"cpu",`, `"app":"cpu","version":null,"metadata":null,"gpu":null,"capacity_used":null,"capacity_available":null,`,
).Replace(advertised),
"metadata": `[{"nodes":[]}]`,
"truncated": `[{"address":`, "oversized": `[{"address":"` + strings.Repeat("a", 1<<20) + `"}]`,
"invalid": `[{"address":"file:///invalid","runners":[{"url":"http://localhost/app","app":"example"}]},{"address":"https://orch.example","runners":[{"url":"http://localhost/app"},{"app":"example"}]}]`,
Expand Down Expand Up @@ -98,6 +104,7 @@ func TestSignerDiscovery(t *testing.T) {
{[]string{"truncated"}, "", 503, nil}, {[]string{"oversized"}, "", 503, nil},
{[]string{"denied", "failed", "malformed", "good"}, "", 200, []string{"retained", "cpu"}},
{[]string{"collected"}, "", 200, []string{"retained", "cpu"}},
{[]string{"lenient"}, "", 200, []string{"retained", "cpu"}},
{[]string{"good"}, "app=missing&app=retained&gpu=L40S&gpu=H100", 200, []string{"retained"}},
{[]string{"good"}, "app=cpu", 200, []string{"cpu"}},
{[]string{"good"}, "app=retained&gpu=L40S", 200, nil},
Expand Down
Loading