Skip to content

Commit

Permalink
fix versiondb build
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Apr 8, 2024
1 parent f18bac6 commit e4f16ca
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 144 deletions.
33 changes: 19 additions & 14 deletions app/versiondb.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"

storetypes "cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/crypto-org-chain/cronos/versiondb"
"github.com/crypto-org-chain/cronos/versiondb/tsrocksdb"
)
Expand All @@ -19,30 +18,36 @@ func (app *App) setupVersionDB(
tkeys map[string]*storetypes.TransientStoreKey,
memKeys map[string]*storetypes.MemoryStoreKey,
okeys map[string]*storetypes.ObjectStoreKey,
) (sdk.RootMultiStore, error) {
) (storetypes.RootMultiStore, error) {
dataDir := filepath.Join(homePath, "data", "versiondb")
if err := os.MkdirAll(dataDir, os.ModePerm); err != nil {
return nil, err
}

versionDB, err := tsrocksdb.NewStore(dataDir)
if err != nil {
return nil, err
}

// default to exposing all
exposeStoreKeys := make([]storetypes.StoreKey, 0, len(keys))
for _, storeKey := range keys {
exposeStoreKeys = append(exposeStoreKeys, storeKey)
}

service := versiondb.NewStreamingService(versionDB, exposeStoreKeys)
app.SetStreamingService(service)
// register in app streaming manager
sm := app.StreamingManager()
sm.ABCIListeners = append(sm.ABCIListeners,
versiondb.NewStreamingService(versionDB),
)
app.SetStreamingManager(sm)

verDB := versiondb.NewMultiStore(app.CommitMultiStore(), versionDB, exposeStoreKeys)
verDB.MountTransientStores(tkeys)
verDB.MountMemoryStores(memKeys)
verDB.MountObjectStores(memKeys)
var delegatedStoreKeys = make(map[storetypes.StoreKey]struct{})
for _, k := range tkeys {
delegatedStoreKeys[k] = struct{}{}
}
for _, k := range memKeys {
delegatedStoreKeys[k] = struct{}{}
}
for _, k := range okeys {
delegatedStoreKeys[k] = struct{}{}
}

verDB := versiondb.NewMultiStore(app.CommitMultiStore(), versionDB, keys, delegatedStoreKeys)
app.SetQueryMultiStore(verDB)
return verDB, nil
}
2 changes: 1 addition & 1 deletion cmd/cronosd/cmd/versiondb.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func ChangeSetCmd() *cobra.Command {
keys, _, _ := app.StoreKeys(true)
keys, _, _, _ := app.StoreKeys()
storeNames := make([]string, 0, len(keys))
for name := range keys {
storeNames = append(storeNames, name)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ replace (
// https://github.com/cosmos/cosmos-db/pull/104
github.com/cosmos/cosmos-db => github.com/yihuang/cosmos-db v0.0.0-20240315085537-afdfa9c0f19d
// release/v0.50.x
github.com/cosmos/cosmos-sdk => github.com/crypto-org-chain/cosmos-sdk v0.46.0-beta2.0.20240408014408-56c165536d0a
github.com/cosmos/cosmos-sdk => github.com/yihuang/cosmos-sdk v0.43.0-beta1.0.20240408071442-3b12176ada7b
github.com/crypto-org-chain/cronos/memiavl => ./memiavl
github.com/crypto-org-chain/cronos/store => ./store
github.com/crypto-org-chain/cronos/versiondb => ./versiondb
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,6 @@ github.com/crypto-org-chain/btree v0.0.0-20240406140148-2687063b042c h1:MOgfS4+F
github.com/crypto-org-chain/btree v0.0.0-20240406140148-2687063b042c/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
github.com/crypto-org-chain/cometbft-db v0.0.0-20231011055109-57922ac52a63 h1:R1QJ9a3XdYMSKo+1RdFifxb/g3lNypC52L/rpYrWoKo=
github.com/crypto-org-chain/cometbft-db v0.0.0-20231011055109-57922ac52a63/go.mod h1:rocwIfnS+kA060x64gkSIRvWB9StSppIkJuo5MWzL24=
github.com/crypto-org-chain/cosmos-sdk v0.46.0-beta2.0.20240408014408-56c165536d0a h1:3IiFpiRRiF0lFCJcKGEu6UafcqzRUwUUl75aXan9pJI=
github.com/crypto-org-chain/cosmos-sdk v0.46.0-beta2.0.20240408014408-56c165536d0a/go.mod h1:nRk8EA8/fEG4zSme2i/Rq5z3k7TrlsHkOYhrY79hhD8=
github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240408014408-56c165536d0a h1:YHeIe/VPKf7xK7xxLWUzvspFZJY6fDT6Epj+MH9dLOc=
github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240408014408-56c165536d0a/go.mod h1:lfuLI1f4o+0SGtlHQS4x5qsjRcZZfYqG8bp3k8hM0M8=
github.com/crypto-org-chain/go-block-stm v0.0.0-20240408011717-9f11af197bde h1:sQIHTJfVt5VTrF7po9eZiFkZiPjlHbFvnXtGCOoBjNM=
Expand Down Expand Up @@ -1154,6 +1152,8 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRT
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yihuang/cosmos-db v0.0.0-20240315085537-afdfa9c0f19d h1:rUqI/S51wN+9VgIoUp+OnDvFOktP5ySfZO9658dLO1U=
github.com/yihuang/cosmos-db v0.0.0-20240315085537-afdfa9c0f19d/go.mod h1:Z8IXcFJ9PqKK6BIsVOB3QXtkKoqUOp1vRvPT39kOXEA=
github.com/yihuang/cosmos-sdk v0.43.0-beta1.0.20240408071442-3b12176ada7b h1:5pvrddaUrmZeHqAzUlZnyoGc02zKgnf2OAe+V9GekLk=
github.com/yihuang/cosmos-sdk v0.43.0-beta1.0.20240408071442-3b12176ada7b/go.mod h1:nRk8EA8/fEG4zSme2i/Rq5z3k7TrlsHkOYhrY79hhD8=
github.com/yihuang/ethermint v0.6.1-0.20240408035005-423db7fe92a3 h1:gS8jy4oFoLMRNxiIbkVIKJNTJkCnMlT/38oRXuMdCDs=
github.com/yihuang/ethermint v0.6.1-0.20240408035005-423db7fe92a3/go.mod h1:rODq/GbhjNkzQKLKA6+ADtPxvxJZnldkHYEUPy3sHe4=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
6 changes: 3 additions & 3 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ schema = 3
version = "v1.0.0-beta.4"
hash = "sha256-5Kn82nsZfiEtuwhhLZqmMxdAY1tX/Fi3HJ0/MEaRohw="
[mod."github.com/cosmos/cosmos-sdk"]
version = "v0.46.0-beta2.0.20240408014408-56c165536d0a"
hash = "sha256-xjZvl0pxsEkSW2X8Q37mPHXAHnMeHiwCtX2FmDZVUHQ="
replaced = "github.com/crypto-org-chain/cosmos-sdk"
version = "v0.43.0-beta1.0.20240408071442-3b12176ada7b"
hash = "sha256-/ha2zxwO5i0e5JfwGx45qW23Drv+wDwoRH0wGPuHrqg="
replaced = "github.com/yihuang/cosmos-sdk"
[mod."github.com/cosmos/go-bip39"]
version = "v1.0.0"
hash = "sha256-Qm2aC2vaS8tjtMUbHmlBSagOSqbduEEDwc51qvQaBmA="
Expand Down
6 changes: 3 additions & 3 deletions versiondb/backend_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
)

func SetupTestDB(t *testing.T, store VersionStore) {
changeSets := [][]types.StoreKVPair{
changeSets := [][]*types.StoreKVPair{
{
{StoreKey: "evm", Key: []byte("delete-in-block2"), Value: []byte("1")},
{StoreKey: "evm", Key: []byte("re-add-in-block3"), Value: []byte("1")},
Expand Down Expand Up @@ -57,7 +57,7 @@ func Run(t *testing.T, storeCreator func() VersionStore) {

// test delete in genesis, noop
store := storeCreator()
err := store.PutAtVersion(0, []types.StoreKVPair{
err := store.PutAtVersion(0, []*types.StoreKVPair{
{StoreKey: "evm", Key: []byte{1}, Delete: true},
})
require.NoError(t, err)
Expand Down Expand Up @@ -219,7 +219,7 @@ func testIterator(t *testing.T, store VersionStore) {
v = int64(len(expItems))
err = store.PutAtVersion(
v,
[]types.StoreKVPair{
[]*types.StoreKVPair{
{StoreKey: "evm", Key: []byte("z-genesis-only"), Delete: true},
},
)
Expand Down
10 changes: 4 additions & 6 deletions versiondb/client/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sync"
"time"

log "cosmossdk.io/log"
"github.com/alitto/pond"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/iavl"
Expand Down Expand Up @@ -78,11 +79,8 @@ func DumpChangeSetCmd(opts Options) *cobra.Command {
if endVersion == 0 {
// use the latest version of the first store for all stores
prefix := []byte(fmt.Sprintf(tsrocksdb.StorePrefixTpl, stores[0]))
tree, err := iavl.NewMutableTree(dbm.NewPrefixDB(db, prefix), 0, true)
if err != nil {
return err
}
latestVersion, err := tree.LazyLoadVersion(0)
tree := iavl.NewMutableTree(dbm.NewPrefixDB(db, prefix), 0, true, log.NewNopLogger())
latestVersion, err := tree.LoadVersion(0)
if err != nil {
return err
}
Expand Down Expand Up @@ -117,7 +115,7 @@ func DumpChangeSetCmd(opts Options) *cobra.Command {
iavlTreePool := sync.Pool{
New: func() any {
// use separate prefixdb and iavl tree in each task to maximize concurrency performance
return iavl.NewImmutableTree(dbm.NewPrefixDB(db, prefix), cacheSize, true)
return iavl.NewImmutableTree(dbm.NewPrefixDB(db, prefix), cacheSize, true, log.NewNopLogger())
},
}

Expand Down
16 changes: 15 additions & 1 deletion versiondb/client/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func verifyOneStore(tree *memiavl.Tree, store, changeSetDir, saveSnapshot string
}

// no need to update hashes for intermediate versions.
tree.ApplyChangeSet(*changeSet)
tree.ApplyChangeSet(convertChangeSet(changeSet))
_, v, err := tree.SaveVersion(false)
if err != nil {
return false, err
Expand Down Expand Up @@ -299,3 +299,17 @@ func convertCommitInfo(commitInfo *storetypes.CommitInfo) *memiavl.CommitInfo {
StoreInfos: storeInfos,
}
}

func convertChangeSet(cs *iavl.ChangeSet) memiavl.ChangeSet {
pairs := make([]*memiavl.KVPair, len(cs.Pairs))
for i, pair := range cs.Pairs {
pairs[i] = &memiavl.KVPair{
Delete: pair.Delete,
Key: pair.Key,
Value: pair.Value,
}
}
return memiavl.ChangeSet{
Pairs: pairs,
}
}
75 changes: 35 additions & 40 deletions versiondb/multistore.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package versiondb

import (
"fmt"
"io"
"sync"

"cosmossdk.io/store/cachemulti"
"cosmossdk.io/store/types"
)

var _ types.MultiStore = (*MultiStore)(nil)
var _ types.RootMultiStore = (*MultiStore)(nil)

// MultiStore wraps `VersionStore` to implement `MultiStore` interface.
type MultiStore struct {
versionDB VersionStore
storeKeys []types.StoreKey
stores map[types.StoreKey]types.KVStore

// transient or memory stores
transientStores map[types.StoreKey]struct{}
// transient/memory/object stores, they are delegated to the parent
delegatedStoreKeys map[types.StoreKey]struct{}

// proxy the calls for transient or mem stores to the parent
parent types.MultiStore
Expand All @@ -27,8 +28,22 @@ type MultiStore struct {
}

// NewMultiStore returns a new versiondb `MultiStore`.
func NewMultiStore(parent types.MultiStore, versionDB VersionStore, storeKeys []types.StoreKey) *MultiStore {
return &MultiStore{versionDB: versionDB, storeKeys: storeKeys, parent: parent, transientStores: make(map[types.StoreKey]struct{})}
func NewMultiStore(
parent types.MultiStore,
versionDB VersionStore,
storeKeys map[string]*types.KVStoreKey,
delegatedStoreKeys map[types.StoreKey]struct{},
) *MultiStore {
stores := make(map[types.StoreKey]types.KVStore, len(storeKeys))
for _, k := range storeKeys {
stores[k] = NewKVStore(versionDB, k, nil)
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
return &MultiStore{
versionDB: versionDB,
stores: stores,
parent: parent,
delegatedStoreKeys: delegatedStoreKeys,
}
}

// GetStoreType implements `MultiStore` interface.
Expand All @@ -38,12 +53,16 @@ func (s *MultiStore) GetStoreType() types.StoreType {

// cacheMultiStore branch out the multistore.
func (s *MultiStore) cacheMultiStore(version *int64) types.CacheMultiStore {
stores := make(map[types.StoreKey]types.CacheWrapper, len(s.transientStores)+len(s.storeKeys))
for k := range s.transientStores {
stores := make(map[types.StoreKey]types.CacheWrapper, len(s.delegatedStoreKeys)+len(s.stores))
for k := range s.delegatedStoreKeys {
stores[k] = types.CacheWrapper(s.parent.GetStore(k))
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
for _, k := range s.storeKeys {
stores[k] = NewKVStore(s.versionDB, k, version)
for k := range s.stores {
if version == nil {
stores[k] = s.stores[k]
} else {
stores[k] = NewKVStore(s.versionDB, k, version)
}
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
return cachemulti.NewStore(stores, s.traceWriter, s.getTracingContext())
}
Expand All @@ -63,17 +82,16 @@ func (s *MultiStore) CacheWrap() types.CacheWrap {
return s.CacheMultiStore().(types.CacheWrap)
}

// CacheWrapWithTrace implements the CacheWrapper interface.
func (s *MultiStore) CacheWrapWithTrace(_ io.Writer, _ types.TraceContext) types.CacheWrap {
return s.CacheWrap()
}

// GetStore implements `MultiStore` interface
func (s *MultiStore) GetStore(storeKey types.StoreKey) types.Store {
if _, ok := s.transientStores[storeKey]; ok {
if store, ok := s.stores[storeKey]; ok {
return store
}
if _, ok := s.delegatedStoreKeys[storeKey]; ok {
// delegate the transient/memory/object stores to real cms
return s.parent.GetStore(storeKey)
}
return NewKVStore(s.versionDB, storeKey, nil)
panic(fmt.Errorf("store key %s is not registered", storeKey.Name()))
}

// GetKVStore implements `MultiStore` interface
Expand All @@ -86,29 +104,6 @@ func (s *MultiStore) GetObjKVStore(storeKey types.StoreKey) types.ObjKVStore {
return s.GetStore(storeKey).(types.ObjKVStore)
}

// MountTransientStores simlates the same behavior as sdk to support grpc query service.
func (s *MultiStore) MountTransientStores(keys map[string]*types.TransientStoreKey) {
for _, key := range keys {
s.transientStores[key] = struct{}{}
}
}

// MountMemoryStores simlates the same behavior as sdk to support grpc query service,
// it shares the existing mem store instance.
func (s *MultiStore) MountMemoryStores(keys map[string]*types.MemoryStoreKey) {
for _, key := range keys {
s.transientStores[key] = struct{}{}
}
}

// MountObjectStores simlates the same behavior as sdk to support grpc query service,
// it shares the existing mem store instance.
func (s *MultiStore) MountObjectStores(keys map[string]*types.ObjectStoreKey) {
for _, key := range keys {
s.transientStores[key] = struct{}{}
}
}

// SetTracer sets the tracer for the MultiStore that the underlying
// stores will utilize to trace operations. A MultiStore is returned.
func (s *MultiStore) SetTracer(w io.Writer) types.MultiStore {
Expand Down
13 changes: 0 additions & 13 deletions versiondb/store.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package versiondb

import (
"io"
"time"

"cosmossdk.io/store/cachekv"
"cosmossdk.io/store/listenkv"
"cosmossdk.io/store/tracekv"
"cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/telemetry"
)
Expand Down Expand Up @@ -37,16 +34,6 @@ func (st *Store) CacheWrap() types.CacheWrap {
return cachekv.NewStore(st)
}

// CacheWrapWithTrace implements the Store interface.
func (st *Store) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.CacheWrap {
return cachekv.NewStore(tracekv.NewStore(st, w, tc))
}

// CacheWrapWithListeners implements the CacheWrapper interface.
func (st *Store) CacheWrapWithListeners(storeKey types.StoreKey, listeners []types.MemoryListener) types.CacheWrap {
return cachekv.NewStore(listenkv.NewStore(st, storeKey, listeners))
}

// Implements types.KVStore.
func (st *Store) Get(key []byte) []byte {
defer telemetry.MeasureSince(time.Now(), "store", "versiondb", "get")
Expand Down
Loading

0 comments on commit e4f16ca

Please sign in to comment.