From 4f80b1f7f698fddb483fde6f1d4527a7ff0d8958 Mon Sep 17 00:00:00 2001 From: mossid Date: Fri, 14 Sep 2018 03:52:09 +0900 Subject: [PATCH 01/22] in progress --- store/{ => cachekv}/cachekvstore.go | 76 ++++++++++--------- store/{ => cachekv}/cachekvstore_test.go | 2 +- store/{ => gaskv}/gaskvstore.go | 0 store/{ => gaskv}/gaskvstore_test.go | 0 store/{ => iavl}/iavlstore.go | 0 store/{ => iavl}/iavlstore_test.go | 0 store/{ => prefix}/prefixstore.go | 41 +++++----- store/{ => prefix}/prefixstore_test.go | 2 +- store/{ => rootmulti}/multistoreproof.go | 0 store/{ => rootmulti}/multistoreproof_test.go | 0 store/{ => rootmulti}/rootmultistore.go | 0 store/{ => rootmulti}/rootmultistore_test.go | 0 store/store.go | 9 +++ store/{ => tracekv}/tracekvstore.go | 22 ++---- store/{ => tracekv}/tracekvstore_test.go | 0 store/{ => transient}/transientstore.go | 0 store/{ => transient}/transientstore_test.go | 0 types/context.go | 4 +- types/store.go | 10 --- 19 files changed, 79 insertions(+), 87 deletions(-) rename store/{ => cachekv}/cachekvstore.go (64%) rename store/{ => cachekv}/cachekvstore_test.go (99%) rename store/{ => gaskv}/gaskvstore.go (100%) rename store/{ => gaskv}/gaskvstore_test.go (100%) rename store/{ => iavl}/iavlstore.go (100%) rename store/{ => iavl}/iavlstore_test.go (100%) rename store/{ => prefix}/prefixstore.go (82%) rename store/{ => prefix}/prefixstore_test.go (99%) rename store/{ => rootmulti}/multistoreproof.go (100%) rename store/{ => rootmulti}/multistoreproof_test.go (100%) rename store/{ => rootmulti}/rootmultistore.go (100%) rename store/{ => rootmulti}/rootmultistore_test.go (100%) create mode 100644 store/store.go rename store/{ => tracekv}/tracekvstore.go (88%) rename store/{ => tracekv}/tracekvstore_test.go (100%) rename store/{ => transient}/transientstore.go (100%) rename store/{ => transient}/transientstore_test.go (100%) diff --git a/store/cachekvstore.go b/store/cachekv/cachekvstore.go similarity index 64% rename from store/cachekvstore.go rename to store/cachekv/cachekvstore.go index 9eb4ae9324db..fa5770b3606b 100644 --- a/store/cachekvstore.go +++ b/store/cachekv/cachekvstore.go @@ -1,4 +1,4 @@ -package store +package cachekv import ( "bytes" @@ -7,6 +7,8 @@ import ( "sync" cmn "github.com/tendermint/tendermint/libs/common" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // If value is nil but deleted is false, it means the parent doesn't have the @@ -17,30 +19,30 @@ type cValue struct { dirty bool } -// cacheKVStore wraps an in-memory cache around an underlying KVStore. -type cacheKVStore struct { +// Store wraps an in-memory cache around an underlying sdk.KVStore. +type Store struct { mtx sync.Mutex cache map[string]cValue - parent KVStore + parent sdk.KVStore } -var _ CacheKVStore = (*cacheKVStore)(nil) +var _ sdk.CacheKVStore = (*Store)(nil) // nolint -func NewCacheKVStore(parent KVStore) *cacheKVStore { - return &cacheKVStore{ +func NewStore(parent sdk.KVStore) *Store { + return &Store{ cache: make(map[string]cValue), parent: parent, } } // Implements Store. -func (ci *cacheKVStore) GetStoreType() StoreType { +func (ci *Store) GetStoreType() sdk.StoreType { return ci.parent.GetStoreType() } -// Implements KVStore. -func (ci *cacheKVStore) Get(key []byte) (value []byte) { +// Implements sdk.KVStore. +func (ci *Store) Get(key []byte) (value []byte) { ci.mtx.Lock() defer ci.mtx.Unlock() ci.assertValidKey(key) @@ -56,8 +58,8 @@ func (ci *cacheKVStore) Get(key []byte) (value []byte) { return value } -// Implements KVStore. -func (ci *cacheKVStore) Set(key []byte, value []byte) { +// Implements sdk.KVStore. +func (ci *Store) Set(key []byte, value []byte) { ci.mtx.Lock() defer ci.mtx.Unlock() ci.assertValidKey(key) @@ -65,14 +67,14 @@ func (ci *cacheKVStore) Set(key []byte, value []byte) { ci.setCacheValue(key, value, false, true) } -// Implements KVStore. -func (ci *cacheKVStore) Has(key []byte) bool { +// Implements sdk.KVStore. +func (ci *Store) Has(key []byte) bool { value := ci.Get(key) return value != nil } -// Implements KVStore. -func (ci *cacheKVStore) Delete(key []byte) { +// Implements sdk.KVStore. +func (ci *Store) Delete(key []byte) { ci.mtx.Lock() defer ci.mtx.Unlock() ci.assertValidKey(key) @@ -80,18 +82,18 @@ func (ci *cacheKVStore) Delete(key []byte) { ci.setCacheValue(key, nil, true, true) } -// Implements KVStore -func (ci *cacheKVStore) Prefix(prefix []byte) KVStore { - return prefixStore{ci, prefix} +// Implements sdk.KVStore +func (ci *Store) Prefix(pref []byte) sdk.KVStore { + return prefix.NewStore(ci, pref) } -// Implements KVStore -func (ci *cacheKVStore) Gas(meter GasMeter, config GasConfig) KVStore { +// Implements sdk.KVStore +func (ci *Store) Gas(meter GasMeter, config GasConfig) sdk.KVStore { return NewGasKVStore(meter, config, ci) } -// Implements CacheKVStore. -func (ci *cacheKVStore) Write() { +// Implements Store. +func (ci *Store) Write() { ci.mtx.Lock() defer ci.mtx.Unlock() @@ -124,33 +126,33 @@ func (ci *cacheKVStore) Write() { } //---------------------------------------- -// To cache-wrap this cacheKVStore further. +// To cache-wrap this Store further. // Implements CacheWrapper. -func (ci *cacheKVStore) CacheWrap() CacheWrap { - return NewCacheKVStore(ci) +func (ci *Store) CacheWrap() CacheWrap { + return NewStore(ci) } // CacheWrapWithTrace implements the CacheWrapper interface. -func (ci *cacheKVStore) CacheWrapWithTrace(w io.Writer, tc TraceContext) CacheWrap { - return NewCacheKVStore(NewTraceKVStore(ci, w, tc)) +func (ci *Store) CacheWrapWithTrace(w io.Writer, tc TraceContext) CacheWrap { + return NewStore(NewTraceKVStore(ci, w, tc)) } //---------------------------------------- // Iteration -// Implements KVStore. -func (ci *cacheKVStore) Iterator(start, end []byte) Iterator { +// Implements sdk.KVStore. +func (ci *Store) Iterator(start, end []byte) sdk.Iterator { return ci.iterator(start, end, true) } -// Implements KVStore. -func (ci *cacheKVStore) ReverseIterator(start, end []byte) Iterator { +// Implements sdk.KVStore. +func (ci *Store) ReverseIterator(start, end []byte) sdk.Iterator { return ci.iterator(start, end, false) } -func (ci *cacheKVStore) iterator(start, end []byte, ascending bool) Iterator { - var parent, cache Iterator +func (ci *Store) iterator(start, end []byte, ascending bool) sdk.Iterator { + var parent, cache sdk.Iterator if ascending { parent = ci.parent.Iterator(start, end) @@ -165,7 +167,7 @@ func (ci *cacheKVStore) iterator(start, end []byte, ascending bool) Iterator { } // Constructs a slice of dirty items, to use w/ memIterator. -func (ci *cacheKVStore) dirtyItems(ascending bool) []cmn.KVPair { +func (ci *Store) dirtyItems(ascending bool) []cmn.KVPair { items := make([]cmn.KVPair, 0, len(ci.cache)) for key, cacheValue := range ci.cache { @@ -190,14 +192,14 @@ func (ci *cacheKVStore) dirtyItems(ascending bool) []cmn.KVPair { //---------------------------------------- // etc -func (ci *cacheKVStore) assertValidKey(key []byte) { +func (ci *Store) assertValidKey(key []byte) { if key == nil { panic("key is nil") } } // Only entrypoint to mutate ci.cache. -func (ci *cacheKVStore) setCacheValue(key, value []byte, deleted bool, dirty bool) { +func (ci *Store) setCacheValue(key, value []byte, deleted bool, dirty bool) { ci.cache[string(key)] = cValue{ value: value, deleted: deleted, diff --git a/store/cachekvstore_test.go b/store/cachekv/cachekvstore_test.go similarity index 99% rename from store/cachekvstore_test.go rename to store/cachekv/cachekvstore_test.go index 37e0364fb5aa..8861dea8779f 100644 --- a/store/cachekvstore_test.go +++ b/store/cachekv/cachekvstore_test.go @@ -1,4 +1,4 @@ -package store +package cachekv import ( "fmt" diff --git a/store/gaskvstore.go b/store/gaskv/gaskvstore.go similarity index 100% rename from store/gaskvstore.go rename to store/gaskv/gaskvstore.go diff --git a/store/gaskvstore_test.go b/store/gaskv/gaskvstore_test.go similarity index 100% rename from store/gaskvstore_test.go rename to store/gaskv/gaskvstore_test.go diff --git a/store/iavlstore.go b/store/iavl/iavlstore.go similarity index 100% rename from store/iavlstore.go rename to store/iavl/iavlstore.go diff --git a/store/iavlstore_test.go b/store/iavl/iavlstore_test.go similarity index 100% rename from store/iavlstore_test.go rename to store/iavl/iavlstore_test.go diff --git a/store/prefixstore.go b/store/prefix/prefixstore.go similarity index 82% rename from store/prefixstore.go rename to store/prefix/prefixstore.go index 3abea46539b8..f08d4200260d 100644 --- a/store/prefixstore.go +++ b/store/prefix/prefixstore.go @@ -1,19 +1,21 @@ -package store +package prefix import ( "bytes" "io" sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/cosmos-sdk/store/cachekv" + "github.com/cosmos/cosmos-sdk/store/tracekv" ) -var _ KVStore = prefixStore{} +var _ sdk.KVStore = Store{} // prefixStore is similar with tendermint/tendermint/libs/db/prefix_db // both gives access only to the limited subset of the store // for convinience or safety - -type prefixStore struct { +type Store struct { parent KVStore prefix []byte } @@ -34,54 +36,53 @@ func (s prefixStore) key(key []byte) (res []byte) { } // Implements Store -func (s prefixStore) GetStoreType() StoreType { +func (s Store) GetStoreType() sdk.StoreType { return s.parent.GetStoreType() } // Implements CacheWrap -func (s prefixStore) CacheWrap() CacheWrap { - return NewCacheKVStore(s) +func (s Store) CacheWrap() sdk.CacheWrap { + return cachekv.NewStore(s) } // CacheWrapWithTrace implements the KVStore interface. -func (s prefixStore) CacheWrapWithTrace(w io.Writer, tc TraceContext) CacheWrap { - return NewCacheKVStore(NewTraceKVStore(s, w, tc)) +func (s Store) CacheWrapWithTrace(w io.Writer, tc sdk.TraceContext) sdk.CacheWrap { + return cachekv.NewStore(tracekv.NewStore(s, w, tc)) } // Implements KVStore -func (s prefixStore) Get(key []byte) []byte { - res := s.parent.Get(s.key(key)) - return res +func (s Store) Get(key []byte) []byte { + return s.parent.Get(s.key(key)) } // Implements KVStore -func (s prefixStore) Has(key []byte) bool { +func (s Store) Has(key []byte) bool { return s.parent.Has(s.key(key)) } // Implements KVStore -func (s prefixStore) Set(key, value []byte) { +func (s Store) Set(key, value []byte) { s.parent.Set(s.key(key), value) } // Implements KVStore -func (s prefixStore) Delete(key []byte) { +func (s Store) Delete(key []byte) { s.parent.Delete(s.key(key)) } // Implements KVStore -func (s prefixStore) Prefix(prefix []byte) KVStore { - return prefixStore{s, prefix} +func (s Store) Prefix(prefix []byte) sdk.KVStore { + return Store{s, prefix} } // Implements KVStore -func (s prefixStore) Gas(meter GasMeter, config GasConfig) KVStore { +func (s Store) Gas(meter sdk.GasMeter, config sdk.GasConfig) sdk.KVStore { return NewGasKVStore(meter, config, s) } // Implements KVStore // Check https://github.com/tendermint/tendermint/blob/master/libs/db/prefix_db.go#L106 -func (s prefixStore) Iterator(start, end []byte) Iterator { +func (s Store) Iterator(start, end []byte) Iterator { newstart := cloneAppend(s.prefix, start) var newend []byte @@ -98,7 +99,7 @@ func (s prefixStore) Iterator(start, end []byte) Iterator { // Implements KVStore // Check https://github.com/tendermint/tendermint/blob/master/libs/db/prefix_db.go#L129 -func (s prefixStore) ReverseIterator(start, end []byte) Iterator { +func (s Store) ReverseIterator(start, end []byte) Iterator { var newstart []byte if start == nil { newstart = cpIncr(s.prefix) diff --git a/store/prefixstore_test.go b/store/prefix/prefixstore_test.go similarity index 99% rename from store/prefixstore_test.go rename to store/prefix/prefixstore_test.go index 4e555292c3c2..01d409c6ea52 100644 --- a/store/prefixstore_test.go +++ b/store/prefix/prefixstore_test.go @@ -1,4 +1,4 @@ -package store +package prefix import ( "math/rand" diff --git a/store/multistoreproof.go b/store/rootmulti/multistoreproof.go similarity index 100% rename from store/multistoreproof.go rename to store/rootmulti/multistoreproof.go diff --git a/store/multistoreproof_test.go b/store/rootmulti/multistoreproof_test.go similarity index 100% rename from store/multistoreproof_test.go rename to store/rootmulti/multistoreproof_test.go diff --git a/store/rootmultistore.go b/store/rootmulti/rootmultistore.go similarity index 100% rename from store/rootmultistore.go rename to store/rootmulti/rootmultistore.go diff --git a/store/rootmultistore_test.go b/store/rootmulti/rootmultistore_test.go similarity index 100% rename from store/rootmultistore_test.go rename to store/rootmulti/rootmultistore_test.go diff --git a/store/store.go b/store/store.go new file mode 100644 index 000000000000..0068b1d8629f --- /dev/null +++ b/store/store.go @@ -0,0 +1,9 @@ +package store + +import ( + "github.com/cosmos/cosmos-sdk/store/gaskv" + "github.com/cosmos/cosmos-sdk/store/iavl" + "github.com/cosmos/cosmos-sdk/store/prefix" + "github.com/cosmos/cosmos-sdk/store/rootmulti" + "github.com/cosmos/cosmos-sdk/store/tracekv" +) diff --git a/store/tracekvstore.go b/store/tracekv/tracekvstore.go similarity index 88% rename from store/tracekvstore.go rename to store/tracekv/tracekvstore.go index d8c34c354542..25367424b7c2 100644 --- a/store/tracekvstore.go +++ b/store/tracekv/tracekvstore.go @@ -27,7 +27,7 @@ type ( TraceKVStore struct { parent sdk.KVStore writer io.Writer - context TraceContext + context sdk.TraceContext } // operation represents an IO operation @@ -44,7 +44,7 @@ type ( // NewTraceKVStore returns a reference to a new traceKVStore given a parent // KVStore implementation and a buffered writer. -func NewTraceKVStore(parent sdk.KVStore, writer io.Writer, tc TraceContext) *TraceKVStore { +func NewTraceKVStore(parent sdk.KVStore, writer io.Writer, tc sdk.TraceContext) *TraceKVStore { return &TraceKVStore{parent: parent, writer: writer, context: tc} } @@ -77,16 +77,6 @@ func (tkv *TraceKVStore) Has(key []byte) bool { return tkv.parent.Has(key) } -// Prefix implements the KVStore interface. -func (tkv *TraceKVStore) Prefix(prefix []byte) KVStore { - return prefixStore{tkv, prefix} -} - -// Gas implements the KVStore interface. -func (tkv *TraceKVStore) Gas(meter GasMeter, config GasConfig) KVStore { - return NewGasKVStore(meter, config, tkv.parent) -} - // Iterator implements the KVStore interface. It delegates the Iterator call // the to the parent KVStore. func (tkv *TraceKVStore) Iterator(start, end []byte) sdk.Iterator { @@ -116,10 +106,10 @@ func (tkv *TraceKVStore) iterator(start, end []byte, ascending bool) sdk.Iterato type traceIterator struct { parent sdk.Iterator writer io.Writer - context TraceContext + context sdk.TraceContext } -func newTraceIterator(w io.Writer, parent sdk.Iterator, tc TraceContext) sdk.Iterator { +func newTraceIterator(w io.Writer, parent sdk.Iterator, tc sdk.TraceContext) sdk.Iterator { return &traceIterator{writer: w, parent: parent, context: tc} } @@ -173,14 +163,14 @@ func (tkv *TraceKVStore) CacheWrap() sdk.CacheWrap { // CacheWrapWithTrace implements the KVStore interface. It panics as a // TraceKVStore cannot be cache wrapped. -func (tkv *TraceKVStore) CacheWrapWithTrace(_ io.Writer, _ TraceContext) CacheWrap { +func (tkv *TraceKVStore) CacheWrapWithTrace(_ io.Writer, _ sdk.TraceContext) sdk.CacheWrap { panic("cannot CacheWrapWithTrace a TraceKVStore") } // writeOperation writes a KVStore operation to the underlying io.Writer as // JSON-encoded data where the key/value pair is base64 encoded. // nolint: errcheck -func writeOperation(w io.Writer, op operation, tc TraceContext, key, value []byte) { +func writeOperation(w io.Writer, op operation, tc sdk.TraceContext, key, value []byte) { traceOp := traceOperation{ Operation: op, Key: base64.StdEncoding.EncodeToString(key), diff --git a/store/tracekvstore_test.go b/store/tracekv/tracekvstore_test.go similarity index 100% rename from store/tracekvstore_test.go rename to store/tracekv/tracekvstore_test.go diff --git a/store/transientstore.go b/store/transient/transientstore.go similarity index 100% rename from store/transientstore.go rename to store/transient/transientstore.go diff --git a/store/transientstore_test.go b/store/transient/transientstore_test.go similarity index 100% rename from store/transientstore_test.go rename to store/transient/transientstore_test.go diff --git a/types/context.go b/types/context.go index bfb4c58fed10..29c7f4d8b7a4 100644 --- a/types/context.go +++ b/types/context.go @@ -73,12 +73,12 @@ func (c Context) Value(key interface{}) interface{} { // KVStore fetches a KVStore from the MultiStore. func (c Context) KVStore(key StoreKey) KVStore { - return c.multiStore().GetKVStore(key).Gas(c.GasMeter(), cachedKVGasConfig) + return c.multiStore().GetKVStore(key) } // TransientStore fetches a TransientStore from the MultiStore. func (c Context) TransientStore(key StoreKey) KVStore { - return c.multiStore().GetKVStore(key).Gas(c.GasMeter(), cachedTransientGasConfig) + return c.multiStore().GetKVStore(key) } //---------------------------------------- diff --git a/types/store.go b/types/store.go index 7bab93b97e60..a1877a9948bf 100644 --- a/types/store.go +++ b/types/store.go @@ -151,16 +151,6 @@ type KVStore interface { // TODO Not yet implemented. // GetSubKVStore(key *storeKey) KVStore - - // Prefix applied keys with the argument - // CONTRACT: when Prefix is called on a KVStore more than once, - // the concatanation of the prefixes is applied - Prefix(prefix []byte) KVStore - - // Gas consuming store - // CONTRACT: when Gas is called on a KVStore more than once, - // the concatanation of the meters/configs is applied - Gas(GasMeter, GasConfig) KVStore } // Alias iterator to db's Iterator for convenience. From 126cc991de6b6a3dba1ce33bfa7242b3a85facbc Mon Sep 17 00:00:00 2001 From: mossid Date: Sat, 15 Sep 2018 01:35:25 +0900 Subject: [PATCH 02/22] finished moving stores to each folder, adding MountableStore --- store/{ => cache}/cachemergeiterator.go | 12 +- store/{ => cache}/memiterator.go | 2 +- .../cachekvstore.go => cache/store.go} | 25 +-- .../store_test.go} | 43 +++--- store/codec.go | 32 ---- store/dbadapter/store.go | 33 ++++ store/dbstoreadapter.go | 41 ----- store/{gaskv/gaskvstore.go => gas/store.go} | 87 ++++------- .../gaskvstore_test.go => gas/store_test.go} | 6 +- store/iavl/{iavlstore.go => store.go} | 68 ++++----- .../iavl/{iavlstore_test.go => store_test.go} | 14 +- store/{ => iavl}/wire.go | 2 +- store/prefix/{prefixstore.go => store.go} | 37 ++--- .../{prefixstore_test.go => store_test.go} | 0 .../{multistoreproof.go => proof.go} | 2 +- ...{multistoreproof_test.go => proof_test.go} | 16 +- .../rootmulti/{rootmultistore.go => store.go} | 144 +++++++++--------- .../{rootmultistore_test.go => store_test.go} | 16 +- store/rootmulti/wire.go | 7 + store/store.go | 4 +- .../tracekvstore.go => trace/store.go} | 10 +- .../store_test.go} | 14 +- store/transient/transientstore.go | 37 ++--- store/transient/transientstore_test.go | 2 +- types/store.go | 22 +-- 25 files changed, 277 insertions(+), 399 deletions(-) rename store/{ => cache}/cachemergeiterator.go (95%) rename store/{ => cache}/memiterator.go (99%) rename store/{cachekv/cachekvstore.go => cache/store.go} (88%) rename store/{cachekv/cachekvstore_test.go => cache/store_test.go} (91%) delete mode 100644 store/codec.go create mode 100644 store/dbadapter/store.go delete mode 100644 store/dbstoreadapter.go rename store/{gaskv/gaskvstore.go => gas/store.go} (55%) rename store/{gaskv/gaskvstore_test.go => gas/store_test.go} (96%) rename store/iavl/{iavlstore.go => store.go} (86%) rename store/iavl/{iavlstore_test.go => store_test.go} (98%) rename store/{ => iavl}/wire.go (83%) rename store/prefix/{prefixstore.go => store.go} (82%) rename store/prefix/{prefixstore_test.go => store_test.go} (100%) rename store/rootmulti/{multistoreproof.go => proof.go} (99%) rename store/rootmulti/{multistoreproof_test.go => proof_test.go} (92%) rename store/rootmulti/{rootmultistore.go => store.go} (72%) rename store/rootmulti/{rootmultistore_test.go => store_test.go} (94%) create mode 100644 store/rootmulti/wire.go rename store/{tracekv/tracekvstore.go => trace/store.go} (94%) rename store/{tracekv/tracekvstore_test.go => trace/store_test.go} (96%) diff --git a/store/cachemergeiterator.go b/store/cache/cachemergeiterator.go similarity index 95% rename from store/cachemergeiterator.go rename to store/cache/cachemergeiterator.go index cd739500de76..cc0d0be511fb 100644 --- a/store/cachemergeiterator.go +++ b/store/cache/cachemergeiterator.go @@ -1,7 +1,9 @@ -package store +package cache import ( "bytes" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // cacheMergeIterator merges a parent Iterator and a cache Iterator. @@ -12,14 +14,14 @@ import ( // // TODO: Optimize by memoizing. type cacheMergeIterator struct { - parent Iterator - cache Iterator + parent sdk.Iterator + cache sdk.Iterator ascending bool } -var _ Iterator = (*cacheMergeIterator)(nil) +var _ sdk.Iterator = (*cacheMergeIterator)(nil) -func newCacheMergeIterator(parent, cache Iterator, ascending bool) *cacheMergeIterator { +func newCacheMergeIterator(parent, cache sdk.Iterator, ascending bool) *cacheMergeIterator { iter := &cacheMergeIterator{ parent: parent, cache: cache, diff --git a/store/memiterator.go b/store/cache/memiterator.go similarity index 99% rename from store/memiterator.go rename to store/cache/memiterator.go index a72418db63c2..4c355057367c 100644 --- a/store/memiterator.go +++ b/store/cache/memiterator.go @@ -1,4 +1,4 @@ -package store +package cache import ( "bytes" diff --git a/store/cachekv/cachekvstore.go b/store/cache/store.go similarity index 88% rename from store/cachekv/cachekvstore.go rename to store/cache/store.go index fa5770b3606b..d677f7fee913 100644 --- a/store/cachekv/cachekvstore.go +++ b/store/cache/store.go @@ -1,4 +1,4 @@ -package cachekv +package cache import ( "bytes" @@ -9,6 +9,8 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/cosmos-sdk/store/trace" ) // If value is nil but deleted is false, it means the parent doesn't have the @@ -36,11 +38,6 @@ func NewStore(parent sdk.KVStore) *Store { } } -// Implements Store. -func (ci *Store) GetStoreType() sdk.StoreType { - return ci.parent.GetStoreType() -} - // Implements sdk.KVStore. func (ci *Store) Get(key []byte) (value []byte) { ci.mtx.Lock() @@ -82,16 +79,6 @@ func (ci *Store) Delete(key []byte) { ci.setCacheValue(key, nil, true, true) } -// Implements sdk.KVStore -func (ci *Store) Prefix(pref []byte) sdk.KVStore { - return prefix.NewStore(ci, pref) -} - -// Implements sdk.KVStore -func (ci *Store) Gas(meter GasMeter, config GasConfig) sdk.KVStore { - return NewGasKVStore(meter, config, ci) -} - // Implements Store. func (ci *Store) Write() { ci.mtx.Lock() @@ -129,13 +116,13 @@ func (ci *Store) Write() { // To cache-wrap this Store further. // Implements CacheWrapper. -func (ci *Store) CacheWrap() CacheWrap { +func (ci *Store) CacheWrap() sdk.CacheWrap { return NewStore(ci) } // CacheWrapWithTrace implements the CacheWrapper interface. -func (ci *Store) CacheWrapWithTrace(w io.Writer, tc TraceContext) CacheWrap { - return NewStore(NewTraceKVStore(ci, w, tc)) +func (ci *Store) CacheWrapWithTrace(w io.Writer, tc sdk.TraceContext) sdk.CacheWrap { + return NewStore(trace.NewStore(ci, w, tc)) } //---------------------------------------- diff --git a/store/cachekv/cachekvstore_test.go b/store/cache/store_test.go similarity index 91% rename from store/cachekv/cachekvstore_test.go rename to store/cache/store_test.go index 8861dea8779f..6258bbe78c7a 100644 --- a/store/cachekv/cachekvstore_test.go +++ b/store/cache/store_test.go @@ -1,4 +1,4 @@ -package cachekv +package cache_test import ( "fmt" @@ -7,19 +7,24 @@ import ( "github.com/stretchr/testify/require" cmn "github.com/tendermint/tendermint/libs/common" dbm "github.com/tendermint/tendermint/libs/db" + + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/cosmos-sdk/store/cachekv" + "github.com/cosmos/cosmos-sdk/store/dbadapter" ) -func newCacheKVStore() CacheKVStore { - mem := dbStoreAdapter{dbm.NewMemDB()} - return NewCacheKVStore(mem) +func newCacheKVStore() sdk.CacheKVStore { + mem := dbadapter.NewStore(dbm.NewMemDB()) + return cachekv.NewStore(mem) } func keyFmt(i int) []byte { return bz(fmt.Sprintf("key%0.8d", i)) } func valFmt(i int) []byte { return bz(fmt.Sprintf("value%0.8d", i)) } func TestCacheKVStore(t *testing.T) { - mem := dbStoreAdapter{dbm.NewMemDB()} - st := NewCacheKVStore(mem) + mem := dbadapter.NewStore(dbm.NewMemDB()) + st := cachekv.NewStore(mem) require.Empty(t, st.Get(keyFmt(1)), "Expected `key1` to be empty") @@ -45,11 +50,11 @@ func TestCacheKVStore(t *testing.T) { require.Equal(t, valFmt(2), st.Get(keyFmt(1))) // make a new one, check it - st = NewCacheKVStore(mem) + st = cachekv.NewStore(mem) require.Equal(t, valFmt(2), st.Get(keyFmt(1))) // make a new one and delete - should not be removed from mem - st = NewCacheKVStore(mem) + st = cachekv.NewStore(mem) st.Delete(keyFmt(1)) require.Empty(t, st.Get(keyFmt(1))) require.Equal(t, mem.Get(keyFmt(1)), valFmt(2)) @@ -61,8 +66,8 @@ func TestCacheKVStore(t *testing.T) { } func TestCacheKVStoreNested(t *testing.T) { - mem := dbStoreAdapter{dbm.NewMemDB()} - st := NewCacheKVStore(mem) + mem := dbadapter.NewStore(dbm.NewMemDB()) + st := cachekv.NewStore(mem) // set. check its there on st and not on mem. st.Set(keyFmt(1), valFmt(1)) @@ -70,7 +75,7 @@ func TestCacheKVStoreNested(t *testing.T) { require.Equal(t, valFmt(1), st.Get(keyFmt(1))) // make a new from st and check - st2 := NewCacheKVStore(st) + st2 := cachekv.NewStore(st) require.Equal(t, valFmt(1), st2.Get(keyFmt(1))) // update the value on st2, check it only effects st2 @@ -312,7 +317,7 @@ func randInt(n int) int { } // useful for replaying a error case if we find one -func doOp(st CacheKVStore, truth dbm.DB, op int, args ...int) { +func doOp(st sdk.CacheKVStore, truth dbm.DB, op int, args ...int) { switch op { case opSet: k := args[0] @@ -335,7 +340,7 @@ func doOp(st CacheKVStore, truth dbm.DB, op int, args ...int) { } } -func doRandomOp(st CacheKVStore, truth dbm.DB, maxKey int) { +func doRandomOp(st sdk.CacheKVStore, truth dbm.DB, maxKey int) { r := randInt(totalOps) switch r { case opSet: @@ -362,7 +367,7 @@ func doRandomOp(st CacheKVStore, truth dbm.DB, maxKey int) { //------------------------------------------------------------------------------------------- // iterate over whole domain -func assertIterateDomain(t *testing.T, st KVStore, expectedN int) { +func assertIterateDomain(t *testing.T, st sdk.KVStore, expectedN int) { itr := st.Iterator(nil, nil) var i = 0 for ; itr.Valid(); itr.Next() { @@ -374,7 +379,7 @@ func assertIterateDomain(t *testing.T, st KVStore, expectedN int) { require.Equal(t, expectedN, i) } -func assertIterateDomainCheck(t *testing.T, st KVStore, mem dbm.DB, r []keyRange) { +func assertIterateDomainCheck(t *testing.T, st sdk.KVStore, mem dbm.DB, r []keyRange) { // iterate over each and check they match the other itr := st.Iterator(nil, nil) itr2 := mem.Iterator(nil, nil) // ground truth @@ -404,7 +409,7 @@ func assertIterateDomainCheck(t *testing.T, st KVStore, mem dbm.DB, r []keyRange require.False(t, itr2.Valid()) } -func assertIterateDomainCompare(t *testing.T, st KVStore, mem dbm.DB) { +func assertIterateDomainCompare(t *testing.T, st sdk.KVStore, mem dbm.DB) { // iterate over each and check they match the other itr := st.Iterator(nil, nil) itr2 := mem.Iterator(nil, nil) // ground truth @@ -412,7 +417,7 @@ func assertIterateDomainCompare(t *testing.T, st KVStore, mem dbm.DB) { checkIterators(t, itr2, itr) } -func checkIterators(t *testing.T, itr, itr2 Iterator) { +func checkIterators(t *testing.T, itr, itr2 sdk.Iterator) { for ; itr.Valid(); itr.Next() { require.True(t, itr2.Valid()) k, v := itr.Key(), itr.Value() @@ -427,14 +432,14 @@ func checkIterators(t *testing.T, itr, itr2 Iterator) { //-------------------------------------------------------- -func setRange(st KVStore, mem dbm.DB, start, end int) { +func setRange(st sdk.KVStore, mem dbm.DB, start, end int) { for i := start; i < end; i++ { st.Set(keyFmt(i), valFmt(i)) mem.Set(keyFmt(i), valFmt(i)) } } -func deleteRange(st KVStore, mem dbm.DB, start, end int) { +func deleteRange(st sdk.KVStore, mem dbm.DB, start, end int) { for i := start; i < end; i++ { st.Delete(keyFmt(i)) mem.Delete(keyFmt(i)) diff --git a/store/codec.go b/store/codec.go deleted file mode 100644 index 353cd2e3cd77..000000000000 --- a/store/codec.go +++ /dev/null @@ -1,32 +0,0 @@ -package store - -import ( - "github.com/cosmos/cosmos-sdk/types" -) - -// Import cosmos-sdk/types/store.go for convenience. -// nolint -type ( - PruningStrategy = types.PruningStrategy - Store = types.Store - Committer = types.Committer - CommitStore = types.CommitStore - MultiStore = types.MultiStore - CacheMultiStore = types.CacheMultiStore - CommitMultiStore = types.CommitMultiStore - KVStore = types.KVStore - KVPair = types.KVPair - Iterator = types.Iterator - CacheKVStore = types.CacheKVStore - CommitKVStore = types.CommitKVStore - CacheWrapper = types.CacheWrapper - CacheWrap = types.CacheWrap - CommitID = types.CommitID - StoreKey = types.StoreKey - StoreType = types.StoreType - Queryable = types.Queryable - TraceContext = types.TraceContext - Gas = types.Gas - GasMeter = types.GasMeter - GasConfig = types.GasConfig -) diff --git a/store/dbadapter/store.go b/store/dbadapter/store.go new file mode 100644 index 000000000000..af848aae93a3 --- /dev/null +++ b/store/dbadapter/store.go @@ -0,0 +1,33 @@ +package dbadapter + +import ( + "io" + + sdk "github.com/cosmos/cosmos-sdk/types" + dbm "github.com/tendermint/tendermint/libs/db" + + "github.com/cosmos/cosmos-sdk/store/cache" + "github.com/cosmos/cosmos-sdk/store/trace" +) + +// Wrapper type for dbm.Db with implementation of KVStore +type Store struct { + dbm.DB +} + +func NewStore(parent dbm.DB) Store { + return Store{parent} +} + +// Implements KVStore. +func (dsa Store) CacheWrap() sdk.CacheWrap { + return cache.NewStore(dsa) +} + +// CacheWrapWithTrace implements the KVStore interface. +func (dsa Store) CacheWrapWithTrace(w io.Writer, tc sdk.TraceContext) sdk.CacheWrap { + return cache.NewStore(trace.NewStore(dsa, w, tc)) +} + +// dbm.DB implements KVStore so we can CacheKVStore it. +var _ sdk.KVStore = Store{} diff --git a/store/dbstoreadapter.go b/store/dbstoreadapter.go deleted file mode 100644 index 739e30596b05..000000000000 --- a/store/dbstoreadapter.go +++ /dev/null @@ -1,41 +0,0 @@ -package store - -import ( - "io" - - sdk "github.com/cosmos/cosmos-sdk/types" - dbm "github.com/tendermint/tendermint/libs/db" -) - -// Wrapper type for dbm.Db with implementation of KVStore -type dbStoreAdapter struct { - dbm.DB -} - -// Implements Store. -func (dbStoreAdapter) GetStoreType() StoreType { - return sdk.StoreTypeDB -} - -// Implements KVStore. -func (dsa dbStoreAdapter) CacheWrap() CacheWrap { - return NewCacheKVStore(dsa) -} - -// CacheWrapWithTrace implements the KVStore interface. -func (dsa dbStoreAdapter) CacheWrapWithTrace(w io.Writer, tc TraceContext) CacheWrap { - return NewCacheKVStore(NewTraceKVStore(dsa, w, tc)) -} - -// Implements KVStore -func (dsa dbStoreAdapter) Prefix(prefix []byte) KVStore { - return prefixStore{dsa, prefix} -} - -// Implements KVStore -func (dsa dbStoreAdapter) Gas(meter GasMeter, config GasConfig) KVStore { - return NewGasKVStore(meter, config, dsa) -} - -// dbm.DB implements KVStore so we can CacheKVStore it. -var _ KVStore = dbStoreAdapter{} diff --git a/store/gaskv/gaskvstore.go b/store/gas/store.go similarity index 55% rename from store/gaskv/gaskvstore.go rename to store/gas/store.go index 2ce6415e666b..db4c5a1936b9 100644 --- a/store/gaskv/gaskvstore.go +++ b/store/gas/store.go @@ -1,4 +1,4 @@ -package store +package gas import ( "io" @@ -6,7 +6,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -var _ KVStore = &gasKVStore{} +var _ sdk.KVStore = &gasKVStore{} // gasKVStore applies gas tracking to an underlying KVStore. It implements the // KVStore interface. @@ -27,80 +27,53 @@ func NewGasKVStore(gasMeter sdk.GasMeter, gasConfig sdk.GasConfig, parent sdk.KV return kvs } -// Implements Store. -func (gs *gasKVStore) GetStoreType() sdk.StoreType { - return gs.parent.GetStoreType() -} - -// Implements KVStore. -func (gs *gasKVStore) Get(key []byte) (value []byte) { - gs.gasMeter.ConsumeGas(gs.gasConfig.ReadCostFlat, sdk.GasReadCostFlatDesc) - value = gs.parent.Get(key) - +// Implements sdk.KVStore. +func (gi *gasKVStore) Get(key []byte) (value []byte) { + gi.gasMeter.ConsumeGas(gi.gasConfig.ReadCostFlat, sdk.GasReadCostFlatDesc) + value = gi.parent.Get(key) // TODO overflow-safe math? - gs.gasMeter.ConsumeGas(gs.gasConfig.ReadCostPerByte*sdk.Gas(len(value)), sdk.GasReadPerByteDesc) + gi.gasMeter.ConsumeGas(gi.gasConfig.ReadCostPerByte*sdk.Gas(len(value)), sdk.GasReadPerByteDesc) return value } -// Implements KVStore. -func (gs *gasKVStore) Set(key []byte, value []byte) { - gs.gasMeter.ConsumeGas(gs.gasConfig.WriteCostFlat, sdk.GasWriteCostFlatDesc) +// Implements sdk.KVStore. +func (gi *gasKVStore) Set(key []byte, value []byte) { + gi.gasMeter.ConsumeGas(gi.gasConfig.WriteCostFlat, sdk.GasWriteCostFlatDesc) // TODO overflow-safe math? - gs.gasMeter.ConsumeGas(gs.gasConfig.WriteCostPerByte*sdk.Gas(len(value)), sdk.GasWritePerByteDesc) - gs.parent.Set(key, value) -} - -// Implements KVStore. -func (gs *gasKVStore) Has(key []byte) bool { - gs.gasMeter.ConsumeGas(gs.gasConfig.HasCost, sdk.GasHasDesc) - return gs.parent.Has(key) -} - -// Implements KVStore. -func (gs *gasKVStore) Delete(key []byte) { - // charge gas to prevent certain attack vectors even though space is being freed - gs.gasMeter.ConsumeGas(gs.gasConfig.DeleteCost, sdk.GasDeleteDesc) - gs.parent.Delete(key) + gi.gasMeter.ConsumeGas(gi.gasConfig.WriteCostPerByte*sdk.Gas(len(value)), sdk.GasWritePerByteDesc) + gi.parent.Set(key, value) } -// Implements KVStore -func (gs *gasKVStore) Prefix(prefix []byte) KVStore { - // Keep gasstore layer at the top - return &gasKVStore{ - gasMeter: gs.gasMeter, - gasConfig: gs.gasConfig, - parent: prefixStore{gs.parent, prefix}, - } +// Implements sdk.KVStore. +func (gi *gasKVStore) Has(key []byte) bool { + gi.gasMeter.ConsumeGas(gi.gasConfig.HasCost, sdk.GasHasDesc) + return gi.parent.Has(key) } -// Implements KVStore -func (gs *gasKVStore) Gas(meter GasMeter, config GasConfig) KVStore { - return NewGasKVStore(meter, config, gs) +// Implements sdk.KVStore. +func (gi *gasKVStore) Delete(key []byte) { + gi.gasMeter.ConsumeGas(gi.gasConfig.DeleteCost, sdk.GasDeleteDesc) + gi.parent.Delete(key) } -// Iterator implements the KVStore interface. It returns an iterator which -// incurs a flat gas cost for seeking to the first key/value pair and a variable -// gas cost based on the current value's length if the iterator is valid. -func (gs *gasKVStore) Iterator(start, end []byte) sdk.Iterator { - return gs.iterator(start, end, true) +// Implements sdk.KVStore. +func (gi *gasKVStore) Iterator(start, end []byte) sdk.Iterator { + return gi.iterator(start, end, true) } -// ReverseIterator implements the KVStore interface. It returns a reverse -// iterator which incurs a flat gas cost for seeking to the first key/value pair -// and a variable gas cost based on the current value's length if the iterator -// is valid. -func (gs *gasKVStore) ReverseIterator(start, end []byte) sdk.Iterator { - return gs.iterator(start, end, false) +// Implements sdk.KVStore. +func (gi *gasKVStore) ReverseIterator(start, end []byte) sdk.Iterator { + return gi.iterator(start, end, false) } -// Implements KVStore. -func (gs *gasKVStore) CacheWrap() sdk.CacheWrap { +// Implements sdk.KVStore. +func (gi *gasKVStore) CacheWrap() sdk.CacheWrap { panic("cannot CacheWrap a GasKVStore") } -// CacheWrapWithTrace implements the KVStore interface. -func (gs *gasKVStore) CacheWrapWithTrace(_ io.Writer, _ TraceContext) CacheWrap { +// CacheWrapWithTrace implements the sdk.KVStore interface. +func (gi *gasKVStore) CacheWrapWithTrace(_ io.Writer, _ sdk.TraceContext) sdk.CacheWrap { panic("cannot CacheWrapWithTrace a GasKVStore") } diff --git a/store/gaskv/gaskvstore_test.go b/store/gas/store_test.go similarity index 96% rename from store/gaskv/gaskvstore_test.go rename to store/gas/store_test.go index ba77bae1f889..aa6ada4c9cde 100644 --- a/store/gaskv/gaskvstore_test.go +++ b/store/gas/store_test.go @@ -1,4 +1,4 @@ -package store +package gas import ( "testing" @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/require" ) -func newGasKVStore() KVStore { +func newGasKVStore() sdk.KVStore { meter := sdk.NewGasMeter(1000) mem := dbStoreAdapter{dbm.NewMemDB()} return NewGasKVStore(meter, sdk.KVGasConfig(), mem) @@ -69,7 +69,7 @@ func TestGasKVStoreOutOfGasIterator(t *testing.T) { require.Panics(t, func() { iterator.Value() }, "Expected out-of-gas") } -func testGasKVStoreWrap(t *testing.T, store KVStore) { +func testGasKVStoreWrap(t *testing.T, store sdk.KVStore) { meter := sdk.NewGasMeter(10000) store = store.Gas(meter, sdk.GasConfig{HasCost: 10}) diff --git a/store/iavl/iavlstore.go b/store/iavl/store.go similarity index 86% rename from store/iavl/iavlstore.go rename to store/iavl/store.go index d535fd436c5b..8f1617dfa50b 100644 --- a/store/iavl/iavlstore.go +++ b/store/iavl/store.go @@ -1,4 +1,4 @@ -package store +package iavl import ( "fmt" @@ -12,6 +12,9 @@ import ( dbm "github.com/tendermint/tendermint/libs/db" sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/cosmos-sdk/store/cache" + "github.com/cosmos/cosmos-sdk/store/trace" ) const ( @@ -19,7 +22,7 @@ const ( ) // load the iavl store -func LoadIAVLStore(db dbm.DB, id CommitID, pruning sdk.PruningStrategy) (CommitStore, error) { +func LoadIAVLStore(db dbm.DB, id sdk.CommitID, pruning sdk.PruningStrategy) (sdk.CommitStore, error) { tree := iavl.NewMutableTree(db, defaultIAVLCacheSize) _, err := tree.LoadVersion(id.Version) if err != nil { @@ -32,11 +35,11 @@ func LoadIAVLStore(db dbm.DB, id CommitID, pruning sdk.PruningStrategy) (CommitS //---------------------------------------- -var _ KVStore = (*iavlStore)(nil) -var _ CommitStore = (*iavlStore)(nil) -var _ Queryable = (*iavlStore)(nil) +var _ sdk.KVStore = (*iavlStore)(nil) +var _ sdk.CommitStore = (*iavlStore)(nil) +var _ sdk.Queryable = (*iavlStore)(nil) -// iavlStore Implements KVStore and CommitStore. +// iavlStore Implements sdk.KVStore and sdk.CommitStore. type iavlStore struct { // The underlying tree. @@ -67,7 +70,7 @@ func newIAVLStore(tree *iavl.MutableTree, numRecent int64, storeEvery int64) *ia } // Implements Committer. -func (st *iavlStore) Commit() CommitID { +func (st *iavlStore) Commit() sdk.CommitID { // Save a new version. hash, version, err := st.tree.SaveVersion() if err != nil { @@ -87,15 +90,15 @@ func (st *iavlStore) Commit() CommitID { } } - return CommitID{ + return sdk.CommitID{ Version: version, Hash: hash, } } // Implements Committer. -func (st *iavlStore) LastCommitID() CommitID { - return CommitID{ +func (st *iavlStore) LastCommitID() sdk.CommitID { + return sdk.CommitID{ Version: st.tree.Version(), Hash: st.tree.Hash(), } @@ -121,58 +124,43 @@ func (st *iavlStore) VersionExists(version int64) bool { } // Implements Store. -func (st *iavlStore) GetStoreType() StoreType { - return sdk.StoreTypeIAVL -} - -// Implements Store. -func (st *iavlStore) CacheWrap() CacheWrap { - return NewCacheKVStore(st) +func (st *iavlStore) CacheWrap() sdk.CacheWrap { + return cache.NewStore(st) } // CacheWrapWithTrace implements the Store interface. -func (st *iavlStore) CacheWrapWithTrace(w io.Writer, tc TraceContext) CacheWrap { - return NewCacheKVStore(NewTraceKVStore(st, w, tc)) +func (st *iavlStore) CacheWrapWithTrace(w io.Writer, tc sdk.TraceContext) sdk.CacheWrap { + return cache.NewStore(trace.NewStore(st, w, tc)) } -// Implements KVStore. +// Implements sdk.KVStore. func (st *iavlStore) Set(key, value []byte) { st.tree.Set(key, value) } -// Implements KVStore. +// Implements sdk.KVStore. func (st *iavlStore) Get(key []byte) (value []byte) { _, v := st.tree.Get(key) return v } -// Implements KVStore. +// Implements sdk.KVStore. func (st *iavlStore) Has(key []byte) (exists bool) { return st.tree.Has(key) } -// Implements KVStore. +// Implements sdk.KVStore. func (st *iavlStore) Delete(key []byte) { st.tree.Remove(key) } -// Implements KVStore -func (st *iavlStore) Prefix(prefix []byte) KVStore { - return prefixStore{st, prefix} -} - -// Implements KVStore -func (st *iavlStore) Gas(meter GasMeter, config GasConfig) KVStore { - return NewGasKVStore(meter, config, st) -} - -// Implements KVStore. -func (st *iavlStore) Iterator(start, end []byte) Iterator { +// Implements sdk.KVStore. +func (st *iavlStore) Iterator(start, end []byte) sdk.Iterator { return newIAVLIterator(st.tree.ImmutableTree, start, end, true) } -// Implements KVStore. -func (st *iavlStore) ReverseIterator(start, end []byte) Iterator { +// Implements sdk.KVStore. +func (st *iavlStore) ReverseIterator(start, end []byte) sdk.Iterator { return newIAVLIterator(st.tree.ImmutableTree, start, end, false) } @@ -237,10 +225,10 @@ func (st *iavlStore) Query(req abci.RequestQuery) (res abci.ResponseQuery) { case "/subspace": subspace := req.Data res.Key = subspace - var KVs []KVPair + var KVs []sdk.KVPair iterator := sdk.KVStorePrefixIterator(st, subspace) for ; iterator.Valid(); iterator.Next() { - KVs = append(KVs, KVPair{Key: iterator.Key(), Value: iterator.Value()}) + KVs = append(KVs, sdk.KVPair{Key: iterator.Key(), Value: iterator.Value()}) } iterator.Close() res.Value = cdc.MustMarshalBinary(KVs) @@ -282,7 +270,7 @@ type iavlIterator struct { value []byte // The current value } -var _ Iterator = (*iavlIterator)(nil) +var _ sdk.Iterator = (*iavlIterator)(nil) // newIAVLIterator will create a new iavlIterator. // CONTRACT: Caller must release the iavlIterator, as each one creates a new diff --git a/store/iavl/iavlstore_test.go b/store/iavl/store_test.go similarity index 98% rename from store/iavl/iavlstore_test.go rename to store/iavl/store_test.go index 1e9263b7bbcf..2968c0c435bb 100644 --- a/store/iavl/iavlstore_test.go +++ b/store/iavl/store_test.go @@ -1,4 +1,4 @@ -package store +package iavl import ( "fmt" @@ -29,7 +29,7 @@ var ( ) // make a tree and save it -func newTree(t *testing.T, db dbm.DB) (*iavl.MutableTree, CommitID) { +func newTree(t *testing.T, db dbm.DB) (*iavl.MutableTree, sdk.CommitID) { tree := iavl.NewMutableTree(db, cacheSize) for k, v := range treeData { tree.Set([]byte(k), []byte(v)) @@ -41,7 +41,7 @@ func newTree(t *testing.T, db dbm.DB) (*iavl.MutableTree, CommitID) { } hash, ver, err := tree.SaveVersion() require.Nil(t, err) - return tree, CommitID{ver, hash} + return tree, sdk.CommitID{ver, hash} } func TestIAVLStoreGetSetHasDelete(t *testing.T) { @@ -385,12 +385,12 @@ func TestIAVLStoreQuery(t *testing.T) { v3 := []byte("val3") ksub := []byte("key") - KVs0 := []KVPair{} - KVs1 := []KVPair{ + KVs0 := []sdk.KVPair{} + KVs1 := []sdk.KVPair{ {Key: k1, Value: v1}, {Key: k2, Value: v2}, } - KVs2 := []KVPair{ + KVs2 := []sdk.KVPair{ {Key: k1, Value: v3}, {Key: k2, Value: v2}, } @@ -475,7 +475,7 @@ func BenchmarkIAVLIteratorNext(b *testing.B) { tree.Set(key, value) } iavlStore := newIAVLStore(tree, numRecent, storeEvery) - iterators := make([]Iterator, b.N/treeSize) + iterators := make([]sdk.Iterator, b.N/treeSize) for i := 0; i < len(iterators); i++ { iterators[i] = iavlStore.Iterator([]byte{0}, []byte{255, 255, 255, 255, 255}) } diff --git a/store/wire.go b/store/iavl/wire.go similarity index 83% rename from store/wire.go rename to store/iavl/wire.go index 5724c8e54800..43173f3e7895 100644 --- a/store/wire.go +++ b/store/iavl/wire.go @@ -1,4 +1,4 @@ -package store +package iavl import ( "github.com/cosmos/cosmos-sdk/codec" diff --git a/store/prefix/prefixstore.go b/store/prefix/store.go similarity index 82% rename from store/prefix/prefixstore.go rename to store/prefix/store.go index f08d4200260d..3902cade8f69 100644 --- a/store/prefix/prefixstore.go +++ b/store/prefix/store.go @@ -6,8 +6,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/store/cachekv" - "github.com/cosmos/cosmos-sdk/store/tracekv" + "github.com/cosmos/cosmos-sdk/store/cache" + "github.com/cosmos/cosmos-sdk/store/trace" ) var _ sdk.KVStore = Store{} @@ -16,7 +16,7 @@ var _ sdk.KVStore = Store{} // both gives access only to the limited subset of the store // for convinience or safety type Store struct { - parent KVStore + parent sdk.KVStore prefix []byte } @@ -27,7 +27,7 @@ func cloneAppend(bz []byte, tail []byte) (res []byte) { return } -func (s prefixStore) key(key []byte) (res []byte) { +func (s Store) key(key []byte) (res []byte) { if key == nil { panic("nil key on prefixStore") } @@ -35,19 +35,14 @@ func (s prefixStore) key(key []byte) (res []byte) { return } -// Implements Store -func (s Store) GetStoreType() sdk.StoreType { - return s.parent.GetStoreType() -} - // Implements CacheWrap func (s Store) CacheWrap() sdk.CacheWrap { - return cachekv.NewStore(s) + return cache.NewStore(s) } // CacheWrapWithTrace implements the KVStore interface. func (s Store) CacheWrapWithTrace(w io.Writer, tc sdk.TraceContext) sdk.CacheWrap { - return cachekv.NewStore(tracekv.NewStore(s, w, tc)) + return cache.NewStore(trace.NewStore(s, w, tc)) } // Implements KVStore @@ -70,19 +65,9 @@ func (s Store) Delete(key []byte) { s.parent.Delete(s.key(key)) } -// Implements KVStore -func (s Store) Prefix(prefix []byte) sdk.KVStore { - return Store{s, prefix} -} - -// Implements KVStore -func (s Store) Gas(meter sdk.GasMeter, config sdk.GasConfig) sdk.KVStore { - return NewGasKVStore(meter, config, s) -} - // Implements KVStore // Check https://github.com/tendermint/tendermint/blob/master/libs/db/prefix_db.go#L106 -func (s Store) Iterator(start, end []byte) Iterator { +func (s Store) Iterator(start, end []byte) sdk.Iterator { newstart := cloneAppend(s.prefix, start) var newend []byte @@ -99,7 +84,7 @@ func (s Store) Iterator(start, end []byte) Iterator { // Implements KVStore // Check https://github.com/tendermint/tendermint/blob/master/libs/db/prefix_db.go#L129 -func (s Store) ReverseIterator(start, end []byte) Iterator { +func (s Store) ReverseIterator(start, end []byte) sdk.Iterator { var newstart []byte if start == nil { newstart = cpIncr(s.prefix) @@ -127,11 +112,11 @@ var _ sdk.Iterator = (*prefixIterator)(nil) type prefixIterator struct { prefix []byte start, end []byte - iter Iterator + iter sdk.Iterator valid bool } -func newPrefixIterator(prefix, start, end []byte, parent Iterator) *prefixIterator { +func newPrefixIterator(prefix, start, end []byte, parent sdk.Iterator) *prefixIterator { return &prefixIterator{ prefix: prefix, start: start, @@ -218,7 +203,7 @@ func cpDecr(bz []byte) (ret []byte) { return nil } -func skipOne(iter Iterator, skipKey []byte) { +func skipOne(iter sdk.Iterator, skipKey []byte) { if iter.Valid() { if bytes.Equal(iter.Key(), skipKey) { iter.Next() diff --git a/store/prefix/prefixstore_test.go b/store/prefix/store_test.go similarity index 100% rename from store/prefix/prefixstore_test.go rename to store/prefix/store_test.go diff --git a/store/rootmulti/multistoreproof.go b/store/rootmulti/proof.go similarity index 99% rename from store/rootmulti/multistoreproof.go rename to store/rootmulti/proof.go index d62bc4aca509..bf8a1a6325f2 100644 --- a/store/rootmulti/multistoreproof.go +++ b/store/rootmulti/proof.go @@ -1,4 +1,4 @@ -package store +package rootmulti import ( "bytes" diff --git a/store/rootmulti/multistoreproof_test.go b/store/rootmulti/proof_test.go similarity index 92% rename from store/rootmulti/multistoreproof_test.go rename to store/rootmulti/proof_test.go index 45a102cd3ba4..318cd653884f 100644 --- a/store/rootmulti/multistoreproof_test.go +++ b/store/rootmulti/proof_test.go @@ -1,4 +1,4 @@ -package store +package rootmulti import ( "encoding/hex" @@ -9,6 +9,8 @@ import ( "github.com/tendermint/iavl" cmn "github.com/tendermint/tendermint/libs/common" "github.com/tendermint/tendermint/libs/db" + + sdk "github.com/cosmos/cosmos-sdk/types" ) func TestVerifyMultiStoreCommitInfo(t *testing.T) { @@ -23,7 +25,7 @@ func TestVerifyMultiStoreCommitInfo(t *testing.T) { storeInfos = append(storeInfos, storeInfo{ Name: "gov", Core: storeCore{ - CommitID: CommitID{ + CommitID: sdk.CommitID{ Version: 689, Hash: gocRootHash, }, @@ -33,7 +35,7 @@ func TestVerifyMultiStoreCommitInfo(t *testing.T) { storeInfos = append(storeInfos, storeInfo{ Name: "main", Core: storeCore{ - CommitID: CommitID{ + CommitID: sdk.CommitID{ Version: 689, Hash: nil, }, @@ -44,7 +46,7 @@ func TestVerifyMultiStoreCommitInfo(t *testing.T) { storeInfos = append(storeInfos, storeInfo{ Name: "acc", Core: storeCore{ - CommitID: CommitID{ + CommitID: sdk.CommitID{ Version: 689, Hash: accRootHash, }, @@ -54,7 +56,7 @@ func TestVerifyMultiStoreCommitInfo(t *testing.T) { storeInfos = append(storeInfos, storeInfo{ Name: "ibc", Core: storeCore{ - CommitID: CommitID{ + CommitID: sdk.CommitID{ Version: 689, Hash: nil, }, @@ -65,7 +67,7 @@ func TestVerifyMultiStoreCommitInfo(t *testing.T) { storeInfos = append(storeInfos, storeInfo{ Name: "stake", Core: storeCore{ - CommitID: CommitID{ + CommitID: sdk.CommitID{ Version: 689, Hash: stakeRootHash, }, @@ -76,7 +78,7 @@ func TestVerifyMultiStoreCommitInfo(t *testing.T) { storeInfos = append(storeInfos, storeInfo{ Name: "slashing", Core: storeCore{ - CommitID: CommitID{ + CommitID: sdk.CommitID{ Version: 689, Hash: slashingRootHash, }, diff --git a/store/rootmulti/rootmultistore.go b/store/rootmulti/store.go similarity index 72% rename from store/rootmulti/rootmultistore.go rename to store/rootmulti/store.go index 7de465f7703e..ea97946b2f6c 100644 --- a/store/rootmulti/rootmultistore.go +++ b/store/rootmulti/store.go @@ -1,4 +1,4 @@ -package store +package rootmulti import ( "fmt" @@ -11,6 +11,9 @@ import ( dbm "github.com/tendermint/tendermint/libs/db" sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/cosmos-sdk/store/trace" + "github.com/cosmos/cosmos-sdk/store/transient" ) const ( @@ -18,31 +21,31 @@ const ( commitInfoKeyFmt = "s/%d" // s/ ) -// rootMultiStore is composed of many CommitStores. Name contrasts with -// cacheMultiStore which is for cache-wrapping other MultiStores. It implements +// rootMultiStore is composed of many sdk.CommitStores. Name contrasts with +// cacheMultiStore which is for cache-wrapping other sdk.MultiStores. It implements // the CommitMultiStore interface. type rootMultiStore struct { db dbm.DB - lastCommitID CommitID + lastCommitID sdk.CommitID pruning sdk.PruningStrategy - storesParams map[StoreKey]storeParams - stores map[StoreKey]CommitStore - keysByName map[string]StoreKey + storesParams map[sdk.StoreKey]storeParams + stores map[sdk.StoreKey]sdk.MountableStore + keysByName map[string]sdk.StoreKey traceWriter io.Writer - traceContext TraceContext + traceContext sdk.TraceContext } -var _ CommitMultiStore = (*rootMultiStore)(nil) -var _ Queryable = (*rootMultiStore)(nil) +var _ sdk.CommitMultiStore = (*rootMultiStore)(nil) +var _ sdk.Queryable = (*rootMultiStore)(nil) // nolint func NewCommitMultiStore(db dbm.DB) *rootMultiStore { return &rootMultiStore{ db: db, - storesParams: make(map[StoreKey]storeParams), - stores: make(map[StoreKey]CommitStore), - keysByName: make(map[string]StoreKey), + storesParams: make(map[sdk.StoreKey]storeParams), + stores: make(map[sdk.StoreKey]sdk.MountableStore), + keysByName: make(map[string]sdk.StoreKey), } } @@ -54,13 +57,8 @@ func (rs *rootMultiStore) SetPruning(pruning sdk.PruningStrategy) { } } -// Implements Store. -func (rs *rootMultiStore) GetStoreType() StoreType { - return sdk.StoreTypeMulti -} - // Implements CommitMultiStore. -func (rs *rootMultiStore) MountStoreWithDB(key StoreKey, typ StoreType, db dbm.DB) { +func (rs *rootMultiStore) MountStoreWithDB(key sdk.StoreKey, db dbm.DB) { if key == nil { panic("MountIAVLStore() key cannot be nil") } @@ -72,20 +70,19 @@ func (rs *rootMultiStore) MountStoreWithDB(key StoreKey, typ StoreType, db dbm.D } rs.storesParams[key] = storeParams{ key: key, - typ: typ, db: db, } rs.keysByName[key.Name()] = key } // Implements CommitMultiStore. -func (rs *rootMultiStore) GetCommitStore(key StoreKey) CommitStore { +func (rs *rootMultiStore) GetCommitStore(key sdk.StoreKey) sdk.CommitStore { return rs.stores[key] } // Implements CommitMultiStore. -func (rs *rootMultiStore) GetCommitKVStore(key StoreKey) CommitKVStore { - return rs.stores[key].(CommitKVStore) +func (rs *rootMultiStore) GetCommitKVStore(key sdk.StoreKey) sdk.CommitKVStore { + return rs.stores[key].(sdk.CommitKVStore) } // Implements CommitMultiStore. @@ -100,7 +97,7 @@ func (rs *rootMultiStore) LoadVersion(ver int64) error { // Special logic for version 0 if ver == 0 { for key, storeParams := range rs.storesParams { - id := CommitID{} + id := sdk.CommitID{} store, err := rs.loadCommitStoreFromParams(key, id, storeParams) if err != nil { return fmt.Errorf("failed to load rootMultiStore: %v", err) @@ -108,7 +105,7 @@ func (rs *rootMultiStore) LoadVersion(ver int64) error { rs.stores[key] = store } - rs.lastCommitID = CommitID{} + rs.lastCommitID = sdk.CommitID{} return nil } // Otherwise, version is 1 or greater @@ -120,15 +117,15 @@ func (rs *rootMultiStore) LoadVersion(ver int64) error { } // Convert StoreInfos slice to map - infos := make(map[StoreKey]storeInfo) + infos := make(map[sdk.StoreKey]storeInfo) for _, storeInfo := range cInfo.StoreInfos { infos[rs.nameToKey(storeInfo.Name)] = storeInfo } // Load each Store - var newStores = make(map[StoreKey]CommitStore) + var newStores = make(map[sdk.StoreKey]sdk.CommitStore) for key, storeParams := range rs.storesParams { - var id CommitID + var id sdk.CommitID info, ok := infos[key] if ok { id = info.Core.CommitID @@ -147,18 +144,18 @@ func (rs *rootMultiStore) LoadVersion(ver int64) error { return nil } -// WithTracer sets the tracer for the MultiStore that the underlying -// stores will utilize to trace operations. A MultiStore is returned. -func (rs *rootMultiStore) WithTracer(w io.Writer) MultiStore { +// WithTracer sets the tracer for the sdk.MultiStore that the underlying +// stores will utilize to trace operations. A sdk.MultiStore is returned. +func (rs *rootMultiStore) WithTracer(w io.Writer) sdk.MultiStore { rs.traceWriter = w return rs } -// WithTracingContext updates the tracing context for the MultiStore by merging +// WithTracingContext updates the tracing context for the sdk.MultiStore by merging // the given context with the existing context by key. Any existing keys will // be overwritten. It is implied that the caller should update the context when -// necessary between tracing operations. It returns a modified MultiStore. -func (rs *rootMultiStore) WithTracingContext(tc TraceContext) MultiStore { +// necessary between tracing operations. It returns a modified sdk.MultiStore. +func (rs *rootMultiStore) WithTracingContext(tc sdk.TraceContext) sdk.MultiStore { if rs.traceContext != nil { for k, v := range tc { rs.traceContext[k] = v @@ -170,13 +167,13 @@ func (rs *rootMultiStore) WithTracingContext(tc TraceContext) MultiStore { return rs } -// TracingEnabled returns if tracing is enabled for the MultiStore. +// TracingEnabled returns if tracing is enabled for the sdk.MultiStore. func (rs *rootMultiStore) TracingEnabled() bool { return rs.traceWriter != nil } // ResetTraceContext resets the current tracing context. -func (rs *rootMultiStore) ResetTraceContext() MultiStore { +func (rs *rootMultiStore) ResetTraceContext() sdk.MultiStore { rs.traceContext = nil return rs } @@ -185,12 +182,12 @@ func (rs *rootMultiStore) ResetTraceContext() MultiStore { // +CommitStore // Implements Committer/CommitStore. -func (rs *rootMultiStore) LastCommitID() CommitID { +func (rs *rootMultiStore) LastCommitID() sdk.CommitID { return rs.lastCommitID } // Implements Committer/CommitStore. -func (rs *rootMultiStore) Commit() CommitID { +func (rs *rootMultiStore) Commit() sdk.CommitID { // Commit stores. version := rs.lastCommitID.Version + 1 @@ -203,7 +200,7 @@ func (rs *rootMultiStore) Commit() CommitID { batch.Write() // Prepare for next version. - commitID := CommitID{ + commitID := sdk.CommitID{ Version: version, Hash: commitInfo.Hash(), } @@ -211,50 +208,50 @@ func (rs *rootMultiStore) Commit() CommitID { return commitID } -// Implements CacheWrapper/Store/CommitStore. -func (rs *rootMultiStore) CacheWrap() CacheWrap { - return rs.CacheMultiStore().(CacheWrap) +// Implements sdk.CacheWrapper/Store/CommitStore. +func (rs *rootMultiStore) CacheWrap() sdk.CacheWrap { + return rs.CacheMultiStore().(sdk.CacheWrap) } -// CacheWrapWithTrace implements the CacheWrapper interface. -func (rs *rootMultiStore) CacheWrapWithTrace(_ io.Writer, _ TraceContext) CacheWrap { +// sdk.CacheWrapWithTrace implements the sdk.CacheWrapper interface. +func (rs *rootMultiStore) CacheWrapWithTrace(_ io.Writer, _ sdk.TraceContext) sdk.CacheWrap { return rs.CacheWrap() } //---------------------------------------- // +MultiStore -// Implements MultiStore. -func (rs *rootMultiStore) CacheMultiStore() CacheMultiStore { +// Implements sdk.MultiStore. +func (rs *rootMultiStore) CacheMultiStore() sdk.CacheMultiStore { return newCacheMultiStoreFromRMS(rs) } -// Implements MultiStore. -func (rs *rootMultiStore) GetStore(key StoreKey) Store { +// Implements sdk.MultiStore. +func (rs *rootMultiStore) GetStore(key sdk.StoreKey) sdk.Store { return rs.stores[key] } -// GetKVStore implements the MultiStore interface. If tracing is enabled on the +// GetKVStore implements the sdk.MultiStore interface. If tracing is enabled on the // rootMultiStore, a wrapped TraceKVStore will be returned with the given -// tracer, otherwise, the original KVStore will be returned. -func (rs *rootMultiStore) GetKVStore(key StoreKey) KVStore { - store := rs.stores[key].(KVStore) +// tracer, otherwise, the original sdk.KVStore will be returned. +func (rs *rootMultiStore) GetKVStore(key sdk.StoreKey) sdk.KVStore { + store := rs.stores[key].(sdk.KVStore) if rs.TracingEnabled() { - store = NewTraceKVStore(store, rs.traceWriter, rs.traceContext) + store = trace.NewStore(store, rs.traceWriter, rs.traceContext) } return store } -// Implements MultiStore +// Implements sdk.MultiStore // getStoreByName will first convert the original name to -// a special key, before looking up the CommitStore. +// a special key, before looking up the sdk.CommitStore. // This is not exposed to the extensions (which will need the -// StoreKey), but is useful in main, and particularly app.Query, -// in order to convert human strings into CommitStores. -func (rs *rootMultiStore) getStoreByName(name string) Store { +// sdk.StoreKey), but is useful in main, and particularly app.Query, +// in order to convert human strings into sdk.CommitStores. +func (rs *rootMultiStore) getStoreByName(name string) sdk.Store { key := rs.keysByName[name] if key == nil { return nil @@ -281,7 +278,7 @@ func (rs *rootMultiStore) Query(req abci.RequestQuery) abci.ResponseQuery { msg := fmt.Sprintf("no such store: %s", storeName) return sdk.ErrUnknownRequest(msg).QueryResult() } - queryable, ok := store.(Queryable) + queryable, ok := store.(sdk.Queryable) if !ok { msg := fmt.Sprintf("store %s doesn't support queries", storeName) return sdk.ErrUnknownRequest(msg).QueryResult() @@ -323,7 +320,7 @@ func parsePath(path string) (storeName string, subpath string, err sdk.Error) { //---------------------------------------- -func (rs *rootMultiStore) loadCommitStoreFromParams(key sdk.StoreKey, id CommitID, params storeParams) (store CommitStore, err error) { +func (rs *rootMultiStore) loadCommitStoreFromParams(key sdk.StoreKey, id sdk.CommitID, params storeParams) (store sdk.CommitStore, err error) { var db dbm.DB if params.db != nil { db = dbm.NewPrefixDB(params.db, []byte("s/_/")) @@ -332,28 +329,28 @@ func (rs *rootMultiStore) loadCommitStoreFromParams(key sdk.StoreKey, id CommitI } switch params.typ { case sdk.StoreTypeMulti: - panic("recursive MultiStores not yet supported") + panic("recursive sdk.MultiStores not yet supported") // TODO: id? // return NewCommitMultiStore(db, id) case sdk.StoreTypeIAVL: store, err = LoadIAVLStore(db, id, rs.pruning) return case sdk.StoreTypeDB: - panic("dbm.DB is not a CommitStore") + panic("dbm.DB is not a sdk.CommitStore") case sdk.StoreTypeTransient: _, ok := key.(*sdk.TransientStoreKey) if !ok { - err = fmt.Errorf("invalid StoreKey for StoreTypeTransient: %s", key.String()) + err = fmt.Errorf("invalid sdk.StoreKey for sdk.StoreTypeTransient: %s", key.String()) return } - store = newTransientStore() + store = transient.NewStore() return default: panic(fmt.Sprintf("unrecognized store type %v", params.typ)) } } -func (rs *rootMultiStore) nameToKey(name string) StoreKey { +func (rs *rootMultiStore) nameToKey(name string) sdk.StoreKey { for key := range rs.storesParams { if key.Name() == name { return key @@ -366,9 +363,8 @@ func (rs *rootMultiStore) nameToKey(name string) StoreKey { // storeParams type storeParams struct { - key StoreKey + key sdk.StoreKey db dbm.DB - typ StoreType } //---------------------------------------- @@ -380,7 +376,7 @@ type commitInfo struct { // Version Version int64 - // Store info for + // sdk.Store info for StoreInfos []storeInfo } @@ -394,8 +390,8 @@ func (ci commitInfo) Hash() []byte { return merkle.SimpleHashFromMap(m) } -func (ci commitInfo) CommitID() CommitID { - return CommitID{ +func (ci commitInfo) CommitID() sdk.CommitID { + return sdk.CommitID{ Version: ci.Version, Hash: ci.Hash(), } @@ -413,8 +409,8 @@ type storeInfo struct { } type storeCore struct { - // StoreType StoreType - CommitID CommitID + // sdk.StoreType sdk.StoreType + CommitID sdk.CommitID // ... maybe add more state } @@ -455,7 +451,7 @@ func setLatestVersion(batch dbm.Batch, version int64) { } // Commits each store and returns a new commitInfo. -func commitStores(version int64, storeMap map[StoreKey]CommitStore) commitInfo { +func commitStores(version int64, storeMap map[sdk.StoreKey]sdk.CommitStore) commitInfo { storeInfos := make([]storeInfo, 0, len(storeMap)) for key, store := range storeMap { @@ -466,7 +462,7 @@ func commitStores(version int64, storeMap map[StoreKey]CommitStore) commitInfo { continue } - // Record CommitID + // Record sdk.CommitID si := storeInfo{} si.Name = key.Name() si.Core.CommitID = commitID diff --git a/store/rootmulti/rootmultistore_test.go b/store/rootmulti/store_test.go similarity index 94% rename from store/rootmulti/rootmultistore_test.go rename to store/rootmulti/store_test.go index d6a714c66efa..74b0e39cdccb 100644 --- a/store/rootmulti/rootmultistore_test.go +++ b/store/rootmulti/store_test.go @@ -1,4 +1,4 @@ -package store +package rootmulti import ( "testing" @@ -46,7 +46,7 @@ func TestMultistoreCommitLoad(t *testing.T) { require.Nil(t, err) // New store has empty last commit. - commitID := CommitID{} + commitID := sdk.CommitID{} checkStore(t, store, commitID, commitID) // Make sure we can get stores by name. @@ -137,11 +137,11 @@ func TestMultiStoreQuery(t *testing.T) { require.Nil(t, garbage) // Set and commit data in one store. - store1 := multi.getStoreByName("store1").(KVStore) + store1 := multi.getStoreByName("store1").(sdk.KVStore) store1.Set(k, v) // ... and another. - store2 := multi.getStoreByName("store2").(KVStore) + store2 := multi.getStoreByName("store2").(sdk.KVStore) store2.Set(k2, v2) // Commit the multistore. @@ -201,20 +201,20 @@ func newMultiStoreWithMounts(db dbm.DB) *rootMultiStore { return store } -func checkStore(t *testing.T, store *rootMultiStore, expect, got CommitID) { +func checkStore(t *testing.T, store *rootMultiStore, expect, got sdk.CommitID) { require.Equal(t, expect, got) require.Equal(t, expect, store.LastCommitID()) } -func getExpectedCommitID(store *rootMultiStore, ver int64) CommitID { - return CommitID{ +func getExpectedCommitID(store *rootMultiStore, ver int64) sdk.CommitID { + return sdk.CommitID{ Version: ver, Hash: hashStores(store.stores), } } -func hashStores(stores map[StoreKey]CommitStore) []byte { +func hashStores(stores map[sdk.StoreKey]sdk.CommitStore) []byte { m := make(map[string]merkle.Hasher, len(stores)) for key, store := range stores { name := key.Name() diff --git a/store/rootmulti/wire.go b/store/rootmulti/wire.go new file mode 100644 index 000000000000..615f81c61eb0 --- /dev/null +++ b/store/rootmulti/wire.go @@ -0,0 +1,7 @@ +package rootmulti + +import ( + "github.com/cosmos/cosmos-sdk/codec" +) + +var cdc = codec.NewCodec() diff --git a/store/store.go b/store/store.go index 0068b1d8629f..10cc7ae9de01 100644 --- a/store/store.go +++ b/store/store.go @@ -1,9 +1,9 @@ package store import ( - "github.com/cosmos/cosmos-sdk/store/gaskv" + "github.com/cosmos/cosmos-sdk/store/gas" "github.com/cosmos/cosmos-sdk/store/iavl" "github.com/cosmos/cosmos-sdk/store/prefix" "github.com/cosmos/cosmos-sdk/store/rootmulti" - "github.com/cosmos/cosmos-sdk/store/tracekv" + "github.com/cosmos/cosmos-sdk/store/trace" ) diff --git a/store/tracekv/tracekvstore.go b/store/trace/store.go similarity index 94% rename from store/tracekv/tracekvstore.go rename to store/trace/store.go index 25367424b7c2..6f1b5effb662 100644 --- a/store/tracekv/tracekvstore.go +++ b/store/trace/store.go @@ -1,4 +1,4 @@ -package store +package trace import ( "encoding/base64" @@ -44,7 +44,7 @@ type ( // NewTraceKVStore returns a reference to a new traceKVStore given a parent // KVStore implementation and a buffered writer. -func NewTraceKVStore(parent sdk.KVStore, writer io.Writer, tc sdk.TraceContext) *TraceKVStore { +func NewStore(parent sdk.KVStore, writer io.Writer, tc sdk.TraceContext) *TraceKVStore { return &TraceKVStore{parent: parent, writer: writer, context: tc} } @@ -149,12 +149,6 @@ func (ti *traceIterator) Close() { ti.parent.Close() } -// GetStoreType implements the KVStore interface. It returns the underlying -// KVStore type. -func (tkv *TraceKVStore) GetStoreType() sdk.StoreType { - return tkv.parent.GetStoreType() -} - // CacheWrap implements the KVStore interface. It panics as a TraceKVStore // cannot be cache wrapped. func (tkv *TraceKVStore) CacheWrap() sdk.CacheWrap { diff --git a/store/tracekv/tracekvstore_test.go b/store/trace/store_test.go similarity index 96% rename from store/tracekv/tracekvstore_test.go rename to store/trace/store_test.go index 887fcf96e5c9..f276247ca4e6 100644 --- a/store/tracekv/tracekvstore_test.go +++ b/store/trace/store_test.go @@ -1,4 +1,4 @@ -package store +package trace import ( "bytes" @@ -8,9 +8,11 @@ import ( "github.com/stretchr/testify/require" dbm "github.com/tendermint/tendermint/libs/db" + + sdk "github.com/cosmos/cosmos-sdk/types" ) -var kvPairs = []KVPair{ +var kvPairs = []sdk.KVPair{ {Key: keyFmt(1), Value: valFmt(1)}, {Key: keyFmt(2), Value: valFmt(2)}, {Key: keyFmt(3), Value: valFmt(3)}, @@ -28,7 +30,7 @@ func newTraceKVStore(w io.Writer) *TraceKVStore { func newEmptyTraceKVStore(w io.Writer) *TraceKVStore { memDB := dbStoreAdapter{dbm.NewMemDB()} - tc := TraceContext(map[string]interface{}{"blockHeight": 64}) + tc := sdk.TraceContext(map[string]interface{}{"blockHeight": 64}) return NewTraceKVStore(memDB, w, tc) } @@ -263,13 +265,13 @@ func TestTestTraceKVStoreReverseIterator(t *testing.T) { func TestTraceKVStorePrefix(t *testing.T) { store := newEmptyTraceKVStore(nil) - pStore := store.Prefix([]byte("trace_prefix")) - require.IsType(t, prefixStore{}, pStore) + pStore := prefix.NewStore(store, []byte("trace_prefix")) + require.IsType(t, prefix.Store{}, pStore) } func TestTraceKVStoreGetStoreType(t *testing.T) { memDB := dbStoreAdapter{dbm.NewMemDB()} - store := newEmptyTraceKVStore(nil) + store := NewEmptyStore(nil) require.Equal(t, memDB.GetStoreType(), store.GetStoreType()) } diff --git a/store/transient/transientstore.go b/store/transient/transientstore.go index a3ce8963171d..169d779e3a9e 100644 --- a/store/transient/transientstore.go +++ b/store/transient/transientstore.go @@ -1,49 +1,36 @@ -package store +package transient import ( sdk "github.com/cosmos/cosmos-sdk/types" dbm "github.com/tendermint/tendermint/libs/db" + + "github.com/cosmos/cosmos-sdk/store/dbadapter" ) -var _ KVStore = (*transientStore)(nil) +var _ sdk.KVStore = (*Store)(nil) // transientStore is a wrapper for a MemDB with Commiter implementation -type transientStore struct { - dbStoreAdapter +type Store struct { + dbadapter.Store } // Constructs new MemDB adapter -func newTransientStore() *transientStore { - return &transientStore{dbStoreAdapter{dbm.NewMemDB()}} +func NewStore() *Store { + return &Store{dbadapter.Store{dbm.NewMemDB()}} } // Implements CommitStore // Commit cleans up transientStore. -func (ts *transientStore) Commit() (id CommitID) { - ts.dbStoreAdapter = dbStoreAdapter{dbm.NewMemDB()} +func (ts *Store) Commit() (id sdk.CommitID) { + ts.Store = dbadapter.Store{dbm.NewMemDB()} return } // Implements CommitStore -func (ts *transientStore) SetPruning(pruning PruningStrategy) { +func (ts *Store) SetPruning(pruning sdk.PruningStrategy) { } // Implements CommitStore -func (ts *transientStore) LastCommitID() (id CommitID) { +func (ts *Store) LastCommitID() (id sdk.CommitID) { return } - -// Implements KVStore -func (ts *transientStore) Prefix(prefix []byte) KVStore { - return prefixStore{ts, prefix} -} - -// Implements KVStore -func (ts *transientStore) Gas(meter GasMeter, config GasConfig) KVStore { - return NewGasKVStore(meter, config, ts) -} - -// Implements Store. -func (ts *transientStore) GetStoreType() StoreType { - return sdk.StoreTypeTransient -} diff --git a/store/transient/transientstore_test.go b/store/transient/transientstore_test.go index 1c9e98cfaaa1..75a639092c80 100644 --- a/store/transient/transientstore_test.go +++ b/store/transient/transientstore_test.go @@ -1,4 +1,4 @@ -package store +package transient import ( "testing" diff --git a/types/store.go b/types/store.go index a1877a9948bf..ddc48328de44 100644 --- a/types/store.go +++ b/types/store.go @@ -26,7 +26,6 @@ const ( ) type Store interface { //nolint - GetStoreType() StoreType CacheWrapper } @@ -95,7 +94,7 @@ type CommitMultiStore interface { // Mount a store of type using the given db. // If db == nil, the new store will use the CommitMultiStore db. - MountStoreWithDB(key StoreKey, typ StoreType, db dbm.DB) + MountStoreWithDB(key StoreKey, db dbm.DB) // Panics on a nil key. GetCommitStore(key StoreKey) CommitStore @@ -182,6 +181,11 @@ type CommitKVStore interface { KVStore } +type MountableStore interface { + CommitStore + Load() +} + //---------------------------------------- // CacheWrap @@ -225,20 +229,6 @@ func (cid CommitID) String() string { return fmt.Sprintf("CommitID{%v:%X}", cid.Hash, cid.Version) } -//---------------------------------------- -// Store types - -// kind of store -type StoreType int - -const ( - //nolint - StoreTypeMulti StoreType = iota - StoreTypeDB - StoreTypeIAVL - StoreTypeTransient -) - //---------------------------------------- // Keys for accessing substores From e9e5d005e64a2c22b121c6b2b85bfb5f4655ff59 Mon Sep 17 00:00:00 2001 From: mossid Date: Sun, 16 Sep 2018 04:31:55 +0900 Subject: [PATCH 03/22] refactor types/{store.go, gas.go} --- store/cache/store.go | 14 +-- store/cache/store_test.go | 133 ++++++++++---------- store/cachemulti/store.go | 86 +++++++++++++ store/cachemultistore.go | 139 --------------------- store/dbadapter/store.go | 10 +- store/gas/store.go | 78 +++++------- store/gas/store_test.go | 88 +++++++------ store/iavl/store.go | 17 +-- store/prefix/store.go | 18 +-- store/rootmulti/store.go | 226 ++++++++++++++-------------------- store/rootmulti/store_test.go | 30 ++--- store/rootmulti/wire.go | 2 +- store/trace/store.go | 68 +++++----- store/trace/store_test.go | 50 ++++---- store/utils/utils.go | 10 ++ types/context.go | 6 +- types/gas.go | 52 +++++++- types/store.go | 108 +++++----------- types/trace.go | 45 +++++++ 19 files changed, 535 insertions(+), 645 deletions(-) create mode 100644 store/cachemulti/store.go delete mode 100644 store/cachemultistore.go create mode 100644 store/utils/utils.go create mode 100644 types/trace.go diff --git a/store/cache/store.go b/store/cache/store.go index d677f7fee913..147631183200 100644 --- a/store/cache/store.go +++ b/store/cache/store.go @@ -2,15 +2,12 @@ package cache import ( "bytes" - "io" "sort" "sync" cmn "github.com/tendermint/tendermint/libs/common" sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/cosmos/cosmos-sdk/store/trace" ) // If value is nil but deleted is false, it means the parent doesn't have the @@ -112,19 +109,10 @@ func (ci *Store) Write() { ci.cache = make(map[string]cValue) } -//---------------------------------------- -// To cache-wrap this Store further. - -// Implements CacheWrapper. -func (ci *Store) CacheWrap() sdk.CacheWrap { +func (ci *Store) CacheWrap() sdk.CacheKVStore { return NewStore(ci) } -// CacheWrapWithTrace implements the CacheWrapper interface. -func (ci *Store) CacheWrapWithTrace(w io.Writer, tc sdk.TraceContext) sdk.CacheWrap { - return NewStore(trace.NewStore(ci, w, tc)) -} - //---------------------------------------- // Iteration diff --git a/store/cache/store_test.go b/store/cache/store_test.go index 6258bbe78c7a..6ce064d28366 100644 --- a/store/cache/store_test.go +++ b/store/cache/store_test.go @@ -1,7 +1,6 @@ package cache_test import ( - "fmt" "testing" "github.com/stretchr/testify/require" @@ -10,88 +9,86 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/store/cachekv" + "github.com/cosmos/cosmos-sdk/store/cache" "github.com/cosmos/cosmos-sdk/store/dbadapter" + "github.com/cosmos/cosmos-sdk/store/utils" ) func newCacheKVStore() sdk.CacheKVStore { mem := dbadapter.NewStore(dbm.NewMemDB()) - return cachekv.NewStore(mem) + return cache.NewStore(mem) } -func keyFmt(i int) []byte { return bz(fmt.Sprintf("key%0.8d", i)) } -func valFmt(i int) []byte { return bz(fmt.Sprintf("value%0.8d", i)) } - func TestCacheKVStore(t *testing.T) { mem := dbadapter.NewStore(dbm.NewMemDB()) - st := cachekv.NewStore(mem) + st := cache.NewStore(mem) - require.Empty(t, st.Get(keyFmt(1)), "Expected `key1` to be empty") + require.Empty(t, st.Get(utils.KeyFmt(1)), "Expected `key1` to be empty") // put something in mem and in cache - mem.Set(keyFmt(1), valFmt(1)) - st.Set(keyFmt(1), valFmt(1)) - require.Equal(t, valFmt(1), st.Get(keyFmt(1))) + mem.Set(utils.KeyFmt(1), utils.ValFmt(1)) + st.Set(utils.KeyFmt(1), utils.ValFmt(1)) + require.Equal(t, utils.ValFmt(1), st.Get(utils.KeyFmt(1))) // update it in cache, shoudn't change mem - st.Set(keyFmt(1), valFmt(2)) - require.Equal(t, valFmt(2), st.Get(keyFmt(1))) - require.Equal(t, valFmt(1), mem.Get(keyFmt(1))) + st.Set(utils.KeyFmt(1), utils.ValFmt(2)) + require.Equal(t, utils.ValFmt(2), st.Get(utils.KeyFmt(1))) + require.Equal(t, utils.ValFmt(1), mem.Get(utils.KeyFmt(1))) // write it. should change mem st.Write() - require.Equal(t, valFmt(2), mem.Get(keyFmt(1))) - require.Equal(t, valFmt(2), st.Get(keyFmt(1))) + require.Equal(t, utils.ValFmt(2), mem.Get(utils.KeyFmt(1))) + require.Equal(t, utils.ValFmt(2), st.Get(utils.KeyFmt(1))) // more writes and checks st.Write() st.Write() - require.Equal(t, valFmt(2), mem.Get(keyFmt(1))) - require.Equal(t, valFmt(2), st.Get(keyFmt(1))) + require.Equal(t, utils.ValFmt(2), mem.Get(utils.KeyFmt(1))) + require.Equal(t, utils.ValFmt(2), st.Get(utils.KeyFmt(1))) // make a new one, check it - st = cachekv.NewStore(mem) - require.Equal(t, valFmt(2), st.Get(keyFmt(1))) + st = cache.NewStore(mem) + require.Equal(t, utils.ValFmt(2), st.Get(utils.KeyFmt(1))) // make a new one and delete - should not be removed from mem - st = cachekv.NewStore(mem) - st.Delete(keyFmt(1)) - require.Empty(t, st.Get(keyFmt(1))) - require.Equal(t, mem.Get(keyFmt(1)), valFmt(2)) + st = cache.NewStore(mem) + st.Delete(utils.KeyFmt(1)) + require.Empty(t, st.Get(utils.KeyFmt(1))) + require.Equal(t, mem.Get(utils.KeyFmt(1)), utils.ValFmt(2)) // Write. should now be removed from both st.Write() - require.Empty(t, st.Get(keyFmt(1)), "Expected `key1` to be empty") - require.Empty(t, mem.Get(keyFmt(1)), "Expected `key1` to be empty") + require.Empty(t, st.Get(utils.KeyFmt(1)), "Expected `key1` to be empty") + require.Empty(t, mem.Get(utils.KeyFmt(1)), "Expected `key1` to be empty") } func TestCacheKVStoreNested(t *testing.T) { mem := dbadapter.NewStore(dbm.NewMemDB()) - st := cachekv.NewStore(mem) + st := cache.NewStore(mem) // set. check its there on st and not on mem. - st.Set(keyFmt(1), valFmt(1)) - require.Empty(t, mem.Get(keyFmt(1))) - require.Equal(t, valFmt(1), st.Get(keyFmt(1))) + st.Set(utils.KeyFmt(1), utils.ValFmt(1)) + require.Empty(t, mem.Get(utils.KeyFmt(1))) + require.Equal(t, utils.ValFmt(1), st.Get(utils.KeyFmt(1))) // make a new from st and check - st2 := cachekv.NewStore(st) - require.Equal(t, valFmt(1), st2.Get(keyFmt(1))) + st2 := cache.NewStore(st) + require.Equal(t, utils.ValFmt(1), st2.Get(utils.KeyFmt(1))) // update the value on st2, check it only effects st2 - st2.Set(keyFmt(1), valFmt(3)) - require.Equal(t, []byte(nil), mem.Get(keyFmt(1))) - require.Equal(t, valFmt(1), st.Get(keyFmt(1))) - require.Equal(t, valFmt(3), st2.Get(keyFmt(1))) + st2.Set(utils.KeyFmt(1), utils.ValFmt(3)) + require.Equal(t, []byte(nil), mem.Get(utils.KeyFmt(1))) + require.Equal(t, utils.ValFmt(1), st.Get(utils.KeyFmt(1))) + require.Equal(t, utils.ValFmt(3), st2.Get(utils.KeyFmt(1))) // st2 writes to its parent, st. doesnt effect mem st2.Write() - require.Equal(t, []byte(nil), mem.Get(keyFmt(1))) - require.Equal(t, valFmt(3), st.Get(keyFmt(1))) + require.Equal(t, []byte(nil), mem.Get(utils.KeyFmt(1))) + require.Equal(t, utils.ValFmt(3), st.Get(utils.KeyFmt(1))) // updates mem st.Write() - require.Equal(t, valFmt(3), mem.Get(keyFmt(1))) + require.Equal(t, utils.ValFmt(3), mem.Get(utils.KeyFmt(1))) } func TestCacheKVIteratorBounds(t *testing.T) { @@ -100,7 +97,7 @@ func TestCacheKVIteratorBounds(t *testing.T) { // set some items nItems := 5 for i := 0; i < nItems; i++ { - st.Set(keyFmt(i), valFmt(i)) + st.Set(utils.KeyFmt(i), utils.ValFmt(i)) } // iterate over all of them @@ -108,8 +105,8 @@ func TestCacheKVIteratorBounds(t *testing.T) { var i = 0 for ; itr.Valid(); itr.Next() { k, v := itr.Key(), itr.Value() - require.Equal(t, keyFmt(i), k) - require.Equal(t, valFmt(i), v) + require.Equal(t, utils.KeyFmt(i), k) + require.Equal(t, utils.ValFmt(i), v) i++ } require.Equal(t, nItems, i) @@ -123,23 +120,23 @@ func TestCacheKVIteratorBounds(t *testing.T) { require.Equal(t, 0, i) // iterate over lower - itr = st.Iterator(keyFmt(0), keyFmt(3)) + itr = st.Iterator(utils.KeyFmt(0), utils.KeyFmt(3)) i = 0 for ; itr.Valid(); itr.Next() { k, v := itr.Key(), itr.Value() - require.Equal(t, keyFmt(i), k) - require.Equal(t, valFmt(i), v) + require.Equal(t, utils.KeyFmt(i), k) + require.Equal(t, utils.ValFmt(i), v) i++ } require.Equal(t, 3, i) // iterate over upper - itr = st.Iterator(keyFmt(2), keyFmt(4)) + itr = st.Iterator(utils.KeyFmt(2), utils.KeyFmt(4)) i = 2 for ; itr.Valid(); itr.Next() { k, v := itr.Key(), itr.Value() - require.Equal(t, keyFmt(i), k) - require.Equal(t, valFmt(i), v) + require.Equal(t, utils.KeyFmt(i), k) + require.Equal(t, utils.ValFmt(i), v) i++ } require.Equal(t, 4, i) @@ -149,7 +146,7 @@ func TestCacheKVMergeIteratorBasics(t *testing.T) { st := newCacheKVStore() // set and delete an item in the cache, iterator should be empty - k, v := keyFmt(0), valFmt(0) + k, v := utils.KeyFmt(0), utils.ValFmt(0) st.Set(k, v) st.Delete(k) assertIterateDomain(t, st, 0) @@ -171,7 +168,7 @@ func TestCacheKVMergeIteratorBasics(t *testing.T) { assertIterateDomain(t, st, 0) // add two keys and assert theyre there - k1, v1 := keyFmt(1), valFmt(1) + k1, v1 := utils.KeyFmt(1), utils.ValFmt(1) st.Set(k, v) st.Set(k1, v1) assertIterateDomain(t, st, 2) @@ -199,13 +196,13 @@ func TestCacheKVMergeIteratorDeleteLast(t *testing.T) { // set some items and write them nItems := 5 for i := 0; i < nItems; i++ { - st.Set(keyFmt(i), valFmt(i)) + st.Set(utils.KeyFmt(i), utils.ValFmt(i)) } st.Write() // set some more items and leave dirty for i := nItems; i < nItems*2; i++ { - st.Set(keyFmt(i), valFmt(i)) + st.Set(utils.KeyFmt(i), utils.ValFmt(i)) } // iterate over all of them @@ -214,7 +211,7 @@ func TestCacheKVMergeIteratorDeleteLast(t *testing.T) { // delete them all for i := 0; i < nItems*2; i++ { last := nItems*2 - 1 - i - st.Delete(keyFmt(last)) + st.Delete(utils.KeyFmt(last)) assertIterateDomain(t, st, last) } } @@ -321,16 +318,16 @@ func doOp(st sdk.CacheKVStore, truth dbm.DB, op int, args ...int) { switch op { case opSet: k := args[0] - st.Set(keyFmt(k), valFmt(k)) - truth.Set(keyFmt(k), valFmt(k)) + st.Set(utils.KeyFmt(k), utils.ValFmt(k)) + truth.Set(utils.KeyFmt(k), utils.ValFmt(k)) case opSetRange: start := args[0] end := args[1] setRange(st, truth, start, end) case opDel: k := args[0] - st.Delete(keyFmt(k)) - truth.Delete(keyFmt(k)) + st.Delete(utils.KeyFmt(k)) + truth.Delete(utils.KeyFmt(k)) case opDelRange: start := args[0] end := args[1] @@ -345,16 +342,16 @@ func doRandomOp(st sdk.CacheKVStore, truth dbm.DB, maxKey int) { switch r { case opSet: k := randInt(maxKey) - st.Set(keyFmt(k), valFmt(k)) - truth.Set(keyFmt(k), valFmt(k)) + st.Set(utils.KeyFmt(k), utils.ValFmt(k)) + truth.Set(utils.KeyFmt(k), utils.ValFmt(k)) case opSetRange: start := randInt(maxKey - 2) end := randInt(maxKey-start) + start setRange(st, truth, start, end) case opDel: k := randInt(maxKey) - st.Delete(keyFmt(k)) - truth.Delete(keyFmt(k)) + st.Delete(utils.KeyFmt(k)) + truth.Delete(utils.KeyFmt(k)) case opDelRange: start := randInt(maxKey - 2) end := randInt(maxKey-start) + start @@ -372,8 +369,8 @@ func assertIterateDomain(t *testing.T, st sdk.KVStore, expectedN int) { var i = 0 for ; itr.Valid(); itr.Next() { k, v := itr.Key(), itr.Value() - require.Equal(t, keyFmt(i), k) - require.Equal(t, valFmt(i), v) + require.Equal(t, utils.KeyFmt(i), k) + require.Equal(t, utils.ValFmt(i), v) i++ } require.Equal(t, expectedN, i) @@ -398,7 +395,7 @@ func assertIterateDomainCheck(t *testing.T, st sdk.KVStore, mem dbm.DB, r []keyR require.Equal(t, v, v2) // check they match the counter - require.Equal(t, k, keyFmt(krc.key())) + require.Equal(t, k, utils.KeyFmt(krc.key())) itr.Next() itr2.Next() @@ -434,15 +431,15 @@ func checkIterators(t *testing.T, itr, itr2 sdk.Iterator) { func setRange(st sdk.KVStore, mem dbm.DB, start, end int) { for i := start; i < end; i++ { - st.Set(keyFmt(i), valFmt(i)) - mem.Set(keyFmt(i), valFmt(i)) + st.Set(utils.KeyFmt(i), utils.ValFmt(i)) + mem.Set(utils.KeyFmt(i), utils.ValFmt(i)) } } func deleteRange(st sdk.KVStore, mem dbm.DB, start, end int) { for i := start; i < end; i++ { - st.Delete(keyFmt(i)) - mem.Delete(keyFmt(i)) + st.Delete(utils.KeyFmt(i)) + mem.Delete(utils.KeyFmt(i)) } } diff --git a/store/cachemulti/store.go b/store/cachemulti/store.go new file mode 100644 index 000000000000..9ef0cabb371e --- /dev/null +++ b/store/cachemulti/store.go @@ -0,0 +1,86 @@ +package cachemulti + +import ( + dbm "github.com/tendermint/tendermint/libs/db" + + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/cosmos-sdk/store/cache" + "github.com/cosmos/cosmos-sdk/store/dbadapter" + "github.com/cosmos/cosmos-sdk/store/trace" +) + +//---------------------------------------- +// Store + +// Store holds many cache-wrapped stores. +// Implements MultiStore. +type Store struct { + db sdk.CacheKVStore + stores map[sdk.StoreKey]sdk.CacheKVStore + keysByName map[string]sdk.StoreKey + + tracer *sdk.Tracer +} + +var _ sdk.CacheMultiStore = Store{} + +func NewStore(db dbm.DB, keysByName map[string]sdk.StoreKey, stores map[sdk.StoreKey]sdk.CommitKVStore, tracer *sdk.Tracer) Store { + cms := Store{ + db: cache.NewStore(dbadapter.NewStore(db)), + stores: make(map[sdk.StoreKey]sdk.CacheKVStore, len(stores)), + keysByName: keysByName, + tracer: tracer, + } + + for key, store := range stores { + if tracer.Enabled() { + cms.stores[key] = cache.NewStore(trace.NewStore(store, tracer)) + } else { + cms.stores[key] = cache.NewStore(store) + } + } + + return cms +} + +func newCacheMultiStoreFromCMS(cms Store) Store { + cms2 := Store{ + db: cache.NewStore(cms.db), + stores: make(map[sdk.StoreKey]sdk.CacheKVStore, len(cms.stores)), + tracer: cms.tracer, + } + + for key, store := range cms.stores { + if cms2.tracer.Enabled() { + cms2.stores[key] = cache.NewStore(trace.NewStore(store, cms.tracer)) + } else { + cms2.stores[key] = cache.NewStore(store) + } + } + + return cms2 +} + +// Implements MultiStore +func (cms Store) GetTracer() *sdk.Tracer { + return cms.tracer +} + +// Implements CacheMultiStore. +func (cms Store) Write() { + cms.db.Write() + for _, store := range cms.stores { + store.Write() + } +} + +// Implements MultiStore. +func (cms Store) CacheWrap() sdk.CacheMultiStore { + return newCacheMultiStoreFromCMS(cms) +} + +// Implements MultiStore. +func (cms Store) GetKVStore(key sdk.StoreKey) sdk.KVStore { + return cms.stores[key].(sdk.KVStore) +} diff --git a/store/cachemultistore.go b/store/cachemultistore.go deleted file mode 100644 index f69ad42aacd1..000000000000 --- a/store/cachemultistore.go +++ /dev/null @@ -1,139 +0,0 @@ -package store - -import ( - "io" - - sdk "github.com/cosmos/cosmos-sdk/types" -) - -//---------------------------------------- -// cacheMultiStore - -// cacheMultiStore holds many cache-wrapped stores. -// Implements MultiStore. -type cacheMultiStore struct { - db CacheKVStore - stores map[StoreKey]CacheWrap - keysByName map[string]StoreKey - - traceWriter io.Writer - traceContext TraceContext -} - -var _ CacheMultiStore = cacheMultiStore{} - -func newCacheMultiStoreFromRMS(rms *rootMultiStore) cacheMultiStore { - cms := cacheMultiStore{ - db: NewCacheKVStore(dbStoreAdapter{rms.db}), - stores: make(map[StoreKey]CacheWrap, len(rms.stores)), - keysByName: rms.keysByName, - traceWriter: rms.traceWriter, - traceContext: rms.traceContext, - } - - for key, store := range rms.stores { - if cms.TracingEnabled() { - cms.stores[key] = store.CacheWrapWithTrace(cms.traceWriter, cms.traceContext) - } else { - cms.stores[key] = store.CacheWrap() - } - } - - return cms -} - -func newCacheMultiStoreFromCMS(cms cacheMultiStore) cacheMultiStore { - cms2 := cacheMultiStore{ - db: NewCacheKVStore(cms.db), - stores: make(map[StoreKey]CacheWrap, len(cms.stores)), - traceWriter: cms.traceWriter, - traceContext: cms.traceContext, - } - - for key, store := range cms.stores { - if cms2.TracingEnabled() { - cms2.stores[key] = store.CacheWrapWithTrace(cms2.traceWriter, cms2.traceContext) - } else { - cms2.stores[key] = store.CacheWrap() - } - } - - return cms2 -} - -// WithTracer sets the tracer for the MultiStore that the underlying -// stores will utilize to trace operations. A MultiStore is returned. -func (cms cacheMultiStore) WithTracer(w io.Writer) MultiStore { - cms.traceWriter = w - return cms -} - -// WithTracingContext updates the tracing context for the MultiStore by merging -// the given context with the existing context by key. Any existing keys will -// be overwritten. It is implied that the caller should update the context when -// necessary between tracing operations. It returns a modified MultiStore. -func (cms cacheMultiStore) WithTracingContext(tc TraceContext) MultiStore { - if cms.traceContext != nil { - for k, v := range tc { - cms.traceContext[k] = v - } - } else { - cms.traceContext = tc - } - - return cms -} - -// TracingEnabled returns if tracing is enabled for the MultiStore. -func (cms cacheMultiStore) TracingEnabled() bool { - return cms.traceWriter != nil -} - -// ResetTraceContext resets the current tracing context. -func (cms cacheMultiStore) ResetTraceContext() MultiStore { - cms.traceContext = nil - return cms -} - -// Implements Store. -func (cms cacheMultiStore) GetStoreType() StoreType { - return sdk.StoreTypeMulti -} - -// Implements CacheMultiStore. -func (cms cacheMultiStore) Write() { - cms.db.Write() - for _, store := range cms.stores { - store.Write() - } -} - -// Implements CacheWrapper. -func (cms cacheMultiStore) CacheWrap() CacheWrap { - return cms.CacheMultiStore().(CacheWrap) -} - -// CacheWrapWithTrace implements the CacheWrapper interface. -func (cms cacheMultiStore) CacheWrapWithTrace(_ io.Writer, _ TraceContext) CacheWrap { - return cms.CacheWrap() -} - -// Implements MultiStore. -func (cms cacheMultiStore) CacheMultiStore() CacheMultiStore { - return newCacheMultiStoreFromCMS(cms) -} - -// Implements MultiStore. -func (cms cacheMultiStore) GetStore(key StoreKey) Store { - return cms.stores[key].(Store) -} - -// Implements MultiStore. -func (cms cacheMultiStore) GetKVStore(key StoreKey) KVStore { - return cms.stores[key].(KVStore) -} - -// Implements MultiStore. -func (cms cacheMultiStore) GetKVStoreWithGas(meter sdk.GasMeter, config sdk.GasConfig, key StoreKey) KVStore { - return NewGasKVStore(meter, config, cms.GetKVStore(key)) -} diff --git a/store/dbadapter/store.go b/store/dbadapter/store.go index af848aae93a3..f72b043b7859 100644 --- a/store/dbadapter/store.go +++ b/store/dbadapter/store.go @@ -1,13 +1,10 @@ package dbadapter import ( - "io" - sdk "github.com/cosmos/cosmos-sdk/types" dbm "github.com/tendermint/tendermint/libs/db" "github.com/cosmos/cosmos-sdk/store/cache" - "github.com/cosmos/cosmos-sdk/store/trace" ) // Wrapper type for dbm.Db with implementation of KVStore @@ -20,14 +17,9 @@ func NewStore(parent dbm.DB) Store { } // Implements KVStore. -func (dsa Store) CacheWrap() sdk.CacheWrap { +func (dsa Store) CacheWrap() sdk.CacheKVStore { return cache.NewStore(dsa) } -// CacheWrapWithTrace implements the KVStore interface. -func (dsa Store) CacheWrapWithTrace(w io.Writer, tc sdk.TraceContext) sdk.CacheWrap { - return cache.NewStore(trace.NewStore(dsa, w, tc)) -} - // dbm.DB implements KVStore so we can CacheKVStore it. var _ sdk.KVStore = Store{} diff --git a/store/gas/store.go b/store/gas/store.go index db4c5a1936b9..f7264a498373 100644 --- a/store/gas/store.go +++ b/store/gas/store.go @@ -1,8 +1,6 @@ package gas import ( - "io" - sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -11,70 +9,58 @@ var _ sdk.KVStore = &gasKVStore{} // gasKVStore applies gas tracking to an underlying KVStore. It implements the // KVStore interface. type gasKVStore struct { - gasMeter sdk.GasMeter - gasConfig sdk.GasConfig - parent sdk.KVStore + tank *sdk.GasTank + parent sdk.KVStore } // NewGasKVStore returns a reference to a new GasKVStore. // nolint -func NewGasKVStore(gasMeter sdk.GasMeter, gasConfig sdk.GasConfig, parent sdk.KVStore) *gasKVStore { +func NewStore(tank *sdk.GasTank, parent sdk.KVStore) *gasKVStore { kvs := &gasKVStore{ - gasMeter: gasMeter, - gasConfig: gasConfig, - parent: parent, + tank: tank, + parent: parent, } return kvs } // Implements sdk.KVStore. -func (gi *gasKVStore) Get(key []byte) (value []byte) { - gi.gasMeter.ConsumeGas(gi.gasConfig.ReadCostFlat, sdk.GasReadCostFlatDesc) - value = gi.parent.Get(key) +func (gs *gasKVStore) Get(key []byte) (value []byte) { + gs.tank.ReadFlat() + value = gs.parent.Get(key) // TODO overflow-safe math? - gi.gasMeter.ConsumeGas(gi.gasConfig.ReadCostPerByte*sdk.Gas(len(value)), sdk.GasReadPerByteDesc) + gs.tank.ReadBytes(len(value)) return value } // Implements sdk.KVStore. -func (gi *gasKVStore) Set(key []byte, value []byte) { - gi.gasMeter.ConsumeGas(gi.gasConfig.WriteCostFlat, sdk.GasWriteCostFlatDesc) +func (gs *gasKVStore) Set(key []byte, value []byte) { + gs.tank.WriteFlat() // TODO overflow-safe math? - gi.gasMeter.ConsumeGas(gi.gasConfig.WriteCostPerByte*sdk.Gas(len(value)), sdk.GasWritePerByteDesc) - gi.parent.Set(key, value) -} - -// Implements sdk.KVStore. -func (gi *gasKVStore) Has(key []byte) bool { - gi.gasMeter.ConsumeGas(gi.gasConfig.HasCost, sdk.GasHasDesc) - return gi.parent.Has(key) + gs.tank.WriteBytes(len(value)) + gs.parent.Set(key, value) } // Implements sdk.KVStore. -func (gi *gasKVStore) Delete(key []byte) { - gi.gasMeter.ConsumeGas(gi.gasConfig.DeleteCost, sdk.GasDeleteDesc) - gi.parent.Delete(key) +func (gs *gasKVStore) Has(key []byte) bool { + gs.tank.HasFlat() + return gs.parent.Has(key) } // Implements sdk.KVStore. -func (gi *gasKVStore) Iterator(start, end []byte) sdk.Iterator { - return gi.iterator(start, end, true) +func (gs *gasKVStore) Delete(key []byte) { + gs.tank.DeleteFlat() + gs.parent.Delete(key) } // Implements sdk.KVStore. -func (gi *gasKVStore) ReverseIterator(start, end []byte) sdk.Iterator { - return gi.iterator(start, end, false) +func (gs *gasKVStore) Iterator(start, end []byte) sdk.Iterator { + return gs.iterator(start, end, true) } // Implements sdk.KVStore. -func (gi *gasKVStore) CacheWrap() sdk.CacheWrap { - panic("cannot CacheWrap a GasKVStore") -} - -// CacheWrapWithTrace implements the sdk.KVStore interface. -func (gi *gasKVStore) CacheWrapWithTrace(_ io.Writer, _ sdk.TraceContext) sdk.CacheWrap { - panic("cannot CacheWrapWithTrace a GasKVStore") +func (gs *gasKVStore) ReverseIterator(start, end []byte) sdk.Iterator { + return gs.iterator(start, end, false) } func (gs *gasKVStore) iterator(start, end []byte, ascending bool) sdk.Iterator { @@ -85,7 +71,7 @@ func (gs *gasKVStore) iterator(start, end []byte, ascending bool) sdk.Iterator { parent = gs.parent.ReverseIterator(start, end) } - gi := newGasIterator(gs.gasMeter, gs.gasConfig, parent) + gi := newGasIterator(gs.tank, parent) if gi.Valid() { gi.(*gasIterator).consumeSeekGas() } @@ -94,16 +80,14 @@ func (gs *gasKVStore) iterator(start, end []byte, ascending bool) sdk.Iterator { } type gasIterator struct { - gasMeter sdk.GasMeter - gasConfig sdk.GasConfig - parent sdk.Iterator + tank *sdk.GasTank + parent sdk.Iterator } -func newGasIterator(gasMeter sdk.GasMeter, gasConfig sdk.GasConfig, parent sdk.Iterator) sdk.Iterator { +func newGasIterator(tank *sdk.GasTank, parent sdk.Iterator) sdk.Iterator { return &gasIterator{ - gasMeter: gasMeter, - gasConfig: gasConfig, - parent: parent, + tank: tank, + parent: parent, } } @@ -152,6 +136,6 @@ func (gi *gasIterator) Close() { func (gi *gasIterator) consumeSeekGas() { value := gi.Value() - gi.gasMeter.ConsumeGas(gi.gasConfig.ValueCostPerByte*sdk.Gas(len(value)), sdk.GasValuePerByteDesc) - gi.gasMeter.ConsumeGas(gi.gasConfig.IterNextCostFlat, sdk.GasIterNextCostFlatDesc) + gi.tank.ValueBytes(len(value)) + gi.tank.IterNextFlat() } diff --git a/store/gas/store_test.go b/store/gas/store_test.go index aa6ada4c9cde..d4f6b7d1a92f 100644 --- a/store/gas/store_test.go +++ b/store/gas/store_test.go @@ -5,65 +5,71 @@ import ( dbm "github.com/tendermint/tendermint/libs/db" + "github.com/cosmos/cosmos-sdk/store/dbadapter" + "github.com/cosmos/cosmos-sdk/store/utils" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" ) +func newGasTank(limit sdk.Gas) *sdk.GasTank { + return sdk.NewGasTank(sdk.NewGasMeter(limit), sdk.KVGasConfig()) +} + func newGasKVStore() sdk.KVStore { - meter := sdk.NewGasMeter(1000) - mem := dbStoreAdapter{dbm.NewMemDB()} - return NewGasKVStore(meter, sdk.KVGasConfig(), mem) + tank := newGasTank(1000) + mem := dbadapter.NewStore(dbm.NewMemDB()) + return NewStore(tank, mem) } func TestGasKVStoreBasic(t *testing.T) { - mem := dbStoreAdapter{dbm.NewMemDB()} - meter := sdk.NewGasMeter(1000) - st := NewGasKVStore(meter, sdk.KVGasConfig(), mem) - require.Empty(t, st.Get(keyFmt(1)), "Expected `key1` to be empty") - st.Set(keyFmt(1), valFmt(1)) - require.Equal(t, valFmt(1), st.Get(keyFmt(1))) - st.Delete(keyFmt(1)) - require.Empty(t, st.Get(keyFmt(1)), "Expected `key1` to be empty") - require.Equal(t, meter.GasConsumed(), sdk.Gas(193)) + tank := newGasTank(1000) + mem := dbadapter.NewStore(dbm.NewMemDB()) + st := NewStore(tank, mem) + require.Empty(t, st.Get(utils.KeyFmt(1)), "Expected `key1` to be empty") + st.Set(utils.KeyFmt(1), utils.ValFmt(1)) + require.Equal(t, utils.ValFmt(1), st.Get(utils.KeyFmt(1))) + st.Delete(utils.KeyFmt(1)) + require.Empty(t, st.Get(utils.KeyFmt(1)), "Expected `key1` to be empty") + require.Equal(t, tank.GasConsumed(), sdk.Gas(193)) } func TestGasKVStoreIterator(t *testing.T) { - mem := dbStoreAdapter{dbm.NewMemDB()} - meter := sdk.NewGasMeter(1000) - st := NewGasKVStore(meter, sdk.KVGasConfig(), mem) - require.Empty(t, st.Get(keyFmt(1)), "Expected `key1` to be empty") - require.Empty(t, st.Get(keyFmt(2)), "Expected `key2` to be empty") - st.Set(keyFmt(1), valFmt(1)) - st.Set(keyFmt(2), valFmt(2)) + tank := newGasTank(1000) + mem := dbadapter.NewStore(dbm.NewMemDB()) + st := NewStore(tank, mem) + require.Empty(t, st.Get(utils.KeyFmt(1)), "Expected `key1` to be empty") + require.Empty(t, st.Get(utils.KeyFmt(2)), "Expected `key2` to be empty") + st.Set(utils.KeyFmt(1), utils.ValFmt(1)) + st.Set(utils.KeyFmt(2), utils.ValFmt(2)) iterator := st.Iterator(nil, nil) ka := iterator.Key() - require.Equal(t, ka, keyFmt(1)) + require.Equal(t, ka, utils.KeyFmt(1)) va := iterator.Value() - require.Equal(t, va, valFmt(1)) + require.Equal(t, va, utils.ValFmt(1)) iterator.Next() kb := iterator.Key() - require.Equal(t, kb, keyFmt(2)) + require.Equal(t, kb, utils.KeyFmt(2)) vb := iterator.Value() - require.Equal(t, vb, valFmt(2)) + require.Equal(t, vb, utils.ValFmt(2)) iterator.Next() require.False(t, iterator.Valid()) require.Panics(t, iterator.Next) - require.Equal(t, meter.GasConsumed(), sdk.Gas(384)) + require.Equal(t, tank.GasConsumed(), sdk.Gas(384)) } func TestGasKVStoreOutOfGasSet(t *testing.T) { - mem := dbStoreAdapter{dbm.NewMemDB()} - meter := sdk.NewGasMeter(0) - st := NewGasKVStore(meter, sdk.KVGasConfig(), mem) - require.Panics(t, func() { st.Set(keyFmt(1), valFmt(1)) }, "Expected out-of-gas") + tank := newGasTank(0) + mem := dbadapter.NewStore(dbm.NewMemDB()) + st := NewStore(tank, mem) + require.Panics(t, func() { st.Set(utils.KeyFmt(1), utils.ValFmt(1)) }, "Expected out-of-gas") } func TestGasKVStoreOutOfGasIterator(t *testing.T) { - mem := dbStoreAdapter{dbm.NewMemDB()} - meter := sdk.NewGasMeter(200) - st := NewGasKVStore(meter, sdk.KVGasConfig(), mem) - st.Set(keyFmt(1), valFmt(1)) + tank := newGasTank(200) + mem := dbadapter.NewStore(dbm.NewMemDB()) + st := NewStore(tank, mem) + st.Set(utils.KeyFmt(1), utils.ValFmt(1)) iterator := st.Iterator(nil, nil) iterator.Next() require.Panics(t, func() { iterator.Value() }, "Expected out-of-gas") @@ -71,35 +77,39 @@ func TestGasKVStoreOutOfGasIterator(t *testing.T) { func testGasKVStoreWrap(t *testing.T, store sdk.KVStore) { meter := sdk.NewGasMeter(10000) + tank := sdk.NewGasTank(meter, sdk.GasConfig{HasCostFlat: 10}) - store = store.Gas(meter, sdk.GasConfig{HasCost: 10}) + store = NewStore(tank, store) require.Equal(t, int64(0), meter.GasConsumed()) store.Has([]byte("key")) require.Equal(t, int64(10), meter.GasConsumed()) - store = store.Gas(meter, sdk.GasConfig{HasCost: 20}) + tank = sdk.NewGasTank(meter, sdk.GasConfig{HasCostFlat: 20}) + store = NewStore(tank, store) store.Has([]byte("key")) require.Equal(t, int64(40), meter.GasConsumed()) } +// XXX: make it stop using iavl or move it to iavl +/* func TestGasKVStoreWrap(t *testing.T) { db := dbm.NewMemDB() tree, _ := newTree(t, db) iavl := newIAVLStore(tree, numRecent, storeEvery) testGasKVStoreWrap(t, iavl) - st := NewCacheKVStore(iavl) + st := cache.NewStore(iavl) testGasKVStoreWrap(t, st) - pref := st.Prefix([]byte("prefix")) + pref := prefix.NewStore(st, []byte("prefix")) testGasKVStoreWrap(t, pref) - dsa := dbStoreAdapter{dbm.NewMemDB()} + dsa := dbadapter.NewStore(dbm.NewMemDB()) testGasKVStoreWrap(t, dsa) - ts := newTransientStore() + ts := transient.NewStore() testGasKVStoreWrap(t, ts) - } +*/ diff --git a/store/iavl/store.go b/store/iavl/store.go index 8f1617dfa50b..03370fdc84dc 100644 --- a/store/iavl/store.go +++ b/store/iavl/store.go @@ -2,7 +2,6 @@ package iavl import ( "fmt" - "io" "sync" "github.com/tendermint/go-amino" @@ -12,9 +11,6 @@ import ( dbm "github.com/tendermint/tendermint/libs/db" sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/cosmos/cosmos-sdk/store/cache" - "github.com/cosmos/cosmos-sdk/store/trace" ) const ( @@ -22,14 +18,13 @@ const ( ) // load the iavl store -func LoadIAVLStore(db dbm.DB, id sdk.CommitID, pruning sdk.PruningStrategy) (sdk.CommitStore, error) { +func (*iavlStore) LoadVersion(db dbm.DB, id sdk.CommitID) (sdk.CommitStore, error) { tree := iavl.NewMutableTree(db, defaultIAVLCacheSize) _, err := tree.LoadVersion(id.Version) if err != nil { return nil, err } iavl := newIAVLStore(tree, int64(0), int64(0)) - iavl.SetPruning(pruning) return iavl, nil } @@ -123,16 +118,6 @@ func (st *iavlStore) VersionExists(version int64) bool { return st.tree.VersionExists(version) } -// Implements Store. -func (st *iavlStore) CacheWrap() sdk.CacheWrap { - return cache.NewStore(st) -} - -// CacheWrapWithTrace implements the Store interface. -func (st *iavlStore) CacheWrapWithTrace(w io.Writer, tc sdk.TraceContext) sdk.CacheWrap { - return cache.NewStore(trace.NewStore(st, w, tc)) -} - // Implements sdk.KVStore. func (st *iavlStore) Set(key, value []byte) { st.tree.Set(key, value) diff --git a/store/prefix/store.go b/store/prefix/store.go index 3902cade8f69..481dc17eed47 100644 --- a/store/prefix/store.go +++ b/store/prefix/store.go @@ -2,12 +2,8 @@ package prefix import ( "bytes" - "io" sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/cosmos/cosmos-sdk/store/cache" - "github.com/cosmos/cosmos-sdk/store/trace" ) var _ sdk.KVStore = Store{} @@ -34,15 +30,11 @@ func (s Store) key(key []byte) (res []byte) { res = cloneAppend(s.prefix, key) return } - -// Implements CacheWrap -func (s Store) CacheWrap() sdk.CacheWrap { - return cache.NewStore(s) -} - -// CacheWrapWithTrace implements the KVStore interface. -func (s Store) CacheWrapWithTrace(w io.Writer, tc sdk.TraceContext) sdk.CacheWrap { - return cache.NewStore(trace.NewStore(s, w, tc)) +func NewStore(parent sdk.KVStore, prefix []byte) Store { + return Store{ + parent: parent, + prefix: prefix, + } } // Implements KVStore diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index ea97946b2f6c..438c74b74f6a 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -2,7 +2,7 @@ package rootmulti import ( "fmt" - "io" + "reflect" "strings" abci "github.com/tendermint/tendermint/abci/types" @@ -12,8 +12,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/store/cachemulti" "github.com/cosmos/cosmos-sdk/store/trace" - "github.com/cosmos/cosmos-sdk/store/transient" ) const ( @@ -21,36 +21,40 @@ const ( commitInfoKeyFmt = "s/%d" // s/ ) -// rootMultiStore is composed of many sdk.CommitStores. Name contrasts with +// Store is composed of many sdk.CommitStores. Name contrasts with // cacheMultiStore which is for cache-wrapping other sdk.MultiStores. It implements // the CommitMultiStore interface. -type rootMultiStore struct { +type Store struct { db dbm.DB lastCommitID sdk.CommitID pruning sdk.PruningStrategy storesParams map[sdk.StoreKey]storeParams - stores map[sdk.StoreKey]sdk.MountableStore + stores map[sdk.StoreKey]sdk.CommitKVStore keysByName map[string]sdk.StoreKey - traceWriter io.Writer - traceContext sdk.TraceContext + tracer *sdk.Tracer } -var _ sdk.CommitMultiStore = (*rootMultiStore)(nil) -var _ sdk.Queryable = (*rootMultiStore)(nil) +var _ sdk.CommitMultiStore = (*Store)(nil) +var _ sdk.Queryable = (*Store)(nil) // nolint -func NewCommitMultiStore(db dbm.DB) *rootMultiStore { - return &rootMultiStore{ +func NewCommitMultiStore(db dbm.DB) *Store { + return &Store{ db: db, storesParams: make(map[sdk.StoreKey]storeParams), - stores: make(map[sdk.StoreKey]sdk.MountableStore), + stores: make(map[sdk.StoreKey]sdk.CommitKVStore), keysByName: make(map[string]sdk.StoreKey), } } +// Implements MultiStore +func (rs *Store) GetTracer() *sdk.Tracer { + return rs.tracer +} + // Implements CommitMultiStore -func (rs *rootMultiStore) SetPruning(pruning sdk.PruningStrategy) { +func (rs *Store) SetPruning(pruning sdk.PruningStrategy) { rs.pruning = pruning for _, substore := range rs.stores { substore.SetPruning(pruning) @@ -58,15 +62,15 @@ func (rs *rootMultiStore) SetPruning(pruning sdk.PruningStrategy) { } // Implements CommitMultiStore. -func (rs *rootMultiStore) MountStoreWithDB(key sdk.StoreKey, db dbm.DB) { +func (rs *Store) MountStoreWithDB(key sdk.StoreKey, db dbm.DB) { if key == nil { panic("MountIAVLStore() key cannot be nil") } if _, ok := rs.storesParams[key]; ok { - panic(fmt.Sprintf("rootMultiStore duplicate store key %v", key)) + panic(fmt.Sprintf("Store duplicate store key %v", key)) } if _, ok := rs.keysByName[key.Name()]; ok { - panic(fmt.Sprintf("rootMultiStore duplicate store key name %v", key)) + panic(fmt.Sprintf("Store duplicate store key name %v", key)) } rs.storesParams[key] = storeParams{ key: key, @@ -76,118 +80,70 @@ func (rs *rootMultiStore) MountStoreWithDB(key sdk.StoreKey, db dbm.DB) { } // Implements CommitMultiStore. -func (rs *rootMultiStore) GetCommitStore(key sdk.StoreKey) sdk.CommitStore { +func (rs *Store) GetCommitStore(key sdk.StoreKey) sdk.CommitStore { return rs.stores[key] } // Implements CommitMultiStore. -func (rs *rootMultiStore) GetCommitKVStore(key sdk.StoreKey) sdk.CommitKVStore { +func (rs *Store) GetCommitKVStore(key sdk.StoreKey) sdk.CommitKVStore { return rs.stores[key].(sdk.CommitKVStore) } // Implements CommitMultiStore. -func (rs *rootMultiStore) LoadLatestVersion() error { +func (rs *Store) LoadLatestVersion() error { ver := getLatestVersion(rs.db) - return rs.LoadVersion(ver) + return rs.LoadMultiStoreVersion(ver) } // Implements CommitMultiStore. -func (rs *rootMultiStore) LoadVersion(ver int64) error { - - // Special logic for version 0 - if ver == 0 { - for key, storeParams := range rs.storesParams { - id := sdk.CommitID{} - store, err := rs.loadCommitStoreFromParams(key, id, storeParams) - if err != nil { - return fmt.Errorf("failed to load rootMultiStore: %v", err) - } - rs.stores[key] = store +func (rs *Store) LoadMultiStoreVersion(ver int64) error { + // Convert StoreInfos slice to map + var lastCommitID sdk.CommitID + infos := make(map[sdk.StoreKey]storeInfo) + if ver != 0 { + // Get commitInfo + cInfo, err := getCommitInfo(rs.db, ver) + if err != nil { + return err } - rs.lastCommitID = sdk.CommitID{} - return nil - } - // Otherwise, version is 1 or greater + for _, storeInfo := range cInfo.StoreInfos { + infos[rs.nameToKey(storeInfo.Name)] = storeInfo + } - // Get commitInfo - cInfo, err := getCommitInfo(rs.db, ver) - if err != nil { - return err - } - - // Convert StoreInfos slice to map - infos := make(map[sdk.StoreKey]storeInfo) - for _, storeInfo := range cInfo.StoreInfos { - infos[rs.nameToKey(storeInfo.Name)] = storeInfo + lastCommitID = cInfo.CommitID() } // Load each Store - var newStores = make(map[sdk.StoreKey]sdk.CommitStore) + var newStores = make(map[sdk.StoreKey]sdk.CommitKVStore) for key, storeParams := range rs.storesParams { var id sdk.CommitID - info, ok := infos[key] - if ok { + if info, ok := infos[key]; ok { id = info.Core.CommitID } - store, err := rs.loadCommitStoreFromParams(key, id, storeParams) if err != nil { - return fmt.Errorf("failed to load rootMultiStore: %v", err) + return fmt.Errorf("failed to load Store: %v", err) } newStores[key] = store } // Success. - rs.lastCommitID = cInfo.CommitID() + rs.lastCommitID = lastCommitID rs.stores = newStores return nil } -// WithTracer sets the tracer for the sdk.MultiStore that the underlying -// stores will utilize to trace operations. A sdk.MultiStore is returned. -func (rs *rootMultiStore) WithTracer(w io.Writer) sdk.MultiStore { - rs.traceWriter = w - return rs -} - -// WithTracingContext updates the tracing context for the sdk.MultiStore by merging -// the given context with the existing context by key. Any existing keys will -// be overwritten. It is implied that the caller should update the context when -// necessary between tracing operations. It returns a modified sdk.MultiStore. -func (rs *rootMultiStore) WithTracingContext(tc sdk.TraceContext) sdk.MultiStore { - if rs.traceContext != nil { - for k, v := range tc { - rs.traceContext[k] = v - } - } else { - rs.traceContext = tc - } - - return rs -} - -// TracingEnabled returns if tracing is enabled for the sdk.MultiStore. -func (rs *rootMultiStore) TracingEnabled() bool { - return rs.traceWriter != nil -} - -// ResetTraceContext resets the current tracing context. -func (rs *rootMultiStore) ResetTraceContext() sdk.MultiStore { - rs.traceContext = nil - return rs -} - //---------------------------------------- // +CommitStore // Implements Committer/CommitStore. -func (rs *rootMultiStore) LastCommitID() sdk.CommitID { +func (rs *Store) LastCommitID() sdk.CommitID { return rs.lastCommitID } // Implements Committer/CommitStore. -func (rs *rootMultiStore) Commit() sdk.CommitID { +func (rs *Store) Commit() sdk.CommitID { // Commit stores. version := rs.lastCommitID.Version + 1 @@ -208,37 +164,22 @@ func (rs *rootMultiStore) Commit() sdk.CommitID { return commitID } -// Implements sdk.CacheWrapper/Store/CommitStore. -func (rs *rootMultiStore) CacheWrap() sdk.CacheWrap { - return rs.CacheMultiStore().(sdk.CacheWrap) -} - -// sdk.CacheWrapWithTrace implements the sdk.CacheWrapper interface. -func (rs *rootMultiStore) CacheWrapWithTrace(_ io.Writer, _ sdk.TraceContext) sdk.CacheWrap { - return rs.CacheWrap() -} - //---------------------------------------- // +MultiStore // Implements sdk.MultiStore. -func (rs *rootMultiStore) CacheMultiStore() sdk.CacheMultiStore { - return newCacheMultiStoreFromRMS(rs) -} - -// Implements sdk.MultiStore. -func (rs *rootMultiStore) GetStore(key sdk.StoreKey) sdk.Store { - return rs.stores[key] +func (rs *Store) CacheWrap() sdk.CacheMultiStore { + return cachemulti.NewStore(rs.db, rs.keysByName, rs.stores, rs.tracer) } // GetKVStore implements the sdk.MultiStore interface. If tracing is enabled on the -// rootMultiStore, a wrapped TraceKVStore will be returned with the given +// Store, a wrapped TraceKVStore will be returned with the given // tracer, otherwise, the original sdk.KVStore will be returned. -func (rs *rootMultiStore) GetKVStore(key sdk.StoreKey) sdk.KVStore { +func (rs *Store) GetKVStore(key sdk.StoreKey) sdk.KVStore { store := rs.stores[key].(sdk.KVStore) - if rs.TracingEnabled() { - store = trace.NewStore(store, rs.traceWriter, rs.traceContext) + if rs.tracer.Enabled() { + store = trace.NewStore(store, rs.tracer) } return store @@ -251,7 +192,7 @@ func (rs *rootMultiStore) GetKVStore(key sdk.StoreKey) sdk.KVStore { // This is not exposed to the extensions (which will need the // sdk.StoreKey), but is useful in main, and particularly app.Query, // in order to convert human strings into sdk.CommitStores. -func (rs *rootMultiStore) getStoreByName(name string) sdk.Store { +func (rs *Store) getStoreByName(name string) sdk.KVStore { key := rs.keysByName[name] if key == nil { return nil @@ -265,7 +206,7 @@ func (rs *rootMultiStore) getStoreByName(name string) sdk.Store { // modified to remove the substore prefix. // Ie. `req.Path` here is `//`, and trimmed to `/` for the substore. // TODO: add proof for `multistore -> substore`. -func (rs *rootMultiStore) Query(req abci.RequestQuery) abci.ResponseQuery { +func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery { // Query just routes this to a substore. path := req.Path storeName, subpath, err := parsePath(path) @@ -320,37 +261,49 @@ func parsePath(path string) (storeName string, subpath string, err sdk.Error) { //---------------------------------------- -func (rs *rootMultiStore) loadCommitStoreFromParams(key sdk.StoreKey, id sdk.CommitID, params storeParams) (store sdk.CommitStore, err error) { +func (rs *Store) loadCommitStoreFromParams(key sdk.StoreKey, id sdk.CommitID, params storeParams) (store sdk.CommitKVStore, err error) { var db dbm.DB if params.db != nil { db = dbm.NewPrefixDB(params.db, []byte("s/_/")) } else { db = dbm.NewPrefixDB(rs.db, []byte("s/k:"+params.key.Name()+"/")) } - switch params.typ { - case sdk.StoreTypeMulti: - panic("recursive sdk.MultiStores not yet supported") - // TODO: id? - // return NewCommitMultiStore(db, id) - case sdk.StoreTypeIAVL: - store, err = LoadIAVLStore(db, id, rs.pruning) - return - case sdk.StoreTypeDB: - panic("dbm.DB is not a sdk.CommitStore") - case sdk.StoreTypeTransient: - _, ok := key.(*sdk.TransientStoreKey) - if !ok { - err = fmt.Errorf("invalid sdk.StoreKey for sdk.StoreTypeTransient: %s", key.String()) + + store = reflect.Zero(params.typ).Interface().(sdk.CommitKVStore) + err = store.LoadKVStoreVersion(db, id) + if err != nil { + store.SetPruning(rs.pruning) + } + + return + + // XXX: move to store subdirectories LoadKVStoreVersion + /* + switch params.typ { + case sdk.StoreTypeMulti: + panic("recursive sdk.MultiStores not yet supported") + // TODO: id? + // return NewCommitMultiStore(db, id) + case sdk.StoreTypeIAVL: + store, err = LoadIAVLStore(db, id, rs.pruning) + return + case sdk.StoreTypeDB: + panic("dbm.DB is not a sdk.CommitStore") + case sdk.StoreTypeTransient: + _, ok := key.(*sdk.TransientStoreKey) + if !ok { + err = fmt.Errorf("invalid sdk.StoreKey for sdk.StoreTypeTransient: %s", key.String()) + return + } + store = transient.NewStore() return + default: + panic(fmt.Sprintf("unrecognized store type %v", params.typ)) } - store = transient.NewStore() - return - default: - panic(fmt.Sprintf("unrecognized store type %v", params.typ)) - } + */ } -func (rs *rootMultiStore) nameToKey(name string) sdk.StoreKey { +func (rs *Store) nameToKey(name string) sdk.StoreKey { for key := range rs.storesParams { if key.Name() == name { return key @@ -365,6 +318,7 @@ func (rs *rootMultiStore) nameToKey(name string) sdk.StoreKey { type storeParams struct { key sdk.StoreKey db dbm.DB + typ reflect.Type } //---------------------------------------- @@ -401,7 +355,7 @@ func (ci commitInfo) CommitID() sdk.CommitID { // storeInfo // storeInfo contains the name and core reference for an -// underlying store. It is the leaf of the rootMultiStores top +// underlying store. It is the leaf of the Stores top // level simple merkle tree. type storeInfo struct { Name string @@ -451,14 +405,14 @@ func setLatestVersion(batch dbm.Batch, version int64) { } // Commits each store and returns a new commitInfo. -func commitStores(version int64, storeMap map[sdk.StoreKey]sdk.CommitStore) commitInfo { +func commitStores(version int64, storeMap map[sdk.StoreKey]sdk.CommitKVStore) commitInfo { storeInfos := make([]storeInfo, 0, len(storeMap)) for key, store := range storeMap { // Commit commitID := store.Commit() - if store.GetStoreType() == sdk.StoreTypeTransient { + if commitID.IsZero() { continue } @@ -484,14 +438,14 @@ func getCommitInfo(db dbm.DB, ver int64) (commitInfo, error) { cInfoKey := fmt.Sprintf(commitInfoKeyFmt, ver) cInfoBytes := db.Get([]byte(cInfoKey)) if cInfoBytes == nil { - return commitInfo{}, fmt.Errorf("failed to get rootMultiStore: no data") + return commitInfo{}, fmt.Errorf("failed to get Store: no data") } // Parse bytes. var cInfo commitInfo err := cdc.UnmarshalBinary(cInfoBytes, &cInfo) if err != nil { - return commitInfo{}, fmt.Errorf("failed to get rootMultiStore: %v", err) + return commitInfo{}, fmt.Errorf("failed to get Store: %v", err) } return cInfo, nil } diff --git a/store/rootmulti/store_test.go b/store/rootmulti/store_test.go index 74b0e39cdccb..f6f315b8f5fc 100644 --- a/store/rootmulti/store_test.go +++ b/store/rootmulti/store_test.go @@ -16,8 +16,7 @@ const useDebugDB = false func TestStoreType(t *testing.T) { db := dbm.NewMemDB() store := NewCommitMultiStore(db) - store.MountStoreWithDB( - sdk.NewKVStoreKey("store1"), sdk.StoreTypeIAVL, db) + store.MountStoreWithDB(sdk.NewKVStoreKey("store1"), db) } @@ -29,11 +28,11 @@ func TestStoreMount(t *testing.T) { key2 := sdk.NewKVStoreKey("store2") dup1 := sdk.NewKVStoreKey("store1") - require.NotPanics(t, func() { store.MountStoreWithDB(key1, sdk.StoreTypeIAVL, db) }) - require.NotPanics(t, func() { store.MountStoreWithDB(key2, sdk.StoreTypeIAVL, db) }) + require.NotPanics(t, func() { store.MountStoreWithDB(key1, db) }) + require.NotPanics(t, func() { store.MountStoreWithDB(key2, db) }) - require.Panics(t, func() { store.MountStoreWithDB(key1, sdk.StoreTypeIAVL, db) }) - require.Panics(t, func() { store.MountStoreWithDB(dup1, sdk.StoreTypeIAVL, db) }) + require.Panics(t, func() { store.MountStoreWithDB(key1, db) }) + require.Panics(t, func() { store.MountStoreWithDB(dup1, db) }) } func TestMultistoreCommitLoad(t *testing.T) { @@ -80,7 +79,7 @@ func TestMultistoreCommitLoad(t *testing.T) { // Load an older multistore and check version. ver := nCommits - 1 store = newMultiStoreWithMounts(db) - err = store.LoadVersion(ver) + err = store.LoadMultiStoreVersion(ver) require.Nil(t, err) commitID = getExpectedCommitID(store, ver) checkStore(t, store, commitID, commitID) @@ -190,31 +189,28 @@ func TestMultiStoreQuery(t *testing.T) { //----------------------------------------------------------------------- // utils -func newMultiStoreWithMounts(db dbm.DB) *rootMultiStore { +func newMultiStoreWithMounts(db dbm.DB) *Store { store := NewCommitMultiStore(db) - store.MountStoreWithDB( - sdk.NewKVStoreKey("store1"), sdk.StoreTypeIAVL, nil) - store.MountStoreWithDB( - sdk.NewKVStoreKey("store2"), sdk.StoreTypeIAVL, nil) - store.MountStoreWithDB( - sdk.NewKVStoreKey("store3"), sdk.StoreTypeIAVL, nil) + store.MountStoreWithDB(sdk.NewKVStoreKey("store1"), nil) + store.MountStoreWithDB(sdk.NewKVStoreKey("store2"), nil) + store.MountStoreWithDB(sdk.NewKVStoreKey("store3"), nil) return store } -func checkStore(t *testing.T, store *rootMultiStore, expect, got sdk.CommitID) { +func checkStore(t *testing.T, store *Store, expect, got sdk.CommitID) { require.Equal(t, expect, got) require.Equal(t, expect, store.LastCommitID()) } -func getExpectedCommitID(store *rootMultiStore, ver int64) sdk.CommitID { +func getExpectedCommitID(store *Store, ver int64) sdk.CommitID { return sdk.CommitID{ Version: ver, Hash: hashStores(store.stores), } } -func hashStores(stores map[sdk.StoreKey]sdk.CommitStore) []byte { +func hashStores(stores map[sdk.StoreKey]sdk.CommitKVStore) []byte { m := make(map[string]merkle.Hasher, len(stores)) for key, store := range stores { name := key.Name() diff --git a/store/rootmulti/wire.go b/store/rootmulti/wire.go index 615f81c61eb0..8d6d936160ff 100644 --- a/store/rootmulti/wire.go +++ b/store/rootmulti/wire.go @@ -4,4 +4,4 @@ import ( "github.com/cosmos/cosmos-sdk/codec" ) -var cdc = codec.NewCodec() +var cdc = codec.New() diff --git a/store/trace/store.go b/store/trace/store.go index 6f1b5effb662..6f3caed24c07 100644 --- a/store/trace/store.go +++ b/store/trace/store.go @@ -18,16 +18,15 @@ const ( ) type ( - // TraceKVStore implements the KVStore interface with tracing enabled. + // Store implements the KVStore interface with tracing enabled. // Operations are traced on each core KVStore call and written to the // underlying io.writer. // // TODO: Should we use a buffered writer and implement Commit on - // TraceKVStore? - TraceKVStore struct { - parent sdk.KVStore - writer io.Writer - context sdk.TraceContext + // Store? + Store struct { + parent sdk.KVStore + tracer *sdk.Tracer } // operation represents an IO operation @@ -42,56 +41,56 @@ type ( } ) -// NewTraceKVStore returns a reference to a new traceKVStore given a parent +// NewStore returns a reference to a new traceKVStore given a parent // KVStore implementation and a buffered writer. -func NewStore(parent sdk.KVStore, writer io.Writer, tc sdk.TraceContext) *TraceKVStore { - return &TraceKVStore{parent: parent, writer: writer, context: tc} +func NewStore(parent sdk.KVStore, tracer *sdk.Tracer) *Store { + return &Store{parent: parent, tracer: tracer} } // Get implements the KVStore interface. It traces a read operation and // delegates a Get call to the parent KVStore. -func (tkv *TraceKVStore) Get(key []byte) []byte { +func (tkv *Store) Get(key []byte) []byte { value := tkv.parent.Get(key) - writeOperation(tkv.writer, readOp, tkv.context, key, value) + writeOperation(tkv.tracer, readOp, key, value) return value } // Set implements the KVStore interface. It traces a write operation and // delegates the Set call to the parent KVStore. -func (tkv *TraceKVStore) Set(key []byte, value []byte) { - writeOperation(tkv.writer, writeOp, tkv.context, key, value) +func (tkv *Store) Set(key []byte, value []byte) { + writeOperation(tkv.tracer, writeOp, key, value) tkv.parent.Set(key, value) } // Delete implements the KVStore interface. It traces a write operation and // delegates the Delete call to the parent KVStore. -func (tkv *TraceKVStore) Delete(key []byte) { - writeOperation(tkv.writer, deleteOp, tkv.context, key, nil) +func (tkv *Store) Delete(key []byte) { + writeOperation(tkv.tracer, deleteOp, key, nil) tkv.parent.Delete(key) } // Has implements the KVStore interface. It delegates the Has call to the // parent KVStore. -func (tkv *TraceKVStore) Has(key []byte) bool { +func (tkv *Store) Has(key []byte) bool { return tkv.parent.Has(key) } // Iterator implements the KVStore interface. It delegates the Iterator call // the to the parent KVStore. -func (tkv *TraceKVStore) Iterator(start, end []byte) sdk.Iterator { +func (tkv *Store) Iterator(start, end []byte) sdk.Iterator { return tkv.iterator(start, end, true) } // ReverseIterator implements the KVStore interface. It delegates the // ReverseIterator call the to the parent KVStore. -func (tkv *TraceKVStore) ReverseIterator(start, end []byte) sdk.Iterator { +func (tkv *Store) ReverseIterator(start, end []byte) sdk.Iterator { return tkv.iterator(start, end, false) } // iterator facilitates iteration over a KVStore. It delegates the necessary // calls to it's parent KVStore. -func (tkv *TraceKVStore) iterator(start, end []byte, ascending bool) sdk.Iterator { +func (tkv *Store) iterator(start, end []byte, ascending bool) sdk.Iterator { var parent sdk.Iterator if ascending { @@ -100,17 +99,16 @@ func (tkv *TraceKVStore) iterator(start, end []byte, ascending bool) sdk.Iterato parent = tkv.parent.ReverseIterator(start, end) } - return newTraceIterator(tkv.writer, parent, tkv.context) + return newTraceIterator(tkv.tracer, parent) } type traceIterator struct { - parent sdk.Iterator - writer io.Writer - context sdk.TraceContext + parent sdk.Iterator + tracer *sdk.Tracer } -func newTraceIterator(w io.Writer, parent sdk.Iterator, tc sdk.TraceContext) sdk.Iterator { - return &traceIterator{writer: w, parent: parent, context: tc} +func newTraceIterator(tracer *sdk.Tracer, parent sdk.Iterator) sdk.Iterator { + return &traceIterator{parent: parent, tracer: tracer} } // Domain implements the Iterator interface. @@ -132,7 +130,7 @@ func (ti *traceIterator) Next() { func (ti *traceIterator) Key() []byte { key := ti.parent.Key() - writeOperation(ti.writer, iterKeyOp, ti.context, key, nil) + writeOperation(ti.tracer, iterKeyOp, key, nil) return key } @@ -140,7 +138,7 @@ func (ti *traceIterator) Key() []byte { func (ti *traceIterator) Value() []byte { value := ti.parent.Value() - writeOperation(ti.writer, iterValueOp, ti.context, nil, value) + writeOperation(ti.tracer, iterValueOp, nil, value) return value } @@ -149,28 +147,23 @@ func (ti *traceIterator) Close() { ti.parent.Close() } -// CacheWrap implements the KVStore interface. It panics as a TraceKVStore +// CacheWrap implements the KVStore interface. It panics as a Store // cannot be cache wrapped. -func (tkv *TraceKVStore) CacheWrap() sdk.CacheWrap { - panic("cannot CacheWrap a TraceKVStore") -} - -// CacheWrapWithTrace implements the KVStore interface. It panics as a -// TraceKVStore cannot be cache wrapped. -func (tkv *TraceKVStore) CacheWrapWithTrace(_ io.Writer, _ sdk.TraceContext) sdk.CacheWrap { - panic("cannot CacheWrapWithTrace a TraceKVStore") +func (tkv *Store) CacheWrap() sdk.CacheKVStore { + panic("cannot CacheWrap a Store") } // writeOperation writes a KVStore operation to the underlying io.Writer as // JSON-encoded data where the key/value pair is base64 encoded. // nolint: errcheck -func writeOperation(w io.Writer, op operation, tc sdk.TraceContext, key, value []byte) { +func writeOperation(tracer *sdk.Tracer, op operation, key, value []byte) { traceOp := traceOperation{ Operation: op, Key: base64.StdEncoding.EncodeToString(key), Value: base64.StdEncoding.EncodeToString(value), } + tc := tracer.Context if tc != nil { traceOp.Metadata = tc } @@ -180,6 +173,7 @@ func writeOperation(w io.Writer, op operation, tc sdk.TraceContext, key, value [ panic(fmt.Sprintf("failed to serialize trace operation: %v", err)) } + w := tracer.Writer if _, err := w.Write(raw); err != nil { panic(fmt.Sprintf("failed to write trace operation: %v", err)) } diff --git a/store/trace/store_test.go b/store/trace/store_test.go index f276247ca4e6..708793211360 100644 --- a/store/trace/store_test.go +++ b/store/trace/store_test.go @@ -10,6 +10,8 @@ import ( dbm "github.com/tendermint/tendermint/libs/db" sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/cosmos-sdk/store/dbadapter" ) var kvPairs = []sdk.KVPair{ @@ -18,8 +20,8 @@ var kvPairs = []sdk.KVPair{ {Key: keyFmt(3), Value: valFmt(3)}, } -func newTraceKVStore(w io.Writer) *TraceKVStore { - store := newEmptyTraceKVStore(w) +func newStore(w io.Writer) *Store { + store := newEmptyStore(w) for _, kvPair := range kvPairs { store.Set(kvPair.Key, kvPair.Value) @@ -28,14 +30,14 @@ func newTraceKVStore(w io.Writer) *TraceKVStore { return store } -func newEmptyTraceKVStore(w io.Writer) *TraceKVStore { +func newEmptyStore(w io.Writer) *Store { memDB := dbStoreAdapter{dbm.NewMemDB()} tc := sdk.TraceContext(map[string]interface{}{"blockHeight": 64}) - return NewTraceKVStore(memDB, w, tc) + return NewStore(memDB, w, tc) } -func TestTraceKVStoreGet(t *testing.T) { +func TestStoreGet(t *testing.T) { testCases := []struct { key []byte expectedValue []byte @@ -61,7 +63,7 @@ func TestTraceKVStoreGet(t *testing.T) { for _, tc := range testCases { var buf bytes.Buffer - store := newTraceKVStore(&buf) + store := newStore(&buf) buf.Reset() value := store.Get(tc.key) @@ -70,7 +72,7 @@ func TestTraceKVStoreGet(t *testing.T) { } } -func TestTraceKVStoreSet(t *testing.T) { +func TestStoreSet(t *testing.T) { testCases := []struct { key []byte value []byte @@ -91,7 +93,7 @@ func TestTraceKVStoreSet(t *testing.T) { for _, tc := range testCases { var buf bytes.Buffer - store := newEmptyTraceKVStore(&buf) + store := newEmptyStore(&buf) buf.Reset() store.Set(tc.key, tc.value) @@ -99,7 +101,7 @@ func TestTraceKVStoreSet(t *testing.T) { } } -func TestTraceKVStoreDelete(t *testing.T) { +func TestStoreDelete(t *testing.T) { testCases := []struct { key []byte expectedOut string @@ -117,7 +119,7 @@ func TestTraceKVStoreDelete(t *testing.T) { for _, tc := range testCases { var buf bytes.Buffer - store := newTraceKVStore(&buf) + store := newStore(&buf) buf.Reset() store.Delete(tc.key) @@ -125,7 +127,7 @@ func TestTraceKVStoreDelete(t *testing.T) { } } -func TestTraceKVStoreHas(t *testing.T) { +func TestStoreHas(t *testing.T) { testCases := []struct { key []byte expected bool @@ -143,7 +145,7 @@ func TestTraceKVStoreHas(t *testing.T) { for _, tc := range testCases { var buf bytes.Buffer - store := newTraceKVStore(&buf) + store := newStore(&buf) buf.Reset() ok := store.Has(tc.key) @@ -151,10 +153,10 @@ func TestTraceKVStoreHas(t *testing.T) { } } -func TestTestTraceKVStoreIterator(t *testing.T) { +func TestTestStoreIterator(t *testing.T) { var buf bytes.Buffer - store := newTraceKVStore(&buf) + store := newStore(&buf) iterator := store.Iterator(nil, nil) s, e := iterator.Domain() @@ -207,10 +209,10 @@ func TestTestTraceKVStoreIterator(t *testing.T) { require.NotPanics(t, iterator.Close) } -func TestTestTraceKVStoreReverseIterator(t *testing.T) { +func TestTestStoreReverseIterator(t *testing.T) { var buf bytes.Buffer - store := newTraceKVStore(&buf) + store := newStore(&buf) iterator := store.ReverseIterator(nil, nil) s, e := iterator.Domain() @@ -263,23 +265,17 @@ func TestTestTraceKVStoreReverseIterator(t *testing.T) { require.NotPanics(t, iterator.Close) } -func TestTraceKVStorePrefix(t *testing.T) { - store := newEmptyTraceKVStore(nil) - pStore := prefix.NewStore(store, []byte("trace_prefix")) - require.IsType(t, prefix.Store{}, pStore) -} - -func TestTraceKVStoreGetStoreType(t *testing.T) { +func TestStoreGetStoreType(t *testing.T) { memDB := dbStoreAdapter{dbm.NewMemDB()} store := NewEmptyStore(nil) require.Equal(t, memDB.GetStoreType(), store.GetStoreType()) } -func TestTraceKVStoreCacheWrap(t *testing.T) { - store := newEmptyTraceKVStore(nil) +func TestStoreCacheWrap(t *testing.T) { + store := newEmptyStore(nil) require.Panics(t, func() { store.CacheWrap() }) } -func TestTraceKVStoreCacheWrapWithTrace(t *testing.T) { - store := newEmptyTraceKVStore(nil) +func TestStoreCacheWrapWithTrace(t *testing.T) { + store := newEmptyStore(nil) require.Panics(t, func() { store.CacheWrapWithTrace(nil, nil) }) } diff --git a/store/utils/utils.go b/store/utils/utils.go new file mode 100644 index 000000000000..087c160fba62 --- /dev/null +++ b/store/utils/utils.go @@ -0,0 +1,10 @@ +package utils + +import ( + "fmt" +) + +func bz(s string) []byte { return []byte(s) } + +func KeyFmt(i int) []byte { return bz(fmt.Sprintf("key%0.8d", i)) } +func ValFmt(i int) []byte { return bz(fmt.Sprintf("value%0.8d", i)) } diff --git a/types/context.go b/types/context.go index 29c7f4d8b7a4..3abda04e2c1a 100644 --- a/types/context.go +++ b/types/context.go @@ -91,9 +91,6 @@ func (c Context) WithValue(key interface{}, value interface{}) Context { func (c Context) WithCloner(key interface{}, value cloner) Context { return c.withValue(key, value) } -func (c Context) WithCacheWrapper(key interface{}, value CacheWrapper) Context { - return c.withValue(key, value) -} func (c Context) WithProtoMsg(key interface{}, value proto.Message) Context { return c.withValue(key, value) } @@ -146,6 +143,7 @@ const ( // NOTE: Do not expose MultiStore. // MultiStore exposes all the keys. // Instead, pass the context and the store key. +// Returning CacheWrapperMultiStore to cachewrap it func (c Context) multiStore() MultiStore { return c.Value(contextKeyMultiStore).(MultiStore) } @@ -230,7 +228,7 @@ func (c Context) WithMinimumFees(minFees Coins) Context { // Cache the multistore and return a new cached context. The cached context is // written to the context when writeCache is called. func (c Context) CacheContext() (cc Context, writeCache func()) { - cms := c.multiStore().CacheMultiStore() + cms := c.multiStore().CacheWrap() cc = c.WithMultiStore(cms) return cc, cms.Write } diff --git a/types/gas.go b/types/gas.go index 7b034746703b..f94a25fd204d 100644 --- a/types/gas.go +++ b/types/gas.go @@ -76,8 +76,8 @@ func (g *infiniteGasMeter) ConsumeGas(amount Gas, descriptor string) { // GasConfig defines gas cost for each operation on KVStores type GasConfig struct { - HasCost Gas - DeleteCost Gas + HasCostFlat Gas + DeleteCostFlat Gas ReadCostFlat Gas ReadCostPerByte Gas WriteCostFlat Gas @@ -89,8 +89,8 @@ type GasConfig struct { // KVGasConfig returns a default gas config for KVStores. func KVGasConfig() GasConfig { return GasConfig{ - HasCost: 10, - DeleteCost: 10, + HasCostFlat: 10, + DeleteCostFlat: 10, ReadCostFlat: 10, ReadCostPerByte: 1, WriteCostFlat: 10, @@ -105,3 +105,47 @@ func TransientGasConfig() GasConfig { // TODO: define gasconfig for transient stores return KVGasConfig() } + +type GasTank struct { + GasMeter + Config GasConfig +} + +func NewGasTank(meter GasMeter, config GasConfig) *GasTank { + return &GasTank{ + GasMeter: meter, + Config: config, + } +} + +func (tank *GasTank) HasFlat() { + tank.GasMeter.ConsumeGas(tank.Config.HasCostFlat, "HasFlat") +} + +func (tank *GasTank) DeleteFlat() { + tank.GasMeter.ConsumeGas(tank.Config.DeleteCostFlat, "DeleteFlat") +} + +func (tank *GasTank) ReadFlat() { + tank.GasMeter.ConsumeGas(tank.Config.ReadCostFlat, "ReadFlat") +} + +func (tank *GasTank) ReadBytes(length int) { + tank.GasMeter.ConsumeGas(tank.Config.ReadCostPerByte*int64(length), "ReadPerByte") +} + +func (tank *GasTank) WriteFlat() { + tank.GasMeter.ConsumeGas(tank.Config.WriteCostFlat, "WriteFlat") +} + +func (tank *GasTank) WriteBytes(length int) { + tank.GasMeter.ConsumeGas(tank.Config.WriteCostPerByte*int64(length), "WritePerByte") +} + +func (tank *GasTank) ValueBytes(length int) { + tank.GasMeter.ConsumeGas(tank.Config.ValueCostPerByte*int64(length), "ValuePerByte") +} + +func (tank *GasTank) IterNextFlat() { + tank.GasMeter.ConsumeGas(tank.Config.IterNextCostFlat, "IterNext") +} diff --git a/types/store.go b/types/store.go index ddc48328de44..9184ae7b2225 100644 --- a/types/store.go +++ b/types/store.go @@ -2,7 +2,6 @@ package types import ( "fmt" - "io" abci "github.com/tendermint/tendermint/abci/types" cmn "github.com/tendermint/tendermint/libs/common" @@ -25,23 +24,14 @@ const ( PruneNothing PruningStrategy = iota ) -type Store interface { //nolint - CacheWrapper -} - -// something that can persist to disk -type Committer interface { +// Stores of MultiStore must implement CommitStore. +type CommitStore interface { + // CONTRACT: return zero CommitID to skip writing Commit() CommitID LastCommitID() CommitID SetPruning(PruningStrategy) } -// Stores of MultiStore must implement CommitStore. -type CommitStore interface { - Committer - Store -} - // Queryable allows a Store to expose internal state to the abci.Query // interface. Multistore can route requests to the proper Store. // @@ -54,42 +44,31 @@ type Queryable interface { // MultiStore type MultiStore interface { //nolint - Store - - // Cache wrap MultiStore. - // NOTE: Caller should probably not call .Write() on each, but - // call CacheMultiStore.Write(). - CacheMultiStore() CacheMultiStore - // Convenience for fetching substores. - GetStore(StoreKey) Store GetKVStore(StoreKey) KVStore - // TracingEnabled returns if tracing is enabled for the MultiStore. - TracingEnabled() bool - - // WithTracer sets the tracer for the MultiStore that the underlying - // stores will utilize to trace operations. A MultiStore is returned. - WithTracer(w io.Writer) MultiStore + // TODO: recursive multistore not yet supported + // GetMultiStore(StoreKey) MultiStore - // WithTracingContext sets the tracing context for a MultiStore. It is - // implied that the caller should update the context when necessary between - // tracing operations. A MultiStore is returned. - WithTracingContext(TraceContext) MultiStore + GetTracer() *Tracer - // ResetTraceContext resets the current tracing context. - ResetTraceContext() MultiStore + // CacheWrap cache wraps + // Having this method here because there is currently no + // implementation of MultiStore that panics on CacheWrap(). + // Move this method to CacheWrapperMultiStore when needed + CacheWrap() CacheMultiStore } // From MultiStore.CacheMultiStore().... type CacheMultiStore interface { MultiStore + Write() // Writes operations to underlying KVStore } // A non-cache MultiStore. type CommitMultiStore interface { - Committer + CommitStore MultiStore // Mount a store of type using the given db. @@ -102,6 +81,10 @@ type CommitMultiStore interface { // Panics on a nil key. GetCommitKVStore(key StoreKey) CommitKVStore + // Panics on a nil key. + // TODO: recursive multistore not yet supported + // GetCommitMultiStore(key StoreKey) CommitMultiStore + // Load the latest persisted version. Called once after all // calls to Mount*Store() are complete. LoadLatestVersion() error @@ -110,7 +93,7 @@ type CommitMultiStore interface { // version, or when the last commit attempt didn't complete, // the next commit after loading must be idempotent (return the // same commit id). Otherwise the behavior is undefined. - LoadVersion(ver int64) error + LoadMultiStoreVersion(ver int64) error } //---------subsp------------------------------- @@ -118,8 +101,6 @@ type CommitMultiStore interface { // KVStore is a simple interface to get/set data type KVStore interface { - Store - // Get returns nil iff key doesn't exist. Panics on nil key. Get(key []byte) []byte @@ -150,6 +131,7 @@ type KVStore interface { // TODO Not yet implemented. // GetSubKVStore(key *storeKey) KVStore + } // Alias iterator to db's Iterator for convenience. @@ -165,11 +147,18 @@ func KVStoreReversePrefixIterator(kvs KVStore, prefix []byte) Iterator { return kvs.ReverseIterator(prefix, PrefixEndBytes(prefix)) } +type CacheWrapperKVStore interface { + KVStore + + // CacheWrap cache wraps + CacheWrap() CacheKVStore +} + // CacheKVStore cache-wraps a KVStore. After calling .Write() on // the CacheKVStore, all previously created CacheKVStores on the // object expire. type CacheKVStore interface { - KVStore + CacheWrapperKVStore // Writes operations to underlying KVStore Write() @@ -177,39 +166,14 @@ type CacheKVStore interface { // Stores of MultiStore must implement CommitStore. type CommitKVStore interface { - Committer - KVStore -} - -type MountableStore interface { CommitStore - Load() -} - -//---------------------------------------- -// CacheWrap - -// CacheWrap makes the most appropriate cache-wrap. For example, -// IAVLStore.CacheWrap() returns a CacheKVStore. CacheWrap should not return -// a Committer, since Commit cache-wraps make no sense. It can return KVStore, -// HeapStore, SpaceStore, etc. -type CacheWrap interface { - // Write syncs with the underlying store. - Write() - - // CacheWrap recursively wraps again. - CacheWrap() CacheWrap - - // CacheWrapWithTrace recursively wraps again with tracing enabled. - CacheWrapWithTrace(w io.Writer, tc TraceContext) CacheWrap -} - -type CacheWrapper interface { //nolint - // CacheWrap cache wraps. - CacheWrap() CacheWrap + KVStore - // CacheWrapWithTrace cache wraps with tracing enabled. - CacheWrapWithTrace(w io.Writer, tc TraceContext) CacheWrap + // Load a specific persisted version. When you load an old + // version, or when the last commit attempt didn't complete, + // the next commit after loading must be idempotent (return the + // same commit id). Otherwise the behavior is undefined. + LoadKVStoreVersion(db dbm.DB, id CommitID) error } //---------------------------------------- @@ -320,9 +284,3 @@ func (key *TransientStoreKey) String() string { // key-value result for iterator queries type KVPair cmn.KVPair - -//---------------------------------------- - -// TraceContext contains TraceKVStore context data. It will be written with -// every trace operation. -type TraceContext map[string]interface{} diff --git a/types/trace.go b/types/trace.go new file mode 100644 index 000000000000..9b2515c8a489 --- /dev/null +++ b/types/trace.go @@ -0,0 +1,45 @@ +package types + +import ( + "io" +) + +//---------------------------------------- + +// TraceContext contains TraceKVStore context data. It will be written with +// every trace operation. +type TraceContext map[string]interface{} + +type Tracer struct { + Writer io.Writer + Context TraceContext +} + +// Enabled returns if tracing is enabled. +func (t *Tracer) Enabled() bool { + return t.Context != nil +} + +// WithTracer sets the writer that the underlying stores of the owner +// multistore will utilize to trace operations. +func (t *Tracer) SetWriter(w io.Writer) { + t.Writer = w +} + +// WithContext sets the tracing context for a Tracer. It is implied that +// the caller should update the context when necessary between tracing +// operations. +func (t *Tracer) WithContext(tc TraceContext) { + if t.Context != nil { + for k, v := range tc { + t.Context[k] = v + } + } else { + t.Context = tc + } +} + +// ResetContext resets the current tracing context. +func (t *Tracer) ResetContext() { + t.Context = nil +} From 9b0612ed4b48ccb506e3e74b7a22782bab2e2813 Mon Sep 17 00:00:00 2001 From: mossid Date: Sun, 16 Sep 2018 05:15:26 +0900 Subject: [PATCH 04/22] mv types/{gas.go, trace.go, store.go} to store/types/ --- store/cache/cachemergeiterator.go | 10 +- store/cache/store.go | 32 +- store/cache/store_test.go | 20 +- store/cachemulti/store.go | 27 +- store/dbadapter/store.go | 6 +- store/gas/store.go | 38 +-- store/gas/store_test.go | 20 +- store/iavl/store.go | 56 ++-- store/prefix/store.go | 24 +- store/prefix/store_test.go | 40 +-- store/rootmulti/store.go | 129 ++++---- store/trace/store.go | 26 +- store/trace/store_test.go | 20 +- .../transient/{transientstore.go => store.go} | 10 +- .../{transientstore_test.go => store_test.go} | 2 +- store/types/errors.go | 1 + {types => store/types}/gas.go | 0 {types => store/types}/gas_test.go | 0 {types => store/types}/trace.go | 0 store/types/types.go | 280 +++++++++++++++++ types/store.go | 283 +----------------- 21 files changed, 512 insertions(+), 512 deletions(-) rename store/transient/{transientstore.go => store.go} (68%) rename store/transient/{transientstore_test.go => store_test.go} (90%) create mode 100644 store/types/errors.go rename {types => store/types}/gas.go (100%) rename {types => store/types}/gas_test.go (100%) rename {types => store/types}/trace.go (100%) create mode 100644 store/types/types.go diff --git a/store/cache/cachemergeiterator.go b/store/cache/cachemergeiterator.go index cc0d0be511fb..b5d50d2deb48 100644 --- a/store/cache/cachemergeiterator.go +++ b/store/cache/cachemergeiterator.go @@ -3,7 +3,7 @@ package cache import ( "bytes" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/store/types" ) // cacheMergeIterator merges a parent Iterator and a cache Iterator. @@ -14,14 +14,14 @@ import ( // // TODO: Optimize by memoizing. type cacheMergeIterator struct { - parent sdk.Iterator - cache sdk.Iterator + parent types.Iterator + cache types.Iterator ascending bool } -var _ sdk.Iterator = (*cacheMergeIterator)(nil) +var _ types.Iterator = (*cacheMergeIterator)(nil) -func newCacheMergeIterator(parent, cache sdk.Iterator, ascending bool) *cacheMergeIterator { +func newCacheMergeIterator(parent, cache types.Iterator, ascending bool) *cacheMergeIterator { iter := &cacheMergeIterator{ parent: parent, cache: cache, diff --git a/store/cache/store.go b/store/cache/store.go index 147631183200..50dbcea681ee 100644 --- a/store/cache/store.go +++ b/store/cache/store.go @@ -7,7 +7,7 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/store/types" ) // If value is nil but deleted is false, it means the parent doesn't have the @@ -18,24 +18,24 @@ type cValue struct { dirty bool } -// Store wraps an in-memory cache around an underlying sdk.KVStore. +// Store wraps an in-memory cache around an underlying types.KVStore. type Store struct { mtx sync.Mutex cache map[string]cValue - parent sdk.KVStore + parent types.KVStore } -var _ sdk.CacheKVStore = (*Store)(nil) +var _ types.CacheKVStore = (*Store)(nil) // nolint -func NewStore(parent sdk.KVStore) *Store { +func NewStore(parent types.KVStore) *Store { return &Store{ cache: make(map[string]cValue), parent: parent, } } -// Implements sdk.KVStore. +// Implements types.KVStore. func (ci *Store) Get(key []byte) (value []byte) { ci.mtx.Lock() defer ci.mtx.Unlock() @@ -52,7 +52,7 @@ func (ci *Store) Get(key []byte) (value []byte) { return value } -// Implements sdk.KVStore. +// Implements types.KVStore. func (ci *Store) Set(key []byte, value []byte) { ci.mtx.Lock() defer ci.mtx.Unlock() @@ -61,13 +61,13 @@ func (ci *Store) Set(key []byte, value []byte) { ci.setCacheValue(key, value, false, true) } -// Implements sdk.KVStore. +// Implements types.KVStore. func (ci *Store) Has(key []byte) bool { value := ci.Get(key) return value != nil } -// Implements sdk.KVStore. +// Implements types.KVStore. func (ci *Store) Delete(key []byte) { ci.mtx.Lock() defer ci.mtx.Unlock() @@ -109,25 +109,25 @@ func (ci *Store) Write() { ci.cache = make(map[string]cValue) } -func (ci *Store) CacheWrap() sdk.CacheKVStore { +func (ci *Store) CacheWrap() types.CacheKVStore { return NewStore(ci) } //---------------------------------------- // Iteration -// Implements sdk.KVStore. -func (ci *Store) Iterator(start, end []byte) sdk.Iterator { +// Implements types.KVStore. +func (ci *Store) Iterator(start, end []byte) types.Iterator { return ci.iterator(start, end, true) } -// Implements sdk.KVStore. -func (ci *Store) ReverseIterator(start, end []byte) sdk.Iterator { +// Implements types.KVStore. +func (ci *Store) ReverseIterator(start, end []byte) types.Iterator { return ci.iterator(start, end, false) } -func (ci *Store) iterator(start, end []byte, ascending bool) sdk.Iterator { - var parent, cache sdk.Iterator +func (ci *Store) iterator(start, end []byte, ascending bool) types.Iterator { + var parent, cache types.Iterator if ascending { parent = ci.parent.Iterator(start, end) diff --git a/store/cache/store_test.go b/store/cache/store_test.go index 6ce064d28366..be257ab98876 100644 --- a/store/cache/store_test.go +++ b/store/cache/store_test.go @@ -7,14 +7,14 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" dbm "github.com/tendermint/tendermint/libs/db" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/store/types" "github.com/cosmos/cosmos-sdk/store/cache" "github.com/cosmos/cosmos-sdk/store/dbadapter" "github.com/cosmos/cosmos-sdk/store/utils" ) -func newCacheKVStore() sdk.CacheKVStore { +func newCacheKVStore() types.CacheKVStore { mem := dbadapter.NewStore(dbm.NewMemDB()) return cache.NewStore(mem) } @@ -314,7 +314,7 @@ func randInt(n int) int { } // useful for replaying a error case if we find one -func doOp(st sdk.CacheKVStore, truth dbm.DB, op int, args ...int) { +func doOp(st types.CacheKVStore, truth dbm.DB, op int, args ...int) { switch op { case opSet: k := args[0] @@ -337,7 +337,7 @@ func doOp(st sdk.CacheKVStore, truth dbm.DB, op int, args ...int) { } } -func doRandomOp(st sdk.CacheKVStore, truth dbm.DB, maxKey int) { +func doRandomOp(st types.CacheKVStore, truth dbm.DB, maxKey int) { r := randInt(totalOps) switch r { case opSet: @@ -364,7 +364,7 @@ func doRandomOp(st sdk.CacheKVStore, truth dbm.DB, maxKey int) { //------------------------------------------------------------------------------------------- // iterate over whole domain -func assertIterateDomain(t *testing.T, st sdk.KVStore, expectedN int) { +func assertIterateDomain(t *testing.T, st types.KVStore, expectedN int) { itr := st.Iterator(nil, nil) var i = 0 for ; itr.Valid(); itr.Next() { @@ -376,7 +376,7 @@ func assertIterateDomain(t *testing.T, st sdk.KVStore, expectedN int) { require.Equal(t, expectedN, i) } -func assertIterateDomainCheck(t *testing.T, st sdk.KVStore, mem dbm.DB, r []keyRange) { +func assertIterateDomainCheck(t *testing.T, st types.KVStore, mem dbm.DB, r []keyRange) { // iterate over each and check they match the other itr := st.Iterator(nil, nil) itr2 := mem.Iterator(nil, nil) // ground truth @@ -406,7 +406,7 @@ func assertIterateDomainCheck(t *testing.T, st sdk.KVStore, mem dbm.DB, r []keyR require.False(t, itr2.Valid()) } -func assertIterateDomainCompare(t *testing.T, st sdk.KVStore, mem dbm.DB) { +func assertIterateDomainCompare(t *testing.T, st types.KVStore, mem dbm.DB) { // iterate over each and check they match the other itr := st.Iterator(nil, nil) itr2 := mem.Iterator(nil, nil) // ground truth @@ -414,7 +414,7 @@ func assertIterateDomainCompare(t *testing.T, st sdk.KVStore, mem dbm.DB) { checkIterators(t, itr2, itr) } -func checkIterators(t *testing.T, itr, itr2 sdk.Iterator) { +func checkIterators(t *testing.T, itr, itr2 types.Iterator) { for ; itr.Valid(); itr.Next() { require.True(t, itr2.Valid()) k, v := itr.Key(), itr.Value() @@ -429,14 +429,14 @@ func checkIterators(t *testing.T, itr, itr2 sdk.Iterator) { //-------------------------------------------------------- -func setRange(st sdk.KVStore, mem dbm.DB, start, end int) { +func setRange(st types.KVStore, mem dbm.DB, start, end int) { for i := start; i < end; i++ { st.Set(utils.KeyFmt(i), utils.ValFmt(i)) mem.Set(utils.KeyFmt(i), utils.ValFmt(i)) } } -func deleteRange(st sdk.KVStore, mem dbm.DB, start, end int) { +func deleteRange(st types.KVStore, mem dbm.DB, start, end int) { for i := start; i < end; i++ { st.Delete(utils.KeyFmt(i)) mem.Delete(utils.KeyFmt(i)) diff --git a/store/cachemulti/store.go b/store/cachemulti/store.go index 9ef0cabb371e..bc77df008c4a 100644 --- a/store/cachemulti/store.go +++ b/store/cachemulti/store.go @@ -3,11 +3,10 @@ package cachemulti import ( dbm "github.com/tendermint/tendermint/libs/db" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/store/cache" "github.com/cosmos/cosmos-sdk/store/dbadapter" "github.com/cosmos/cosmos-sdk/store/trace" + "github.com/cosmos/cosmos-sdk/store/types" ) //---------------------------------------- @@ -16,19 +15,19 @@ import ( // Store holds many cache-wrapped stores. // Implements MultiStore. type Store struct { - db sdk.CacheKVStore - stores map[sdk.StoreKey]sdk.CacheKVStore - keysByName map[string]sdk.StoreKey + db types.CacheKVStore + stores map[types.StoreKey]types.CacheKVStore + keysByName map[string]types.StoreKey - tracer *sdk.Tracer + tracer *types.Tracer } -var _ sdk.CacheMultiStore = Store{} +var _ types.CacheMultiStore = Store{} -func NewStore(db dbm.DB, keysByName map[string]sdk.StoreKey, stores map[sdk.StoreKey]sdk.CommitKVStore, tracer *sdk.Tracer) Store { +func NewStore(db dbm.DB, keysByName map[string]types.StoreKey, stores map[types.StoreKey]types.CommitKVStore, tracer *types.Tracer) Store { cms := Store{ db: cache.NewStore(dbadapter.NewStore(db)), - stores: make(map[sdk.StoreKey]sdk.CacheKVStore, len(stores)), + stores: make(map[types.StoreKey]types.CacheKVStore, len(stores)), keysByName: keysByName, tracer: tracer, } @@ -47,7 +46,7 @@ func NewStore(db dbm.DB, keysByName map[string]sdk.StoreKey, stores map[sdk.Stor func newCacheMultiStoreFromCMS(cms Store) Store { cms2 := Store{ db: cache.NewStore(cms.db), - stores: make(map[sdk.StoreKey]sdk.CacheKVStore, len(cms.stores)), + stores: make(map[types.StoreKey]types.CacheKVStore, len(cms.stores)), tracer: cms.tracer, } @@ -63,7 +62,7 @@ func newCacheMultiStoreFromCMS(cms Store) Store { } // Implements MultiStore -func (cms Store) GetTracer() *sdk.Tracer { +func (cms Store) GetTracer() *types.Tracer { return cms.tracer } @@ -76,11 +75,11 @@ func (cms Store) Write() { } // Implements MultiStore. -func (cms Store) CacheWrap() sdk.CacheMultiStore { +func (cms Store) CacheWrap() types.CacheMultiStore { return newCacheMultiStoreFromCMS(cms) } // Implements MultiStore. -func (cms Store) GetKVStore(key sdk.StoreKey) sdk.KVStore { - return cms.stores[key].(sdk.KVStore) +func (cms Store) GetKVStore(key types.StoreKey) types.KVStore { + return cms.stores[key].(types.KVStore) } diff --git a/store/dbadapter/store.go b/store/dbadapter/store.go index f72b043b7859..b46e19561d4f 100644 --- a/store/dbadapter/store.go +++ b/store/dbadapter/store.go @@ -1,10 +1,10 @@ package dbadapter import ( - sdk "github.com/cosmos/cosmos-sdk/types" dbm "github.com/tendermint/tendermint/libs/db" "github.com/cosmos/cosmos-sdk/store/cache" + "github.com/cosmos/cosmos-sdk/store/types" ) // Wrapper type for dbm.Db with implementation of KVStore @@ -17,9 +17,9 @@ func NewStore(parent dbm.DB) Store { } // Implements KVStore. -func (dsa Store) CacheWrap() sdk.CacheKVStore { +func (dsa Store) CacheWrap() types.CacheKVStore { return cache.NewStore(dsa) } // dbm.DB implements KVStore so we can CacheKVStore it. -var _ sdk.KVStore = Store{} +var _ types.KVStore = Store{} diff --git a/store/gas/store.go b/store/gas/store.go index f7264a498373..bb81c9af8542 100644 --- a/store/gas/store.go +++ b/store/gas/store.go @@ -1,21 +1,21 @@ package gas import ( - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/store/types" ) -var _ sdk.KVStore = &gasKVStore{} +var _ types.KVStore = &gasKVStore{} // gasKVStore applies gas tracking to an underlying KVStore. It implements the // KVStore interface. type gasKVStore struct { - tank *sdk.GasTank - parent sdk.KVStore + tank *types.GasTank + parent types.KVStore } // NewGasKVStore returns a reference to a new GasKVStore. // nolint -func NewStore(tank *sdk.GasTank, parent sdk.KVStore) *gasKVStore { +func NewStore(tank *types.GasTank, parent types.KVStore) *gasKVStore { kvs := &gasKVStore{ tank: tank, parent: parent, @@ -23,7 +23,7 @@ func NewStore(tank *sdk.GasTank, parent sdk.KVStore) *gasKVStore { return kvs } -// Implements sdk.KVStore. +// Implements types.KVStore. func (gs *gasKVStore) Get(key []byte) (value []byte) { gs.tank.ReadFlat() value = gs.parent.Get(key) @@ -33,7 +33,7 @@ func (gs *gasKVStore) Get(key []byte) (value []byte) { return value } -// Implements sdk.KVStore. +// Implements types.KVStore. func (gs *gasKVStore) Set(key []byte, value []byte) { gs.tank.WriteFlat() // TODO overflow-safe math? @@ -41,30 +41,30 @@ func (gs *gasKVStore) Set(key []byte, value []byte) { gs.parent.Set(key, value) } -// Implements sdk.KVStore. +// Implements types.KVStore. func (gs *gasKVStore) Has(key []byte) bool { gs.tank.HasFlat() return gs.parent.Has(key) } -// Implements sdk.KVStore. +// Implements types.KVStore. func (gs *gasKVStore) Delete(key []byte) { - gs.tank.DeleteFlat() + // No gas costs for deletion gs.parent.Delete(key) } -// Implements sdk.KVStore. -func (gs *gasKVStore) Iterator(start, end []byte) sdk.Iterator { +// Implements types.KVStore. +func (gs *gasKVStore) Iterator(start, end []byte) types.Iterator { return gs.iterator(start, end, true) } -// Implements sdk.KVStore. -func (gs *gasKVStore) ReverseIterator(start, end []byte) sdk.Iterator { +// Implements types.KVStore. +func (gs *gasKVStore) ReverseIterator(start, end []byte) types.Iterator { return gs.iterator(start, end, false) } -func (gs *gasKVStore) iterator(start, end []byte, ascending bool) sdk.Iterator { - var parent sdk.Iterator +func (gs *gasKVStore) iterator(start, end []byte, ascending bool) types.Iterator { + var parent types.Iterator if ascending { parent = gs.parent.Iterator(start, end) } else { @@ -80,11 +80,11 @@ func (gs *gasKVStore) iterator(start, end []byte, ascending bool) sdk.Iterator { } type gasIterator struct { - tank *sdk.GasTank - parent sdk.Iterator + tank *types.GasTank + parent types.Iterator } -func newGasIterator(tank *sdk.GasTank, parent sdk.Iterator) sdk.Iterator { +func newGasIterator(tank *types.GasTank, parent types.Iterator) types.Iterator { return &gasIterator{ tank: tank, parent: parent, diff --git a/store/gas/store_test.go b/store/gas/store_test.go index d4f6b7d1a92f..91c7f0289415 100644 --- a/store/gas/store_test.go +++ b/store/gas/store_test.go @@ -6,17 +6,17 @@ import ( dbm "github.com/tendermint/tendermint/libs/db" "github.com/cosmos/cosmos-sdk/store/dbadapter" + "github.com/cosmos/cosmos-sdk/store/types" "github.com/cosmos/cosmos-sdk/store/utils" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" ) -func newGasTank(limit sdk.Gas) *sdk.GasTank { - return sdk.NewGasTank(sdk.NewGasMeter(limit), sdk.KVGasConfig()) +func newGasTank(limit types.Gas) *types.GasTank { + return types.NewGasTank(types.NewGasMeter(limit), types.KVGasConfig()) } -func newGasKVStore() sdk.KVStore { +func newGasKVStore() types.KVStore { tank := newGasTank(1000) mem := dbadapter.NewStore(dbm.NewMemDB()) return NewStore(tank, mem) @@ -31,7 +31,7 @@ func TestGasKVStoreBasic(t *testing.T) { require.Equal(t, utils.ValFmt(1), st.Get(utils.KeyFmt(1))) st.Delete(utils.KeyFmt(1)) require.Empty(t, st.Get(utils.KeyFmt(1)), "Expected `key1` to be empty") - require.Equal(t, tank.GasConsumed(), sdk.Gas(193)) + require.Equal(t, tank.GasConsumed(), types.Gas(193)) } func TestGasKVStoreIterator(t *testing.T) { @@ -55,7 +55,7 @@ func TestGasKVStoreIterator(t *testing.T) { iterator.Next() require.False(t, iterator.Valid()) require.Panics(t, iterator.Next) - require.Equal(t, tank.GasConsumed(), sdk.Gas(384)) + require.Equal(t, tank.GasConsumed(), types.Gas(384)) } func TestGasKVStoreOutOfGasSet(t *testing.T) { @@ -75,9 +75,9 @@ func TestGasKVStoreOutOfGasIterator(t *testing.T) { require.Panics(t, func() { iterator.Value() }, "Expected out-of-gas") } -func testGasKVStoreWrap(t *testing.T, store sdk.KVStore) { - meter := sdk.NewGasMeter(10000) - tank := sdk.NewGasTank(meter, sdk.GasConfig{HasCostFlat: 10}) +func testGasKVStoreWrap(t *testing.T, store types.KVStore) { + meter := types.NewGasMeter(10000) + tank := types.NewGasTank(meter, types.GasConfig{HasCostFlat: 10}) store = NewStore(tank, store) require.Equal(t, int64(0), meter.GasConsumed()) @@ -85,7 +85,7 @@ func testGasKVStoreWrap(t *testing.T, store sdk.KVStore) { store.Has([]byte("key")) require.Equal(t, int64(10), meter.GasConsumed()) - tank = sdk.NewGasTank(meter, sdk.GasConfig{HasCostFlat: 20}) + tank = types.NewGasTank(meter, types.GasConfig{HasCostFlat: 20}) store = NewStore(tank, store) store.Has([]byte("key")) diff --git a/store/iavl/store.go b/store/iavl/store.go index 03370fdc84dc..b2079b3095dd 100644 --- a/store/iavl/store.go +++ b/store/iavl/store.go @@ -10,7 +10,7 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" dbm "github.com/tendermint/tendermint/libs/db" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/store/types" ) const ( @@ -18,7 +18,7 @@ const ( ) // load the iavl store -func (*iavlStore) LoadVersion(db dbm.DB, id sdk.CommitID) (sdk.CommitStore, error) { +func (*iavlStore) LoadVersion(db dbm.DB, id types.CommitID) (types.CommitStore, error) { tree := iavl.NewMutableTree(db, defaultIAVLCacheSize) _, err := tree.LoadVersion(id.Version) if err != nil { @@ -30,11 +30,11 @@ func (*iavlStore) LoadVersion(db dbm.DB, id sdk.CommitID) (sdk.CommitStore, erro //---------------------------------------- -var _ sdk.KVStore = (*iavlStore)(nil) -var _ sdk.CommitStore = (*iavlStore)(nil) -var _ sdk.Queryable = (*iavlStore)(nil) +var _ types.KVStore = (*iavlStore)(nil) +var _ types.CommitStore = (*iavlStore)(nil) +var _ types.Queryable = (*iavlStore)(nil) -// iavlStore Implements sdk.KVStore and sdk.CommitStore. +// iavlStore Implements types.KVStore and types.CommitStore. type iavlStore struct { // The underlying tree. @@ -65,7 +65,7 @@ func newIAVLStore(tree *iavl.MutableTree, numRecent int64, storeEvery int64) *ia } // Implements Committer. -func (st *iavlStore) Commit() sdk.CommitID { +func (st *iavlStore) Commit() types.CommitID { // Save a new version. hash, version, err := st.tree.SaveVersion() if err != nil { @@ -85,29 +85,29 @@ func (st *iavlStore) Commit() sdk.CommitID { } } - return sdk.CommitID{ + return types.CommitID{ Version: version, Hash: hash, } } // Implements Committer. -func (st *iavlStore) LastCommitID() sdk.CommitID { - return sdk.CommitID{ +func (st *iavlStore) LastCommitID() types.CommitID { + return types.CommitID{ Version: st.tree.Version(), Hash: st.tree.Hash(), } } // Implements Committer. -func (st *iavlStore) SetPruning(pruning sdk.PruningStrategy) { +func (st *iavlStore) SetPruning(pruning types.PruningStrategy) { switch pruning { - case sdk.PruneEverything: + case types.PruneEverything: st.numRecent = 0 st.storeEvery = 0 - case sdk.PruneNothing: + case types.PruneNothing: st.storeEvery = 1 - case sdk.PruneSyncable: + case types.PruneSyncable: st.numRecent = 100 st.storeEvery = 10000 } @@ -118,34 +118,34 @@ func (st *iavlStore) VersionExists(version int64) bool { return st.tree.VersionExists(version) } -// Implements sdk.KVStore. +// Implements types.KVStore. func (st *iavlStore) Set(key, value []byte) { st.tree.Set(key, value) } -// Implements sdk.KVStore. +// Implements types.KVStore. func (st *iavlStore) Get(key []byte) (value []byte) { _, v := st.tree.Get(key) return v } -// Implements sdk.KVStore. +// Implements types.KVStore. func (st *iavlStore) Has(key []byte) (exists bool) { return st.tree.Has(key) } -// Implements sdk.KVStore. +// Implements types.KVStore. func (st *iavlStore) Delete(key []byte) { st.tree.Remove(key) } -// Implements sdk.KVStore. -func (st *iavlStore) Iterator(start, end []byte) sdk.Iterator { +// Implements types.KVStore. +func (st *iavlStore) Iterator(start, end []byte) types.Iterator { return newIAVLIterator(st.tree.ImmutableTree, start, end, true) } -// Implements sdk.KVStore. -func (st *iavlStore) ReverseIterator(start, end []byte) sdk.Iterator { +// Implements types.KVStore. +func (st *iavlStore) ReverseIterator(start, end []byte) types.Iterator { return newIAVLIterator(st.tree.ImmutableTree, start, end, false) } @@ -173,7 +173,7 @@ func getHeight(tree *iavl.MutableTree, req abci.RequestQuery) int64 { func (st *iavlStore) Query(req abci.RequestQuery) (res abci.ResponseQuery) { if len(req.Data) == 0 { msg := "Query cannot be zero length" - return sdk.ErrTxDecode(msg).QueryResult() + return types.ErrTxDecode(msg).QueryResult() } tree := st.tree @@ -210,16 +210,16 @@ func (st *iavlStore) Query(req abci.RequestQuery) (res abci.ResponseQuery) { case "/subspace": subspace := req.Data res.Key = subspace - var KVs []sdk.KVPair - iterator := sdk.KVStorePrefixIterator(st, subspace) + var KVs []types.KVPair + iterator := types.KVStorePrefixIterator(st, subspace) for ; iterator.Valid(); iterator.Next() { - KVs = append(KVs, sdk.KVPair{Key: iterator.Key(), Value: iterator.Value()}) + KVs = append(KVs, types.KVPair{Key: iterator.Key(), Value: iterator.Value()}) } iterator.Close() res.Value = cdc.MustMarshalBinary(KVs) default: msg := fmt.Sprintf("Unexpected Query path: %v", req.Path) - return sdk.ErrUnknownRequest(msg).QueryResult() + return types.ErrUnknownRequest(msg).QueryResult() } return } @@ -255,7 +255,7 @@ type iavlIterator struct { value []byte // The current value } -var _ sdk.Iterator = (*iavlIterator)(nil) +var _ types.Iterator = (*iavlIterator)(nil) // newIAVLIterator will create a new iavlIterator. // CONTRACT: Caller must release the iavlIterator, as each one creates a new diff --git a/store/prefix/store.go b/store/prefix/store.go index 481dc17eed47..62b3b9e53574 100644 --- a/store/prefix/store.go +++ b/store/prefix/store.go @@ -3,16 +3,16 @@ package prefix import ( "bytes" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/store/types" ) -var _ sdk.KVStore = Store{} +var _ types.KVStore = Store{} // prefixStore is similar with tendermint/tendermint/libs/db/prefix_db // both gives access only to the limited subset of the store // for convinience or safety type Store struct { - parent sdk.KVStore + parent types.KVStore prefix []byte } @@ -30,7 +30,7 @@ func (s Store) key(key []byte) (res []byte) { res = cloneAppend(s.prefix, key) return } -func NewStore(parent sdk.KVStore, prefix []byte) Store { +func NewStore(parent types.KVStore, prefix []byte) Store { return Store{ parent: parent, prefix: prefix, @@ -59,7 +59,7 @@ func (s Store) Delete(key []byte) { // Implements KVStore // Check https://github.com/tendermint/tendermint/blob/master/libs/db/prefix_db.go#L106 -func (s Store) Iterator(start, end []byte) sdk.Iterator { +func (s Store) Iterator(start, end []byte) types.Iterator { newstart := cloneAppend(s.prefix, start) var newend []byte @@ -76,7 +76,7 @@ func (s Store) Iterator(start, end []byte) sdk.Iterator { // Implements KVStore // Check https://github.com/tendermint/tendermint/blob/master/libs/db/prefix_db.go#L129 -func (s Store) ReverseIterator(start, end []byte) sdk.Iterator { +func (s Store) ReverseIterator(start, end []byte) types.Iterator { var newstart []byte if start == nil { newstart = cpIncr(s.prefix) @@ -99,16 +99,16 @@ func (s Store) ReverseIterator(start, end []byte) sdk.Iterator { return newPrefixIterator(s.prefix, start, end, iter) } -var _ sdk.Iterator = (*prefixIterator)(nil) +var _ types.Iterator = (*prefixIterator)(nil) type prefixIterator struct { prefix []byte start, end []byte - iter sdk.Iterator + iter types.Iterator valid bool } -func newPrefixIterator(prefix, start, end []byte, parent sdk.Iterator) *prefixIterator { +func newPrefixIterator(prefix, start, end []byte, parent types.Iterator) *prefixIterator { return &prefixIterator{ prefix: prefix, start: start, @@ -170,9 +170,9 @@ func stripPrefix(key []byte, prefix []byte) []byte { return key[len(prefix):] } -// wrapping sdk.PrefixEndBytes +// wrapping types.PrefixEndBytes func cpIncr(bz []byte) []byte { - return sdk.PrefixEndBytes(bz) + return types.PrefixEndBytes(bz) } // copied from github.com/tendermint/tendermint/libs/db/util.go @@ -195,7 +195,7 @@ func cpDecr(bz []byte) (ret []byte) { return nil } -func skipOne(iter sdk.Iterator, skipKey []byte) { +func skipOne(iter types.Iterator, skipKey []byte) { if iter.Valid() { if bytes.Equal(iter.Key(), skipKey) { iter.Next() diff --git a/store/prefix/store_test.go b/store/prefix/store_test.go index 01d409c6ea52..90e31ca9fffe 100644 --- a/store/prefix/store_test.go +++ b/store/prefix/store_test.go @@ -9,7 +9,7 @@ import ( "github.com/tendermint/iavl" dbm "github.com/tendermint/tendermint/libs/db" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/store/types" ) type kvpair struct { @@ -30,7 +30,7 @@ func genRandomKVPairs(t *testing.T) []kvpair { return kvps } -func setRandomKVPairs(t *testing.T, store KVStore) []kvpair { +func setRandomKVPairs(t *testing.T, store types.KVStore) []kvpair { kvps := genRandomKVPairs(t) for _, kvp := range kvps { store.Set(kvp.key, kvp.value) @@ -38,9 +38,9 @@ func setRandomKVPairs(t *testing.T, store KVStore) []kvpair { return kvps } -func testPrefixStore(t *testing.T, baseStore KVStore, prefix []byte) { - prefixStore := baseStore.Prefix(prefix) - prefixPrefixStore := prefixStore.Prefix([]byte("prefix")) +func testPrefixStore(t *testing.T, baseStore types.KVStore, prefix []byte) { + prefixStore := NewStore(baseStore, prefix) + prefixPrefixStore := NewStore(prefixStore, []byte("prefix")) require.Panics(t, func() { prefixStore.Get(nil) }) require.Panics(t, func() { prefixStore.Set(nil, []byte{}) }) @@ -82,15 +82,15 @@ func TestIAVLStorePrefix(t *testing.T) { } func TestCacheKVStorePrefix(t *testing.T) { - cacheStore := newCacheKVStore() + cacheStore := newCachetypes.KVStore() testPrefixStore(t, cacheStore, []byte("test")) } func TestGasKVStorePrefix(t *testing.T) { - meter := sdk.NewGasMeter(100000000) + meter := types.NewGasMeter(100000000) mem := dbStoreAdapter{dbm.NewMemDB()} - gasStore := NewGasKVStore(meter, sdk.KVGasConfig(), mem) + gasStore := gas.NewStore(meter, types.KVGasConfig(), mem) testPrefixStore(t, gasStore, []byte("test")) } @@ -103,8 +103,8 @@ func TestPrefixStoreIterate(t *testing.T) { setRandomKVPairs(t, prefixStore) - bIter := sdk.KVStorePrefixIterator(baseStore, prefix) - pIter := sdk.KVStorePrefixIterator(prefixStore, nil) + bIter := types.KVStorePrefixIterator(baseStore, prefix) + pIter := types.KVStorePrefixIterator(prefixStore, nil) for bIter.Valid() && pIter.Valid() { require.Equal(t, bIter.Key(), append(prefix, pIter.Key()...)) @@ -230,7 +230,7 @@ func TestPrefixStoreReverseIteratorEdgeCase(t *testing.T) { // Tests below are ported from https://github.com/tendermint/tendermint/blob/master/libs/db/prefix_db_test.go -func mockStoreWithStuff() sdk.KVStore { +func mockStoreWithStuff() types.KVStore { db := dbm.NewMemDB() store := dbStoreAdapter{db} // Under "key" prefix @@ -246,49 +246,49 @@ func mockStoreWithStuff() sdk.KVStore { return store } -func checkValue(t *testing.T, store sdk.KVStore, key []byte, expected []byte) { +func checkValue(t *testing.T, store types.KVStore, key []byte, expected []byte) { bz := store.Get(key) require.Equal(t, expected, bz) } -func checkValid(t *testing.T, itr sdk.Iterator, expected bool) { +func checkValid(t *testing.T, itr types.Iterator, expected bool) { valid := itr.Valid() require.Equal(t, expected, valid) } -func checkNext(t *testing.T, itr sdk.Iterator, expected bool) { +func checkNext(t *testing.T, itr types.Iterator, expected bool) { itr.Next() valid := itr.Valid() require.Equal(t, expected, valid) } -func checkDomain(t *testing.T, itr sdk.Iterator, start, end []byte) { +func checkDomain(t *testing.T, itr types.Iterator, start, end []byte) { ds, de := itr.Domain() require.Equal(t, start, ds) require.Equal(t, end, de) } -func checkItem(t *testing.T, itr sdk.Iterator, key, value []byte) { +func checkItem(t *testing.T, itr types.Iterator, key, value []byte) { require.Exactly(t, key, itr.Key()) require.Exactly(t, value, itr.Value()) } -func checkInvalid(t *testing.T, itr sdk.Iterator) { +func checkInvalid(t *testing.T, itr types.Iterator) { checkValid(t, itr, false) checkKeyPanics(t, itr) checkValuePanics(t, itr) checkNextPanics(t, itr) } -func checkKeyPanics(t *testing.T, itr sdk.Iterator) { +func checkKeyPanics(t *testing.T, itr types.Iterator) { require.Panics(t, func() { itr.Key() }) } -func checkValuePanics(t *testing.T, itr sdk.Iterator) { +func checkValuePanics(t *testing.T, itr types.Iterator) { require.Panics(t, func() { itr.Value() }) } -func checkNextPanics(t *testing.T, itr sdk.Iterator) { +func checkNextPanics(t *testing.T, itr types.Iterator) { require.Panics(t, func() { itr.Next() }) } diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 438c74b74f6a..c3a3db8c3515 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -10,10 +10,9 @@ import ( "github.com/tendermint/tendermint/crypto/tmhash" dbm "github.com/tendermint/tendermint/libs/db" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/store/cachemulti" "github.com/cosmos/cosmos-sdk/store/trace" + "github.com/cosmos/cosmos-sdk/store/types" ) const ( @@ -21,40 +20,40 @@ const ( commitInfoKeyFmt = "s/%d" // s/ ) -// Store is composed of many sdk.CommitStores. Name contrasts with -// cacheMultiStore which is for cache-wrapping other sdk.MultiStores. It implements +// Store is composed of many types.CommitStores. Name contrasts with +// cacheMultiStore which is for cache-wrapping other types.MultiStores. It implements // the CommitMultiStore interface. type Store struct { db dbm.DB - lastCommitID sdk.CommitID - pruning sdk.PruningStrategy - storesParams map[sdk.StoreKey]storeParams - stores map[sdk.StoreKey]sdk.CommitKVStore - keysByName map[string]sdk.StoreKey + lastCommitID types.CommitID + pruning types.PruningStrategy + storesParams map[types.StoreKey]storeParams + stores map[types.StoreKey]types.CommitKVStore + keysByName map[string]types.StoreKey - tracer *sdk.Tracer + tracer *types.Tracer } -var _ sdk.CommitMultiStore = (*Store)(nil) -var _ sdk.Queryable = (*Store)(nil) +var _ types.CommitMultiStore = (*Store)(nil) +var _ types.Queryable = (*Store)(nil) // nolint func NewCommitMultiStore(db dbm.DB) *Store { return &Store{ db: db, - storesParams: make(map[sdk.StoreKey]storeParams), - stores: make(map[sdk.StoreKey]sdk.CommitKVStore), - keysByName: make(map[string]sdk.StoreKey), + storesParams: make(map[types.StoreKey]storeParams), + stores: make(map[types.StoreKey]types.CommitKVStore), + keysByName: make(map[string]types.StoreKey), } } // Implements MultiStore -func (rs *Store) GetTracer() *sdk.Tracer { +func (rs *Store) GetTracer() *types.Tracer { return rs.tracer } // Implements CommitMultiStore -func (rs *Store) SetPruning(pruning sdk.PruningStrategy) { +func (rs *Store) SetPruning(pruning types.PruningStrategy) { rs.pruning = pruning for _, substore := range rs.stores { substore.SetPruning(pruning) @@ -62,9 +61,9 @@ func (rs *Store) SetPruning(pruning sdk.PruningStrategy) { } // Implements CommitMultiStore. -func (rs *Store) MountStoreWithDB(key sdk.StoreKey, db dbm.DB) { +func (rs *Store) MountStoreWithDB(key types.StoreKey, db dbm.DB) { if key == nil { - panic("MountIAVLStore() key cannot be nil") + panic("MountStoreWithDB() key cannot be nil") } if _, ok := rs.storesParams[key]; ok { panic(fmt.Sprintf("Store duplicate store key %v", key)) @@ -80,13 +79,13 @@ func (rs *Store) MountStoreWithDB(key sdk.StoreKey, db dbm.DB) { } // Implements CommitMultiStore. -func (rs *Store) GetCommitStore(key sdk.StoreKey) sdk.CommitStore { +func (rs *Store) GetCommitStore(key types.StoreKey) types.CommitStore { return rs.stores[key] } // Implements CommitMultiStore. -func (rs *Store) GetCommitKVStore(key sdk.StoreKey) sdk.CommitKVStore { - return rs.stores[key].(sdk.CommitKVStore) +func (rs *Store) GetCommitKVStore(key types.StoreKey) types.CommitKVStore { + return rs.stores[key].(types.CommitKVStore) } // Implements CommitMultiStore. @@ -98,8 +97,8 @@ func (rs *Store) LoadLatestVersion() error { // Implements CommitMultiStore. func (rs *Store) LoadMultiStoreVersion(ver int64) error { // Convert StoreInfos slice to map - var lastCommitID sdk.CommitID - infos := make(map[sdk.StoreKey]storeInfo) + var lastCommitID types.CommitID + infos := make(map[types.StoreKey]storeInfo) if ver != 0 { // Get commitInfo cInfo, err := getCommitInfo(rs.db, ver) @@ -115,9 +114,9 @@ func (rs *Store) LoadMultiStoreVersion(ver int64) error { } // Load each Store - var newStores = make(map[sdk.StoreKey]sdk.CommitKVStore) + var newStores = make(map[types.StoreKey]types.CommitKVStore) for key, storeParams := range rs.storesParams { - var id sdk.CommitID + var id types.CommitID if info, ok := infos[key]; ok { id = info.Core.CommitID } @@ -138,12 +137,12 @@ func (rs *Store) LoadMultiStoreVersion(ver int64) error { // +CommitStore // Implements Committer/CommitStore. -func (rs *Store) LastCommitID() sdk.CommitID { +func (rs *Store) LastCommitID() types.CommitID { return rs.lastCommitID } // Implements Committer/CommitStore. -func (rs *Store) Commit() sdk.CommitID { +func (rs *Store) Commit() types.CommitID { // Commit stores. version := rs.lastCommitID.Version + 1 @@ -156,7 +155,7 @@ func (rs *Store) Commit() sdk.CommitID { batch.Write() // Prepare for next version. - commitID := sdk.CommitID{ + commitID := types.CommitID{ Version: version, Hash: commitInfo.Hash(), } @@ -167,16 +166,16 @@ func (rs *Store) Commit() sdk.CommitID { //---------------------------------------- // +MultiStore -// Implements sdk.MultiStore. -func (rs *Store) CacheWrap() sdk.CacheMultiStore { +// Implements types.MultiStore. +func (rs *Store) CacheWrap() types.CacheMultiStore { return cachemulti.NewStore(rs.db, rs.keysByName, rs.stores, rs.tracer) } -// GetKVStore implements the sdk.MultiStore interface. If tracing is enabled on the +// GetKVStore implements the types.MultiStore interface. If tracing is enabled on the // Store, a wrapped TraceKVStore will be returned with the given -// tracer, otherwise, the original sdk.KVStore will be returned. -func (rs *Store) GetKVStore(key sdk.StoreKey) sdk.KVStore { - store := rs.stores[key].(sdk.KVStore) +// tracer, otherwise, the original types.KVStore will be returned. +func (rs *Store) GetKVStore(key types.StoreKey) types.KVStore { + store := rs.stores[key].(types.KVStore) if rs.tracer.Enabled() { store = trace.NewStore(store, rs.tracer) @@ -185,14 +184,14 @@ func (rs *Store) GetKVStore(key sdk.StoreKey) sdk.KVStore { return store } -// Implements sdk.MultiStore +// Implements types.MultiStore // getStoreByName will first convert the original name to -// a special key, before looking up the sdk.CommitStore. +// a special key, before looking up the types.CommitStore. // This is not exposed to the extensions (which will need the -// sdk.StoreKey), but is useful in main, and particularly app.Query, -// in order to convert human strings into sdk.CommitStores. -func (rs *Store) getStoreByName(name string) sdk.KVStore { +// types.StoreKey), but is useful in main, and particularly app.Query, +// in order to convert human strings into types.CommitStores. +func (rs *Store) getStoreByName(name string) types.KVStore { key := rs.keysByName[name] if key == nil { return nil @@ -217,12 +216,12 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery { store := rs.getStoreByName(storeName) if store == nil { msg := fmt.Sprintf("no such store: %s", storeName) - return sdk.ErrUnknownRequest(msg).QueryResult() + return types.ErrUnknownRequest(msg).QueryResult() } - queryable, ok := store.(sdk.Queryable) + queryable, ok := store.(types.Queryable) if !ok { msg := fmt.Sprintf("store %s doesn't support queries", storeName) - return sdk.ErrUnknownRequest(msg).QueryResult() + return types.ErrUnknownRequest(msg).QueryResult() } // trim the path and make the query @@ -235,7 +234,7 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery { commitInfo, errMsg := getCommitInfo(rs.db, res.Height) if errMsg != nil { - return sdk.ErrInternal(errMsg.Error()).QueryResult() + return types.ErrInternal(errMsg.Error()).QueryResult() } res.Proof = buildMultiStoreProof(res.Proof, storeName, commitInfo.StoreInfos) @@ -246,9 +245,9 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery { // parsePath expects a format like /[/] // Must start with /, subpath may be empty // Returns error if it doesn't start with / -func parsePath(path string) (storeName string, subpath string, err sdk.Error) { +func parsePath(path string) (storeName string, subpath string, err types.Error) { if !strings.HasPrefix(path, "/") { - err = sdk.ErrUnknownRequest(fmt.Sprintf("invalid path: %s", path)) + err = types.ErrUnknownRequest(fmt.Sprintf("invalid path: %s", path)) return } paths := strings.SplitN(path[1:], "/", 2) @@ -261,7 +260,7 @@ func parsePath(path string) (storeName string, subpath string, err sdk.Error) { //---------------------------------------- -func (rs *Store) loadCommitStoreFromParams(key sdk.StoreKey, id sdk.CommitID, params storeParams) (store sdk.CommitKVStore, err error) { +func (rs *Store) loadCommitStoreFromParams(key types.StoreKey, id types.CommitID, params storeParams) (store types.CommitKVStore, err error) { var db dbm.DB if params.db != nil { db = dbm.NewPrefixDB(params.db, []byte("s/_/")) @@ -269,7 +268,7 @@ func (rs *Store) loadCommitStoreFromParams(key sdk.StoreKey, id sdk.CommitID, pa db = dbm.NewPrefixDB(rs.db, []byte("s/k:"+params.key.Name()+"/")) } - store = reflect.Zero(params.typ).Interface().(sdk.CommitKVStore) + store = reflect.Zero(params.typ).Interface().(types.CommitKVStore) err = store.LoadKVStoreVersion(db, id) if err != nil { store.SetPruning(rs.pruning) @@ -280,19 +279,19 @@ func (rs *Store) loadCommitStoreFromParams(key sdk.StoreKey, id sdk.CommitID, pa // XXX: move to store subdirectories LoadKVStoreVersion /* switch params.typ { - case sdk.StoreTypeMulti: - panic("recursive sdk.MultiStores not yet supported") + case types.StoreTypeMulti: + panic("recursive types.MultiStores not yet supported") // TODO: id? // return NewCommitMultiStore(db, id) - case sdk.StoreTypeIAVL: + case types.StoreTypeIAVL: store, err = LoadIAVLStore(db, id, rs.pruning) return - case sdk.StoreTypeDB: - panic("dbm.DB is not a sdk.CommitStore") - case sdk.StoreTypeTransient: - _, ok := key.(*sdk.TransientStoreKey) + case types.StoreTypeDB: + panic("dbm.DB is not a types.CommitStore") + case types.StoreTypeTransient: + _, ok := key.(*types.TransientStoreKey) if !ok { - err = fmt.Errorf("invalid sdk.StoreKey for sdk.StoreTypeTransient: %s", key.String()) + err = fmt.Errorf("invalid types.StoreKey for types.StoreTypeTransient: %s", key.String()) return } store = transient.NewStore() @@ -303,7 +302,7 @@ func (rs *Store) loadCommitStoreFromParams(key sdk.StoreKey, id sdk.CommitID, pa */ } -func (rs *Store) nameToKey(name string) sdk.StoreKey { +func (rs *Store) nameToKey(name string) types.StoreKey { for key := range rs.storesParams { if key.Name() == name { return key @@ -316,7 +315,7 @@ func (rs *Store) nameToKey(name string) sdk.StoreKey { // storeParams type storeParams struct { - key sdk.StoreKey + key types.StoreKey db dbm.DB typ reflect.Type } @@ -330,7 +329,7 @@ type commitInfo struct { // Version Version int64 - // sdk.Store info for + // types.Store info for StoreInfos []storeInfo } @@ -344,8 +343,8 @@ func (ci commitInfo) Hash() []byte { return merkle.SimpleHashFromMap(m) } -func (ci commitInfo) CommitID() sdk.CommitID { - return sdk.CommitID{ +func (ci commitInfo) CommitID() types.CommitID { + return types.CommitID{ Version: ci.Version, Hash: ci.Hash(), } @@ -363,8 +362,8 @@ type storeInfo struct { } type storeCore struct { - // sdk.StoreType sdk.StoreType - CommitID sdk.CommitID + // types.StoreType types.StoreType + CommitID types.CommitID // ... maybe add more state } @@ -405,7 +404,7 @@ func setLatestVersion(batch dbm.Batch, version int64) { } // Commits each store and returns a new commitInfo. -func commitStores(version int64, storeMap map[sdk.StoreKey]sdk.CommitKVStore) commitInfo { +func commitStores(version int64, storeMap map[types.StoreKey]types.CommitKVStore) commitInfo { storeInfos := make([]storeInfo, 0, len(storeMap)) for key, store := range storeMap { @@ -416,7 +415,7 @@ func commitStores(version int64, storeMap map[sdk.StoreKey]sdk.CommitKVStore) co continue } - // Record sdk.CommitID + // Record types.CommitID si := storeInfo{} si.Name = key.Name() si.Core.CommitID = commitID diff --git a/store/trace/store.go b/store/trace/store.go index 6f3caed24c07..f4a83518e2d3 100644 --- a/store/trace/store.go +++ b/store/trace/store.go @@ -6,7 +6,7 @@ import ( "fmt" "io" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/store/types" ) const ( @@ -25,8 +25,8 @@ type ( // TODO: Should we use a buffered writer and implement Commit on // Store? Store struct { - parent sdk.KVStore - tracer *sdk.Tracer + parent types.KVStore + tracer *types.Tracer } // operation represents an IO operation @@ -43,7 +43,7 @@ type ( // NewStore returns a reference to a new traceKVStore given a parent // KVStore implementation and a buffered writer. -func NewStore(parent sdk.KVStore, tracer *sdk.Tracer) *Store { +func NewStore(parent types.KVStore, tracer *types.Tracer) *Store { return &Store{parent: parent, tracer: tracer} } @@ -78,20 +78,20 @@ func (tkv *Store) Has(key []byte) bool { // Iterator implements the KVStore interface. It delegates the Iterator call // the to the parent KVStore. -func (tkv *Store) Iterator(start, end []byte) sdk.Iterator { +func (tkv *Store) Iterator(start, end []byte) types.Iterator { return tkv.iterator(start, end, true) } // ReverseIterator implements the KVStore interface. It delegates the // ReverseIterator call the to the parent KVStore. -func (tkv *Store) ReverseIterator(start, end []byte) sdk.Iterator { +func (tkv *Store) ReverseIterator(start, end []byte) types.Iterator { return tkv.iterator(start, end, false) } // iterator facilitates iteration over a KVStore. It delegates the necessary // calls to it's parent KVStore. -func (tkv *Store) iterator(start, end []byte, ascending bool) sdk.Iterator { - var parent sdk.Iterator +func (tkv *Store) iterator(start, end []byte, ascending bool) types.Iterator { + var parent types.Iterator if ascending { parent = tkv.parent.Iterator(start, end) @@ -103,11 +103,11 @@ func (tkv *Store) iterator(start, end []byte, ascending bool) sdk.Iterator { } type traceIterator struct { - parent sdk.Iterator - tracer *sdk.Tracer + parent types.Iterator + tracer *types.Tracer } -func newTraceIterator(tracer *sdk.Tracer, parent sdk.Iterator) sdk.Iterator { +func newTraceIterator(tracer *types.Tracer, parent types.Iterator) types.Iterator { return &traceIterator{parent: parent, tracer: tracer} } @@ -149,14 +149,14 @@ func (ti *traceIterator) Close() { // CacheWrap implements the KVStore interface. It panics as a Store // cannot be cache wrapped. -func (tkv *Store) CacheWrap() sdk.CacheKVStore { +func (tkv *Store) CacheWrap() types.CacheKVStore { panic("cannot CacheWrap a Store") } // writeOperation writes a KVStore operation to the underlying io.Writer as // JSON-encoded data where the key/value pair is base64 encoded. // nolint: errcheck -func writeOperation(tracer *sdk.Tracer, op operation, key, value []byte) { +func writeOperation(tracer *types.Tracer, op operation, key, value []byte) { traceOp := traceOperation{ Operation: op, Key: base64.StdEncoding.EncodeToString(key), diff --git a/store/trace/store_test.go b/store/trace/store_test.go index 708793211360..4d8cf9f1f329 100644 --- a/store/trace/store_test.go +++ b/store/trace/store_test.go @@ -9,15 +9,15 @@ import ( dbm "github.com/tendermint/tendermint/libs/db" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/store/dbadapter" + "github.com/cosmos/cosmos-sdk/store/types" + "github.com/cosmos/cosmos-sdk/store/utils" ) -var kvPairs = []sdk.KVPair{ - {Key: keyFmt(1), Value: valFmt(1)}, - {Key: keyFmt(2), Value: valFmt(2)}, - {Key: keyFmt(3), Value: valFmt(3)}, +var kvPairs = []types.KVPair{ + {Key: utils.KeyFmt(1), Value: utils.ValFmt(1)}, + {Key: utils.KeyFmt(2), Value: utils.ValFmt(2)}, + {Key: utils.KeyFmt(3), Value: utils.ValFmt(3)}, } func newStore(w io.Writer) *Store { @@ -31,10 +31,10 @@ func newStore(w io.Writer) *Store { } func newEmptyStore(w io.Writer) *Store { - memDB := dbStoreAdapter{dbm.NewMemDB()} - tc := sdk.TraceContext(map[string]interface{}{"blockHeight": 64}) + memDB := dbadapter.NewStore(dbm.NewMemDB()) + tc := types.TraceContext(map[string]interface{}{"blockHeight": 64}) - return NewStore(memDB, w, tc) + return NewStore(memDB, &types.Tracer{w, tc}) } func TestStoreGet(t *testing.T) { @@ -266,7 +266,7 @@ func TestTestStoreReverseIterator(t *testing.T) { } func TestStoreGetStoreType(t *testing.T) { - memDB := dbStoreAdapter{dbm.NewMemDB()} + memDB := dbadapter.NewStore(dbm.NewMemDB()) store := NewEmptyStore(nil) require.Equal(t, memDB.GetStoreType(), store.GetStoreType()) } diff --git a/store/transient/transientstore.go b/store/transient/store.go similarity index 68% rename from store/transient/transientstore.go rename to store/transient/store.go index 169d779e3a9e..9dd37aacf660 100644 --- a/store/transient/transientstore.go +++ b/store/transient/store.go @@ -1,13 +1,13 @@ package transient import ( - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/store/types" dbm "github.com/tendermint/tendermint/libs/db" "github.com/cosmos/cosmos-sdk/store/dbadapter" ) -var _ sdk.KVStore = (*Store)(nil) +var _ types.KVStore = (*Store)(nil) // transientStore is a wrapper for a MemDB with Commiter implementation type Store struct { @@ -21,16 +21,16 @@ func NewStore() *Store { // Implements CommitStore // Commit cleans up transientStore. -func (ts *Store) Commit() (id sdk.CommitID) { +func (ts *Store) Commit() (id types.CommitID) { ts.Store = dbadapter.Store{dbm.NewMemDB()} return } // Implements CommitStore -func (ts *Store) SetPruning(pruning sdk.PruningStrategy) { +func (ts *Store) SetPruning(pruning types.PruningStrategy) { } // Implements CommitStore -func (ts *Store) LastCommitID() (id sdk.CommitID) { +func (ts *Store) LastCommitID() (id types.CommitID) { return } diff --git a/store/transient/transientstore_test.go b/store/transient/store_test.go similarity index 90% rename from store/transient/transientstore_test.go rename to store/transient/store_test.go index 75a639092c80..846c8a3a43ca 100644 --- a/store/transient/transientstore_test.go +++ b/store/transient/store_test.go @@ -9,7 +9,7 @@ import ( var k, v = []byte("hello"), []byte("world") func TestTransientStore(t *testing.T) { - tstore := newTransientStore() + tstore := NewStore() require.Nil(t, tstore.Get(k)) diff --git a/store/types/errors.go b/store/types/errors.go new file mode 100644 index 000000000000..ab1254f4c2be --- /dev/null +++ b/store/types/errors.go @@ -0,0 +1 @@ +package types diff --git a/types/gas.go b/store/types/gas.go similarity index 100% rename from types/gas.go rename to store/types/gas.go diff --git a/types/gas_test.go b/store/types/gas_test.go similarity index 100% rename from types/gas_test.go rename to store/types/gas_test.go diff --git a/types/trace.go b/store/types/trace.go similarity index 100% rename from types/trace.go rename to store/types/trace.go diff --git a/store/types/types.go b/store/types/types.go new file mode 100644 index 000000000000..b429c57fe00d --- /dev/null +++ b/store/types/types.go @@ -0,0 +1,280 @@ +package types + +import ( + "fmt" + + abci "github.com/tendermint/tendermint/abci/types" + cmn "github.com/tendermint/tendermint/libs/common" + dbm "github.com/tendermint/tendermint/libs/db" +) + +// NOTE: These are implemented in cosmos-sdk/store. + +// PruningStrategy specfies how old states will be deleted over time +type PruningStrategy uint8 + +const ( + // PruneSyncable means only those states not needed for state syncing will be deleted (keeps last 100 + every 10000th) + PruneSyncable PruningStrategy = iota + + // PruneEverything means all saved states will be deleted, storing only the current state + PruneEverything PruningStrategy = iota + + // PruneNothing means all historic states will be saved, nothing will be deleted + PruneNothing PruningStrategy = iota +) + +// Stores of MultiStore must implement CommitStore. +type CommitStore interface { + // CONTRACT: return zero CommitID to skip writing + Commit() CommitID + LastCommitID() CommitID + SetPruning(PruningStrategy) +} + +// Queryable allows a Store to expose internal state to the abci.Query +// interface. Multistore can route requests to the proper Store. +// +// This is an optional, but useful extension to any CommitStore +type Queryable interface { + Query(abci.RequestQuery) abci.ResponseQuery +} + +//---------------------------------------- +// MultiStore + +type MultiStore interface { //nolint + // Convenience for fetching substores. + GetKVStore(StoreKey) KVStore + + // TODO: recursive multistore not yet supported + // GetMultiStore(StoreKey) MultiStore + + GetTracer() *Tracer + + // CacheWrap cache wraps + // Having this method here because there is currently no + // implementation of MultiStore that panics on CacheWrap(). + // Move this method to CacheWrapperMultiStore when needed + CacheWrap() CacheMultiStore +} + +// From MultiStore.CacheMultiStore().... +type CacheMultiStore interface { + MultiStore + + Write() // Writes operations to underlying KVStore +} + +// A non-cache MultiStore. +type CommitMultiStore interface { + CommitStore + MultiStore + + // Mount a store of type using the given db. + // If db == nil, the new store will use the CommitMultiStore db. + MountStoreWithDB(key StoreKey, db dbm.DB) + + // Panics on a nil key. + GetCommitStore(key StoreKey) CommitStore + + // Panics on a nil key. + GetCommitKVStore(key StoreKey) CommitKVStore + + // Panics on a nil key. + // TODO: recursive multistore not yet supported + // GetCommitMultiStore(key StoreKey) CommitMultiStore + + // Load the latest persisted version. Called once after all + // calls to Mount*Store() are complete. + LoadLatestVersion() error + + // Load a specific persisted version. When you load an old + // version, or when the last commit attempt didn't complete, + // the next commit after loading must be idempotent (return the + // same commit id). Otherwise the behavior is undefined. + LoadMultiStoreVersion(ver int64) error +} + +//---------subsp------------------------------- +// KVStore + +// KVStore is a simple interface to get/set data +type KVStore interface { + // Get returns nil iff key doesn't exist. Panics on nil key. + Get(key []byte) []byte + + // Has checks if a key exists. Panics on nil key. + Has(key []byte) bool + + // Set sets the key. Panics on nil key. + Set(key, value []byte) + + // Delete deletes the key. Panics on nil key. + Delete(key []byte) + + // Iterator over a domain of keys in ascending order. End is exclusive. + // Start must be less than end, or the Iterator is invalid. + // Iterator must be closed by caller. + // To iterate over entire domain, use store.Iterator(nil, nil) + // CONTRACT: No writes may happen within a domain while an iterator exists over it. + Iterator(start, end []byte) Iterator + + // Iterator over a domain of keys in descending order. End is exclusive. + // Start must be greater than end, or the Iterator is invalid. + // Iterator must be closed by caller. + // CONTRACT: No writes may happen within a domain while an iterator exists over it. + ReverseIterator(start, end []byte) Iterator + + // TODO Not yet implemented. + // CreateSubKVStore(key *storeKey) (KVStore, error) + + // TODO Not yet implemented. + // GetSubKVStore(key *storeKey) KVStore + +} + +// Alias iterator to db's Iterator for convenience. +type Iterator = dbm.Iterator + +// Iterator over all the keys with a certain prefix in ascending order +func KVStorePrefixIterator(kvs KVStore, prefix []byte) Iterator { + return kvs.Iterator(prefix, PrefixEndBytes(prefix)) +} + +// Iterator over all the keys with a certain prefix in descending order. +func KVStoreReversePrefixIterator(kvs KVStore, prefix []byte) Iterator { + return kvs.ReverseIterator(prefix, PrefixEndBytes(prefix)) +} + +type CacheWrapperKVStore interface { + KVStore + + // CacheWrap cache wraps + CacheWrap() CacheKVStore +} + +// CacheKVStore cache-wraps a KVStore. After calling .Write() on +// the CacheKVStore, all previously created CacheKVStores on the +// object expire. +type CacheKVStore interface { + CacheWrapperKVStore + + // Writes operations to underlying KVStore + Write() +} + +// Stores of MultiStore must implement CommitStore. +type CommitKVStore interface { + CommitStore + KVStore + + // Load a specific persisted version. When you load an old + // version, or when the last commit attempt didn't complete, + // the next commit after loading must be idempotent (return the + // same commit id). Otherwise the behavior is undefined. + LoadKVStoreVersion(db dbm.DB, id CommitID) error +} + +//---------------------------------------- +// CommitID + +// CommitID contains the tree version number and its merkle root. +type CommitID struct { + Version int64 + Hash []byte +} + +func (cid CommitID) IsZero() bool { //nolint + return cid.Version == 0 && len(cid.Hash) == 0 +} + +func (cid CommitID) String() string { + return fmt.Sprintf("CommitID{%v:%X}", cid.Hash, cid.Version) +} + +//---------------------------------------- +// Keys for accessing substores + +// StoreKey is a key used to index stores in a MultiStore. +type StoreKey interface { + Name() string + String() string + NewStore() CommitStore +} + +// KVStoreKey is used for accessing substores. +// Only the pointer value should ever be used - it functions as a capabilities key. +type KVStoreKey struct { + name string +} + +// NewKVStoreKey returns a new pointer to a KVStoreKey. +// Use a pointer so keys don't collide. +func NewKVStoreKey(name string) *KVStoreKey { + return &KVStoreKey{ + name: name, + } +} + +func (key *KVStoreKey) Name() string { + return key.name +} + +func (key *KVStoreKey) String() string { + return fmt.Sprintf("KVStoreKey{%p, %s}", key, key.name) +} + +// PrefixEndBytes returns the []byte that would end a +// range query for all []byte with a certain prefix +// Deals with last byte of prefix being FF without overflowing +func PrefixEndBytes(prefix []byte) []byte { + if prefix == nil { + return nil + } + + end := make([]byte, len(prefix)) + copy(end, prefix) + + for { + if end[len(end)-1] != byte(255) { + end[len(end)-1]++ + break + } else { + end = end[:len(end)-1] + if len(end) == 0 { + end = nil + break + } + } + } + return end +} + +// TransientStoreKey is used for indexing transient stores in a MultiStore +type TransientStoreKey struct { + name string +} + +// Constructs new TransientStoreKey +// Must return a pointer according to the ocap principle +func NewTransientStoreKey(name string) *TransientStoreKey { + return &TransientStoreKey{ + name: name, + } +} + +// Implements StoreKey +func (key *TransientStoreKey) Name() string { + return key.name +} + +// Implements StoreKey +func (key *TransientStoreKey) String() string { + return fmt.Sprintf("TransientStoreKey{%p, %s}", key, key.name) +} + +//---------------------------------------- + +// key-value result for iterator queries +type KVPair cmn.KVPair diff --git a/types/store.go b/types/store.go index 9184ae7b2225..64b3e31e60fe 100644 --- a/types/store.go +++ b/types/store.go @@ -1,286 +1,7 @@ package types import ( - "fmt" - - abci "github.com/tendermint/tendermint/abci/types" - cmn "github.com/tendermint/tendermint/libs/common" - dbm "github.com/tendermint/tendermint/libs/db" -) - -// NOTE: These are implemented in cosmos-sdk/store. - -// PruningStrategy specfies how old states will be deleted over time -type PruningStrategy uint8 - -const ( - // PruneSyncable means only those states not needed for state syncing will be deleted (keeps last 100 + every 10000th) - PruneSyncable PruningStrategy = iota - - // PruneEverything means all saved states will be deleted, storing only the current state - PruneEverything PruningStrategy = iota - - // PruneNothing means all historic states will be saved, nothing will be deleted - PruneNothing PruningStrategy = iota +//"github.com/cosmos/cosmos-sdk/store" ) -// Stores of MultiStore must implement CommitStore. -type CommitStore interface { - // CONTRACT: return zero CommitID to skip writing - Commit() CommitID - LastCommitID() CommitID - SetPruning(PruningStrategy) -} - -// Queryable allows a Store to expose internal state to the abci.Query -// interface. Multistore can route requests to the proper Store. -// -// This is an optional, but useful extension to any CommitStore -type Queryable interface { - Query(abci.RequestQuery) abci.ResponseQuery -} - -//---------------------------------------- -// MultiStore - -type MultiStore interface { //nolint - // Convenience for fetching substores. - GetKVStore(StoreKey) KVStore - - // TODO: recursive multistore not yet supported - // GetMultiStore(StoreKey) MultiStore - - GetTracer() *Tracer - - // CacheWrap cache wraps - // Having this method here because there is currently no - // implementation of MultiStore that panics on CacheWrap(). - // Move this method to CacheWrapperMultiStore when needed - CacheWrap() CacheMultiStore -} - -// From MultiStore.CacheMultiStore().... -type CacheMultiStore interface { - MultiStore - - Write() // Writes operations to underlying KVStore -} - -// A non-cache MultiStore. -type CommitMultiStore interface { - CommitStore - MultiStore - - // Mount a store of type using the given db. - // If db == nil, the new store will use the CommitMultiStore db. - MountStoreWithDB(key StoreKey, db dbm.DB) - - // Panics on a nil key. - GetCommitStore(key StoreKey) CommitStore - - // Panics on a nil key. - GetCommitKVStore(key StoreKey) CommitKVStore - - // Panics on a nil key. - // TODO: recursive multistore not yet supported - // GetCommitMultiStore(key StoreKey) CommitMultiStore - - // Load the latest persisted version. Called once after all - // calls to Mount*Store() are complete. - LoadLatestVersion() error - - // Load a specific persisted version. When you load an old - // version, or when the last commit attempt didn't complete, - // the next commit after loading must be idempotent (return the - // same commit id). Otherwise the behavior is undefined. - LoadMultiStoreVersion(ver int64) error -} - -//---------subsp------------------------------- -// KVStore - -// KVStore is a simple interface to get/set data -type KVStore interface { - // Get returns nil iff key doesn't exist. Panics on nil key. - Get(key []byte) []byte - - // Has checks if a key exists. Panics on nil key. - Has(key []byte) bool - - // Set sets the key. Panics on nil key. - Set(key, value []byte) - - // Delete deletes the key. Panics on nil key. - Delete(key []byte) - - // Iterator over a domain of keys in ascending order. End is exclusive. - // Start must be less than end, or the Iterator is invalid. - // Iterator must be closed by caller. - // To iterate over entire domain, use store.Iterator(nil, nil) - // CONTRACT: No writes may happen within a domain while an iterator exists over it. - Iterator(start, end []byte) Iterator - - // Iterator over a domain of keys in descending order. End is exclusive. - // Start must be greater than end, or the Iterator is invalid. - // Iterator must be closed by caller. - // CONTRACT: No writes may happen within a domain while an iterator exists over it. - ReverseIterator(start, end []byte) Iterator - - // TODO Not yet implemented. - // CreateSubKVStore(key *storeKey) (KVStore, error) - - // TODO Not yet implemented. - // GetSubKVStore(key *storeKey) KVStore - -} - -// Alias iterator to db's Iterator for convenience. -type Iterator = dbm.Iterator - -// Iterator over all the keys with a certain prefix in ascending order -func KVStorePrefixIterator(kvs KVStore, prefix []byte) Iterator { - return kvs.Iterator(prefix, PrefixEndBytes(prefix)) -} - -// Iterator over all the keys with a certain prefix in descending order. -func KVStoreReversePrefixIterator(kvs KVStore, prefix []byte) Iterator { - return kvs.ReverseIterator(prefix, PrefixEndBytes(prefix)) -} - -type CacheWrapperKVStore interface { - KVStore - - // CacheWrap cache wraps - CacheWrap() CacheKVStore -} - -// CacheKVStore cache-wraps a KVStore. After calling .Write() on -// the CacheKVStore, all previously created CacheKVStores on the -// object expire. -type CacheKVStore interface { - CacheWrapperKVStore - - // Writes operations to underlying KVStore - Write() -} - -// Stores of MultiStore must implement CommitStore. -type CommitKVStore interface { - CommitStore - KVStore - - // Load a specific persisted version. When you load an old - // version, or when the last commit attempt didn't complete, - // the next commit after loading must be idempotent (return the - // same commit id). Otherwise the behavior is undefined. - LoadKVStoreVersion(db dbm.DB, id CommitID) error -} - -//---------------------------------------- -// CommitID - -// CommitID contains the tree version number and its merkle root. -type CommitID struct { - Version int64 - Hash []byte -} - -func (cid CommitID) IsZero() bool { //nolint - return cid.Version == 0 && len(cid.Hash) == 0 -} - -func (cid CommitID) String() string { - return fmt.Sprintf("CommitID{%v:%X}", cid.Hash, cid.Version) -} - -//---------------------------------------- -// Keys for accessing substores - -// StoreKey is a key used to index stores in a MultiStore. -type StoreKey interface { - Name() string - String() string -} - -// KVStoreKey is used for accessing substores. -// Only the pointer value should ever be used - it functions as a capabilities key. -type KVStoreKey struct { - name string -} - -// NewKVStoreKey returns a new pointer to a KVStoreKey. -// Use a pointer so keys don't collide. -func NewKVStoreKey(name string) *KVStoreKey { - return &KVStoreKey{ - name: name, - } -} - -func (key *KVStoreKey) Name() string { - return key.name -} - -func (key *KVStoreKey) String() string { - return fmt.Sprintf("KVStoreKey{%p, %s}", key, key.name) -} - -// PrefixEndBytes returns the []byte that would end a -// range query for all []byte with a certain prefix -// Deals with last byte of prefix being FF without overflowing -func PrefixEndBytes(prefix []byte) []byte { - if prefix == nil { - return nil - } - - end := make([]byte, len(prefix)) - copy(end, prefix) - - for { - if end[len(end)-1] != byte(255) { - end[len(end)-1]++ - break - } else { - end = end[:len(end)-1] - if len(end) == 0 { - end = nil - break - } - } - } - return end -} - -// InclusiveEndBytes returns the []byte that would end a -// range query such that the input would be included -func InclusiveEndBytes(inclusiveBytes []byte) (exclusiveBytes []byte) { - exclusiveBytes = append(inclusiveBytes, byte(0x00)) - return exclusiveBytes -} - -// TransientStoreKey is used for indexing transient stores in a MultiStore -type TransientStoreKey struct { - name string -} - -// Constructs new TransientStoreKey -// Must return a pointer according to the ocap principle -func NewTransientStoreKey(name string) *TransientStoreKey { - return &TransientStoreKey{ - name: name, - } -} - -// Implements StoreKey -func (key *TransientStoreKey) Name() string { - return key.name -} - -// Implements StoreKey -func (key *TransientStoreKey) String() string { - return fmt.Sprintf("TransientStoreKey{%p, %s}", key, key.name) -} - -//---------------------------------------- - -// key-value result for iterator queries -type KVPair cmn.KVPair +// XXX: reexport From bd6217a2ebaf349e3f4515525dced63e020db166 Mon Sep 17 00:00:00 2001 From: mossid Date: Sun, 16 Sep 2018 19:31:57 +0900 Subject: [PATCH 05/22] move storekeys to each store --- store/cache/store_test.go | 3 +- store/cachemulti/store.go | 8 +- store/firstlast.go | 16 +++- store/gas/store_test.go | 13 ++- store/iavl/key.go | 35 ++++++++ store/iavl/store.go | 44 +++++----- store/iavl/store_test.go | 48 +++++------ store/prefix/store_test.go | 30 ++----- store/rootmulti/store.go | 15 +++- store/rootmulti/store_test.go | 50 +++++------ store/store.go | 9 -- store/trace/store.go | 6 -- store/trace/store_test.go | 15 ---- store/transient/key.go | 37 +++++++++ store/types/errors.go | 128 +++++++++++++++++++++++++++++ store/types/gas.go | 19 ++++- store/types/{types.go => store.go} | 47 +---------- types/context.go | 6 +- types/errors.go | 12 +-- types/errors_test.go | 7 +- types/store.go | 22 ++++- 21 files changed, 366 insertions(+), 204 deletions(-) create mode 100644 store/iavl/key.go delete mode 100644 store/store.go create mode 100644 store/transient/key.go rename store/types/{types.go => store.go} (85%) diff --git a/store/cache/store_test.go b/store/cache/store_test.go index be257ab98876..6448edcad0f0 100644 --- a/store/cache/store_test.go +++ b/store/cache/store_test.go @@ -7,10 +7,9 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" dbm "github.com/tendermint/tendermint/libs/db" - "github.com/cosmos/cosmos-sdk/store/types" - "github.com/cosmos/cosmos-sdk/store/cache" "github.com/cosmos/cosmos-sdk/store/dbadapter" + "github.com/cosmos/cosmos-sdk/store/types" "github.com/cosmos/cosmos-sdk/store/utils" ) diff --git a/store/cachemulti/store.go b/store/cachemulti/store.go index bc77df008c4a..a3c40f8cdb5b 100644 --- a/store/cachemulti/store.go +++ b/store/cachemulti/store.go @@ -20,16 +20,18 @@ type Store struct { keysByName map[string]types.StoreKey tracer *types.Tracer + tank *types.GasTank } var _ types.CacheMultiStore = Store{} -func NewStore(db dbm.DB, keysByName map[string]types.StoreKey, stores map[types.StoreKey]types.CommitKVStore, tracer *types.Tracer) Store { +func NewStore(db dbm.DB, keysByName map[string]types.StoreKey, stores map[types.StoreKey]types.CommitKVStore, tracer *types.Tracer, tank *types.GasTank) Store { cms := Store{ db: cache.NewStore(dbadapter.NewStore(db)), stores: make(map[types.StoreKey]types.CacheKVStore, len(stores)), keysByName: keysByName, tracer: tracer, + tank: tank, } for key, store := range stores { @@ -66,6 +68,10 @@ func (cms Store) GetTracer() *types.Tracer { return cms.tracer } +func (cms Store) GetGasTank() *types.GasTank { + return cms.tank +} + // Implements CacheMultiStore. func (cms Store) Write() { cms.db.Write() diff --git a/store/firstlast.go b/store/firstlast.go index a47f1396d158..353f44aa2e22 100644 --- a/store/firstlast.go +++ b/store/firstlast.go @@ -4,10 +4,12 @@ import ( "bytes" cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/store/types" ) // Gets the first item. -func First(st KVStore, start, end []byte) (kv cmn.KVPair, ok bool) { +func First(st types.KVStore, start, end []byte) (kv cmn.KVPair, ok bool) { iter := st.Iterator(start, end) if !iter.Valid() { return kv, false @@ -18,7 +20,7 @@ func First(st KVStore, start, end []byte) (kv cmn.KVPair, ok bool) { } // Gets the last item. `end` is exclusive. -func Last(st KVStore, start, end []byte) (kv cmn.KVPair, ok bool) { +func Last(st types.KVStore, start, end []byte) (kv cmn.KVPair, ok bool) { iter := st.ReverseIterator(end, start) if !iter.Valid() { if v := st.Get(start); v != nil { @@ -38,3 +40,13 @@ func Last(st KVStore, start, end []byte) (kv cmn.KVPair, ok bool) { return cmn.KVPair{Key: iter.Key(), Value: iter.Value()}, true } + +// copied from iavl/store.go +func cp(bz []byte) (ret []byte) { + if bz == nil { + return nil + } + ret = make([]byte, len(bz)) + copy(ret, bz) + return ret +} diff --git a/store/gas/store_test.go b/store/gas/store_test.go index 91c7f0289415..fe6d9ab65bce 100644 --- a/store/gas/store_test.go +++ b/store/gas/store_test.go @@ -13,7 +13,7 @@ import ( ) func newGasTank(limit types.Gas) *types.GasTank { - return types.NewGasTank(types.NewGasMeter(limit), types.KVGasConfig()) + return types.NewGasTank(limit, types.KVGasConfig()) } func newGasKVStore() types.KVStore { @@ -76,20 +76,19 @@ func TestGasKVStoreOutOfGasIterator(t *testing.T) { } func testGasKVStoreWrap(t *testing.T, store types.KVStore) { - meter := types.NewGasMeter(10000) - tank := types.NewGasTank(meter, types.GasConfig{HasCostFlat: 10}) + tank := types.NewGasTank(10000, types.GasConfig{HasCostFlat: 10}) store = NewStore(tank, store) - require.Equal(t, int64(0), meter.GasConsumed()) + require.Equal(t, int64(0), tank.GasConsumed()) store.Has([]byte("key")) - require.Equal(t, int64(10), meter.GasConsumed()) + require.Equal(t, int64(10), tank.GasConsumed()) - tank = types.NewGasTank(meter, types.GasConfig{HasCostFlat: 20}) + tank.SetConfig(types.GasConfig{HasCostFlat: 20}) store = NewStore(tank, store) store.Has([]byte("key")) - require.Equal(t, int64(40), meter.GasConsumed()) + require.Equal(t, int64(40), tank.GasConsumed()) } // XXX: make it stop using iavl or move it to iavl diff --git a/store/iavl/key.go b/store/iavl/key.go new file mode 100644 index 000000000000..dbe30c2b9142 --- /dev/null +++ b/store/iavl/key.go @@ -0,0 +1,35 @@ +package iavl + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/store/types" +) + +var _ types.StoreKey = (*KVStoreKey)(nil) + +// KVStoreKey is used for accessing substores. +// Only the pointer value should ever be used - it functions as a capabilities key. +type KVStoreKey struct { + name string +} + +// NewKVStoreKey returns a new pointer to a KVStoreKey. +// Use a pointer so keys don't collide. +func NewKey(name string) *KVStoreKey { + return &KVStoreKey{ + name: name, + } +} + +func (key *KVStoreKey) Name() string { + return key.name +} + +func (key *KVStoreKey) String() string { + return fmt.Sprintf("KVStoreKey{%p, %s}", key, key.name) +} + +func (key *KVStoreKey) NewStore() types.CommitStore { + return &Store{} +} diff --git a/store/iavl/store.go b/store/iavl/store.go index b2079b3095dd..d825f6a5f5a8 100644 --- a/store/iavl/store.go +++ b/store/iavl/store.go @@ -18,24 +18,26 @@ const ( ) // load the iavl store -func (*iavlStore) LoadVersion(db dbm.DB, id types.CommitID) (types.CommitStore, error) { +func (store *Store) LoadKVStoreVersion(db dbm.DB, id types.CommitID) error { tree := iavl.NewMutableTree(db, defaultIAVLCacheSize) _, err := tree.LoadVersion(id.Version) if err != nil { - return nil, err + return err } iavl := newIAVLStore(tree, int64(0), int64(0)) - return iavl, nil + store = iavl + return nil } //---------------------------------------- -var _ types.KVStore = (*iavlStore)(nil) -var _ types.CommitStore = (*iavlStore)(nil) -var _ types.Queryable = (*iavlStore)(nil) +var _ types.KVStore = (*Store)(nil) +var _ types.CommitStore = (*Store)(nil) +var _ types.Queryable = (*Store)(nil) +var _ types.CommitKVStore = (*Store)(nil) -// iavlStore Implements types.KVStore and types.CommitStore. -type iavlStore struct { +// Store Implements types.KVStore and types.CommitStore. +type Store struct { // The underlying tree. tree *iavl.MutableTree @@ -55,8 +57,8 @@ type iavlStore struct { // CONTRACT: tree should be fully loaded. // nolint: unparam -func newIAVLStore(tree *iavl.MutableTree, numRecent int64, storeEvery int64) *iavlStore { - st := &iavlStore{ +func newIAVLStore(tree *iavl.MutableTree, numRecent int64, storeEvery int64) *Store { + st := &Store{ tree: tree, numRecent: numRecent, storeEvery: storeEvery, @@ -65,7 +67,7 @@ func newIAVLStore(tree *iavl.MutableTree, numRecent int64, storeEvery int64) *ia } // Implements Committer. -func (st *iavlStore) Commit() types.CommitID { +func (st *Store) Commit() types.CommitID { // Save a new version. hash, version, err := st.tree.SaveVersion() if err != nil { @@ -92,7 +94,7 @@ func (st *iavlStore) Commit() types.CommitID { } // Implements Committer. -func (st *iavlStore) LastCommitID() types.CommitID { +func (st *Store) LastCommitID() types.CommitID { return types.CommitID{ Version: st.tree.Version(), Hash: st.tree.Hash(), @@ -100,7 +102,7 @@ func (st *iavlStore) LastCommitID() types.CommitID { } // Implements Committer. -func (st *iavlStore) SetPruning(pruning types.PruningStrategy) { +func (st *Store) SetPruning(pruning types.PruningStrategy) { switch pruning { case types.PruneEverything: st.numRecent = 0 @@ -114,38 +116,38 @@ func (st *iavlStore) SetPruning(pruning types.PruningStrategy) { } // VersionExists returns whether or not a given version is stored. -func (st *iavlStore) VersionExists(version int64) bool { +func (st *Store) VersionExists(version int64) bool { return st.tree.VersionExists(version) } // Implements types.KVStore. -func (st *iavlStore) Set(key, value []byte) { +func (st *Store) Set(key, value []byte) { st.tree.Set(key, value) } // Implements types.KVStore. -func (st *iavlStore) Get(key []byte) (value []byte) { +func (st *Store) Get(key []byte) (value []byte) { _, v := st.tree.Get(key) return v } // Implements types.KVStore. -func (st *iavlStore) Has(key []byte) (exists bool) { +func (st *Store) Has(key []byte) (exists bool) { return st.tree.Has(key) } // Implements types.KVStore. -func (st *iavlStore) Delete(key []byte) { +func (st *Store) Delete(key []byte) { st.tree.Remove(key) } // Implements types.KVStore. -func (st *iavlStore) Iterator(start, end []byte) types.Iterator { +func (st *Store) Iterator(start, end []byte) types.Iterator { return newIAVLIterator(st.tree.ImmutableTree, start, end, true) } // Implements types.KVStore. -func (st *iavlStore) ReverseIterator(start, end []byte) types.Iterator { +func (st *Store) ReverseIterator(start, end []byte) types.Iterator { return newIAVLIterator(st.tree.ImmutableTree, start, end, false) } @@ -170,7 +172,7 @@ func getHeight(tree *iavl.MutableTree, req abci.RequestQuery) int64 { // If latest-1 is not present, use latest (which must be present) // if you care to have the latest data to see a tx results, you must // explicitly set the height you want to see -func (st *iavlStore) Query(req abci.RequestQuery) (res abci.ResponseQuery) { +func (st *Store) Query(req abci.RequestQuery) (res abci.ResponseQuery) { if len(req.Data) == 0 { msg := "Query cannot be zero length" return types.ErrTxDecode(msg).QueryResult() diff --git a/store/iavl/store_test.go b/store/iavl/store_test.go index 2968c0c435bb..cd9273edddcb 100644 --- a/store/iavl/store_test.go +++ b/store/iavl/store_test.go @@ -11,7 +11,7 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" dbm "github.com/tendermint/tendermint/libs/db" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/store/types" ) var ( @@ -29,7 +29,7 @@ var ( ) // make a tree and save it -func newTree(t *testing.T, db dbm.DB) (*iavl.MutableTree, sdk.CommitID) { +func newTree(t *testing.T, db dbm.DB) (*iavl.MutableTree, types.CommitID) { tree := iavl.NewMutableTree(db, cacheSize) for k, v := range treeData { tree.Set([]byte(k), []byte(v)) @@ -41,7 +41,7 @@ func newTree(t *testing.T, db dbm.DB) (*iavl.MutableTree, sdk.CommitID) { } hash, ver, err := tree.SaveVersion() require.Nil(t, err) - return tree, sdk.CommitID{ver, hash} + return tree, types.CommitID{ver, hash} } func TestIAVLStoreGetSetHasDelete(t *testing.T) { @@ -159,7 +159,7 @@ func TestIAVLSubspaceIterator(t *testing.T) { var i int - iter := sdk.KVStorePrefixIterator(iavlStore, []byte("test")) + iter := types.KVStorePrefixIterator(iavlStore, []byte("test")) expected := []string{"test1", "test2", "test3"} for i = 0; iter.Valid(); iter.Next() { expectedKey := expected[i] @@ -171,7 +171,7 @@ func TestIAVLSubspaceIterator(t *testing.T) { iter.Close() require.Equal(t, len(expected), i) - iter = sdk.KVStorePrefixIterator(iavlStore, []byte{byte(55), byte(255), byte(255)}) + iter = types.KVStorePrefixIterator(iavlStore, []byte{byte(55), byte(255), byte(255)}) expected2 := [][]byte{ {byte(55), byte(255), byte(255), byte(0)}, {byte(55), byte(255), byte(255), byte(1)}, @@ -187,7 +187,7 @@ func TestIAVLSubspaceIterator(t *testing.T) { iter.Close() require.Equal(t, len(expected), i) - iter = sdk.KVStorePrefixIterator(iavlStore, []byte{byte(255), byte(255)}) + iter = types.KVStorePrefixIterator(iavlStore, []byte{byte(255), byte(255)}) expected2 = [][]byte{ {byte(255), byte(255), byte(0)}, {byte(255), byte(255), byte(1)}, @@ -221,7 +221,7 @@ func TestIAVLReverseSubspaceIterator(t *testing.T) { var i int - iter := sdk.KVStoreReversePrefixIterator(iavlStore, []byte("test")) + iter := types.KVStoreReversePrefixIterator(iavlStore, []byte("test")) expected := []string{"test3", "test2", "test1"} for i = 0; iter.Valid(); iter.Next() { expectedKey := expected[i] @@ -232,7 +232,7 @@ func TestIAVLReverseSubspaceIterator(t *testing.T) { } require.Equal(t, len(expected), i) - iter = sdk.KVStoreReversePrefixIterator(iavlStore, []byte{byte(55), byte(255), byte(255)}) + iter = types.KVStoreReversePrefixIterator(iavlStore, []byte{byte(55), byte(255), byte(255)}) expected2 := [][]byte{ {byte(55), byte(255), byte(255), byte(255)}, {byte(55), byte(255), byte(255), byte(1)}, @@ -247,7 +247,7 @@ func TestIAVLReverseSubspaceIterator(t *testing.T) { } require.Equal(t, len(expected), i) - iter = sdk.KVStoreReversePrefixIterator(iavlStore, []byte{byte(255), byte(255)}) + iter = types.KVStoreReversePrefixIterator(iavlStore, []byte{byte(255), byte(255)}) expected2 = [][]byte{ {byte(255), byte(255), byte(255)}, {byte(255), byte(255), byte(1)}, @@ -263,7 +263,7 @@ func TestIAVLReverseSubspaceIterator(t *testing.T) { require.Equal(t, len(expected), i) } -func nextVersion(iavl *iavlStore) { +func nextVersion(iavl *Store) { key := []byte(fmt.Sprintf("Key for tree: %d", iavl.LastCommitID().Version)) value := []byte(fmt.Sprintf("Value for tree: %d", iavl.LastCommitID().Version)) iavl.Set(key, value) @@ -385,12 +385,12 @@ func TestIAVLStoreQuery(t *testing.T) { v3 := []byte("val3") ksub := []byte("key") - KVs0 := []sdk.KVPair{} - KVs1 := []sdk.KVPair{ + KVs0 := []types.KVPair{} + KVs1 := []types.KVPair{ {Key: k1, Value: v1}, {Key: k2, Value: v2}, } - KVs2 := []sdk.KVPair{ + KVs2 := []types.KVPair{ {Key: k1, Value: v3}, {Key: k2, Value: v2}, } @@ -405,7 +405,7 @@ func TestIAVLStoreQuery(t *testing.T) { // query subspace before anything set qres := iavlStore.Query(querySub) - require.Equal(t, uint32(sdk.CodeOK), qres.Code) + require.Equal(t, uint32(types.CodeOK), qres.Code) require.Equal(t, valExpSubEmpty, qres.Value) // set data @@ -414,24 +414,24 @@ func TestIAVLStoreQuery(t *testing.T) { // set data without commit, doesn't show up qres = iavlStore.Query(query) - require.Equal(t, uint32(sdk.CodeOK), qres.Code) + require.Equal(t, uint32(types.CodeOK), qres.Code) require.Nil(t, qres.Value) // commit it, but still don't see on old version cid = iavlStore.Commit() qres = iavlStore.Query(query) - require.Equal(t, uint32(sdk.CodeOK), qres.Code) + require.Equal(t, uint32(types.CodeOK), qres.Code) require.Nil(t, qres.Value) // but yes on the new version query.Height = cid.Version qres = iavlStore.Query(query) - require.Equal(t, uint32(sdk.CodeOK), qres.Code) + require.Equal(t, uint32(types.CodeOK), qres.Code) require.Equal(t, v1, qres.Value) // and for the subspace qres = iavlStore.Query(querySub) - require.Equal(t, uint32(sdk.CodeOK), qres.Code) + require.Equal(t, uint32(types.CodeOK), qres.Code) require.Equal(t, valExpSub1, qres.Value) // modify @@ -440,28 +440,28 @@ func TestIAVLStoreQuery(t *testing.T) { // query will return old values, as height is fixed qres = iavlStore.Query(query) - require.Equal(t, uint32(sdk.CodeOK), qres.Code) + require.Equal(t, uint32(types.CodeOK), qres.Code) require.Equal(t, v1, qres.Value) // update to latest in the query and we are happy query.Height = cid.Version qres = iavlStore.Query(query) - require.Equal(t, uint32(sdk.CodeOK), qres.Code) + require.Equal(t, uint32(types.CodeOK), qres.Code) require.Equal(t, v3, qres.Value) query2 := abci.RequestQuery{Path: "/key", Data: k2, Height: cid.Version} qres = iavlStore.Query(query2) - require.Equal(t, uint32(sdk.CodeOK), qres.Code) + require.Equal(t, uint32(types.CodeOK), qres.Code) require.Equal(t, v2, qres.Value) // and for the subspace qres = iavlStore.Query(querySub) - require.Equal(t, uint32(sdk.CodeOK), qres.Code) + require.Equal(t, uint32(types.CodeOK), qres.Code) require.Equal(t, valExpSub2, qres.Value) // default (height 0) will show latest -1 query0 := abci.RequestQuery{Path: "/store", Data: k1} qres = iavlStore.Query(query0) - require.Equal(t, uint32(sdk.CodeOK), qres.Code) + require.Equal(t, uint32(types.CodeOK), qres.Code) require.Equal(t, v1, qres.Value) } @@ -475,7 +475,7 @@ func BenchmarkIAVLIteratorNext(b *testing.B) { tree.Set(key, value) } iavlStore := newIAVLStore(tree, numRecent, storeEvery) - iterators := make([]sdk.Iterator, b.N/treeSize) + iterators := make([]types.Iterator, b.N/treeSize) for i := 0; i < len(iterators); i++ { iterators[i] = iavlStore.Iterator([]byte{0}, []byte{255, 255, 255, 255, 255}) } diff --git a/store/prefix/store_test.go b/store/prefix/store_test.go index 90e31ca9fffe..7a492a63854f 100644 --- a/store/prefix/store_test.go +++ b/store/prefix/store_test.go @@ -6,9 +6,9 @@ import ( "github.com/stretchr/testify/require" - "github.com/tendermint/iavl" dbm "github.com/tendermint/tendermint/libs/db" + "github.com/cosmos/cosmos-sdk/store/dbadapter" "github.com/cosmos/cosmos-sdk/store/types" ) @@ -44,6 +44,8 @@ func testPrefixStore(t *testing.T, baseStore types.KVStore, prefix []byte) { require.Panics(t, func() { prefixStore.Get(nil) }) require.Panics(t, func() { prefixStore.Set(nil, []byte{}) }) + require.Panics(t, func() { prefixPrefixStore.Get(nil) }) + require.Panics(t, func() { prefixPrefixStore.Set(nil, []byte{}) }) kvps := setRandomKVPairs(t, prefixPrefixStore) @@ -73,33 +75,11 @@ func testPrefixStore(t *testing.T, baseStore types.KVStore, prefix []byte) { } } -func TestIAVLStorePrefix(t *testing.T) { - db := dbm.NewMemDB() - tree := iavl.NewMutableTree(db, cacheSize) - iavlStore := newIAVLStore(tree, numRecent, storeEvery) - - testPrefixStore(t, iavlStore, []byte("test")) -} - -func TestCacheKVStorePrefix(t *testing.T) { - cacheStore := newCachetypes.KVStore() - - testPrefixStore(t, cacheStore, []byte("test")) -} - -func TestGasKVStorePrefix(t *testing.T) { - meter := types.NewGasMeter(100000000) - mem := dbStoreAdapter{dbm.NewMemDB()} - gasStore := gas.NewStore(meter, types.KVGasConfig(), mem) - - testPrefixStore(t, gasStore, []byte("test")) -} - func TestPrefixStoreIterate(t *testing.T) { db := dbm.NewMemDB() - baseStore := dbStoreAdapter{db} + baseStore := dbadapter.NewStore(db) prefix := []byte("test") - prefixStore := baseStore.Prefix(prefix) + prefixStore := NewStore(baseStore, prefix) setRandomKVPairs(t, prefixStore) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index c3a3db8c3515..02b7c765cc61 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -32,18 +32,22 @@ type Store struct { keysByName map[string]types.StoreKey tracer *types.Tracer + tank *types.GasTank } var _ types.CommitMultiStore = (*Store)(nil) var _ types.Queryable = (*Store)(nil) // nolint -func NewCommitMultiStore(db dbm.DB) *Store { +func NewStore(db dbm.DB) *Store { return &Store{ db: db, storesParams: make(map[types.StoreKey]storeParams), stores: make(map[types.StoreKey]types.CommitKVStore), keysByName: make(map[string]types.StoreKey), + + tracer: new(types.Tracer), + tank: new(types.GasTank), } } @@ -52,6 +56,11 @@ func (rs *Store) GetTracer() *types.Tracer { return rs.tracer } +// Implements MultiStore +func (rs *Store) GetGasTank() *types.GasTank { + return rs.tank +} + // Implements CommitMultiStore func (rs *Store) SetPruning(pruning types.PruningStrategy) { rs.pruning = pruning @@ -168,7 +177,7 @@ func (rs *Store) Commit() types.CommitID { // Implements types.MultiStore. func (rs *Store) CacheWrap() types.CacheMultiStore { - return cachemulti.NewStore(rs.db, rs.keysByName, rs.stores, rs.tracer) + return cachemulti.NewStore(rs.db, rs.keysByName, rs.stores, rs.tracer, rs.tank) } // GetKVStore implements the types.MultiStore interface. If tracing is enabled on the @@ -268,7 +277,7 @@ func (rs *Store) loadCommitStoreFromParams(key types.StoreKey, id types.CommitID db = dbm.NewPrefixDB(rs.db, []byte("s/k:"+params.key.Name()+"/")) } - store = reflect.Zero(params.typ).Interface().(types.CommitKVStore) + store = key.NewStore().(types.CommitKVStore) err = store.LoadKVStoreVersion(db, id) if err != nil { store.SetPruning(rs.pruning) diff --git a/store/rootmulti/store_test.go b/store/rootmulti/store_test.go index f6f315b8f5fc..159d00982b6b 100644 --- a/store/rootmulti/store_test.go +++ b/store/rootmulti/store_test.go @@ -8,25 +8,25 @@ import ( "github.com/tendermint/tendermint/crypto/merkle" dbm "github.com/tendermint/tendermint/libs/db" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/store/iavl" + "github.com/cosmos/cosmos-sdk/store/types" ) const useDebugDB = false func TestStoreType(t *testing.T) { db := dbm.NewMemDB() - store := NewCommitMultiStore(db) - store.MountStoreWithDB(sdk.NewKVStoreKey("store1"), db) - + store := NewStore(db) + store.MountStoreWithDB(iavl.NewKey("store1"), db) } func TestStoreMount(t *testing.T) { db := dbm.NewMemDB() - store := NewCommitMultiStore(db) + store := NewStore(db) - key1 := sdk.NewKVStoreKey("store1") - key2 := sdk.NewKVStoreKey("store2") - dup1 := sdk.NewKVStoreKey("store1") + key1 := iavl.NewKey("store1") + key2 := iavl.NewKey("store2") + dup1 := iavl.NewKey("store1") require.NotPanics(t, func() { store.MountStoreWithDB(key1, db) }) require.NotPanics(t, func() { store.MountStoreWithDB(key2, db) }) @@ -45,7 +45,7 @@ func TestMultistoreCommitLoad(t *testing.T) { require.Nil(t, err) // New store has empty last commit. - commitID := sdk.CommitID{} + commitID := types.CommitID{} checkStore(t, store, commitID, commitID) // Make sure we can get stores by name. @@ -136,11 +136,11 @@ func TestMultiStoreQuery(t *testing.T) { require.Nil(t, garbage) // Set and commit data in one store. - store1 := multi.getStoreByName("store1").(sdk.KVStore) + store1 := multi.getStoreByName("store1").(types.KVStore) store1.Set(k, v) // ... and another. - store2 := multi.getStoreByName("store2").(sdk.KVStore) + store2 := multi.getStoreByName("store2").(types.KVStore) store2.Set(k2, v2) // Commit the multistore. @@ -155,34 +155,34 @@ func TestMultiStoreQuery(t *testing.T) { // Test bad path. query := abci.RequestQuery{Path: "/key", Data: k, Height: ver} qres := multi.Query(query) - require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeUnknownRequest), sdk.ABCICodeType(qres.Code)) + require.Equal(t, types.ToABCICode(types.CodeUnknownRequest), types.ABCICodeType(qres.Code)) query.Path = "h897fy32890rf63296r92" qres = multi.Query(query) - require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeUnknownRequest), sdk.ABCICodeType(qres.Code)) + require.Equal(t, types.ToABCICode(types.CodeUnknownRequest), types.ABCICodeType(qres.Code)) // Test invalid store name. query.Path = "/garbage/key" qres = multi.Query(query) - require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeUnknownRequest), sdk.ABCICodeType(qres.Code)) + require.Equal(t, types.ToABCICode(types.CodeUnknownRequest), types.ABCICodeType(qres.Code)) // Test valid query with data. query.Path = "/store1/key" qres = multi.Query(query) - require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeOK), sdk.ABCICodeType(qres.Code)) + require.Equal(t, types.ToABCICode(types.CodeOK), types.ABCICodeType(qres.Code)) require.Equal(t, v, qres.Value) // Test valid but empty query. query.Path = "/store2/key" query.Prove = true qres = multi.Query(query) - require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeOK), sdk.ABCICodeType(qres.Code)) + require.Equal(t, types.ToABCICode(types.CodeOK), types.ABCICodeType(qres.Code)) require.Nil(t, qres.Value) // Test store2 data. query.Data = k2 qres = multi.Query(query) - require.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeOK), sdk.ABCICodeType(qres.Code)) + require.Equal(t, types.ToABCICode(types.CodeOK), types.ABCICodeType(qres.Code)) require.Equal(t, v2, qres.Value) } @@ -190,27 +190,27 @@ func TestMultiStoreQuery(t *testing.T) { // utils func newMultiStoreWithMounts(db dbm.DB) *Store { - store := NewCommitMultiStore(db) - store.MountStoreWithDB(sdk.NewKVStoreKey("store1"), nil) - store.MountStoreWithDB(sdk.NewKVStoreKey("store2"), nil) - store.MountStoreWithDB(sdk.NewKVStoreKey("store3"), nil) + store := NewStore(db) + store.MountStoreWithDB(iavl.NewKey("store1"), nil) + store.MountStoreWithDB(iavl.NewKey("store2"), nil) + store.MountStoreWithDB(iavl.NewKey("store3"), nil) return store } -func checkStore(t *testing.T, store *Store, expect, got sdk.CommitID) { +func checkStore(t *testing.T, store *Store, expect, got types.CommitID) { require.Equal(t, expect, got) require.Equal(t, expect, store.LastCommitID()) } -func getExpectedCommitID(store *Store, ver int64) sdk.CommitID { - return sdk.CommitID{ +func getExpectedCommitID(store *Store, ver int64) types.CommitID { + return types.CommitID{ Version: ver, Hash: hashStores(store.stores), } } -func hashStores(stores map[sdk.StoreKey]sdk.CommitKVStore) []byte { +func hashStores(stores map[types.StoreKey]types.CommitKVStore) []byte { m := make(map[string]merkle.Hasher, len(stores)) for key, store := range stores { name := key.Name() diff --git a/store/store.go b/store/store.go deleted file mode 100644 index 10cc7ae9de01..000000000000 --- a/store/store.go +++ /dev/null @@ -1,9 +0,0 @@ -package store - -import ( - "github.com/cosmos/cosmos-sdk/store/gas" - "github.com/cosmos/cosmos-sdk/store/iavl" - "github.com/cosmos/cosmos-sdk/store/prefix" - "github.com/cosmos/cosmos-sdk/store/rootmulti" - "github.com/cosmos/cosmos-sdk/store/trace" -) diff --git a/store/trace/store.go b/store/trace/store.go index f4a83518e2d3..e91d6c62c6cc 100644 --- a/store/trace/store.go +++ b/store/trace/store.go @@ -147,12 +147,6 @@ func (ti *traceIterator) Close() { ti.parent.Close() } -// CacheWrap implements the KVStore interface. It panics as a Store -// cannot be cache wrapped. -func (tkv *Store) CacheWrap() types.CacheKVStore { - panic("cannot CacheWrap a Store") -} - // writeOperation writes a KVStore operation to the underlying io.Writer as // JSON-encoded data where the key/value pair is base64 encoded. // nolint: errcheck diff --git a/store/trace/store_test.go b/store/trace/store_test.go index 4d8cf9f1f329..af9fc81ae81b 100644 --- a/store/trace/store_test.go +++ b/store/trace/store_test.go @@ -264,18 +264,3 @@ func TestTestStoreReverseIterator(t *testing.T) { require.Panics(t, iterator.Next) require.NotPanics(t, iterator.Close) } - -func TestStoreGetStoreType(t *testing.T) { - memDB := dbadapter.NewStore(dbm.NewMemDB()) - store := NewEmptyStore(nil) - require.Equal(t, memDB.GetStoreType(), store.GetStoreType()) -} - -func TestStoreCacheWrap(t *testing.T) { - store := newEmptyStore(nil) - require.Panics(t, func() { store.CacheWrap() }) -} -func TestStoreCacheWrapWithTrace(t *testing.T) { - store := newEmptyStore(nil) - require.Panics(t, func() { store.CacheWrapWithTrace(nil, nil) }) -} diff --git a/store/transient/key.go b/store/transient/key.go new file mode 100644 index 000000000000..edba3fed4109 --- /dev/null +++ b/store/transient/key.go @@ -0,0 +1,37 @@ +package transient + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/store/types" +) + +var _ types.StoreKey = (*TransientStoreKey)(nil) + +// TransientStoreKey is used for indexing transient stores in a MultiStore +type TransientStoreKey struct { + name string +} + +// Constructs new TransientStoreKey +// Must return a pointer according to the ocap principle +func NewKey(name string) *TransientStoreKey { + return &TransientStoreKey{ + name: name, + } +} + +// Implements StoreKey +func (key *TransientStoreKey) Name() string { + return key.name +} + +// Implements StoreKey +func (key *TransientStoreKey) String() string { + return fmt.Sprintf("TransientStoreKey{%p, %s}", key, key.name) +} + +// Implements StoreKey +func (key *TransientStoreKey) NewStore() types.CommitStore { + return &Store{} +} diff --git a/store/types/errors.go b/store/types/errors.go index ab1254f4c2be..045483585abc 100644 --- a/store/types/errors.go +++ b/store/types/errors.go @@ -1 +1,129 @@ package types + +import ( + "fmt" + "strings" + + abci "github.com/tendermint/tendermint/abci/types" + cmn "github.com/tendermint/tendermint/libs/common" + + "github.com/cosmos/cosmos-sdk/codec" +) + +type ABCICodeType uint32 + +type CodeType uint16 + +type CodespaceType uint16 + +func ToABCICode(code CodeType) ABCICodeType { + if code == CodeOK { + return ABCICodeOK + } + return ABCICodeType((uint32(CodespaceRoot) << 16) | uint32(code)) +} + +const ( + // Using same code with sdk/errors.go to reduce confusion + ABCICodeOK ABCICodeType = 0 + + CodeOK CodeType = 0 + CodeInternal CodeType = 1 + CodeTxDecode CodeType = 2 + CodeUnknownRequest CodeType = 6 + + CodespaceRoot CodespaceType = 1 +) + +func unknownCodeMsg(code CodeType) string { + return fmt.Sprintf("unknown code %d", code) +} + +func CodeToDefaultMsg(code CodeType) string { + switch code { + case CodeInternal: + return "internal error" + case CodeTxDecode: + return "tx parse error" + case CodeUnknownRequest: + return "unknown request" + default: + return unknownCodeMsg(code) + } +} + +// ErrInternal is for internal "err"s +func ErrInternal(msg string) Error { + return newError(CodeInternal, msg) +} + +// ErrTxDecode is for syntatically invalid query request +func ErrTxDecode(msg string) Error { + return newError(CodeTxDecode, msg) +} + +// ErrUnknownRequest is for semantically invalid query request +func ErrUnknownRequest(msg string) Error { + return newError(CodeUnknownRequest, msg) +} + +type cmnError = cmn.Error + +type Error interface { + cmnError + + QueryResult() abci.ResponseQuery +} + +func newError(code CodeType, format string, args ...interface{}) Error { + if format == "" { + format = CodeToDefaultMsg(code) + } + + return &queryError{ + code: code, + cmnError: cmn.NewError(format, args...), + } +} + +type queryError struct { + code CodeType + cmnError +} + +func parseCmnError(err string) string { + if idx := strings.Index(err, "{"); idx != -1 { + err = err[idx+1 : len(err)-1] + } + return err +} + +// Copied from types/errors.go +func (err *queryError) ABCILog() string { + cdc := codec.New() + parsedErrMsg := parseCmnError(err.cmnError.Error()) + jsonErr := humanReadableError{ + Code: err.code, + Message: parsedErrMsg, + } + bz, er := cdc.MarshalJSON(jsonErr) + if er != nil { + panic(er) + } + stringifiedJSON := string(bz) + return stringifiedJSON +} + +func (err *queryError) QueryResult() abci.ResponseQuery { + return abci.ResponseQuery{ + Code: uint32(ToABCICode(err.code)), + Log: err.ABCILog(), + } +} + +type humanReadableError struct { + Codespace CodespaceType `json:"codespace"` + Code CodeType `json:"code"` + ABCICode ABCICodeType `json:"abci_code"` + Message string `json:message` +} diff --git a/store/types/gas.go b/store/types/gas.go index f94a25fd204d..03d497c328fa 100644 --- a/store/types/gas.go +++ b/store/types/gas.go @@ -111,13 +111,28 @@ type GasTank struct { Config GasConfig } -func NewGasTank(meter GasMeter, config GasConfig) *GasTank { +func NewGasTank(limit Gas, config GasConfig) *GasTank { return &GasTank{ - GasMeter: meter, + GasMeter: NewGasMeter(limit), Config: config, } } +func NewInfiniteGasTank(config GasConfig) *GasTank { + return &GasTank{ + GasMeter: NewInfiniteGasMeter(), + Config: config, + } +} + +func (tank *GasTank) SetMeter(meter GasMeter) { + tank.GasMeter = meter +} + +func (tank *GasTank) SetConfig(config GasConfig) { + tank.Config = config +} + func (tank *GasTank) HasFlat() { tank.GasMeter.ConsumeGas(tank.Config.HasCostFlat, "HasFlat") } diff --git a/store/types/types.go b/store/types/store.go similarity index 85% rename from store/types/types.go rename to store/types/store.go index b429c57fe00d..d4a5f7d12fef 100644 --- a/store/types/types.go +++ b/store/types/store.go @@ -52,6 +52,8 @@ type MultiStore interface { //nolint GetTracer() *Tracer + GetGasTank() *GasTank + // CacheWrap cache wraps // Having this method here because there is currently no // implementation of MultiStore that panics on CacheWrap(). @@ -203,28 +205,6 @@ type StoreKey interface { NewStore() CommitStore } -// KVStoreKey is used for accessing substores. -// Only the pointer value should ever be used - it functions as a capabilities key. -type KVStoreKey struct { - name string -} - -// NewKVStoreKey returns a new pointer to a KVStoreKey. -// Use a pointer so keys don't collide. -func NewKVStoreKey(name string) *KVStoreKey { - return &KVStoreKey{ - name: name, - } -} - -func (key *KVStoreKey) Name() string { - return key.name -} - -func (key *KVStoreKey) String() string { - return fmt.Sprintf("KVStoreKey{%p, %s}", key, key.name) -} - // PrefixEndBytes returns the []byte that would end a // range query for all []byte with a certain prefix // Deals with last byte of prefix being FF without overflowing @@ -251,29 +231,6 @@ func PrefixEndBytes(prefix []byte) []byte { return end } -// TransientStoreKey is used for indexing transient stores in a MultiStore -type TransientStoreKey struct { - name string -} - -// Constructs new TransientStoreKey -// Must return a pointer according to the ocap principle -func NewTransientStoreKey(name string) *TransientStoreKey { - return &TransientStoreKey{ - name: name, - } -} - -// Implements StoreKey -func (key *TransientStoreKey) Name() string { - return key.name -} - -// Implements StoreKey -func (key *TransientStoreKey) String() string { - return fmt.Sprintf("TransientStoreKey{%p, %s}", key, key.name) -} - //---------------------------------------- // key-value result for iterator queries diff --git a/types/context.go b/types/context.go index 3abda04e2c1a..8eda9bb810fb 100644 --- a/types/context.go +++ b/types/context.go @@ -198,11 +198,7 @@ func (c Context) WithBlockHeight(height int64) Context { } func (c Context) WithConsensusParams(params *abci.ConsensusParams) Context { - if params == nil { - return c - } - return c.withValue(contextKeyConsensusParams, params). - WithGasMeter(NewGasMeter(params.BlockSize.MaxGas)) + return c.withValue(contextKeyConsensusParams, params) } func (c Context) WithChainID(chainID string) Context { return c.withValue(contextKeyChainID, chainID) } diff --git a/types/errors.go b/types/errors.go index e05800b539dd..e84c5a37deff 100644 --- a/types/errors.go +++ b/types/errors.go @@ -6,8 +6,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" cmn "github.com/tendermint/tendermint/libs/common" - - abci "github.com/tendermint/tendermint/abci/types" ) // ABCICodeType - combined codetype / codespace @@ -181,7 +179,6 @@ type Error interface { ABCILog() string ABCICode() ABCICodeType Result() Result - QueryResult() abci.ResponseQuery } // NewError - create an error. @@ -279,12 +276,11 @@ func (err *sdkError) Result() Result { } } -// QueryResult allows us to return sdk.Error.QueryResult() in query responses -func (err *sdkError) QueryResult() abci.ResponseQuery { - return abci.ResponseQuery{ - Code: uint32(err.ABCICode()), - Log: err.ABCILog(), +func parseCmnError(err string) string { + if idx := strings.Index(err, "{"); idx != -1 { + err = err[idx+1 : len(err)-1] } + return err } //---------------------------------------- diff --git a/types/errors_test.go b/types/errors_test.go index 1d63e09908e2..0fb067d7c90d 100644 --- a/types/errors_test.go +++ b/types/errors_test.go @@ -60,8 +60,11 @@ func TestErrFn(t *testing.T) { require.Equal(t, err.Code(), codeType, "Err function expected to return proper code. tc #%d", i) require.Equal(t, err.Codespace(), CodespaceRoot, "Err function expected to return proper codespace. tc #%d", i) require.Equal(t, err.Result().Code, ToABCICode(CodespaceRoot, codeType), "Err function expected to return proper ABCICode. tc #%d") - require.Equal(t, err.QueryResult().Code, uint32(err.ABCICode()), "Err function expected to return proper ABCICode from QueryResult. tc #%d") - require.Equal(t, err.QueryResult().Log, err.ABCILog(), "Err function expected to return proper ABCILog from QueryResult. tc #%d") + // XXX: move to store/types/error_test.go + /* + require.Equal(t, err.QueryResult().Code, uint32(err.ABCICode()), "Err function expected to return proper ABCICode from QueryResult. tc #%d") + require.Equal(t, err.QueryResult().Log, err.ABCILog(), "Err function expected to return proper ABCILog from QueryResult. tc #%d") + */ } require.Equal(t, ABCICodeOK, ToABCICode(CodespaceRoot, CodeOK)) diff --git a/types/store.go b/types/store.go index 64b3e31e60fe..a0178d3af56f 100644 --- a/types/store.go +++ b/types/store.go @@ -1,7 +1,25 @@ package types import ( -//"github.com/cosmos/cosmos-sdk/store" + "github.com/cosmos/cosmos-sdk/store/types" ) -// XXX: reexport +// nolint: reexport +type ( + KVStore = types.KVStore + MultiStore = types.MultiStore + StoreKey = types.StoreKey + CommitID = types.CommitID + Gas = types.Gas + GasTank = types.GasTank + GasConfig = types.GasConfig + GasMeter = types.GasMeter +) + +// nolint: reexport +func NewGasTank(limit Gas, config GasConfig) *GasTank { + return types.NewGasTank(limit, config) +} +func NewInfiniteGasTank(config GasConfig) *GasTank { + return types.NewInfiniteGasTank(config) +} From 51cbe59cc9a5ce007f6860a02eb3ce5378033887 Mon Sep 17 00:00:00 2001 From: mossid Date: Sun, 16 Sep 2018 20:35:43 +0900 Subject: [PATCH 06/22] fix test in progress --- store/iavl/store.go | 2 +- store/rootmulti/store.go | 3 +- store/transient/store.go | 4 ++- store/types/gas.go | 5 ---- store/types/store.go | 6 ++-- types/context.go | 28 +++++++++++++----- types/context_test.go | 6 ++-- types/store.go | 55 ++++++++++++++++++++++++++++++----- x/stake/keeper/test_common.go | 14 ++++----- 9 files changed, 86 insertions(+), 37 deletions(-) diff --git a/store/iavl/store.go b/store/iavl/store.go index d825f6a5f5a8..de4e81ba4068 100644 --- a/store/iavl/store.go +++ b/store/iavl/store.go @@ -25,7 +25,7 @@ func (store *Store) LoadKVStoreVersion(db dbm.DB, id types.CommitID) error { return err } iavl := newIAVLStore(tree, int64(0), int64(0)) - store = iavl + *store = *iavl return nil } diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 02b7c765cc61..c6ab01cfcce4 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -280,9 +280,10 @@ func (rs *Store) loadCommitStoreFromParams(key types.StoreKey, id types.CommitID store = key.NewStore().(types.CommitKVStore) err = store.LoadKVStoreVersion(db, id) if err != nil { - store.SetPruning(rs.pruning) + return } + store.SetPruning(rs.pruning) return // XXX: move to store subdirectories LoadKVStoreVersion diff --git a/store/transient/store.go b/store/transient/store.go index 9dd37aacf660..4cf1e3c60fce 100644 --- a/store/transient/store.go +++ b/store/transient/store.go @@ -12,11 +12,13 @@ var _ types.KVStore = (*Store)(nil) // transientStore is a wrapper for a MemDB with Commiter implementation type Store struct { dbadapter.Store + + tank *types.GasTank } // Constructs new MemDB adapter func NewStore() *Store { - return &Store{dbadapter.Store{dbm.NewMemDB()}} + return &Store{dbadapter.Store{dbm.NewMemDB()}, new(types.GasTank)} } // Implements CommitStore diff --git a/store/types/gas.go b/store/types/gas.go index 03d497c328fa..5d835772288e 100644 --- a/store/types/gas.go +++ b/store/types/gas.go @@ -12,11 +12,6 @@ const ( GasDeleteDesc = "Delete" ) -var ( - cachedKVGasConfig = KVGasConfig() - cachedTransientGasConfig = TransientGasConfig() -) - // Gas measured by the SDK type Gas = int64 diff --git a/store/types/store.go b/store/types/store.go index d4a5f7d12fef..37da7314fff2 100644 --- a/store/types/store.go +++ b/store/types/store.go @@ -50,15 +50,13 @@ type MultiStore interface { //nolint // TODO: recursive multistore not yet supported // GetMultiStore(StoreKey) MultiStore - GetTracer() *Tracer - - GetGasTank() *GasTank - // CacheWrap cache wraps // Having this method here because there is currently no // implementation of MultiStore that panics on CacheWrap(). // Move this method to CacheWrapperMultiStore when needed CacheWrap() CacheMultiStore + + GetTracer() *Tracer } // From MultiStore.CacheMultiStore().... diff --git a/types/context.go b/types/context.go index 8eda9bb810fb..fbd8e5e660a3 100644 --- a/types/context.go +++ b/types/context.go @@ -10,6 +10,8 @@ import ( abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" + + "github.com/cosmos/cosmos-sdk/store/gas" ) /* @@ -72,13 +74,25 @@ func (c Context) Value(key interface{}) interface{} { } // KVStore fetches a KVStore from the MultiStore. -func (c Context) KVStore(key StoreKey) KVStore { - return c.multiStore().GetKVStore(key) -} - -// TransientStore fetches a TransientStore from the MultiStore. -func (c Context) TransientStore(key StoreKey) KVStore { - return c.multiStore().GetKVStore(key) +func (c Context) KVStore(key *KVStoreKey) KVStore { + return gas.NewStore( + &GasTank{ + GasMeter: c.GasMeter(), + Config: cachedKVGasConfig, + }, + c.multiStore().GetKVStore(key), + ) +} + +// TransientStore fetches a TransientStore from the MultiStore +func (c Context) TransientStore(key *TransientStoreKey) KVStore { + return gas.NewStore( + &GasTank{ + GasMeter: c.GasMeter(), + Config: cachedTransientGasConfig, + }, + c.multiStore().GetKVStore(key), + ) } //---------------------------------------- diff --git a/types/context_test.go b/types/context_test.go index 0ab6c8dfc79c..4cb8f1105d44 100644 --- a/types/context_test.go +++ b/types/context_test.go @@ -8,7 +8,7 @@ import ( dbm "github.com/tendermint/tendermint/libs/db" "github.com/tendermint/tendermint/libs/log" - "github.com/cosmos/cosmos-sdk/store" + "github.com/cosmos/cosmos-sdk/store/rootmulti" "github.com/cosmos/cosmos-sdk/types" abci "github.com/tendermint/tendermint/abci/types" ) @@ -54,8 +54,8 @@ func TestContextGetOpShouldNeverPanic(t *testing.T) { func defaultContext(key types.StoreKey) types.Context { db := dbm.NewMemDB() - cms := store.NewCommitMultiStore(db) - cms.MountStoreWithDB(key, types.StoreTypeIAVL, db) + cms := rootmulti.NewStore(db) + cms.MountStoreWithDB(key, db) cms.LoadLatestVersion() ctx := types.NewContext(cms, abci.Header{}, false, log.NewNopLogger()) return ctx diff --git a/types/store.go b/types/store.go index a0178d3af56f..9bdb762fdfeb 100644 --- a/types/store.go +++ b/types/store.go @@ -1,25 +1,64 @@ package types import ( + "github.com/cosmos/cosmos-sdk/store/iavl" + "github.com/cosmos/cosmos-sdk/store/transient" "github.com/cosmos/cosmos-sdk/store/types" ) // nolint: reexport type ( - KVStore = types.KVStore - MultiStore = types.MultiStore - StoreKey = types.StoreKey - CommitID = types.CommitID - Gas = types.Gas - GasTank = types.GasTank - GasConfig = types.GasConfig - GasMeter = types.GasMeter + CommitStore = types.CommitStore + KVStore = types.KVStore + CacheKVStore = types.CacheKVStore + MultiStore = types.MultiStore + CommitMultiStore = types.CommitMultiStore + CommitID = types.CommitID + Iterator = types.Iterator + + StoreKey = types.StoreKey + KVStoreKey = iavl.KVStoreKey + TransientStoreKey = transient.TransientStoreKey + + Gas = types.Gas + GasTank = types.GasTank + GasConfig = types.GasConfig + GasMeter = types.GasMeter + + ErrorOutOfGas = types.ErrorOutOfGas ) // nolint: reexport +func KVStorePrefixIterator(store KVStore, prefix []byte) Iterator { + return types.KVStorePrefixIterator(store, prefix) +} +func KVStoreReversePrefixIterator(store KVStore, prefix []byte) Iterator { + return types.KVStoreReversePrefixIterator(store, prefix) +} +func PrefixEndBytes(prefix []byte) []byte { + return types.PrefixEndBytes(prefix) +} +func NewKVStoreKey(name string) *KVStoreKey { + return iavl.NewKey(name) +} +func NewTransientStoreKey(name string) *TransientStoreKey { + return transient.NewKey(name) +} +func NewGasMeter(limit Gas) GasMeter { + return types.NewGasMeter(limit) +} +func NewInfiniteGasMeter() GasMeter { + return types.NewInfiniteGasMeter() +} func NewGasTank(limit Gas, config GasConfig) *GasTank { return types.NewGasTank(limit, config) } func NewInfiniteGasTank(config GasConfig) *GasTank { return types.NewInfiniteGasTank(config) } + +// nolint: internal +var ( + cachedKVGasConfig = types.KVGasConfig() + cachedTransientGasConfig = types.TransientGasConfig() +) diff --git a/x/stake/keeper/test_common.go b/x/stake/keeper/test_common.go index 7978f17e94f3..e83f7925c377 100644 --- a/x/stake/keeper/test_common.go +++ b/x/stake/keeper/test_common.go @@ -15,7 +15,7 @@ import ( "github.com/tendermint/tendermint/libs/log" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/store" + "github.com/cosmos/cosmos-sdk/store/rootmulti" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" @@ -83,12 +83,12 @@ func CreateTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context tkeyParams := sdk.NewTransientStoreKey("transient_params") db := dbm.NewMemDB() - ms := store.NewCommitMultiStore(db) - ms.MountStoreWithDB(tkeyStake, sdk.StoreTypeTransient, nil) - ms.MountStoreWithDB(keyStake, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) + ms := rootmulti.NewStore(db) + ms.MountStoreWithDB(tkeyStake, nil) + ms.MountStoreWithDB(keyStake, db) + ms.MountStoreWithDB(keyAcc, db) + ms.MountStoreWithDB(keyParams, db) + ms.MountStoreWithDB(tkeyParams, db) err := ms.LoadLatestVersion() require.Nil(t, err) From f0a915e50c54f2306003395fdbba782e55c3ea4a Mon Sep 17 00:00:00 2001 From: mossid Date: Sun, 16 Sep 2018 21:04:07 +0900 Subject: [PATCH 07/22] now compiles --- baseapp/baseapp.go | 59 +++++++++++++++++--------------- client/context/query.go | 12 +++---- store/rootmulti/proof.go | 10 +++--- store/rootmulti/store.go | 2 +- store/store.go | 21 ++++++++++++ store/transient/store.go | 7 ++++ store/types/store.go | 7 ++++ store/types/trace.go | 2 +- types/context.go | 4 +-- types/store.go | 19 ++++++++++ x/auth/mapper_test.go | 4 +-- x/mock/app.go | 10 +----- x/params/keeper_test.go | 9 ++--- x/params/subspace/subspace.go | 6 ++-- x/params/subspace/test_common.go | 9 ++--- x/slashing/test_common.go | 8 +++++ 16 files changed, 125 insertions(+), 64 deletions(-) create mode 100644 store/store.go diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 17942b976aa9..42369fc70f4b 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -115,7 +115,7 @@ func (app *BaseApp) Name() string { // SetCommitMultiStoreTracer sets the store tracer on the BaseApp's underlying // CommitMultiStore. func (app *BaseApp) SetCommitMultiStoreTracer(w io.Writer) { - app.cms.WithTracer(w) + app.cms.GetTracer().Writer = w } // Register the next available codespace through the baseapp's codespacer, starting from a default @@ -126,25 +126,25 @@ func (app *BaseApp) RegisterCodespace(codespace sdk.CodespaceType) sdk.Codespace // Mount IAVL stores to the provided keys in the BaseApp multistore func (app *BaseApp) MountStoresIAVL(keys ...*sdk.KVStoreKey) { for _, key := range keys { - app.MountStore(key, sdk.StoreTypeIAVL) + app.MountStore(key) } } // Mount stores to the provided keys in the BaseApp multistore func (app *BaseApp) MountStoresTransient(keys ...*sdk.TransientStoreKey) { for _, key := range keys { - app.MountStore(key, sdk.StoreTypeTransient) + app.MountStore(key) } } // Mount a store to the provided key in the BaseApp multistore, using a specified DB -func (app *BaseApp) MountStoreWithDB(key sdk.StoreKey, typ sdk.StoreType, db dbm.DB) { - app.cms.MountStoreWithDB(key, typ, db) +func (app *BaseApp) MountStoreWithDB(key sdk.StoreKey, db dbm.DB) { + app.cms.MountStoreWithDB(key, db) } // Mount a store to the provided key in the BaseApp multistore, using the default DB -func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) { - app.cms.MountStoreWithDB(key, typ, nil) +func (app *BaseApp) MountStore(key sdk.StoreKey) { + app.cms.MountStoreWithDB(key, nil) } // load latest application version @@ -158,7 +158,7 @@ func (app *BaseApp) LoadLatestVersion(mainKey sdk.StoreKey) error { // load application version func (app *BaseApp) LoadVersion(version int64, mainKey sdk.StoreKey) error { - err := app.cms.LoadVersion(version) + err := app.cms.LoadMultiStoreVersion(version) if err != nil { return err } @@ -208,11 +208,11 @@ type state struct { } func (st *state) CacheMultiStore() sdk.CacheMultiStore { - return st.ms.CacheMultiStore() + return st.ms.CacheWrap() } func (app *BaseApp) setCheckState(header abci.Header) { - ms := app.cms.CacheMultiStore() + ms := app.cms.CacheWrap() app.checkState = &state{ ms: ms, ctx: sdk.NewContext(ms, header, true, app.Logger).WithMinimumFees(app.minimumFees), @@ -220,7 +220,7 @@ func (app *BaseApp) setCheckState(header abci.Header) { } func (app *BaseApp) setDeliverState(header abci.Header) { - ms := app.cms.CacheMultiStore() + ms := app.cms.CacheWrap() app.deliverState = &state{ ms: ms, ctx: sdk.NewContext(ms, header, false, app.Logger), @@ -297,7 +297,7 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) { path := splitPath(req.Path) if len(path) == 0 { msg := "no query path provided" - return sdk.ErrUnknownRequest(msg).QueryResult() + return store.ErrUnknownRequest(msg).QueryResult() } switch path[0] { // "/app" prefix for special application queries @@ -312,7 +312,7 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) { } msg := "unknown query path" - return sdk.ErrUnknownRequest(msg).QueryResult() + return store.ErrUnknownRequest(msg).QueryResult() } func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) (res abci.ResponseQuery) { @@ -344,7 +344,7 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) (res abc } } msg := "Expected second parameter to be either simulate or version, neither was present" - return sdk.ErrUnknownRequest(msg).QueryResult() + return store.ErrUnknownRequest(msg).QueryResult() } func handleQueryStore(app *BaseApp, path []string, req abci.RequestQuery) (res abci.ResponseQuery) { @@ -352,7 +352,7 @@ func handleQueryStore(app *BaseApp, path []string, req abci.RequestQuery) (res a queryable, ok := app.cms.(sdk.Queryable) if !ok { msg := "multistore doesn't support queries" - return sdk.ErrUnknownRequest(msg).QueryResult() + return store.ErrUnknownRequest(msg).QueryResult() } req.Path = "/" + strings.Join(path[1:], "/") return queryable.Query(req) @@ -373,26 +373,26 @@ func handleQueryP2P(app *BaseApp, path []string, req abci.RequestQuery) (res abc } } else { msg := "Expected second parameter to be filter" - return sdk.ErrUnknownRequest(msg).QueryResult() + return store.ErrUnknownRequest(msg).QueryResult() } } msg := "Expected path is p2p filter " - return sdk.ErrUnknownRequest(msg).QueryResult() + return store.ErrUnknownRequest(msg).QueryResult() } func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) (res abci.ResponseQuery) { // path[0] should be "custom" because "/custom" prefix is required for keeper queries. // the queryRouter routes using path[1]. For example, in the path "custom/gov/proposal", queryRouter routes using "gov" if len(path) < 2 || path[1] == "" { - return sdk.ErrUnknownRequest("No route for custom query specified").QueryResult() + return store.ErrUnknownRequest("No route for custom query specified").QueryResult() } querier := app.queryRouter.Route(path[1]) if querier == nil { - return sdk.ErrUnknownRequest(fmt.Sprintf("no custom querier found for route %s", path[1])).QueryResult() + return store.ErrUnknownRequest(fmt.Sprintf("no custom querier found for route %s", path[1])).QueryResult() } - ctx := sdk.NewContext(app.cms.CacheMultiStore(), app.checkState.ctx.BlockHeader(), true, app.Logger). + ctx := sdk.NewContext(app.cms.CacheWrap(), app.checkState.ctx.BlockHeader(), true, app.Logger). WithMinimumFees(app.minimumFees) // Passes the rest of the path as an argument to the querier. // For example, in the path "custom/gov/proposal/test", the gov querier gets []string{"proposal", "test"} as the path @@ -411,9 +411,10 @@ func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) (res // BeginBlock implements the ABCI application interface. func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) { - if app.cms.TracingEnabled() { - app.cms.ResetTraceContext() - app.cms.WithTracingContext(sdk.TraceContext( + tracer := app.cms.GetTracer() + if tracer.Enabled() { + tracer.ResetContext() + tracer.SetContext(sdk.TraceContext( map[string]interface{}{"blockHeight": req.Header.Height}, )) } @@ -644,10 +645,11 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk // Keep the state in a transient CacheWrap in case processing the messages // fails. msCache = getState(app, mode).CacheMultiStore() - if msCache.TracingEnabled() { - msCache = msCache.WithTracingContext(sdk.TraceContext( + tracer := msCache.GetTracer() + if tracer.Enabled() { + tracer.SetContext(sdk.TraceContext( map[string]interface{}{"txHash": cmn.HexBytes(tmhash.Sum(txBytes)).String()}, - )).(sdk.CacheMultiStore) + )) } ctx = ctx.WithMultiStore(msCache) @@ -664,8 +666,9 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk // EndBlock implements the ABCI application interface. func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) { - if app.deliverState.ms.TracingEnabled() { - app.deliverState.ms = app.deliverState.ms.ResetTraceContext().(sdk.CacheMultiStore) + tracer := app.deliverState.ms.GetTracer() + if tracer.Enabled() { + tracer.ResetContext() } if app.endBlocker != nil { diff --git a/client/context/query.go b/client/context/query.go index 8fca9becff07..fd837d1a1975 100644 --- a/client/context/query.go +++ b/client/context/query.go @@ -11,7 +11,7 @@ import ( "strings" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/store" + "github.com/cosmos/cosmos-sdk/store/rootmulti" abci "github.com/tendermint/tendermint/abci/types" cmn "github.com/tendermint/tendermint/libs/common" tmliteErr "github.com/tendermint/tendermint/lite/errors" @@ -209,7 +209,7 @@ func (ctx CLIContext) verifyProof(_ string, resp abci.ResponseQuery) error { return err } - var multiStoreProof store.MultiStoreProof + var multiStoreProof rootmulti.Proof cdc := codec.New() err = cdc.UnmarshalBinary(resp.Proof, &multiStoreProof) @@ -217,15 +217,15 @@ func (ctx CLIContext) verifyProof(_ string, resp abci.ResponseQuery) error { return errors.Wrap(err, "failed to unmarshalBinary rangeProof") } - // verify the substore commit hash against trusted appHash - substoreCommitHash, err := store.VerifyMultiStoreCommitInfo( + // Verify the substore commit hash against trusted appHash + substoreCommitHash, err := rootmulti.VerifyMultiStoreCommitInfo( multiStoreProof.StoreName, multiStoreProof.StoreInfos, commit.Header.AppHash, ) if err != nil { return errors.Wrap(err, "failed in verifying the proof against appHash") } - err = store.VerifyRangeProof(resp.Key, resp.Value, substoreCommitHash, &multiStoreProof.RangeProof) + err = rootmulti.VerifyRangeProof(resp.Key, resp.Value, substoreCommitHash, &multiStoreProof.RangeProof) if err != nil { return errors.Wrap(err, "failed in the range proof verification") } @@ -252,7 +252,7 @@ func isQueryStoreWithProof(path string) bool { return false } - if store.RequireProof("/" + paths[2]) { + if rootmulti.RequireProof("/" + paths[2]) { return true } diff --git a/store/rootmulti/proof.go b/store/rootmulti/proof.go index bf8a1a6325f2..760001297fbc 100644 --- a/store/rootmulti/proof.go +++ b/store/rootmulti/proof.go @@ -8,19 +8,19 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" ) -// MultiStoreProof defines a collection of store proofs in a multi-store -type MultiStoreProof struct { +// Proof defines a collection of store proofs in a multi-store +type Proof struct { StoreInfos []storeInfo StoreName string RangeProof iavl.RangeProof } -// buildMultiStoreProof build MultiStoreProof based on iavl proof and storeInfos -func buildMultiStoreProof(iavlProof []byte, storeName string, storeInfos []storeInfo) []byte { +// buildProof build Proof based on iavl proof and storeInfos +func buildProof(iavlProof []byte, storeName string, storeInfos []storeInfo) []byte { var rangeProof iavl.RangeProof cdc.MustUnmarshalBinary(iavlProof, &rangeProof) - msp := MultiStoreProof{ + msp := Proof{ StoreInfos: storeInfos, StoreName: storeName, RangeProof: rangeProof, diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index c6ab01cfcce4..15b9c59db1e1 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -246,7 +246,7 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery { return types.ErrInternal(errMsg.Error()).QueryResult() } - res.Proof = buildMultiStoreProof(res.Proof, storeName, commitInfo.StoreInfos) + res.Proof = buildProof(res.Proof, storeName, commitInfo.StoreInfos) return res } diff --git a/store/store.go b/store/store.go new file mode 100644 index 000000000000..7daf39a91067 --- /dev/null +++ b/store/store.go @@ -0,0 +1,21 @@ +package store + +import ( + dbm "github.com/tendermint/tendermint/libs/db" + + "github.com/cosmos/cosmos-sdk/store/rootmulti" + "github.com/cosmos/cosmos-sdk/store/types" +) + +// nolint: reexport +type ( + CommitMultiStore = types.CommitMultiStore +) + +// nolint: reexport +func NewCommitMultiStore(db dbm.DB) *rootmulti.Store { + return rootmulti.NewStore(db) +} +func ErrUnknownRequest(msg string) types.Error { + return types.ErrUnknownRequest(msg) +} diff --git a/store/transient/store.go b/store/transient/store.go index 4cf1e3c60fce..b88f06c9d409 100644 --- a/store/transient/store.go +++ b/store/transient/store.go @@ -8,6 +8,7 @@ import ( ) var _ types.KVStore = (*Store)(nil) +var _ types.CommitKVStore = (*Store)(nil) // transientStore is a wrapper for a MemDB with Commiter implementation type Store struct { @@ -28,6 +29,12 @@ func (ts *Store) Commit() (id types.CommitID) { return } +// Implements LoadKVStore +func (ts *Store) LoadKVStoreVersion(db dbm.DB, id types.CommitID) error { + *ts = *NewStore() + return nil +} + // Implements CommitStore func (ts *Store) SetPruning(pruning types.PruningStrategy) { } diff --git a/store/types/store.go b/store/types/store.go index 37da7314fff2..1f6837c31bdf 100644 --- a/store/types/store.go +++ b/store/types/store.go @@ -229,6 +229,13 @@ func PrefixEndBytes(prefix []byte) []byte { return end } +// InclusiveEndBytes returns the []byte that would end a +// range query such that the input would be included +func InclusiveEndBytes(inclusiveBytes []byte) (exclusiveBytes []byte) { + exclusiveBytes = append(inclusiveBytes, byte(0x00)) + return exclusiveBytes +} + //---------------------------------------- // key-value result for iterator queries diff --git a/store/types/trace.go b/store/types/trace.go index 9b2515c8a489..e0f9e31e98dc 100644 --- a/store/types/trace.go +++ b/store/types/trace.go @@ -29,7 +29,7 @@ func (t *Tracer) SetWriter(w io.Writer) { // WithContext sets the tracing context for a Tracer. It is implied that // the caller should update the context when necessary between tracing // operations. -func (t *Tracer) WithContext(tc TraceContext) { +func (t *Tracer) SetContext(tc TraceContext) { if t.Context != nil { for k, v := range tc { t.Context[k] = v diff --git a/types/context.go b/types/context.go index fbd8e5e660a3..702cea7e3cda 100644 --- a/types/context.go +++ b/types/context.go @@ -74,7 +74,7 @@ func (c Context) Value(key interface{}) interface{} { } // KVStore fetches a KVStore from the MultiStore. -func (c Context) KVStore(key *KVStoreKey) KVStore { +func (c Context) KVStore(key StoreKey) KVStore { return gas.NewStore( &GasTank{ GasMeter: c.GasMeter(), @@ -85,7 +85,7 @@ func (c Context) KVStore(key *KVStoreKey) KVStore { } // TransientStore fetches a TransientStore from the MultiStore -func (c Context) TransientStore(key *TransientStoreKey) KVStore { +func (c Context) TransientStore(key StoreKey) KVStore { return gas.NewStore( &GasTank{ GasMeter: c.GasMeter(), diff --git a/types/store.go b/types/store.go index 9bdb762fdfeb..29c17f83b987 100644 --- a/types/store.go +++ b/types/store.go @@ -8,14 +8,20 @@ import ( // nolint: reexport type ( + PruningStrategy = types.PruningStrategy CommitStore = types.CommitStore + Queryable = types.Queryable KVStore = types.KVStore CacheKVStore = types.CacheKVStore + CommitKVStore = types.CommitKVStore MultiStore = types.MultiStore + CacheMultiStore = types.CacheMultiStore CommitMultiStore = types.CommitMultiStore CommitID = types.CommitID Iterator = types.Iterator + KVPair = types.KVPair + StoreKey = types.StoreKey KVStoreKey = iavl.KVStoreKey TransientStoreKey = transient.TransientStoreKey @@ -25,9 +31,19 @@ type ( GasConfig = types.GasConfig GasMeter = types.GasMeter + Tracer = types.Tracer + TraceContext = types.TraceContext + ErrorOutOfGas = types.ErrorOutOfGas ) +// nolint: reexport +const ( + PruneNothing = types.PruneNothing + PruneEverything = types.PruneEverything + PruneSyncable = types.PruneSyncable +) + // nolint: reexport func KVStorePrefixIterator(store KVStore, prefix []byte) Iterator { return types.KVStorePrefixIterator(store, prefix) @@ -38,6 +54,9 @@ func KVStoreReversePrefixIterator(store KVStore, prefix []byte) Iterator { func PrefixEndBytes(prefix []byte) []byte { return types.PrefixEndBytes(prefix) } +func InclusiveEndBytes(inclusiveBytes []byte) []byte { + return types.InclusiveEndBytes(inclusiveBytes) +} func NewKVStoreKey(name string) *KVStoreKey { return iavl.NewKey(name) } diff --git a/x/auth/mapper_test.go b/x/auth/mapper_test.go index 326f83dcca10..682dab2418ad 100644 --- a/x/auth/mapper_test.go +++ b/x/auth/mapper_test.go @@ -19,8 +19,8 @@ func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey) { capKey := sdk.NewKVStoreKey("capkey") capKey2 := sdk.NewKVStoreKey("capkey2") ms := store.NewCommitMultiStore(db) - ms.MountStoreWithDB(capKey, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(capKey2, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(capKey, db) + ms.MountStoreWithDB(capKey2, db) ms.LoadLatestVersion() return ms, capKey, capKey2 } diff --git a/x/mock/app.go b/x/mock/app.go index 627434a513d9..bf00f2f17225 100644 --- a/x/mock/app.go +++ b/x/mock/app.go @@ -1,7 +1,6 @@ package mock import ( - "fmt" "math/rand" "os" @@ -81,14 +80,7 @@ func (app *App) CompleteSetup(newKeys ...sdk.StoreKey) error { newKeys = append(newKeys, app.KeyAccount) for _, key := range newKeys { - switch key.(type) { - case *sdk.KVStoreKey: - app.MountStore(key, sdk.StoreTypeIAVL) - case *sdk.TransientStoreKey: - app.MountStore(key, sdk.StoreTypeTransient) - default: - return fmt.Errorf("unsupported StoreKey: %+v", key) - } + app.MountStore(key) } err := app.LoadLatestVersion(app.KeyMain) diff --git a/x/params/keeper_test.go b/x/params/keeper_test.go index 585db3c410e5..3361fc002d71 100644 --- a/x/params/keeper_test.go +++ b/x/params/keeper_test.go @@ -12,14 +12,15 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store" + "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" ) func defaultContext(key sdk.StoreKey, tkey sdk.StoreKey) sdk.Context { db := dbm.NewMemDB() cms := store.NewCommitMultiStore(db) - cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db) - cms.MountStoreWithDB(tkey, sdk.StoreTypeTransient, db) + cms.MountStoreWithDB(key, db) + cms.MountStoreWithDB(tkey, db) cms.LoadLatestVersion() ctx := sdk.NewContext(cms, abci.Header{}, false, log.NewNopLogger()) return ctx @@ -71,7 +72,7 @@ func TestKeeper(t *testing.T) { ctx := defaultContext(skey, tkey) keeper := NewKeeper(cdc, skey, tkey) space := keeper.Subspace("test").WithTypeTable(table) - store := ctx.KVStore(skey).Prefix([]byte("test/")) + store := prefix.NewStore(ctx.KVStore(skey), []byte("test/")) // Set params for i, kv := range kvs { @@ -177,7 +178,7 @@ func TestSubspace(t *testing.T) { []byte("struct"), s{}, ) - store := ctx.KVStore(key).Prefix([]byte("test/")) + store := prefix.NewStore(ctx.KVStore(key), []byte("test/")) space := keeper.Subspace("test").WithTypeTable(table) // Test space.Set, space.Modified diff --git a/x/params/subspace/subspace.go b/x/params/subspace/subspace.go index fe5889a908ca..313b41795e7c 100644 --- a/x/params/subspace/subspace.go +++ b/x/params/subspace/subspace.go @@ -5,6 +5,8 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/cosmos-sdk/store/prefix" ) // Individual parameter store for each keeper @@ -61,14 +63,14 @@ func (s Subspace) WithTypeTable(table TypeTable) Subspace { func (s Subspace) kvStore(ctx sdk.Context) sdk.KVStore { // append here is safe, appends within a function won't cause // weird side effects when its singlethreaded - return ctx.KVStore(s.key).Prefix(append(s.name, '/')) + return prefix.NewStore(ctx.KVStore(s.key), append(s.name, '/')) } // Returns a KVStore identical with ctx.TransientStore(s.tkey).Prefix() func (s Subspace) transientStore(ctx sdk.Context) sdk.KVStore { // append here is safe, appends within a function won't cause // weird side effects when its singlethreaded - return ctx.TransientStore(s.tkey).Prefix(append(s.name, '/')) + return prefix.NewStore(ctx.TransientStore(s.tkey), append(s.name, '/')) } // Get parameter from store diff --git a/x/params/subspace/test_common.go b/x/params/subspace/test_common.go index e3d980a72a4d..bd8c071a08d8 100644 --- a/x/params/subspace/test_common.go +++ b/x/params/subspace/test_common.go @@ -27,10 +27,11 @@ func DefaultTestComponents(t *testing.T, table TypeTable) (sdk.Context, Subspace tkey := sdk.NewTransientStoreKey("tparams") db := dbm.NewMemDB() ms := store.NewCommitMultiStore(db) - ms.WithTracer(os.Stdout) - ms.WithTracingContext(sdk.TraceContext{}) - ms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(tkey, sdk.StoreTypeTransient, db) + tracer := ms.GetTracer() + tracer.SetWriter(os.Stdout) + tracer.SetContext(sdk.TraceContext{}) + ms.MountStoreWithDB(key, db) + ms.MountStoreWithDB(tkey, db) err := ms.LoadLatestVersion() require.Nil(t, err) ctx := sdk.NewContext(ms, abci.Header{}, false, log.NewTMLogger(os.Stdout)) diff --git a/x/slashing/test_common.go b/x/slashing/test_common.go index 55a6fda18f49..c5e9afa64b3b 100644 --- a/x/slashing/test_common.go +++ b/x/slashing/test_common.go @@ -58,12 +58,20 @@ func createTestInput(t *testing.T, defaults Params) (sdk.Context, bank.Keeper, s tkeyParams := sdk.NewTransientStoreKey("transient_params") db := dbm.NewMemDB() ms := store.NewCommitMultiStore(db) +<<<<<<< HEAD ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(tkeyStake, sdk.StoreTypeTransient, nil) ms.MountStoreWithDB(keyStake, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keySlashing, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) +======= + ms.MountStoreWithDB(keyAcc, db) + ms.MountStoreWithDB(tkeyStake, nil) + ms.MountStoreWithDB(keyStake, db) + ms.MountStoreWithDB(keySlashing, db) + ms.MountStoreWithDB(keyParams, db) +>>>>>>> eb1d017... now compiles err := ms.LoadLatestVersion() require.Nil(t, err) ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewTMLogger(os.Stdout)) From fd6e0f8e4af6d36abf3d03e734f236bd031b5d36 Mon Sep 17 00:00:00 2001 From: mossid Date: Mon, 17 Sep 2018 00:02:03 +0900 Subject: [PATCH 08/22] test passes --- cmd/gaia/cmd/gaiadebug/hack.go | 2 +- .../democoin/x/assoc/validator_set_test.go | 5 +- examples/democoin/x/cool/keeper_test.go | 2 +- examples/democoin/x/oracle/oracle_test.go | 2 +- examples/democoin/x/pow/keeper_test.go | 2 +- .../democoin/x/simplestake/keeper_test.go | 4 +- server/mock/store.go | 154 ------------------ server/mock/store_test.go | 33 ---- store/{ => list}/list.go | 6 +- store/{ => list}/list_test.go | 30 +++- store/prefix/store_test.go | 34 ++-- store/{ => queue}/queue.go | 32 ++-- store/{ => queue}/queue_test.go | 18 +- store/{ => types}/firstlast.go | 8 +- x/bank/keeper_test.go | 2 +- x/distribution/keeper/test_common.go | 14 +- x/ibc/ibc_test.go | 2 +- x/slashing/test_common.go | 10 +- 18 files changed, 101 insertions(+), 259 deletions(-) delete mode 100644 server/mock/store.go delete mode 100644 server/mock/store_test.go rename store/{ => list}/list.go (96%) rename store/{ => list}/list_test.go (67%) rename store/{ => queue}/queue.go (74%) rename store/{ => queue}/queue_test.go (80%) rename store/{ => types}/firstlast.go (80%) diff --git a/cmd/gaia/cmd/gaiadebug/hack.go b/cmd/gaia/cmd/gaiadebug/hack.go index 734a83df304f..5e307709ac5e 100644 --- a/cmd/gaia/cmd/gaiadebug/hack.go +++ b/cmd/gaia/cmd/gaiadebug/hack.go @@ -189,7 +189,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp app.SetEndBlocker(app.EndBlocker) app.SetAnteHandler(auth.NewAnteHandler(app.accountKeeper, app.feeCollectionKeeper)) app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyStake, app.keySlashing, app.keyParams) - app.MountStore(app.tkeyParams, sdk.StoreTypeTransient) + app.MountStore(app.tkeyParams) err := app.LoadLatestVersion(app.keyMain) if err != nil { cmn.Exit(err.Error()) diff --git a/examples/democoin/x/assoc/validator_set_test.go b/examples/democoin/x/assoc/validator_set_test.go index 9fc6526f8d3e..2215c51187c8 100644 --- a/examples/democoin/x/assoc/validator_set_test.go +++ b/examples/democoin/x/assoc/validator_set_test.go @@ -12,13 +12,14 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/examples/democoin/mock" "github.com/cosmos/cosmos-sdk/store" + "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" ) func defaultContext(key sdk.StoreKey) sdk.Context { db := dbm.NewMemDB() cms := store.NewCommitMultiStore(db) - cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db) + cms.MountStoreWithDB(key, db) cms.LoadLatestVersion() ctx := sdk.NewContext(cms, abci.Header{}, false, nil) return ctx @@ -36,7 +37,7 @@ func TestValidatorSet(t *testing.T) { {addr2, sdk.NewDec(2)}, }} - valset := NewValidatorSet(codec.New(), ctx.KVStore(key).Prefix([]byte("assoc")), base, 1, 5) + valset := NewValidatorSet(codec.New(), prefix.NewStore(ctx.KVStore(key), []byte("assoc")), base, 1, 5) require.Equal(t, base.Validator(ctx, addr1), valset.Validator(ctx, addr1)) require.Equal(t, base.Validator(ctx, addr2), valset.Validator(ctx, addr2)) diff --git a/examples/democoin/x/cool/keeper_test.go b/examples/democoin/x/cool/keeper_test.go index 1eb40dfb295c..451c43e680fa 100644 --- a/examples/democoin/x/cool/keeper_test.go +++ b/examples/democoin/x/cool/keeper_test.go @@ -19,7 +19,7 @@ func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey) { db := dbm.NewMemDB() capKey := sdk.NewKVStoreKey("capkey") ms := store.NewCommitMultiStore(db) - ms.MountStoreWithDB(capKey, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(capKey, db) ms.LoadLatestVersion() return ms, capKey } diff --git a/examples/democoin/x/oracle/oracle_test.go b/examples/democoin/x/oracle/oracle_test.go index 7621ea86090f..1672a3b7c1a8 100644 --- a/examples/democoin/x/oracle/oracle_test.go +++ b/examples/democoin/x/oracle/oracle_test.go @@ -19,7 +19,7 @@ func defaultContext(keys ...sdk.StoreKey) sdk.Context { db := dbm.NewMemDB() cms := store.NewCommitMultiStore(db) for _, key := range keys { - cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db) + cms.MountStoreWithDB(key, db) } cms.LoadLatestVersion() ctx := sdk.NewContext(cms, abci.Header{}, false, nil) diff --git a/examples/democoin/x/pow/keeper_test.go b/examples/democoin/x/pow/keeper_test.go index 86ccbc8c0130..b833c05a2f01 100644 --- a/examples/democoin/x/pow/keeper_test.go +++ b/examples/democoin/x/pow/keeper_test.go @@ -21,7 +21,7 @@ func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey) { db := dbm.NewMemDB() capKey := sdk.NewKVStoreKey("capkey") ms := store.NewCommitMultiStore(db) - ms.MountStoreWithDB(capKey, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(capKey, db) ms.LoadLatestVersion() return ms, capKey diff --git a/examples/democoin/x/simplestake/keeper_test.go b/examples/democoin/x/simplestake/keeper_test.go index c3876cf4adbe..36dc614f0fbc 100644 --- a/examples/democoin/x/simplestake/keeper_test.go +++ b/examples/democoin/x/simplestake/keeper_test.go @@ -24,8 +24,8 @@ func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey) { authKey := sdk.NewKVStoreKey("authkey") capKey := sdk.NewKVStoreKey("capkey") ms := store.NewCommitMultiStore(db) - ms.MountStoreWithDB(capKey, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(authKey, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(capKey, db) + ms.MountStoreWithDB(authKey, db) ms.LoadLatestVersion() return ms, authKey, capKey } diff --git a/server/mock/store.go b/server/mock/store.go deleted file mode 100644 index ec963a1bc22c..000000000000 --- a/server/mock/store.go +++ /dev/null @@ -1,154 +0,0 @@ -package mock - -import ( - "io" - - dbm "github.com/tendermint/tendermint/libs/db" - - sdk "github.com/cosmos/cosmos-sdk/types" -) - -var _ sdk.MultiStore = multiStore{} - -type multiStore struct { - kv map[sdk.StoreKey]kvStore -} - -func (ms multiStore) CacheMultiStore() sdk.CacheMultiStore { - panic("not implemented") -} - -func (ms multiStore) CacheWrap() sdk.CacheWrap { - panic("not implemented") -} - -func (ms multiStore) CacheWrapWithTrace(_ io.Writer, _ sdk.TraceContext) sdk.CacheWrap { - panic("not implemented") -} - -func (ms multiStore) ResetTraceContext() sdk.MultiStore { - panic("not implemented") -} - -func (ms multiStore) TracingEnabled() bool { - panic("not implemented") -} - -func (ms multiStore) WithTracingContext(tc sdk.TraceContext) sdk.MultiStore { - panic("not implemented") -} - -func (ms multiStore) WithTracer(w io.Writer) sdk.MultiStore { - panic("not implemented") -} - -func (ms multiStore) Commit() sdk.CommitID { - panic("not implemented") -} - -func (ms multiStore) LastCommitID() sdk.CommitID { - panic("not implemented") -} - -func (ms multiStore) SetPruning(s sdk.PruningStrategy) { - panic("not implemented") -} - -func (ms multiStore) GetCommitKVStore(key sdk.StoreKey) sdk.CommitKVStore { - panic("not implemented") -} - -func (ms multiStore) GetCommitStore(key sdk.StoreKey) sdk.CommitStore { - panic("not implemented") -} - -func (ms multiStore) MountStoreWithDB(key sdk.StoreKey, typ sdk.StoreType, db dbm.DB) { - ms.kv[key] = kvStore{store: make(map[string][]byte)} -} - -func (ms multiStore) LoadLatestVersion() error { - return nil -} - -func (ms multiStore) LoadVersion(ver int64) error { - panic("not implemented") -} - -func (ms multiStore) GetKVStore(key sdk.StoreKey) sdk.KVStore { - return ms.kv[key] -} - -func (ms multiStore) GetStore(key sdk.StoreKey) sdk.Store { - panic("not implemented") -} - -func (ms multiStore) GetStoreType() sdk.StoreType { - panic("not implemented") -} - -var _ sdk.KVStore = kvStore{} - -type kvStore struct { - store map[string][]byte -} - -func (kv kvStore) CacheWrap() sdk.CacheWrap { - panic("not implemented") -} - -func (kv kvStore) CacheWrapWithTrace(w io.Writer, tc sdk.TraceContext) sdk.CacheWrap { - panic("not implemented") -} - -func (kv kvStore) GetStoreType() sdk.StoreType { - panic("not implemented") -} - -func (kv kvStore) Get(key []byte) []byte { - v, ok := kv.store[string(key)] - if !ok { - return nil - } - return v -} - -func (kv kvStore) Has(key []byte) bool { - _, ok := kv.store[string(key)] - return ok -} - -func (kv kvStore) Set(key, value []byte) { - kv.store[string(key)] = value -} - -func (kv kvStore) Delete(key []byte) { - delete(kv.store, string(key)) -} - -func (kv kvStore) Prefix(prefix []byte) sdk.KVStore { - panic("not implemented") -} - -func (kv kvStore) Gas(meter sdk.GasMeter, config sdk.GasConfig) sdk.KVStore { - panic("not implmeneted") -} - -func (kv kvStore) Iterator(start, end []byte) sdk.Iterator { - panic("not implemented") -} - -func (kv kvStore) ReverseIterator(start, end []byte) sdk.Iterator { - panic("not implemented") -} - -func (kv kvStore) SubspaceIterator(prefix []byte) sdk.Iterator { - panic("not implemented") -} - -func (kv kvStore) ReverseSubspaceIterator(prefix []byte) sdk.Iterator { - panic("not implemented") -} - -func NewCommitMultiStore() sdk.CommitMultiStore { - return multiStore{kv: make(map[sdk.StoreKey]kvStore)} -} diff --git a/server/mock/store_test.go b/server/mock/store_test.go deleted file mode 100644 index 0ea5ffd2c7a0..000000000000 --- a/server/mock/store_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package mock - -import ( - "testing" - - "github.com/stretchr/testify/require" - - dbm "github.com/tendermint/tendermint/libs/db" - - sdk "github.com/cosmos/cosmos-sdk/types" -) - -func TestStore(t *testing.T) { - db := dbm.NewMemDB() - cms := NewCommitMultiStore() - - key := sdk.NewKVStoreKey("test") - cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db) - err := cms.LoadLatestVersion() - require.Nil(t, err) - - store := cms.GetKVStore(key) - require.NotNil(t, store) - - k := []byte("hello") - v := []byte("world") - require.False(t, store.Has(k)) - store.Set(k, v) - require.True(t, store.Has(k)) - require.Equal(t, v, store.Get(k)) - store.Delete(k) - require.False(t, store.Has(k)) -} diff --git a/store/list.go b/store/list/list.go similarity index 96% rename from store/list.go rename to store/list/list.go index 9905b0d6f5be..5e304ae19bb1 100644 --- a/store/list.go +++ b/store/list/list.go @@ -1,4 +1,4 @@ -package store +package list import ( "fmt" @@ -25,8 +25,8 @@ type List struct { store sdk.KVStore } -// NewList constructs new List -func NewList(cdc *codec.Codec, store sdk.KVStore) List { +// New constructs new List +func New(cdc *codec.Codec, store sdk.KVStore) List { return List{ cdc: cdc, store: store, diff --git a/store/list_test.go b/store/list/list_test.go similarity index 67% rename from store/list_test.go rename to store/list/list_test.go index 396e2d1a1996..8a097d6eb28c 100644 --- a/store/list_test.go +++ b/store/list/list_test.go @@ -1,18 +1,40 @@ -package store +package list import ( "math/rand" "testing" + abci "github.com/tendermint/tendermint/abci/types" + dbm "github.com/tendermint/tendermint/libs/db" + "github.com/tendermint/tendermint/libs/log" + + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/store/rootmulti" ) +type S struct { + I uint64 + B bool +} + +func defaultComponents(key sdk.StoreKey) (sdk.Context, *codec.Codec) { + db := dbm.NewMemDB() + cms := rootmulti.NewStore(db) + cms.MountStoreWithDB(key, 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) + lm := New(cdc, store) val := S{1, true} var res S @@ -54,11 +76,11 @@ func TestList(t *testing.T) { require.Equal(t, S{3, true}, res) } -func TestListRandom(t *testing.T) { +func TestRandom(t *testing.T) { key := sdk.NewKVStoreKey("test") ctx, cdc := defaultComponents(key) store := ctx.KVStore(key) - list := NewList(cdc, store) + list := New(cdc, store) mocklist := []uint32{} for i := 0; i < 100; i++ { diff --git a/store/prefix/store_test.go b/store/prefix/store_test.go index 7a492a63854f..d83c628177cd 100644 --- a/store/prefix/store_test.go +++ b/store/prefix/store_test.go @@ -123,11 +123,11 @@ func TestCloneAppend(t *testing.T) { func TestPrefixStoreIteratorEdgeCase(t *testing.T) { db := dbm.NewMemDB() - baseStore := dbStoreAdapter{db} + baseStore := dbadapter.NewStore(db) // overflow in cpIncr prefix := []byte{0xAA, 0xFF, 0xFF} - prefixStore := baseStore.Prefix(prefix) + prefixStore := NewStore(baseStore, prefix) // ascending order baseStore.Set([]byte{0xAA, 0xFF, 0xFE}, []byte{}) @@ -153,11 +153,11 @@ func TestPrefixStoreIteratorEdgeCase(t *testing.T) { func TestPrefixStoreReverseIteratorEdgeCase(t *testing.T) { db := dbm.NewMemDB() - baseStore := dbStoreAdapter{db} + baseStore := dbadapter.NewStore(db) // overflow in cpIncr prefix := []byte{0xAA, 0xFF, 0xFF} - prefixStore := baseStore.Prefix(prefix) + prefixStore := NewStore(baseStore, prefix) // descending order baseStore.Set([]byte{0xAB, 0x00, 0x00}, []byte{}) @@ -181,11 +181,11 @@ func TestPrefixStoreReverseIteratorEdgeCase(t *testing.T) { iter.Close() db = dbm.NewMemDB() - baseStore = dbStoreAdapter{db} + baseStore = dbadapter.NewStore(db) // underflow in cpDecr prefix = []byte{0xAA, 0x00, 0x00} - prefixStore = baseStore.Prefix(prefix) + prefixStore = NewStore(baseStore, prefix) baseStore.Set([]byte{0xAB, 0x00, 0x01, 0x00, 0x00}, []byte{}) baseStore.Set([]byte{0xAB, 0x00, 0x01, 0x00}, []byte{}) @@ -212,7 +212,7 @@ func TestPrefixStoreReverseIteratorEdgeCase(t *testing.T) { func mockStoreWithStuff() types.KVStore { db := dbm.NewMemDB() - store := dbStoreAdapter{db} + store := dbadapter.NewStore(db) // Under "key" prefix store.Set(bz("key"), bz("value")) store.Set(bz("key1"), bz("value1")) @@ -274,7 +274,7 @@ func checkNextPanics(t *testing.T, itr types.Iterator) { func TestPrefixDBSimple(t *testing.T) { store := mockStoreWithStuff() - pstore := store.Prefix(bz("key")) + pstore := NewStore(store, bz("key")) checkValue(t, pstore, bz("key"), nil) checkValue(t, pstore, bz(""), bz("value")) @@ -292,7 +292,7 @@ func TestPrefixDBSimple(t *testing.T) { func TestPrefixDBIterator1(t *testing.T) { store := mockStoreWithStuff() - pstore := store.Prefix(bz("key")) + pstore := NewStore(store, bz("key")) itr := pstore.Iterator(nil, nil) checkDomain(t, itr, nil, nil) @@ -310,7 +310,7 @@ func TestPrefixDBIterator1(t *testing.T) { func TestPrefixDBIterator2(t *testing.T) { store := mockStoreWithStuff() - pstore := store.Prefix(bz("key")) + pstore := NewStore(store, bz("key")) itr := pstore.Iterator(nil, bz("")) checkDomain(t, itr, nil, bz("")) @@ -320,7 +320,7 @@ func TestPrefixDBIterator2(t *testing.T) { func TestPrefixDBIterator3(t *testing.T) { store := mockStoreWithStuff() - pstore := store.Prefix(bz("key")) + pstore := NewStore(store, bz("key")) itr := pstore.Iterator(bz(""), nil) checkDomain(t, itr, bz(""), nil) @@ -338,7 +338,7 @@ func TestPrefixDBIterator3(t *testing.T) { func TestPrefixDBIterator4(t *testing.T) { store := mockStoreWithStuff() - pstore := store.Prefix(bz("key")) + pstore := NewStore(store, bz("key")) itr := pstore.Iterator(bz(""), bz("")) checkDomain(t, itr, bz(""), bz("")) @@ -348,7 +348,7 @@ func TestPrefixDBIterator4(t *testing.T) { func TestPrefixDBReverseIterator1(t *testing.T) { store := mockStoreWithStuff() - pstore := store.Prefix(bz("key")) + pstore := NewStore(store, bz("key")) itr := pstore.ReverseIterator(nil, nil) checkDomain(t, itr, nil, nil) @@ -366,7 +366,7 @@ func TestPrefixDBReverseIterator1(t *testing.T) { func TestPrefixDBReverseIterator2(t *testing.T) { store := mockStoreWithStuff() - pstore := store.Prefix(bz("key")) + pstore := NewStore(store, bz("key")) itr := pstore.ReverseIterator(nil, bz("")) checkDomain(t, itr, nil, bz("")) @@ -382,7 +382,7 @@ func TestPrefixDBReverseIterator2(t *testing.T) { func TestPrefixDBReverseIterator3(t *testing.T) { store := mockStoreWithStuff() - pstore := store.Prefix(bz("key")) + pstore := NewStore(store, bz("key")) itr := pstore.ReverseIterator(bz(""), nil) checkDomain(t, itr, bz(""), nil) @@ -394,9 +394,11 @@ func TestPrefixDBReverseIterator3(t *testing.T) { func TestPrefixDBReverseIterator4(t *testing.T) { store := mockStoreWithStuff() - pstore := store.Prefix(bz("key")) + pstore := NewStore(store, bz("key")) itr := pstore.ReverseIterator(bz(""), bz("")) checkInvalid(t, itr) itr.Close() } + +func bz(s string) []byte { return []byte(s) } diff --git a/store/queue.go b/store/queue/queue.go similarity index 74% rename from store/queue.go rename to store/queue/queue.go index 4081f63b3c6b..35617f19c3ae 100644 --- a/store/queue.go +++ b/store/queue/queue.go @@ -1,39 +1,49 @@ -package store +package queue import ( "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/cosmos-sdk/store/list" + "github.com/cosmos/cosmos-sdk/store/prefix" + "github.com/cosmos/cosmos-sdk/store/types" ) +func ListKey() []byte { + return []byte{0x00} +} + // Key for the top element position in the queue func TopKey() []byte { - return []byte{0x02} + return []byte{0x01} } // 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 + cdc *codec.Codec + store types.KVStore + + List list.List } -// NewQueue constructs new Queue -func NewQueue(cdc *codec.Codec, store sdk.KVStore) Queue { - return Queue{NewList(cdc, store)} +// New constructs new Queue +func New(cdc *codec.Codec, store types.KVStore) Queue { + return Queue{cdc, store, list.New(cdc, prefix.NewStore(store, ListKey()))} } func (m Queue) getTop() (res uint64) { - bz := m.List.store.Get(TopKey()) + bz := m.store.Get(TopKey()) if bz == nil { return 0 } - m.List.cdc.MustUnmarshalBinary(bz, &res) + m.cdc.MustUnmarshalBinary(bz, &res) return } func (m Queue) setTop(top uint64) { - bz := m.List.cdc.MustMarshalBinary(top) - m.List.store.Set(TopKey(), bz) + bz := m.cdc.MustMarshalBinary(top) + m.store.Set(TopKey(), bz) } // Push() inserts the elements to the rear of the queue diff --git a/store/queue_test.go b/store/queue/queue_test.go similarity index 80% rename from store/queue_test.go rename to store/queue/queue_test.go index 5ea6d906a4e1..7f8804a87a07 100644 --- a/store/queue_test.go +++ b/store/queue/queue_test.go @@ -1,4 +1,4 @@ -package store +package queue import ( "testing" @@ -11,6 +11,9 @@ import ( abci "github.com/tendermint/tendermint/abci/types" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/store/list" + "github.com/cosmos/cosmos-sdk/store/prefix" + "github.com/cosmos/cosmos-sdk/store/rootmulti" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -21,8 +24,8 @@ type S struct { func defaultComponents(key sdk.StoreKey) (sdk.Context, *codec.Codec) { db := dbm.NewMemDB() - cms := NewCommitMultiStore(db) - cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db) + cms := rootmulti.NewStore(db) + cms.MountStoreWithDB(key, db) cms.LoadLatestVersion() ctx := sdk.NewContext(cms, abci.Header{}, false, log.NewNopLogger()) cdc := codec.New() @@ -34,7 +37,7 @@ func TestQueue(t *testing.T) { ctx, cdc := defaultComponents(key) store := ctx.KVStore(key) - qm := NewQueue(cdc, store) + qm := New(cdc, store) val := S{1, true} var res S @@ -69,7 +72,8 @@ func TestKeys(t *testing.T) { key := sdk.NewKVStoreKey("test") ctx, cdc := defaultComponents(key) store := ctx.KVStore(key) - queue := NewQueue(cdc, store) + listStore := prefix.NewStore(store, ListKey()) + queue := New(cdc, store) for i := 0; i < 10; i++ { queue.Push(i) @@ -81,14 +85,14 @@ func TestKeys(t *testing.T) { var actual int // Checking keys.LengthKey - err := cdc.UnmarshalBinary(store.Get(LengthKey()), &len) + err := cdc.UnmarshalBinary(listStore.Get(list.LengthKey()), &len) require.Nil(t, err) require.Equal(t, len, queue.List.Len()) // Checking keys.ElemKey for i := 0; i < 10; i++ { queue.List.Get(uint64(i), &expected) - bz := store.Get(ElemKey(uint64(i))) + bz := listStore.Get(list.ElemKey(uint64(i))) err = cdc.UnmarshalBinary(bz, &actual) require.Nil(t, err) require.Equal(t, expected, actual) diff --git a/store/firstlast.go b/store/types/firstlast.go similarity index 80% rename from store/firstlast.go rename to store/types/firstlast.go index 353f44aa2e22..70be8057e4ff 100644 --- a/store/firstlast.go +++ b/store/types/firstlast.go @@ -1,15 +1,13 @@ -package store +package types import ( "bytes" cmn "github.com/tendermint/tendermint/libs/common" - - "github.com/cosmos/cosmos-sdk/store/types" ) // Gets the first item. -func First(st types.KVStore, start, end []byte) (kv cmn.KVPair, ok bool) { +func First(st KVStore, start, end []byte) (kv cmn.KVPair, ok bool) { iter := st.Iterator(start, end) if !iter.Valid() { return kv, false @@ -20,7 +18,7 @@ func First(st types.KVStore, start, end []byte) (kv cmn.KVPair, ok bool) { } // Gets the last item. `end` is exclusive. -func Last(st types.KVStore, start, end []byte) (kv cmn.KVPair, ok bool) { +func Last(st KVStore, start, end []byte) (kv cmn.KVPair, ok bool) { iter := st.ReverseIterator(end, start) if !iter.Valid() { if v := st.Get(start); v != nil { diff --git a/x/bank/keeper_test.go b/x/bank/keeper_test.go index 26c1446d27c7..d7bde704f627 100644 --- a/x/bank/keeper_test.go +++ b/x/bank/keeper_test.go @@ -21,7 +21,7 @@ func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey) { db := dbm.NewMemDB() authKey := sdk.NewKVStoreKey("authkey") ms := store.NewCommitMultiStore(db) - ms.MountStoreWithDB(authKey, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(authKey, db) ms.LoadLatestVersion() return ms, authKey } diff --git a/x/distribution/keeper/test_common.go b/x/distribution/keeper/test_common.go index b1bd4ae4e05c..303359cc99cd 100644 --- a/x/distribution/keeper/test_common.go +++ b/x/distribution/keeper/test_common.go @@ -94,13 +94,13 @@ func CreateTestInputAdvanced(t *testing.T, isCheckTx bool, initCoins int64, db := dbm.NewMemDB() ms := store.NewCommitMultiStore(db) - ms.MountStoreWithDB(keyDistr, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(tkeyStake, sdk.StoreTypeTransient, nil) - ms.MountStoreWithDB(keyStake, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(keyFeeCollection, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) + ms.MountStoreWithDB(keyDistr, db) + ms.MountStoreWithDB(tkeyStake, nil) + ms.MountStoreWithDB(keyStake, db) + ms.MountStoreWithDB(keyAcc, db) + ms.MountStoreWithDB(keyFeeCollection, db) + ms.MountStoreWithDB(keyParams, db) + ms.MountStoreWithDB(tkeyParams, db) err := ms.LoadLatestVersion() require.Nil(t, err) diff --git a/x/ibc/ibc_test.go b/x/ibc/ibc_test.go index 6cd89fded4ea..12f01e252954 100644 --- a/x/ibc/ibc_test.go +++ b/x/ibc/ibc_test.go @@ -22,7 +22,7 @@ import ( func defaultContext(key sdk.StoreKey) sdk.Context { db := dbm.NewMemDB() cms := store.NewCommitMultiStore(db) - cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db) + cms.MountStoreWithDB(key, db) cms.LoadLatestVersion() ctx := sdk.NewContext(cms, abci.Header{}, false, log.NewNopLogger()) return ctx diff --git a/x/slashing/test_common.go b/x/slashing/test_common.go index c5e9afa64b3b..edc8cd6dc1e6 100644 --- a/x/slashing/test_common.go +++ b/x/slashing/test_common.go @@ -58,20 +58,12 @@ func createTestInput(t *testing.T, defaults Params) (sdk.Context, bank.Keeper, s tkeyParams := sdk.NewTransientStoreKey("transient_params") db := dbm.NewMemDB() ms := store.NewCommitMultiStore(db) -<<<<<<< HEAD - ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(tkeyStake, sdk.StoreTypeTransient, nil) - ms.MountStoreWithDB(keyStake, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(keySlashing, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) - ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) -======= ms.MountStoreWithDB(keyAcc, db) ms.MountStoreWithDB(tkeyStake, nil) ms.MountStoreWithDB(keyStake, db) ms.MountStoreWithDB(keySlashing, db) ms.MountStoreWithDB(keyParams, db) ->>>>>>> eb1d017... now compiles + ms.MountStoreWithDB(tkeyParams, db) err := ms.LoadLatestVersion() require.Nil(t, err) ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewTMLogger(os.Stdout)) From 730f7d7623885fd45295324773502ea5bc99245c Mon Sep 17 00:00:00 2001 From: mossid Date: Mon, 17 Sep 2018 02:15:46 +0900 Subject: [PATCH 09/22] fix lint in progress --- store/cache/store.go | 1 + store/cachemulti/store.go | 1 + store/iavl/key.go | 3 +++ store/iavl/store.go | 4 ++-- store/store.go | 4 ++-- store/types/errors.go | 15 ++++++++++++++- 6 files changed, 23 insertions(+), 5 deletions(-) diff --git a/store/cache/store.go b/store/cache/store.go index 50dbcea681ee..c9997aa32b7f 100644 --- a/store/cache/store.go +++ b/store/cache/store.go @@ -109,6 +109,7 @@ func (ci *Store) Write() { ci.cache = make(map[string]cValue) } +// Implements types.KVStore func (ci *Store) CacheWrap() types.CacheKVStore { return NewStore(ci) } diff --git a/store/cachemulti/store.go b/store/cachemulti/store.go index a3c40f8cdb5b..4fd3e4e7782c 100644 --- a/store/cachemulti/store.go +++ b/store/cachemulti/store.go @@ -68,6 +68,7 @@ func (cms Store) GetTracer() *types.Tracer { return cms.tracer } +// Implements MultiStore func (cms Store) GetGasTank() *types.GasTank { return cms.tank } diff --git a/store/iavl/key.go b/store/iavl/key.go index dbe30c2b9142..0a09b043d12d 100644 --- a/store/iavl/key.go +++ b/store/iavl/key.go @@ -22,14 +22,17 @@ func NewKey(name string) *KVStoreKey { } } +// Implements StoreKey func (key *KVStoreKey) Name() string { return key.name } +// Implements StoreKey func (key *KVStoreKey) String() string { return fmt.Sprintf("KVStoreKey{%p, %s}", key, key.name) } +// Implements StoreKey func (key *KVStoreKey) NewStore() types.CommitStore { return &Store{} } diff --git a/store/iavl/store.go b/store/iavl/store.go index de4e81ba4068..159c846226e0 100644 --- a/store/iavl/store.go +++ b/store/iavl/store.go @@ -18,14 +18,14 @@ const ( ) // load the iavl store -func (store *Store) LoadKVStoreVersion(db dbm.DB, id types.CommitID) error { +func (st *Store) LoadKVStoreVersion(db dbm.DB, id types.CommitID) error { tree := iavl.NewMutableTree(db, defaultIAVLCacheSize) _, err := tree.LoadVersion(id.Version) if err != nil { return err } iavl := newIAVLStore(tree, int64(0), int64(0)) - *store = *iavl + *st = *iavl return nil } diff --git a/store/store.go b/store/store.go index 7daf39a91067..21e9dc249961 100644 --- a/store/store.go +++ b/store/store.go @@ -7,12 +7,12 @@ import ( "github.com/cosmos/cosmos-sdk/store/types" ) -// nolint: reexport +// nolint - reexport type ( CommitMultiStore = types.CommitMultiStore ) -// nolint: reexport +// nolint - reexport func NewCommitMultiStore(db dbm.DB) *rootmulti.Store { return rootmulti.NewStore(db) } diff --git a/store/types/errors.go b/store/types/errors.go index 045483585abc..0fda347a1705 100644 --- a/store/types/errors.go +++ b/store/types/errors.go @@ -10,12 +10,16 @@ import ( "github.com/cosmos/cosmos-sdk/codec" ) +// ABCICodeType - combined codetype / codespace type ABCICodeType uint32 +// CodeType - code identifier within codespace type CodeType uint16 +// CodespaceType - codespace identifier type CodespaceType uint16 +// get the abci code from the local code and codespace func ToABCICode(code CodeType) ABCICodeType { if code == CodeOK { return ABCICodeOK @@ -23,10 +27,14 @@ func ToABCICode(code CodeType) ABCICodeType { return ABCICodeType((uint32(CodespaceRoot) << 16) | uint32(code)) } +// SDK error codes const ( // Using same code with sdk/errors.go to reduce confusion + + // ABCI error codes ABCICodeOK ABCICodeType = 0 + // Base error codes CodeOK CodeType = 0 CodeInternal CodeType = 1 CodeTxDecode CodeType = 2 @@ -39,6 +47,7 @@ func unknownCodeMsg(code CodeType) string { return fmt.Sprintf("unknown code %d", code) } +// nolint: gocyclo func CodeToDefaultMsg(code CodeType) string { switch code { case CodeInternal: @@ -69,6 +78,7 @@ func ErrUnknownRequest(msg string) Error { type cmnError = cmn.Error +// Query error type type Error interface { cmnError @@ -99,6 +109,7 @@ func parseCmnError(err string) string { } // Copied from types/errors.go +// Implements Error func (err *queryError) ABCILog() string { cdc := codec.New() parsedErrMsg := parseCmnError(err.cmnError.Error()) @@ -114,6 +125,7 @@ func (err *queryError) ABCILog() string { return stringifiedJSON } +// Implements Error func (err *queryError) QueryResult() abci.ResponseQuery { return abci.ResponseQuery{ Code: uint32(ToABCICode(err.code)), @@ -121,9 +133,10 @@ func (err *queryError) QueryResult() abci.ResponseQuery { } } +// TODO: Do we need Codespace/ABCICode here? type humanReadableError struct { Codespace CodespaceType `json:"codespace"` Code CodeType `json:"code"` ABCICode ABCICodeType `json:"abci_code"` - Message string `json:message` + Message string `json:"message"` } From 91656b2af241ac21c7041c21f64cf7161859127e Mon Sep 17 00:00:00 2001 From: mossid Date: Sun, 7 Oct 2018 01:55:25 +0900 Subject: [PATCH 10/22] finalize rebase --- client/context/query.go | 2 +- store/rootmulti/proof.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/client/context/query.go b/client/context/query.go index fd837d1a1975..a565b1c95d0b 100644 --- a/client/context/query.go +++ b/client/context/query.go @@ -218,7 +218,7 @@ func (ctx CLIContext) verifyProof(_ string, resp abci.ResponseQuery) error { } // Verify the substore commit hash against trusted appHash - substoreCommitHash, err := rootmulti.VerifyMultiStoreCommitInfo( + substoreCommitHash, err := rootmulti.VerifyCommitInfo( multiStoreProof.StoreName, multiStoreProof.StoreInfos, commit.Header.AppHash, ) if err != nil { diff --git a/store/rootmulti/proof.go b/store/rootmulti/proof.go index 760001297fbc..50b85d8007f8 100644 --- a/store/rootmulti/proof.go +++ b/store/rootmulti/proof.go @@ -30,8 +30,8 @@ func buildProof(iavlProof []byte, storeName string, storeInfos []storeInfo) []by return proof } -// VerifyMultiStoreCommitInfo verify multiStoreCommitInfo against appHash -func VerifyMultiStoreCommitInfo(storeName string, storeInfos []storeInfo, appHash []byte) ([]byte, error) { +// VerifyCommitInfo verify multiStoreCommitInfo against appHash +func VerifyCommitInfo(storeName string, storeInfos []storeInfo, appHash []byte) ([]byte, error) { var substoreCommitHash []byte var height int64 for _, storeInfo := range storeInfos { From 05a2b46e3918d2d350a82d547ee858a447910f5c Mon Sep 17 00:00:00 2001 From: mossid Date: Sun, 7 Oct 2018 02:06:59 +0900 Subject: [PATCH 11/22] fix test --- store/gas/store.go | 2 +- store/rootmulti/proof_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/store/gas/store.go b/store/gas/store.go index bb81c9af8542..ffe8befee96e 100644 --- a/store/gas/store.go +++ b/store/gas/store.go @@ -49,7 +49,7 @@ func (gs *gasKVStore) Has(key []byte) bool { // Implements types.KVStore. func (gs *gasKVStore) Delete(key []byte) { - // No gas costs for deletion + gs.tank.DeleteFlat() gs.parent.Delete(key) } diff --git a/store/rootmulti/proof_test.go b/store/rootmulti/proof_test.go index 318cd653884f..70375eb38020 100644 --- a/store/rootmulti/proof_test.go +++ b/store/rootmulti/proof_test.go @@ -85,13 +85,13 @@ func TestVerifyMultiStoreCommitInfo(t *testing.T) { }, }) - commitHash, err := VerifyMultiStoreCommitInfo(storeName, storeInfos, appHash) + commitHash, err := VerifyCommitInfo(storeName, storeInfos, appHash) require.Nil(t, err) require.Equal(t, commitHash, substoreRootHash) appHash, _ = hex.DecodeString("29de216bf5e2531c688de36caaf024cd3bb09ee3") - _, err = VerifyMultiStoreCommitInfo(storeName, storeInfos, appHash) + _, err = VerifyCommitInfo(storeName, storeInfos, appHash) require.Error(t, err, "appHash doesn't match to the merkle root of multiStoreCommitInfo") } From 4677207956663002ccec6c4e4b2447771601a0c6 Mon Sep 17 00:00:00 2001 From: mossid Date: Sun, 7 Oct 2018 21:17:19 +0900 Subject: [PATCH 12/22] fix lint --- store/cachemulti/store.go | 2 ++ store/gas/store.go | 24 ++++++++++++------------ store/iavl/key.go | 20 ++++++++++---------- store/iavl/store.go | 6 ++++++ store/transient/key.go | 20 ++++++++++---------- store/types/store.go | 20 ++++++++------------ store/types/trace.go | 1 + store/utils/utils.go | 1 + types/store.go | 10 +++++----- x/slashing/test_common.go | 4 +++- x/stake/keeper/test_common.go | 3 ++- 11 files changed, 60 insertions(+), 51 deletions(-) diff --git a/store/cachemulti/store.go b/store/cachemulti/store.go index 4fd3e4e7782c..c43330167f14 100644 --- a/store/cachemulti/store.go +++ b/store/cachemulti/store.go @@ -14,6 +14,8 @@ import ( // Store holds many cache-wrapped stores. // Implements MultiStore. +// TODO: support recursive multistores, +// currently only using CacheKVStores type Store struct { db types.CacheKVStore stores map[types.StoreKey]types.CacheKVStore diff --git a/store/gas/store.go b/store/gas/store.go index ffe8befee96e..215f110f66fd 100644 --- a/store/gas/store.go +++ b/store/gas/store.go @@ -4,19 +4,19 @@ import ( "github.com/cosmos/cosmos-sdk/store/types" ) -var _ types.KVStore = &gasKVStore{} +var _ types.KVStore = &Store{} -// gasKVStore applies gas tracking to an underlying KVStore. It implements the +// Store applies gas tracking to an underlying KVStore. It implements the // KVStore interface. -type gasKVStore struct { +type Store struct { tank *types.GasTank parent types.KVStore } // NewGasKVStore returns a reference to a new GasKVStore. // nolint -func NewStore(tank *types.GasTank, parent types.KVStore) *gasKVStore { - kvs := &gasKVStore{ +func NewStore(tank *types.GasTank, parent types.KVStore) *Store { + kvs := &Store{ tank: tank, parent: parent, } @@ -24,7 +24,7 @@ func NewStore(tank *types.GasTank, parent types.KVStore) *gasKVStore { } // Implements types.KVStore. -func (gs *gasKVStore) Get(key []byte) (value []byte) { +func (gs *Store) Get(key []byte) (value []byte) { gs.tank.ReadFlat() value = gs.parent.Get(key) // TODO overflow-safe math? @@ -34,7 +34,7 @@ func (gs *gasKVStore) Get(key []byte) (value []byte) { } // Implements types.KVStore. -func (gs *gasKVStore) Set(key []byte, value []byte) { +func (gs *Store) Set(key []byte, value []byte) { gs.tank.WriteFlat() // TODO overflow-safe math? gs.tank.WriteBytes(len(value)) @@ -42,28 +42,28 @@ func (gs *gasKVStore) Set(key []byte, value []byte) { } // Implements types.KVStore. -func (gs *gasKVStore) Has(key []byte) bool { +func (gs *Store) Has(key []byte) bool { gs.tank.HasFlat() return gs.parent.Has(key) } // Implements types.KVStore. -func (gs *gasKVStore) Delete(key []byte) { +func (gs *Store) Delete(key []byte) { gs.tank.DeleteFlat() gs.parent.Delete(key) } // Implements types.KVStore. -func (gs *gasKVStore) Iterator(start, end []byte) types.Iterator { +func (gs *Store) Iterator(start, end []byte) types.Iterator { return gs.iterator(start, end, true) } // Implements types.KVStore. -func (gs *gasKVStore) ReverseIterator(start, end []byte) types.Iterator { +func (gs *Store) ReverseIterator(start, end []byte) types.Iterator { return gs.iterator(start, end, false) } -func (gs *gasKVStore) iterator(start, end []byte, ascending bool) types.Iterator { +func (gs *Store) iterator(start, end []byte, ascending bool) types.Iterator { var parent types.Iterator if ascending { parent = gs.parent.Iterator(start, end) diff --git a/store/iavl/key.go b/store/iavl/key.go index 0a09b043d12d..e70efef409c9 100644 --- a/store/iavl/key.go +++ b/store/iavl/key.go @@ -6,33 +6,33 @@ import ( "github.com/cosmos/cosmos-sdk/store/types" ) -var _ types.StoreKey = (*KVStoreKey)(nil) +var _ types.StoreKey = (*StoreKey)(nil) -// KVStoreKey is used for accessing substores. +// StoreKey is used for accessing substores. // Only the pointer value should ever be used - it functions as a capabilities key. -type KVStoreKey struct { +type StoreKey struct { name string } -// NewKVStoreKey returns a new pointer to a KVStoreKey. +// NewStoreKey returns a new pointer to a StoreKey. // Use a pointer so keys don't collide. -func NewKey(name string) *KVStoreKey { - return &KVStoreKey{ +func NewKey(name string) *StoreKey { + return &StoreKey{ name: name, } } // Implements StoreKey -func (key *KVStoreKey) Name() string { +func (key *StoreKey) Name() string { return key.name } // Implements StoreKey -func (key *KVStoreKey) String() string { - return fmt.Sprintf("KVStoreKey{%p, %s}", key, key.name) +func (key *StoreKey) String() string { + return fmt.Sprintf("StoreKey{%p, %s}", key, key.name) } // Implements StoreKey -func (key *KVStoreKey) NewStore() types.CommitStore { +func (key *StoreKey) NewStore() types.CommitStore { return &Store{} } diff --git a/store/iavl/store.go b/store/iavl/store.go index 159c846226e0..e237addc9001 100644 --- a/store/iavl/store.go +++ b/store/iavl/store.go @@ -10,6 +10,7 @@ import ( cmn "github.com/tendermint/tendermint/libs/common" dbm "github.com/tendermint/tendermint/libs/db" + "github.com/cosmos/cosmos-sdk/store/cache" "github.com/cosmos/cosmos-sdk/store/types" ) @@ -120,6 +121,11 @@ func (st *Store) VersionExists(version int64) bool { return st.tree.VersionExists(version) } +// Implements types.KVStore +func (st *Store) CacheWrap() types.CacheKVStore { + return cache.NewStore(st) +} + // Implements types.KVStore. func (st *Store) Set(key, value []byte) { st.tree.Set(key, value) diff --git a/store/transient/key.go b/store/transient/key.go index edba3fed4109..5be0549b223f 100644 --- a/store/transient/key.go +++ b/store/transient/key.go @@ -6,32 +6,32 @@ import ( "github.com/cosmos/cosmos-sdk/store/types" ) -var _ types.StoreKey = (*TransientStoreKey)(nil) +var _ types.StoreKey = (*StoreKey)(nil) -// TransientStoreKey is used for indexing transient stores in a MultiStore -type TransientStoreKey struct { +// StoreKey is used for indexing transient stores in a MultiStore +type StoreKey struct { name string } -// Constructs new TransientStoreKey +// Constructs new StoreKey // Must return a pointer according to the ocap principle -func NewKey(name string) *TransientStoreKey { - return &TransientStoreKey{ +func NewKey(name string) *StoreKey { + return &StoreKey{ name: name, } } // Implements StoreKey -func (key *TransientStoreKey) Name() string { +func (key *StoreKey) Name() string { return key.name } // Implements StoreKey -func (key *TransientStoreKey) String() string { - return fmt.Sprintf("TransientStoreKey{%p, %s}", key, key.name) +func (key *StoreKey) String() string { + return fmt.Sprintf("StoreKey{%p, %s}", key, key.name) } // Implements StoreKey -func (key *TransientStoreKey) NewStore() types.CommitStore { +func (key *StoreKey) NewStore() types.CommitStore { return &Store{} } diff --git a/store/types/store.go b/store/types/store.go index 1f6837c31bdf..e0a10698cd51 100644 --- a/store/types/store.go +++ b/store/types/store.go @@ -24,7 +24,7 @@ const ( PruneNothing PruningStrategy = iota ) -// Stores of MultiStore must implement CommitStore. +// Stores of MultiStore must implement commitStore. type CommitStore interface { // CONTRACT: return zero CommitID to skip writing Commit() CommitID @@ -50,13 +50,9 @@ type MultiStore interface { //nolint // TODO: recursive multistore not yet supported // GetMultiStore(StoreKey) MultiStore - // CacheWrap cache wraps - // Having this method here because there is currently no - // implementation of MultiStore that panics on CacheWrap(). - // Move this method to CacheWrapperMultiStore when needed - CacheWrap() CacheMultiStore - GetTracer() *Tracer + + CacheWrap() CacheMultiStore } // From MultiStore.CacheMultiStore().... @@ -147,9 +143,7 @@ func KVStoreReversePrefixIterator(kvs KVStore, prefix []byte) Iterator { return kvs.ReverseIterator(prefix, PrefixEndBytes(prefix)) } -type CacheWrapperKVStore interface { - KVStore - +type cacheWrappableKVStore interface { // CacheWrap cache wraps CacheWrap() CacheKVStore } @@ -158,7 +152,8 @@ type CacheWrapperKVStore interface { // the CacheKVStore, all previously created CacheKVStores on the // object expire. type CacheKVStore interface { - CacheWrapperKVStore + KVStore + cacheWrappableKVStore // Writes operations to underlying KVStore Write() @@ -166,8 +161,9 @@ type CacheKVStore interface { // Stores of MultiStore must implement CommitStore. type CommitKVStore interface { - CommitStore KVStore + CommitStore + cacheWrappableKVStore // Load a specific persisted version. When you load an old // version, or when the last commit attempt didn't complete, diff --git a/store/types/trace.go b/store/types/trace.go index e0f9e31e98dc..735ff1d55a25 100644 --- a/store/types/trace.go +++ b/store/types/trace.go @@ -10,6 +10,7 @@ import ( // every trace operation. type TraceContext map[string]interface{} +// Tracer is pair of io.Writer and TraceContext type Tracer struct { Writer io.Writer Context TraceContext diff --git a/store/utils/utils.go b/store/utils/utils.go index 087c160fba62..8d869460777a 100644 --- a/store/utils/utils.go +++ b/store/utils/utils.go @@ -6,5 +6,6 @@ import ( func bz(s string) []byte { return []byte(s) } +// Used for tests - formats integer to bytes func KeyFmt(i int) []byte { return bz(fmt.Sprintf("key%0.8d", i)) } func ValFmt(i int) []byte { return bz(fmt.Sprintf("value%0.8d", i)) } diff --git a/types/store.go b/types/store.go index 29c17f83b987..d9d40a70540e 100644 --- a/types/store.go +++ b/types/store.go @@ -6,7 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/store/types" ) -// nolint: reexport +// nolint - reexport type ( PruningStrategy = types.PruningStrategy CommitStore = types.CommitStore @@ -23,8 +23,8 @@ type ( KVPair = types.KVPair StoreKey = types.StoreKey - KVStoreKey = iavl.KVStoreKey - TransientStoreKey = transient.TransientStoreKey + KVStoreKey = iavl.StoreKey + TransientStoreKey = transient.StoreKey Gas = types.Gas GasTank = types.GasTank @@ -37,14 +37,14 @@ type ( ErrorOutOfGas = types.ErrorOutOfGas ) -// nolint: reexport +// nolint - reexport const ( PruneNothing = types.PruneNothing PruneEverything = types.PruneEverything PruneSyncable = types.PruneSyncable ) -// nolint: reexport +// nolint - reexport func KVStorePrefixIterator(store KVStore, prefix []byte) Iterator { return types.KVStorePrefixIterator(store, prefix) } diff --git a/x/slashing/test_common.go b/x/slashing/test_common.go index edc8cd6dc1e6..59e2ec7bf472 100644 --- a/x/slashing/test_common.go +++ b/x/slashing/test_common.go @@ -66,7 +66,9 @@ func createTestInput(t *testing.T, defaults Params) (sdk.Context, bank.Keeper, s ms.MountStoreWithDB(tkeyParams, db) err := ms.LoadLatestVersion() require.Nil(t, err) - ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewTMLogger(os.Stdout)) + + cms := ms.CacheWrap() + ctx := sdk.NewContext(cms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewTMLogger(os.Stdout)) cdc := createTestCodec() accountKeeper := auth.NewAccountKeeper(cdc, keyAcc, auth.ProtoBaseAccount) diff --git a/x/stake/keeper/test_common.go b/x/stake/keeper/test_common.go index e83f7925c377..7d404d70fc63 100644 --- a/x/stake/keeper/test_common.go +++ b/x/stake/keeper/test_common.go @@ -92,7 +92,8 @@ func CreateTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context err := ms.LoadLatestVersion() require.Nil(t, err) - ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, log.NewNopLogger()) + cms := ms.CacheWrap() + ctx := sdk.NewContext(cms, abci.Header{ChainID: "foochainid"}, isCheckTx, log.NewNopLogger()) cdc := MakeTestCodec() accountKeeper := auth.NewAccountKeeper( cdc, // amino codec From 698754f6844c99731698e3296b89185d6c2cb5e0 Mon Sep 17 00:00:00 2001 From: mossid Date: Tue, 16 Oct 2018 03:15:10 +0900 Subject: [PATCH 13/22] loadCommitStoreFromParams -> loadCommitKVStoreFromParams --- store/rootmulti/store.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 15b9c59db1e1..51f75016255d 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -28,7 +28,7 @@ type Store struct { lastCommitID types.CommitID pruning types.PruningStrategy storesParams map[types.StoreKey]storeParams - stores map[types.StoreKey]types.CommitKVStore + kvstores map[types.StoreKey]types.CommitKVStore keysByName map[string]types.StoreKey tracer *types.Tracer @@ -43,7 +43,7 @@ func NewStore(db dbm.DB) *Store { return &Store{ db: db, storesParams: make(map[types.StoreKey]storeParams), - stores: make(map[types.StoreKey]types.CommitKVStore), + kvstores: make(map[types.StoreKey]types.CommitKVStore), keysByName: make(map[string]types.StoreKey), tracer: new(types.Tracer), @@ -64,7 +64,7 @@ func (rs *Store) GetGasTank() *types.GasTank { // Implements CommitMultiStore func (rs *Store) SetPruning(pruning types.PruningStrategy) { rs.pruning = pruning - for _, substore := range rs.stores { + for _, substore := range rs.kvstores { substore.SetPruning(pruning) } } @@ -89,13 +89,21 @@ func (rs *Store) MountStoreWithDB(key types.StoreKey, db dbm.DB) { // Implements CommitMultiStore. func (rs *Store) GetCommitStore(key types.StoreKey) types.CommitStore { - return rs.stores[key] + return rs.kvstores[key] } // Implements CommitMultiStore. func (rs *Store) GetCommitKVStore(key types.StoreKey) types.CommitKVStore { - return rs.stores[key].(types.CommitKVStore) + return rs.kvstores[key] +} + +/* +// Recursive MultiStore not yet implemented +// Implements CommitMultiStore +func (rs *Store) GetCommitMultiStore(key types.StoreKey) types.CommitMultiStore { + return rs.multistores[key] } +*/ // Implements CommitMultiStore. func (rs *Store) LoadLatestVersion() error { @@ -129,7 +137,7 @@ func (rs *Store) LoadMultiStoreVersion(ver int64) error { if info, ok := infos[key]; ok { id = info.Core.CommitID } - store, err := rs.loadCommitStoreFromParams(key, id, storeParams) + store, err := rs.loadCommitKVStoreFromParams(key, id, storeParams) if err != nil { return fmt.Errorf("failed to load Store: %v", err) } @@ -269,7 +277,7 @@ func parsePath(path string) (storeName string, subpath string, err types.Error) //---------------------------------------- -func (rs *Store) loadCommitStoreFromParams(key types.StoreKey, id types.CommitID, params storeParams) (store types.CommitKVStore, err error) { +func (rs *Store) loadCommitKVStoreFromParams(key types.StoreKey, id types.CommitID, params storeParams) (store types.CommitKVStore, err error) { var db dbm.DB if params.db != nil { db = dbm.NewPrefixDB(params.db, []byte("s/_/")) From 070ac7b830919bc676aa2fc3fd693e068369fcdf Mon Sep 17 00:00:00 2001 From: mossid Date: Wed, 17 Oct 2018 17:37:35 +0200 Subject: [PATCH 14/22] distinguish KVStoreKey and MultiStoreKey, basemulti in progress --- store/basemulti/store.go | 147 ++++++++++++++++++++++++++++++++++++++ store/basemulti/wire.go | 7 ++ store/cachemulti/store.go | 36 +++++----- store/iavl/key.go | 4 +- store/iavlmulti/store.go | 25 +++++++ store/rootmulti/store.go | 84 +++++++--------------- store/transient/key.go | 4 +- store/types/store.go | 32 ++++++--- types/context.go | 4 +- types/store.go | 6 +- 10 files changed, 255 insertions(+), 94 deletions(-) create mode 100644 store/basemulti/store.go create mode 100644 store/basemulti/wire.go create mode 100644 store/iavlmulti/store.go diff --git a/store/basemulti/store.go b/store/basemulti/store.go new file mode 100644 index 000000000000..9ca0d59b2e32 --- /dev/null +++ b/store/basemulti/store.go @@ -0,0 +1,147 @@ +package basemulti + +import ( + "fmt" + + "github.com/tendermint/tendermint/crypto/merkle" + dbm "github.com/tendermint/tendermint/libs/db" + + "github.com/cosmos/cosmos-sdk/store/types" +) + +type Store struct { + db dbm.DB + + kvstores map[types.KVStoreKey]types.CommitKVStore + kvkeysByName map[string]types.KVStoreKey + + pruning types.PruningStrategy +} + +func (store *Store) MountKVStoreWithDB(key types.KVStoreKey, db dbm.DB) { + if key == nil { + panic("MountStoreWithDB() key cannot be nil") + } + if _, ok := store.kvkeysByName[key.Name()]; ok { + panic(fmt.Sprintf("Store duplicate store key %v", key)) + } + + store.kvkeysByName[key.Name()] = key +} + +func (store *Store) GetCommitKVStore(key types.KVStoreKey) types.CommitKVStore { + return store.kvstores[key] +} + +func (store *Store) LoadMultiStoreVersion(ver int64) (err error) { + // Convert StoreInfos slice to map + var lastCommitID types.CommitID + infos := make(map[types.KVStoreKey]storeInfo) + if ver != 0 { + // Get commitInfo + cInfo, err := getCommitInfo(store.db, ver) + if err != nil { + return err + } + + for _, sInfo := range cInfo.storeInfos { + infos[store.nameToKVKey(sInfo.Name)] = sInfo + } + + lastCommitID = cInfo.CommitID() + } + + for _, key := range store.kvkeysByName { + var id types.CommitID + if info, ok := infos[key]; ok { + id = info.Core.CommitID + } + kvstore := key.NewStore() + db := dbm.NewPrefixDB(store.db, []byte("s/k:"+key.Name()+"/")) + err = kvstore.LoadKVStoreVersion(db, id) + if err != nil { + return + } + + kvstore.SetPruning(store.pruning) + } +} + +func (store *Store) nameToKVKey(name string) types.KVStoreKey { + for key := range kvstores { + if key.Name() == name { + return key + } + } +} + +// ------------------------------- +// storeInfo + +// storeInfo contains the name and core reference for an +// underlying store. It is the leaf of the Stores top +// level simple merkle tree + +type storeInfo struct { + Name string + Core storeCore +} + +type storeCore struct { + CommitID types.CommitID + // ... maybe add more state +} + +// ------------------------------ +// commitInfo + +// NOTE: keep commitInfo a simple immutable struct. +type commitInfo struct { + // Version + Version int64 + + // types.Store info for + storeInfos []storeInfo +} + +// Hash returns the simple merkle root hash of the stores sorted by name. +func (ci commitInfo) Hash() []byte { + // TODO cache to ci.hash []byte + m := make(map[string]merkle.Hasher, len(ci.StoreInfos)) + for _, storeInfo := range ci.StoreInfos { + m[storeInfo.Name] = storeInfo + } + return merkle.SimpleHashFromMap(m) +} + +func (ci commitInfo) CommitID() types.CommitID { + return types.CommitID{ + Version: ci.Version, + Hash: ci.Hash(), + } +} + +// ------------------------------- +// Misc. + +func getLatestVestoreion(db dbm.DB) (latest int64) { + latestBytes := db.Get([]byte("s/latest")) + if latestBytes == nil { + return 0 + } + cdc.MustUnmarshalBinary(latestBytes, &latest) + return +} + +func getCommitInfo(db dbm.DB, ver int64) (cInfo commitInfo, err error) { + cInfoBytes := db.Get([]byte(fmt.Sprintf("s/%d", ver))) + if cInfoBytes == nil { + err = fmt.Errorf("failed to get Store: no data") + } + + err = cdc.UnmarshalBinary(cInfoBytes, &cInfo) + if err != nil { + err = fmt.Errorf("failed to get Store: %v", err) + } + return +} diff --git a/store/basemulti/wire.go b/store/basemulti/wire.go new file mode 100644 index 000000000000..123ec0c86375 --- /dev/null +++ b/store/basemulti/wire.go @@ -0,0 +1,7 @@ +package basemulti + +import ( + "github.com/cosmos/cosmos-sdk/codec" +) + +var cdc = codec.New() diff --git a/store/cachemulti/store.go b/store/cachemulti/store.go index c43330167f14..41131c837aff 100644 --- a/store/cachemulti/store.go +++ b/store/cachemulti/store.go @@ -12,14 +12,14 @@ import ( //---------------------------------------- // Store -// Store holds many cache-wrapped stores. +// Store holds many cache-wrapped kvstores. // Implements MultiStore. -// TODO: support recursive multistores, +// TODO: support recursive multikvstores, // currently only using CacheKVStores type Store struct { db types.CacheKVStore - stores map[types.StoreKey]types.CacheKVStore - keysByName map[string]types.StoreKey + kvstores map[types.KVStoreKey]types.CacheKVStore + keysByName map[string]types.KVStoreKey tracer *types.Tracer tank *types.GasTank @@ -27,20 +27,20 @@ type Store struct { var _ types.CacheMultiStore = Store{} -func NewStore(db dbm.DB, keysByName map[string]types.StoreKey, stores map[types.StoreKey]types.CommitKVStore, tracer *types.Tracer, tank *types.GasTank) Store { +func NewStore(db dbm.DB, keysByName map[string]types.KVStoreKey, kvstores map[types.KVStoreKey]types.CommitKVStore, tracer *types.Tracer, tank *types.GasTank) Store { cms := Store{ db: cache.NewStore(dbadapter.NewStore(db)), - stores: make(map[types.StoreKey]types.CacheKVStore, len(stores)), + kvstores: make(map[types.KVStoreKey]types.CacheKVStore, len(kvstores)), keysByName: keysByName, tracer: tracer, tank: tank, } - for key, store := range stores { + for key, store := range kvstores { if tracer.Enabled() { - cms.stores[key] = cache.NewStore(trace.NewStore(store, tracer)) + cms.kvstores[key] = cache.NewStore(trace.NewStore(store, tracer)) } else { - cms.stores[key] = cache.NewStore(store) + cms.kvstores[key] = cache.NewStore(store) } } @@ -49,16 +49,16 @@ func NewStore(db dbm.DB, keysByName map[string]types.StoreKey, stores map[types. func newCacheMultiStoreFromCMS(cms Store) Store { cms2 := Store{ - db: cache.NewStore(cms.db), - stores: make(map[types.StoreKey]types.CacheKVStore, len(cms.stores)), - tracer: cms.tracer, + db: cache.NewStore(cms.db), + kvstores: make(map[types.KVStoreKey]types.CacheKVStore, len(cms.kvstores)), + tracer: cms.tracer, } - for key, store := range cms.stores { + for key, store := range cms.kvstores { if cms2.tracer.Enabled() { - cms2.stores[key] = cache.NewStore(trace.NewStore(store, cms.tracer)) + cms2.kvstores[key] = cache.NewStore(trace.NewStore(store, cms.tracer)) } else { - cms2.stores[key] = cache.NewStore(store) + cms2.kvstores[key] = cache.NewStore(store) } } @@ -78,7 +78,7 @@ func (cms Store) GetGasTank() *types.GasTank { // Implements CacheMultiStore. func (cms Store) Write() { cms.db.Write() - for _, store := range cms.stores { + for _, store := range cms.kvstores { store.Write() } } @@ -89,6 +89,6 @@ func (cms Store) CacheWrap() types.CacheMultiStore { } // Implements MultiStore. -func (cms Store) GetKVStore(key types.StoreKey) types.KVStore { - return cms.stores[key].(types.KVStore) +func (cms Store) GetKVStore(key types.KVStoreKey) types.KVStore { + return cms.kvstores[key].(types.KVStore) } diff --git a/store/iavl/key.go b/store/iavl/key.go index e70efef409c9..4a94f18f93e2 100644 --- a/store/iavl/key.go +++ b/store/iavl/key.go @@ -6,7 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/store/types" ) -var _ types.StoreKey = (*StoreKey)(nil) +var _ types.KVStoreKey = (*StoreKey)(nil) // StoreKey is used for accessing substores. // Only the pointer value should ever be used - it functions as a capabilities key. @@ -33,6 +33,6 @@ func (key *StoreKey) String() string { } // Implements StoreKey -func (key *StoreKey) NewStore() types.CommitStore { +func (key *StoreKey) NewStore() types.CommitKVStore { return &Store{} } diff --git a/store/iavlmulti/store.go b/store/iavlmulti/store.go new file mode 100644 index 000000000000..b1734350d3a1 --- /dev/null +++ b/store/iavlmulti/store.go @@ -0,0 +1,25 @@ +package iavlmulti + +import ( + dbm "github.com/tendermint/tendermint/libs/db" + + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/cosmos-sdk/store/cachemulti" + "github.com/cosmos/cosmos-sdk/store/iavl" + "github.com/cosmos/cosmos-sdk/store/types" +) + +// iavlmulti.Store works similar with rootmulti.Store +// but stores +type Store struct { + db dbm.DB + + kvstores map[types.KVStoreKey]iavl.KVStore +} + +var _ types.CommitMultiStore = (*Store)(nil) + +func (store *Store) CacheWrap() types.CacheMultiStore { + return cachemulti.NewStore(store.db, store.keysByName, store.iavlstores) +} diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 51f75016255d..9e23d628e283 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -27,9 +27,9 @@ type Store struct { db dbm.DB lastCommitID types.CommitID pruning types.PruningStrategy - storesParams map[types.StoreKey]storeParams - kvstores map[types.StoreKey]types.CommitKVStore - keysByName map[string]types.StoreKey + storeParams map[types.KVStoreKey]storeParams + kvstores map[types.KVStoreKey]types.CommitKVStore + keysByName map[string]types.KVStoreKey tracer *types.Tracer tank *types.GasTank @@ -41,10 +41,10 @@ var _ types.Queryable = (*Store)(nil) // nolint func NewStore(db dbm.DB) *Store { return &Store{ - db: db, - storesParams: make(map[types.StoreKey]storeParams), - kvstores: make(map[types.StoreKey]types.CommitKVStore), - keysByName: make(map[string]types.StoreKey), + db: db, + storeParams: make(map[types.KVStoreKey]storeParams), + kvstores: make(map[types.KVStoreKey]types.CommitKVStore), + keysByName: make(map[string]types.KVStoreKey), tracer: new(types.Tracer), tank: new(types.GasTank), @@ -70,17 +70,17 @@ func (rs *Store) SetPruning(pruning types.PruningStrategy) { } // Implements CommitMultiStore. -func (rs *Store) MountStoreWithDB(key types.StoreKey, db dbm.DB) { +func (rs *Store) MountKVStoreWithDB(key types.KVStoreKey, db dbm.DB) { if key == nil { panic("MountStoreWithDB() key cannot be nil") } - if _, ok := rs.storesParams[key]; ok { + if _, ok := rs.storeParams[key]; ok { 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)) } - rs.storesParams[key] = storeParams{ + rs.storeParams[key] = storeParams{ key: key, db: db, } @@ -88,19 +88,14 @@ func (rs *Store) MountStoreWithDB(key types.StoreKey, db dbm.DB) { } // Implements CommitMultiStore. -func (rs *Store) GetCommitStore(key types.StoreKey) types.CommitStore { - return rs.kvstores[key] -} - -// Implements CommitMultiStore. -func (rs *Store) GetCommitKVStore(key types.StoreKey) types.CommitKVStore { +func (rs *Store) GetCommitKVStore(key types.KVStoreKey) types.CommitKVStore { return rs.kvstores[key] } /* // Recursive MultiStore not yet implemented // Implements CommitMultiStore -func (rs *Store) GetCommitMultiStore(key types.StoreKey) types.CommitMultiStore { +func (rs *Store) GetCommitMultiStore(key types.KVStoreKey) types.CommitMultiStore { return rs.multistores[key] } */ @@ -115,7 +110,7 @@ func (rs *Store) LoadLatestVersion() error { func (rs *Store) LoadMultiStoreVersion(ver int64) error { // Convert StoreInfos slice to map var lastCommitID types.CommitID - infos := make(map[types.StoreKey]storeInfo) + infos := make(map[types.KVStoreKey]storeInfo) if ver != 0 { // Get commitInfo cInfo, err := getCommitInfo(rs.db, ver) @@ -131,8 +126,8 @@ func (rs *Store) LoadMultiStoreVersion(ver int64) error { } // Load each Store - var newStores = make(map[types.StoreKey]types.CommitKVStore) - for key, storeParams := range rs.storesParams { + var newStores = make(map[types.KVStoreKey]types.CommitKVStore) + for key, storeParams := range rs.storeParams { var id types.CommitID if info, ok := infos[key]; ok { id = info.Core.CommitID @@ -146,7 +141,7 @@ func (rs *Store) LoadMultiStoreVersion(ver int64) error { // Success. rs.lastCommitID = lastCommitID - rs.stores = newStores + rs.kvstores = newStores return nil } @@ -163,7 +158,7 @@ func (rs *Store) Commit() types.CommitID { // Commit stores. version := rs.lastCommitID.Version + 1 - commitInfo := commitStores(version, rs.stores) + commitInfo := commitStores(version, rs.kvstores) // Need to update atomically. batch := rs.db.NewBatch() @@ -185,14 +180,14 @@ func (rs *Store) Commit() types.CommitID { // Implements types.MultiStore. func (rs *Store) CacheWrap() types.CacheMultiStore { - return cachemulti.NewStore(rs.db, rs.keysByName, rs.stores, rs.tracer, rs.tank) + return cachemulti.NewStore(rs.db, rs.keysByName, rs.kvstores, rs.tracer, rs.tank) } // GetKVStore implements the types.MultiStore interface. If tracing is enabled on the // Store, a wrapped TraceKVStore will be returned with the given // tracer, otherwise, the original types.KVStore will be returned. -func (rs *Store) GetKVStore(key types.StoreKey) types.KVStore { - store := rs.stores[key].(types.KVStore) +func (rs *Store) GetKVStore(key types.KVStoreKey) types.KVStore { + store := rs.kvstores[key].(types.KVStore) if rs.tracer.Enabled() { store = trace.NewStore(store, rs.tracer) @@ -206,14 +201,14 @@ func (rs *Store) GetKVStore(key types.StoreKey) types.KVStore { // getStoreByName will first convert the original name to // a special key, before looking up the types.CommitStore. // This is not exposed to the extensions (which will need the -// types.StoreKey), but is useful in main, and particularly app.Query, +// types.KVStoreKey), but is useful in main, and particularly app.Query, // in order to convert human strings into types.CommitStores. func (rs *Store) getStoreByName(name string) types.KVStore { key := rs.keysByName[name] if key == nil { return nil } - return rs.stores[key] + return rs.kvstores[key] } //---------------------- Query ------------------ @@ -277,7 +272,7 @@ func parsePath(path string) (storeName string, subpath string, err types.Error) //---------------------------------------- -func (rs *Store) loadCommitKVStoreFromParams(key types.StoreKey, id types.CommitID, params storeParams) (store types.CommitKVStore, err error) { +func (rs *Store) loadCommitKVStoreFromParams(key types.KVStoreKey, id types.CommitID, params storeParams) (store types.CommitKVStore, err error) { var db dbm.DB if params.db != nil { db = dbm.NewPrefixDB(params.db, []byte("s/_/")) @@ -293,35 +288,10 @@ func (rs *Store) loadCommitKVStoreFromParams(key types.StoreKey, id types.Commit store.SetPruning(rs.pruning) return - - // XXX: move to store subdirectories LoadKVStoreVersion - /* - switch params.typ { - case types.StoreTypeMulti: - panic("recursive types.MultiStores not yet supported") - // TODO: id? - // return NewCommitMultiStore(db, id) - case types.StoreTypeIAVL: - store, err = LoadIAVLStore(db, id, rs.pruning) - return - case types.StoreTypeDB: - panic("dbm.DB is not a types.CommitStore") - case types.StoreTypeTransient: - _, ok := key.(*types.TransientStoreKey) - if !ok { - err = fmt.Errorf("invalid types.StoreKey for types.StoreTypeTransient: %s", key.String()) - return - } - store = transient.NewStore() - return - default: - panic(fmt.Sprintf("unrecognized store type %v", params.typ)) - } - */ } -func (rs *Store) nameToKey(name string) types.StoreKey { - for key := range rs.storesParams { +func (rs *Store) nameToKey(name string) types.KVStoreKey { + for key := range rs.storeParams { if key.Name() == name { return key } @@ -333,7 +303,7 @@ func (rs *Store) nameToKey(name string) types.StoreKey { // storeParams type storeParams struct { - key types.StoreKey + key types.KVStoreKey db dbm.DB typ reflect.Type } @@ -422,7 +392,7 @@ func setLatestVersion(batch dbm.Batch, version int64) { } // Commits each store and returns a new commitInfo. -func commitStores(version int64, storeMap map[types.StoreKey]types.CommitKVStore) commitInfo { +func commitStores(version int64, storeMap map[types.KVStoreKey]types.CommitKVStore) commitInfo { storeInfos := make([]storeInfo, 0, len(storeMap)) for key, store := range storeMap { diff --git a/store/transient/key.go b/store/transient/key.go index 5be0549b223f..96a90455dc03 100644 --- a/store/transient/key.go +++ b/store/transient/key.go @@ -6,7 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/store/types" ) -var _ types.StoreKey = (*StoreKey)(nil) +var _ types.KVStoreKey = (*StoreKey)(nil) // StoreKey is used for indexing transient stores in a MultiStore type StoreKey struct { @@ -32,6 +32,6 @@ func (key *StoreKey) String() string { } // Implements StoreKey -func (key *StoreKey) NewStore() types.CommitStore { +func (key *StoreKey) NewStore() types.CommitKVStore { return &Store{} } diff --git a/store/types/store.go b/store/types/store.go index e0a10698cd51..9b351360536e 100644 --- a/store/types/store.go +++ b/store/types/store.go @@ -45,7 +45,7 @@ type Queryable interface { type MultiStore interface { //nolint // Convenience for fetching substores. - GetKVStore(StoreKey) KVStore + GetKVStore(KVStoreKey) KVStore // TODO: recursive multistore not yet supported // GetMultiStore(StoreKey) MultiStore @@ -67,19 +67,21 @@ type CommitMultiStore interface { CommitStore MultiStore - // Mount a store of type using the given db. + // Mount a KVStore of the key using the given db. // If db == nil, the new store will use the CommitMultiStore db. - MountStoreWithDB(key StoreKey, db dbm.DB) + MountKVStoreWithDB(key KVStoreKey, db dbm.DB) - // Panics on a nil key. - GetCommitStore(key StoreKey) CommitStore + // TODO: recursive multistore not implemented + // Mount a MultiStore of the key using the given db. + // If db == nil, the new store will use the CommitMultiStore db. + // MountMultiStoreWithDB(key MultiStoreKey, db dbm.DB) // Panics on a nil key. - GetCommitKVStore(key StoreKey) CommitKVStore + GetCommitKVStore(key KVStoreKey) CommitKVStore // Panics on a nil key. // TODO: recursive multistore not yet supported - // GetCommitMultiStore(key StoreKey) CommitMultiStore + // GetCommitMultiStore(key MultiStoreKey) CommitMultiStore // Load the latest persisted version. Called once after all // calls to Mount*Store() are complete. @@ -192,12 +194,22 @@ func (cid CommitID) String() string { //---------------------------------------- // Keys for accessing substores -// StoreKey is a key used to index stores in a MultiStore. -type StoreKey interface { +// KVStoreKey is a key used to index KVStores in a MultiStore. +type KVStoreKey interface { + Name() string + String() string + NewStore() CommitKVStore +} + +/* +// TODO: recursive multistore not implemented +// MultiStoreKey is a key used to index MultiStores in a MultiStore. +type MultiStoreKey interface { Name() string String() string - NewStore() CommitStore + NewStore() CommitMultiStore } +*/ // PrefixEndBytes returns the []byte that would end a // range query for all []byte with a certain prefix diff --git a/types/context.go b/types/context.go index 702cea7e3cda..bda2072bd6ee 100644 --- a/types/context.go +++ b/types/context.go @@ -74,7 +74,7 @@ func (c Context) Value(key interface{}) interface{} { } // KVStore fetches a KVStore from the MultiStore. -func (c Context) KVStore(key StoreKey) KVStore { +func (c Context) KVStore(key KVStoreKey) KVStore { return gas.NewStore( &GasTank{ GasMeter: c.GasMeter(), @@ -85,7 +85,7 @@ func (c Context) KVStore(key StoreKey) KVStore { } // TransientStore fetches a TransientStore from the MultiStore -func (c Context) TransientStore(key StoreKey) KVStore { +func (c Context) TransientStore(key KVStoreKey) KVStore { return gas.NewStore( &GasTank{ GasMeter: c.GasMeter(), diff --git a/types/store.go b/types/store.go index d9d40a70540e..d9f92bd8337e 100644 --- a/types/store.go +++ b/types/store.go @@ -22,8 +22,8 @@ type ( KVPair = types.KVPair - StoreKey = types.StoreKey - KVStoreKey = iavl.StoreKey + KVStoreKey = types.KVStoreKey + IAVLStoreKey = iavl.StoreKey TransientStoreKey = transient.StoreKey Gas = types.Gas @@ -57,7 +57,7 @@ func PrefixEndBytes(prefix []byte) []byte { func InclusiveEndBytes(inclusiveBytes []byte) []byte { return types.InclusiveEndBytes(inclusiveBytes) } -func NewKVStoreKey(name string) *KVStoreKey { +func NewKVStoreKey(name string) *IAVLStoreKey { return iavl.NewKey(name) } func NewTransientStoreKey(name string) *TransientStoreKey { From 155f94564a0533766aeb51320878c8cb479052c3 Mon Sep 17 00:00:00 2001 From: mossid Date: Fri, 26 Oct 2018 12:42:34 +0200 Subject: [PATCH 15/22] finalize rebase --- baseapp/baseapp.go | 16 ++++++++-------- baseapp/baseapp_test.go | 4 ++-- cmd/gaia/app/app.go | 18 +++++++++--------- cmd/gaia/cmd/gaiadebug/hack.go | 10 +++++----- x/auth/feekeeper.go | 4 ++-- x/auth/mapper.go | 4 ++-- x/distribution/keeper/keeper.go | 4 ++-- x/distribution/keeper/test_common.go | 14 +++++++------- x/gov/keeper.go | 4 ++-- x/mint/keeper.go | 4 ++-- x/mock/app.go | 6 +++--- x/params/keeper.go | 6 +++--- x/params/subspace/subspace.go | 6 +++--- x/params/subspace/test_common.go | 4 ++-- x/slashing/keeper.go | 4 ++-- x/slashing/test_common.go | 12 ++++++------ x/stake/keeper/keeper.go | 6 +++--- x/stake/keeper/test_common.go | 10 +++++----- 18 files changed, 68 insertions(+), 68 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 42369fc70f4b..40d06e1029e3 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -124,7 +124,7 @@ func (app *BaseApp) RegisterCodespace(codespace sdk.CodespaceType) sdk.Codespace } // Mount IAVL stores to the provided keys in the BaseApp multistore -func (app *BaseApp) MountStoresIAVL(keys ...*sdk.KVStoreKey) { +func (app *BaseApp) MountStoresIAVL(keys ...sdk.KVStoreKey) { for _, key := range keys { app.MountStore(key) } @@ -138,17 +138,17 @@ func (app *BaseApp) MountStoresTransient(keys ...*sdk.TransientStoreKey) { } // Mount a store to the provided key in the BaseApp multistore, using a specified DB -func (app *BaseApp) MountStoreWithDB(key sdk.StoreKey, db dbm.DB) { - app.cms.MountStoreWithDB(key, db) +func (app *BaseApp) MountStoreWithDB(key sdk.KVStoreKey, db dbm.DB) { + app.cms.MountKVStoreWithDB(key, db) } // Mount a store to the provided key in the BaseApp multistore, using the default DB -func (app *BaseApp) MountStore(key sdk.StoreKey) { - app.cms.MountStoreWithDB(key, nil) +func (app *BaseApp) MountStore(key sdk.KVStoreKey) { + app.cms.MountKVStoreWithDB(key, nil) } // load latest application version -func (app *BaseApp) LoadLatestVersion(mainKey sdk.StoreKey) error { +func (app *BaseApp) LoadLatestVersion(mainKey sdk.KVStoreKey) error { err := app.cms.LoadLatestVersion() if err != nil { return err @@ -157,7 +157,7 @@ func (app *BaseApp) LoadLatestVersion(mainKey sdk.StoreKey) error { } // load application version -func (app *BaseApp) LoadVersion(version int64, mainKey sdk.StoreKey) error { +func (app *BaseApp) LoadVersion(version int64, mainKey sdk.KVStoreKey) error { err := app.cms.LoadMultiStoreVersion(version) if err != nil { return err @@ -176,7 +176,7 @@ func (app *BaseApp) LastBlockHeight() int64 { } // initializes the remaining logic from app.cms -func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error { +func (app *BaseApp) initFromStore(mainKey sdk.KVStoreKey) error { // main store should exist. // TODO: we don't actually need the main store here main := app.cms.GetKVStore(mainKey) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index ff6cbb83418f..33387dc23a3e 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -366,7 +366,7 @@ func testTxDecoder(cdc *codec.Codec) sdk.TxDecoder { } } -func anteHandlerTxTest(t *testing.T, capKey *sdk.KVStoreKey, storeKey []byte) sdk.AnteHandler { +func anteHandlerTxTest(t *testing.T, capKey sdk.KVStoreKey, storeKey []byte) sdk.AnteHandler { return func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, res sdk.Result, abort bool) { store := ctx.KVStore(capKey) msgCounter := tx.(txTest).Counter @@ -375,7 +375,7 @@ func anteHandlerTxTest(t *testing.T, capKey *sdk.KVStoreKey, storeKey []byte) sd } } -func handlerMsgCounter(t *testing.T, capKey *sdk.KVStoreKey, deliverKey []byte) sdk.Handler { +func handlerMsgCounter(t *testing.T, capKey sdk.KVStoreKey, deliverKey []byte) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { store := ctx.KVStore(capKey) var msgCount int64 diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index 21ef855f026a..16108a7e9a24 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -43,17 +43,17 @@ type GaiaApp struct { cdc *codec.Codec // keys to access the substores - keyMain *sdk.KVStoreKey - keyAccount *sdk.KVStoreKey - keyStake *sdk.KVStoreKey + keyMain sdk.KVStoreKey + keyAccount sdk.KVStoreKey + keyStake sdk.KVStoreKey tkeyStake *sdk.TransientStoreKey - keySlashing *sdk.KVStoreKey - keyMint *sdk.KVStoreKey - keyDistr *sdk.KVStoreKey + keySlashing sdk.KVStoreKey + keyMint sdk.KVStoreKey + keyDistr sdk.KVStoreKey tkeyDistr *sdk.TransientStoreKey - keyGov *sdk.KVStoreKey - keyFeeCollection *sdk.KVStoreKey - keyParams *sdk.KVStoreKey + keyGov sdk.KVStoreKey + keyFeeCollection sdk.KVStoreKey + keyParams sdk.KVStoreKey tkeyParams *sdk.TransientStoreKey // Manage getting and setting accounts diff --git a/cmd/gaia/cmd/gaiadebug/hack.go b/cmd/gaia/cmd/gaiadebug/hack.go index 5e307709ac5e..c0b232e8449c 100644 --- a/cmd/gaia/cmd/gaiadebug/hack.go +++ b/cmd/gaia/cmd/gaiadebug/hack.go @@ -129,12 +129,12 @@ type GaiaApp struct { cdc *codec.Codec // keys to access the substores - keyMain *sdk.KVStoreKey - keyAccount *sdk.KVStoreKey - keyStake *sdk.KVStoreKey + keyMain sdk.KVStoreKey + keyAccount sdk.KVStoreKey + keyStake sdk.KVStoreKey tkeyStake *sdk.TransientStoreKey - keySlashing *sdk.KVStoreKey - keyParams *sdk.KVStoreKey + keySlashing sdk.KVStoreKey + keyParams sdk.KVStoreKey tkeyParams *sdk.TransientStoreKey // Manage getting and setting accounts diff --git a/x/auth/feekeeper.go b/x/auth/feekeeper.go index a6be2e12def0..242c4161719d 100644 --- a/x/auth/feekeeper.go +++ b/x/auth/feekeeper.go @@ -14,13 +14,13 @@ var ( type FeeCollectionKeeper struct { // The (unexposed) key used to access the fee store from the Context. - key sdk.StoreKey + key sdk.KVStoreKey // The codec codec for binary encoding/decoding of accounts. cdc *codec.Codec } -func NewFeeCollectionKeeper(cdc *codec.Codec, key sdk.StoreKey) FeeCollectionKeeper { +func NewFeeCollectionKeeper(cdc *codec.Codec, key sdk.KVStoreKey) FeeCollectionKeeper { return FeeCollectionKeeper{ key: key, cdc: cdc, diff --git a/x/auth/mapper.go b/x/auth/mapper.go index dd127c7312e3..fb13044dc88d 100644 --- a/x/auth/mapper.go +++ b/x/auth/mapper.go @@ -13,7 +13,7 @@ var globalAccountNumberKey = []byte("globalAccountNumber") type AccountKeeper struct { // The (unexposed) key used to access the store from the Context. - key sdk.StoreKey + key sdk.KVStoreKey // The prototypical Account constructor. proto func() Account @@ -25,7 +25,7 @@ type AccountKeeper struct { // NewAccountKeeper returns a new sdk.AccountKeeper that // uses go-amino to (binary) encode and decode concrete sdk.Accounts. // nolint -func NewAccountKeeper(cdc *codec.Codec, key sdk.StoreKey, proto func() Account) AccountKeeper { +func NewAccountKeeper(cdc *codec.Codec, key sdk.KVStoreKey, proto func() Account) AccountKeeper { return AccountKeeper{ key: key, proto: proto, diff --git a/x/distribution/keeper/keeper.go b/x/distribution/keeper/keeper.go index d167e0efe275..05585470b969 100644 --- a/x/distribution/keeper/keeper.go +++ b/x/distribution/keeper/keeper.go @@ -9,7 +9,7 @@ import ( // keeper of the stake store type Keeper struct { - storeKey sdk.StoreKey + storeKey sdk.KVStoreKey cdc *codec.Codec paramSpace params.Subspace bankKeeper types.BankKeeper @@ -20,7 +20,7 @@ type Keeper struct { codespace sdk.CodespaceType } -func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramSpace params.Subspace, ck types.BankKeeper, +func NewKeeper(cdc *codec.Codec, key sdk.KVStoreKey, paramSpace params.Subspace, ck types.BankKeeper, sk types.StakeKeeper, fck types.FeeCollectionKeeper, codespace sdk.CodespaceType) Keeper { keeper := Keeper{ diff --git a/x/distribution/keeper/test_common.go b/x/distribution/keeper/test_common.go index 303359cc99cd..f1fdc5c871db 100644 --- a/x/distribution/keeper/test_common.go +++ b/x/distribution/keeper/test_common.go @@ -94,13 +94,13 @@ func CreateTestInputAdvanced(t *testing.T, isCheckTx bool, initCoins int64, db := dbm.NewMemDB() ms := store.NewCommitMultiStore(db) - ms.MountStoreWithDB(keyDistr, db) - ms.MountStoreWithDB(tkeyStake, nil) - ms.MountStoreWithDB(keyStake, db) - ms.MountStoreWithDB(keyAcc, db) - ms.MountStoreWithDB(keyFeeCollection, db) - ms.MountStoreWithDB(keyParams, db) - ms.MountStoreWithDB(tkeyParams, db) + ms.MountKVStoreWithDB(keyDistr, db) + ms.MountKVStoreWithDB(tkeyStake, nil) + ms.MountKVStoreWithDB(keyStake, db) + ms.MountKVStoreWithDB(keyAcc, db) + ms.MountKVStoreWithDB(keyFeeCollection, db) + ms.MountKVStoreWithDB(keyParams, db) + ms.MountKVStoreWithDB(tkeyParams, db) err := ms.LoadLatestVersion() require.Nil(t, err) diff --git a/x/gov/keeper.go b/x/gov/keeper.go index d061f1206232..92d23df7964f 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -46,7 +46,7 @@ type Keeper struct { ds sdk.DelegationSet // The (unexposed) keys used to access the stores from the Context. - storeKey sdk.StoreKey + storeKey sdk.KVStoreKey // The codec codec for binary encoding/decoding. cdc *codec.Codec @@ -60,7 +60,7 @@ type Keeper struct { // - depositing funds into proposals, and activating upon sufficient funds being deposited // - users voting on proposals, with weight proportional to stake in the system // - and tallying the result of the vote. -func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramsKeeper params.Keeper, paramSpace params.Subspace, ck bank.Keeper, ds sdk.DelegationSet, codespace sdk.CodespaceType) Keeper { +func NewKeeper(cdc *codec.Codec, key sdk.KVStoreKey, paramsKeeper params.Keeper, paramSpace params.Subspace, ck bank.Keeper, ds sdk.DelegationSet, codespace sdk.CodespaceType) Keeper { return Keeper{ storeKey: key, paramsKeeper: paramsKeeper, diff --git a/x/mint/keeper.go b/x/mint/keeper.go index 6e6a642f48c3..bfef1d64b240 100644 --- a/x/mint/keeper.go +++ b/x/mint/keeper.go @@ -8,14 +8,14 @@ import ( // keeper of the stake store type Keeper struct { - storeKey sdk.StoreKey + storeKey sdk.KVStoreKey cdc *codec.Codec paramSpace params.Subspace sk StakeKeeper fck FeeCollectionKeeper } -func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, +func NewKeeper(cdc *codec.Codec, key sdk.KVStoreKey, paramSpace params.Subspace, sk StakeKeeper, fck FeeCollectionKeeper) Keeper { keeper := Keeper{ diff --git a/x/mock/app.go b/x/mock/app.go index bf00f2f17225..782f2a5dc5a8 100644 --- a/x/mock/app.go +++ b/x/mock/app.go @@ -24,8 +24,8 @@ const chainID = "" type App struct { *bam.BaseApp Cdc *codec.Codec // Cdc is public since the codec is passed into the module anyways - KeyMain *sdk.KVStoreKey - KeyAccount *sdk.KVStoreKey + KeyMain sdk.KVStoreKey + KeyAccount sdk.KVStoreKey // TODO: Abstract this out from not needing to be auth specifically AccountKeeper auth.AccountKeeper @@ -75,7 +75,7 @@ func NewApp() *App { // CompleteSetup completes the application setup after the routes have been // registered. -func (app *App) CompleteSetup(newKeys ...sdk.StoreKey) error { +func (app *App) CompleteSetup(newKeys ...sdk.KVStoreKey) error { newKeys = append(newKeys, app.KeyMain) newKeys = append(newKeys, app.KeyAccount) diff --git a/x/params/keeper.go b/x/params/keeper.go index cf78b60ff4f3..da8d4da8a560 100644 --- a/x/params/keeper.go +++ b/x/params/keeper.go @@ -10,14 +10,14 @@ import ( // Keeper of the global paramstore type Keeper struct { cdc *codec.Codec - key sdk.StoreKey - tkey sdk.StoreKey + key sdk.KVStoreKey + tkey sdk.KVStoreKey spaces map[string]*Subspace } // NewKeeper constructs a params keeper -func NewKeeper(cdc *codec.Codec, key *sdk.KVStoreKey, tkey *sdk.TransientStoreKey) (k Keeper) { +func NewKeeper(cdc *codec.Codec, key sdk.KVStoreKey, tkey *sdk.TransientStoreKey) (k Keeper) { k = Keeper{ cdc: cdc, key: key, diff --git a/x/params/subspace/subspace.go b/x/params/subspace/subspace.go index 313b41795e7c..3f42ac81ef68 100644 --- a/x/params/subspace/subspace.go +++ b/x/params/subspace/subspace.go @@ -14,8 +14,8 @@ import ( // recording whether the parameter has been changed or not type Subspace struct { cdc *codec.Codec - key sdk.StoreKey // []byte -> []byte, stores parameter - tkey sdk.StoreKey // []byte -> bool, stores parameter change + key sdk.KVStoreKey // []byte -> []byte, stores parameter + tkey sdk.KVStoreKey // []byte -> bool, stores parameter change name []byte @@ -23,7 +23,7 @@ type Subspace struct { } // NewSubspace constructs a store with namestore -func NewSubspace(cdc *codec.Codec, key sdk.StoreKey, tkey sdk.StoreKey, name string) (res Subspace) { +func NewSubspace(cdc *codec.Codec, key sdk.KVStoreKey, tkey sdk.KVStoreKey, name string) (res Subspace) { res = Subspace{ cdc: cdc, key: key, diff --git a/x/params/subspace/test_common.go b/x/params/subspace/test_common.go index bd8c071a08d8..ebf2d5d31ab3 100644 --- a/x/params/subspace/test_common.go +++ b/x/params/subspace/test_common.go @@ -30,8 +30,8 @@ func DefaultTestComponents(t *testing.T, table TypeTable) (sdk.Context, Subspace tracer := ms.GetTracer() tracer.SetWriter(os.Stdout) tracer.SetContext(sdk.TraceContext{}) - ms.MountStoreWithDB(key, db) - ms.MountStoreWithDB(tkey, db) + ms.MountKVStoreWithDB(key, db) + ms.MountKVStoreWithDB(tkey, db) err := ms.LoadLatestVersion() require.Nil(t, err) ctx := sdk.NewContext(ms, abci.Header{}, false, log.NewTMLogger(os.Stdout)) diff --git a/x/slashing/keeper.go b/x/slashing/keeper.go index 647a9df09863..5f81cf966110 100644 --- a/x/slashing/keeper.go +++ b/x/slashing/keeper.go @@ -16,7 +16,7 @@ import ( // Keeper of the slashing store type Keeper struct { - storeKey sdk.StoreKey + storeKey sdk.KVStoreKey cdc *codec.Codec validatorSet sdk.ValidatorSet paramspace params.Subspace @@ -26,7 +26,7 @@ type Keeper struct { } // NewKeeper creates a slashing keeper -func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, vs sdk.ValidatorSet, paramspace params.Subspace, codespace sdk.CodespaceType) Keeper { +func NewKeeper(cdc *codec.Codec, key sdk.KVStoreKey, vs sdk.ValidatorSet, paramspace params.Subspace, codespace sdk.CodespaceType) Keeper { keeper := Keeper{ storeKey: key, cdc: cdc, diff --git a/x/slashing/test_common.go b/x/slashing/test_common.go index 59e2ec7bf472..9d84530a9e7c 100644 --- a/x/slashing/test_common.go +++ b/x/slashing/test_common.go @@ -58,12 +58,12 @@ func createTestInput(t *testing.T, defaults Params) (sdk.Context, bank.Keeper, s tkeyParams := sdk.NewTransientStoreKey("transient_params") db := dbm.NewMemDB() ms := store.NewCommitMultiStore(db) - ms.MountStoreWithDB(keyAcc, db) - ms.MountStoreWithDB(tkeyStake, nil) - ms.MountStoreWithDB(keyStake, db) - ms.MountStoreWithDB(keySlashing, db) - ms.MountStoreWithDB(keyParams, db) - ms.MountStoreWithDB(tkeyParams, db) + ms.MountKVStoreWithDB(keyAcc, db) + ms.MountKVStoreWithDB(tkeyStake, nil) + ms.MountKVStoreWithDB(keyStake, db) + ms.MountKVStoreWithDB(keySlashing, db) + ms.MountKVStoreWithDB(keyParams, db) + ms.MountKVStoreWithDB(tkeyParams, db) err := ms.LoadLatestVersion() require.Nil(t, err) diff --git a/x/stake/keeper/keeper.go b/x/stake/keeper/keeper.go index 17ee31268cff..743d332f1f37 100644 --- a/x/stake/keeper/keeper.go +++ b/x/stake/keeper/keeper.go @@ -11,8 +11,8 @@ import ( // keeper of the stake store type Keeper struct { - storeKey sdk.StoreKey - storeTKey sdk.StoreKey + storeKey sdk.KVStoreKey + storeTKey sdk.KVStoreKey cdc *codec.Codec bankKeeper bank.Keeper hooks sdk.StakingHooks @@ -22,7 +22,7 @@ type Keeper struct { codespace sdk.CodespaceType } -func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, ck bank.Keeper, paramstore params.Subspace, codespace sdk.CodespaceType) Keeper { +func NewKeeper(cdc *codec.Codec, key, tkey sdk.KVStoreKey, ck bank.Keeper, paramstore params.Subspace, codespace sdk.CodespaceType) Keeper { keeper := Keeper{ storeKey: key, storeTKey: tkey, diff --git a/x/stake/keeper/test_common.go b/x/stake/keeper/test_common.go index 7d404d70fc63..8992c201dc55 100644 --- a/x/stake/keeper/test_common.go +++ b/x/stake/keeper/test_common.go @@ -84,11 +84,11 @@ func CreateTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context db := dbm.NewMemDB() ms := rootmulti.NewStore(db) - ms.MountStoreWithDB(tkeyStake, nil) - ms.MountStoreWithDB(keyStake, db) - ms.MountStoreWithDB(keyAcc, db) - ms.MountStoreWithDB(keyParams, db) - ms.MountStoreWithDB(tkeyParams, db) + ms.MountKVStoreWithDB(tkeyStake, nil) + ms.MountKVStoreWithDB(keyStake, db) + ms.MountKVStoreWithDB(keyAcc, db) + ms.MountKVStoreWithDB(keyParams, db) + ms.MountKVStoreWithDB(tkeyParams, db) err := ms.LoadLatestVersion() require.Nil(t, err) From 5b807727b0a179e08dcc01c3198c30b157e4a67c Mon Sep 17 00:00:00 2001 From: mossid Date: Mon, 29 Oct 2018 10:18:20 +0100 Subject: [PATCH 16/22] make it compile again --- docs/sdk/core/examples/app1.go | 2 +- docs/sdk/core/examples/app2.go | 2 +- examples/basecoin/app/app.go | 6 ++--- examples/democoin/app/app.go | 10 +++---- .../democoin/x/assoc/validator_set_test.go | 4 +-- examples/democoin/x/cool/keeper.go | 4 +-- examples/democoin/x/cool/keeper_test.go | 4 +-- examples/democoin/x/oracle/keeper.go | 4 +-- examples/democoin/x/oracle/oracle_test.go | 10 +++---- examples/democoin/x/pow/keeper.go | 4 +-- examples/democoin/x/pow/keeper_test.go | 4 +-- examples/democoin/x/simplestake/keeper.go | 4 +-- .../democoin/x/simplestake/keeper_test.go | 6 ++--- server/mock/app.go | 4 +-- store/basemulti/store.go | 27 +++++++++++++++---- store/iavlmulti/store.go | 3 ++- store/list/list_test.go | 4 +-- store/queue/queue_test.go | 4 +-- store/rootmulti/store_test.go | 24 ++++++++--------- x/ibc/mapper.go | 4 +-- 20 files changed, 76 insertions(+), 58 deletions(-) diff --git a/docs/sdk/core/examples/app1.go b/docs/sdk/core/examples/app1.go index 8ee34fca59e7..57a1123e065a 100644 --- a/docs/sdk/core/examples/app1.go +++ b/docs/sdk/core/examples/app1.go @@ -101,7 +101,7 @@ func (msg MsgSend) Tags() sdk.Tags { // Handle MsgSend. // NOTE: msg.From, msg.To, and msg.Amount were already validated // in ValidateBasic(). -func handleMsgSend(key *sdk.KVStoreKey) sdk.Handler { +func handleMsgSend(key sdk.KVStoreKey) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { sendMsg, ok := msg.(MsgSend) if !ok { diff --git a/docs/sdk/core/examples/app2.go b/docs/sdk/core/examples/app2.go index c1ae43666824..c15cc408e7ee 100644 --- a/docs/sdk/core/examples/app2.go +++ b/docs/sdk/core/examples/app2.go @@ -122,7 +122,7 @@ func (msg MsgIssue) Tags() sdk.Tags { // Handler for the message // Handle MsgIssue. -func handleMsgIssue(keyIssue *sdk.KVStoreKey, keyAcc *sdk.KVStoreKey) sdk.Handler { +func handleMsgIssue(keyIssue sdk.KVStoreKey, keyAcc sdk.KVStoreKey) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { issueMsg, ok := msg.(MsgIssue) if !ok { diff --git a/examples/basecoin/app/app.go b/examples/basecoin/app/app.go index 286afd003e26..26754e9c0012 100644 --- a/examples/basecoin/app/app.go +++ b/examples/basecoin/app/app.go @@ -37,9 +37,9 @@ type BasecoinApp struct { cdc *codec.Codec // keys to access the multistore - keyMain *sdk.KVStoreKey - keyAccount *sdk.KVStoreKey - keyIBC *sdk.KVStoreKey + keyMain sdk.KVStoreKey + keyAccount sdk.KVStoreKey + keyIBC sdk.KVStoreKey // manage getting and setting accounts accountKeeper auth.AccountKeeper diff --git a/examples/democoin/app/app.go b/examples/democoin/app/app.go index 12f5d8d2947c..227dd71981b6 100644 --- a/examples/democoin/app/app.go +++ b/examples/democoin/app/app.go @@ -40,11 +40,11 @@ type DemocoinApp struct { cdc *codec.Codec // keys to access the substores - capKeyMainStore *sdk.KVStoreKey - capKeyAccountStore *sdk.KVStoreKey - capKeyPowStore *sdk.KVStoreKey - capKeyIBCStore *sdk.KVStoreKey - capKeyStakingStore *sdk.KVStoreKey + capKeyMainStore sdk.KVStoreKey + capKeyAccountStore sdk.KVStoreKey + capKeyPowStore sdk.KVStoreKey + capKeyIBCStore sdk.KVStoreKey + capKeyStakingStore sdk.KVStoreKey // keepers feeCollectionKeeper auth.FeeCollectionKeeper diff --git a/examples/democoin/x/assoc/validator_set_test.go b/examples/democoin/x/assoc/validator_set_test.go index 2215c51187c8..b70a2c149423 100644 --- a/examples/democoin/x/assoc/validator_set_test.go +++ b/examples/democoin/x/assoc/validator_set_test.go @@ -16,10 +16,10 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -func defaultContext(key sdk.StoreKey) sdk.Context { +func defaultContext(key sdk.KVStoreKey) sdk.Context { db := dbm.NewMemDB() cms := store.NewCommitMultiStore(db) - cms.MountStoreWithDB(key, db) + cms.MountKVStoreWithDB(key, db) cms.LoadLatestVersion() ctx := sdk.NewContext(cms, abci.Header{}, false, nil) return ctx diff --git a/examples/democoin/x/cool/keeper.go b/examples/democoin/x/cool/keeper.go index f805ca880b40..59982507905e 100644 --- a/examples/democoin/x/cool/keeper.go +++ b/examples/democoin/x/cool/keeper.go @@ -9,13 +9,13 @@ import ( type Keeper struct { ck bank.Keeper - storeKey sdk.StoreKey // The (unexposed) key used to access the store from the Context. + storeKey sdk.KVStoreKey // The (unexposed) key used to access the store from the Context. codespace sdk.CodespaceType } // NewKeeper - Returns the Keeper -func NewKeeper(key sdk.StoreKey, bankKeeper bank.Keeper, codespace sdk.CodespaceType) Keeper { +func NewKeeper(key sdk.KVStoreKey, bankKeeper bank.Keeper, codespace sdk.CodespaceType) Keeper { return Keeper{bankKeeper, key, codespace} } diff --git a/examples/democoin/x/cool/keeper_test.go b/examples/democoin/x/cool/keeper_test.go index 451c43e680fa..1056e93f5bca 100644 --- a/examples/democoin/x/cool/keeper_test.go +++ b/examples/democoin/x/cool/keeper_test.go @@ -15,11 +15,11 @@ import ( bank "github.com/cosmos/cosmos-sdk/x/bank" ) -func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey) { +func setupMultiStore() (sdk.MultiStore, sdk.KVStoreKey) { db := dbm.NewMemDB() capKey := sdk.NewKVStoreKey("capkey") ms := store.NewCommitMultiStore(db) - ms.MountStoreWithDB(capKey, db) + ms.MountKVStoreWithDB(capKey, db) ms.LoadLatestVersion() return ms, capKey } diff --git a/examples/democoin/x/oracle/keeper.go b/examples/democoin/x/oracle/keeper.go index e55cd7083c0d..ed6758ddaebe 100644 --- a/examples/democoin/x/oracle/keeper.go +++ b/examples/democoin/x/oracle/keeper.go @@ -8,7 +8,7 @@ import ( // Keeper of the oracle store type Keeper struct { - key sdk.StoreKey + key sdk.KVStoreKey cdc *codec.Codec valset sdk.ValidatorSet @@ -18,7 +18,7 @@ type Keeper struct { } // NewKeeper constructs a new keeper -func NewKeeper(key sdk.StoreKey, cdc *codec.Codec, valset sdk.ValidatorSet, supermaj sdk.Dec, timeout int64) Keeper { +func NewKeeper(key sdk.KVStoreKey, cdc *codec.Codec, valset sdk.ValidatorSet, supermaj sdk.Dec, timeout int64) Keeper { if timeout < 0 { panic("Timeout should not be negative") } diff --git a/examples/democoin/x/oracle/oracle_test.go b/examples/democoin/x/oracle/oracle_test.go index 1672a3b7c1a8..fddbffdb5050 100644 --- a/examples/democoin/x/oracle/oracle_test.go +++ b/examples/democoin/x/oracle/oracle_test.go @@ -15,11 +15,11 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -func defaultContext(keys ...sdk.StoreKey) sdk.Context { +func defaultContext(keys ...sdk.KVStoreKey) sdk.Context { db := dbm.NewMemDB() cms := store.NewCommitMultiStore(db) for _, key := range keys { - cms.MountStoreWithDB(key, db) + cms.MountKVStoreWithDB(key, db) } cms.LoadLatestVersion() ctx := sdk.NewContext(cms, abci.Header{}, false, nil) @@ -56,7 +56,7 @@ func makeCodec() *codec.Codec { return cdc } -func seqHandler(ork Keeper, key sdk.StoreKey, codespace sdk.CodespaceType) sdk.Handler { +func seqHandler(ork Keeper, key sdk.KVStoreKey, codespace sdk.CodespaceType) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { switch msg := msg.(type) { case Msg: @@ -74,7 +74,7 @@ func seqHandler(ork Keeper, key sdk.StoreKey, codespace sdk.CodespaceType) sdk.H } } -func getSequence(ctx sdk.Context, key sdk.StoreKey) int { +func getSequence(ctx sdk.Context, key sdk.KVStoreKey) int { store := ctx.KVStore(key) seqbz := store.Get([]byte("seq")) @@ -88,7 +88,7 @@ func getSequence(ctx sdk.Context, key sdk.StoreKey) int { return seq } -func handleSeqOracle(ctx sdk.Context, key sdk.StoreKey, o seqOracle) sdk.Error { +func handleSeqOracle(ctx sdk.Context, key sdk.KVStoreKey, o seqOracle) sdk.Error { store := ctx.KVStore(key) seq := getSequence(ctx, key) diff --git a/examples/democoin/x/pow/keeper.go b/examples/democoin/x/pow/keeper.go index 38a0d93c6a0a..37c2756e79cc 100644 --- a/examples/democoin/x/pow/keeper.go +++ b/examples/democoin/x/pow/keeper.go @@ -22,7 +22,7 @@ type Genesis struct { // POW Keeper type Keeper struct { - key sdk.StoreKey + key sdk.KVStoreKey config Config ck bank.Keeper codespace sdk.CodespaceType @@ -32,7 +32,7 @@ func NewConfig(denomination string, reward int64) Config { return Config{denomination, reward} } -func NewKeeper(key sdk.StoreKey, config Config, ck bank.Keeper, codespace sdk.CodespaceType) Keeper { +func NewKeeper(key sdk.KVStoreKey, config Config, ck bank.Keeper, codespace sdk.CodespaceType) Keeper { return Keeper{key, config, ck, codespace} } diff --git a/examples/democoin/x/pow/keeper_test.go b/examples/democoin/x/pow/keeper_test.go index b833c05a2f01..ef322ac70ee9 100644 --- a/examples/democoin/x/pow/keeper_test.go +++ b/examples/democoin/x/pow/keeper_test.go @@ -17,11 +17,11 @@ import ( ) // possibly share this kind of setup functionality between module testsuites? -func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey) { +func setupMultiStore() (sdk.MultiStore, sdk.KVStoreKey) { db := dbm.NewMemDB() capKey := sdk.NewKVStoreKey("capkey") ms := store.NewCommitMultiStore(db) - ms.MountStoreWithDB(capKey, db) + ms.MountKVStoreWithDB(capKey, db) ms.LoadLatestVersion() return ms, capKey diff --git a/examples/democoin/x/simplestake/keeper.go b/examples/democoin/x/simplestake/keeper.go index 7bdc17937b55..646ed5467064 100644 --- a/examples/democoin/x/simplestake/keeper.go +++ b/examples/democoin/x/simplestake/keeper.go @@ -16,12 +16,12 @@ const moduleName = "simplestake" type Keeper struct { ck bank.Keeper - key sdk.StoreKey + key sdk.KVStoreKey cdc *codec.Codec codespace sdk.CodespaceType } -func NewKeeper(key sdk.StoreKey, bankKeeper bank.Keeper, codespace sdk.CodespaceType) Keeper { +func NewKeeper(key sdk.KVStoreKey, bankKeeper bank.Keeper, codespace sdk.CodespaceType) Keeper { cdc := codec.New() codec.RegisterCrypto(cdc) return Keeper{ diff --git a/examples/democoin/x/simplestake/keeper_test.go b/examples/democoin/x/simplestake/keeper_test.go index 36dc614f0fbc..54c5e7b41e3c 100644 --- a/examples/democoin/x/simplestake/keeper_test.go +++ b/examples/democoin/x/simplestake/keeper_test.go @@ -19,13 +19,13 @@ import ( "github.com/cosmos/cosmos-sdk/x/bank" ) -func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey) { +func setupMultiStore() (sdk.MultiStore, sdk.KVStoreKey, sdk.KVStoreKey) { db := dbm.NewMemDB() authKey := sdk.NewKVStoreKey("authkey") capKey := sdk.NewKVStoreKey("capkey") ms := store.NewCommitMultiStore(db) - ms.MountStoreWithDB(capKey, db) - ms.MountStoreWithDB(authKey, db) + ms.MountKVStoreWithDB(capKey, db) + ms.MountKVStoreWithDB(authKey, db) ms.LoadLatestVersion() return ms, authKey, capKey } diff --git a/server/mock/app.go b/server/mock/app.go index 18afa164ec64..08e3ed35d722 100644 --- a/server/mock/app.go +++ b/server/mock/app.go @@ -47,7 +47,7 @@ func NewApp(rootDir string, logger log.Logger) (abci.Application, error) { // KVStoreHandler is a simple handler that takes kvstoreTx and writes // them to the db -func KVStoreHandler(storeKey sdk.StoreKey) sdk.Handler { +func KVStoreHandler(storeKey sdk.KVStoreKey) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { dTx, ok := msg.(kvstoreTx) if !ok { @@ -81,7 +81,7 @@ type GenesisJSON struct { // InitChainer returns a function that can initialize the chain // with key/value pairs -func InitChainer(key sdk.StoreKey) func(sdk.Context, abci.RequestInitChain) abci.ResponseInitChain { +func InitChainer(key sdk.KVStoreKey) func(sdk.Context, abci.RequestInitChain) abci.ResponseInitChain { return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain { stateJSON := req.AppStateBytes diff --git a/store/basemulti/store.go b/store/basemulti/store.go index 9ca0d59b2e32..43c3aa35eda0 100644 --- a/store/basemulti/store.go +++ b/store/basemulti/store.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/tendermint/tendermint/crypto/merkle" + "github.com/tendermint/tendermint/crypto/tmhash" dbm "github.com/tendermint/tendermint/libs/db" "github.com/cosmos/cosmos-sdk/store/types" @@ -35,7 +36,6 @@ func (store *Store) GetCommitKVStore(key types.KVStoreKey) types.CommitKVStore { func (store *Store) LoadMultiStoreVersion(ver int64) (err error) { // Convert StoreInfos slice to map - var lastCommitID types.CommitID infos := make(map[types.KVStoreKey]storeInfo) if ver != 0 { // Get commitInfo @@ -48,7 +48,7 @@ func (store *Store) LoadMultiStoreVersion(ver int64) (err error) { infos[store.nameToKVKey(sInfo.Name)] = sInfo } - lastCommitID = cInfo.CommitID() + store.lastCommitID = cInfo.CommitID() } for _, key := range store.kvkeysByName { @@ -65,14 +65,17 @@ func (store *Store) LoadMultiStoreVersion(ver int64) (err error) { kvstore.SetPruning(store.pruning) } + + return } func (store *Store) nameToKVKey(name string) types.KVStoreKey { - for key := range kvstores { + for key := range store.kvstores { if key.Name() == name { return key } } + return nil } // ------------------------------- @@ -87,6 +90,20 @@ type storeInfo struct { Core storeCore } +// Implements merkle.Hasher +func (si storeInfo) Hash() []byte { + // Doesn't write Name, since merkle.SimpleHashFromMap() will + // include them via the keys. + bz, _ := cdc.MarshalBinary(si.Core) // Does not error + hasher := tmhash.New() + _, err := hasher.Write(bz) + if err != nil { + // TODO: handle with #870 + panic(err) + } + return hasher.Sum(nil) +} + type storeCore struct { CommitID types.CommitID // ... maybe add more state @@ -107,8 +124,8 @@ type commitInfo struct { // Hash returns the simple merkle root hash of the stores sorted by name. func (ci commitInfo) Hash() []byte { // TODO cache to ci.hash []byte - m := make(map[string]merkle.Hasher, len(ci.StoreInfos)) - for _, storeInfo := range ci.StoreInfos { + m := make(map[string]merkle.Hasher, len(ci.storeInfos)) + for _, storeInfo := range ci.storeInfos { m[storeInfo.Name] = storeInfo } return merkle.SimpleHashFromMap(m) diff --git a/store/iavlmulti/store.go b/store/iavlmulti/store.go index b1734350d3a1..1f20522662fe 100644 --- a/store/iavlmulti/store.go +++ b/store/iavlmulti/store.go @@ -1,5 +1,6 @@ package iavlmulti +/* import ( dbm "github.com/tendermint/tendermint/libs/db" @@ -22,4 +23,4 @@ var _ types.CommitMultiStore = (*Store)(nil) func (store *Store) CacheWrap() types.CacheMultiStore { return cachemulti.NewStore(store.db, store.keysByName, store.iavlstores) -} +}*/ diff --git a/store/list/list_test.go b/store/list/list_test.go index 8a097d6eb28c..92c002879623 100644 --- a/store/list/list_test.go +++ b/store/list/list_test.go @@ -20,10 +20,10 @@ type S struct { B bool } -func defaultComponents(key sdk.StoreKey) (sdk.Context, *codec.Codec) { +func defaultComponents(key sdk.KVStoreKey) (sdk.Context, *codec.Codec) { db := dbm.NewMemDB() cms := rootmulti.NewStore(db) - cms.MountStoreWithDB(key, db) + cms.MountKVStoreWithDB(key, db) cms.LoadLatestVersion() ctx := sdk.NewContext(cms, abci.Header{}, false, log.NewNopLogger()) cdc := codec.New() diff --git a/store/queue/queue_test.go b/store/queue/queue_test.go index 7f8804a87a07..c2bd267d262a 100644 --- a/store/queue/queue_test.go +++ b/store/queue/queue_test.go @@ -22,10 +22,10 @@ type S struct { B bool } -func defaultComponents(key sdk.StoreKey) (sdk.Context, *codec.Codec) { +func defaultComponents(key sdk.KVStoreKey) (sdk.Context, *codec.Codec) { db := dbm.NewMemDB() cms := rootmulti.NewStore(db) - cms.MountStoreWithDB(key, db) + cms.MountKVStoreWithDB(key, db) cms.LoadLatestVersion() ctx := sdk.NewContext(cms, abci.Header{}, false, log.NewNopLogger()) cdc := codec.New() diff --git a/store/rootmulti/store_test.go b/store/rootmulti/store_test.go index 159d00982b6b..79a92863c54f 100644 --- a/store/rootmulti/store_test.go +++ b/store/rootmulti/store_test.go @@ -17,7 +17,7 @@ const useDebugDB = false func TestStoreType(t *testing.T) { db := dbm.NewMemDB() store := NewStore(db) - store.MountStoreWithDB(iavl.NewKey("store1"), db) + store.MountKVStoreWithDB(iavl.NewKey("store1"), db) } func TestStoreMount(t *testing.T) { @@ -28,11 +28,11 @@ func TestStoreMount(t *testing.T) { key2 := iavl.NewKey("store2") dup1 := iavl.NewKey("store1") - require.NotPanics(t, func() { store.MountStoreWithDB(key1, db) }) - require.NotPanics(t, func() { store.MountStoreWithDB(key2, db) }) + require.NotPanics(t, func() { store.MountKVStoreWithDB(key1, db) }) + require.NotPanics(t, func() { store.MountKVStoreWithDB(key2, db) }) - require.Panics(t, func() { store.MountStoreWithDB(key1, db) }) - require.Panics(t, func() { store.MountStoreWithDB(dup1, db) }) + require.Panics(t, func() { store.MountKVStoreWithDB(key1, db) }) + require.Panics(t, func() { store.MountKVStoreWithDB(dup1, db) }) } func TestMultistoreCommitLoad(t *testing.T) { @@ -191,9 +191,9 @@ func TestMultiStoreQuery(t *testing.T) { func newMultiStoreWithMounts(db dbm.DB) *Store { store := NewStore(db) - store.MountStoreWithDB(iavl.NewKey("store1"), nil) - store.MountStoreWithDB(iavl.NewKey("store2"), nil) - store.MountStoreWithDB(iavl.NewKey("store3"), nil) + store.MountKVStoreWithDB(iavl.NewKey("store1"), nil) + store.MountKVStoreWithDB(iavl.NewKey("store2"), nil) + store.MountKVStoreWithDB(iavl.NewKey("store3"), nil) return store } @@ -206,13 +206,13 @@ func checkStore(t *testing.T, store *Store, expect, got types.CommitID) { func getExpectedCommitID(store *Store, ver int64) types.CommitID { return types.CommitID{ Version: ver, - Hash: hashStores(store.stores), + Hash: hashStores(store.kvstores), } } -func hashStores(stores map[types.StoreKey]types.CommitKVStore) []byte { - m := make(map[string]merkle.Hasher, len(stores)) - for key, store := range stores { +func hashStores( /*TODO: multistores*/ kvstores map[types.KVStoreKey]types.CommitKVStore) []byte { + m := make(map[string]merkle.Hasher, len(kvstores)) + for key, store := range kvstores { name := key.Name() m[name] = storeInfo{ Name: name, diff --git a/x/ibc/mapper.go b/x/ibc/mapper.go index 95c88b62fbc1..6998acf0cc17 100644 --- a/x/ibc/mapper.go +++ b/x/ibc/mapper.go @@ -9,14 +9,14 @@ import ( // IBC Mapper type Mapper struct { - key sdk.StoreKey + key sdk.KVStoreKey cdc *codec.Codec codespace sdk.CodespaceType } // XXX: The Mapper should not take a CoinKeeper. Rather have the CoinKeeper // take an Mapper. -func NewMapper(cdc *codec.Codec, key sdk.StoreKey, codespace sdk.CodespaceType) Mapper { +func NewMapper(cdc *codec.Codec, key sdk.KVStoreKey, codespace sdk.CodespaceType) Mapper { // XXX: How are these codecs supposed to work? return Mapper{ key: key, From c45005c0195b1f56a2b1d86b8150c5eb726f79c6 Mon Sep 17 00:00:00 2001 From: mossid Date: Mon, 29 Oct 2018 18:39:22 +0100 Subject: [PATCH 17/22] fix kvstore --- examples/kvstore/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/kvstore/main.go b/examples/kvstore/main.go index 125c7cd47ea1..ce28406b6f1a 100644 --- a/examples/kvstore/main.go +++ b/examples/kvstore/main.go @@ -69,7 +69,7 @@ func main() { } // KVStore Handler -func Handler(storeKey sdk.StoreKey) sdk.Handler { +func Handler(storeKey sdk.KVStoreKey) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { dTx, ok := msg.(kvstoreTx) if !ok { From 28d95ce8e6b5ab0b8f62244aa0c3e64c72926594 Mon Sep 17 00:00:00 2001 From: mossid Date: Mon, 29 Oct 2018 19:15:53 +0100 Subject: [PATCH 18/22] ci now passes --- store/basemulti/store.go | 164 --------------------------------------- store/basemulti/wire.go | 7 -- store/iavlmulti/store.go | 26 ------- types/context_test.go | 4 +- x/auth/mapper_test.go | 6 +- x/bank/keeper_test.go | 4 +- x/ibc/ibc_test.go | 4 +- x/params/keeper_test.go | 6 +- 8 files changed, 12 insertions(+), 209 deletions(-) delete mode 100644 store/basemulti/store.go delete mode 100644 store/basemulti/wire.go delete mode 100644 store/iavlmulti/store.go diff --git a/store/basemulti/store.go b/store/basemulti/store.go deleted file mode 100644 index 43c3aa35eda0..000000000000 --- a/store/basemulti/store.go +++ /dev/null @@ -1,164 +0,0 @@ -package basemulti - -import ( - "fmt" - - "github.com/tendermint/tendermint/crypto/merkle" - "github.com/tendermint/tendermint/crypto/tmhash" - dbm "github.com/tendermint/tendermint/libs/db" - - "github.com/cosmos/cosmos-sdk/store/types" -) - -type Store struct { - db dbm.DB - - kvstores map[types.KVStoreKey]types.CommitKVStore - kvkeysByName map[string]types.KVStoreKey - - pruning types.PruningStrategy -} - -func (store *Store) MountKVStoreWithDB(key types.KVStoreKey, db dbm.DB) { - if key == nil { - panic("MountStoreWithDB() key cannot be nil") - } - if _, ok := store.kvkeysByName[key.Name()]; ok { - panic(fmt.Sprintf("Store duplicate store key %v", key)) - } - - store.kvkeysByName[key.Name()] = key -} - -func (store *Store) GetCommitKVStore(key types.KVStoreKey) types.CommitKVStore { - return store.kvstores[key] -} - -func (store *Store) LoadMultiStoreVersion(ver int64) (err error) { - // Convert StoreInfos slice to map - infos := make(map[types.KVStoreKey]storeInfo) - if ver != 0 { - // Get commitInfo - cInfo, err := getCommitInfo(store.db, ver) - if err != nil { - return err - } - - for _, sInfo := range cInfo.storeInfos { - infos[store.nameToKVKey(sInfo.Name)] = sInfo - } - - store.lastCommitID = cInfo.CommitID() - } - - for _, key := range store.kvkeysByName { - var id types.CommitID - if info, ok := infos[key]; ok { - id = info.Core.CommitID - } - kvstore := key.NewStore() - db := dbm.NewPrefixDB(store.db, []byte("s/k:"+key.Name()+"/")) - err = kvstore.LoadKVStoreVersion(db, id) - if err != nil { - return - } - - kvstore.SetPruning(store.pruning) - } - - return -} - -func (store *Store) nameToKVKey(name string) types.KVStoreKey { - for key := range store.kvstores { - if key.Name() == name { - return key - } - } - return nil -} - -// ------------------------------- -// storeInfo - -// storeInfo contains the name and core reference for an -// underlying store. It is the leaf of the Stores top -// level simple merkle tree - -type storeInfo struct { - Name string - Core storeCore -} - -// Implements merkle.Hasher -func (si storeInfo) Hash() []byte { - // Doesn't write Name, since merkle.SimpleHashFromMap() will - // include them via the keys. - bz, _ := cdc.MarshalBinary(si.Core) // Does not error - hasher := tmhash.New() - _, err := hasher.Write(bz) - if err != nil { - // TODO: handle with #870 - panic(err) - } - return hasher.Sum(nil) -} - -type storeCore struct { - CommitID types.CommitID - // ... maybe add more state -} - -// ------------------------------ -// commitInfo - -// NOTE: keep commitInfo a simple immutable struct. -type commitInfo struct { - // Version - Version int64 - - // types.Store info for - storeInfos []storeInfo -} - -// Hash returns the simple merkle root hash of the stores sorted by name. -func (ci commitInfo) Hash() []byte { - // TODO cache to ci.hash []byte - m := make(map[string]merkle.Hasher, len(ci.storeInfos)) - for _, storeInfo := range ci.storeInfos { - m[storeInfo.Name] = storeInfo - } - return merkle.SimpleHashFromMap(m) -} - -func (ci commitInfo) CommitID() types.CommitID { - return types.CommitID{ - Version: ci.Version, - Hash: ci.Hash(), - } -} - -// ------------------------------- -// Misc. - -func getLatestVestoreion(db dbm.DB) (latest int64) { - latestBytes := db.Get([]byte("s/latest")) - if latestBytes == nil { - return 0 - } - cdc.MustUnmarshalBinary(latestBytes, &latest) - return -} - -func getCommitInfo(db dbm.DB, ver int64) (cInfo commitInfo, err error) { - cInfoBytes := db.Get([]byte(fmt.Sprintf("s/%d", ver))) - if cInfoBytes == nil { - err = fmt.Errorf("failed to get Store: no data") - } - - err = cdc.UnmarshalBinary(cInfoBytes, &cInfo) - if err != nil { - err = fmt.Errorf("failed to get Store: %v", err) - } - return -} diff --git a/store/basemulti/wire.go b/store/basemulti/wire.go deleted file mode 100644 index 123ec0c86375..000000000000 --- a/store/basemulti/wire.go +++ /dev/null @@ -1,7 +0,0 @@ -package basemulti - -import ( - "github.com/cosmos/cosmos-sdk/codec" -) - -var cdc = codec.New() diff --git a/store/iavlmulti/store.go b/store/iavlmulti/store.go deleted file mode 100644 index 1f20522662fe..000000000000 --- a/store/iavlmulti/store.go +++ /dev/null @@ -1,26 +0,0 @@ -package iavlmulti - -/* -import ( - dbm "github.com/tendermint/tendermint/libs/db" - - sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/cosmos/cosmos-sdk/store/cachemulti" - "github.com/cosmos/cosmos-sdk/store/iavl" - "github.com/cosmos/cosmos-sdk/store/types" -) - -// iavlmulti.Store works similar with rootmulti.Store -// but stores -type Store struct { - db dbm.DB - - kvstores map[types.KVStoreKey]iavl.KVStore -} - -var _ types.CommitMultiStore = (*Store)(nil) - -func (store *Store) CacheWrap() types.CacheMultiStore { - return cachemulti.NewStore(store.db, store.keysByName, store.iavlstores) -}*/ diff --git a/types/context_test.go b/types/context_test.go index 4cb8f1105d44..e39cc7bf19fe 100644 --- a/types/context_test.go +++ b/types/context_test.go @@ -52,10 +52,10 @@ func TestContextGetOpShouldNeverPanic(t *testing.T) { } } -func defaultContext(key types.StoreKey) types.Context { +func defaultContext(key types.KVStoreKey) types.Context { db := dbm.NewMemDB() cms := rootmulti.NewStore(db) - cms.MountStoreWithDB(key, db) + cms.MountKVStoreWithDB(key, db) cms.LoadLatestVersion() ctx := types.NewContext(cms, abci.Header{}, false, log.NewNopLogger()) return ctx diff --git a/x/auth/mapper_test.go b/x/auth/mapper_test.go index 682dab2418ad..90ed1bd91797 100644 --- a/x/auth/mapper_test.go +++ b/x/auth/mapper_test.go @@ -14,13 +14,13 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey) { +func setupMultiStore() (sdk.MultiStore, sdk.KVStoreKey, sdk.KVStoreKey) { db := dbm.NewMemDB() capKey := sdk.NewKVStoreKey("capkey") capKey2 := sdk.NewKVStoreKey("capkey2") ms := store.NewCommitMultiStore(db) - ms.MountStoreWithDB(capKey, db) - ms.MountStoreWithDB(capKey2, db) + ms.MountKVStoreWithDB(capKey, db) + ms.MountKVStoreWithDB(capKey2, db) ms.LoadLatestVersion() return ms, capKey, capKey2 } diff --git a/x/bank/keeper_test.go b/x/bank/keeper_test.go index d7bde704f627..5247d723d495 100644 --- a/x/bank/keeper_test.go +++ b/x/bank/keeper_test.go @@ -17,11 +17,11 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" ) -func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey) { +func setupMultiStore() (sdk.MultiStore, sdk.KVStoreKey) { db := dbm.NewMemDB() authKey := sdk.NewKVStoreKey("authkey") ms := store.NewCommitMultiStore(db) - ms.MountStoreWithDB(authKey, db) + ms.MountKVStoreWithDB(authKey, db) ms.LoadLatestVersion() return ms, authKey } diff --git a/x/ibc/ibc_test.go b/x/ibc/ibc_test.go index 12f01e252954..ff9fc1e543a7 100644 --- a/x/ibc/ibc_test.go +++ b/x/ibc/ibc_test.go @@ -19,10 +19,10 @@ import ( // AccountKeeper(/Keeper) and IBCMapper should use different StoreKey later -func defaultContext(key sdk.StoreKey) sdk.Context { +func defaultContext(key sdk.KVStoreKey) sdk.Context { db := dbm.NewMemDB() cms := store.NewCommitMultiStore(db) - cms.MountStoreWithDB(key, db) + cms.MountKVStoreWithDB(key, db) cms.LoadLatestVersion() ctx := sdk.NewContext(cms, abci.Header{}, false, log.NewNopLogger()) return ctx diff --git a/x/params/keeper_test.go b/x/params/keeper_test.go index 3361fc002d71..b785e090ad80 100644 --- a/x/params/keeper_test.go +++ b/x/params/keeper_test.go @@ -16,11 +16,11 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -func defaultContext(key sdk.StoreKey, tkey sdk.StoreKey) sdk.Context { +func defaultContext(key sdk.KVStoreKey, tkey sdk.KVStoreKey) sdk.Context { db := dbm.NewMemDB() cms := store.NewCommitMultiStore(db) - cms.MountStoreWithDB(key, db) - cms.MountStoreWithDB(tkey, db) + cms.MountKVStoreWithDB(key, db) + cms.MountKVStoreWithDB(tkey, db) cms.LoadLatestVersion() ctx := sdk.NewContext(cms, abci.Header{}, false, log.NewNopLogger()) return ctx From 0374f979c8f5f8da44cb88fd9924d373208984bb Mon Sep 17 00:00:00 2001 From: mossid Date: Thu, 1 Nov 2018 01:05:46 +0100 Subject: [PATCH 19/22] concrete key types on NewKeepers --- x/auth/feekeeper.go | 2 +- x/auth/mapper.go | 2 +- x/distribution/keeper/keeper.go | 2 +- x/gov/keeper.go | 2 +- x/ibc/mapper.go | 2 +- x/mint/keeper.go | 2 +- x/params/keeper.go | 2 +- x/slashing/keeper.go | 2 +- x/stake/keeper/keeper.go | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/x/auth/feekeeper.go b/x/auth/feekeeper.go index 242c4161719d..7e4272ac43d9 100644 --- a/x/auth/feekeeper.go +++ b/x/auth/feekeeper.go @@ -20,7 +20,7 @@ type FeeCollectionKeeper struct { cdc *codec.Codec } -func NewFeeCollectionKeeper(cdc *codec.Codec, key sdk.KVStoreKey) FeeCollectionKeeper { +func NewFeeCollectionKeeper(cdc *codec.Codec, key *sdk.IAVLStoreKey) FeeCollectionKeeper { return FeeCollectionKeeper{ key: key, cdc: cdc, diff --git a/x/auth/mapper.go b/x/auth/mapper.go index fb13044dc88d..baf96ee3840f 100644 --- a/x/auth/mapper.go +++ b/x/auth/mapper.go @@ -25,7 +25,7 @@ type AccountKeeper struct { // NewAccountKeeper returns a new sdk.AccountKeeper that // uses go-amino to (binary) encode and decode concrete sdk.Accounts. // nolint -func NewAccountKeeper(cdc *codec.Codec, key sdk.KVStoreKey, proto func() Account) AccountKeeper { +func NewAccountKeeper(cdc *codec.Codec, key *sdk.IAVLStoreKey, proto func() Account) AccountKeeper { return AccountKeeper{ key: key, proto: proto, diff --git a/x/distribution/keeper/keeper.go b/x/distribution/keeper/keeper.go index 05585470b969..a12e08616a07 100644 --- a/x/distribution/keeper/keeper.go +++ b/x/distribution/keeper/keeper.go @@ -20,7 +20,7 @@ type Keeper struct { codespace sdk.CodespaceType } -func NewKeeper(cdc *codec.Codec, key sdk.KVStoreKey, paramSpace params.Subspace, ck types.BankKeeper, +func NewKeeper(cdc *codec.Codec, key *sdk.IAVLStoreKey, paramSpace params.Subspace, ck types.BankKeeper, sk types.StakeKeeper, fck types.FeeCollectionKeeper, codespace sdk.CodespaceType) Keeper { keeper := Keeper{ diff --git a/x/gov/keeper.go b/x/gov/keeper.go index 92d23df7964f..52cc1c6f0ee4 100644 --- a/x/gov/keeper.go +++ b/x/gov/keeper.go @@ -60,7 +60,7 @@ type Keeper struct { // - depositing funds into proposals, and activating upon sufficient funds being deposited // - users voting on proposals, with weight proportional to stake in the system // - and tallying the result of the vote. -func NewKeeper(cdc *codec.Codec, key sdk.KVStoreKey, paramsKeeper params.Keeper, paramSpace params.Subspace, ck bank.Keeper, ds sdk.DelegationSet, codespace sdk.CodespaceType) Keeper { +func NewKeeper(cdc *codec.Codec, key *sdk.IAVLStoreKey, paramsKeeper params.Keeper, paramSpace params.Subspace, ck bank.Keeper, ds sdk.DelegationSet, codespace sdk.CodespaceType) Keeper { return Keeper{ storeKey: key, paramsKeeper: paramsKeeper, diff --git a/x/ibc/mapper.go b/x/ibc/mapper.go index 6998acf0cc17..000362e228a5 100644 --- a/x/ibc/mapper.go +++ b/x/ibc/mapper.go @@ -16,7 +16,7 @@ type Mapper struct { // XXX: The Mapper should not take a CoinKeeper. Rather have the CoinKeeper // take an Mapper. -func NewMapper(cdc *codec.Codec, key sdk.KVStoreKey, codespace sdk.CodespaceType) Mapper { +func NewMapper(cdc *codec.Codec, key *sdk.IAVLStoreKey, codespace sdk.CodespaceType) Mapper { // XXX: How are these codecs supposed to work? return Mapper{ key: key, diff --git a/x/mint/keeper.go b/x/mint/keeper.go index bfef1d64b240..00274a64a976 100644 --- a/x/mint/keeper.go +++ b/x/mint/keeper.go @@ -15,7 +15,7 @@ type Keeper struct { fck FeeCollectionKeeper } -func NewKeeper(cdc *codec.Codec, key sdk.KVStoreKey, +func NewKeeper(cdc *codec.Codec, key *sdk.IAVLStoreKey, paramSpace params.Subspace, sk StakeKeeper, fck FeeCollectionKeeper) Keeper { keeper := Keeper{ diff --git a/x/params/keeper.go b/x/params/keeper.go index da8d4da8a560..7dcd3c7d77af 100644 --- a/x/params/keeper.go +++ b/x/params/keeper.go @@ -17,7 +17,7 @@ type Keeper struct { } // NewKeeper constructs a params keeper -func NewKeeper(cdc *codec.Codec, key sdk.KVStoreKey, tkey *sdk.TransientStoreKey) (k Keeper) { +func NewKeeper(cdc *codec.Codec, key *sdk.IAVLStoreKey, tkey *sdk.TransientStoreKey) (k Keeper) { k = Keeper{ cdc: cdc, key: key, diff --git a/x/slashing/keeper.go b/x/slashing/keeper.go index 5f81cf966110..d9b5e034fe44 100644 --- a/x/slashing/keeper.go +++ b/x/slashing/keeper.go @@ -26,7 +26,7 @@ type Keeper struct { } // NewKeeper creates a slashing keeper -func NewKeeper(cdc *codec.Codec, key sdk.KVStoreKey, vs sdk.ValidatorSet, paramspace params.Subspace, codespace sdk.CodespaceType) Keeper { +func NewKeeper(cdc *codec.Codec, key *sdk.IAVLStoreKey, vs sdk.ValidatorSet, paramspace params.Subspace, codespace sdk.CodespaceType) Keeper { keeper := Keeper{ storeKey: key, cdc: cdc, diff --git a/x/stake/keeper/keeper.go b/x/stake/keeper/keeper.go index 743d332f1f37..480c60055fa3 100644 --- a/x/stake/keeper/keeper.go +++ b/x/stake/keeper/keeper.go @@ -22,7 +22,7 @@ type Keeper struct { codespace sdk.CodespaceType } -func NewKeeper(cdc *codec.Codec, key, tkey sdk.KVStoreKey, ck bank.Keeper, paramstore params.Subspace, codespace sdk.CodespaceType) Keeper { +func NewKeeper(cdc *codec.Codec, key *sdk.IAVLStoreKey, tkey *sdk.TransientStoreKey, ck bank.Keeper, paramstore params.Subspace, codespace sdk.CodespaceType) Keeper { keeper := Keeper{ storeKey: key, storeTKey: tkey, From e1bd6dd3c44294f9cffb51c8f1e3d77368b0560e Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Mon, 29 Oct 2018 21:58:56 -0700 Subject: [PATCH 20/22] fixed lint --- store/queue/queue.go | 1 + store/types/gas.go | 1 + store/utils/utils.go | 1 + 3 files changed, 3 insertions(+) diff --git a/store/queue/queue.go b/store/queue/queue.go index 35617f19c3ae..22ebaea77863 100644 --- a/store/queue/queue.go +++ b/store/queue/queue.go @@ -8,6 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/store/types" ) +// Prefix for the PrefixStore of the queue func ListKey() []byte { return []byte{0x00} } diff --git a/store/types/gas.go b/store/types/gas.go index 5d835772288e..692cfaa40111 100644 --- a/store/types/gas.go +++ b/store/types/gas.go @@ -1,3 +1,4 @@ +// nolint package types // Gas consumption descriptors. diff --git a/store/utils/utils.go b/store/utils/utils.go index 8d869460777a..d1915de81260 100644 --- a/store/utils/utils.go +++ b/store/utils/utils.go @@ -7,5 +7,6 @@ import ( func bz(s string) []byte { return []byte(s) } // Used for tests - formats integer to bytes +// nolint func KeyFmt(i int) []byte { return bz(fmt.Sprintf("key%0.8d", i)) } func ValFmt(i int) []byte { return bz(fmt.Sprintf("value%0.8d", i)) } From 23f6ee79a5c1da6d5b6d8c6d6a68110bdd74b871 Mon Sep 17 00:00:00 2001 From: mossid Date: Thu, 1 Nov 2018 17:34:44 +0100 Subject: [PATCH 21/22] fix syntax error --- cmd/gaia/app/app.go | 18 +++++++++--------- x/mock/app.go | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index 16108a7e9a24..40df7e36cf69 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -43,17 +43,17 @@ type GaiaApp struct { cdc *codec.Codec // keys to access the substores - keyMain sdk.KVStoreKey - keyAccount sdk.KVStoreKey - keyStake sdk.KVStoreKey + keyMain *sdk.IAVLStoreKey + keyAccount *sdk.IAVLStoreKey + keyStake *sdk.IAVLStoreKey tkeyStake *sdk.TransientStoreKey - keySlashing sdk.KVStoreKey - keyMint sdk.KVStoreKey - keyDistr sdk.KVStoreKey + keySlashing *sdk.IAVLStoreKey + keyMint *sdk.IAVLStoreKey + keyDistr *sdk.IAVLStoreKey tkeyDistr *sdk.TransientStoreKey - keyGov sdk.KVStoreKey - keyFeeCollection sdk.KVStoreKey - keyParams sdk.KVStoreKey + keyGov *sdk.IAVLStoreKey + keyFeeCollection *sdk.IAVLStoreKey + keyParams *sdk.IAVLStoreKey tkeyParams *sdk.TransientStoreKey // Manage getting and setting accounts diff --git a/x/mock/app.go b/x/mock/app.go index 782f2a5dc5a8..aac4b9e2304e 100644 --- a/x/mock/app.go +++ b/x/mock/app.go @@ -24,8 +24,8 @@ const chainID = "" type App struct { *bam.BaseApp Cdc *codec.Codec // Cdc is public since the codec is passed into the module anyways - KeyMain sdk.KVStoreKey - KeyAccount sdk.KVStoreKey + KeyMain *sdk.IAVLStoreKey + KeyAccount *sdk.IAVLStoreKey // TODO: Abstract this out from not needing to be auth specifically AccountKeeper auth.AccountKeeper From 8da214ebf54f580b0e1ffca34f21d8d50c44b076 Mon Sep 17 00:00:00 2001 From: mossid Date: Thu, 1 Nov 2018 17:40:38 +0100 Subject: [PATCH 22/22] fix syntax error --- cmd/gaia/cmd/gaiadebug/hack.go | 10 +++++----- examples/basecoin/app/app.go | 6 +++--- examples/democoin/app/app.go | 10 +++++----- examples/democoin/x/cool/keeper_test.go | 2 +- examples/democoin/x/pow/keeper_test.go | 2 +- examples/democoin/x/simplestake/keeper_test.go | 2 +- x/auth/mapper_test.go | 2 +- x/bank/keeper_test.go | 2 +- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cmd/gaia/cmd/gaiadebug/hack.go b/cmd/gaia/cmd/gaiadebug/hack.go index c0b232e8449c..e83e6912fbfd 100644 --- a/cmd/gaia/cmd/gaiadebug/hack.go +++ b/cmd/gaia/cmd/gaiadebug/hack.go @@ -129,12 +129,12 @@ type GaiaApp struct { cdc *codec.Codec // keys to access the substores - keyMain sdk.KVStoreKey - keyAccount sdk.KVStoreKey - keyStake sdk.KVStoreKey + keyMain *sdk.IAVLStoreKey + keyAccount *sdk.IAVLStoreKey + keyStake *sdk.IAVLStoreKey tkeyStake *sdk.TransientStoreKey - keySlashing sdk.KVStoreKey - keyParams sdk.KVStoreKey + keySlashing *sdk.IAVLStoreKey + keyParams *sdk.IAVLStoreKey tkeyParams *sdk.TransientStoreKey // Manage getting and setting accounts diff --git a/examples/basecoin/app/app.go b/examples/basecoin/app/app.go index 26754e9c0012..8c769fcdb337 100644 --- a/examples/basecoin/app/app.go +++ b/examples/basecoin/app/app.go @@ -37,9 +37,9 @@ type BasecoinApp struct { cdc *codec.Codec // keys to access the multistore - keyMain sdk.KVStoreKey - keyAccount sdk.KVStoreKey - keyIBC sdk.KVStoreKey + keyMain *sdk.IAVLStoreKey + keyAccount *sdk.IAVLStoreKey + keyIBC *sdk.IAVLStoreKey // manage getting and setting accounts accountKeeper auth.AccountKeeper diff --git a/examples/democoin/app/app.go b/examples/democoin/app/app.go index 227dd71981b6..d52dca295342 100644 --- a/examples/democoin/app/app.go +++ b/examples/democoin/app/app.go @@ -40,11 +40,11 @@ type DemocoinApp struct { cdc *codec.Codec // keys to access the substores - capKeyMainStore sdk.KVStoreKey - capKeyAccountStore sdk.KVStoreKey - capKeyPowStore sdk.KVStoreKey - capKeyIBCStore sdk.KVStoreKey - capKeyStakingStore sdk.KVStoreKey + capKeyMainStore *sdk.IAVLStoreKey + capKeyAccountStore *sdk.IAVLStoreKey + capKeyPowStore *sdk.IAVLStoreKey + capKeyIBCStore *sdk.IAVLStoreKey + capKeyStakingStore *sdk.IAVLStoreKey // keepers feeCollectionKeeper auth.FeeCollectionKeeper diff --git a/examples/democoin/x/cool/keeper_test.go b/examples/democoin/x/cool/keeper_test.go index 1056e93f5bca..843a14580638 100644 --- a/examples/democoin/x/cool/keeper_test.go +++ b/examples/democoin/x/cool/keeper_test.go @@ -15,7 +15,7 @@ import ( bank "github.com/cosmos/cosmos-sdk/x/bank" ) -func setupMultiStore() (sdk.MultiStore, sdk.KVStoreKey) { +func setupMultiStore() (sdk.MultiStore, *sdk.IAVLStoreKey) { db := dbm.NewMemDB() capKey := sdk.NewKVStoreKey("capkey") ms := store.NewCommitMultiStore(db) diff --git a/examples/democoin/x/pow/keeper_test.go b/examples/democoin/x/pow/keeper_test.go index ef322ac70ee9..c8db83d30c72 100644 --- a/examples/democoin/x/pow/keeper_test.go +++ b/examples/democoin/x/pow/keeper_test.go @@ -17,7 +17,7 @@ import ( ) // possibly share this kind of setup functionality between module testsuites? -func setupMultiStore() (sdk.MultiStore, sdk.KVStoreKey) { +func setupMultiStore() (sdk.MultiStore, *sdk.IAVLStoreKey) { db := dbm.NewMemDB() capKey := sdk.NewKVStoreKey("capkey") ms := store.NewCommitMultiStore(db) diff --git a/examples/democoin/x/simplestake/keeper_test.go b/examples/democoin/x/simplestake/keeper_test.go index 54c5e7b41e3c..972789a2a767 100644 --- a/examples/democoin/x/simplestake/keeper_test.go +++ b/examples/democoin/x/simplestake/keeper_test.go @@ -19,7 +19,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/bank" ) -func setupMultiStore() (sdk.MultiStore, sdk.KVStoreKey, sdk.KVStoreKey) { +func setupMultiStore() (sdk.MultiStore, *sdk.IAVLStoreKey, *sdk.IAVLStoreKey) { db := dbm.NewMemDB() authKey := sdk.NewKVStoreKey("authkey") capKey := sdk.NewKVStoreKey("capkey") diff --git a/x/auth/mapper_test.go b/x/auth/mapper_test.go index 90ed1bd91797..c0fcd4f560ea 100644 --- a/x/auth/mapper_test.go +++ b/x/auth/mapper_test.go @@ -14,7 +14,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -func setupMultiStore() (sdk.MultiStore, sdk.KVStoreKey, sdk.KVStoreKey) { +func setupMultiStore() (sdk.MultiStore, *sdk.IAVLStoreKey, *sdk.IAVLStoreKey) { db := dbm.NewMemDB() capKey := sdk.NewKVStoreKey("capkey") capKey2 := sdk.NewKVStoreKey("capkey2") diff --git a/x/bank/keeper_test.go b/x/bank/keeper_test.go index 5247d723d495..3e55b57445bf 100644 --- a/x/bank/keeper_test.go +++ b/x/bank/keeper_test.go @@ -17,7 +17,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" ) -func setupMultiStore() (sdk.MultiStore, sdk.KVStoreKey) { +func setupMultiStore() (sdk.MultiStore, *sdk.IAVLStoreKey) { db := dbm.NewMemDB() authKey := sdk.NewKVStoreKey("authkey") ms := store.NewCommitMultiStore(db)