120 lines
3.8 KiB
Go
120 lines
3.8 KiB
Go
package circuit
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
|
|
"google.golang.org/grpc"
|
|
|
|
"cosmossdk.io/core/appmodule"
|
|
"cosmossdk.io/x/circuit/keeper"
|
|
"cosmossdk.io/x/circuit/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
"github.com/cosmos/cosmos-sdk/telemetry"
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
|
)
|
|
|
|
// ConsensusVersion defines the current circuit module consensus version.
|
|
const ConsensusVersion = 1
|
|
|
|
var (
|
|
_ module.AppModuleBasic = AppModule{}
|
|
_ module.HasGenesis = AppModule{}
|
|
|
|
_ appmodule.AppModule = AppModule{}
|
|
_ appmodule.HasServices = AppModule{}
|
|
)
|
|
|
|
// AppModuleBasic defines the basic application module used by the circuit module.
|
|
type AppModuleBasic struct {
|
|
cdc codec.Codec
|
|
}
|
|
|
|
// Name returns the circuit module's name.
|
|
func (AppModuleBasic) Name() string { return types.ModuleName }
|
|
|
|
// RegisterLegacyAminoCodec registers the circuit module's types on the LegacyAmino codec.
|
|
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
|
|
}
|
|
|
|
// DefaultGenesis returns default genesis state as raw bytes for the circuit
|
|
// module.
|
|
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
|
|
return cdc.MustMarshalJSON(types.DefaultGenesisState())
|
|
}
|
|
|
|
// ValidateGenesis performs genesis state validation for the circuit module.
|
|
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 fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
|
|
}
|
|
|
|
return data.Validate()
|
|
}
|
|
|
|
// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the circuit module.
|
|
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *gwruntime.ServeMux) {
|
|
if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// RegisterInterfaces registers interfaces and implementations of the circuit module.
|
|
func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
|
|
types.RegisterInterfaces(registry)
|
|
}
|
|
|
|
// AppModule implements an application module for the circuit module.
|
|
type AppModule struct {
|
|
AppModuleBasic
|
|
|
|
keeper keeper.Keeper
|
|
}
|
|
|
|
// IsAppModule implements the appmodule.AppModule interface.
|
|
func (am AppModule) IsAppModule() {}
|
|
|
|
// RegisterServices registers module services.
|
|
func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {
|
|
types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.keeper))
|
|
types.RegisterQueryServer(registrar, keeper.NewQueryServer(am.keeper))
|
|
|
|
return nil
|
|
}
|
|
|
|
// NewAppModule creates a new AppModule object
|
|
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper) AppModule {
|
|
return AppModule{
|
|
AppModuleBasic: AppModuleBasic{cdc: cdc},
|
|
keeper: keeper,
|
|
}
|
|
}
|
|
|
|
// ConsensusVersion implements AppModule/ConsensusVersion.
|
|
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
|
|
|
|
// InitGenesis performs genesis initialization for the circuit module. It returns
|
|
// no validator updates.
|
|
func (am AppModule) InitGenesis(ctx context.Context, cdc codec.JSONCodec, data json.RawMessage) {
|
|
start := time.Now()
|
|
var genesisState types.GenesisState
|
|
cdc.MustUnmarshalJSON(data, &genesisState)
|
|
telemetry.MeasureSince(start, "InitGenesis", "crisis", "unmarshal")
|
|
|
|
am.keeper.InitGenesis(ctx, &genesisState)
|
|
}
|
|
|
|
// ExportGenesis returns the exported genesis state as raw bytes for the circuit
|
|
// module.
|
|
func (am AppModule) ExportGenesis(ctx context.Context, cdc codec.JSONCodec) json.RawMessage {
|
|
gs := am.keeper.ExportGenesis(ctx)
|
|
return cdc.MustMarshalJSON(gs)
|
|
}
|