Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Oct 17, 2024
1 parent c7ddc39 commit 61f7539
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 44 deletions.
2 changes: 1 addition & 1 deletion runtime/v2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// 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[T transaction.Tx] struct {
*appmanager.AppManager[T]
appmanager.AppManager[T]

// app configuration
logger log.Logger
Expand Down
6 changes: 1 addition & 5 deletions runtime/v2/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) {
}
a.app.stf = stf

appManager, err := appmanager.New[T](
a.app.AppManager = appmanager.New[T](
appmanager.Config{
ValidateTxGasLimit: a.app.config.GasConfig.ValidateTxGasLimit,
QueryGasLimit: a.app.config.GasConfig.QueryGasLimit,
Expand All @@ -121,10 +121,6 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) {
a.initGenesis,
a.exportGenesis,
)
if err != nil {
return nil, fmt.Errorf("failed to create AppManager: %w", err)
}
a.app.AppManager = appManager

return a.app, nil
}
Expand Down
2 changes: 1 addition & 1 deletion server/v2/api/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *Server[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logger log.L
grpc.MaxSendMsgSize(serverCfg.MaxSendMsgSize),
grpc.MaxRecvMsgSize(serverCfg.MaxRecvMsgSize),
grpc.UnknownServiceHandler(
makeUnknownServiceHandler(methodsMap, appI.AppManager()),
makeUnknownServiceHandler(methodsMap, appI),
),
)

Expand Down
4 changes: 2 additions & 2 deletions server/v2/api/rest/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ const (
MaxBodySize = 1 << 20 // 1 MB
)

func NewDefaultHandler[T transaction.Tx](appManager *appmanager.AppManager[T]) http.Handler {
func NewDefaultHandler[T transaction.Tx](appManager appmanager.AppManager[T]) http.Handler {
return &DefaultHandler[T]{appManager: appManager}
}

type DefaultHandler[T transaction.Tx] struct {
appManager *appmanager.AppManager[T]
appManager appmanager.AppManager[T]
}

func (h *DefaultHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion server/v2/api/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *Server[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logger log.L
}

s.router = http.NewServeMux()
s.router.Handle("/", NewDefaultHandler(appI.AppManager()))
s.router.Handle("/", NewDefaultHandler(appI))
s.config = serverCfg

return nil
Expand Down
72 changes: 56 additions & 16 deletions server/v2/appmanager/appmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,45 @@ import (
"cosmossdk.io/core/transaction"
)

// AppManager is a coordinator for all things related to an application
// It is responsible for interacting with stf and store.
// Runtime/v2 is an extension of this interface.
type AppManager[T transaction.Tx] interface {
// InitGenesis initializes the genesis state of the application.
InitGenesis(
ctx context.Context,
blockRequest *server.BlockRequest[T],
initGenesisJSON []byte,
txDecoder transaction.Codec[T],
) (*server.BlockResponse, corestore.WriterMap, error)

// ExportGenesis exports the genesis state of the application.
ExportGenesis(ctx context.Context, version uint64) ([]byte, error)

// DeliverBlock executes a block of transactions.
DeliverBlock(
ctx context.Context,
block *server.BlockRequest[T],
) (*server.BlockResponse, corestore.WriterMap, error)

// ValidateTx will validate the tx against the latest storage state. This means that
// only the stateful validation will be run, not the execution portion of the tx.
// If full execution is needed, Simulate must be used.
ValidateTx(ctx context.Context, tx T) (server.TxResult, error)

// Simulate runs validation and execution flow of a Tx.
Simulate(ctx context.Context, tx T) (server.TxResult, corestore.WriterMap, error)

// Query queries the application at the provided version.
// CONTRACT: Version must always be provided, if 0, get latest
Query(ctx context.Context, version uint64, request transaction.Msg) (transaction.Msg, error)

// QueryWithState executes a query with the provided state. This allows to process a query
// independently of the db state. For example, it can be used to process a query with temporary
// and uncommitted state
QueryWithState(ctx context.Context, state corestore.ReaderMap, request transaction.Msg) (transaction.Msg, error)
}

// Store defines the underlying storage behavior needed by AppManager.
type Store interface {
// StateLatest returns a readonly view over the latest
Expand All @@ -24,8 +63,8 @@ type Store interface {
StateAt(version uint64) (corestore.ReaderMap, error)
}

// AppManager is a coordinator for all things related to an application
type AppManager[T transaction.Tx] struct {
// appManager is a coordinator for all things related to an application
type appManager[T transaction.Tx] struct {
// Gas limits for validating, querying, and simulating transactions.
config Config
// InitGenesis is a function that initializes the application state from a genesis file.
Expand All @@ -46,18 +85,18 @@ func New[T transaction.Tx](
stf StateTransitionFunction[T],
initGenesisImpl InitGenesis,
exportGenesisImpl ExportGenesis,
) (*AppManager[T], error) {
appManager := &AppManager[T]{
config: config,
db: db,
stf: stf,
) AppManager[T] {
return &appManager[T]{
config: config,
db: db,
stf: stf,
initGenesis: initGenesisImpl,
exportGenesis: exportGenesisImpl,
}

return appManager, nil
}

// InitGenesis initializes the genesis state of the application.
func (a AppManager[T]) InitGenesis(
func (a appManager[T]) InitGenesis(
ctx context.Context,
blockRequest *server.BlockRequest[T],
initGenesisJSON []byte,
Expand Down Expand Up @@ -102,15 +141,16 @@ func (a AppManager[T]) InitGenesis(
}

// ExportGenesis exports the genesis state of the application.
func (a AppManager[T]) ExportGenesis(ctx context.Context, version uint64) ([]byte, error) {
func (a appManager[T]) ExportGenesis(ctx context.Context, version uint64) ([]byte, error) {
if a.exportGenesis == nil {
return nil, errors.New("export genesis function not set")
}

return a.exportGenesis(ctx, version)
}

func (a AppManager[T]) DeliverBlock(
// DeliverBlock executes a block of transactions.
func (a appManager[T]) DeliverBlock(
ctx context.Context,
block *server.BlockRequest[T],
) (*server.BlockResponse, corestore.WriterMap, error) {
Expand All @@ -134,7 +174,7 @@ func (a AppManager[T]) DeliverBlock(
// ValidateTx will validate the tx against the latest storage state. This means that
// only the stateful validation will be run, not the execution portion of the tx.
// If full execution is needed, Simulate must be used.
func (a AppManager[T]) ValidateTx(ctx context.Context, tx T) (server.TxResult, error) {
func (a appManager[T]) ValidateTx(ctx context.Context, tx T) (server.TxResult, error) {
_, latestState, err := a.db.StateLatest()
if err != nil {
return server.TxResult{}, err
Expand All @@ -144,7 +184,7 @@ func (a AppManager[T]) ValidateTx(ctx context.Context, tx T) (server.TxResult, e
}

// Simulate runs validation and execution flow of a Tx.
func (a AppManager[T]) Simulate(ctx context.Context, tx T) (server.TxResult, corestore.WriterMap, error) {
func (a appManager[T]) Simulate(ctx context.Context, tx T) (server.TxResult, corestore.WriterMap, error) {
_, state, err := a.db.StateLatest()
if err != nil {
return server.TxResult{}, nil, err
Expand All @@ -155,7 +195,7 @@ func (a AppManager[T]) Simulate(ctx context.Context, tx T) (server.TxResult, cor

// Query queries the application at the provided version.
// CONTRACT: Version must always be provided, if 0, get latest
func (a AppManager[T]) Query(ctx context.Context, version uint64, request transaction.Msg) (transaction.Msg, error) {
func (a appManager[T]) Query(ctx context.Context, version uint64, request transaction.Msg) (transaction.Msg, error) {
// if version is provided attempt to do a height query.
if version != 0 {
queryState, err := a.db.StateAt(version)
Expand All @@ -176,6 +216,6 @@ func (a AppManager[T]) Query(ctx context.Context, version uint64, request transa
// QueryWithState executes a query with the provided state. This allows to process a query
// independently of the db state. For example, it can be used to process a query with temporary
// and uncommitted state
func (a AppManager[T]) QueryWithState(ctx context.Context, state corestore.ReaderMap, request transaction.Msg) (transaction.Msg, error) {
func (a appManager[T]) QueryWithState(ctx context.Context, state corestore.ReaderMap, request transaction.Msg) (transaction.Msg, error) {
return a.stf.Query(ctx, state, a.config.QueryGasLimit, request)
}
File renamed without changes.
4 changes: 2 additions & 2 deletions server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var _ abci.Application = (*Consensus[transaction.Tx])(nil)
type Consensus[T transaction.Tx] struct {
logger log.Logger
appName, version string
app *appmanager.AppManager[T]
app appmanager.AppManager[T]
appCloser func() error
txCodec transaction.Codec[T]
store types.Store
Expand Down Expand Up @@ -77,7 +77,7 @@ type Consensus[T transaction.Tx] struct {
func NewConsensus[T transaction.Tx](
logger log.Logger,
appName string,
app *appmanager.AppManager[T],
app appmanager.AppManager[T],
appCloser func() error,
mp mempool.Mempool[T],
indexedEvents map[string]struct{},
Expand Down
3 changes: 1 addition & 2 deletions server/v2/cometbft/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
sc := cometmock.NewMockCommiter(log.NewNopLogger(), string(actorName), "stf")
mockStore := cometmock.NewMockStore(ss, sc)

am, err := appmanager.New(appmanager.Config{
am := appmanager.New(appmanager.Config{
ValidateTxGasLimit: gasLimit,
QueryGasLimit: gasLimit,
SimulationGasLimit: gasLimit},
Expand All @@ -685,7 +685,6 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
},
nil,
)
require.NoError(t, err)

return NewConsensus[mock.Tx](log.NewNopLogger(), "testing-app", am, func() error { return nil }, mempool, map[string]struct{}{}, nil, mockStore, Config{AppTomlConfig: DefaultAppTomlConfig()}, mock.TxCodec{}, "test")
}
Expand Down
2 changes: 1 addition & 1 deletion server/v2/cometbft/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logg
consensus := NewConsensus(
s.logger,
appI.Name(),
appI.AppManager(),
appI,
appI.Close,
s.serverOptions.Mempool(cfg),
indexEvents,
Expand Down
13 changes: 1 addition & 12 deletions server/v2/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import (
"cosmossdk.io/log"
serverv2 "cosmossdk.io/server/v2"
grpc "cosmossdk.io/server/v2/api/grpc"
"cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/server/v2/store"
storev2 "cosmossdk.io/store/v2"
)

type mockInterfaceRegistry struct{}
Expand All @@ -37,22 +35,13 @@ type mockApp[T transaction.Tx] struct {
serverv2.AppI[T]
}

func (*mockApp[T]) GetQueryHandlers() map[string]appmodulev2.Handler {
func (*mockApp[T]) QueryHandlers() map[string]appmodulev2.Handler {
return map[string]appmodulev2.Handler{}
}

func (*mockApp[T]) GetAppManager() *appmanager.AppManager[T] {
return nil
}

func (*mockApp[T]) InterfaceRegistry() coreserver.InterfaceRegistry {
return &mockInterfaceRegistry{}
}

func (*mockApp[T]) GetStore() storev2.RootStore {
return nil
}

func TestServer(t *testing.T) {
currentDir, err := os.Getwd()
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion server/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (
type AppCreator[T transaction.Tx] func(log.Logger, *viper.Viper) AppI[T]

type AppI[T transaction.Tx] interface {
appmanager.AppManager[T]

Name() string
InterfaceRegistry() server.InterfaceRegistry
AppManager() *appmanager.AppManager[T]
QueryHandlers() map[string]appmodulev2.Handler
Store() store.RootStore
SchemaDecoderResolver() decoding.DecoderResolver
Expand Down

0 comments on commit 61f7539

Please sign in to comment.