Skip to content

Commit

Permalink
chore: upstream runtime/v2 (cosmos#20320)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
kocubinski and julienrbrt authored May 14, 2024
1 parent b4f8815 commit 7ae23e2
Show file tree
Hide file tree
Showing 37 changed files with 5,864 additions and 66 deletions.
2,909 changes: 2,909 additions & 0 deletions api/cosmos/app/runtime/v2/module.pulsar.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions core/appmodule/v2/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ type HasGenesis interface {
InitGenesis(ctx context.Context, data json.RawMessage) error
ExportGenesis(ctx context.Context) (json.RawMessage, error)
}
type HasABCIGenesis interface {
InitGenesis(ctx context.Context, data json.RawMessage) ([]ValidatorUpdate, error)
}
4 changes: 1 addition & 3 deletions core/appmodule/v2/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ 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
type Message = gogoproto.Message

func messageName[M Message]() string {
switch m := any(*new(M)).(type) {
Expand Down
7 changes: 6 additions & 1 deletion core/context/context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package appmodule
package context

// ExecMode defines the execution mode which can be set on a Context.
type ExecMode uint8
Expand All @@ -14,3 +14,8 @@ const (
ExecModeVerifyVoteExtension
ExecModeFinalize
)

const (
ExecModeKey = iota
CometInfoKey
)
6 changes: 3 additions & 3 deletions core/transaction/transaction.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package transaction

import (
"github.com/cosmos/gogoproto/proto"
gogoproto "github.com/cosmos/gogoproto/proto"
)

type (
Msg = proto.Message
Msg = gogoproto.Message
Identity = []byte
)

Expand All @@ -22,7 +22,7 @@ type Tx interface {
// Hash returns the unique identifier for the Tx.
Hash() [32]byte
// GetMessages returns the list of state transitions of the Tx.
GetMessages() []Msg
GetMessages() ([]Msg, error)
// GetSenders returns the tx state transition sender.
GetSenders() ([]Identity, error) // TODO reduce this to a single identity if accepted
// GetGasLimit returns the gas limit of the tx. Must return math.MaxUint64 for infinite gas
Expand Down
6 changes: 4 additions & 2 deletions go.work.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
go 1.22
go 1.22.2

toolchain go1.22
toolchain go1.22.2

use (
.
Expand All @@ -16,8 +16,10 @@ use (
./simapp
./tests
./server/v2/stf
./server/v2/appmanager
./store
./store/v2
./runtime/v2
./tools/cosmovisor
./tools/confix
./tools/hubl
Expand Down
73 changes: 73 additions & 0 deletions proto/cosmos/app/runtime/v2/module.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
syntax = "proto3";

package cosmos.app.runtime.v2;

import "cosmos/app/v1alpha1/module.proto";

// Module is the config object for the runtime module.
message Module {
option (cosmos.app.v1alpha1.module) = {
go_import: "cosmossdk.io/runtime/v2"
use_package: {name: "cosmos.app.v1alpha1"}
};

// app_name is the name of the app.
string app_name = 1;

// pre_blockers specifies the module names of pre blockers
// to call in the order in which they should be called. If this is left empty
// no pre blocker will be registered.
repeated string pre_blockers = 2;

// begin_blockers specifies the module names of begin blockers
// to call in the order in which they should be called. If this is left empty
// no begin blocker will be registered.
repeated string begin_blockers = 3;

// end_blockers specifies the module names of the end blockers
// to call in the order in which they should be called. If this is left empty
// no end blocker will be registered.
repeated string end_blockers = 4;

// tx_validators specifies the module names for tx validators
// If this is left empty, no tx validation will be registered.
repeated string tx_validators = 5;

// init_genesis specifies the module names of init genesis functions
// to call in the order in which they should be called. If this is left empty
// no init genesis function will be registered.
repeated string init_genesis = 6;

// export_genesis specifies the order in which to export module genesis data.
// If this is left empty, the init_genesis order will be used for export genesis
// if it is specified.
repeated string export_genesis = 7;

// order_migrations defines the order in which module migrations are performed.
// If this is left empty, it uses the default migration order (alphabeticaly).
repeated string order_migrations = 8;

// GasConfig is the config object for gas limits.
GasConfig gas_config = 9;

// override_store_keys is an optional list of overrides for the module store keys
// to be used in keeper construction.
repeated StoreKeyConfig override_store_keys = 10;
}

// GasConfig is the config object for gas limits.
message GasConfig {
uint64 validate_tx_gas_limit = 1;
uint64 query_gas_limit = 2;
uint64 simulation_gas_limit = 3;
}

// StoreKeyConfig may be supplied to override the default module store key, which
// is the module name.
message StoreKeyConfig {
// name of the module to override the store key of
string module_name = 1;

// the kv store key to use instead of the module name.
string kv_store_key = 2;
}
130 changes: 130 additions & 0 deletions runtime/v2/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package runtime

import (
"context"
"encoding/json"
"errors"

"golang.org/x/exp/slices"

runtimev2 "cosmossdk.io/api/cosmos/app/runtime/v2"
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
coreappmanager "cosmossdk.io/core/app"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
"cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/server/v2/stf"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
)

var _ AppI[transaction.Tx] = (*App)(nil)

// AppI is an interface that defines the methods required by the App.
type AppI[T transaction.Tx] interface {
DeliverBlock(
ctx context.Context,
block *coreappmanager.BlockRequest[T],
) (*coreappmanager.BlockResponse, store.WriterMap, error)
ValidateTx(ctx context.Context, tx T) (coreappmanager.TxResult, error)
Simulate(ctx context.Context, tx T) (coreappmanager.TxResult, store.WriterMap, error)
Query(ctx context.Context, version uint64, request transaction.Msg) (transaction.Msg, error)
QueryWithState(ctx context.Context, state store.ReaderMap, request transaction.Msg) (transaction.Msg, error)

Logger() log.Logger
ModuleManager() *MM
Close() error
}

// App is a wrapper around AppManager and ModuleManager that can be used in hybrid
// app.go/app config scenarios or directly as a servertypes.Application instance.
// To get an instance of *App, *AppBuilder must be requested as a dependency
// in a container which declares the runtime module and the AppBuilder.Build()
// method must be called.
//
// App can be used to create a hybrid app.go setup where some configuration is
// done declaratively with an app config and the rest of it is done the old way.
// See simapp/app_v2.go for an example of this setup.
type App struct {
*appmanager.AppManager[transaction.Tx]

// app manager dependencies
stf *stf.STF[transaction.Tx]
msgRouterBuilder *stf.MsgRouterBuilder
queryRouterBuilder *stf.MsgRouterBuilder
db Store

// app configuration
logger log.Logger
config *runtimev2.Module
appConfig *appv1alpha1.Config

// modules configuration
storeKeys []string
interfaceRegistry codectypes.InterfaceRegistry
cdc codec.Codec
amino *codec.LegacyAmino
moduleManager *MM
}

// Logger returns the app logger.
func (a *App) Logger() log.Logger {
return a.logger
}

// ModuleManager returns the module manager.
func (a *App) ModuleManager() *MM {
return a.moduleManager
}

// DefaultGenesis returns a default genesis from the registered modules.
func (a *App) DefaultGenesis() map[string]json.RawMessage {
return a.moduleManager.DefaultGenesis()
}

// LoadLatest loads the latest version.
func (a *App) LoadLatest() error {
return a.db.LoadLatestVersion()
}

// LoadHeight loads a particular height
func (a *App) LoadHeight(height uint64) error {
return a.db.LoadVersion(height)
}

// Close is called in start cmd to gracefully cleanup resources.
func (a *App) Close() error {
return nil
}

// GetStoreKeys returns all the app store keys.
func (a *App) GetStoreKeys() []string {
return a.storeKeys
}

// UnsafeFindStoreKey fetches a registered StoreKey from the App in linear time.
// NOTE: This should only be used in testing.
func (a *App) UnsafeFindStoreKey(storeKey string) (string, error) {
i := slices.IndexFunc(a.storeKeys, func(s string) bool { return s == storeKey })
if i == -1 {
return "", errors.New("store key not found")
}

return a.storeKeys[i], nil
}

// GetStore returns the app store.
func (a *App) GetStore() Store {
return a.db
}

// GetLogger returns the app logger.
func (a *App) GetLogger() log.Logger {
return a.logger
}

func (a *App) ExecuteGenesisTx(_ []byte) error {
panic("not implemented")
}
Loading

0 comments on commit 7ae23e2

Please sign in to comment.