Skip to content

Commit

Permalink
refactor(store/v2)!: simplify storage (#22683)
Browse files Browse the repository at this point in the history
(cherry picked from commit 94cfcc1)

# Conflicts:
#	runtime/v2/builder.go
#	server/v2/stf/branch/bench_test.go
#	server/v2/store/snapshot.go
#	store/iavl/store_test.go
#	store/v2/commitment/iavl/tree.go
#	store/v2/commitment/store.go
#	store/v2/commitment/store_test_suite.go
#	store/v2/database.go
#	store/v2/migration/README.md
#	store/v2/migration/manager.go
#	store/v2/migration/manager_test.go
#	store/v2/mock/db_mock.go
#	store/v2/mock/types.go
#	store/v2/pruning/manager.go
#	store/v2/pruning/manager_test.go
#	store/v2/root/factory.go
#	store/v2/root/migrate_test.go
#	store/v2/root/store.go
#	store/v2/root/store_mock_test.go
#	store/v2/root/store_test.go
#	store/v2/root/upgrade_test.go
#	store/v2/snapshots/helpers_test.go
#	store/v2/snapshots/manager.go
#	store/v2/snapshots/manager_test.go
#	store/v2/snapshots/snapshotter.go
#	store/v2/store.go
#	tests/integration/v2/auth/app_test.go
  • Loading branch information
tac0turtle authored and mergify[bot] committed Dec 3, 2024
1 parent a9f8400 commit d4d1562
Show file tree
Hide file tree
Showing 33 changed files with 7,364 additions and 30 deletions.
219 changes: 219 additions & 0 deletions runtime/v2/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package runtime

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

"cosmossdk.io/core/appmodule"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
"cosmossdk.io/runtime/v2/services"
"cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/server/v2/stf"
"cosmossdk.io/server/v2/stf/branch"
"cosmossdk.io/store/v2/root"
)

// AppBuilder is a type that is injected into a container by the runtime/v2 module
// (as *AppBuilder) which can be used to create an app which is compatible with
// the existing app.go initialization conventions.
type AppBuilder[T transaction.Tx] struct {
app *App[T]
storeBuilder root.Builder
storeConfig *root.Config

// the following fields are used to overwrite the default
branch func(state store.ReaderMap) store.WriterMap
txValidator func(ctx context.Context, tx T) error
postTxExec func(ctx context.Context, tx T, success bool) error
}

// RegisterModules registers the provided modules with the module manager.
// This is the primary hook for integrating with modules which are not registered using the app config.
func (a *AppBuilder[T]) RegisterModules(modules map[string]appmodulev2.AppModule) error {
for name, appModule := range modules {
// if a (legacy) module implements the HasName interface, check that the name matches
if mod, ok := appModule.(interface{ Name() string }); ok {
if name != mod.Name() {
a.app.logger.Warn(fmt.Sprintf("module name %q does not match name returned by HasName: %q", name, mod.Name()))
}
}

if _, ok := a.app.moduleManager.modules[name]; ok {
return fmt.Errorf("module named %q already exists", name)
}
a.app.moduleManager.modules[name] = appModule

if mod, ok := appModule.(appmodulev2.HasRegisterInterfaces); ok {
mod.RegisterInterfaces(a.app.interfaceRegistrar)
}

if mod, ok := appModule.(appmodule.HasAminoCodec); ok {
mod.RegisterLegacyAminoCodec(a.app.amino)
}
}

return nil
}

// Build builds an *App instance.
func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) {
for _, opt := range opts {
opt(a)
}

// default branch
if a.branch == nil {
a.branch = branch.DefaultNewWriterMap
}

// default tx validator
if a.txValidator == nil {
a.txValidator = a.app.moduleManager.TxValidators()
}

// default post tx exec
if a.postTxExec == nil {
a.postTxExec = func(ctx context.Context, tx T, success bool) error {
return nil
}
}

var err error
a.app.db, err = a.storeBuilder.Build(a.app.logger, a.storeConfig)
if err != nil {
return nil, err
}

if err = a.app.moduleManager.RegisterServices(a.app); err != nil {
return nil, err
}

endBlocker, valUpdate := a.app.moduleManager.EndBlock()

stf, err := stf.New[T](
a.app.logger.With("module", "stf"),
a.app.msgRouterBuilder,
a.app.queryRouterBuilder,
a.app.moduleManager.PreBlocker(),
a.app.moduleManager.BeginBlock(),
endBlocker,
a.txValidator,
valUpdate,
a.postTxExec,
a.branch,
)
if err != nil {
return nil, fmt.Errorf("failed to create STF: %w", err)
}
a.app.stf = stf

a.app.AppManager = appmanager.New[T](
appmanager.Config{
ValidateTxGasLimit: a.app.config.GasConfig.ValidateTxGasLimit,
QueryGasLimit: a.app.config.GasConfig.QueryGasLimit,
SimulationGasLimit: a.app.config.GasConfig.SimulationGasLimit,
},
a.app.db,
a.app.stf,
a.initGenesis,
a.exportGenesis,
)

return a.app, nil
}

