store: cleanup (#5753)

* store: cleanup

* minor change

Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
Federico Kunze 2020-03-06 09:28:19 -03:00 committed by GitHub
parent 05c5900a03
commit c5b2abd814
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 50 additions and 346 deletions

View File

@ -1,105 +0,0 @@
package list
import (
"fmt"
"strconv"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/types"
)
// Key for the length of the list
func LengthKey() []byte {
return []byte{0x00}
}
// Key for the elements of the list
func ElemKey(index uint64) []byte {
return append([]byte{0x01}, []byte(fmt.Sprintf("%020d", index))...)
}
// List defines an integer indexable mapper
// It panics when the element type cannot be (un/)marshalled by the codec
type List struct {
cdc *codec.Codec
store types.KVStore
}
// NewList constructs new List
func NewList(cdc *codec.Codec, store types.KVStore) List {
return List{
cdc: cdc,
store: store,
}
}
// Len() returns the length of the list
// The length is only increased by Push() and not decreased
// List dosen't check if an index is in bounds
// The user should check Len() before doing any actions
func (m List) Len() (res uint64) {
bz := m.store.Get(LengthKey())
if bz == nil {
return 0
}
m.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &res)
return
}
// Get() returns the element by its index
func (m List) Get(index uint64, ptr interface{}) error {
bz := m.store.Get(ElemKey(index))
return m.cdc.UnmarshalBinaryLengthPrefixed(bz, ptr)
}
// Set() stores the element to the given position
// Setting element out of range will break length counting
// Use Push() instead of Set() to append a new element
func (m List) Set(index uint64, value interface{}) {
bz := m.cdc.MustMarshalBinaryLengthPrefixed(value)
m.store.Set(ElemKey(index), bz)
}
// Delete() deletes the element in the given position
// Other elements' indices are preserved after deletion
// Panics when the index is out of range
func (m List) Delete(index uint64) {
m.store.Delete(ElemKey(index))
}
// Push() inserts the element to the end of the list
// It will increase the length when it is called
func (m List) Push(value interface{}) {
length := m.Len()
m.Set(length, value)
m.store.Set(LengthKey(), m.cdc.MustMarshalBinaryLengthPrefixed(length+1))
}
// Iterate() is used to iterate over all existing elements in the list
// Return true in the continuation to break
// The second element of the continuation will indicate the position of the element
// Using it with Get() will return the same one with the provided element
// CONTRACT: No writes may happen within a domain while iterating over it.
func (m List) Iterate(ptr interface{}, fn func(uint64) bool) {
iter := types.KVStorePrefixIterator(m.store, []byte{0x01})
defer iter.Close()
for ; iter.Valid(); iter.Next() {
v := iter.Value()
m.cdc.MustUnmarshalBinaryLengthPrefixed(v, ptr)
k := iter.Key()
s := string(k[len(k)-20:])
index, err := strconv.ParseUint(s, 10, 64)
if err != nil {
panic(err)
}
if fn(index) {
break
}
}
}

View File

