Skip to content

Commit

Permalink
pass basic integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Apr 4, 2023
1 parent 3a21291 commit a25cc71
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ buildGoApplication rec {
"!/client/"
"!/versiondb/"
"!/memiavl/"
"!/store/"
"!go.mod"
"!go.sum"
"!gomod2nix.toml"
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/configs/default.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
},
},
'app-config': {
memiavl: true,
// 'app-db-backend': 'rocksdb',
'app-db-backend': 'rocksdb',
'minimum-gas-prices': '0basetcro',
'index-events': ['ethereum_tx.ethereumTxHash'],
'iavl-lazy-loading': true,
Expand All @@ -36,6 +35,7 @@
staked: '1000000000000000000stake',
mnemonic: '${VALIDATOR1_MNEMONIC}',
'app-config': {
memiavl: true,
store: {
streamers: ['file', 'versiondb'],
},
Expand Down
6 changes: 5 additions & 1 deletion memiavl/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ func (t *Tree) remove(key []byte) {
func (t *Tree) saveVersion(updateHash bool) ([]byte, int64, error) {
var hash []byte
if updateHash {
hash = t.root.Hash()
if t.root == nil {
hash = emptyHash
} else {
hash = t.root.Hash()
}
}

if t.version >= uint32(math.MaxUint32) {
Expand Down
4 changes: 2 additions & 2 deletions store/memiavlstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ func (st *Store) Delete(key []byte) {
}

func (st *Store) Iterator(start, end []byte) types.Iterator {
return nil
return st.tree.Iterator(start, end, true)
}

func (st *Store) ReverseIterator(start, end []byte) types.Iterator {
return nil
return st.tree.Iterator(start, end, false)
}

// SetInitialVersion sets the initial version of the IAVL tree. It is used when
Expand Down
31 changes: 29 additions & 2 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sort"
"strings"

"github.com/cosmos/cosmos-sdk/store/listenkv"
"github.com/crypto-org-chain/cronos/store/memiavlstore"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -35,6 +36,7 @@ type Store struct {
storesParams map[types.StoreKey]storeParams
keysByName map[string]types.StoreKey
stores map[types.StoreKey]types.CommitKVStore
listeners map[types.StoreKey][]types.WriteListener

initialVersion int64
lastCommitInfo *types.CommitInfo
Expand All @@ -48,6 +50,7 @@ func NewStore(dir string, logger log.Logger) *Store {
storesParams: make(map[types.StoreKey]storeParams),
keysByName: make(map[string]types.StoreKey),
stores: make(map[types.StoreKey]types.CommitKVStore),
listeners: make(map[types.StoreKey][]types.WriteListener),
}
}

Expand Down Expand Up @@ -112,7 +115,13 @@ func (rs *Store) CacheMultiStore() types.CacheMultiStore {
// TODO custom cache store
stores := make(map[types.StoreKey]types.CacheWrapper)
for k, v := range rs.stores {
stores[k] = types.KVStore(v)
store := types.KVStore(v)
// Wire the listenkv.Store to allow listeners to observe the writes from the cache store,
// set same listeners on cache store will observe duplicated writes.
if rs.ListeningEnabled(k) {
store = listenkv.NewStore(store, k, rs.listeners[k])
}
stores[k] = store
}
return cachemulti.NewStore(nil, stores, rs.keysByName, nil, nil)
}
Expand All @@ -130,7 +139,17 @@ func (rs *Store) GetStore(key types.StoreKey) types.Store {

// Implements interface MultiStore
func (rs *Store) GetKVStore(key types.StoreKey) types.KVStore {
return rs.stores[key]
s := rs.stores[key]
if s == nil {
panic(fmt.Sprintf("store does not exist for key: %s", key.Name()))
}
store := types.KVStore(s)

if rs.ListeningEnabled(key) {
store = listenkv.NewStore(store, key, rs.listeners[key])
}

return store
}

// Implements interface MultiStore
Expand Down Expand Up @@ -365,11 +384,19 @@ func (rs *Store) RollbackToVersion(version int64) error {

// Implements interface CommitMultiStore
func (rs *Store) ListeningEnabled(key types.StoreKey) bool {
if ls, ok := rs.listeners[key]; ok {
return len(ls) != 0
}
return false
}

// Implements interface CommitMultiStore
func (rs *Store) AddListeners(key types.StoreKey, listeners []types.WriteListener) {
if ls, ok := rs.listeners[key]; ok {
rs.listeners[key] = append(ls, listeners...)
} else {
rs.listeners[key] = listeners
}
}

type storeParams struct {
Expand Down

0 comments on commit a25cc71

Please sign in to comment.