chore(core): bring changes from serverv2 (#19617)
This commit is contained in:
@@ -1,21 +1,8 @@
|
||||
package appmodule
|
||||
|
||||
import (
|
||||
"cosmossdk.io/core/branch"
|
||||
"cosmossdk.io/core/event"
|
||||
"cosmossdk.io/core/gas"
|
||||
"cosmossdk.io/core/header"
|
||||
"cosmossdk.io/core/store"
|
||||
"cosmossdk.io/log"
|
||||
appmodule "cosmossdk.io/core/appmodule/v2"
|
||||
)
|
||||
|
||||
// Environment is used to get all services to their respective module
|
||||
type Environment struct {
|
||||
BranchService branch.Service
|
||||
EventService event.Service
|
||||
GasService gas.Service
|
||||
HeaderService header.Service
|
||||
KVStoreService store.KVStoreService
|
||||
MemStoreService store.MemoryStoreService
|
||||
Logger log.Logger
|
||||
}
|
||||
type Environment = appmodule.Environment
|
||||
|
||||
+14
-27
@@ -5,20 +5,27 @@ import (
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/protobuf/runtime/protoiface"
|
||||
|
||||
appmodule "cosmossdk.io/core/appmodule/v2"
|
||||
)
|
||||
|
||||
// AppModule is a tag interface for app module implementations to use as a basis
|
||||
// for extension interfaces. It provides no functionality itself, but is the
|
||||
// type that all valid app modules should provide so that they can be identified
|
||||
// by other modules (usually via depinject) as app modules.
|
||||
type AppModule interface {
|
||||
// IsAppModule is a dummy method to tag a struct as implementing an AppModule.
|
||||
IsAppModule()
|
||||
type AppModule = appmodule.AppModule
|
||||
|
||||
// IsOnePerModuleType is a dummy method to help depinject resolve modules.
|
||||
IsOnePerModuleType()
|
||||
// HasMigrations is the extension interface that modules should implement to register migrations.
|
||||
type HasMigrations interface {
|
||||
AppModule
|
||||
|
||||
// RegisterMigrations registers the module's migrations with the app's migrator.
|
||||
RegisterMigrations(MigrationRegistrar) error
|
||||
}
|
||||
|
||||
// HasConsensusVersion is the interface for declaring a module consensus version.
|
||||
type HasConsensusVersion = appmodule.HasConsensusVersion
|
||||
|
||||
// HasServices is the extension interface that modules should implement to register
|
||||
// implementations of services defined in .proto files.
|
||||
type HasServices interface {
|
||||
@@ -39,14 +46,6 @@ type HasServices interface {
|
||||
RegisterServices(grpc.ServiceRegistrar) error
|
||||
}
|
||||
|
||||
// HasMigrations is the extension interface that modules should implement to register migrations.
|
||||
type HasMigrations interface {
|
||||
AppModule
|
||||
|
||||
// RegisterMigrations registers the module's migrations with the app's migrator.
|
||||
RegisterMigrations(MigrationRegistrar) error
|
||||
}
|
||||
|
||||
// ResponsePreBlock represents the response from the PreBlock method.
|
||||
// It can modify consensus parameters in storage and signal the caller through the return value.
|
||||
// When it returns ConsensusParamsChanged=true, the caller must refresh the consensus parameter in the finalize context.
|
||||
@@ -65,23 +64,11 @@ type HasPreBlocker interface {
|
||||
|
||||
// HasBeginBlocker is the extension interface that modules should implement to run
|
||||
// custom logic before transaction processing in a block.
|
||||
type HasBeginBlocker interface {
|
||||
AppModule
|
||||
|
||||
// BeginBlock is a method that will be run before transactions are processed in
|
||||
// a block.
|
||||
BeginBlock(context.Context) error
|
||||
}
|
||||
type HasBeginBlocker = appmodule.HasBeginBlocker
|
||||
|
||||
// HasEndBlocker is the extension interface that modules should implement to run
|
||||
// custom logic after transaction processing in a block.
|
||||
type HasEndBlocker interface {
|
||||
AppModule
|
||||
|
||||
// EndBlock is a method that will be run after transactions are processed in
|
||||
// a block.
|
||||
EndBlock(context.Context) error
|
||||
}
|
||||
type HasEndBlocker = appmodule.HasEndBlocker
|
||||
|
||||
// MsgHandlerRouter is implemented by the runtime provider.
|
||||
type MsgHandlerRouter interface {
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package appmodule
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cosmossdk.io/core/transaction"
|
||||
)
|
||||
|
||||
// AppModule is a tag interface for app module implementations to use as a basis
|
||||
// for extension interfaces. It provides no functionality itself, but is the
|
||||
// type that all valid app modules should provide so that they can be identified
|
||||
// by other modules (usually via depinject) as app modules.
|
||||
type AppModule interface {
|
||||
// IsAppModule is a dummy method to tag a struct as implementing an AppModule.
|
||||
IsAppModule()
|
||||
|
||||
// IsOnePerModuleType is a dummy method to help depinject resolve modules.
|
||||
IsOnePerModuleType()
|
||||
}
|
||||
|
||||
// HasBeginBlocker is the extension interface that modules should implement to run
|
||||
// custom logic before transaction processing in a block.
|
||||
type HasBeginBlocker interface {
|
||||
AppModule
|
||||
|
||||
// BeginBlock is a method that will be run before transactions are processed in
|
||||
// a block.
|
||||
BeginBlock(context.Context) error
|
||||
}
|
||||
|
||||
// HasEndBlocker is the extension interface that modules should implement to run
|
||||
// custom logic after transaction processing in a block.
|
||||
type HasEndBlocker interface {
|
||||
AppModule
|
||||
|
||||
// EndBlock is a method that will be run after transactions are processed in
|
||||
// a block.
|
||||
EndBlock(context.Context) error
|
||||
}
|
||||
|
||||
// HasTxValidation is the extension interface that modules should implement to run
|
||||
// custom logic for validating transactions.
|
||||
// It was previously known as AnteHandler/Decorator.
|
||||
type HasTxValidation[T transaction.Tx] interface {
|
||||
AppModule
|
||||
|
||||
// TxValidator is a method that will be run on each transaction.
|
||||
// If an error is returned:
|
||||
// ,---.
|
||||
// / |
|
||||
// / |
|
||||
// You shall not pass! / |
|
||||
// / |
|
||||
// \ ___,' |
|
||||
// < -' :
|
||||
// `-.__..--'``-,_\_
|
||||
// |o/ <o>` :,.)_`>
|
||||
// :/ ` ||/)
|
||||
// (_.).__,-` |\
|
||||
// /( `.`` `| :
|
||||
// \'`-.) ` ; ;
|
||||
// | ` /-<
|
||||
// | ` / `.
|
||||
// ,-_-..____ /| ` :__..-'\
|
||||
// /,'-.__\\ ``-./ :` ; \
|
||||
// `\ `\ `\\ \ : ( ` / , `. \
|
||||
// \` \ \\ | | ` : : .\ \
|
||||
// \ `\_ )) : ; | | ): :
|
||||
// (`-.-'\ || |\ \ ` ; ; | |
|
||||
// \-_ `;;._ ( ` / /_ | |
|
||||
// `-.-.// ,'`-._\__/_,' ; |
|
||||
// \:: : / ` , / |
|
||||
// || | ( ,' / / |
|
||||
// || ,' / |
|
||||
TxValidator(ctx context.Context, tx T) error
|
||||
}
|
||||
|
||||
// HasUpdateValidators is an extension interface that contains information about the AppModule and UpdateValidators.
|
||||
// It can be seen as the alternative of the Cosmos SDK' HasABCIEndBlocker.
|
||||
// Both are still supported.
|
||||
type HasUpdateValidators interface {
|
||||
AppModule
|
||||
|
||||
UpdateValidators(ctx context.Context) ([]ValidatorUpdate, error)
|
||||
}
|
||||
|
||||
// ValidatorUpdate defines a validator update.
|
||||
type ValidatorUpdate struct {
|
||||
PubKey []byte
|
||||
PubKeyType string
|
||||
Power int64 // updated power of the validtor
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package appmodule
|
||||
|
||||
import (
|
||||
"cosmossdk.io/core/branch"
|
||||
"cosmossdk.io/core/event"
|
||||
"cosmossdk.io/core/gas"
|
||||
"cosmossdk.io/core/header"
|
||||
"cosmossdk.io/core/store"
|
||||
"cosmossdk.io/log"
|
||||
)
|
||||
|
||||
// Environment is used to get all services to their respective module
|
||||
type Environment struct {
|
||||
BranchService branch.Service
|
||||
EventService event.Service
|
||||
GasService gas.Service
|
||||
HeaderService header.Service
|
||||
KVStoreService store.KVStoreService
|
||||
MemStoreService store.MemoryStoreService
|
||||
DataBaseService store.DatabaseService
|
||||
Logger log.Logger
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package appmodule
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// HasGenesis defines a custom genesis handling API implementation.
|
||||
// WARNING: this API is meant as a short-term solution to allow for the
|
||||
// migration of existing modules to the new app module API. It is intended to be replaced by collections
|
||||
type HasGenesis interface {
|
||||
AppModule
|
||||
DefaultGenesis() Message
|
||||
ValidateGenesis(data json.RawMessage) error
|
||||
InitGenesis(ctx context.Context, data json.RawMessage) error
|
||||
ExportGenesis(ctx context.Context) (json.RawMessage, error)
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package appmodule
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type (
|
||||
// PreMsgHandler is a handler that is executed before Handler. If it errors the execution reverts.
|
||||
PreMsgHandler = func(ctx context.Context, msg Message) error
|
||||
// Handler handles the state transition of the provided message.
|
||||
Handler = func(ctx context.Context, msg Message) (msgResp Message, err error)
|
||||
// PostMsgHandler runs after Handler, only if Handler does not error. If PostMsgHandler errors
|
||||
// then the execution is reverted.
|
||||
PostMsgHandler = func(ctx context.Context, msg, msgResp Message) error
|
||||
)
|
||||
|
||||
// RegisterHandler is a helper function that modules can use to not lose type safety when registering handlers to the
|
||||
// QueryRouter or MsgRouter. Example usage:
|
||||
// ```go
|
||||
//
|
||||
// func (k Keeper) QueryBalance(ctx context.Context, req *types.QueryBalanceRequest) (*types.QueryBalanceResponse, error) {
|
||||
// ... query logic ...
|
||||
// }
|
||||
//
|
||||
// func (m Module) RegisterQueryHandlers(router appmodule.QueryRouter) {
|
||||
// appmodule.RegisterHandler(router, keeper.QueryBalance)
|
||||
// }
|
||||
//
|
||||
// ```
|
||||
func RegisterHandler[R interface{ Register(string, Handler) }, Req, Resp Message](
|
||||
router R,
|
||||
handler func(ctx context.Context, msg Req) (msgResp Resp, err error),
|
||||
) {
|
||||
untypedHandler := func(ctx context.Context, m Message) (Message, error) {
|
||||
typed, ok := m.(Req)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected type %T, wanted: %T", m, *new(Req))
|
||||
}
|
||||
return handler(ctx, typed)
|
||||
}
|
||||
router.Register(messageName[Req](), untypedHandler)
|
||||
}
|
||||
|
||||
// RegisterPreHandler is a helper function that modules can use to not lose type safety when registering PreMsgHandler to the
|
||||
// PreMsgRouter. Example usage:
|
||||
// ```go
|
||||
//
|
||||
// func (k Keeper) BeforeSend(ctx context.Context, req *types.MsgSend) (*types.QueryBalanceResponse, error) {
|
||||
// ... before send logic ...
|
||||
// }
|
||||
//
|
||||
// func (m Module) RegisterPreMsgHandlers(router appmodule.PreMsgRouter) {
|
||||
// appmodule.RegisterPreHandler(router, keeper.BeforeSend)
|
||||
// }
|
||||
//
|
||||
// ```
|
||||
func RegisterPreHandler[Req Message](
|
||||
router PreMsgRouter,
|
||||
handler func(ctx context.Context, msg Req) error,
|
||||
) {
|
||||
untypedHandler := func(ctx context.Context, m Message) error {
|
||||
typed, ok := m.(Req)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T, wanted: %T", m, *new(Req))
|
||||
}
|
||||
return handler(ctx, typed)
|
||||
}
|
||||
router.Register(messageName[Req](), untypedHandler)
|
||||
}
|
||||
|
||||
// RegisterPostHandler is a helper function that modules can use to not lose type safety when registering handlers to the
|
||||
// PostMsgRouter. Example usage:
|
||||
// ```go
|
||||
//
|
||||
// func (k Keeper) AfterSend(ctx context.Context, req *types.MsgSend, resp *types.MsgSendResponse) error {
|
||||
// ... query logic ...
|
||||
// }
|
||||
//
|
||||
// func (m Module) RegisterPostMsgHandlers(router appmodule.PostMsgRouter) {
|
||||
// appmodule.RegisterPostHandler(router, keeper.AfterSend)
|
||||
// }
|
||||
//
|
||||
// ```
|
||||
func RegisterPostHandler[Req, Resp Message](
|
||||
router PostMsgRouter,
|
||||
handler func(ctx context.Context, msg Req, msgResp Resp) error,
|
||||
) {
|
||||
untypedHandler := func(ctx context.Context, m, mResp Message) error {
|
||||
typed, ok := m.(Req)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T, wanted: %T", m, *new(Req))
|
||||
}
|
||||
typedResp, ok := mResp.(Resp)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T, wanted: %T", m, *new(Resp))
|
||||
}
|
||||
return handler(ctx, typed, typedResp)
|
||||
}
|
||||
router.Register(messageName[Req](), untypedHandler)
|
||||
}
|
||||
|
||||
// msg handler
|
||||
|
||||
type PreMsgRouter interface {
|
||||
// Register will register a specific message handler hooking into the message with
|
||||
// the provided name.
|
||||
Register(msgName string, handler PreMsgHandler)
|
||||
// RegisterGlobal will register a global message handler hooking into any message
|
||||
// being executed.
|
||||
RegisterGlobal(handler PreMsgHandler)
|
||||
}
|
||||
|
||||
type HasPreMsgHandlers interface {
|
||||
RegisterPreMsgHandlers(router PreMsgRouter)
|
||||
}
|
||||
|
||||
type MsgRouter interface {
|
||||
Register(msgName string, handler Handler)
|
||||
}
|
||||
|
||||
type HasMsgHandlers interface {
|
||||
RegisterMsgHandlers(router MsgRouter)
|
||||
}
|
||||
|
||||
type PostMsgRouter interface {
|
||||
// Register will register a specific message handler hooking after the execution of message with
|
||||
// the provided name.
|
||||
Register(msgName string, handler PostMsgHandler)
|
||||
// RegisterGlobal will register a global message handler hooking after the execution of any message.
|
||||
RegisterGlobal(handler PreMsgHandler)
|
||||
}
|
||||
|
||||
// query handler
|
||||
|
||||
type QueryRouter interface {
|
||||
Register(queryName string, handler Handler)
|
||||
}
|
||||
|
||||
type HasQueryHandlers interface {
|
||||
RegisterQueryHandlers(router QueryRouter)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package appmodule
|
||||
|
||||
import (
|
||||
gogoproto "github.com/cosmos/gogoproto/proto"
|
||||
protov2 "google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/runtime/protoiface"
|
||||
)
|
||||
|
||||
// Message aliases protoiface.MessageV1 for convenience.
|
||||
type Message = protoiface.MessageV1
|
||||
|
||||
func messageName[M Message]() string {
|
||||
switch m := any(*new(M)).(type) {
|
||||
case protov2.Message:
|
||||
return string(m.ProtoReflect().Descriptor().FullName())
|
||||
case gogoproto.Message:
|
||||
return gogoproto.MessageName(m)
|
||||
default:
|
||||
panic("unknown message type")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package appmodule
|
||||
|
||||
import "context"
|
||||
|
||||
// HasConsensusVersion is the interface for declaring a module consensus version.
|
||||
type HasConsensusVersion interface {
|
||||
// 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.
|
||||
ConsensusVersion() uint64
|
||||
}
|
||||
|
||||
// HasMigrations is implemented by a module which upgrades or has upgraded
|
||||
// to a new consensus version.
|
||||
type HasMigrations interface {
|
||||
AppModule
|
||||
HasConsensusVersion
|
||||
|
||||
// RegisterMigrations registers the module's migrations with the app's migrator.
|
||||
RegisterMigrations(MigrationRegistrar) error
|
||||
}
|
||||
|
||||
type MigrationRegistrar interface {
|
||||
// Register registers an in-place store migration for a module. The
|
||||
// handler is a migration script to perform in-place migrations from version
|
||||
// `fromVersion` to version `fromVersion+1`.
|
||||
//
|
||||
// EACH TIME a module's ConsensusVersion increments, a new migration MUST
|
||||
// be registered using this function. If a migration handler is missing for
|
||||
// a particular function, the upgrade logic (see RunMigrations function)
|
||||
// will panic. If the ConsensusVersion bump does not introduce any store
|
||||
// changes, then a no-op function must be registered here.
|
||||
Register(moduleName string, fromVersion uint64, handler MigrationHandler) error
|
||||
}
|
||||
|
||||
// MigrationHandler is the migration function that each module registers.
|
||||
type MigrationHandler func(context.Context) error
|
||||
|
||||
// VersionMap is a map of moduleName -> version
|
||||
type VersionMap map[string]uint64
|
||||
Reference in New Issue
Block a user