@ -1,98 +0,0 @@
package list
import (
"math/rand"
"testing"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type TestStruct struct {
I uint64
B bool
}
func defaultComponents(key sdk.StoreKey) (sdk.Context, *codec.Codec) {
db := dbm.NewMemDB()
cms := rootmulti.NewStore(db)
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
cms.LoadLatestVersion()
ctx := sdk.NewContext(cms, abci.Header{}, false, log.NewNopLogger())
cdc := codec.New()
return ctx, cdc
}
func TestList(t *testing.T) {
key := sdk.NewKVStoreKey("test")
ctx, cdc := defaultComponents(key)
store := ctx.KVStore(key)
lm := NewList(cdc, store)
val := TestStruct{1, true}
var res TestStruct
lm.Push(val)
require.Equal(t, uint64(1), lm.Len())
lm.Get(uint64(0), &res)
require.Equal(t, val, res)
val = TestStruct{2, false}
lm.Set(uint64(0), val)
lm.Get(uint64(0), &res)
require.Equal(t, val, res)
val = TestStruct{100, false}
lm.Push(val)
require.Equal(t, uint64(2), lm.Len())
lm.Get(uint64(1), &res)
require.Equal(t, val, res)
lm.Delete(uint64(1))
require.Equal(t, uint64(2), lm.Len())
lm.Iterate(&res, func(index uint64) (brk bool) {
var temp TestStruct
lm.Get(index, &temp)
require.Equal(t, temp, res)
require.True(t, index != 1)
return
})
lm.Iterate(&res, func(index uint64) (brk bool) {
lm.Set(index, TestStruct{res.I + 1, !res.B})
return
})
lm.Get(uint64(0), &res)
require.Equal(t, TestStruct{3, true}, res)
}
func TestListRandom(t *testing.T) {
key := sdk.NewKVStoreKey("test")
ctx, cdc := defaultComponents(key)
store := ctx.KVStore(key)
list := NewList(cdc, store)
mocklist := []uint32{}
for i := 0; i < 100; i++ {
item := rand.Uint32()
list.Push(item)
mocklist = append(mocklist, item)
}
for k, v := range mocklist {
k := k
var i uint32
require.NotPanics(t, func() { list.Get(uint64(k), &i) })
require.Equal(t, v, i)
}
}

View File

@ -97,7 +97,7 @@ func (s Store) Iterator(start, end []byte) types.Iterator {
return newPrefixIterator(s.prefix, start, end, iter)
}
// Implements KVStore
// ReverseIterator implements KVStore
// Check https://github.com/tendermint/tendermint/blob/master/libs/db/prefix_db.go#L129
func (s Store) ReverseIterator(start, end []byte) types.Iterator {
newstart := cloneAppend(s.prefix, start)
@ -117,10 +117,11 @@ func (s Store) ReverseIterator(start, end []byte) types.Iterator {
var _ types.Iterator = (*prefixIterator)(nil)
type prefixIterator struct {
prefix []byte
start, end []byte
iter types.Iterator
valid bool
prefix []byte
start []byte
end []byte
iter types.Iterator
valid bool
}
func newPrefixIterator(prefix, start, end []byte, parent types.Iterator) *prefixIterator {
@ -134,53 +135,54 @@ func newPrefixIterator(prefix, start, end []byte, parent types.Iterator) *prefix
}
// Implements Iterator
func (iter *prefixIterator) Domain() ([]byte, []byte) {
return iter.start, iter.end
func (pi *prefixIterator) Domain() ([]byte, []byte) {
return pi.start, pi.end
}
// Implements Iterator
func (iter *prefixIterator) Valid() bool {
return iter.valid && iter.iter.Valid()
func (pi *prefixIterator) Valid() bool {
return pi.valid && pi.iter.Valid()
}
// Implements Iterator
func (iter *prefixIterator) Next() {
if !iter.valid {
func (pi *prefixIterator) Next() {
if !pi.valid {
panic("prefixIterator invalid, cannot call Next()")
}
iter.iter.Next()
if !iter.iter.Valid() || !bytes.HasPrefix(iter.iter.Key(), iter.prefix) {
iter.valid = false
pi.iter.Next()
if !pi.iter.Valid() || !bytes.HasPrefix(pi.iter.Key(), pi.prefix) {
// TODO: shouldn't pi be set to nil instead?
pi.valid = false
}
}
// Implements Iterator
func (iter *prefixIterator) Key() (key []byte) {
if !iter.valid {
func (pi *prefixIterator) Key() (key []byte) {
if !pi.valid {
panic("prefixIterator invalid, cannot call Key()")
}
key = iter.iter.Key()
key = stripPrefix(key, iter.prefix)
key = pi.iter.Key()
key = stripPrefix(key, pi.prefix)
return
}
// Implements Iterator
func (iter *prefixIterator) Value() []byte {
if !iter.valid {
func (pi *prefixIterator) Value() []byte {
if !pi.valid {
panic("prefixIterator invalid, cannot call Value()")
}
return iter.iter.Value()
return pi.iter.Value()
}
// Implements Iterator
func (iter *prefixIterator) Close() {
iter.iter.Close()
func (pi *prefixIterator) Close() {
pi.iter.Close()
}
// Error returns an error if the prefixIterator is invalid defined by the Valid
// method.
func (iter *prefixIterator) Error() error {
if !iter.Valid() {
func (pi *prefixIterator) Error() error {
if !pi.Valid() {
return errors.New("invalid prefixIterator")
}

View File

@ -1,93 +0,0 @@
package store
// TODO: make it independent from list
/*
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/store/list"
)
// Key for the top element position in the queue
func TopKey() []byte {
return []byte{0x02}
}
// Queue is a List wrapper that provides queue-like functions
// It panics when the element type cannot be (un/)marshalled by the codec
type Queue struct {
List list.List
}
// NewQueue constructs new Queue
func NewQueue(cdc *codec.Codec, store sdk.KVStore) Queue {
return Queue{NewList(cdc, store)}
}
func (m Queue) getTop() (res uint64) {
bz := m.List.store.Get(TopKey())
if bz == nil {
return 0
}
m.List.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &res)
return
}
func (m Queue) setTop(top uint64) {
bz := m.List.cdc.MustMarshalBinaryLengthPrefixed(top)
m.List.store.Set(TopKey(), bz)
}
// Push() inserts the elements to the rear of the queue
func (m Queue) Push(value interface{}) {
m.List.Push(value)
}
// Popping/Peeking on an empty queue will cause panic
// The user should check IsEmpty() before doing any actions
// Peek() returns the element at the front of the queue without removing it
func (m Queue) Peek(ptr interface{}) error {
top := m.getTop()
return m.List.Get(top, ptr)
}
// Pop() returns the element at the front of the queue and removes it
func (m Queue) Pop() {
top := m.getTop()
m.List.Delete(top)
m.setTop(top + 1)
}
// IsEmpty() checks if the queue is empty
func (m Queue) IsEmpty() bool {
top := m.getTop()
length := m.List.Len()
return top >= length
}
// Flush() removes elements it processed
// Return true in the continuation to break
// The interface{} is unmarshalled before the continuation is called
// Starts from the top(head) of the queue
// CONTRACT: Pop() or Push() should not be performed while flushing
func (m Queue) Flush(ptr interface{}, fn func() bool) {
top := m.getTop()
length := m.List.Len()
var i uint64
for i = top; i < length; i++ {
err := m.List.Get(i, ptr)
if err != nil {
// TODO: Handle with #870
panic(err)
}
m.List.Delete(i)
if fn() {
break
}
}
m.setTop(i)
}
*/

View File

@ -21,7 +21,6 @@ func NewMultiStoreProof(storeInfos []storeInfo) *MultiStoreProof {
// ComputeRootHash returns the root hash for a given multi-store proof.
func (proof *MultiStoreProof) ComputeRootHash() []byte {
ci := commitInfo{
Version: -1, // TODO: Not needed; improve code.
StoreInfos: proof.StoreInfos,
}
return ci.Hash()

View File

@ -5,11 +5,14 @@ import (
"io"
"strings"
"github.com/pkg/errors"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/crypto/tmhash"
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/cachemulti"
"github.com/cosmos/cosmos-sdk/store/dbadapter"
"github.com/cosmos/cosmos-sdk/store/iavl"
@ -24,6 +27,8 @@ const (
commitInfoKeyFmt = "s/%d" // s/<version>
)
var cdc = codec.New()
// Store is composed of many CommitStores. Name contrasts with
// cacheMultiStore which is for cache-wrapping other MultiStores. It implements
// the CommitMultiStore interface.
@ -77,21 +82,21 @@ func (rs *Store) SetLazyLoading(lazyLoading bool) {
rs.lazyLoading = lazyLoading
}
// Implements Store.
// GetStoreType implements Store.
func (rs *Store) GetStoreType() types.StoreType {
return types.StoreTypeMulti
}
// Implements CommitMultiStore.
// MountStoreWithDB implements CommitMultiStore.
func (rs *Store) MountStoreWithDB(key types.StoreKey, typ types.StoreType, db dbm.DB) {
if key == nil {
panic("MountIAVLStore() key cannot be nil")
}
if _, ok := rs.storesParams[key]; ok {
panic(fmt.Sprintf("Store duplicate store key %v", key))
panic(fmt.Sprintf("store duplicate store key %v", key))
}
if _, ok := rs.keysByName[key.Name()]; ok {
panic(fmt.Sprintf("Store duplicate store key name %v", key))
panic(fmt.Sprintf("store duplicate store key name %v", key))
}
rs.storesParams[key] = storeParams{
key: key,
@ -168,14 +173,14 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error {
// Load it
store, err := rs.loadCommitStoreFromParams(key, rs.getCommitID(infos, key.Name()), storeParams)
if err != nil {
return fmt.Errorf("failed to load Store: %v", err)
return errors.Wrap(err, "failed to load store")
}
newStores[key] = store
// If it was deleted, remove all data
if upgrades.IsDeleted(key.Name()) {
if err := deleteKVStore(store.(types.KVStore)); err != nil {
return fmt.Errorf("failed to delete store %s: %v", key.Name(), err)
return errors.Wrapf(err, "failed to delete store %s", key.Name())
}
} else if oldName := upgrades.RenamedFrom(key.Name()); oldName != "" {
// handle renames specially
@ -187,12 +192,12 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error {
// load from the old name
oldStore, err := rs.loadCommitStoreFromParams(oldKey, rs.getCommitID(infos, oldName), oldParams)
if err != nil {
return fmt.Errorf("failed to load old Store '%s': %v", oldName, err)
return errors.Wrapf(err, "failed to load old store %s", oldName)
}
// move all data
if err := moveKVStoreData(oldStore.(types.KVStore), store.(types.KVStore)); err != nil {
return fmt.Errorf("failed to move store %s -> %s: %v", oldName, key.Name(), err)
return errors.Wrapf(err, "failed to move store %s -> %s", oldName, key.Name())
}
}
}
@ -279,12 +284,12 @@ func (rs *Store) TracingEnabled() bool {
//----------------------------------------
// +CommitStore
// Implements Committer/CommitStore.
// LastCommitID implements Committer/CommitStore.
func (rs *Store) LastCommitID() types.CommitID {
return rs.lastCommitInfo.CommitID()
}
// Implements Committer/CommitStore.
// Commit implements Committer/CommitStore.
func (rs *Store) Commit() types.CommitID {
// Commit stores.
@ -304,7 +309,7 @@ func (rs *Store) Commit() types.CommitID {
return commitID
}
// Implements CacheWrapper/Store/CommitStore.
// CacheWrap implements CacheWrapper/Store/CommitStore.
func (rs *Store) CacheWrap() types.CacheWrap {
return rs.CacheMultiStore().(types.CacheWrap)
}
@ -656,16 +661,16 @@ func getCommitInfo(db dbm.DB, ver int64) (commitInfo, error) {
cInfoKey := fmt.Sprintf(commitInfoKeyFmt, ver)
cInfoBytes, err := db.Get([]byte(cInfoKey))
if err != nil {
return commitInfo{}, fmt.Errorf("failed to get commit info: %v", err)
return commitInfo{}, errors.Wrap(err, "failed to get commit info")
} else if cInfoBytes == nil {
return commitInfo{}, fmt.Errorf("failed to get commit info: no data")
return commitInfo{}, errors.New("failed to get commit info: no data")
}
var cInfo commitInfo
err = cdc.UnmarshalBinaryLengthPrefixed(cInfoBytes, &cInfo)
if err != nil {
return commitInfo{}, fmt.Errorf("failed to get Store: %v", err)
return commitInfo{}, errors.Wrap(err, "failed to get store")
}
return cInfo, nil

View File

@ -49,6 +49,7 @@ func TestStoreMount(t *testing.T) {
require.NotPanics(t, func() { store.MountStoreWithDB(key2, types.StoreTypeIAVL, db) })
require.Panics(t, func() { store.MountStoreWithDB(key1, types.StoreTypeIAVL, db) })
require.Panics(t, func() { store.MountStoreWithDB(nil, types.StoreTypeIAVL, db) })
require.Panics(t, func() { store.MountStoreWithDB(dup1, types.StoreTypeIAVL, db) })
}

View File

@ -1,7 +0,0 @@
package rootmulti
import (
"github.com/cosmos/cosmos-sdk/codec"
)
var cdc = codec.New()

View File

@ -3,10 +3,10 @@ package tracekv
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/types/errors"
)
const (
@ -187,11 +187,11 @@ func writeOperation(w io.Writer, op operation, tc types.TraceContext, key, value
raw, err := json.Marshal(traceOp)
if err != nil {
panic(fmt.Sprintf("failed to serialize trace operation: %v", err))
panic(errors.Wrap(err, "failed to serialize trace operation"))
}
if _, err := w.Write(raw); err != nil {
panic(fmt.Sprintf("failed to write trace operation: %v", err))
panic(errors.Wrap(err, "failed to write trace operation"))
}
io.WriteString(w, "\n")