Skip to content

Commit

Permalink
fix(server/v2/cometbft): wire app closer (#22240)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Oct 14, 2024
1 parent 1f941bb commit 0b43fcc
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 13 deletions.
3 changes: 3 additions & 0 deletions server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Consensus[T transaction.Tx] struct {
logger log.Logger
appName, version string
app *appmanager.AppManager[T]
appCloser func() error
txCodec transaction.Codec[T]
store types.Store
streaming streaming.Manager
Expand Down Expand Up @@ -77,6 +78,7 @@ func NewConsensus[T transaction.Tx](
logger log.Logger,
appName string,
app *appmanager.AppManager[T],
appCloser func() error,
mp mempool.Mempool[T],
indexedEvents map[string]struct{},
queryHandlersMap map[string]appmodulev2.Handler,
Expand All @@ -89,6 +91,7 @@ func NewConsensus[T transaction.Tx](
appName: appName,
version: getCometBFTServerVersion(),
app: app,
appCloser: appCloser,
cfg: cfg,
store: store,
logger: logger,
Expand Down
6 changes: 3 additions & 3 deletions server/v2/cometbft/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
am "cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/server/v2/cometbft/handlers"
cometmock "cosmossdk.io/server/v2/cometbft/internal/mock"
"cosmossdk.io/server/v2/cometbft/mempool"
Expand Down 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)

b := am.Builder[mock.Tx]{
b := appmanager.Builder[mock.Tx]{
STF: s,
DB: mockStore,
ValidateTxGasLimit: gasLimit,
Expand All @@ -688,7 +688,7 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
am, err := b.Build()
require.NoError(t, err)

return NewConsensus[mock.Tx](log.NewNopLogger(), "testing-app", am, mempool, map[string]struct{}{}, nil, mockStore, Config{AppTomlConfig: DefaultAppTomlConfig()}, mock.TxCodec{}, "test")
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")
}

// Check target version same with store's latest version
Expand Down
1 change: 1 addition & 0 deletions server/v2/cometbft/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logg
s.logger,
appI.Name(),
appI.GetAppManager(),
appI.Close,
s.serverOptions.Mempool(cfg),
indexEvents,
appI.GetQueryHandlers(),
Expand Down
2 changes: 1 addition & 1 deletion server/v2/testdata/app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ minimum-gas-prices = '0stake'
app-db-backend = 'goleveldb'

[store.options]
# SState storage database type. Currently we support: "sqlite", "pebble" and "rocksdb"
# State storage database type. Currently we support: "sqlite", "pebble" and "rocksdb"
ss-type = 'sqlite'
# State commitment database type. Currently we support: "iavl" and "iavl-v2"
sc-type = 'iavl'
Expand Down
1 change: 1 addition & 0 deletions server/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ type AppI[T transaction.Tx] interface {
GetQueryHandlers() map[string]appmodulev2.Handler
GetStore() store.RootStore
GetSchemaDecoderResolver() decoding.DecoderResolver
Close() error
}
10 changes: 10 additions & 0 deletions simapp/v2/app_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ func (app *SimApp[T]) TxConfig() client.TxConfig {
return app.txConfig
}

// GetStore returns the root store.
func (app *SimApp[T]) GetStore() store.RootStore {
return app.store
}

// Close overwrites the base Close method to close the stores.
func (app *SimApp[T]) Close() error {
if err := app.store.Close(); err != nil {
return err
}

return app.App.Close()
}
1 change: 1 addition & 0 deletions store/v2/commitment/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
)

// MetadataStore is a store for metadata related to the commitment store.
// It isn't metadata store role to close the underlying KVStore.
type MetadataStore struct {
kv corestore.KVStoreWithBatch
}
Expand Down
4 changes: 2 additions & 2 deletions store/v2/root/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const (

// Options are the options for creating a root store.
type Options struct {
SSType SSType `mapstructure:"ss-type" toml:"ss-type" comment:"SState storage database type. Currently we support: \"sqlite\", \"pebble\" and \"rocksdb\""`
SSType SSType `mapstructure:"ss-type" toml:"ss-type" comment:"State storage database type. Currently we support: \"sqlite\", \"pebble\" and \"rocksdb\""`
SCType SCType `mapstructure:"sc-type" toml:"sc-type" comment:"State commitment database type. Currently we support: \"iavl\" and \"iavl-v2\""`
SSPruningOption *store.PruningOption `mapstructure:"ss-pruning-option" toml:"ss-pruning-option" comment:"Pruning options for state storage"`
SCPruningOption *store.PruningOption `mapstructure:"sc-pruning-option" toml:"sc-pruning-option" comment:"Pruning options for state commitment"`
Expand Down Expand Up @@ -177,5 +177,5 @@ func CreateRootStore(opts *FactoryOptions) (store.RootStore, error) {
}

pm := pruning.NewManager(sc, ss, storeOpts.SCPruningOption, storeOpts.SSPruningOption)
return New(opts.Logger, ss, sc, pm, nil, nil)
return New(opts.SCRawDB, opts.Logger, ss, sc, pm, nil, nil)
}
2 changes: 1 addition & 1 deletion store/v2/root/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (s *MigrateStoreTestSuite) SetupTest() {
pm := pruning.NewManager(sc, ss, nil, nil)

// assume no storage store, simulate the migration process
s.rootStore, err = New(testLog, ss, orgSC, pm, migrationManager, nil)
s.rootStore, err = New(dbm.NewMemDB(), testLog, ss, orgSC, pm, migrationManager, nil)
s.Require().NoError(err)
}

Expand Down
7 changes: 7 additions & 0 deletions store/v2/root/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"errors"
"fmt"
"io"
"sync"
"time"

Expand Down Expand Up @@ -33,6 +34,9 @@ type Store struct {
logger corelog.Logger
initialVersion uint64

// holds the db instance for closing it
dbCloser io.Closer

// stateStorage reflects the state storage backend
stateStorage store.VersionedDatabase

Expand Down Expand Up @@ -68,6 +72,7 @@ type Store struct {
//
// NOTE: The migration manager is optional and can be nil if no migration is required.
func New(
dbCloser io.Closer,
logger corelog.Logger,
ss store.VersionedDatabase,
sc store.Committer,
Expand All @@ -76,6 +81,7 @@ func New(
m metrics.StoreMetrics,
) (store.RootStore, error) {
return &Store{
dbCloser: dbCloser,
logger: logger,
initialVersion: 1,
stateStorage: ss,
Expand All @@ -92,6 +98,7 @@ func New(
func (s *Store) Close() (err error) {
err = errors.Join(err, s.stateStorage.Close())
err = errors.Join(err, s.stateCommitment.Close())
err = errors.Join(err, s.dbCloser.Close())

s.stateStorage = nil
s.stateCommitment = nil
Expand Down
6 changes: 3 additions & 3 deletions store/v2/root/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *RootStoreTestSuite) SetupTest() {
s.Require().NoError(err)

pm := pruning.NewManager(sc, ss, nil, nil)
rs, err := New(noopLog, ss, sc, pm, nil, nil)
rs, err := New(dbm.NewMemDB(), noopLog, ss, sc, pm, nil, nil)
s.Require().NoError(err)

s.rootStore = rs
Expand All @@ -84,7 +84,7 @@ func (s *RootStoreTestSuite) newStoreWithPruneConfig(config *store.PruningOption

pm := pruning.NewManager(sc, ss, config, config)

rs, err := New(noopLog, ss, sc, pm, nil, nil)
rs, err := New(dbm.NewMemDB(), noopLog, ss, sc, pm, nil, nil)
s.Require().NoError(err)

s.rootStore = rs
Expand All @@ -93,7 +93,7 @@ func (s *RootStoreTestSuite) newStoreWithPruneConfig(config *store.PruningOption
func (s *RootStoreTestSuite) newStoreWithBackendMount(ss store.VersionedDatabase, sc store.Committer, pm *pruning.Manager) {
noopLog := coretesting.NewNopLogger()

rs, err := New(noopLog, ss, sc, pm, nil, nil)
rs, err := New(dbm.NewMemDB(), noopLog, ss, sc, pm, nil, nil)
s.Require().NoError(err)

s.rootStore = rs
Expand Down
4 changes: 2 additions & 2 deletions store/v2/root/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *UpgradeStoreTestSuite) SetupTest() {
sc, err := commitment.NewCommitStore(multiTrees, nil, s.commitDB, testLog)
s.Require().NoError(err)
pm := pruning.NewManager(sc, ss, nil, nil)
s.rootStore, err = New(testLog, ss, sc, pm, nil, nil)
s.rootStore, err = New(s.commitDB, testLog, ss, sc, pm, nil, nil)
s.Require().NoError(err)

// commit changeset
Expand Down Expand Up @@ -92,7 +92,7 @@ func (s *UpgradeStoreTestSuite) loadWithUpgrades(upgrades *corestore.StoreUpgrad
sc, err := commitment.NewCommitStore(multiTrees, oldTrees, s.commitDB, testLog)
s.Require().NoError(err)
pm := pruning.NewManager(sc, s.rootStore.GetStateStorage().(store.Pruner), nil, nil)
s.rootStore, err = New(testLog, s.rootStore.GetStateStorage(), sc, pm, nil, nil)
s.rootStore, err = New(s.commitDB, testLog, s.rootStore.GetStateStorage(), sc, pm, nil, nil)
s.Require().NoError(err)
}

Expand Down
2 changes: 1 addition & 1 deletion tools/confix/data/v2-app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ minimum-gas-prices = '0stake'
app-db-backend = 'goleveldb'

[store.options]
# SState storage database type. Currently we support: "sqlite", "pebble" and "rocksdb"
# State storage database type. Currently we support: "sqlite", "pebble" and "rocksdb"
ss-type = 'sqlite'
# State commitment database type. Currently we support: "iavl" and "iavl-v2"
sc-type = 'iavl'
Expand Down

0 comments on commit 0b43fcc

Please sign in to comment.