diff --git a/store/cachekv/memiterator.go b/store/cachekv/memiterator.go index 04df40ff56aa..0a4bc57a6406 100644 --- a/store/cachekv/memiterator.go +++ b/store/cachekv/memiterator.go @@ -1,8 +1,6 @@ package cachekv import ( - "bytes" - dbm "github.com/tendermint/tm-db" "github.com/cosmos/cosmos-sdk/store/types" @@ -14,7 +12,6 @@ import ( type memIterator struct { types.Iterator - lastKey []byte deleted map[string]struct{} } @@ -32,25 +29,22 @@ func newMemIterator(start, end []byte, items *dbm.MemDB, deleted map[string]stru panic(err) } + newDeleted := make(map[string]struct{}) + for k, v := range deleted { + newDeleted[k] = v + } + return &memIterator{ Iterator: iter, - lastKey: nil, - deleted: deleted, + deleted: newDeleted, } } func (mi *memIterator) Value() []byte { key := mi.Iterator.Key() - // We need to handle the case where deleted is modified and includes our current key - // We handle this by maintaining a lastKey object in the iterator. - // If the current key is the same as the last key (and last key is not nil / the start) - // then we are calling value on the same thing as last time. - // Therefore we don't check the mi.deleted to see if this key is included in there. - reCallingOnOldLastKey := (mi.lastKey != nil) && bytes.Equal(key, mi.lastKey) - if _, ok := mi.deleted[string(key)]; ok && !reCallingOnOldLastKey { + if _, ok := mi.deleted[string(key)]; ok { return nil } - mi.lastKey = key return mi.Iterator.Value() } diff --git a/store/cachekv/store.go b/store/cachekv/store.go index cdf5f8829bbf..d3c7b4d01be7 100644 --- a/store/cachekv/store.go +++ b/store/cachekv/store.go @@ -218,7 +218,7 @@ func (store *Store) dirtyItems(start, end []byte) { } else { // else do a linear scan to determine if the unsorted pairs are in the pool. for key := range store.unsortedCache { - if dbm.IsKeyInDomain(strToBytes(key), start, end) { + if dbm.IsKeyInDomain(strToByte(key), start, end) { cacheValue := store.cache[key] unsorted = append(unsorted, &kv.Pair{Key: []byte(key), Value: cacheValue.value}) } @@ -246,10 +246,7 @@ func (store *Store) clearUnsortedCacheSubset(unsorted []*kv.Pair) { if item.Value == nil { // deleted element, tracked by store.deleted // setting arbitrary value - err := store.sortedCache.Set(item.Key, []byte{}) - if err != nil { - panic(err) - } + store.sortedCache.Set(item.Key, []byte{}) continue } err := store.sortedCache.Set(item.Key, item.Value) @@ -264,15 +261,14 @@ func (store *Store) clearUnsortedCacheSubset(unsorted []*kv.Pair) { // Only entrypoint to mutate store.cache. func (store *Store) setCacheValue(key, value []byte, deleted bool, dirty bool) { - keyStr := byteSliceToStr(key) - store.cache[keyStr] = &cValue{ + store.cache[string(key)] = &cValue{ value: value, dirty: dirty, } if deleted { - store.deleted[keyStr] = struct{}{} + store.deleted[string(key)] = struct{}{} } else { - delete(store.deleted, keyStr) + delete(store.deleted, string(key)) } if dirty { store.unsortedCache[string(key)] = struct{}{}