2025-10-03 14:45:52 -04:00
|
|
|
// Package module provides the DWN module implementation.
|
2024-09-26 18:01:49 -04:00
|
|
|
package module
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
2024-11-26 22:05:50 -05:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
|
|
|
|
|
|
|
|
|
abci "github.com/cometbft/cometbft/abci/types"
|
|
|
|
|
|
2024-09-26 18:01:49 -04:00
|
|
|
"cosmossdk.io/client/v2/autocli"
|
|
|
|
|
errorsmod "cosmossdk.io/errors"
|
2024-11-26 22:05:50 -05:00
|
|
|
|
2024-09-26 18:01:49 -04:00
|
|
|
"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"
|
|
|
|
|
|
2025-10-03 14:45:52 -04:00
|
|
|
"github.com/sonr-io/sonr/x/dwn/keeper"
|
|
|
|
|
"github.com/sonr-io/sonr/x/dwn/types"
|
2024-09-26 18:01:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2024-11-26 22:05:50 -05:00
|
|
|
// ConsensusVersion defines the current x/dwn module consensus version.
|
2024-09-26 18:01:49 -04:00
|
|
|
ConsensusVersion = 1
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
_ module.AppModuleBasic = AppModuleBasic{}
|
|
|
|
|
_ module.AppModuleGenesis = AppModule{}
|
|
|
|
|
_ module.AppModule = AppModule{}
|
2025-10-03 14:45:52 -04:00
|
|
|
_ module.HasABCIEndBlock = AppModule{}
|
2024-09-26 18:01:49 -04:00
|
|
|
|
|
|
|
|
_ autocli.HasAutoCLIConfig = AppModule{}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// AppModuleBasic defines the basic application module used by the wasm module.
|
|
|
|
|
type AppModuleBasic struct {
|
|
|
|
|
cdc codec.Codec
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AppModule struct {
|
|
|
|
|
AppModuleBasic
|
|
|
|
|
|
|
|
|
|
keeper keeper.Keeper
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewAppModule constructor
|
|
|
|
|
func NewAppModule(
|
|
|
|
|
cdc codec.Codec,
|
|
|
|
|
keeper keeper.Keeper,
|
|
|
|
|
) *AppModule {
|
|
|
|
|
return &AppModule{
|
|
|
|
|
AppModuleBasic: AppModuleBasic{cdc: cdc},
|
|
|
|
|
keeper: keeper,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AppModuleBasic) Name() string {
|
|
|
|
|
return types.ModuleName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
|
|
|
|
|
return cdc.MustMarshalJSON(&types.GenesisState{
|
|
|
|
|
Params: types.DefaultParams(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 14:45:52 -04:00
|
|
|
func (a AppModuleBasic) ValidateGenesis(
|
|
|
|
|
marshaler codec.JSONCodec,
|
|
|
|
|
_ client.TxEncodingConfig,
|
|
|
|
|
message json.RawMessage,
|
|
|
|
|
) error {
|
2024-09-26 18:01:49 -04:00
|
|
|
var data types.GenesisState
|
|
|
|
|
err := marshaler.UnmarshalJSON(message, &data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := data.Params.Validate(); err != nil {
|
|
|
|
|
return errorsmod.Wrap(err, "params")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AppModuleBasic) RegisterRESTRoutes(_ client.Context, _ *mux.Router) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
|
2025-10-03 14:45:52 -04:00
|
|
|
err := types.RegisterQueryHandlerClient(
|
|
|
|
|
context.Background(),
|
|
|
|
|
mux,
|
|
|
|
|
types.NewQueryClient(clientCtx),
|
|
|
|
|
)
|
2024-09-26 18:01:49 -04:00
|
|
|
if err != nil {
|
|
|
|
|
// same behavior as in cosmos-sdk
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Disable in favor of autocli.go. If you wish to use these, it will override AutoCLI methods.
|
|
|
|
|
/*
|
|
|
|
|
func (a AppModuleBasic) GetTxCmd() *cobra.Command {
|
|
|
|
|
return cli.NewTxCmd()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AppModuleBasic) GetQueryCmd() *cobra.Command {
|
|
|
|
|
return cli.GetQueryCmd()
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
|
|
|
|
|
types.RegisterLegacyAminoCodec(cdc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AppModuleBasic) RegisterInterfaces(r codectypes.InterfaceRegistry) {
|
|
|
|
|
types.RegisterInterfaces(r)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 14:45:52 -04:00
|
|
|
func (a AppModule) InitGenesis(
|
|
|
|
|
ctx sdk.Context,
|
|
|
|
|
marshaler codec.JSONCodec,
|
|
|
|
|
message json.RawMessage,
|
|
|
|
|
) []abci.ValidatorUpdate {
|
2024-09-26 18:01:49 -04:00
|
|
|
var genesisState types.GenesisState
|
|
|
|
|
marshaler.MustUnmarshalJSON(message, &genesisState)
|
|
|
|
|
|
|
|
|
|
if err := a.keeper.Params.Set(ctx, genesisState.Params); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 14:45:52 -04:00
|
|
|
if err := a.keeper.InitGenesis(ctx, &genesisState); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-26 18:01:49 -04:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AppModule) ExportGenesis(ctx sdk.Context, marshaler codec.JSONCodec) json.RawMessage {
|
|
|
|
|
genState := a.keeper.ExportGenesis(ctx)
|
|
|
|
|
return marshaler.MustMarshalJSON(genState)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AppModule) QuerierRoute() string {
|
|
|
|
|
return types.QuerierRoute
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AppModule) RegisterServices(cfg module.Configurator) {
|
|
|
|
|
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(a.keeper))
|
|
|
|
|
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(a.keeper))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ConsensusVersion is a sequence number for state-breaking change of the
|
|
|
|
|
// module. It should be incremented on each consensus-breaking change
|
|
|
|
|
// introduced by the module. To avoid wrong/empty versions, the initial version
|
|
|
|
|
// should be set to 1.
|
|
|
|
|
func (a AppModule) ConsensusVersion() uint64 {
|
|
|
|
|
return ConsensusVersion
|
|
|
|
|
}
|
2025-10-03 14:45:52 -04:00
|
|
|
|
|
|
|
|
// EndBlock executes all ABCI EndBlock logic respective to the DWN module.
|
|
|
|
|
// It performs automatic key rotation checks and returns an empty validator update set.
|
|
|
|
|
func (a AppModule) EndBlock(ctx context.Context) ([]abci.ValidatorUpdate, error) {
|
|
|
|
|
// Check if key rotation is due and perform it if needed
|
|
|
|
|
err := a.keeper.CheckAndPerformKeyRotation(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// Log error but don't fail the block - key rotation is not critical for consensus
|
|
|
|
|
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
|
|
|
|
logger := a.keeper.Logger()
|
|
|
|
|
logger.Error("Failed to check and perform key rotation in EndBlock",
|
|
|
|
|
"error", err,
|
|
|
|
|
"block_height", sdkCtx.BlockHeight(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DWN module does not modify validator set
|
|
|
|
|
return []abci.ValidatorUpdate{}, nil
|
|
|
|
|
}
|