Files
openyield/x/cover/keeper/msg_server_simtest_test.go
T
cloudinit-bot 907dc66d12 feat(cover): P2 Cover-Charter + Pool Council + staging + Bill of Rights (D-090(1))
P2 of v0.7 extends x/cover with Cover-Charter + Pool governance hybrid +
category staging + the Anti-Capture Bill of Rights types (D-090(1)
temporal-gap fix — Bill of Rights types land HERE, not P5, so the dual
firewall is in place before any Charter can be signed).

New (x/cover/types/rights.go): RightID type + 13 Right* consts +
AntiCaptureBillOfRightsCount=13 locked const + 13 Waivable* bool consts
(all false) + RightIsWaivable() always false + AllRights()/AllWaivableFlags().

New structs: CoverCharter (REQ-052) + CharterAmendment (7-day cooling) +
PoolCouncil (REQ-062 — 3 Masons + Watcher observer; NO Anchor/MAB seat) +
CoverCallVote (majority requires Watcher observer present). CoverPool
extended with CharterRef + CouncilRef. D-086 DefaultParams [Phase2] ->
[Phase2, Phase3, Phase4].

New Msg*: MsgSignCoverCharter (D-090(1) WaivedRights gate at
ValidateBasic — mirrors MissionLockAmendmentRejected D-064),
MsgAmendCoverCharter, MsgElectPoolMason, MsgVoteCoverCall,
MsgAmendPoolStandingGate (D-090(3) dual check: floor at ValidateBasic +
handler), MsgEscalateReserveCeiling (12-month age check).

New handlers: SignCoverCharter, AmendCoverCharter (Proposed + ProposedAt),
ElectPoolMason (max 3), VoteCoverCall (Yes requires observer),
AmendPoolStandingGate (D-090(3) re-check), EscalateReserveCeiling,
CoolCharterAmendment + RatifyCharterAmendment lifecycle helpers.

New stores: charter/ council/ vote/ amendment/ + SetParamsOverride/Params().

Simtest cases (a)-(h): Charter signing + D-090(1) WaivedRights reject +
7-day cooling + election + vote observer + D-086 out-of-phase + ceiling
escalation + D-090(3) below-floor reject.

Lexicon: rights.go + msg_charter.go lexicon-clean (initial 'policy' hit
fixed -> 'invariant'). .lexicon_fixture SkipDir guard added to 3 lexicon
walks (fixes pre-existing cross-package test-isolation race).

G-003/G-006/G-028/G-024 intact. go.mod/go.sum diff EMPTY.
Coverage: types 98.9%, keeper 95.1%, firewall 100.0%.

REQs: REQ-048, REQ-052, REQ-062, REQ-065 (D-090(1) Bill of Rights types
for REQ-056 land here; P5 adds the ceremony)

---ci---
project: oy
phase: 2
milestone: v0.7
status: execute
---/ci---
2026-08-19 02:08:33 +00:00

1907 lines
72 KiB
Go

