Skip to content

Commit

Permalink
feat!: migrate x/group to store service (#19410)
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica authored Feb 13, 2024
1 parent 869c96c commit c57dde2
Show file tree
Hide file tree
Showing 39 changed files with 830 additions and 549 deletions.
4 changes: 2 additions & 2 deletions math/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ func BenchmarkIntSize(b *testing.B) {
}

func BenchmarkIntOverflowCheckTime(b *testing.B) {
var ints = []*big.Int{}
ints := []*big.Int{}

for _, st := range sizeTests {
ii, _ := math.NewIntFromString(st.s)
Expand All @@ -699,7 +699,7 @@ func BenchmarkIntOverflowCheckTime(b *testing.B) {

b.ReportAllocs()
for i := 0; i < b.N; i++ {
for j, _ := range sizeTests {
for j := range sizeTests {
got := math.NewIntFromBigIntMut(ints[j])
sink = got
}
Expand Down
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func NewSimApp(
config.MaxProposalTitleLen = 255 // example max title length in characters
config.MaxProposalSummaryLen = 10200 // example max summary length in characters
*/
app.GroupKeeper = groupkeeper.NewKeeper(keys[group.StoreKey], appCodec, app.MsgServiceRouter(), app.AuthKeeper, groupConfig)
app.GroupKeeper = groupkeeper.NewKeeper(runtime.NewKVStoreService(keys[group.StoreKey]), appCodec, app.MsgServiceRouter(), app.AuthKeeper, groupConfig)

// get skipUpgradeHeights from the app options
skipUpgradeHeights := map[int64]bool{}
Expand Down
2 changes: 2 additions & 0 deletions x/group/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Features

### API Breaking Changes

* [#19410](https://github.com/cosmos/cosmos-sdk/pull/19410) Migrate to Store Service.
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
21 changes: 13 additions & 8 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 @@ -81,7 +81,9 @@ func (i Indexer) OnDelete(store storetypes.KVStore, rowID RowID, value interface
if err != nil {
return err
}
store.Delete(indexKey)
if err := store.Delete(indexKey); err != nil {
return err
}
}
return nil
}
Expand All @@ -105,7 +107,9 @@ func (i Indexer) OnUpdate(store storetypes.KVStore, rowID RowID, newValue, oldVa
if err != nil {
return err
}
store.Delete(indexKey)
if err := store.Delete(indexKey); err != nil {
return err
}
}
newKeys, err := difference(newSecIdxKeys, oldSecIdxKeys)
if err != nil {
Expand Down Expand Up @@ -138,13 +142,15 @@ func uniqueKeysAddFunc(store storetypes.KVStore, secondaryIndexKey interface{},
return err
}

store.Set(indexKey, []byte{})
return nil
return store.Set(indexKey, []byte{})
}

// 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 All @@ -170,8 +176,7 @@ func multiKeyAddFunc(store storetypes.KVStore, secondaryIndexKey interface{}, ro
return errorsmod.Wrap(errors.ErrORMInvalidArgument, "empty index key")
}

store.Set(encodedKey, []byte{})
return nil
return store.Set(encodedKey, []byte{})
}

// difference returns the list of elements that are in a but not in b.
Expand Down
Loading

0 comments on commit c57dde2

Please sign in to comment.