// initGenesis returns the app initialization genesis for modules
func (a *AppBuilder[T]) initGenesis(ctx context.Context, src io.Reader, txHandler func(json.RawMessage) error) (store.WriterMap, error) {
// this implementation assumes that the state is a JSON object
bz, err := io.ReadAll(src)
if err != nil {
return nil, fmt.Errorf("failed to read import state: %w", err)
}

var genesisJSON map[string]json.RawMessage
if err = json.Unmarshal(bz, &genesisJSON); err != nil {
return nil, err
}

v, zeroState, err := a.app.db.StateLatest()
if err != nil {
return nil, fmt.Errorf("unable to get latest state: %w", err)
}
if v != 0 { // TODO: genesis state may be > 0, we need to set version on store
return nil, errors.New("cannot init genesis on non-zero state")
}
genesisCtx := services.NewGenesisContext(a.branch(zeroState))
genesisState, err := genesisCtx.Mutate(ctx, func(ctx context.Context) error {
err = a.app.moduleManager.InitGenesisJSON(ctx, genesisJSON, txHandler)
if err != nil {
return fmt.Errorf("failed to init genesis: %w", err)
}
return nil
})

return genesisState, err
}

// exportGenesis returns the app export genesis logic for modules
func (a *AppBuilder[T]) exportGenesis(ctx context.Context, version uint64) ([]byte, error) {
state, err := a.app.db.StateAt(version)
if err != nil {
return nil, fmt.Errorf("unable to get state at given version: %w", err)
}

genesisJson, err := a.app.moduleManager.ExportGenesisForModules(
ctx,
func() store.WriterMap {
return a.branch(state)
},
)
if err != nil {
return nil, fmt.Errorf("failed to export genesis: %w", err)
}

bz, err := json.Marshal(genesisJson)
if err != nil {
return nil, fmt.Errorf("failed to marshal genesis: %w", err)
}

return bz, nil
}

// AppBuilderOption is a function that can be passed to AppBuilder.Build to customize the resulting app.
type AppBuilderOption[T transaction.Tx] func(*AppBuilder[T])

// AppBuilderWithBranch sets a custom branch implementation for the app.
func AppBuilderWithBranch[T transaction.Tx](branch func(state store.ReaderMap) store.WriterMap) AppBuilderOption[T] {
return func(a *AppBuilder[T]) {
a.branch = branch
}
}

// AppBuilderWithTxValidator sets the tx validator for the app.
// It overrides all default tx validators defined by modules.
func AppBuilderWithTxValidator[T transaction.Tx](
txValidators func(
ctx context.Context, tx T,
) error,
) AppBuilderOption[T] {
return func(a *AppBuilder[T]) {
a.txValidator = txValidators
}
}