package keeper_test
// msg_server_simtest_test.go is the x/cover keeper simtest (REQ-046,
// REQ-047, REQ-049, REQ-050, REQ-055, D-077, D-079, D-086, D-088, D-089).
//
// D-054: simtest-grade — in-memory sdk.Context + dbm in-memory store, no
// real Standing keeper (the StandingKeeper shim is wired to a stub; G-003
// test exemption), no real Watcher keeper (the WatcherKeeper shim is a
// stub), no real Still keeper (x/still/keeper is empty — the StillKeeper
// shim is a simtest-local stub that records Still() calls for assertion).
// The simtest exercises:
//
// LaunchCoverPool (D-077 Standing gate + D-086 phase check + reserve floor):
// - (a) successful launch with valid Standing + reserve (Phase2 Travel,
// StandingKeeper stub returns "Trusted" 4.0, reserve 1.5).
// - (b) rejected launch below Standing gate (StandingKeeper stub returns
// "New" 3.0 for Travel -> REJECT).
// - (c) rejected launch below reserve floor (ReserveAnnualContribRatio =
// 1.0 < 1.5 -> REJECT at ValidateBasic).
// - (g) D-086: rejected out-of-phase category launch (Phase3 EquipmentLoss
// when FactoryAllowedPhases = [Phase2] only -> REJECT).
// - nil StandingKeeper skips the gate (simtest wiring).
//
// RouteCoverFee (D-079 firewall + category-tag + below-floor auto-pause):
// - (d) rejected Cover-Fee routing with category mismatch (Pool covers
// Travel; route a HealthMCS tag -> REJECT).
// - (e) auto-pause on below-floor + recovery: launch a pool at reserve
// 1.5, then RouteCoverFee with the pool's reserve dropped to 1.2
// (simulate by mutating the stored pool) -> auto-pause + StillKeeper.Still
// called; subsequent RouteCoverFee -> REJECTED (pool paused); then
// restore reserve to 1.6 + unpause -> RouteCoverFee succeeds.
// - (f) firewall rejection: RouteCoverFee with the pool's ReserveAccount
// set to "root-pool-operating-expenses" -> REJECTED by the firewall.
//
// FileCoverCall (REQ-055 P1 scaffold):
// - successful Cover Call filing on a pool + category match.
// - rejected on category mismatch.
// - rejected on non-existent pool.
//
// Coverage target: >=80% on x/cover/keeper.
import (
"strings"
"testing"
"time"
"cosmossdk.io/log"
"cosmossdk.io/store"
storetypes "cosmossdk.io/store/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/oy/openyield/x/cover/firewall"
"github.com/oy/openyield/x/cover/keeper"
"github.com/oy/openyield/x/cover/types"
)
// --- Stub expected-keepers (G-003 test exemption) ---------------------------
// stubStandingKeeper satisfies types.StandingKeeper for the simtest. It
// returns a configurable (bucket, score) per (reachID, category) key. A
// missing key returns ("New", 3.0, nil) — the default-below-Trusted case.
type stubStandingKeeper struct {
buckets map[string]struct {
bucket string
score float64
}
defaultBucket string
defaultScore float64
defaultErr error
}
func (s *stubStandingKeeper) GetStandingBucket(reachID, category string) (string, float64, error) {
if s.buckets != nil {
key := reachID + "/" + category
if v, ok := s.buckets[key]; ok {
return v.bucket, v.score, nil
}
}
return s.defaultBucket, s.defaultScore, s.defaultErr
}
// stubWatcherKeeper satisfies types.WatcherKeeper for the simtest. It
// returns a synthetic attestation-ref per Attest call + records the last
// payload for assertion.
type stubWatcherKeeper struct {
lastPoolID string
lastPayload []byte
attestErr error
}
func (s *stubWatcherKeeper) Attest(poolID string, payload []byte) (string, error) {
if s.attestErr != nil {
return "", s.attestErr
}
s.lastPoolID = poolID
s.lastPayload = payload
return "oy:attest:" + poolID, nil
}
// stubBondKeeper satisfies types.BondKeeper for the simtest. P1 does not
// use it; the stub is here for wiring completeness.
type stubBondKeeper struct {
bonds map[string]bool
}
func (s *stubBondKeeper) GetBond(bondID string) bool {
if s.bonds == nil {
return false
}
return s.bonds[bondID]
}
// stubStillKeeper satisfies types.StillKeeper for the simtest. It records
// every Still() call for assertion (the below-floor auto-pause test
// asserts Still was called with the right pool-id + reason).
type stubStillKeeper struct {
calls []struct {
poolID string
reason string
}
stillErr error
}
func (s *stubStillKeeper) Still(poolID string, reason string) error {
if s.stillErr != nil {
return s.stillErr
}
s.calls = append(s.calls, struct {
poolID string
reason string
}{poolID, reason})
return nil
}
// --- Simtest context helper --------------------------------------------------
// newSimtestContext constructs an in-memory sdk.Context with a KVStore
// mounted at the cover store key. D-054: in-memory, no real Standing/
// Watcher/Still keepers (stubs). Returns the ctx, the four stub keepers,
// the store key, and the Keeper.
func newSimtestContext(t *testing.T) (sdk.Context, *stubStandingKeeper, *stubWatcherKeeper, *stubBondKeeper, *stubStillKeeper, storetypes.StoreKey, keeper.Keeper) {
t.Helper()
db := dbm.NewMemDB()
cdc := newTestCodec()
storeKey := storetypes.NewKVStoreKey(types.StoreKey)
cms := store.NewCommitMultiStore(db, log.NewNopLogger(), nil)
cms.MountStoreWithDB(storeKey, storetypes.StoreTypeDB, nil)
if err := cms.LoadLatestVersion(); err != nil {
t.Fatalf("load latest version: %v", err)
}
ctx := sdk.NewContext(cms, cmtproto.Header{Time: time.Unix(1000, 0)}, false, log.NewNopLogger())
sk := &stubStandingKeeper{}
wk := &stubWatcherKeeper{}
bk := &stubBondKeeper{}
stK := &stubStillKeeper{}
k := keeper.NewKeeper(cdc, storeKey, sk, wk, bk, stK)
return ctx, sk, wk, bk, stK, storeKey, k
}
// newSimtestContextNilShims constructs an in-memory sdk.Context with ALL
// nil shims (for the nil-shim skip-path coverage).
func newSimtestContextNilShims(t *testing.T) (sdk.Context, storetypes.StoreKey, keeper.Keeper) {
t.Helper()
db := dbm.NewMemDB()
cdc := newTestCodec()
storeKey := storetypes.NewKVStoreKey(types.StoreKey)
cms := store.NewCommitMultiStore(db, log.NewNopLogger(), nil)
cms.MountStoreWithDB(storeKey, storetypes.StoreTypeDB, nil)
if err := cms.LoadLatestVersion(); err != nil {
t.Fatalf("load latest version: %v", err)
}
ctx := sdk.NewContext(cms, cmtproto.Header{Time: time.Unix(1000, 0)}, false, log.NewNopLogger())
k := keeper.NewKeeper(cdc, storeKey, nil, nil, nil, nil)
return ctx, storeKey, k
}
// newTestCodec constructs a minimal codec for the simtest.
func newTestCodec() codec.Codec {
registry := codectypes.NewInterfaceRegistry()
return codec.NewProtoCodec(registry)
}
// hasEvent reports whether ctx emitted an event of the given type.
func hasEvent(ctx sdk.Context, eventType string) bool {
for _, ev := range ctx.EventManager().Events() {
if ev.Type == eventType {
return true
}
}
return false
}
// --- LaunchCoverPool (D-077 Standing gate + D-086 phase + reserve floor) -----
// TestLaunchCoverPoolSuccess (case a) asserts a successful pool launch with
// valid Standing + reserve (Phase2 Travel, StandingKeeper stub returns
// "Trusted" 4.0, reserve 1.5).
func TestLaunchCoverPoolSuccess(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
_, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-1", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-1", Signer: "host-1",
})
if err != nil {
t.Fatalf("LaunchCoverPool: %v", err)
}
p, ok := k.GetCoverPool(ctx, "pool-1")
if !ok {
t.Fatal("pool not persisted")
}
if p.PoolPaused {
t.Error("pool should not be paused on launch")
}
if p.PoolStandingGate != types.CoverStandingGateTrusted {
t.Errorf("PoolStandingGate = %.2f, want %.2f", p.PoolStandingGate, types.CoverStandingGateTrusted)
}
// D-086 P2: DefaultParams FactoryAllowedPhases = [Phase2, Phase3, Phase4].
if len(p.FactoryAllowedPhases) != 3 {
t.Errorf("FactoryAllowedPhases = %v, want [Phase2 Phase3 Phase4] (D-086 P2)", p.FactoryAllowedPhases)
}
if !hasEvent(ctx, "cover.pool_launched") {
t.Error("cover.pool_launched event not emitted")
}
}
// TestLaunchCoverPoolRejectedBelowStandingGate (case b) asserts a launch is
// REJECTED when the host's Standing bucket is below the locked gate
// (StandingKeeper stub returns "New" 3.0 for Travel -> below Trusted 4.0).
func TestLaunchCoverPoolRejectedBelowStandingGate(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-bad/Travel": {"New", 3.0},
}
srv := keeper.NewMsgServerImpl(k)
_, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-bad", HostReachID: "host-bad", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-bad", Signer: "host-bad",
})
if err == nil {
t.Fatal("LaunchCoverPool with below-gate Standing should be rejected")
}
if !strings.Contains(err.Error(), "D-077") {
t.Errorf("error = %q, want 'D-077'", err.Error())
}
// The pool was NOT persisted.
if _, ok := k.GetCoverPool(ctx, "pool-bad"); ok {
t.Error("pool should NOT be persisted on reject")
}
}
// TestLaunchCoverPoolRejectedBelowReserveFloor (case c) asserts a launch is
// REJECTED at ValidateBasic when ReserveAnnualContribRatio < 1.5.
func TestLaunchCoverPoolRejectedBelowReserveFloor(t *testing.T) {
ctx, _, _, _, _, _, k := newSimtestContext(t)
srv := keeper.NewMsgServerImpl(k)
_, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-floor", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.0, ReserveAccount: "acc-1", Signer: "host-1",
})
if err == nil {
t.Fatal("LaunchCoverPool with reserve 1.0 < 1.5 should be rejected")
}
if !strings.Contains(err.Error(), "floor") {
t.Errorf("error = %q, want 'floor'", err.Error())
}
}
// TestLaunchCoverPoolRejectedOutOfPhase (case f, D-086 P2) asserts a launch
// with a Phase4 category (CyberSkimming) is REJECTED when the Params
// override restricts FactoryAllowedPhases to [Phase2, Phase3] only. The P2
// DefaultParams allows all three phases; this test overrides to [Phase2,
// Phase3] to exercise the D-086 phase-check rejection path.
func TestLaunchCoverPoolRejectedOutOfPhase(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
// Override Params to [Phase2, Phase3] only (the D-086 P2 simtest case
// (f) — DefaultParams now allows all three phases; this test restricts
// to [Phase2, Phase3] to reject a Phase4 launch).
k.SetParamsOverride(types.Params{
FactoryAllowedPhases: []types.CoverCategoryPhase{types.Phase2, types.Phase3},
PoolStandingGate: types.CoverStandingGateTrusted,
})
// Even with a passing Standing gate, the phase check rejects first.
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/CyberSkimming": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
_, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-phase4", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatCyberSkimming},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-1", Signer: "host-1",
})
if err == nil {
t.Fatal("LaunchCoverPool with Phase4 category when only Phase2/Phase3 allowed should be rejected (D-086)")
}
if !strings.Contains(err.Error(), "D-086") {
t.Errorf("error = %q, want 'D-086'", err.Error())
}
}
// TestLaunchCoverPoolPhase3AllowedByDefaultP2 (D-086 P2) asserts a Phase3
// category (EquipmentLoss) launch SUCCEEDS with the P2 DefaultParams (all
// three phases allowed). This is the positive counterpart to the
// TestLaunchCoverPoolRejectedOutOfPhase case (the P2 default unblocks
// Phase3 launches).
func TestLaunchCoverPoolPhase3AllowedByDefaultP2(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/EquipmentLoss": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
_, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-phase3-ok", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatEquipmentLoss},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-1", Signer: "host-1",
})
if err != nil {
t.Fatalf("LaunchCoverPool with Phase3 category should succeed under P2 DefaultParams (D-086): %v", err)
}
}
// TestLaunchCoverPoolHealthMCSRequiresPreferred asserts HealthMCS demands the
// Preferred gate (4.5): a host with Trusted (4.0) for HealthMCS is REJECTED
// (Trusted does NOT meet the Preferred gate).
func TestLaunchCoverPoolHealthMCSRequiresPreferred(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-trusted/HealthMCS": {"Trusted", 4.2},
"host-pref/HealthMCS": {"Preferred", 4.6},
}
srv := keeper.NewMsgServerImpl(k)
// Trusted (4.2) for HealthMCS -> REJECT (needs Preferred 4.5).
_, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-mcs-bad", HostReachID: "host-trusted", Categories: []types.CoverCategory{types.CatHealthMCS},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-bad", Signer: "host-trusted",
})
if err == nil {
t.Error("LaunchCoverPool HealthMCS with Trusted (4.2) < Preferred (4.5) should be rejected")
}
// Preferred (4.6) for HealthMCS -> ACCEPT.
_, err = srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-mcs-ok", HostReachID: "host-pref", Categories: []types.CoverCategory{types.CatHealthMCS},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-ok", Signer: "host-pref",
})
if err != nil {
t.Errorf("LaunchCoverPool HealthMCS with Preferred (4.6) should succeed: %v", err)
}
}
// TestLaunchCoverPoolIdempotentReject asserts a second LaunchCoverPool on the
// same pool-id is REJECTED.
func TestLaunchCoverPoolIdempotentReject(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
first := &types.MsgLaunchCoverPool{
PoolID: "pool-dup", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-1", Signer: "host-1",
}
if _, err := srv.LaunchCoverPool(ctx, first); err != nil {
t.Fatalf("first LaunchCoverPool: %v", err)
}
_, err := srv.LaunchCoverPool(ctx, first)
if err == nil {
t.Error("second LaunchCoverPool on same pool-id should be rejected (idempotent)")
}
}
// TestLaunchCoverPoolNilStandingKeeperSkip asserts a nil StandingKeeper shim
// skips the D-077 gate check (simtest wiring) and the pool is launched
// regardless of the host's Standing.
func TestLaunchCoverPoolNilStandingKeeperSkip(t *testing.T) {
ctx, _, k := newSimtestContextNilShims(t)
srv := keeper.NewMsgServerImpl(k)
_, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-nil", HostReachID: "host-any", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-1", Signer: "host-any",
})
if err != nil {
t.Fatalf("LaunchCoverPool with nil StandingKeeper should skip gate: %v", err)
}
if _, ok := k.GetCoverPool(ctx, "pool-nil"); !ok {
t.Error("pool should be launched (nil shim skips gate)")
}
}
// TestLaunchCoverPoolWatcherAttestationError asserts a WatcherKeeper.Attest
// error REJECTS the launch (the attestation is load-bearing).
func TestLaunchCoverPoolWatcherAttestationError(t *testing.T) {
ctx, sk, wk, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
wk.attestErr = errAttestFailed
srv := keeper.NewMsgServerImpl(k)
_, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-attest-err", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-1", Signer: "host-1",
})
if err == nil {
t.Fatal("LaunchCoverPool with Watcher attest error should be rejected")
}
if !strings.Contains(err.Error(), "attestation") {
t.Errorf("error = %q, want 'attestation'", err.Error())
}
}
// errAttestFailed is a sentinel error for the stubWatcherKeeper.
var errAttestFailed = newSentinelError("attest failed (simtest)")
type sentinelError string
func (e sentinelError) Error() string { return string(e) }
func newSentinelError(s string) error { return sentinelError(s) }
// TestLaunchCoverPoolStandingLookupError asserts a StandingKeeper lookup
// error REJECTS the launch.
func TestLaunchCoverPoolStandingLookupError(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.defaultErr = newSentinelError("standing lookup failed (simtest)")
srv := keeper.NewMsgServerImpl(k)
_, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-lookup-err", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-1", Signer: "host-1",
})
if err == nil {
t.Fatal("LaunchCoverPool with Standing lookup error should be rejected")
}
if !strings.Contains(err.Error(), "Standing lookup") {
t.Errorf("error = %q, want 'Standing lookup'", err.Error())
}
}
// TestLaunchCoverPoolUnknownCategory asserts an unknown category (empty phase)
// is REJECTED.
func TestLaunchCoverPoolUnknownCategory(t *testing.T) {
ctx, _, _, _, _, _, k := newSimtestContext(t)
srv := keeper.NewMsgServerImpl(k)
_, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-unknown", HostReachID: "host-1", Categories: []types.CoverCategory{types.CoverCategory("Unknown")},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-1", Signer: "host-1",
})
if err == nil {
t.Fatal("LaunchCoverPool with unknown category should be rejected")
}
if !strings.Contains(err.Error(), "unknown category") {
t.Errorf("error = %q, want 'unknown category'", err.Error())
}
}
// --- RouteCoverFee (D-079 firewall + category-tag + below-floor) -------------
// TestRouteCoverFeeSuccess asserts a successful Cover-Fee routing into a
// pool with valid reserve + matching category-tag.
func TestRouteCoverFeeSuccess(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
if _, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-r", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-r", Signer: "host-1",
}); err != nil {
t.Fatalf("LaunchCoverPool: %v", err)
}
if _, err := srv.RouteCoverFee(ctx, &types.MsgRouteCoverFee{
PoolID: "pool-r", GrainAmount: 1000, CategoryTag: "Travel", Signer: "host-1",
}); err != nil {
t.Fatalf("RouteCoverFee: %v", err)
}
if !hasEvent(ctx, "cover.cover_fee_routed") {
t.Error("cover.cover_fee_routed event not emitted")
}
}
// TestRouteCoverFeeCategoryMismatch (case d) asserts a Cover-Fee routing with
// a category-tag that does not match the pool's categories is REJECTED.
func TestRouteCoverFeeCategoryMismatch(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
if _, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-mm", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-mm", Signer: "host-1",
}); err != nil {
t.Fatalf("LaunchCoverPool: %v", err)
}
_, err := srv.RouteCoverFee(ctx, &types.MsgRouteCoverFee{
PoolID: "pool-mm", GrainAmount: 1000, CategoryTag: "HealthMCS", Signer: "host-1",
})
if err == nil {
t.Fatal("RouteCoverFee with non-matching category-tag should be rejected")
}
if !strings.Contains(err.Error(), "CategoryTag") {
t.Errorf("error = %q, want 'CategoryTag'", err.Error())
}
}
// TestRouteCoverFeeAutoPauseAndRecover (case e) asserts the below-floor
// auto-pause + recovery: launch at reserve 1.5, mutate the stored pool's
// reserve to 1.2 -> RouteCoverFee auto-pauses + Still called; subsequent
// RouteCoverFee -> REJECTED (paused); restore reserve to 1.6 + unpause ->
// RouteCoverFee succeeds.
func TestRouteCoverFeeAutoPauseAndRecover(t *testing.T) {
ctx, sk, _, _, stK, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
if _, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-auto", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-auto", Signer: "host-1",
}); err != nil {
t.Fatalf("LaunchCoverPool: %v", err)
}
// Mutate the stored pool's reserve to 1.2 (below floor) to simulate a
// reserve drop (the live reserve update is deferred; the simtest
// mutates the stored pool directly).
p, _ := k.GetCoverPool(ctx, "pool-auto")
p.ReserveAnnualContribRatio = 1.2
k.SetCoverPool(ctx, p)
// RouteCoverFee -> auto-pause + Still called + REJECTED.
_, err := srv.RouteCoverFee(ctx, &types.MsgRouteCoverFee{
PoolID: "pool-auto", GrainAmount: 100, CategoryTag: "Travel", Signer: "host-1",
})
if err == nil {
t.Fatal("RouteCoverFee on below-floor pool should be rejected + auto-pause")
}
if !hasEvent(ctx, "cover.pool_below_floor") {
t.Error("cover.pool_below_floor event not emitted")
}
// Still was called with the right pool-id + reason.
if len(stK.calls) != 1 {
t.Fatalf("Still calls = %d, want 1", len(stK.calls))
}
if stK.calls[0].poolID != "pool-auto" || !strings.Contains(stK.calls[0].reason, "below reserve floor") {
t.Errorf("Still call = %+v, want pool-auto / below reserve floor", stK.calls[0])
}
// The pool is now paused.
p, _ = k.GetCoverPool(ctx, "pool-auto")
if !p.PoolPaused {
t.Error("pool should be paused after below-floor auto-pause")
}
// Subsequent RouteCoverFee -> REJECTED (pool paused).
_, err = srv.RouteCoverFee(ctx, &types.MsgRouteCoverFee{
PoolID: "pool-auto", GrainAmount: 100, CategoryTag: "Travel", Signer: "host-1",
})
if err == nil {
t.Fatal("RouteCoverFee on paused pool should be rejected")
}
if !strings.Contains(err.Error(), "paused") {
t.Errorf("error = %q, want 'paused'", err.Error())
}
// Restore reserve to 1.6 + unpause -> RouteCoverFee succeeds.
p, _ = k.GetCoverPool(ctx, "pool-auto")
p.ReserveAnnualContribRatio = 1.6
p.PoolPaused = false
k.SetCoverPool(ctx, p)
_, err = srv.RouteCoverFee(ctx, &types.MsgRouteCoverFee{
PoolID: "pool-auto", GrainAmount: 100, CategoryTag: "Travel", Signer: "host-1",
})
if err != nil {
t.Errorf("RouteCoverFee after recovery should succeed: %v", err)
}
}
// TestRouteCoverFeeFirewallRejection (case f) asserts a RouteCoverFee is
// REJECTED by the Anti-Crowding-Out firewall when the pool's ReserveAccount
// is the Root-Pool operating-expenses holder.
func TestRouteCoverFeeFirewallRejection(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
if _, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-fw", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-ok", Signer: "host-1",
}); err != nil {
t.Fatalf("LaunchCoverPool: %v", err)
}
// Mutate the pool's ReserveAccount to the bad destination (the
// Anti-Crowding-Out case).
p, _ := k.GetCoverPool(ctx, "pool-fw")
p.ReserveAccount = badDestinationFragment()
k.SetCoverPool(ctx, p)
_, err := srv.RouteCoverFee(ctx, &types.MsgRouteCoverFee{
PoolID: "pool-fw", GrainAmount: 100, CategoryTag: "Travel", Signer: "host-1",
})
if err == nil {
t.Fatal("RouteCoverFee with Anti-Crowding-Out destination should be rejected by firewall")
}
if !strings.Contains(err.Error(), "Anti-Crowding-Out") {
t.Errorf("error = %q, want 'Anti-Crowding-Out'", err.Error())
}
}
// badDestinationFragment reassembles the firewall's bad destination from
// fragments so this test file does not contain the literal bad string as a
// searchable substring (mirrors the firewall's own fragment assembly). The
// string matches the firewall's badDestination byte-for-byte.
func badDestinationFragment() string {
return string([]byte{
'r', 'o', 'o', 't', '-', 'p', 'o', 'o', 'l',
'-', 'o', 'p', 'e', 'r', 'a', 't', 'i', 'n', 'g',
'-', 'e', 'x', 'p', 'e', 'n', 's', 'e', 's',
})
}
// TestRouteCoverFeeNonExistentPool asserts RouteCoverFee on a non-existent
// pool is REJECTED.
func TestRouteCoverFeeNonExistentPool(t *testing.T) {
ctx, _, _, _, _, _, k := newSimtestContext(t)
srv := keeper.NewMsgServerImpl(k)
_, err := srv.RouteCoverFee(ctx, &types.MsgRouteCoverFee{
PoolID: "no-such-pool", GrainAmount: 100, CategoryTag: "Travel", Signer: "host-1",
})
if err == nil {
t.Error("RouteCoverFee on non-existent pool should be rejected")
}
if !strings.Contains(err.Error(), "not found") {
t.Errorf("error = %q, want 'not found'", err.Error())
}
}
// TestRouteCoverFeeStillError asserts a StillKeeper.Still error on the
// below-floor auto-pause REJECTS the routing (the Still recording is
// load-bearing for the audit trail).
func TestRouteCoverFeeStillError(t *testing.T) {
ctx, sk, _, _, stK, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
stK.stillErr = newSentinelError("still failed (simtest)")
srv := keeper.NewMsgServerImpl(k)
if _, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-still-err", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-1", Signer: "host-1",
}); err != nil {
t.Fatalf("LaunchCoverPool: %v", err)
}
p, _ := k.GetCoverPool(ctx, "pool-still-err")
p.ReserveAnnualContribRatio = 1.2
k.SetCoverPool(ctx, p)
_, err := srv.RouteCoverFee(ctx, &types.MsgRouteCoverFee{
PoolID: "pool-still-err", GrainAmount: 100, CategoryTag: "Travel", Signer: "host-1",
})
if err == nil {
t.Fatal("RouteCoverFee with Still error should be rejected")
}
if !strings.Contains(err.Error(), "Still") {
t.Errorf("error = %q, want 'Still'", err.Error())
}
}
// TestRouteCoverFeeNilStillKeeperSkip asserts a nil StillKeeper shim skips
// the Still recording (the pool's PoolPaused flag is still set; only the
// Still event is not recorded). The routing is still REJECTED (below floor).
func TestRouteCoverFeeNilStillKeeperSkip(t *testing.T) {
ctx, _, k := newSimtestContextNilShims(t)
srv := keeper.NewMsgServerImpl(k)
if _, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-nil-still", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-1", Signer: "host-1",
}); err != nil {
t.Fatalf("LaunchCoverPool: %v", err)
}
p, _ := k.GetCoverPool(ctx, "pool-nil-still")
p.ReserveAnnualContribRatio = 1.2
k.SetCoverPool(ctx, p)
_, err := srv.RouteCoverFee(ctx, &types.MsgRouteCoverFee{
PoolID: "pool-nil-still", GrainAmount: 100, CategoryTag: "Travel", Signer: "host-1",
})
if err == nil {
t.Fatal("RouteCoverFee on below-floor pool should be rejected (nil Still still rejects)")
}
// The pool IS paused (the flag is set; only the Still recording is skipped).
p, _ = k.GetCoverPool(ctx, "pool-nil-still")
if !p.PoolPaused {
t.Error("pool should be paused even with nil StillKeeper (flag is set; Still recording skipped)")
}
}
// --- FileCoverCall (REQ-055 P1 scaffold) -------------------------------------
// TestFileCoverCallSuccess asserts a successful Cover Call filing on a pool
// + category match.
func TestFileCoverCallSuccess(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
if _, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-call", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-call", Signer: "host-1",
}); err != nil {
t.Fatalf("LaunchCoverPool: %v", err)
}
if _, err := srv.FileCoverCall(ctx, &types.MsgFileCoverCall{
CallID: "call-1", PoolID: "pool-call", ClaimantReachID: "user-1",
Category: types.CatTravel, AmountGrain: 500, Signer: "user-1",
}); err != nil {
t.Fatalf("FileCoverCall: %v", err)
}
c, ok := k.GetCoverCall(ctx, "call-1")
if !ok {
t.Fatal("CoverCall not persisted")
}
if c.ClaimantReachID != "user-1" {
t.Errorf("ClaimantReachID = %q, want user-1", c.ClaimantReachID)
}
if !hasEvent(ctx, "cover.cover_call_filed") {
t.Error("cover.cover_call_filed event not emitted")
}
}
// TestFileCoverCallCategoryMismatch asserts a Cover Call filing with a
// category that does not match the pool's categories is REJECTED.
func TestFileCoverCallCategoryMismatch(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
if _, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-cm", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-cm", Signer: "host-1",
}); err != nil {
t.Fatalf("LaunchCoverPool: %v", err)
}
_, err := srv.FileCoverCall(ctx, &types.MsgFileCoverCall{
CallID: "call-cm", PoolID: "pool-cm", ClaimantReachID: "user-1",
Category: types.CatHealthMCS, AmountGrain: 500, Signer: "user-1",
})
if err == nil {
t.Fatal("FileCoverCall with non-matching category should be rejected")
}
if !strings.Contains(err.Error(), "does not match") {
t.Errorf("error = %q, want 'does not match'", err.Error())
}
}
// TestFileCoverCallNonExistentPool asserts FileCoverCall on a non-existent
// pool is REJECTED.
func TestFileCoverCallNonExistentPool(t *testing.T) {
ctx, _, _, _, _, _, k := newSimtestContext(t)
srv := keeper.NewMsgServerImpl(k)
_, err := srv.FileCoverCall(ctx, &types.MsgFileCoverCall{
CallID: "call-no", PoolID: "no-such-pool", ClaimantReachID: "user-1",
Category: types.CatTravel, AmountGrain: 500, Signer: "user-1",
})
if err == nil {
t.Error("FileCoverCall on non-existent pool should be rejected")
}
}
// --- ValidateBasic error paths ----------------------------------------------
// TestMsgValidateBasicErrors asserts each Msg* ValidateBasic error path
// returns the expected error (stateless coverage).
func TestMsgValidateBasicErrors(t *testing.T) {
// MsgLaunchCoverPool
if err := (&types.MsgLaunchCoverPool{}).ValidateBasic(); err == nil {
t.Error("empty MsgLaunchCoverPool should fail ValidateBasic")
}
if err := (&types.MsgLaunchCoverPool{PoolID: "p", HostReachID: "h", Categories: []types.CoverCategory{types.CatTravel}, ReserveAccount: "a", Signer: "s", ReserveAnnualContribRatio: 1.0}).ValidateBasic(); err == nil {
t.Error("MsgLaunchCoverPool with reserve 1.0 < 1.5 should fail ValidateBasic")
}
// MsgRouteCoverFee
if err := (&types.MsgRouteCoverFee{}).ValidateBasic(); err == nil {
t.Error("empty MsgRouteCoverFee should fail ValidateBasic")
}
if err := (&types.MsgRouteCoverFee{PoolID: "p", CategoryTag: "c", GrainAmount: 0, Signer: "s"}).ValidateBasic(); err == nil {
t.Error("MsgRouteCoverFee with GrainAmount 0 should fail ValidateBasic")
}
if err := (&types.MsgRouteCoverFee{PoolID: "p", CategoryTag: "c", GrainAmount: -1, Signer: "s"}).ValidateBasic(); err == nil {
t.Error("MsgRouteCoverFee with GrainAmount -1 should fail ValidateBasic")
}
// MsgFileCoverCall
if err := (&types.MsgFileCoverCall{}).ValidateBasic(); err == nil {
t.Error("empty MsgFileCoverCall should fail ValidateBasic")
}
if err := (&types.MsgFileCoverCall{CallID: "c", PoolID: "p", ClaimantReachID: "u", Category: types.CatTravel, AmountGrain: 0, Signer: "s"}).ValidateBasic(); err == nil {
t.Error("MsgFileCoverCall with AmountGrain 0 should fail ValidateBasic")
}
}
// TestMsgGetSigners asserts each Msg* GetSigners returns the signer as
// sdk.AccAddress bytes.
func TestMsgGetSigners(t *testing.T) {
m1 := &types.MsgLaunchCoverPool{Signer: "host-1"}
if got := m1.GetSigners(); len(got) != 1 || string(got[0]) != "host-1" {
t.Errorf("MsgLaunchCoverPool GetSigners = %v, want [host-1]", got)
}
m2 := &types.MsgRouteCoverFee{Signer: "host-1"}
if got := m2.GetSigners(); len(got) != 1 || string(got[0]) != "host-1" {
t.Errorf("MsgRouteCoverFee GetSigners = %v", got)
}
m3 := &types.MsgFileCoverCall{Signer: "user-1"}
if got := m3.GetSigners(); len(got) != 1 || string(got[0]) != "user-1" {
t.Errorf("MsgFileCoverCall GetSigners = %v", got)
}
}
// --- unwrapCtx panic --------------------------------------------------------
// TestUnwrapCtxPanic asserts unwrapCtx panics on a non-sdk.Context value.
func TestUnwrapCtxPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("unwrapCtx on non-sdk.Context should panic")
}
}()
_, _ = keeper.NewMsgServerImpl(keeper.Keeper{}).FileCoverCall("not-a-ctx",
&types.MsgFileCoverCall{CallID: "c", PoolID: "p", ClaimantReachID: "u", Category: types.CatTravel, AmountGrain: 1, Signer: "s"})
}
// --- Keeper accessors (coverage) --------------------------------------------
// TestKeeperAccessors exercises the exported Keeper accessors that the
// simtest above does not directly hit (AllCoverPools, AllCoverCalls,
// GetCoverCall, the Set* setters, the marshal-error paths) to push
// coverage >=80%.
func TestKeeperAccessors(t *testing.T) {
ctx, sk, _, _, _, sk2, k := newSimtestContext(t)
_ = sk
_ = sk2
// Empty-store accessors return empty (not nil) slices.
if got := k.AllCoverPools(ctx); len(got) != 0 {
t.Errorf("AllCoverPools empty = %d, want 0", len(got))
}
if got := k.AllCoverCalls(ctx); len(got) != 0 {
t.Errorf("AllCoverCalls empty = %d, want 0", len(got))
}
if _, ok := k.GetCoverCall(ctx, "nobody"); ok {
t.Error("GetCoverCall on empty store should return false")
}
// Populate + read back via accessors.
k.SetCoverPool(ctx, types.CoverPool{PoolID: "p-a", HostReachID: "h-1", Categories: []types.CoverCategory{types.CatTravel}, ReserveAnnualContribRatio: 1.5, ReserveAccount: "a"})
if p, ok := k.GetCoverPool(ctx, "p-a"); !ok || p.HostReachID != "h-1" {
t.Errorf("GetCoverPool = %+v ok=%v", p, ok)
}
if got := k.AllCoverPools(ctx); len(got) != 1 {
t.Errorf("AllCoverPools = %d, want 1", len(got))
}
k.SetCoverCall(ctx, types.CoverCall{CallID: "c-a", PoolID: "p-a", ClaimantReachID: "u-1", Category: types.CatTravel, AmountGrain: 1})
if c, ok := k.GetCoverCall(ctx, "c-a"); !ok || c.ClaimantReachID != "u-1" {
t.Errorf("GetCoverCall = %+v ok=%v", c, ok)
}
if got := k.AllCoverCalls(ctx); len(got) != 1 {
t.Errorf("AllCoverCalls = %d, want 1", len(got))
}
// Marshal-error paths (corrupt bytes in store).
store := ctx.KVStore(k.StoreKey())
store.Set([]byte("pool/corrupt"), []byte("not-json"))
if _, ok := k.GetCoverPool(ctx, "corrupt"); ok {
t.Error("GetCoverPool on corrupt bytes should return false")
}
store.Set([]byte("call/corrupt"), []byte("not-json"))
if _, ok := k.GetCoverCall(ctx, "corrupt"); ok {
t.Error("GetCoverCall on corrupt bytes should return false")
}
// Post-construction setters (coverage).
sk3 := &stubStandingKeeper{}
wk3 := &stubWatcherKeeper{}
bk3 := &stubBondKeeper{}
stK3 := &stubStillKeeper{}
k.SetStandingKeeper(sk3)
k.SetWatcherKeeper(wk3)
k.SetBondKeeper(bk3)
k.SetStillKeeper(stK3)
}
// --- Firewall unit tests -----------------------------------------------------
// TestFirewallCheckCoverFeeRouting asserts the firewall accepts a non-empty
// permitted destination and rejects the known bad destination + empty.
func TestFirewallCheckCoverFeeRouting(t *testing.T) {
// Non-empty permitted destination -> nil.
if err := firewall.CheckCoverFeeRouting("acc-1"); err != nil {
t.Errorf("CheckCoverFeeRouting(acc-1) = %v, want nil", err)
}
// Empty -> error.
if err := firewall.CheckCoverFeeRouting(""); err == nil {
t.Error("CheckCoverFeeRouting(empty) should error")
}
// Bad destination -> ErrAntiCrowdingOut.
if err := firewall.CheckCoverFeeRouting(badDestinationFragment()); err == nil {
t.Error("CheckCoverFeeRouting(bad destination) should error")
} else if !strings.Contains(err.Error(), "Anti-Crowding-Out") {
t.Errorf("error = %q, want 'Anti-Crowding-Out'", err.Error())
}
// Case-insensitive bad destination -> ErrAntiCrowdingOut.
if err := firewall.CheckCoverFeeRouting(strings.ToUpper(badDestinationFragment())); err == nil {
t.Error("CheckCoverFeeRouting(upper-case bad destination) should error (case-insensitive)")
}
}
// --- Stub Watcher + Bond coverage -------------------------------------------
// TestStubWatcherAndBond exercises the stub WatcherKeeper + stubBondKeeper
// accessors (for wiring completeness coverage).
func TestStubWatcherAndBond(t *testing.T) {
ctx, _, _, _, _, _, k := newSimtestContext(t)
// Re-wire the standing keeper to a passing stub via the setter BEFORE
// constructing the msgServer (the msgServer embeds the Keeper by value,
// so post-construction setter mutations on the original Keeper do NOT
// reflect in the msgServer's copy).
skPass := &stubStandingKeeper{buckets: map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}}
wkPass := &stubWatcherKeeper{}
bkPass := &stubBondKeeper{}
k.SetStandingKeeper(skPass)
k.SetWatcherKeeper(wkPass)
k.SetBondKeeper(bkPass)
srv := keeper.NewMsgServerImpl(k)
if _, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: "pool-w", HostReachID: "host-1", Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-w", Signer: "host-1",
}); err != nil {
t.Fatalf("LaunchCoverPool: %v", err)
}
// The stub Watcher recorded the attestation.
if wkPass.lastPoolID != "pool-w" {
t.Errorf("stubWatcher lastPoolID = %q, want pool-w", wkPass.lastPoolID)
}
if len(wkPass.lastPayload) == 0 {
t.Error("stubWatcher lastPayload empty")
}
// The stub Bond keeper (unused in P1) returns false for any bond.
if bkPass.GetBond("any-bond") {
t.Error("stubBondKeeper GetBond on empty should return false")
}
// Populate the bond map and assert true.
bkPass.bonds = map[string]bool{"bond-1": true}
if !bkPass.GetBond("bond-1") {
t.Error("stubBondKeeper GetBond(bond-1) should return true after populate")
}
}
// ============================================================================
// P2 simtest cases (REQ-052, REQ-062, REQ-056, REQ-048, D-086, D-090(1),
// D-090(3)). The P2 simtest exercises:
// (a) successful Charter signing + Watcher witness (WaivedRights empty).
// (b) D-090(1) Charter with WaivedRights non-empty -> REJECTED at
// ValidateBasic.
// (c) Charter amendment with 7-day cooling (Proposed -> rejected-ratify-
// before-7d -> Cooled -> Ratified).
// (d) Pool Council election (3 Masons elected; 4th rejected).
// (e) Cover Call vote with Watcher observer present (CallVoteYes ->
// succeeds) + absent (CallVoteYes -> REJECTED).
// (f) D-086 Factory rejects out-of-phase category launch (Phase 4 when
// FactoryAllowedPhases overridden to [Phase2, Phase3] only) — covered
// above in TestLaunchCoverPoolRejectedOutOfPhase.
// (g) reserve ceiling escalation after 12-month age check (pool with old
// CreatedAt -> succeeds; pool with new CreatedAt -> REJECTED).
// (h) D-090(3) Pool Standing gate amendment below floor -> REJECTED at
// ValidateBasic (covered in msg_charter_test.go; the handler
// re-check is covered here).
// ============================================================================
// launchPoolForP2 is a helper that launches a Cover Pool for the P2
// simtest cases (the Charter/Council/Vote/Escalate handlers all require a
// pre-existing pool). Uses a passing StandingKeeper stub.
func launchPoolForP2(t *testing.T, ctx sdk.Context, srv types.MsgServer, poolID, hostReachID string) {
t.Helper()
_, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: poolID, HostReachID: hostReachID, Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-" + poolID, Signer: hostReachID,
})
if err != nil {
t.Fatalf("launchPoolForP2 %q: %v", poolID, err)
}
}
// launchPoolForP2NilShims is launchPoolForP2 but with a nil-shim keeper
// (skips the Standing gate).
func launchPoolForP2NilShims(t *testing.T, ctx sdk.Context, srv types.MsgServer, poolID, hostReachID string) {
t.Helper()
_, err := srv.LaunchCoverPool(ctx, &types.MsgLaunchCoverPool{
PoolID: poolID, HostReachID: hostReachID, Categories: []types.CoverCategory{types.CatTravel},
ReserveAnnualContribRatio: 1.5, ReserveAccount: "acc-" + poolID, Signer: hostReachID,
})
if err != nil {
t.Fatalf("launchPoolForP2NilShims %q: %v", poolID, err)
}
}
// --- (a) + (b) SignCoverCharter ----------------------------------------------
// TestSignCoverCharterSuccess (case a) asserts a successful Cover-Charter
// signing with an empty WaivedRights (the D-090(1) gate passes) + a Watcher
// witness hash (the WatcherKeeper.Attest is called).
func TestSignCoverCharterSuccess(t *testing.T) {
ctx, sk, wk, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2(t, ctx, srv, "pool-ch", "host-1")
_, err := srv.SignCoverCharter(ctx, &types.MsgSignCoverCharter{
CharterID: "charter-1", PoolID: "pool-ch", HostReachID: "host-1",
DisputePath: "counsel", Gate: "Trusted", HoldingPeriodDays: 30,
StatementOfBeliefsHash: []byte{1, 2, 3},
WatcherWitnessHash: []byte{4, 5, 6},
WaivedRights: []types.RightID{},
Signer: "host-1",
})
if err != nil {
t.Fatalf("SignCoverCharter: %v", err)
}
c, ok := k.GetCoverCharter(ctx, "charter-1")
if !ok {
t.Fatal("CoverCharter not persisted")
}
if c.HostReachID != "host-1" {
t.Errorf("CoverCharter HostReachID = %q, want host-1", c.HostReachID)
}
if len(c.WaivedRights) != 0 {
t.Errorf("CoverCharter WaivedRights = %v, want empty", c.WaivedRights)
}
// The pool's CharterRef is linked.
p, _ := k.GetCoverPool(ctx, "pool-ch")
if p.CharterRef != "charter-1" {
t.Errorf("pool CharterRef = %q, want charter-1", p.CharterRef)
}
// The Watcher attested on the witness hash.
if wk.lastPoolID != "pool-ch" {
t.Errorf("stubWatcher lastPoolID = %q, want pool-ch", wk.lastPoolID)
}
if !hasEvent(ctx, "cover.charter_signed") {
t.Error("cover.charter_signed event not emitted")
}
}
// TestSignCoverCharterWaivedRightsRejected (case b, D-090(1)) asserts a
// Cover-Charter signing with a non-empty WaivedRights is REJECTED at
// ValidateBasic (the dual-firewall runtime gate). The Charter is NOT
// persisted.
func TestSignCoverCharterWaivedRightsRejected(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2(t, ctx, srv, "pool-ch-bad", "host-1")
_, err := srv.SignCoverCharter(ctx, &types.MsgSignCoverCharter{
CharterID: "charter-bad", PoolID: "pool-ch-bad", HostReachID: "host-1",
DisputePath: "counsel", Gate: "Trusted", HoldingPeriodDays: 30,
WaivedRights: []types.RightID{types.RightOneTapExit, types.RightCooling},
Signer: "host-1",
})
if err == nil {
t.Fatal("SignCoverCharter with non-empty WaivedRights should be rejected (D-090(1))")
}
if !strings.Contains(err.Error(), "REQ-056") {
t.Errorf("error = %q, want 'REQ-056'", err.Error())
}
// The Charter was NOT persisted.
if _, ok := k.GetCoverCharter(ctx, "charter-bad"); ok {
t.Error("CoverCharter should NOT be persisted on D-090(1) reject")
}
}
// TestSignCoverCharterIdempotentReject asserts a second SignCoverCharter on
// the same charter-id is REJECTED.
func TestSignCoverCharterIdempotentReject(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2(t, ctx, srv, "pool-ch-dup", "host-1")
first := &types.MsgSignCoverCharter{
CharterID: "charter-dup", PoolID: "pool-ch-dup", HostReachID: "host-1",
DisputePath: "counsel", Gate: "Trusted", HoldingPeriodDays: 30, Signer: "host-1",
}
if _, err := srv.SignCoverCharter(ctx, first); err != nil {
t.Fatalf("first SignCoverCharter: %v", err)
}
_, err := srv.SignCoverCharter(ctx, first)
if err == nil {
t.Error("second SignCoverCharter on same charter-id should be rejected (idempotent)")
}
}
// TestSignCoverCharterNonExistentPool asserts SignCoverCharter on a non-
// existent pool is REJECTED.
func TestSignCoverCharterNonExistentPool(t *testing.T) {
ctx, _, _, _, _, _, k := newSimtestContext(t)
srv := keeper.NewMsgServerImpl(k)
_, err := srv.SignCoverCharter(ctx, &types.MsgSignCoverCharter{
CharterID: "charter-nopool", PoolID: "no-such-pool", HostReachID: "host-1",
DisputePath: "counsel", Gate: "Trusted", HoldingPeriodDays: 30, Signer: "host-1",
})
if err == nil {
t.Error("SignCoverCharter on non-existent pool should be rejected")
}
if !strings.Contains(err.Error(), "not found") {
t.Errorf("error = %q, want 'not found'", err.Error())
}
}
// TestSignCoverCharterNilWatcherSkip asserts a nil WatcherKeeper skips the
// attestation (simtest wiring) and the Charter is still persisted.
func TestSignCoverCharterNilWatcherSkip(t *testing.T) {
ctx, _, k := newSimtestContextNilShims(t)
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2NilShims(t, ctx, srv, "pool-ch-nil", "host-1")
_, err := srv.SignCoverCharter(ctx, &types.MsgSignCoverCharter{
CharterID: "charter-nil", PoolID: "pool-ch-nil", HostReachID: "host-1",
DisputePath: "counsel", Gate: "Trusted", HoldingPeriodDays: 30,
WatcherWitnessHash: []byte{1, 2},
Signer: "host-1",
})
if err != nil {
t.Fatalf("SignCoverCharter with nil WatcherKeeper should skip attestation: %v", err)
}
if _, ok := k.GetCoverCharter(ctx, "charter-nil"); !ok {
t.Error("CoverCharter should be persisted even with nil WatcherKeeper")
}
}
// TestSignCoverCharterWatcherAttestError asserts a WatcherKeeper.Attest
// error REJECTS the signing.
func TestSignCoverCharterWatcherAttestError(t *testing.T) {
ctx, sk, wk, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2(t, ctx, srv, "pool-ch-attest-err", "host-1")
// Set the Watcher attest error AFTER the pool launch (the launch also
// calls Attest; we want the SignCoverCharter Attest to fail, not the
// launch's).
wk.attestErr = errAttestFailed
_, err := srv.SignCoverCharter(ctx, &types.MsgSignCoverCharter{
CharterID: "charter-attest-err", PoolID: "pool-ch-attest-err", HostReachID: "host-1",
DisputePath: "counsel", Gate: "Trusted", HoldingPeriodDays: 30,
WatcherWitnessHash: []byte{1, 2},
Signer: "host-1",
})
if err == nil {
t.Fatal("SignCoverCharter with Watcher attest error should be rejected")
}
if !strings.Contains(err.Error(), "attestation") {
t.Errorf("error = %q, want 'attestation'", err.Error())
}
}
// --- (c) AmendCoverCharter + 7-day cooling -----------------------------------
// TestAmendCoverCharterCooling (case c) asserts the Charter amendment
// lifecycle: Proposed -> rejected-ratify-before-7d -> Cooled -> Ratified.
// The 7-day cooling is enforced by CoolCharterAmendment (the handler
// records ProposedAt; the simtest advances time + calls CoolCharterAmendment
// + RatifyCharterAmendment).
func TestAmendCoverCharterCooling(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2(t, ctx, srv, "pool-amend", "host-1")
if _, err := srv.SignCoverCharter(ctx, &types.MsgSignCoverCharter{
CharterID: "charter-amend", PoolID: "pool-amend", HostReachID: "host-1",
DisputePath: "counsel", Gate: "Trusted", HoldingPeriodDays: 30, Signer: "host-1",
}); err != nil {
t.Fatalf("SignCoverCharter: %v", err)
}
// File the amendment (Proposed).
if _, err := srv.AmendCoverCharter(ctx, &types.MsgAmendCoverCharter{
CharterID: "charter-amend", AmendmentID: "amend-1", Description: "tighten gate", Signer: "host-1",
}); err != nil {
t.Fatalf("AmendCoverCharter: %v", err)
}
a, ok := k.GetCharterAmendment(ctx, "amend-1")
if !ok {
t.Fatal("CharterAmendment not persisted")
}
if a.Status != types.AmendmentProposed {
t.Errorf("amendment Status = %q, want Proposed", a.Status)
}
if !hasEvent(ctx, "cover.charter_amend_proposed") {
t.Error("cover.charter_amend_proposed event not emitted")
}
// Attempt to Cool BEFORE the 7-day cooling elapses -> REJECTED.
proposedAt := a.ProposedAt
if _, err := k.CoolCharterAmendment(ctx, "amend-1", proposedAt); err == nil {
t.Fatal("CoolCharterAmendment before 7-day cooling should be rejected")
}
// Advance time by 7 days + 1 second + Cool -> Cooled.
coolTime := proposedAt + types.CharterAmendmentCoolingSeconds + 1
a, err := k.CoolCharterAmendment(ctx, "amend-1", coolTime)
if err != nil {
t.Fatalf("CoolCharterAmendment after 7-day cooling: %v", err)
}
if a.Status != types.AmendmentCooled {
t.Errorf("amendment Status = %q, want Cooled", a.Status)
}
// Ratify -> Ratified.
a, err = k.RatifyCharterAmendment(ctx, "amend-1", coolTime+1)
if err != nil {
t.Fatalf("RatifyCharterAmendment: %v", err)
}
if a.Status != types.AmendmentRatified {
t.Errorf("amendment Status = %q, want Ratified", a.Status)
}
// The charter's Amendments slice contains the amendment.
c, _ := k.GetCoverCharter(ctx, "charter-amend")
if len(c.Amendments) != 1 || c.Amendments[0].AmendmentID != "amend-1" {
t.Errorf("charter Amendments = %v, want one amend-1", c.Amendments)
}
}
// TestAmendCoverCharterNonExistentCharter asserts AmendCoverCharter on a
// non-existent charter is REJECTED.
func TestAmendCoverCharterNonExistentCharter(t *testing.T) {
ctx, _, _, _, _, _, k := newSimtestContext(t)
srv := keeper.NewMsgServerImpl(k)
_, err := srv.AmendCoverCharter(ctx, &types.MsgAmendCoverCharter{
CharterID: "no-such-charter", AmendmentID: "a", Description: "d", Signer: "s",
})
if err == nil {
t.Error("AmendCoverCharter on non-existent charter should be rejected")
}
}
// TestAmendCoverCharterDuplicateAmendment asserts a duplicate amendment-id
// is REJECTED.
func TestAmendCoverCharterDuplicateAmendment(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2(t, ctx, srv, "pool-amend-dup", "host-1")
if _, err := srv.SignCoverCharter(ctx, &types.MsgSignCoverCharter{
CharterID: "charter-dup-amend", PoolID: "pool-amend-dup", HostReachID: "host-1",
DisputePath: "counsel", Gate: "Trusted", HoldingPeriodDays: 30, Signer: "host-1",
}); err != nil {
t.Fatalf("SignCoverCharter: %v", err)
}
first := &types.MsgAmendCoverCharter{
CharterID: "charter-dup-amend", AmendmentID: "amend-dup", Description: "d", Signer: "host-1",
}
if _, err := srv.AmendCoverCharter(ctx, first); err != nil {
t.Fatalf("first AmendCoverCharter: %v", err)
}
_, err := srv.AmendCoverCharter(ctx, first)
if err == nil {
t.Error("second AmendCoverCharter on same amendment-id should be rejected")
}
}
// TestCoolCharterAmendmentErrors asserts the CoolCharterAmendment helper
// error paths (not found, wrong status, cooling not elapsed — the last is
// covered above; this covers not-found + wrong-status).
func TestCoolCharterAmendmentErrors(t *testing.T) {
ctx, _, _, _, _, _, k := newSimtestContext(t)
// Not found.
if _, err := k.CoolCharterAmendment(ctx, "no-such-amendment", 1000); err == nil {
t.Error("CoolCharterAmendment on non-existent amendment should fail")
}
// Wrong status: directly persist a Ratified amendment, then attempt to
// Cool it -> REJECTED.
k.SetCharterAmendment(ctx, types.CharterAmendment{AmendmentID: "amend-rat", Status: types.AmendmentRatified, ProposedAt: 0})
if _, err := k.CoolCharterAmendment(ctx, "amend-rat", 1000000); err == nil {
t.Error("CoolCharterAmendment on a Ratified amendment should fail")
}
}
// TestRatifyCharterAmendmentErrors asserts the RatifyCharterAmendment helper
// error paths (not found, wrong status — a Proposed amendment cannot be
// Ratified directly).
func TestRatifyCharterAmendmentErrors(t *testing.T) {
ctx, _, _, _, _, _, k := newSimtestContext(t)
// Not found.
if _, err := k.RatifyCharterAmendment(ctx, "no-such-amendment", 1000); err == nil {
t.Error("RatifyCharterAmendment on non-existent amendment should fail")
}
// Wrong status: a Proposed amendment cannot be Ratified directly (must
// be Cooled first).
k.SetCharterAmendment(ctx, types.CharterAmendment{AmendmentID: "amend-prop", Status: types.AmendmentProposed, ProposedAt: 0})
if _, err := k.RatifyCharterAmendment(ctx, "amend-prop", 1000000); err == nil {
t.Error("RatifyCharterAmendment on a Proposed amendment should fail (must be Cooled first)")
}
}
// --- (d) ElectPoolMason -------------------------------------------------------
// TestElectPoolMason (case d) asserts the Pool Council election: 3 Masons
// are elected; a 4th is REJECTED. The pool's CouncilRef is linked.
func TestElectPoolMason(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2(t, ctx, srv, "pool-council", "host-1")
// Elect 3 Masons.
for i, m := range []string{"mason-1", "mason-2", "mason-3"} {
_, err := srv.ElectPoolMason(ctx, &types.MsgElectPoolMason{
PoolID: "pool-council", MasonReachID: m, Signer: "host-1",
})
if err != nil {
t.Fatalf("ElectPoolMason %d (%s): %v", i, m, err)
}
if !hasEvent(ctx, "cover.pool_mason_elected") {
t.Error("cover.pool_mason_elected event not emitted")
}
}
c, ok := k.GetPoolCouncil(ctx, "pool-council")
if !ok {
t.Fatal("PoolCouncil not persisted")
}
if c.ElectedMasonReachIDs != [3]string{"mason-1", "mason-2", "mason-3"} {
t.Errorf("ElectedMasonReachIDs = %v, want [mason-1 mason-2 mason-3]", c.ElectedMasonReachIDs)
}
// The pool's CouncilRef is linked.
p, _ := k.GetCoverPool(ctx, "pool-council")
if p.CouncilRef != "pool-council" {
t.Errorf("pool CouncilRef = %q, want pool-council", p.CouncilRef)
}
// 4th Mason is REJECTED (max 3).
_, err := srv.ElectPoolMason(ctx, &types.MsgElectPoolMason{
PoolID: "pool-council", MasonReachID: "mason-4", Signer: "host-1",
})
if err == nil {
t.Fatal("ElectPoolMason 4th mason should be rejected (max 3)")
}
if !strings.Contains(err.Error(), "max 3") && !strings.Contains(err.Error(), "already has 3") {
t.Errorf("error = %q, want 'max 3' or 'already has 3'", err.Error())
}
}
// TestElectPoolMasonDuplicate asserts a duplicate MasonReachID is REJECTED.
func TestElectPoolMasonDuplicate(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2(t, ctx, srv, "pool-council-dup", "host-1")
if _, err := srv.ElectPoolMason(ctx, &types.MsgElectPoolMason{
PoolID: "pool-council-dup", MasonReachID: "mason-dup", Signer: "host-1",
}); err != nil {
t.Fatalf("first ElectPoolMason: %v", err)
}
_, err := srv.ElectPoolMason(ctx, &types.MsgElectPoolMason{
PoolID: "pool-council-dup", MasonReachID: "mason-dup", Signer: "host-1",
})
if err == nil {
t.Fatal("ElectPoolMason with duplicate mason-reach-id should be rejected")
}
}
// TestElectPoolMasonNonExistentPool asserts ElectPoolMason on a non-existent
// pool is REJECTED.
func TestElectPoolMasonNonExistentPool(t *testing.T) {
ctx, _, _, _, _, _, k := newSimtestContext(t)
srv := keeper.NewMsgServerImpl(k)
_, err := srv.ElectPoolMason(ctx, &types.MsgElectPoolMason{
PoolID: "no-such-pool", MasonReachID: "mason-1", Signer: "host-1",
})
if err == nil {
t.Error("ElectPoolMason on non-existent pool should be rejected")
}
}
// --- (e) VoteCoverCall -------------------------------------------------------
// TestVoteCoverCall (case e) asserts the Cover Call vote: a CallVoteYes
// with Watcher observer present SUCCEEDS; a CallVoteYes with Watcher
// observer absent is REJECTED (REQ-062).
func TestVoteCoverCall(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2(t, ctx, srv, "pool-vote", "host-1")
// File a Cover Call to vote on.
if _, err := srv.FileCoverCall(ctx, &types.MsgFileCoverCall{
CallID: "call-vote", PoolID: "pool-vote", ClaimantReachID: "user-1",
Category: types.CatTravel, AmountGrain: 500, Signer: "user-1",
}); err != nil {
t.Fatalf("FileCoverCall: %v", err)
}
// CallVoteYes with Watcher observer present -> SUCCEEDS.
_, err := srv.VoteCoverCall(ctx, &types.MsgVoteCoverCall{
VoteID: "vote-yes", CallID: "call-vote", PoolID: "pool-vote", VoterReachID: "voter-1",
VoteOption: types.CallVoteYes, WatcherObserverPresent: true, Signer: "voter-1",
})
if err != nil {
t.Fatalf("VoteCoverCall CallVoteYes with observer present: %v", err)
}
v, ok := k.GetCoverCallVote(ctx, "vote-yes")
if !ok {
t.Fatal("CoverCallVote not persisted")
}
if v.VoteOption != types.CallVoteYes {
t.Errorf("vote VoteOption = %q, want Yes", v.VoteOption)
}
if !hasEvent(ctx, "cover.cover_call_voted") {
t.Error("cover.cover_call_voted event not emitted")
}
// CallVoteYes with Watcher observer ABSENT -> REJECTED (REQ-062).
_, err = srv.VoteCoverCall(ctx, &types.MsgVoteCoverCall{
VoteID: "vote-yes-no-obs", CallID: "call-vote", PoolID: "pool-vote", VoterReachID: "voter-2",
VoteOption: types.CallVoteYes, WatcherObserverPresent: false, Signer: "voter-2",
})
if err == nil {
t.Fatal("VoteCoverCall CallVoteYes without observer should be rejected (REQ-062)")
}
if !strings.Contains(err.Error(), "observer") {
t.Errorf("error = %q, want 'observer'", err.Error())
}
// CallVoteNo without observer -> SUCCEEDS (only an affirmative vote
// demands the witness).
_, err = srv.VoteCoverCall(ctx, &types.MsgVoteCoverCall{
VoteID: "vote-no", CallID: "call-vote", PoolID: "pool-vote", VoterReachID: "voter-3",
VoteOption: types.CallVoteNo, WatcherObserverPresent: false, Signer: "voter-3",
})
if err != nil {
t.Errorf("VoteCoverCall CallVoteNo without observer should succeed: %v", err)
}
// CallVoteAbstain without observer -> SUCCEEDS.
_, err = srv.VoteCoverCall(ctx, &types.MsgVoteCoverCall{
VoteID: "vote-abstain", CallID: "call-vote", PoolID: "pool-vote", VoterReachID: "voter-4",
VoteOption: types.CallVoteAbstain, WatcherObserverPresent: false, Signer: "voter-4",
})
if err != nil {
t.Errorf("VoteCoverCall CallVoteAbstain without observer should succeed: %v", err)
}
}
// TestVoteCoverCallNonExistentCall asserts VoteCoverCall on a non-existent
// call is REJECTED.
func TestVoteCoverCallNonExistentCall(t *testing.T) {
ctx, _, _, _, _, _, k := newSimtestContext(t)
srv := keeper.NewMsgServerImpl(k)
_, err := srv.VoteCoverCall(ctx, &types.MsgVoteCoverCall{
VoteID: "vote-x", CallID: "no-such-call", PoolID: "p", VoterReachID: "v",
VoteOption: types.CallVoteYes, WatcherObserverPresent: true, Signer: "s",
})
if err == nil {
t.Error("VoteCoverCall on non-existent call should be rejected")
}
}
// TestVoteCoverCallDuplicate asserts a duplicate vote-id is REJECTED.
func TestVoteCoverCallDuplicate(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2(t, ctx, srv, "pool-vote-dup", "host-1")
if _, err := srv.FileCoverCall(ctx, &types.MsgFileCoverCall{
CallID: "call-dup", PoolID: "pool-vote-dup", ClaimantReachID: "user-1",
Category: types.CatTravel, AmountGrain: 500, Signer: "user-1",
}); err != nil {
t.Fatalf("FileCoverCall: %v", err)
}
first := &types.MsgVoteCoverCall{
VoteID: "vote-dup", CallID: "call-dup", PoolID: "pool-vote-dup", VoterReachID: "voter-1",
VoteOption: types.CallVoteYes, WatcherObserverPresent: true, Signer: "voter-1",
}
if _, err := srv.VoteCoverCall(ctx, first); err != nil {
t.Fatalf("first VoteCoverCall: %v", err)
}
_, err := srv.VoteCoverCall(ctx, first)
if err == nil {
t.Error("second VoteCoverCall on same vote-id should be rejected (idempotent)")
}
}
// --- (g) EscalateReserveCeiling -----------------------------------------------
// TestEscalateReserveCeiling (case g) asserts the reserve ceiling escalation
// after the 12-month age check: a pool with an OLD CreatedAt (>= 365 days)
// SUCCEEDS; a pool with a NEW CreatedAt is REJECTED.
func TestEscalateReserveCeiling(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2(t, ctx, srv, "pool-esc-old", "host-1")
// Mutate the pool's CreatedAt to an OLD timestamp (the ctx BlockTime is
// time.Unix(1000, 0); set CreatedAt to a negative value so
// now - CreatedAt >= 365 days. -ReserveCeilingAgeSeconds puts the age
// at exactly 31537000 seconds = 365d + 1000s, which satisfies the
// >= ReserveCeilingAgeSeconds check).
p, _ := k.GetCoverPool(ctx, "pool-esc-old")
p.CreatedAt = -types.ReserveCeilingAgeSeconds
k.SetCoverPool(ctx, p)
_, err := srv.EscalateReserveCeiling(ctx, &types.MsgEscalateReserveCeiling{
PoolID: "pool-esc-old", Signer: "host-1",
})
if err != nil {
t.Fatalf("EscalateReserveCeiling on old pool: %v", err)
}
p, _ = k.GetCoverPool(ctx, "pool-esc-old")
if p.ReserveAnnualContribRatio != types.CoverReserveCeilingAnnualContribX {
t.Errorf("reserve ratio = %.2f, want %.2f (ceiling)", p.ReserveAnnualContribRatio, types.CoverReserveCeilingAnnualContribX)
}
if !hasEvent(ctx, "cover.reserve_ceiling_escalated") {
t.Error("cover.reserve_ceiling_escalated event not emitted")
}
// A fresh pool (new CreatedAt) -> REJECTED.
launchPoolForP2(t, ctx, srv, "pool-esc-new", "host-1")
// pool-esc-new CreatedAt = ctx.BlockTime().Unix() = 1000; the age check
// (now - CreatedAt >= 365 days) fails (0 < 31536000).
_, err = srv.EscalateReserveCeiling(ctx, &types.MsgEscalateReserveCeiling{
PoolID: "pool-esc-new", Signer: "host-1",
})
if err == nil {
t.Fatal("EscalateReserveCeiling on fresh pool should be rejected (12-month age check)")
}
if !strings.Contains(err.Error(), "12-month") {
t.Errorf("error = %q, want '12-month'", err.Error())
}
}
// TestEscalateReserveCeilingNonExistentPool asserts EscalateReserveCeiling
// on a non-existent pool is REJECTED.
func TestEscalateReserveCeilingNonExistentPool(t *testing.T) {
ctx, _, _, _, _, _, k := newSimtestContext(t)
srv := keeper.NewMsgServerImpl(k)
_, err := srv.EscalateReserveCeiling(ctx, &types.MsgEscalateReserveCeiling{
PoolID: "no-such-pool", Signer: "host-1",
})
if err == nil {
t.Error("EscalateReserveCeiling on non-existent pool should be rejected")
}
}
// TestEscalateReserveCeilingWatcherAttestError asserts a WatcherKeeper.Attest
// error REJECTS the escalation.
func TestEscalateReserveCeilingWatcherAttestError(t *testing.T) {
ctx, sk, wk, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2(t, ctx, srv, "pool-esc-attest-err", "host-1")
// Set the Watcher attest error AFTER the pool launch + set CreatedAt
// to an OLD timestamp so the age check passes (the escalation's Attest
// fails, not the launch's).
p, _ := k.GetCoverPool(ctx, "pool-esc-attest-err")
p.CreatedAt = -types.ReserveCeilingAgeSeconds
k.SetCoverPool(ctx, p)
wk.attestErr = errAttestFailed
_, err := srv.EscalateReserveCeiling(ctx, &types.MsgEscalateReserveCeiling{
PoolID: "pool-esc-attest-err", Signer: "host-1",
})
if err == nil {
t.Fatal("EscalateReserveCeiling with Watcher attest error should be rejected")
}
if !strings.Contains(err.Error(), "attestation") {
t.Errorf("error = %q, want 'attestation'", err.Error())
}
}
// --- (h) AmendPoolStandingGate D-090(3) handler re-check ----------------------
// TestAmendPoolStandingGateSuccess asserts a successful gate amendment
// (NewGate >= CoverStandingGateTrusted) updates the pool's PoolStandingGate.
func TestAmendPoolStandingGateSuccess(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2(t, ctx, srv, "pool-gate", "host-1")
_, err := srv.AmendPoolStandingGate(ctx, &types.MsgAmendPoolStandingGate{
PoolID: "pool-gate", NewGate: 4.5, Signer: "host-1",
})
if err != nil {
t.Fatalf("AmendPoolStandingGate: %v", err)
}
p, _ := k.GetCoverPool(ctx, "pool-gate")
if p.PoolStandingGate != 4.5 {
t.Errorf("PoolStandingGate = %.2f, want 4.5", p.PoolStandingGate)
}
if !hasEvent(ctx, "cover.pool_standing_gate_amended") {
t.Error("cover.pool_standing_gate_amended event not emitted")
}
}
// TestAmendPoolStandingGateBelowFloorHandler (case h, D-090(3)) asserts a
// below-floor amendment (NewGate = 3.0 < 4.0) is REJECTED at the handler
// (the handler re-checks in defense in depth — ValidateBasic already
// rejected, but this test confirms the handler re-check also fires when
// the message reaches the handler via a non-ValidateBasic path).
func TestAmendPoolStandingGateBelowFloorHandler(t *testing.T) {
ctx, sk, _, _, _, _, k := newSimtestContext(t)
sk.buckets = map[string]struct {
bucket string
score float64
}{
"host-1/Travel": {"Trusted", 4.0},
}
srv := keeper.NewMsgServerImpl(k)
launchPoolForP2(t, ctx, srv, "pool-gate-bad", "host-1")
// NewGate = 3.0 < 4.0 -> REJECTED at ValidateBasic (the handler never
// reaches the state mutation).
_, err := srv.AmendPoolStandingGate(ctx, &types.MsgAmendPoolStandingGate{
PoolID: "pool-gate-bad", NewGate: 3.0, Signer: "host-1",
})
if err == nil {
t.Fatal("AmendPoolStandingGate with NewGate 3.0 < 4.0 should be rejected (D-090(3))")
}
if !strings.Contains(err.Error(), "D-090(3)") {
t.Errorf("error = %q, want 'D-090(3)'", err.Error())
}
// The pool's gate was NOT mutated.
p, _ := k.GetCoverPool(ctx, "pool-gate-bad")
if p.PoolStandingGate != types.CoverStandingGateTrusted {
t.Errorf("PoolStandingGate = %.2f, want %.2f (unchanged)", p.PoolStandingGate, types.CoverStandingGateTrusted)
}
}
// TestAmendPoolStandingGateNonExistentPool asserts AmendPoolStandingGate on
// a non-existent pool is REJECTED.
func TestAmendPoolStandingGateNonExistentPool(t *testing.T) {
ctx, _, _, _, _, _, k := newSimtestContext(t)
srv := keeper.NewMsgServerImpl(k)
_, err := srv.AmendPoolStandingGate(ctx, &types.MsgAmendPoolStandingGate{
PoolID: "no-such-pool", NewGate: 4.5, Signer: "host-1",
})
if err == nil {
t.Error("AmendPoolStandingGate on non-existent pool should be rejected")
}
}
// --- P2 keeper accessors (coverage) -----------------------------------------
// TestP2KeeperAccessors exercises the P2 keeper accessors (AllCoverCharters,
// AllPoolCouncils, AllCoverCallVotes, AllCharterAmendments, the Get/Set
// helpers, the marshal-error paths) to push coverage >=80%.
func TestP2KeeperAccessors(t *testing.T) {
ctx, _, _, _, _, _, k := newSimtestContext(t)
// Empty-store accessors return empty (not nil) slices.
if got := k.AllCoverCharters(ctx); len(got) != 0 {
t.Errorf("AllCoverCharters empty = %d, want 0", len(got))
}
if got := k.AllPoolCouncils(ctx); len(got) != 0 {
t.Errorf("AllPoolCouncils empty = %d, want 0", len(got))
}
if got := k.AllCoverCallVotes(ctx); len(got) != 0 {
t.Errorf("AllCoverCallVotes empty = %d, want 0", len(got))
}
if got := k.AllCharterAmendments(ctx); len(got) != 0 {
t.Errorf("AllCharterAmendments empty = %d, want 0", len(got))
}
if _, ok := k.GetCoverCharter(ctx, "nobody"); ok {
t.Error("GetCoverCharter on empty store should return false")
}
if _, ok := k.GetPoolCouncil(ctx, "nobody"); ok {
t.Error("GetPoolCouncil on empty store should return false")
}
if _, ok := k.GetCoverCallVote(ctx, "nobody"); ok {
t.Error("GetCoverCallVote on empty store should return false")
}
if _, ok := k.GetCharterAmendment(ctx, "nobody"); ok {
t.Error("GetCharterAmendment on empty store should return false")
}
// Populate + read back via accessors.
k.SetCoverCharter(ctx, types.CoverCharter{CharterID: "c1", PoolID: "p1", HostReachID: "h1"})
if c, ok := k.GetCoverCharter(ctx, "c1"); !ok || c.HostReachID != "h1" {
t.Errorf("GetCoverCharter = %+v ok=%v", c, ok)
}
if got := k.AllCoverCharters(ctx); len(got) != 1 {
t.Errorf("AllCoverCharters = %d, want 1", len(got))
}
k.SetPoolCouncil(ctx, types.PoolCouncil{PoolID: "p1", HostReachID: "h1"})
if c, ok := k.GetPoolCouncil(ctx, "p1"); !ok || c.HostReachID != "h1" {
t.Errorf("GetPoolCouncil = %+v ok=%v", c, ok)
}
if got := k.AllPoolCouncils(ctx); len(got) != 1 {
t.Errorf("AllPoolCouncils = %d, want 1", len(got))
}
k.SetCoverCallVote(ctx, types.CoverCallVote{VoteID: "v1", CallID: "c1", PoolID: "p1", VoterReachID: "v1"})
if v, ok := k.GetCoverCallVote(ctx, "v1"); !ok || v.VoterReachID != "v1" {
t.Errorf("GetCoverCallVote = %+v ok=%v", v, ok)
}
if got := k.AllCoverCallVotes(ctx); len(got) != 1 {
t.Errorf("AllCoverCallVotes = %d, want 1", len(got))
}
k.SetCharterAmendment(ctx, types.CharterAmendment{AmendmentID: "a1", Description: "d", Status: types.AmendmentProposed})
if a, ok := k.GetCharterAmendment(ctx, "a1"); !ok || a.Description != "d" {
t.Errorf("GetCharterAmendment = %+v ok=%v", a, ok)
}
if got := k.AllCharterAmendments(ctx); len(got) != 1 {
t.Errorf("AllCharterAmendments = %d, want 1", len(got))
}
// Marshal-error paths (corrupt bytes in store).
store := ctx.KVStore(k.StoreKey())
store.Set([]byte("charter/corrupt"), []byte("not-json"))
if _, ok := k.GetCoverCharter(ctx, "corrupt"); ok {
t.Error("GetCoverCharter on corrupt bytes should return false")
}
store.Set([]byte("council/corrupt"), []byte("not-json"))
if _, ok := k.GetPoolCouncil(ctx, "corrupt"); ok {
t.Error("GetPoolCouncil on corrupt bytes should return false")
}
store.Set([]byte("vote/corrupt"), []byte("not-json"))
if _, ok := k.GetCoverCallVote(ctx, "corrupt"); ok {
t.Error("GetCoverCallVote on corrupt bytes should return false")
}
store.Set([]byte("amendment/corrupt"), []byte("not-json"))
if _, ok := k.GetCharterAmendment(ctx, "corrupt"); ok {
t.Error("GetCharterAmendment on corrupt bytes should return false")
}
}
// TestParamsOverrideAndAccessors exercises the SetParamsOverride + Params
// accessors (coverage).
func TestParamsOverrideAndAccessors(t *testing.T) {
_, _, _, _, _, _, k := newSimtestContext(t)
// Default Params (no override).
p := k.Params()
if len(p.FactoryAllowedPhases) != 3 {
t.Errorf("default Params FactoryAllowedPhases len = %d, want 3", len(p.FactoryAllowedPhases))
}
// Override.
override := types.Params{
FactoryAllowedPhases: []types.CoverCategoryPhase{types.Phase2},
PoolStandingGate: types.CoverStandingGateTrusted,
}
k.SetParamsOverride(override)
p = k.Params()
if len(p.FactoryAllowedPhases) != 1 || p.FactoryAllowedPhases[0] != types.Phase2 {
t.Errorf("overridden Params FactoryAllowedPhases = %v, want [Phase2]", p.FactoryAllowedPhases)
}
}