Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: ADR-040: Refactor App to use v2.MultiStore #10174

Closed
wants to merge 93 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
a549522
[WIP] refactor multistore => root store
roysc Aug 31, 2021
ec5d41e
baseapp - disable snapshot tests
roysc Oct 27, 2021
9db21a5
store updates, fixes
roysc Jan 20, 2022
ceabd08
updates, rf
roysc Jan 18, 2022
ab531ff
changelog + docs
roysc Jan 20, 2022
47330c5
Merge remote-tracking branch 'upstream/master' into refactor-multistore
roysc Jan 20, 2022
929f5ad
fixes
roysc Jan 20, 2022
3876113
Merge branch 'master' into roysc/adr-040-refactor-multistore
roysc Jan 20, 2022
25d0a9f
changelog fix
roysc Jan 21, 2022
355f181
cleanup
roysc Jan 21, 2022
8cbf4b2
nit, StoreConfig =>StoreParams
roysc Jan 21, 2022
4e431eb
store doc
roysc Jan 21, 2022
861b24c
store rearrange
roysc Jan 21, 2022
f45ce0f
godoc nits
roysc Jan 27, 2022
79bea1d
rm StoreConstructor
roysc Jan 28, 2022
e912111
compatibility wrapper for original MultiStore interface
roysc Jan 27, 2022
704570b
rename BasicMultiStore -> MultiStore
roysc Feb 7, 2022
b36c8f5
patch multistore
roysc Feb 7, 2022
d035fb8
patch compat store
roysc Feb 7, 2022
bfa7b88
multi store- use StoreKey instances, not strings
roysc Feb 14, 2022
ccb908d
wrap v1 MultiStore in v2 interface; use in baseapp
roysc Feb 7, 2022
532a6ad
viewstore.CacheStore() replaces CacheMultiStoreWithVersion
roysc Feb 15, 2022
5f6fe12
test cleanup
roysc Feb 15, 2022
b200867
trace context fence
roysc Feb 15, 2022
9cdd990
get all versions
roysc Feb 15, 2022
2c59d34
Merge branch 'master' of github.com:cosmos/cosmos-sdk into refactor-m…
roysc Feb 15, 2022
2627ce3
Merge remote-tracking branch 'upstream/master' into refactor-multistore
roysc Feb 15, 2022
555be73
patches
roysc Feb 18, 2022
b631a9a
Merge branch 'master' of github.com:cosmos/cosmos-sdk into refactor-m…
roysc Feb 18, 2022
83cd109
patches
roysc Feb 18, 2022
71a9bb9
fix v1asv2 commit
roysc Feb 18, 2022
78b0a12
PR revisions
roysc Feb 23, 2022
ddf8d0f
fix simd export
roysc Feb 23, 2022
3ddf675
open db at data/application/
roysc Feb 23, 2022
def4107
db cleanup
roysc Feb 23, 2022
880a4ee
badgerdb fix
roysc Feb 23, 2022
8f58686
Merge branch 'master' of github.com:cosmos/cosmos-sdk into refactor-m…
roysc Feb 25, 2022
fcb9ff9
Merge remote-tracking branch 'upstream/master' into refactor-multistore
roysc Feb 25, 2022
332c3e9
change rocksdb build tag to "rocksdb"
roysc Mar 2, 2022
d7a51f5
badgerdb - handle ErrTxnTooBig
roysc Mar 2, 2022
5102f92
remove multistore reverse index
roysc Mar 2, 2022
81642a5
rename tmdb adapter
roysc Mar 4, 2022
b33b162
GetAllVersions patch
roysc Mar 4, 2022
df2e47b
db/adapter - return discard error
roysc Mar 5, 2022
1dd7de8
multistore - minor additions
roysc Mar 10, 2022
4c34467
spelling
roysc Mar 16, 2022
44fafb2
Merge remote-tracking branch 'upstream/master' into refactor-multistore
roysc Mar 16, 2022
86929d3
badgerdb: fix versions file write
roysc Mar 25, 2022
4cdd5ee
version manager - DeleteAbove() & implement w/ slice
roysc Mar 24, 2022
e36e3ad
DBConnection.RevertTo
roysc Mar 24, 2022
f0d31ba
unit test badgerdb.versionmanager
roysc Mar 25, 2022
ae5c007
Merge remote-tracking branch 'upstream/master' into refactor-multistore
roysc Mar 25, 2022
620728f
refactor multistore: fix state sync
roysc Mar 25, 2022
9e2152e
refactor multistore: fix store migration
roysc Mar 27, 2022
68da759
fix rename store/{v2 => v2alpha1}
roysc Mar 27, 2022
bae2e7a
dbmapstore.Delete should raise smt.InvalidKeyError
roysc Mar 30, 2022
138bd43
Merge remote-tracking branch 'upstream/master' into refactor-multistore
roysc Apr 7, 2022
1295d81
dispatching db constructor
roysc Apr 7, 2022
fcf64ed
refactor server rollback cmd
roysc Apr 7, 2022
4dd37ca
go mod tidy
roysc Apr 7, 2022
7eb1f9e
fix app-db-backend config
roysc Apr 8, 2022
0499da2
comment typo
roysc Apr 12, 2022
924d2ec
reorg multistore tests
roysc Apr 12, 2022
7f747ab
check multistore key types
roysc Apr 12, 2022
7de3adf
fix v1asv2 methods
roysc Apr 12, 2022
47ab63f
tests and fixes for v1asv2 adapter
roysc Apr 12, 2022
1568198
force mem & transient stores to use bespoke key type
roysc Apr 12, 2022
bb15cd3
godoc copyedit
roysc Apr 19, 2022
e103a63
smt store test nits
roysc Apr 19, 2022
1a37cc2
[dev] update SMT library
roysc Apr 18, 2022
7b1e2f3
Merge remote-tracking branch 'upstream/main' into refactor-multistore
roysc May 30, 2022
db91469
Patch merge
roysc Jun 2, 2022
5c1b566
Patch simapp upgrade handler
roysc Jun 2, 2022
5f83976
Revert "[dev] update SMT library"
roysc May 31, 2022
2ad0ee0
Merge remote-tracking branch 'upstream/main' into refactor-multistore
roysc Jun 14, 2022
0b9aadc
put pruning manager into v2/multi store
roysc Jun 23, 2022
538bacb
fixup renames
roysc Jul 16, 2022
d7ff909
fix db creators - refactor packages/build directives
roysc Jul 4, 2022
04c832f
clean up v2 store re-exports
roysc Jun 27, 2022
1232d5a
v2 store additions
roysc Jun 28, 2022
f007704
add multi.Store.HasKVStore()
roysc Jun 24, 2022
bc67cfe
bank keeper: validate account store address
roysc Jun 29, 2022
e409ca3
fix typos, docs, rm redundant
roysc Jun 29, 2022
4823d1a
feat: list snapshots query (#246) (#252)
roysc Jul 20, 2022
69fd2f7
Restore baseapp pruning & snapshot tests
roysc Jun 22, 2022
d3f6289
pruning manager - decouple db, rm logger, fix test
roysc Jun 23, 2022
0f24e1e
Merge remote-tracking branch 'upstream/main' into refactor-multistore
roysc Jul 20, 2022
23d1a92
Merge branch 'main' into refactor-multistore
roysc Jul 21, 2022
db3c28a
fix merge
roysc Jul 20, 2022
eaa83be
go.mod/sum
roysc Jul 20, 2022
c1e800c
PR clean up
roysc Jul 21, 2022
1a9e194
v2 store: allow shared substore name prefixes
roysc Jul 21, 2022
24dd6c0
Merge remote-tracking branch 'upstream/main' into refactor-multistore
roysc Jul 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
v2 store additions
* remove iterator read lock; doesn't play with orm tests, so just rely on the stated contract: no store writes while iterator open
* use v2 store in orm tests
* clean up app boilerplate
* update NewCommitMultiStore()
* fix NewUncachedContext
  • Loading branch information
roysc committed Jul 16, 2022
commit 1232d5a1360a23ab36e83be8fedaa65501447969
19 changes: 19 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ func SetSubstores(keys ...storetypes.StoreKey) StoreOption {
}
}