// AppBuilderWithPostTxExec sets logic that will be executed after each transaction.
// When not provided, a no-op function will be used.
func AppBuilderWithPostTxExec[T transaction.Tx](
postTxExec func(
ctx context.Context, tx T, success bool,
) error,
) AppBuilderOption[T] {
return func(a *AppBuilder[T]) {
a.postTxExec = postTxExec
}
}
6 changes: 3 additions & 3 deletions server/v2/cometbft/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ func TestConsensus_Query(t *testing.T) {
c := setUpConsensus(t, 100_000, cometmock.MockMempool[mock.Tx]{})

// Write data to state storage
err := c.store.GetStateStorage().ApplyChangeset(&store.Changeset{
err := c.store.GetStateCommitment().WriteChangeset(&store.Changeset{
Version: 1,
Changes: []store.StateChanges{
{
Expand Down Expand Up @@ -691,9 +691,8 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
)
require.NoError(t, err)

ss := cometmock.NewMockStorage(log.NewNopLogger(), t.TempDir())
sc := cometmock.NewMockCommiter(log.NewNopLogger(), string(actorName), "stf")
mockStore := cometmock.NewMockStore(ss, sc)
mockStore := cometmock.NewMockStore(sc)

am := appmanager.New(appmanager.Config{
ValidateTxGasLimit: gasLimit,
Expand Down Expand Up @@ -786,6 +785,7 @@ func TestOptimisticExecution(t *testing.T) {
Txs: ppReq.Txs,
}
fbResp, err := c.FinalizeBlock(context.Background(), fbReq)
require.Nil(t, fbResp)
require.Error(t, err)
require.ErrorContains(t, err, "test error") // from optimisticMockFunc
require.Equal(t, 1, calledTimes)
Expand Down
8 changes: 4 additions & 4 deletions server/v2/cometbft/internal/mock/mock_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewMockReader(v uint64, rs *MockStore, actor []byte) *MockReader {
}

func (roa *MockReader) Has(key []byte) (bool, error) {
val, err := roa.store.GetStateStorage().Has(roa.actor, roa.version, key)
val, err := roa.store.GetStateCommitment().Has(roa.actor, roa.version, key)
if err != nil {
return false, err
}
Expand All @@ -48,7 +48,7 @@ func (roa *MockReader) Has(key []byte) (bool, error) {
}

func (roa *MockReader) Get(key []byte) ([]byte, error) {
result, err := roa.store.GetStateStorage().Get(roa.actor, roa.version, key)
result, err := roa.store.GetStateCommitment().Get(roa.actor, roa.version, key)
if err != nil {
return nil, err
}
Expand All @@ -57,9 +57,9 @@ func (roa *MockReader) Get(key []byte) ([]byte, error) {
}

func (roa *MockReader) Iterator(start, end []byte) (corestore.Iterator, error) {
return roa.store.GetStateStorage().Iterator(roa.actor, roa.version, start, end)
return roa.store.GetStateCommitment().Iterator(roa.actor, roa.version, start, end)
}

func (roa *MockReader) ReverseIterator(start, end []byte) (corestore.Iterator, error) {
return roa.store.GetStateStorage().ReverseIterator(roa.actor, roa.version, start, end)
return roa.store.GetStateCommitment().ReverseIterator(roa.actor, roa.version, start, end)
}
24 changes: 3 additions & 21 deletions server/v2/cometbft/internal/mock/mock_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,12 @@ import (
"cosmossdk.io/store/v2/commitment/iavl"
dbm "cosmossdk.io/store/v2/db"
"cosmossdk.io/store/v2/proof"
"cosmossdk.io/store/v2/storage"
"cosmossdk.io/store/v2/storage/pebbledb"
)

type MockStore struct {
Storage storev2.VersionedWriter
Committer storev2.Committer
}

func NewMockStorage(logger log.Logger, dir string) storev2.VersionedWriter {
storageDB, _ := pebbledb.New(dir)
ss := storage.NewStorageStore(storageDB, logger)
return ss
}

func NewMockCommiter(logger log.Logger, actors ...string) storev2.Committer {
treeMap := make(map[string]commitment.Tree)
for _, actor := range actors {
Expand All @@ -36,8 +27,8 @@ func NewMockCommiter(logger log.Logger, actors ...string) storev2.Committer {
return sc
}

func NewMockStore(ss storev2.VersionedWriter, sc storev2.Committer) *MockStore {
return &MockStore{Storage: ss, Committer: sc}
func NewMockStore(sc storev2.Committer) *MockStore {
return &MockStore{Committer: sc}
}

func (s *MockStore) GetLatestVersion() (uint64, error) {
Expand All @@ -59,12 +50,7 @@ func (s *MockStore) StateLatest() (uint64, corestore.ReaderMap, error) {
}

func (s *MockStore) Commit(changeset *corestore.Changeset) (corestore.Hash, error) {
err := s.Storage.ApplyChangeset(changeset)
if err != nil {
return []byte{}, err
}

err = s.Committer.WriteChangeset(changeset)
err := s.Committer.WriteChangeset(changeset)
if err != nil {
return []byte{}, err
}
Expand All @@ -81,10 +67,6 @@ func (s *MockStore) StateAt(version uint64) (corestore.ReaderMap, error) {
return NewMockReaderMap(version, s), nil
}

func (s *MockStore) GetStateStorage() storev2.VersionedWriter {
return s.Storage
}

func (s *MockStore) GetStateCommitment() storev2.Committer {
return s.Committer
}
Expand Down
2 changes: 0 additions & 2 deletions server/v2/cometbft/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ func New[T transaction.Tx](
indexEvents[e] = struct{}{}
}

ss := store.GetStateStorage().(snapshots.StorageSnapshotter)
sc := store.GetStateCommitment().(snapshots.CommitSnapshotter)

snapshotStore, err := GetSnapshotStore(srv.config.ConfigTomlConfig.RootDir)
Expand Down Expand Up @@ -155,7 +154,6 @@ func New[T transaction.Tx](
snapshotStore,
srv.serverOptions.SnapshotOptions(cfg),
sc,
ss,
nil, // extensions snapshotter registered below
logger,
)
Expand Down
Loading

0 comments on commit d4d1562

Please sign in to comment.