Skip to content

Commit

Permalink
fix(tally): add default genesis state
Browse files Browse the repository at this point in the history
Also wire up the message server so we can actually send updateParams
transactions from the governance module.
  • Loading branch information
Thomasvdam committed Dec 10, 2024
1 parent 60480be commit 67f1956
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ func NewApp(
ibctm.AppModule{},
packetforward.NewAppModule(app.PacketForwardKeeper, nil),
wasmstorage.NewAppModule(appCodec, app.WasmStorageKeeper),
tally.NewAppModule(app.TallyKeeper),
tally.NewAppModule(appCodec, app.TallyKeeper),
dataproxy.NewAppModule(appCodec, app.DataProxyKeeper),
pubkey.NewAppModule(appCodec, app.PubKeyKeeper),
batching.NewAppModule(appCodec, app.BatchingKeeper),
Expand Down
2 changes: 1 addition & 1 deletion x/batching/keeper/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func initFixture(tb testing.TB) *fixture {
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper, nil)
stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper, pubKeyKeeper)
wasmStorageModule := wasmstorage.NewAppModule(cdc, *wasmStorageKeeper)
tallyModule := tally.NewAppModule(tallyKeeper)
tallyModule := tally.NewAppModule(cdc, tallyKeeper)
pubKeyModule := pubkey.NewAppModule(cdc, pubKeyKeeper)
batchingModule := batching.NewAppModule(cdc, batchingKeeper)

Expand Down
2 changes: 1 addition & 1 deletion x/tally/keeper/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func initFixture(tb testing.TB) *fixture {
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper, nil)
stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper, pubKeyKeeper)
wasmStorageModule := wasmstorage.NewAppModule(cdc, *wasmStorageKeeper)
tallyModule := tally.NewAppModule(tallyKeeper)
tallyModule := tally.NewAppModule(cdc, tallyKeeper)

// Upload and instantiate the SEDA contract.
creator := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
Expand Down
47 changes: 32 additions & 15 deletions x/tally/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
abci "github.com/cometbft/cometbft/abci/types"

"cosmossdk.io/core/appmodule"
errorsmod "cosmossdk.io/errors"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -33,10 +34,12 @@ var (
// ----------------------------------------------------------------------------

// AppModuleBasic implements the AppModuleBasic interface that defines the independent methods a Cosmos SDK module needs to implement.
type AppModuleBasic struct{}
type AppModuleBasic struct {
cdc codec.BinaryCodec
}

func NewAppModuleBasic() AppModuleBasic {
return AppModuleBasic{}
func NewAppModuleBasic(cdc codec.BinaryCodec) AppModuleBasic {
return AppModuleBasic{cdc: cdc}
}

// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
Expand All @@ -51,19 +54,27 @@ func (AppModuleBasic) Name() string {
}

// RegisterLegacyAminoCodec registers the amino codec for the module, which is used to marshal and unmarshal structs to/from []byte in order to persist them in the module's KVStore
func (AppModuleBasic) RegisterLegacyAminoCodec(_ *codec.LegacyAmino) {}
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterCodec(cdc)
}

// RegisterInterfaces registers a module's interface types and their concrete implementations as proto.Message
func (a AppModuleBasic) RegisterInterfaces(_ cdctypes.InterfaceRegistry) {}
func (a AppModuleBasic) RegisterInterfaces(cdc cdctypes.InterfaceRegistry) {
types.RegisterInterfaces(cdc)
}

// DefaultGenesis returns a default GenesisState for the module, marshaled to json.RawMessage. The default GenesisState need to be defined by the module developer and is primarily used for testing
func (AppModuleBasic) DefaultGenesis(_ codec.JSONCodec) json.RawMessage {
return nil
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(types.DefaultGenesisState())
}

// ValidateGenesis performs genesis state validation for the tally module.
func (AppModuleBasic) ValidateGenesis(_ codec.JSONCodec, _ client.TxEncodingConfig, _ json.RawMessage) error {
return nil
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncodingConfig, bz json.RawMessage) error {
var data types.GenesisState
if err := cdc.UnmarshalJSON(bz, &data); err != nil {
return errorsmod.Wrapf(err, "failed to unmarshal %s genesis state", types.ModuleName)
}
return types.ValidateGenesis(data)
}

// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module
Expand All @@ -89,27 +100,33 @@ type AppModule struct {
keeper keeper.Keeper
}

func NewAppModule(keeper keeper.Keeper) AppModule {
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper) AppModule {
return AppModule{
AppModuleBasic: NewAppModuleBasic(),
AppModuleBasic: NewAppModuleBasic(cdc),
keeper: keeper,
}
}

// RegisterServices registers a gRPC query service to respond to the module-specific gRPC queries
func (am AppModule) RegisterServices(_ module.Configurator) {}
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
}

// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// InitGenesis performs the module's genesis initialization. It returns no validator updates.
func (am AppModule) InitGenesis(_ sdk.Context, _ codec.JSONCodec, _ json.RawMessage) []abci.ValidatorUpdate {
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.RawMessage) []abci.ValidatorUpdate {
var genesisState types.GenesisState
cdc.MustUnmarshalJSON(gs, &genesisState)
am.keeper.InitGenesis(ctx, genesisState)
return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the module's exported genesis state as raw JSON bytes.
func (am AppModule) ExportGenesis(_ sdk.Context, _ codec.JSONCodec) json.RawMessage {
return nil
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
gs := am.keeper.ExportGenesis(ctx)
return cdc.MustMarshalJSON(&gs)
}

// 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
Expand Down
18 changes: 18 additions & 0 deletions x/tally/types/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
)

func RegisterCodec(_ *codec.LegacyAmino) {
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgUpdateParams{},
)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}
13 changes: 13 additions & 0 deletions x/tally/types/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package types

// DefaultGenesisState creates a default GenesisState object.
func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
}
}

// ValidateGenesis validates batching genesis data.
func ValidateGenesis(state GenesisState) error {
return state.Params.Validate()
}
7 changes: 7 additions & 0 deletions x/tally/types/params.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package types

import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

const (
DefaultMaxTallyGasLimit = 300_000_000_000_000
)
Expand All @@ -13,5 +17,8 @@ func DefaultParams() Params {

// ValidateBasic performs basic validation on tally module parameters.
func (p *Params) Validate() error {
if p.MaxTallyGasLimit <= 0 {
return sdkerrors.ErrInvalidRequest.Wrapf("max tally gas limit must be greater than 0: %d", p.MaxTallyGasLimit)
}
return nil
}

0 comments on commit 67f1956

Please sign in to comment.