caelum0x 9e2746707a feat(governance): complete governance module with veto votes, cancel, queries, and execution log
- Task 1.1/1.2: Add no_with_veto vote option with 33.4% veto threshold
  - New VoteOptionNoWithVeto constant and DefaultVetoThresholdBps (3340)
  - VetoVotes field on Proposal struct (proto field 15)
  - CastVote handles no_with_veto; TallyProposal rejects if veto > 33.4%

- Task 1.3: Add MsgCancelProposal
  - New ErrUnauthorizedCancel (1411) and ErrProposalNotCancellable (1412)
  - ProposalStatusCancelled constant
  - CancelProposal keeper method: checks proposer, voting status, refunds deposit

- Task 1.4: Add QueryTallyResult and QueryVoterVotes
  - QueryTallyResult returns counts, percentages (bps), pass/veto status
  - QueryVoterVotes walks Votes collection filtered by voter address

- Task 1.5: ParamExecutor coverage for marketplace, reputation, messaging, modelregistry
  - All 4 modules tested via mock executor registration + ExecuteProposal

- Task 1.6: Execution log fields
  - ExecutionHeight (proto field 16) and ExecutionError (proto field 17) on Proposal
  - ExecuteProposal sets these on both success and failure paths

All 24 tests pass (5 existing + 19 new), go build ./... clean.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-16 14:24:34 +03:00

134 lines
4.4 KiB
Go

package governance
import (
"context"
"encoding/json"
"fmt"
"cosmossdk.io/core/appmodule"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"clawchain/x/governance/keeper"
"clawchain/x/governance/types"
)
var (
_ module.AppModuleBasic = (*AppModule)(nil)
_ module.AppModule = (*AppModule)(nil)
_ module.HasGenesis = (*AppModule)(nil)
_ module.HasServices = (*AppModule)(nil)
_ appmodule.AppModule = (*AppModule)(nil)
_ appmodule.HasEndBlocker = (*AppModule)(nil)
)
// AppModule implements the AppModule interface for the governance parameter change module.
type AppModule struct {
cdc codec.Codec
keeper keeper.Keeper
}
func NewAppModule(
cdc codec.Codec,
keeper keeper.Keeper,
) AppModule {
return AppModule{
cdc: cdc,
keeper: keeper,
}
}
// IsAppModule implements the appmodule.AppModule interface.
func (AppModule) IsAppModule() {}
// Name returns the name of the module as a string.
func (AppModule) Name() string {
return types.ModuleName
}
// RegisterLegacyAminoCodec registers the amino codec.
func (AppModule) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterCodec(cdc)
}
// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module.
func (AppModule) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
if err := types.RegisterQueryHandlerClient(clientCtx.CmdContext, mux, types.NewQueryClient(clientCtx)); err != nil {
panic(err)
}
}
// RegisterInterfaces registers a module's interface types and their concrete implementations.
func (AppModule) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
types.RegisterInterfaces(registry)
}
// RegisterServices registers gRPC query and msg services for the module.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper))
m := keeper.NewMigrator(am.keeper)
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err))
}
}
// DefaultGenesis returns a default GenesisState for the module.
func (am AppModule) DefaultGenesis(codec.JSONCodec) json.RawMessage {
gs := types.DefaultGenesis()
bz, err := json.Marshal(gs)
if err != nil {
panic(err)
}
return bz
}
// ValidateGenesis validates the GenesisState, given in its json.RawMessage form.
func (am AppModule) ValidateGenesis(_ codec.JSONCodec, _ client.TxEncodingConfig, bz json.RawMessage) error {
var genState types.GenesisState
if err := json.Unmarshal(bz, &genState); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
}
return types.ValidateGenesis(genState)
}
// InitGenesis performs the module's genesis initialization.
func (am AppModule) InitGenesis(ctx sdk.Context, _ codec.JSONCodec, gs json.RawMessage) {
var genState types.GenesisState
if err := json.Unmarshal(gs, &genState); err != nil {
panic(fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err))
}
if err := am.keeper.InitGenesis(ctx, genState); err != nil {
panic(fmt.Errorf("failed to initialize %s genesis state: %w", types.ModuleName, err))
}
}
// ExportGenesis returns the module's exported genesis state as raw JSON bytes.
func (am AppModule) ExportGenesis(ctx sdk.Context, _ codec.JSONCodec) json.RawMessage {
genState, err := am.keeper.ExportGenesis(ctx)
if err != nil {
panic(fmt.Errorf("failed to export %s genesis state: %w", types.ModuleName, err))
}
bz, err := json.Marshal(genState)
if err != nil {
panic(fmt.Errorf("failed to marshal %s genesis state: %w", types.ModuleName, err))
}
return bz
}
// ConsensusVersion is a sequence number for state-breaking change of the module.
// v2: Added gRPC services and stake-weighted voting.
func (AppModule) ConsensusVersion() uint64 { return 2 }
// EndBlock contains the logic that is automatically triggered at the end of each block.
// It processes proposals whose voting period has ended.
func (am AppModule) EndBlock(ctx context.Context) error {
return am.keeper.EndBlocker(ctx)
}