Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Jan 17, 2025
1 parent feffc02 commit 1123fbe
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 137 deletions.
35 changes: 35 additions & 0 deletions core/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,38 @@ type GIterator[V any] interface {
// Close closes the iterator, relasing any allocated resources.
Close() error
}

// GBasicKVStore is a simple interface to get/set data
type GBasicKVStore[V any] interface {
// Get returns nil if key doesn't exist. Panics on nil key.
Get(key []byte) V

// Has checks if a key exists. Panics on nil key.
Has(key []byte) bool

// Set sets the key. Panics on nil key or value.
Set(key []byte, value V)

// Delete deletes the key. Panics on nil key.
Delete(key []byte)
}

// GKVStore additionally provides iteration and deletion
type GKVStore[V any] interface {
GBasicKVStore[V]

// 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.
// Exceptionally allowed for cachekv.Store, safe to write in the modules.
Iterator(start, end []byte) GIterator[V]

// Iterator over a domain of keys in descending order. End is exclusive.
// Start must be less 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.
// Exceptionally allowed for cachekv.Store, safe to write in the modules.
ReverseIterator(start, end []byte) GIterator[V]
}
1 change: 1 addition & 0 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func ProvideObjKVStoreService(config *runtimev1alpha1.Module, key depinject.Modu
storeKey := ProvideObjectStoreKey(key, app)
return objectStoreService{key: storeKey}
}

func ProvideEventService() event.Service {
return EventService{}
}
Expand Down
7 changes: 4 additions & 3 deletions store/cachekv/internal/mergeiterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"

corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/types"
)

