cosmos-sdk/x/genutil/module.go
Marie f59df68a97
Migrate x/genutil to use TxConfig (#6734)
* Update genutil collect and gentx to use TxGenerator

* Remove print statement

* Use Tx in genutil DeliverGenTxs

* Use Tx in genutil genesis_state

* Use Tx in ValidateGenesis

* Use amino txJSONDecoder and txBinaryEncoder in genutil InitGenesis

* Use TxConfig in place of TxGenerator

* Add gentx tests

* Remove commented line

* Test fixes

* Apply suggestions from code review

Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>

* Fixes

* Fixes

* Fixes

* Fixes

* Remove unneeded test case (doesn't apply to proto marshaling)

* linting

* Refactor to use new TxEncodingConfig interface in genutil module

* Replace golang/protobuf with gogo/protobuf package

* Use TxEncodingConfig in InitTestnet

* Remove old amino.go file

* Use TxJSONDecoder in genutil ValidateGenesis

* Add parameter to ValidateGenesis to resolve the tx JSON decoder issue

* Address review feedback

Co-authored-by: Jack Zampolin <jack.zampolin@gmail.com>
Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com>
Co-authored-by: Aaron Craelius <aaron@regen.network>
Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-07-25 08:10:04 +00:00

104 lines
3.5 KiB
Go

package genutil
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/client"
"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/module"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
)
var (
_ module.AppModuleGenesis = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
)
// AppModuleBasic defines the basic application module used by the genutil module.
type AppModuleBasic struct{}
// Name returns the genutil module's name.
func (AppModuleBasic) Name() string {
return types.ModuleName
}
// RegisterCodec registers the genutil module's types for the given codec.
func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {}
// RegisterInterfaces registers the module's interface types
func (b AppModuleBasic) RegisterInterfaces(_ cdctypes.InterfaceRegistry) {}
// DefaultGenesis returns default genesis state as raw bytes for the genutil
// module.
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage {
return cdc.MustMarshalJSON(types.DefaultGenesisState())
}
// ValidateGenesis performs genesis state validation for the genutil module.
func (b AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, txEncodingConfig client.TxEncodingConfig, bz json.RawMessage) error {
var data types.GenesisState
if err := cdc.UnmarshalJSON(bz, &data); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
}
return types.ValidateGenesis(data, txEncodingConfig.TxJSONDecoder())
}
// RegisterRESTRoutes registers the REST routes for the genutil module.
func (AppModuleBasic) RegisterRESTRoutes(_ client.Context, _ *mux.Router) {}
// GetTxCmd returns no root tx command for the genutil module.
func (AppModuleBasic) GetTxCmd() *cobra.Command { return nil }
// GetQueryCmd returns no root query command for the genutil module.
func (AppModuleBasic) GetQueryCmd() *cobra.Command { return nil }
//____________________________________________________________________________
// AppModule implements an application module for the genutil module.
type AppModule struct {
AppModuleBasic
accountKeeper types.AccountKeeper
stakingKeeper types.StakingKeeper
deliverTx deliverTxfn
txEncodingConfig client.TxEncodingConfig
}
// NewAppModule creates a new AppModule object
func NewAppModule(accountKeeper types.AccountKeeper,
stakingKeeper types.StakingKeeper, deliverTx deliverTxfn,
txEncodingConfig client.TxEncodingConfig,
) module.AppModule {
return module.NewGenesisOnlyAppModule(AppModule{
AppModuleBasic: AppModuleBasic{},
accountKeeper: accountKeeper,
stakingKeeper: stakingKeeper,
deliverTx: deliverTx,
txEncodingConfig: txEncodingConfig,
})
}
// InitGenesis performs genesis initialization for the genutil module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState types.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
return InitGenesis(ctx, am.stakingKeeper, am.deliverTx, genesisState, am.txEncodingConfig)
}
// ExportGenesis returns the exported genesis state as raw bytes for the genutil
// module.
func (am AppModule) ExportGenesis(_ sdk.Context, cdc codec.JSONMarshaler) json.RawMessage {
return am.DefaultGenesis(cdc)
}