diff --git a/app/app.go b/app/app.go index 93ba4b1d..9191456a 100644 --- a/app/app.go +++ b/app/app.go @@ -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), diff --git a/x/batching/keeper/integration_test.go b/x/batching/keeper/integration_test.go index 4be9fd81..6af2307a 100644 --- a/x/batching/keeper/integration_test.go +++ b/x/batching/keeper/integration_test.go @@ -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) diff --git a/x/tally/keeper/integration_test.go b/x/tally/keeper/integration_test.go index f17c7d4e..b123cc87 100644 --- a/x/tally/keeper/integration_test.go +++ b/x/tally/keeper/integration_test.go @@ -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()) diff --git a/x/tally/module.go b/x/tally/module.go index 6f19f0f6..066b0179 100644 --- a/x/tally/module.go +++ b/x/tally/module.go @@ -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" @@ -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. @@ -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 @@ -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 diff --git a/x/tally/types/codec.go b/x/tally/types/codec.go new file mode 100644 index 00000000..ddfa702e --- /dev/null +++ b/x/tally/types/codec.go @@ -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) +} diff --git a/x/tally/types/genesis.go b/x/tally/types/genesis.go new file mode 100644 index 00000000..911a28f6 --- /dev/null +++ b/x/tally/types/genesis.go @@ -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() +} diff --git a/x/tally/types/params.go b/x/tally/types/params.go index 56acd917..d10cf530 100644 --- a/x/tally/types/params.go +++ b/x/tally/types/params.go @@ -1,5 +1,9 @@ package types +import ( + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + const ( DefaultMaxTallyGasLimit = 300_000_000_000_000 ) @@ -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 }