Expand All @@ -15,8 +16,8 @@ import (
//
// TODO: Optimize by memoizing.
type cacheMergeIterator[V any] struct {
parent types.GIterator[V]
cache types.GIterator[V]
parent corestore.GIterator[V]
cache corestore.GIterator[V]
ascending bool

valid bool
Expand All @@ -26,7 +27,7 @@ type cacheMergeIterator[V any] struct {

var _ types.Iterator = (*cacheMergeIterator[[]byte])(nil)

func NewCacheMergeIterator[V any](parent, cache types.GIterator[V], ascending bool, isZero func(V) bool) types.GIterator[V] {
func NewCacheMergeIterator[V any](parent, cache corestore.GIterator[V], ascending bool, isZero func(V) bool) corestore.GIterator[V] {
iter := &cacheMergeIterator[V]{
parent: parent,
cache: cache,
Expand Down
87 changes: 44 additions & 43 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cachekv
import (
"io"

corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/cachekv/internal"
"cosmossdk.io/store/internal/btree"
"cosmossdk.io/store/types"
Expand Down Expand Up @@ -48,124 +49,124 @@ func NewGStore[V any](parent types.GKVStore[V], isZero func(V) bool, valueLen fu
}

// GetStoreType implements Store.
func (store *GStore[V]) GetStoreType() types.StoreType {
return store.parent.GetStoreType()
func (s *GStore[V]) GetStoreType() types.StoreType {
return s.parent.GetStoreType()
}

// Clone creates a copy-on-write snapshot of the cache store,
// it only performs a shallow copy so is very fast.
func (store *GStore[V]) Clone() types.BranchStore {
v := *store
v.writeSet = store.writeSet.Copy()
func (s *GStore[V]) Clone() types.BranchStore {
v := *s
v.writeSet = s.writeSet.Copy()
return &v
}

// swapCache swap out the internal cache store and leave the current store unusable.
func (store *GStore[V]) swapCache() btree.BTree[V] {
cache := store.writeSet
store.writeSet = btree.BTree[V]{}
func (s *GStore[V]) swapCache() btree.BTree[V] {
cache := s.writeSet
s.writeSet = btree.BTree[V]{}
return cache
}

// Restore restores the store cache to a given snapshot, leaving the snapshot unusable.
func (store *GStore[V]) Restore(s types.BranchStore) {
store.writeSet = s.(*GStore[V]).swapCache()
func (s *GStore[V]) Restore(store types.BranchStore) {
s.writeSet = store.(*GStore[V]).swapCache()
}

// Get implements types.KVStore.
func (store *GStore[V]) Get(key []byte) V {
func (s *GStore[V]) Get(key []byte) V {
types.AssertValidKey(key)

value, found := store.writeSet.Get(key)
value, found := s.writeSet.Get(key)
if !found {
return store.parent.Get(key)
return s.parent.Get(key)
}
return value
}

// Set implements types.KVStore.
func (store *GStore[V]) Set(key []byte, value V) {
func (s *GStore[V]) Set(key []byte, value V) {
types.AssertValidKey(key)
types.AssertValidValueGeneric(value, store.isZero, store.valueLen)
types.AssertValidValueGeneric(value, s.isZero, s.valueLen)

store.writeSet.Set(key, value)
s.writeSet.Set(key, value)
}

// Has implements types.KVStore.
func (store *GStore[V]) Has(key []byte) bool {
func (s *GStore[V]) Has(key []byte) bool {
types.AssertValidKey(key)

value, found := store.writeSet.Get(key)
value, found := s.writeSet.Get(key)
if !found {
return store.parent.Has(key)
return s.parent.Has(key)
}
return !store.isZero(value)
return !s.isZero(value)
}

// Delete implements types.KVStore.
func (store *GStore[V]) Delete(key []byte) {
func (s *GStore[V]) Delete(key []byte) {
types.AssertValidKey(key)
store.writeSet.Set(key, store.zeroValue)
s.writeSet.Set(key, s.zeroValue)
}

// Implements Cachetypes.KVStore.
func (store *GStore[V]) Write() {
store.writeSet.Scan(func(key []byte, value V) bool {
if store.isZero(value) {
store.parent.Delete(key)
func (s *GStore[V]) Write() {
s.writeSet.Scan(func(key []byte, value V) bool {
if s.isZero(value) {
s.parent.Delete(key)
} else {
store.parent.Set(key, value)
s.parent.Set(key, value)
}
return true
})
store.writeSet.Clear()
s.writeSet.Clear()
}

func (store *GStore[V]) Discard() {
store.writeSet.Clear()
func (s *GStore[V]) Discard() {
s.writeSet.Clear()
}

// CacheWrap implements CacheWrapper.
func (store *GStore[V]) CacheWrap() types.CacheWrap {
return NewGStore(store, store.isZero, store.valueLen)
func (s *GStore[V]) CacheWrap() types.CacheWrap {
return NewGStore(s, s.isZero, s.valueLen)
}

// CacheWrapWithTrace implements the CacheWrapper interface.
func (store *GStore[V]) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.CacheWrap {
func (s *GStore[V]) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.CacheWrap {
panic("cannot CacheWrapWithTrace a cachekv Store")
}

//----------------------------------------
// Iteration

// Iterator implements types.KVStore.
func (store *GStore[V]) Iterator(start, end []byte) types.GIterator[V] {
return store.iterator(start, end, true)
func (s *GStore[V]) Iterator(start, end []byte) corestore.GIterator[V] {
return s.iterator(start, end, true)
}

// ReverseIterator implements types.KVStore.
func (store *GStore[V]) ReverseIterator(start, end []byte) types.GIterator[V] {
return store.iterator(start, end, false)
func (s *GStore[V]) ReverseIterator(start, end []byte) corestore.GIterator[V] {
return s.iterator(start, end, false)
}

func (store *GStore[V]) iterator(start, end []byte, ascending bool) types.GIterator[V] {
isoSortedCache := store.writeSet.Copy()
func (s *GStore[V]) iterator(start, end []byte, ascending bool) corestore.GIterator[V] {
isoSortedCache := s.writeSet.Copy()

var (
err error
parent, cache types.GIterator[V]
parent, cache corestore.GIterator[V]
)

if ascending {
parent = store.parent.Iterator(start, end)
parent = s.parent.Iterator(start, end)
cache, err = isoSortedCache.Iterator(start, end)
} else {
parent = store.parent.ReverseIterator(start, end)
parent = s.parent.ReverseIterator(start, end)
cache, err = isoSortedCache.ReverseIterator(start, end)
}
if err != nil {
panic(err)
}

return internal.NewCacheMergeIterator(parent, cache, ascending, store.isZero)
return internal.NewCacheMergeIterator(parent, cache, ascending, s.isZero)
}
13 changes: 7 additions & 6 deletions store/gaskv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gaskv
import (
"io"

corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/types"
)

Expand Down Expand Up @@ -102,15 +103,15 @@ func (gs *GStore[V]) Delete(key []byte) {
// 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 *GStore[V]) Iterator(start, end []byte) types.GIterator[V] {
func (gs *GStore[V]) Iterator(start, end []byte) corestore.GIterator[V] {
return gs.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 *GStore[V]) ReverseIterator(start, end []byte) types.GIterator[V] {
func (gs *GStore[V]) ReverseIterator(start, end []byte) corestore.GIterator[V] {
return gs.iterator(start, end, false)
}

Expand All @@ -124,8 +125,8 @@ func (gs *GStore[V]) CacheWrapWithTrace(_ io.Writer, _ types.TraceContext) types
panic("cannot CacheWrapWithTrace a GasKVStore")
}

func (gs *GStore[V]) iterator(start, end []byte, ascending bool) types.GIterator[V] {
var parent types.GIterator[V]
func (gs *GStore[V]) iterator(start, end []byte, ascending bool) corestore.GIterator[V] {
var parent corestore.GIterator[V]
if ascending {
parent = gs.parent.Iterator(start, end)
} else {
Expand All @@ -141,11 +142,11 @@ func (gs *GStore[V]) iterator(start, end []byte, ascending bool) types.GIterator
type gasIterator[V any] struct {
gasMeter types.GasMeter
gasConfig types.GasConfig
parent types.GIterator[V]
parent corestore.GIterator[V]
valueLen func(V) int
}

func newGasIterator[V any](gasMeter types.GasMeter, gasConfig types.GasConfig, parent types.GIterator[V], valueLen func(V) int) *gasIterator[V] {
func newGasIterator[V any](gasMeter types.GasMeter, gasConfig types.GasConfig, parent corestore.GIterator[V], valueLen func(V) int) *gasIterator[V] {
return &gasIterator[V]{
gasMeter: gasMeter,
gasConfig: gasConfig,
Expand Down
7 changes: 5 additions & 2 deletions store/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module cosmossdk.io/store
go 1.21

require (
cosmossdk.io/core v0.11.1
cosmossdk.io/errors v1.0.0
cosmossdk.io/log v1.4.1
cosmossdk.io/math v1.3.0
Expand All @@ -21,7 +22,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/tidwall/btree v1.7.0
golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0
google.golang.org/grpc v1.62.1
google.golang.org/grpc v1.64.1
google.golang.org/protobuf v1.34.2
gotest.tools/v3 v3.5.1
)
Expand Down Expand Up @@ -75,6 +76,8 @@ require (
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240709173604-40e1e62336c5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace cosmossdk.io/core => ../core
8 changes: 4 additions & 4 deletions store/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,10 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c h1:lfpJ/2rWPa/kJgxyyXM8PrNnfCzcmxJ265mADgwmvLI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk=
google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240709173604-40e1e62336c5 h1:SbSDUWW1PAO24TNpLdeheoYPd7kllICcLU52x6eD4kQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240709173604-40e1e62336c5/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA=
google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand Down
6 changes: 3 additions & 3 deletions store/internal/btree/btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/tidwall/btree"

"cosmossdk.io/store/types"
corestore "cosmossdk.io/core/store"
)

const (
Expand Down Expand Up @@ -63,14 +63,14 @@ func (bt *BTree[V]) Delete(key []byte) {
bt.tree.Delete(newItemWithKey[V](key))
}

func (bt BTree[V]) Iterator(start, end []byte) (types.GIterator[V], error) {
func (bt BTree[V]) Iterator(start, end []byte) (corestore.GIterator[V], error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
return newMemIterator(start, end, bt, true), nil
}

func (bt BTree[V]) ReverseIterator(start, end []byte) (types.GIterator[V], error) {
func (bt BTree[V]) ReverseIterator(start, end []byte) (corestore.GIterator[V], error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
Expand Down
5 changes: 3 additions & 2 deletions store/internal/btreeadaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"io"

corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/cachekv"
"cosmossdk.io/store/internal/btree"
"cosmossdk.io/store/types"
Expand Down Expand Up @@ -36,15 +37,15 @@ func (ts *BTreeStore[V]) Has(key []byte) bool {
return found
}

func (ts *BTreeStore[V]) Iterator(start, end []byte) types.GIterator[V] {
func (ts *BTreeStore[V]) Iterator(start, end []byte) corestore.GIterator[V] {
it, err := ts.BTree.Iterator(start, end)
if err != nil {
panic(err)
}
return it
}

func (ts *BTreeStore[V]) ReverseIterator(start, end []byte) types.GIterator[V] {
func (ts *BTreeStore[V]) ReverseIterator(start, end []byte) corestore.GIterator[V] {
it, err := ts.BTree.ReverseIterator(start, end)
if err != nil {
panic(err)
Expand Down
Loading

0 comments on commit 1123fbe

Please sign in to comment.