Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: migrate x/group to store service #19410

Merged
merged 7 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x/group/internal/orm/auto_uint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package orm
import (
"github.com/cosmos/gogoproto/proto"

storetypes "cosmossdk.io/core/store"
"cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
)
Expand Down
10 changes: 7 additions & 3 deletions x/group/internal/orm/auto_uint64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

corestore "cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/group/errors"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
)

Expand All @@ -23,8 +26,9 @@ func TestAutoUInt64PrefixScan(t *testing.T) {
tb, err := NewAutoUInt64Table(AutoUInt64TablePrefix, AutoUInt64TableSeqPrefix, &testdata.TableModel{}, cdc)
require.NoError(t, err)

ctx := NewMockContext()
store := ctx.KVStore(storetypes.NewKVStoreKey("test"))
key := storetypes.NewKVStoreKey("test")
testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test"))
store := runtime.NewKVStoreService(key).OpenKVStore(testCtx.Ctx)

metadata := []byte("metadata")
t1 := testdata.TableModel{
Expand Down Expand Up @@ -53,7 +57,7 @@ func TestAutoUInt64PrefixScan(t *testing.T) {
expResult []testdata.TableModel
expRowIDs []RowID
expError *errorsmod.Error
method func(store storetypes.KVStore, start, end uint64) (Iterator, error)
method func(store corestore.KVStore, start, end uint64) (Iterator, error)
}{
"first element": {
start: 1,
Expand Down
2 changes: 1 addition & 1 deletion x/group/internal/orm/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package orm

import storetypes "cosmossdk.io/store/types"
import storetypes "cosmossdk.io/core/store"

// TableExportable defines the methods to import and export a table.
type TableExportable interface {
Expand Down
7 changes: 5 additions & 2 deletions x/group/internal/orm/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
)

Expand All @@ -19,8 +21,9 @@ func TestImportExportTableData(t *testing.T) {
table, err := NewAutoUInt64Table(AutoUInt64TablePrefix, AutoUInt64TableSeqPrefix, &testdata.TableModel{}, cdc)
require.NoError(t, err)

ctx := NewMockContext()
store := ctx.KVStore(storetypes.NewKVStoreKey("test"))
key := storetypes.NewKVStoreKey("test")
testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test"))
store := runtime.NewKVStoreService(key).OpenKVStore(testCtx.Ctx)

tms := []*testdata.TableModel{
{
Expand Down
66 changes: 42 additions & 24 deletions x/group/internal/orm/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (

"github.com/cosmos/gogoproto/proto"

storetypes "cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/store/prefix"
"cosmossdk.io/store/types"
"cosmossdk.io/x/group/errors"
"cosmossdk.io/x/group/internal/orm/prefixstore"

"github.com/cosmos/cosmos-sdk/types/query"
)

// indexer creates and modifies the second MultiKeyIndex based on the operations and changes on the primary object.
type indexer interface {
OnCreate(store types.KVStore, rowID RowID, value interface{}) error
OnDelete(store types.KVStore, rowID RowID, value interface{}) error
OnUpdate(store types.KVStore, rowID RowID, newValue, oldValue interface{}) error
OnCreate(store storetypes.KVStore, rowID RowID, value interface{}) error
OnDelete(store storetypes.KVStore, rowID RowID, value interface{}) error
OnUpdate(store storetypes.KVStore, rowID RowID, newValue, oldValue interface{}) error
}

var _ Index = &MultiKeyIndex{}
Expand Down Expand Up @@ -72,34 +73,40 @@ func newIndex(tb Indexable, prefix byte, indexer *Indexer, indexerF IndexerFunc,
}

// Has checks if a key exists. Returns an error on nil key.
func (i MultiKeyIndex) Has(store types.KVStore, key interface{}) (bool, error) {
func (i MultiKeyIndex) Has(store storetypes.KVStore, key interface{}) (bool, error) {
encodedKey, err := keyPartBytes(key, false)
if err != nil {
return false, err
}

pStore := prefix.NewStore(store, []byte{i.prefix})
it := pStore.Iterator(PrefixRange(encodedKey))
pStore := prefixstore.New(store, []byte{i.prefix})
it, err := pStore.Iterator(PrefixRange(encodedKey))
if err != nil {
return false, err
}
defer it.Close()
return it.Valid(), nil
}

// Get returns a result iterator for the searchKey. Parameters must not be nil.
func (i MultiKeyIndex) Get(store types.KVStore, searchKey interface{}) (Iterator, error) {
func (i MultiKeyIndex) Get(store storetypes.KVStore, searchKey interface{}) (Iterator, error) {
encodedKey, err := keyPartBytes(searchKey, false)
if err != nil {
return nil, err
}

pStore := prefix.NewStore(store, []byte{i.prefix})
it := pStore.Iterator(PrefixRange(encodedKey))
pStore := prefixstore.New(store, []byte{i.prefix})
it, err := pStore.Iterator(PrefixRange(encodedKey))
if err != nil {
return nil, err
}
return indexIterator{store: store, it: it, rowGetter: i.rowGetter, indexKey: i.indexKey}, nil
}

// GetPaginated creates an iterator for the searchKey
// starting from pageRequest.Key if provided.
// The pageRequest.Key is the rowID while searchKey is a MultiKeyIndex key.
func (i MultiKeyIndex) GetPaginated(store types.KVStore, searchKey interface{}, pageRequest *query.PageRequest) (Iterator, error) {
func (i MultiKeyIndex) GetPaginated(store storetypes.KVStore, searchKey interface{}, pageRequest *query.PageRequest) (Iterator, error) {
encodedKey, err := keyPartBytes(searchKey, false)
if err != nil {
return nil, err
Expand All @@ -114,8 +121,11 @@ func (i MultiKeyIndex) GetPaginated(store types.KVStore, searchKey interface{},
}
}

pStore := prefix.NewStore(store, []byte{i.prefix})
it := pStore.Iterator(start, end)
pStore := prefixstore.New(store, []byte{i.prefix})
it, err := pStore.Iterator(start, end)
if err != nil {
return nil, err
}
return indexIterator{store: store, it: it, rowGetter: i.rowGetter, indexKey: i.indexKey}, nil
}

Expand All @@ -136,14 +146,18 @@ func (i MultiKeyIndex) GetPaginated(store types.KVStore, searchKey interface{},
// it = LimitIterator(it, defaultLimit)
//
// CONTRACT: No writes may happen within a domain while an iterator exists over it.
func (i MultiKeyIndex) PrefixScan(store types.KVStore, startI, endI interface{}) (Iterator, error) {
func (i MultiKeyIndex) PrefixScan(store storetypes.KVStore, startI, endI interface{}) (Iterator, error) {
start, end, err := getStartEndBz(startI, endI)
if err != nil {
return nil, err
}

pStore := prefix.NewStore(store, []byte{i.prefix})
it := pStore.Iterator(start, end)
pStore := prefixstore.New(store, []byte{i.prefix})
it, err := pStore.Iterator(start, end)
if err != nil {
return nil, err
}

return indexIterator{store: store, it: it, rowGetter: i.rowGetter, indexKey: i.indexKey}, nil
}

Expand All @@ -156,14 +170,18 @@ func (i MultiKeyIndex) PrefixScan(store types.KVStore, startI, endI interface{})
// this as an endpoint to the public without further limits. See `LimitIterator`
//
// CONTRACT: No writes may happen within a domain while an iterator exists over it.
func (i MultiKeyIndex) ReversePrefixScan(store types.KVStore, startI, endI interface{}) (Iterator, error) {
func (i MultiKeyIndex) ReversePrefixScan(store storetypes.KVStore, startI, endI interface{}) (Iterator, error) {
start, end, err := getStartEndBz(startI, endI)
if err != nil {
return nil, err
}

pStore := prefix.NewStore(store, []byte{i.prefix})
it := pStore.ReverseIterator(start, end)
pStore := prefixstore.New(store, []byte{i.prefix})
it, err := pStore.ReverseIterator(start, end)
if err != nil {
return nil, err
}

return indexIterator{store: store, it: it, rowGetter: i.rowGetter, indexKey: i.indexKey}, nil
}

Expand Down Expand Up @@ -202,16 +220,16 @@ func getPrefixScanKeyBytes(keyI interface{}) ([]byte, error) {
return key, nil
}

func (i MultiKeyIndex) onSet(store types.KVStore, rowID RowID, newValue, oldValue proto.Message) error {
pStore := prefix.NewStore(store, []byte{i.prefix})
func (i MultiKeyIndex) onSet(store storetypes.KVStore, rowID RowID, newValue, oldValue proto.Message) error {
pStore := prefixstore.New(store, []byte{i.prefix})
if oldValue == nil {
return i.indexer.OnCreate(pStore, rowID, newValue)
}
return i.indexer.OnUpdate(pStore, rowID, newValue, oldValue)
}

func (i MultiKeyIndex) onDelete(store types.KVStore, rowID RowID, oldValue proto.Message) error {
pStore := prefix.NewStore(store, []byte{i.prefix})
func (i MultiKeyIndex) onDelete(store storetypes.KVStore, rowID RowID, oldValue proto.Message) error {
pStore := prefixstore.New(store, []byte{i.prefix})
return i.indexer.OnDelete(pStore, rowID, oldValue)
}

Expand All @@ -236,7 +254,7 @@ func NewUniqueIndex(tb Indexable, prefix byte, uniqueIndexerFunc UniqueIndexerFu

// indexIterator uses rowGetter to lazy load new model values on request.
type indexIterator struct {
store types.KVStore
store storetypes.KVStore
rowGetter RowGetter
it types.Iterator
indexKey interface{}
Expand Down
15 changes: 10 additions & 5 deletions x/group/internal/orm/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

corestore "cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/group/errors"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
"github.com/cosmos/cosmos-sdk/types/query"
)
Expand Down Expand Up @@ -101,8 +104,9 @@ func TestIndexPrefixScan(t *testing.T) {
}, testdata.TableModel{}.Name)
require.NoError(t, err)

ctx := NewMockContext()
store := ctx.KVStore(storetypes.NewKVStoreKey("test"))
key := storetypes.NewKVStoreKey("test")
testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test"))
store := runtime.NewKVStoreService(key).OpenKVStore(testCtx.Ctx)

g1 := testdata.TableModel{
Id: 1,
Expand Down Expand Up @@ -130,7 +134,7 @@ func TestIndexPrefixScan(t *testing.T) {
expResult []testdata.TableModel
expRowIDs []RowID
expError *errorsmod.Error
method func(store storetypes.KVStore, start, end interface{}) (Iterator, error)
method func(store corestore.KVStore, start, end interface{}) (Iterator, error)
}{
"exact match with a single result": {
start: []byte("metadata-a"),
Expand Down Expand Up @@ -301,8 +305,9 @@ func TestUniqueIndex(t *testing.T) {
}, []byte{})
require.NoError(t, err)

ctx := NewMockContext()
store := ctx.KVStore(storetypes.NewKVStoreKey("test"))
key := storetypes.NewKVStoreKey("test")
testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test"))
store := runtime.NewKVStoreService(key).OpenKVStore(testCtx.Ctx)

m := testdata.TableModel{
Id: 1,
Expand Down
7 changes: 5 additions & 2 deletions x/group/internal/orm/indexer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package orm

import (
storetypes "cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/group/errors"
)

Expand Down Expand Up @@ -144,7 +144,10 @@ func uniqueKeysAddFunc(store storetypes.KVStore, secondaryIndexKey interface{},

// checkUniqueIndexKey checks that the given secondary index key is unique
func checkUniqueIndexKey(store storetypes.KVStore, secondaryIndexKeyBytes []byte) error {
it := store.Iterator(PrefixRange(secondaryIndexKeyBytes))
it, err := store.Iterator(PrefixRange(secondaryIndexKeyBytes))
if err != nil {
return err
}
defer it.Close()
if it.Valid() {
return errors.ErrORMUniqueConstraint
Expand Down
Loading
Loading