func SetSubstoresFromMaps(
keys map[string]*storetypes.KVStoreKey,
tkeys map[string]*storetypes.TransientStoreKey,
memKeys map[string]*storetypes.MemoryStoreKey,
) StoreOption {
return func(params *multi.StoreParams, _ uint64) error {
if err := multi.RegisterSubstoresFromMap(params, keys); err != nil {
return err
}
if err := multi.RegisterSubstoresFromMap(params, tkeys); err != nil {
return err
}
if err := multi.RegisterSubstoresFromMap(params, memKeys); err != nil {
return err
}
return nil
}
}

// SetSnapshot sets the snapshot store.
func SetSnapshot(snapshotStore *snapshots.Store, opts snapshottypes.SnapshotOptions) AppOption {
return AppOptionOrdered{
Expand Down
3 changes: 1 addition & 2 deletions baseapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package baseapp
import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/store/v2alpha1/multi"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand Down Expand Up @@ -48,7 +47,7 @@ func (app *BaseApp) NewContext(isCheckTx bool, header tmproto.Header) sdk.Contex
}

func (app *BaseApp) NewUncachedContext(isCheckTx bool, header tmproto.Header) sdk.Context {
return sdk.NewContext(multi.CommitAsCacheStore(app.store), header, isCheckTx, app.logger)
return sdk.NewContext(app.store, header, isCheckTx, app.logger)
}

// NewContextAt creates a context using a (read-only) store at a given block height.
Expand Down
38 changes: 2 additions & 36 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/store/streaming"
storetypes "github.com/cosmos/cosmos-sdk/store/v2alpha1"
"github.com/cosmos/cosmos-sdk/store/v2alpha1/multi"
"github.com/cosmos/cosmos-sdk/testutil/testdata_pulsar"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand Down Expand Up @@ -252,40 +251,8 @@ func NewSimApp(
)
// NOTE: The testingkey is just mounted for testing purposes. Actual applications should
// not include this key.
app.memKeys = sdk.NewMemoryStoreKeys("testingkey")

// Store loader which initialized substores
setNamespaces := func(config *multi.StoreParams, ver uint64) error {
for _, key := range keys {
typ, err := storetypes.StoreKeyToType(key)
if err != nil {
return err
}
if err = config.RegisterSubstore(key, typ); err != nil {
return err
}
}
for _, key := range memKeys {
typ, err := storetypes.StoreKeyToType(key)
if err != nil {
return err
}
if err = config.RegisterSubstore(key, typ); err != nil {
return err
}
}
for _, key := range tkeys {
typ, err := storetypes.StoreKeyToType(key)
if err != nil {
return err
}
if err = config.RegisterSubstore(key, typ); err != nil {
return err
}
}
return nil
}
baseAppOptions = append(baseAppOptions, baseapp.StoreOption(setNamespaces))
app.memKeys = sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey, "testingkey")
baseAppOptions = append(baseAppOptions, baseapp.SetSubstoresFromMaps(keys, tkeys, memKeys))

