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

70 lines
2.1 KiB
Go

package settlement
import "cosmossdk.io/math"
// ComputeSettlement is a faithful, chain-independent port of the cost split in
// Keeper.CompleteInferenceJob (x/modelregistry/keeper/msg_server_inference.go).
//
// actualCost = min( max(pricePerQuery, pricePerToken * tokensUsed), escrow )
// payout = actualCost (paid to provider)
// refund = escrow - actualCost (returned to requester)
//
// The computation is pure and deterministic: identical inputs always yield
// identical outputs, which is what lets the off-chain outcome be signed and
// independently verified.
func ComputeSettlement(p Pricing, escrow math.Int, tokensUsed uint64) (payout, refund math.Int) {
perToken := orZero(p.PricePerToken)
perQuery := orZero(p.PricePerQuery)
esc := orZero(escrow)
// Apply the on-chain max-tokens cap before charging.
effectiveTokens := tokensUsed
if p.MaxTokens > 0 && effectiveTokens > p.MaxTokens {
effectiveTokens = p.MaxTokens
}
tokenCost := perToken.MulRaw(int64(effectiveTokens))
actualCost := perQuery
if tokenCost.GT(actualCost) {
actualCost = tokenCost
}
// Never charge more than was escrowed.
if actualCost.GT(esc) {
actualCost = esc
}
payout = actualCost
refund = esc.Sub(actualCost)
return payout, refund
}
// SlashReputation is a faithful port of x/reputation Keeper.SlashReputation:
// saturating subtraction that never underflows below 0. Slashing an unknown /
// already-zero score is a no-op that returns 0.
func SlashReputation(score, points uint64) uint64 {
if score > points {
return score - points
}
return 0
}
// RestoreReputation is a faithful port of x/reputation Keeper.RestoreReputation:
// saturating addition capped at MaxReputationBps. It is the symmetric undo of
// SlashReputation, applied when a dispute is rejected.
func RestoreReputation(score, points uint64) uint64 {
next := score + points
if next > MaxReputationBps {
return MaxReputationBps
}
return next
}
// orZero returns math.ZeroInt() for a nil (uninitialized) math.Int, mirroring
// the on-chain keeper's tolerance of unset numeric fields.
func orZero(v math.Int) math.Int {
if v.IsNil() {
return math.ZeroInt()
}
return v
}