Skip to content

Commit

Permalink
Refactor for new DB interface
Browse files Browse the repository at this point in the history
- Refactor store/ and types/ packages
- leaves out IAVL and multistore code pending confirmed deprecation
- some hacks to glue existing tmdb usage and get compile/tests working
  • Loading branch information
roysc committed Jun 23, 2021
1 parent 3ef24fb commit 924f265
Show file tree
Hide file tree
Showing 26 changed files with 86 additions and 75 deletions.
2 changes: 1 addition & 1 deletion store/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@ type Store struct {
}
```

`Store.Store` is a `dbadapter.Store` with a `dbm.NewMemDB()`. All `KVStore` methods are reused. When `Store.Commit()` is called, new `dbadapter.Store` is assigned, discarding previous reference and making it garbage collected.
`Store.Store` is a `dbadapter.Store` with an in-memory backing `DB`. All `KVStore` methods are reused. When `Store.Commit()` is called, new `dbadapter.Store` is assigned, discarding previous reference and making it garbage collected.
2 changes: 1 addition & 1 deletion store/cachekv/memiterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cachekv
import (
"errors"

dbm "github.com/tendermint/tm-db"
dbm "github.com/cosmos/cosmos-sdk/db"

"github.com/cosmos/cosmos-sdk/types/kv"
)
Expand Down
2 changes: 1 addition & 1 deletion store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"
"time"

dbm "github.com/tendermint/tm-db"
dbm "github.com/cosmos/cosmos-sdk/db"

"github.com/cosmos/cosmos-sdk/internal/conv"
"github.com/cosmos/cosmos-sdk/store/listenkv"
Expand Down
4 changes: 2 additions & 2 deletions store/cachekv/store_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"sort"
"testing"

dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/db/memdb"

"github.com/cosmos/cosmos-sdk/store/cachekv"
"github.com/cosmos/cosmos-sdk/store/dbadapter"
)

func benchmarkCacheKVStoreIterator(numKVs int, b *testing.B) {
b.ReportAllocs()
mem := dbadapter.Store{DB: dbm.NewMemDB()}
mem := dbadapter.Store{DB: memdb.NewDB()}
cstore := cachekv.NewStore(mem)
keys := make([]string, numKVs)

Expand Down
31 changes: 16 additions & 15 deletions store/cachekv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@ import (
"fmt"
"testing"

dbm "github.com/cosmos/cosmos-sdk/db"
"github.com/cosmos/cosmos-sdk/db/memdb"
"github.com/stretchr/testify/require"
tmrand "github.com/tendermint/tendermint/libs/rand"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/store/cachekv"
"github.com/cosmos/cosmos-sdk/store/dbadapter"
"github.com/cosmos/cosmos-sdk/store/types"
)

func newCacheKVStore() types.CacheKVStore {
mem := dbadapter.Store{DB: dbm.NewMemDB()}
mem := dbadapter.Store{DB: memdb.NewDB()}
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 := dbadapter.Store{DB: dbm.NewMemDB()}
mem := dbadapter.Store{DB: memdb.NewDB()}
st := cachekv.NewStore(mem)

require.Empty(t, st.Get(keyFmt(1)), "Expected `key1` to be empty")
Expand Down Expand Up @@ -65,15 +66,15 @@ func TestCacheKVStore(t *testing.T) {
}

func TestCacheKVStoreNoNilSet(t *testing.T) {
mem := dbadapter.Store{DB: dbm.NewMemDB()}
mem := dbadapter.Store{DB: memdb.NewDB()}
st := cachekv.NewStore(mem)
require.Panics(t, func() { st.Set([]byte("key"), nil) }, "setting a nil value should panic")
require.Panics(t, func() { st.Set(nil, []byte("value")) }, "setting a nil key should panic")
require.Panics(t, func() { st.Set([]byte(""), []byte("value")) }, "setting an empty key should panic")
}

func TestCacheKVStoreNested(t *testing.T) {
mem := dbadapter.Store{DB: dbm.NewMemDB()}
mem := dbadapter.Store{DB: memdb.NewDB()}
st := cachekv.NewStore(mem)

// set. check its there on st and not on mem.
Expand Down Expand Up @@ -228,7 +229,7 @@ func TestCacheKVMergeIteratorDeleteLast(t *testing.T) {

func TestCacheKVMergeIteratorDeletes(t *testing.T) {
st := newCacheKVStore()
truth := dbm.NewMemDB()
truth := memdb.NewDB()

// set some items and write them
nItems := 10
Expand All @@ -245,7 +246,7 @@ func TestCacheKVMergeIteratorDeletes(t *testing.T) {

// reset
st = newCacheKVStore()
truth = dbm.NewMemDB()
truth = memdb.NewDB()

// set some items and write them
for i := 0; i < nItems; i++ {
Expand All @@ -264,7 +265,7 @@ func TestCacheKVMergeIteratorChunks(t *testing.T) {
st := newCacheKVStore()

// Use the truth to check values on the merge iterator
truth := dbm.NewMemDB()
truth := memdb.NewDB()

// sets to the parent
setRange(t, st, truth, 0, 20)
Expand Down Expand Up @@ -293,7 +294,7 @@ func TestCacheKVMergeIteratorChunks(t *testing.T) {

func TestCacheKVMergeIteratorRandom(t *testing.T) {
st := newCacheKVStore()
truth := dbm.NewMemDB()
truth := memdb.NewDB()

start, end := 25, 975
max := 1000
Expand Down Expand Up @@ -324,7 +325,7 @@ func randInt(n int) int {
}

// useful for replaying a error case if we find one
func doOp(t *testing.T, st types.CacheKVStore, truth dbm.DB, op int, args ...int) {
func doOp(t *testing.T, st types.CacheKVStore, truth dbm.DBReadWriter, op int, args ...int) {
switch op {
case opSet:
k := args[0]
Expand All @@ -349,7 +350,7 @@ func doOp(t *testing.T, st types.CacheKVStore, truth dbm.DB, op int, args ...int
}
}

func doRandomOp(t *testing.T, st types.CacheKVStore, truth dbm.DB, maxKey int) {
func doRandomOp(t *testing.T, st types.CacheKVStore, truth dbm.DBReadWriter, maxKey int) {
r := randInt(totalOps)
switch r {
case opSet:
Expand Down Expand Up @@ -390,7 +391,7 @@ func assertIterateDomain(t *testing.T, st types.KVStore, expectedN int) {
require.Equal(t, expectedN, i)
}

func assertIterateDomainCheck(t *testing.T, st types.KVStore, mem dbm.DB, r []keyRange) {
func assertIterateDomainCheck(t *testing.T, st types.KVStore, mem dbm.DBReadWriter, r []keyRange) {
// iterate over each and check they match the other
itr := st.Iterator(nil, nil)
itr2, err := mem.Iterator(nil, nil) // ground truth
Expand Down Expand Up @@ -421,7 +422,7 @@ func assertIterateDomainCheck(t *testing.T, st types.KVStore, mem dbm.DB, r []ke
require.False(t, itr2.Valid())
}

func assertIterateDomainCompare(t *testing.T, st types.KVStore, mem dbm.DB) {
func assertIterateDomainCompare(t *testing.T, st types.KVStore, mem dbm.DBReadWriter) {
// iterate over each and check they match the other
itr := st.Iterator(nil, nil)
itr2, err := mem.Iterator(nil, nil) // ground truth
Expand All @@ -445,15 +446,15 @@ func checkIterators(t *testing.T, itr, itr2 types.Iterator) {

//--------------------------------------------------------

func setRange(t *testing.T, st types.KVStore, mem dbm.DB, start, end int) {
func setRange(t *testing.T, st types.KVStore, mem dbm.DBReadWriter, start, end int) {
for i := start; i < end; i++ {
st.Set(keyFmt(i), valFmt(i))
err := mem.Set(keyFmt(i), valFmt(i))
require.NoError(t, err)
}
}

func deleteRange(t *testing.T, st types.KVStore, mem dbm.DB, start, end int) {
func deleteRange(t *testing.T, st types.KVStore, mem dbm.DBReadWriter, start, end int) {
for i := start; i < end; i++ {
st.Delete(keyFmt(i))
err := mem.Delete(keyFmt(i))
Expand Down
2 changes: 1 addition & 1 deletion store/cachemulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewStore(
traceWriter io.Writer, traceContext types.TraceContext, listeners map[types.StoreKey][]types.WriteListener,
) Store {

return NewFromKVStore(dbadapter.Store{DB: db}, stores, keys, traceWriter, traceContext, listeners)
return NewFromKVStore(dbadapter.NewStore(db), stores, keys, traceWriter, traceContext, listeners)
}

func newCacheMultiStoreFromCMS(cms Store) Store {
Expand Down
9 changes: 7 additions & 2 deletions store/dbadapter/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package dbadapter
import (
"io"

dbm "github.com/tendermint/tm-db"
dbm "github.com/cosmos/cosmos-sdk/db"
tmdb "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/store/cachekv"
"github.com/cosmos/cosmos-sdk/store/listenkv"
Expand All @@ -13,7 +14,11 @@ import (

// Wrapper type for dbm.Db with implementation of KVStore
type Store struct {
dbm.DB
DB dbm.DBReadWriter
}

func NewStore(db tmdb.DB) Store {
return Store{DB: dbm.MungeDBRW(db)}
}

// Get wraps the underlying DB's Get method panicing on error.
Expand Down
6 changes: 3 additions & 3 deletions store/dbadapter/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/cosmos/cosmos-sdk/store/dbadapter"
"github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/tests/mocks"
mocks "github.com/cosmos/cosmos-sdk/tests/mocks/db"
)

var errFoo = errors.New("dummy")
Expand All @@ -21,7 +21,7 @@ func TestAccessors(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockDB := mocks.NewMockDB(mockCtrl)
mockDB := mocks.NewMockDBReadWriter(mockCtrl)
store := dbadapter.Store{mockDB}
key := []byte("test")
value := []byte("testvalue")
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestAccessors(t *testing.T) {

func TestCacheWraps(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockDB := mocks.NewMockDB(mockCtrl)
mockDB := mocks.NewMockDBReadWriter(mockCtrl)
store := dbadapter.Store{mockDB}

cacheWrapper := store.CacheWrap()
Expand Down
10 changes: 5 additions & 5 deletions store/gaskv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/db/memdb"

"github.com/cosmos/cosmos-sdk/store/dbadapter"
"github.com/cosmos/cosmos-sdk/store/gaskv"
Expand All @@ -19,7 +19,7 @@ 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 TestGasKVStoreBasic(t *testing.T) {
mem := dbadapter.Store{DB: dbm.NewMemDB()}
mem := dbadapter.Store{DB: memdb.NewDB()}
meter := types.NewGasMeter(10000)
st := gaskv.NewStore(mem, meter, types.KVGasConfig())

Expand All @@ -40,7 +40,7 @@ func TestGasKVStoreBasic(t *testing.T) {
}

func TestGasKVStoreIterator(t *testing.T) {
mem := dbadapter.Store{DB: dbm.NewMemDB()}
mem := dbadapter.Store{DB: memdb.NewDB()}
meter := types.NewGasMeter(10000)
st := gaskv.NewStore(mem, meter, types.KVGasConfig())
require.False(t, st.Has(keyFmt(1)))
Expand Down Expand Up @@ -92,14 +92,14 @@ func TestGasKVStoreIterator(t *testing.T) {
}

func TestGasKVStoreOutOfGasSet(t *testing.T) {
mem := dbadapter.Store{DB: dbm.NewMemDB()}
mem := dbadapter.Store{DB: memdb.NewDB()}
meter := types.NewGasMeter(0)
st := gaskv.NewStore(mem, meter, types.KVGasConfig())
require.Panics(t, func() { st.Set(keyFmt(1), valFmt(1)) }, "Expected out-of-gas")
}

func TestGasKVStoreOutOfGasIterator(t *testing.T) {
mem := dbadapter.Store{DB: dbm.NewMemDB()}
mem := dbadapter.Store{DB: memdb.NewDB()}
meter := types.NewGasMeter(20000)
st := gaskv.NewStore(mem, meter, types.KVGasConfig())
st.Set(keyFmt(1), valFmt(1))
Expand Down
6 changes: 3 additions & 3 deletions store/listenkv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

"github.com/stretchr/testify/require"

dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/db/memdb"
)

func bz(s string) []byte { return []byte(s) }
Expand Down Expand Up @@ -45,7 +45,7 @@ func newListenKVStore(w io.Writer) *listenkv.Store {

func newEmptyListenKVStore(w io.Writer) *listenkv.Store {
listener := types.NewStoreKVPairWriteListener(w, testMarshaller)
memDB := dbadapter.Store{DB: dbm.NewMemDB()}
memDB := dbadapter.Store{DB: memdb.NewDB()}

return listenkv.NewStore(memDB, testStoreKey, []types.WriteListener{listener})
}
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestListenKVStorePrefix(t *testing.T) {
}

func TestListenKVStoreGetStoreType(t *testing.T) {
memDB := dbadapter.Store{DB: dbm.NewMemDB()}
memDB := dbadapter.Store{DB: memdb.NewDB()}
store := newEmptyListenKVStore(nil)
require.Equal(t, memDB.GetStoreType(), store.GetStoreType())
}
Expand Down
8 changes: 4 additions & 4 deletions store/mem/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mem
import (
"io"

dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/db/memdb"

"github.com/cosmos/cosmos-sdk/store/cachekv"
"github.com/cosmos/cosmos-sdk/store/dbadapter"
Expand All @@ -24,11 +24,11 @@ type Store struct {
}

func NewStore() *Store {
return NewStoreWithDB(dbm.NewMemDB())
return NewStoreWithDB(memdb.NewDB())
}

func NewStoreWithDB(db *dbm.MemDB) *Store { // nolint: interfacer
return &Store{Store: dbadapter.Store{DB: db}}
func NewStoreWithDB(db *memdb.MemDB) *Store { // nolint: interfacer
return &Store{Store: dbadapter.Store{DB: db.ReadWriter()}}
}

// GetStoreType returns the Store's type.
Expand Down
Loading

0 comments on commit 924f265

Please sign in to comment.