// set the governance module account as the authority for conducting upgrades
upgradeKeeper := upgradekeeper.NewKeeper(skipUpgradeHeights, app.keys[upgradetypes.StoreKey], app.appCodec, homePath, nil, authtypes.NewModuleAddress(govtypes.ModuleName).String())
Expand Down Expand Up @@ -427,7 +394,6 @@ func (app *SimApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.

// LoadHeight loads a particular height
func (app *SimApp) LoadHeight(height int64) error {
// return app.LoadVersion(height)
return errors.New("cannot load arbitrary height")
}

Expand Down
15 changes: 9 additions & 6 deletions store/store.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package store

import (
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
dbm "github.com/cosmos/cosmos-sdk/db"

"github.com/cosmos/cosmos-sdk/store/cache"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
"github.com/cosmos/cosmos-sdk/store/types"
types "github.com/cosmos/cosmos-sdk/store/v2alpha1"
"github.com/cosmos/cosmos-sdk/store/v2alpha1/multi"
)

func NewCommitMultiStore(db dbm.DB) types.CommitMultiStore {
return rootmulti.NewStore(db, log.NewNopLogger())
func NewCommitMultiStore(db dbm.DBConnection) types.CommitMultiStore {
store, err := multi.NewV1MultiStoreAsV2(db, multi.DefaultStoreParams())
if err != nil {
panic(err)
}
return store
}

func NewCommitKVStoreCacheManager() types.MultiStorePersistentCache {
Expand Down
63 changes: 63 additions & 0 deletions store/v2alpha1/multi/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package multi

import (
"fmt"

pruningtypes "github.com/cosmos/cosmos-sdk/pruning/types"
types "github.com/cosmos/cosmos-sdk/store/v2alpha1"
)

// DefaultStoreParams returns a MultiStore config with an empty schema, a single backing DB,
// pruning with PruneDefault, no listeners and no tracer.
func DefaultStoreParams() StoreParams {
return StoreParams{
Pruning: pruningtypes.NewPruningOptions(pruningtypes.PruningDefault),
SchemaBuilder: newSchemaBuilder(),
storeKeys: storeKeys{},
traceListenMixin: newTraceListenMixin(),
}
}

func (par *StoreParams) RegisterSubstore(skey types.StoreKey, typ types.StoreType) error {
if !validSubStoreType(typ) {
return fmt.Errorf("StoreType not supported: %v", typ)
}
var ok bool
switch typ {
case types.StoreTypePersistent:
_, ok = skey.(*types.KVStoreKey)
case types.StoreTypeMemory:
_, ok = skey.(*types.MemoryStoreKey)
case types.StoreTypeTransient:
_, ok = skey.(*types.TransientStoreKey)
}
if !ok {
return fmt.Errorf("invalid StoreKey for %v: %T", typ, skey)
}
if err := par.registerName(skey.Name(), typ); err != nil {
return err
}
par.storeKeys[skey.Name()] = skey
return nil
}

func (par *StoreParams) storeKey(key string) (types.StoreKey, error) {
skey, ok := par.storeKeys[key]
if !ok {
return nil, fmt.Errorf("StoreKey instance not mapped: %s", key)
}
return skey, nil
}

func RegisterSubstoresFromMap[T types.StoreKey](par *StoreParams, keys map[string]T) error {
for _, key := range keys {
typ, err := types.StoreKeyToType(key)
if err != nil {
return err
}
if err = par.RegisterSubstore(key, typ); err != nil {
return err
}
}
Comment on lines +53 to +61

Check warning

Code scanning / CodeQL

Iteration over map

Iteration over map may be a possible source of non-determinism
return nil
}
71 changes: 10 additions & 61 deletions store/v2alpha1/multi/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,48 +141,6 @@ func newSchemaBuilder() SchemaBuilder {
return SchemaBuilder{StoreSchema: StoreSchema{}}
}

// DefaultStoreParams returns a MultiStore config with an empty schema, a single backing DB,
// pruning with PruneDefault, no listeners and no tracer.
func DefaultStoreParams() StoreParams {
return StoreParams{
Pruning: pruningtypes.NewPruningOptions(pruningtypes.PruningDefault),
SchemaBuilder: newSchemaBuilder(),
storeKeys: storeKeys{},
traceListenMixin: newTraceListenMixin(),
}
}

func (par *StoreParams) RegisterSubstore(skey types.StoreKey, typ types.StoreType) error {
if !validSubStoreType(typ) {
return fmt.Errorf("StoreType not supported: %v", typ)
}
var ok bool
switch typ {
case types.StoreTypePersistent:
_, ok = skey.(*types.KVStoreKey)
case types.StoreTypeMemory:
_, ok = skey.(*types.MemoryStoreKey)
case types.StoreTypeTransient:
_, ok = skey.(*types.TransientStoreKey)
}
if !ok {
return fmt.Errorf("invalid StoreKey for %v: %T", typ, skey)
}
if err := par.registerName(skey.Name(), typ); err != nil {
return err
}
par.storeKeys[skey.Name()] = skey
return nil
}

func (par *StoreParams) storeKey(key string) (types.StoreKey, error) {
skey, ok := par.storeKeys[key]
if !ok {
return nil, fmt.Errorf("StoreKey instance not mapped: %s", key)
}
return skey, nil
}

// Returns true for valid store types for a MultiStore schema
func validSubStoreType(sst types.StoreType) bool {
switch sst {
Expand Down Expand Up @@ -279,8 +237,7 @@ func NewStore(db dbm.DBConnection, opts StoreParams) (ret *Store, err error) {
// To abide by atomicity constraints, revert the DB to the last saved version, in case it contains
// committed data in the "working" version.
// This should only happen if Store.Commit previously failed.
err = db.Revert()
if err != nil {
if err = db.Revert(); err != nil {
return
}
stateTxn := db.ReadWriter()
Expand All @@ -292,17 +249,15 @@ func NewStore(db dbm.DBConnection, opts StoreParams) (ret *Store, err error) {
stateCommitmentTxn := stateTxn
if opts.StateCommitmentDB != nil {
var scVersions dbm.VersionSet
scVersions, err = opts.StateCommitmentDB.Versions()
if err != nil {
if scVersions, err = opts.StateCommitmentDB.Versions(); err != nil {
return
}
// Version sets of each DB must match
if !versions.Equal(scVersions) {
err = fmt.Errorf("different version history between Storage and StateCommitment DB ")
return
}
err = opts.StateCommitmentDB.Revert()
if err != nil {
if err = opts.StateCommitmentDB.Revert(); err != nil {
return
}
stateCommitmentTxn = opts.StateCommitmentDB.ReadWriter()
Expand Down Expand Up @@ -333,8 +288,7 @@ func NewStore(db dbm.DBConnection, opts StoreParams) (ret *Store, err error) {
writeSchema := func(sch StoreSchema) {
schemaWriter := prefixdb.NewWriter(ret.stateTxn, schemaPrefix)
var it dbm.Iterator
it, err = schemaView.Iterator(nil, nil)
if err != nil {
if it, err = schemaView.Iterator(nil, nil); err != nil {
return
}
for it.Next() {
Expand All @@ -343,12 +297,10 @@ func NewStore(db dbm.DBConnection, opts StoreParams) (ret *Store, err error) {
return
}
}
err = it.Close()
if err != nil {
if err = it.Close(); err != nil {
return
}
err = schemaView.Discard()
if err != nil {
if err = schemaView.Discard(); err != nil {
return
}
// NB. the migrated contents and schema are not committed until the next store.Commit
Expand Down Expand Up @@ -642,9 +594,8 @@ func (s *Store) commit(target uint64) (id *types.CommitID, err error) {
}
// Update substore Merkle roots
for key, storeHash := range storeHashes {
pfx := substorePrefix(key)
stateW := prefixdb.NewReadWriter(s.stateTxn, pfx)
if err = stateW.Set(substoreMerkleRootKey, storeHash); err != nil {
w := prefixdb.NewPrefixReadWriter(s.stateTxn, substorePrefix(key))
if err = w.Set(substoreMerkleRootKey, storeHash); err != nil {
return
}
}
Expand All @@ -660,8 +611,7 @@ func (s *Store) commit(target uint64) (id *types.CommitID, err error) {
err = util.CombineErrors(err, s.stateDB.Revert(), "stateDB.Revert also failed")
}
}()
err = s.stateDB.SaveVersion(target)
if err != nil {
if err = s.stateDB.SaveVersion(target); err != nil {
return
}

Expand Down Expand Up @@ -691,8 +641,7 @@ func (s *Store) commit(target uint64) (id *types.CommitID, err error) {
}
}()

err = s.StateCommitmentDB.SaveVersion(target)
if err != nil {
if err = s.StateCommitmentDB.SaveVersion(target); err != nil {
return
}
}
Expand Down
22 changes: 2 additions & 20 deletions store/v2alpha1/multi/sub_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package multi

import (
"io"
"sync"

dbm "github.com/cosmos/cosmos-sdk/db"
dbutil "github.com/cosmos/cosmos-sdk/internal/db"
"github.com/cosmos/cosmos-sdk/store/cachekv"
"github.com/cosmos/cosmos-sdk/store/listenkv"
Expand Down Expand Up @@ -57,29 +55,13 @@ func (s *substore) Delete(key []byte) {
_ = s.dataBucket.Delete(key)
}

type contentsIterator struct {
types.Iterator
locker sync.Locker
}

func (s *substore) newSubstoreIterator(source dbm.Iterator) *contentsIterator {
locker := s.root.mtx.RLocker()
locker.Lock()
return &contentsIterator{dbutil.ToStoreIterator(source), locker}
}

func (it *contentsIterator) Close() error {
defer it.locker.Unlock()
return it.Iterator.Close()
}

// Iterator implements KVStore.
func (s *substore) Iterator(start, end []byte) types.Iterator {
iter, err := s.dataBucket.Iterator(start, end)
if err != nil {
panic(err)
}
return s.newSubstoreIterator(iter)
return dbutil.DBToStoreIterator(iter)
}

// ReverseIterator implements KVStore.
Expand All @@ -88,7 +70,7 @@ func (s *substore) ReverseIterator(start, end []byte) types.Iterator {
if err != nil {
panic(err)
}
return s.newSubstoreIterator(iter)
return dbutil.DBToStoreIterator(iter)
}

// GetStoreType implements Store.
Expand Down
Loading