Skip to content

Commit 61f7539

Browse files
committed
simplify
1 parent c7ddc39 commit 61f7539

File tree

12 files changed

+69
-44
lines changed

12 files changed

+69
-44
lines changed

runtime/v2/app.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
// done declaratively with an app config and the rest of it is done the old way.
2424
// See simapp/app_v2.go for an example of this setup.
2525
type App[T transaction.Tx] struct {
26-
*appmanager.AppManager[T]
26+
appmanager.AppManager[T]
2727

2828
// app configuration
2929
logger log.Logger

runtime/v2/builder.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) {
110110
}
111111
a.app.stf = stf
112112

113-
appManager, err := appmanager.New[T](
113+
a.app.AppManager = appmanager.New[T](
114114
appmanager.Config{
115115
ValidateTxGasLimit: a.app.config.GasConfig.ValidateTxGasLimit,
116116
QueryGasLimit: a.app.config.GasConfig.QueryGasLimit,
@@ -121,10 +121,6 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) {
121121
a.initGenesis,
122122
a.exportGenesis,
123123
)
124-
if err != nil {
125-
return nil, fmt.Errorf("failed to create AppManager: %w", err)
126-
}
127-
a.app.AppManager = appManager
128124

129125
return a.app, nil
130126
}

server/v2/api/grpc/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (s *Server[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logger log.L
6464
grpc.MaxSendMsgSize(serverCfg.MaxSendMsgSize),
6565
grpc.MaxRecvMsgSize(serverCfg.MaxRecvMsgSize),
6666
grpc.UnknownServiceHandler(
67-
makeUnknownServiceHandler(methodsMap, appI.AppManager()),
67+
makeUnknownServiceHandler(methodsMap, appI),
6868
),
6969
)
7070

server/v2/api/rest/handler.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ const (
2020
MaxBodySize = 1 << 20 // 1 MB
2121
)
2222

23-
func NewDefaultHandler[T transaction.Tx](appManager *appmanager.AppManager[T]) http.Handler {
23+
func NewDefaultHandler[T transaction.Tx](appManager appmanager.AppManager[T]) http.Handler {
2424
return &DefaultHandler[T]{appManager: appManager}
2525
}
2626

2727
type DefaultHandler[T transaction.Tx] struct {
28-
appManager *appmanager.AppManager[T]
28+
appManager appmanager.AppManager[T]
2929
}
3030

3131
func (h *DefaultHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {

server/v2/api/rest/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (s *Server[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logger log.L
4545
}
4646

4747
s.router = http.NewServeMux()
48-
s.router.Handle("/", NewDefaultHandler(appI.AppManager()))
48+
s.router.Handle("/", NewDefaultHandler(appI))
4949
s.config = serverCfg
5050

5151
return nil

server/v2/appmanager/appmanager.go

+56-16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,45 @@ import (
1212
"cosmossdk.io/core/transaction"
1313
)
1414

15+
// AppManager is a coordinator for all things related to an application
16+
// It is responsible for interacting with stf and store.
17+
// Runtime/v2 is an extension of this interface.
18+
type AppManager[T transaction.Tx] interface {
19+
// InitGenesis initializes the genesis state of the application.
20+
InitGenesis(
21+
ctx context.Context,
22+
blockRequest *server.BlockRequest[T],
23+
initGenesisJSON []byte,
24+
txDecoder transaction.Codec[T],
25+
) (*server.BlockResponse, corestore.WriterMap, error)
26+
27+
// ExportGenesis exports the genesis state of the application.
28+
ExportGenesis(ctx context.Context, version uint64) ([]byte, error)
29+
30+
// DeliverBlock executes a block of transactions.
31+
DeliverBlock(
32+
ctx context.Context,
33+
block *server.BlockRequest[T],
34+
) (*server.BlockResponse, corestore.WriterMap, error)
35+
36+
// ValidateTx will validate the tx against the latest storage state. This means that
37+
// only the stateful validation will be run, not the execution portion of the tx.
38+
// If full execution is needed, Simulate must be used.
39+
ValidateTx(ctx context.Context, tx T) (server.TxResult, error)
40+
41+
// Simulate runs validation and execution flow of a Tx.
42+
Simulate(ctx context.Context, tx T) (server.TxResult, corestore.WriterMap, error)
43+
44+
// Query queries the application at the provided version.
45+
// CONTRACT: Version must always be provided, if 0, get latest
46+
Query(ctx context.Context, version uint64, request transaction.Msg) (transaction.Msg, error)
47+
48+
// QueryWithState executes a query with the provided state. This allows to process a query
49+
// independently of the db state. For example, it can be used to process a query with temporary
50+
// and uncommitted state
51+
QueryWithState(ctx context.Context, state corestore.ReaderMap, request transaction.Msg) (transaction.Msg, error)
52+
}
53+
1554
// Store defines the underlying storage behavior needed by AppManager.
1655
type Store interface {
1756
// StateLatest returns a readonly view over the latest
@@ -24,8 +63,8 @@ type Store interface {
2463
StateAt(version uint64) (corestore.ReaderMap, error)
2564
}
2665

27-
// AppManager is a coordinator for all things related to an application
28-
type AppManager[T transaction.Tx] struct {
66+
// appManager is a coordinator for all things related to an application
67+
type appManager[T transaction.Tx] struct {
2968
// Gas limits for validating, querying, and simulating transactions.
3069
config Config
3170
// InitGenesis is a function that initializes the application state from a genesis file.
@@ -46,18 +85,18 @@ func New[T transaction.Tx](
4685
stf StateTransitionFunction[T],
4786
initGenesisImpl InitGenesis,
4887
exportGenesisImpl ExportGenesis,
49-
) (*AppManager[T], error) {
50-
appManager := &AppManager[T]{
51-
config: config,
52-
db: db,
53-
stf: stf,
88+
) AppManager[T] {
89+
return &appManager[T]{
90+
config: config,
91+
db: db,
92+
stf: stf,
93+
initGenesis: initGenesisImpl,
94+
exportGenesis: exportGenesisImpl,
5495
}
55-
56-
return appManager, nil
5796
}
5897

5998
// InitGenesis initializes the genesis state of the application.
60-
func (a AppManager[T]) InitGenesis(
99+
func (a appManager[T]) InitGenesis(
61100
ctx context.Context,
62101
blockRequest *server.BlockRequest[T],
63102
initGenesisJSON []byte,
@@ -102,15 +141,16 @@ func (a AppManager[T]) InitGenesis(
102141
}
103142

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

110149
return a.exportGenesis(ctx, version)
111150
}
112151

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

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

156196
// Query queries the application at the provided version.
157197
// CONTRACT: Version must always be provided, if 0, get latest
158-
func (a AppManager[T]) Query(ctx context.Context, version uint64, request transaction.Msg) (transaction.Msg, error) {
198+
func (a appManager[T]) Query(ctx context.Context, version uint64, request transaction.Msg) (transaction.Msg, error) {
159199
// if version is provided attempt to do a height query.
160200
if version != 0 {
161201
queryState, err := a.db.StateAt(version)
@@ -176,6 +216,6 @@ func (a AppManager[T]) Query(ctx context.Context, version uint64, request transa
176216
// QueryWithState executes a query with the provided state. This allows to process a query
177217
// independently of the db state. For example, it can be used to process a query with temporary
178218
// and uncommitted state
179-
func (a AppManager[T]) QueryWithState(ctx context.Context, state corestore.ReaderMap, request transaction.Msg) (transaction.Msg, error) {
219+
func (a appManager[T]) QueryWithState(ctx context.Context, state corestore.ReaderMap, request transaction.Msg) (transaction.Msg, error) {
180220
return a.stf.Query(ctx, state, a.config.QueryGasLimit, request)
181221
}
File renamed without changes.

server/v2/cometbft/abci.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var _ abci.Application = (*Consensus[transaction.Tx])(nil)
4242
type Consensus[T transaction.Tx] struct {
4343
logger log.Logger
4444
appName, version string
45-
app *appmanager.AppManager[T]
45+
app appmanager.AppManager[T]
4646
appCloser func() error
4747
txCodec transaction.Codec[T]
4848
store types.Store
@@ -77,7 +77,7 @@ type Consensus[T transaction.Tx] struct {
7777
func NewConsensus[T transaction.Tx](
7878
logger log.Logger,
7979
appName string,
80-
app *appmanager.AppManager[T],
80+
app appmanager.AppManager[T],
8181
appCloser func() error,
8282
mp mempool.Mempool[T],
8383
indexedEvents map[string]struct{},

server/v2/cometbft/abci_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
672672
sc := cometmock.NewMockCommiter(log.NewNopLogger(), string(actorName), "stf")
673673
mockStore := cometmock.NewMockStore(ss, sc)
674674

675-
am, err := appmanager.New(appmanager.Config{
675+
am := appmanager.New(appmanager.Config{
676676
ValidateTxGasLimit: gasLimit,
677677
QueryGasLimit: gasLimit,
678678
SimulationGasLimit: gasLimit},
@@ -685,7 +685,6 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
685685
},
686686
nil,
687687
)
688-
require.NoError(t, err)
689688

690689
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")
691690
}

server/v2/cometbft/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logg
106106
consensus := NewConsensus(
107107
s.logger,
108108
appI.Name(),
109-
appI.AppManager(),
109+
appI,
110110
appI.Close,
111111
s.serverOptions.Mempool(cfg),
112112
indexEvents,

server/v2/server_test.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ import (
1717
"cosmossdk.io/log"
1818
serverv2 "cosmossdk.io/server/v2"
1919
grpc "cosmossdk.io/server/v2/api/grpc"
20-
"cosmossdk.io/server/v2/appmanager"
2120
"cosmossdk.io/server/v2/store"
22-
storev2 "cosmossdk.io/store/v2"
2321
)
2422

2523
type mockInterfaceRegistry struct{}
@@ -37,22 +35,13 @@ type mockApp[T transaction.Tx] struct {
3735
serverv2.AppI[T]
3836
}
3937

40-
func (*mockApp[T]) GetQueryHandlers() map[string]appmodulev2.Handler {
38+
func (*mockApp[T]) QueryHandlers() map[string]appmodulev2.Handler {
4139
return map[string]appmodulev2.Handler{}
4240
}
43-
44-
func (*mockApp[T]) GetAppManager() *appmanager.AppManager[T] {
45-
return nil
46-
}
47-
4841
func (*mockApp[T]) InterfaceRegistry() coreserver.InterfaceRegistry {
4942
return &mockInterfaceRegistry{}
5043
}
5144

52-
func (*mockApp[T]) GetStore() storev2.RootStore {
53-
return nil
54-
}
55-
5645
func TestServer(t *testing.T) {
5746
currentDir, err := os.Getwd()
5847
require.NoError(t, err)

server/v2/types.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import (
1515
type AppCreator[T transaction.Tx] func(log.Logger, *viper.Viper) AppI[T]
1616

1717
type AppI[T transaction.Tx] interface {
18+
appmanager.AppManager[T]
19+
1820
Name() string
1921
InterfaceRegistry() server.InterfaceRegistry
20-
AppManager() *appmanager.AppManager[T]
2122
QueryHandlers() map[string]appmodulev2.Handler
2223
Store() store.RootStore
2324
SchemaDecoderResolver() decoding.DecoderResolver

0 commit comments

Comments
 (0)