Skip to content

Commit

Permalink
feat(collections)!: remove dependency on SDK (#14324)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc authored Dec 15, 2022
1 parent c275ec4 commit 175de85
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 243 deletions.
11 changes: 0 additions & 11 deletions collections/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package collections
import (
"errors"
"math"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
)

var (
Expand All @@ -14,15 +12,6 @@ var (
ErrEncoding = errors.New("collections: encoding error")
)

// StorageProvider is implemented by types
// which provide a KVStore given a StoreKey.
// It represents sdk.Context, it exists to
// reduce dependencies.
type StorageProvider interface {
// KVStore returns a KVStore given its StoreKey.
KVStore(key storetypes.StoreKey) storetypes.KVStore
}

// collection is the interface that all collections support. It will eventually
// include methods for importing/exporting genesis data and schema
// reflection for clients.
Expand Down
68 changes: 56 additions & 12 deletions collections/collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,73 @@ package collections

import (
"context"
"cosmossdk.io/core/store"
db "github.com/tendermint/tm-db"
"math"
"testing"

"github.com/cosmos/cosmos-sdk/store/mem"
"github.com/cosmos/cosmos-sdk/store/types"
"github.com/stretchr/testify/require"
)

var _ StorageProvider = (*mockStorageProvider)(nil)
type testStore struct {
db db.DB
}

func (t testStore) OpenKVStore(ctx context.Context) store.KVStore {
return t
}

func (t testStore) Get(key []byte) []byte {
res, err := t.db.Get(key)
if err != nil {
panic(err)
}
return res
}

func (t testStore) Has(key []byte) bool {
res, err := t.db.Has(key)
if err != nil {
panic(err)
}
return res
}

func (t testStore) Set(key, value []byte) {
err := t.db.Set(key, value)
if err != nil {
panic(err)
}
}

type mockStorageProvider struct {
context.Context
store types.KVStore
func (t testStore) Delete(key []byte) {
err := t.db.Delete(key)
if err != nil {
panic(err)
}
}

func (m mockStorageProvider) KVStore(key types.StoreKey) types.KVStore {
return m.store
func (t testStore) Iterator(start, end []byte) store.Iterator {
res, err := t.db.Iterator(start, end)
if err != nil {
panic(err)
}
return res
}

func deps() (types.StoreKey, context.Context) {
kv := mem.NewStore()
key := types.NewKVStoreKey("test")
return key, mockStorageProvider{store: kv}
func (t testStore) ReverseIterator(start, end []byte) store.Iterator {
res, err := t.db.ReverseIterator(start, end)
if err != nil {
panic(err)
}
return res
}

var _ store.KVStore = testStore{}

func deps() (store.KVStoreService, context.Context) {
kv := db.NewMemDB()
return &testStore{kv}, context.Background()
}

// checkKeyCodec asserts the correct behaviour of a KeyCodec over the type T.
Expand Down
24 changes: 3 additions & 21 deletions collections/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,33 @@ module cosmossdk.io/collections
go 1.19

require (
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20221207205747-f3be41836f4d
cosmossdk.io/core v0.3.3
github.com/stretchr/testify v1.8.1
github.com/tendermint/tm-db v0.6.7
)

require (
cosmossdk.io/errors v1.0.0-beta.7 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/confio/ics23/go v0.9.0 // indirect
github.com/cosmos/gogoproto v1.4.3 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.15.12 // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tendermint/tendermint v0.37.0-rc2 // indirect
github.com/tendermint/tm-db v0.6.7 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/exp v0.0.0-20221019170559-20944726eadf // indirect
golang.org/x/net v0.3.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit 175de85

Please sign in to comment.