clawchain/offchain/settlement/store_signer_test.go
caelum0x 5ba72d4fe0 feat(offchain): Apache-2.0 LICENSE + off-chain inference-settlement dispute/slash SaaS
Add the missing repository LICENSE (Apache-2.0, matching the Cosmos SDK /
CometBFT dependency licensing) and note it in the README.

Build the off-chain, chain-independent dispute/slash SaaS surface
(claw-settlement) that exposes ClawChain's inference-settlement +
dispute/resolution lifecycle without requiring the chain, validators, keys,
or moving funds. It is a faithful off-chain port of the on-chain domain logic:

- Settlement math ported from x/modelregistry Keeper.CompleteInferenceJob
  (payout = min(max(perQuery, perToken*tokens), escrow); refund = escrow-payout).
- Dispute/slash/resolve status machine ported from DisputeInferenceJob /
  ResolveInferenceDispute, reusing types.DisputeReputationPenalty.
- Saturating reputation slash/restore ported from x/reputation (floor 0,
  ceiling 10000 bps).

Surface:
- HTTP API: submit claim, open dispute, resolve dispute, get claim, get fee
  ledger, publish signer pubkey.
- Real persistence via embedded bbolt (already vendored; pure Go, no new deps).
- Metered: configurable per-settlement / per-dispute fees accrued to a real
  per-account fee ledger the SaaS bills against (FeeHook interface).
- Verifiable outcomes: every transition is ed25519-signed over canonical bytes
  and independently verifiable with only the service public key.
- Idempotency keys give exactly-once semantics (no double-slash / double-charge).

Tests (real bbolt, no mocks): full settle->dispute->resolve lifecycle for both
upheld and rejected resolutions, deterministic byte-identical signed outcomes,
idempotent replays, fee metering, authorization/state guards, tamper detection,
store edge cases, and HTTP error branches. 83.2% coverage; go build/vet and
race tests pass.
2026-08-26 21:29:57 +03:00

124 lines
3.8 KiB
Go

package settlement
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
)
func TestBoltStoreEdgeCases(t *testing.T) {
ctx := context.Background()
store, err := NewBoltStore(filepath.Join(t.TempDir(), "edge.db"))
if err != nil {
t.Fatalf("open: %v", err)
}
defer store.Close()
// Missing claim.
if _, err := store.GetClaim(ctx, "missing"); err != ErrClaimNotFound {
t.Fatalf("want ErrClaimNotFound, got %v", err)
}
// Unknown provider defaults to max reputation.
if rep, err := store.Reputation(ctx, "unknown"); err != nil || rep != MaxReputationBps {
t.Fatalf("unknown provider rep = %d (%v), want %d", rep, err, MaxReputationBps)
}
// Empty-key idempotency is a no-op.
if o, err := store.GetIdempotent(ctx, ""); err != nil || o != nil {
t.Fatalf("empty idem key must be nil no-op, got %v %v", o, err)
}
if err := store.PutIdempotent(ctx, "", &Outcome{}); err != nil {
t.Fatalf("empty idem put must no-op, got %v", err)
}
// Missing idempotency key returns nil.
if o, err := store.GetIdempotent(ctx, "never"); err != nil || o != nil {
t.Fatalf("missing idem must be nil, got %v %v", o, err)
}
// Fee ledger accumulates and rejects bad amounts.
if _, err := store.AddFee(ctx, "acct", "ufee", "not-a-number"); err == nil {
t.Fatalf("AddFee must reject non-integer amount")
}
l1, err := store.AddFee(ctx, "acct", "ufee", "100")
if err != nil || l1.Count != 1 || l1.Total != "100" {
t.Fatalf("first fee wrong: %+v (%v)", l1, err)
}
l2, err := store.AddFee(ctx, "acct", "ufee", "50")
if err != nil || l2.Count != 2 || l2.Total != "150" {
t.Fatalf("second fee wrong: %+v (%v)", l2, err)
}
// Empty account fee ledger is zeroed, not an error.
empty, err := store.GetFee(ctx, "nobody")
if err != nil || empty.Total != "0" || empty.Count != 0 {
t.Fatalf("empty ledger wrong: %+v (%v)", empty, err)
}
}
func TestSignerErrors(t *testing.T) {
if _, err := NewSigner([]byte("too-short")); err == nil {
t.Fatalf("NewSigner must reject short seed")
}
signer, err := NewSigner(testSeed())
if err != nil {
t.Fatalf("signer: %v", err)
}
claim := &Claim{ID: "x", Status: StatusSettled}
out, err := signer.Sign(EventSettled, claim, 1)
if err != nil {
t.Fatalf("sign: %v", err)
}
if err := VerifyOutcome(out); err != nil {
t.Fatalf("valid outcome must verify: %v", err)
}
// Bad public key.
bad := *out
bad.PublicKey = "!!!not-base64!!!"
if err := VerifyOutcome(&bad); err == nil {
t.Fatalf("bad pubkey must fail verification")
}
// Bad signature encoding.
bad2 := *out
bad2.Signature = "!!!not-base64!!!"
if err := VerifyOutcome(&bad2); err == nil {
t.Fatalf("bad signature encoding must fail verification")
}
// Wrong digest.
bad3 := *out
bad3.Digest = "deadbeef"
if err := VerifyOutcome(&bad3); err == nil {
t.Fatalf("wrong digest must fail verification")
}
}
func TestServiceErrorBranches(t *testing.T) {
svc := newTestService(t)
h := svc.Handler()
// Wrong method on POST endpoints.
for _, path := range []string{"/v1/claims", "/v1/disputes", "/v1/resolutions"} {
req := httptest.NewRequest(http.MethodGet, path, nil)
rr := httptest.NewRecorder()
h.ServeHTTP(rr, req)
if rr.Code != http.StatusMethodNotAllowed {
t.Fatalf("%s GET want 405, got %d", path, rr.Code)
}
}
// Wrong method on GET endpoints.
for _, path := range []string{"/v1/fees/x"} {
req := httptest.NewRequest(http.MethodPost, path, nil)
rr := httptest.NewRecorder()
h.ServeHTTP(rr, req)
if rr.Code != http.StatusMethodNotAllowed {
t.Fatalf("%s POST want 405, got %d", path, rr.Code)
}
}
// Bad JSON body.
req := httptest.NewRequest(http.MethodPost, "/v1/claims", bytes.NewBufferString("{not json"))
rr := httptest.NewRecorder()
h.ServeHTTP(rr, req)
if rr.Code != http.StatusBadRequest {
t.Fatalf("bad json want 400, got %d", rr.Code)
}
}