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

46 lines
2.0 KiB
Go

package settlement
import "context"
// FeeLedger is the accrued, billable fee total for one account. It is the
// metering record the SaaS bills against.
type FeeLedger struct {
Account string `json:"account"`
Denom string `json:"denom"`
Total string `json:"total"` // math.Int as string
Count uint64 `json:"count"` // number of chargeable events
}
// Store is the persistence boundary for the settlement service. It is
// deliberately small and storage-agnostic; the production implementation is a
// real embedded bbolt database (see boltstore.go). All methods must be safe for
// concurrent use.
type Store interface {
// GetClaim returns the claim by ID, or (nil, ErrClaimNotFound).
GetClaim(ctx context.Context, id string) (*Claim, error)
// PutClaim persists a claim (create or overwrite).
PutClaim(ctx context.Context, c *Claim) error
// Reputation returns a provider's current reputation in bps, defaulting to
// MaxReputationBps for a provider never seen before (fresh providers start
// at the ceiling, matching on-chain semantics for a full-score agent).
Reputation(ctx context.Context, provider string) (uint64, error)
// SetReputation persists a provider reputation score.
SetReputation(ctx context.Context, provider string, bps uint64) error
// GetIdempotent returns a previously stored outcome for an idempotency key,
// or (nil, nil) if none exists. This gives exactly-once semantics for
// slashing and fee charges across client retries.
GetIdempotent(ctx context.Context, key string) (*Outcome, error)
// PutIdempotent stores the outcome produced for an idempotency key.
PutIdempotent(ctx context.Context, key string, o *Outcome) error
// AddFee atomically accrues a fee for an account and returns the new ledger.
AddFee(ctx context.Context, account, denom, amount string) (*FeeLedger, error)
// GetFee returns the accrued fee ledger for an account (zeroed if none).
GetFee(ctx context.Context, account string) (*FeeLedger, error)
// Close releases resources.
Close() error
}