Skip to content

Commit

Permalink
refactor(core): remove cosmos-db dependency (#18848)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Dec 21, 2023
1 parent f9f9211 commit 6649bb7
Show file tree
Hide file tree
Showing 16 changed files with 215 additions and 175 deletions.
16 changes: 0 additions & 16 deletions core/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
cosmossdk.io/api v0.7.2
cosmossdk.io/depinject v1.0.0-alpha.4
cosmossdk.io/math v1.2.0
github.com/cosmos/cosmos-db v1.0.0
github.com/cosmos/cosmos-proto v1.0.0-beta.3
github.com/stretchr/testify v1.8.4
google.golang.org/grpc v1.60.1
Expand All @@ -16,35 +15,20 @@ require (
)

require (
github.com/DataDog/zstd v1.5.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/errors v1.11.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/pebble v0.0.0-20230525220056-bb4fc9527b3b // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cosmos/gogoproto v1.4.11 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/getsentry/sentry-go v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/linxGnu/grocksdb v1.8.10 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
Expand Down
114 changes: 0 additions & 114 deletions core/go.sum

Large diffs are not rendered by default.

54 changes: 50 additions & 4 deletions core/store/store.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package store

import dbm "github.com/cosmos/cosmos-db"

// KVStore describes the basic interface for interacting with key-value stores.
type KVStore interface {
// Get returns nil iff key doesn't exist. Errors on nil key.
Expand Down Expand Up @@ -32,5 +30,53 @@ type KVStore interface {
ReverseIterator(start, end []byte) (Iterator, error)
}

// Iterator is an alias db's Iterator for convenience.
type Iterator = dbm.Iterator
// Iterator represents an iterator over a domain of keys. Callers must call Close when done.
// No writes can happen to a domain while there exists an iterator over it, some backends may take
// out database locks to ensure this will not happen.
//
// Callers must make sure the iterator is valid before calling any methods on it, otherwise
// these methods will panic. This is in part caused by most backend databases using this convention.
//
// As with DB, keys and values should be considered read-only, and must be copied before they are
// modified.
//
// Typical usage:
//
// var itr Iterator = ...
// defer itr.Close()
//
// for ; itr.Valid(); itr.Next() {
// k, v := itr.Key(); itr.Value()
// ...
// }
//
// if err := itr.Error(); err != nil {
// ...
// }
type Iterator interface {
// Domain returns the start (inclusive) and end (exclusive) limits of the iterator.
// CONTRACT: start, end readonly []byte
Domain() (start, end []byte)

// Valid returns whether the current iterator is valid. Once invalid, the Iterator remains
// invalid forever.
Valid() bool

// Next moves the iterator to the next key in the database, as defined by order of iteration.
// If Valid returns false, this method will panic.
Next()

// Key returns the key at the current position. Panics if the iterator is invalid.
// CONTRACT: key readonly []byte
Key() (key []byte)

// Value returns the value at the current position. Panics if the iterator is invalid.
// CONTRACT: value readonly []byte
Value() (value []byte)

// Error returns the last error encountered by the iterator, if any.
Error() error

// Close closes the iterator, relasing any allocated resources.
Close() error
}
4 changes: 2 additions & 2 deletions orm/internal/testkv/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"gotest.tools/v3/assert"

"cosmossdk.io/core/store"
"cosmossdk.io/orm/model/ormtable"
"cosmossdk.io/orm/types/kv"
)

func AssertBackendsEqual(t assert.TestingT, b1, b2 ormtable.Backend) {
Expand All @@ -27,7 +27,7 @@ func AssertBackendsEqual(t assert.TestingT, b1, b2 ormtable.Backend) {
AssertIteratorsEqual(t, it1, it2)
}

func AssertIteratorsEqual(t assert.TestingT, it1, it2 kv.Iterator) {
func AssertIteratorsEqual(t assert.TestingT, it1, it2 store.Iterator) {
for it1.Valid() {
assert.Assert(t, it2.Valid())
assert.Assert(t, bytes.Equal(it1.Key(), it2.Key()))
Expand Down
122 changes: 122 additions & 0 deletions orm/internal/testkv/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package testkv

import (
dbm "github.com/cosmos/cosmos-db"

"cosmossdk.io/core/store"
)

type TestStore struct {
Db dbm.DB
}

func (ts TestStore) Get(bz []byte) ([]byte, error) {
return ts.Db.Get(bz)
}

// // Has checks if a key exists.
func (ts TestStore) Has(key []byte) (bool, error) {
return ts.Db.Has(key)
}

func (ts TestStore) Set(k, v []byte) error {
return ts.Db.Set(k, v)
}

// // SetSync sets the value for the given key, and flushes it to storage before returning.
func (ts TestStore) SetSync(k, v []byte) error {
return ts.Db.SetSync(k, v)
}

// // Delete deletes the key, or does nothing if the key does not exist.
// // CONTRACT: key readonly []byte
func (ts TestStore) Delete(bz []byte) error {
return ts.Db.Delete(bz)
}

// // DeleteSync deletes the key, and flushes the delete to storage before returning.
func (ts TestStore) DeleteSync(bz []byte) error {
return ts.Db.DeleteSync(bz)
}

func (ts TestStore) Iterator(start, end []byte) (store.Iterator, error) {
itr, err := ts.Db.Iterator(start, end)
return IteratorWrapper{itr: itr}, err
}

func (ts TestStore) ReverseIterator(start, end []byte) (store.Iterator, error) {
itr, err := ts.Db.ReverseIterator(start, end)
return itr, err
}

// Close closes the database connection.
func (ts TestStore) Close() error {
return ts.Db.Close()
}

// NewBatch creates a batch for atomic updates. The caller must call Batch.Close.
func (ts TestStore) NewBatch() dbm.Batch {
return ts.Db.NewBatch()
}

// NewBatchWithSize create a new batch for atomic updates, but with pre-allocated size.
// This will does the same thing as NewBatch if the batch implementation doesn't support pre-allocation.
func (ts TestStore) NewBatchWithSize(i int) dbm.Batch {
return ts.Db.NewBatchWithSize(i)
}

// Print is used for debugging.
func (ts TestStore) Print() error {
return ts.Db.Print()
}

// Stats returns a map of property values for all keys and the size of the cache.
func (ts TestStore) Stats() map[string]string {
return ts.Db.Stats()
}

var _ store.Iterator = IteratorWrapper{}

type IteratorWrapper struct {
itr dbm.Iterator
}

// Domain returns the start (inclusive) and end (exclusive) limits of the iterator.
// CONTRACT: start, end readonly []byte
func (iw IteratorWrapper) Domain() (start, end []byte) {
return iw.itr.Domain()
}

// Valid returns whether the current iterator is valid. Once invalid, the Iterator remains
// invalid forever.
func (iw IteratorWrapper) Valid() bool {
return iw.itr.Valid()
}

// Next moves the iterator to the next key in the database, as defined by order of iteration.
// If Valid returns false, this method will panic.
func (iw IteratorWrapper) Next() {
iw.itr.Next()
}

// Key returns the key at the current position. Panics if the iterator is invalid.
// CONTRACT: key readonly []byte
func (iw IteratorWrapper) Key() (key []byte) {
return iw.itr.Key()
}

// Value returns the value at the current position. Panics if the iterator is invalid.
// CONTRACT: value readonly []byte
func (iw IteratorWrapper) Value() (value []byte) {
return iw.itr.Value()
}

// Error returns the last error encountered by the iterator, if any.
func (iw IteratorWrapper) Error() error {
return iw.itr.Error()
}

// Close closes the iterator, relasing any allocated resources.
func (iw IteratorWrapper) Close() error {
return iw.itr.Close()
}
9 changes: 5 additions & 4 deletions orm/internal/testkv/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"google.golang.org/protobuf/proto"

"cosmossdk.io/core/store"
"cosmossdk.io/orm/encoding/ormkv"
"cosmossdk.io/orm/internal/stablejson"
"cosmossdk.io/orm/model/ormtable"
Expand Down Expand Up @@ -77,7 +78,7 @@ func (t debugStore) Has(key []byte) (bool, error) {
return has, nil
}

func (t debugStore) Iterator(start, end []byte) (kv.Iterator, error) {
func (t debugStore) Iterator(start, end []byte) (store.Iterator, error) {
if t.debugger != nil {
t.debugger.Log(fmt.Sprintf("ITERATOR %x -> %x", start, end))
}
Expand All @@ -92,7 +93,7 @@ func (t debugStore) Iterator(start, end []byte) (kv.Iterator, error) {
}, nil
}

func (t debugStore) ReverseIterator(start, end []byte) (kv.Iterator, error) {
func (t debugStore) ReverseIterator(start, end []byte) (store.Iterator, error) {
if t.debugger != nil {
t.debugger.Log(fmt.Sprintf("ITERATOR %x <- %x", start, end))
}
Expand Down Expand Up @@ -140,7 +141,7 @@ func (t debugStore) Delete(key []byte) error {
var _ kv.Store = &debugStore{}

type debugIterator struct {
iterator kv.Iterator
iterator store.Iterator
storeName string
debugger Debugger
}
Expand Down Expand Up @@ -185,7 +186,7 @@ func (d debugIterator) Close() error {
return d.iterator.Close()
}

var _ kv.Iterator = &debugIterator{}
var _ store.Iterator = &debugIterator{}

// EntryCodecDebugger is a Debugger instance that uses an EntryCodec and Print
// function for debugging.
Expand Down
2 changes: 1 addition & 1 deletion orm/internal/testkv/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ func NewGoLevelDBBackend(tb testing.TB) ormtable.Backend {
db, err := dbm.NewGoLevelDB("test", tb.TempDir(), nil)
assert.NilError(tb, err)
return ormtable.NewBackend(ormtable.BackendOptions{
CommitmentStore: db,
CommitmentStore: TestStore{Db: db},
})
}
6 changes: 3 additions & 3 deletions orm/internal/testkv/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
// are really two separate backing stores.
func NewSplitMemBackend() ormtable.Backend {
return ormtable.NewBackend(ormtable.BackendOptions{
CommitmentStore: dbm.NewMemDB(),
IndexStore: dbm.NewMemDB(),
CommitmentStore: TestStore{dbm.NewMemDB()},
IndexStore: TestStore{dbm.NewMemDB()},
})
}

Expand All @@ -21,7 +21,7 @@ func NewSplitMemBackend() ormtable.Backend {
// where only a single KV-store is available to modules.
func NewSharedMemBackend() ormtable.Backend {
return ormtable.NewBackend(ormtable.BackendOptions{
CommitmentStore: dbm.NewMemDB(),
CommitmentStore: TestStore{dbm.NewMemDB()},
// commit store is automatically used as the index store
})
}
4 changes: 2 additions & 2 deletions orm/model/ormdb/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,11 @@ type testStoreService struct {
}

func (t testStoreService) OpenKVStore(context.Context) store.KVStore {
return t.db
return testkv.TestStore{Db: t.db}
}

func (t testStoreService) OpenMemoryStore(context.Context) store.KVStore {
return t.db
return testkv.TestStore{Db: t.db}
}

func TestGetBackendResolver(t *testing.T) {
Expand Down
17 changes: 9 additions & 8 deletions orm/model/ormtable/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"cosmossdk.io/core/store"
"cosmossdk.io/orm/types/kv"
)

Expand All @@ -25,11 +26,11 @@ type Backend interface {
ReadBackend

// CommitmentStore returns the merklized commitment store.
CommitmentStore() kv.Store
CommitmentStore() store.KVStore

// IndexStore returns the index store if a separate one exists,
// otherwise it the commitment store.
IndexStore() kv.Store
IndexStore() store.KVStore

// ValidateHooks returns a ValidateHooks instance or nil.
ValidateHooks() ValidateHooks
Expand Down Expand Up @@ -86,8 +87,8 @@ func NewReadBackend(options ReadBackendOptions) ReadBackend {
}

type backend struct {
commitmentStore kv.Store
indexStore kv.Store
commitmentStore store.KVStore
indexStore store.KVStore
validateHooks ValidateHooks
writeHooks WriteHooks
}
Expand Down Expand Up @@ -120,11 +121,11 @@ func (c backend) IndexStoreReader() kv.ReadonlyStore {
return c.indexStore
}

func (c backend) CommitmentStore() kv.Store {
func (c backend) CommitmentStore() store.KVStore {
return c.commitmentStore
}

func (c backend) IndexStore() kv.Store {
func (c backend) IndexStore() store.KVStore {
return c.indexStore
}

Expand All @@ -135,11 +136,11 @@ func (c backend) IndexStore() kv.Store {
// used for all operations.
type BackendOptions struct {
// CommitmentStore is the commitment store.
CommitmentStore kv.Store
CommitmentStore store.KVStore

// IndexStore is the optional index store.
// If it is nil the CommitmentStore will be used.
IndexStore kv.Store
IndexStore store.KVStore

// ValidateHooks are optional hooks into ORM insert, update and delete operations.
ValidateHooks ValidateHooks
Expand Down
Loading

0 comments on commit 6649bb7

Please sign in to comment.