Skip to content

Commit

Permalink
Merge branch 'bez/staking-genesis-mod' of github.com:cosmos/cosmos-sd…
Browse files Browse the repository at this point in the history
…k into bez/staking-genesis-mod
  • Loading branch information
alexanderbez committed May 4, 2022
2 parents 671c64c + 0fb8ee1 commit a4c5255
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion x/group/internal/orm/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// TableExportable
// TableExportable defines the methods to import and export a table.
type TableExportable interface {
// Export stores all the values in the table in the passed
// ModelSlicePtr. If the table has an associated sequence, then its
Expand Down
33 changes: 19 additions & 14 deletions x/group/internal/orm/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,11 @@ func (i MultiKeyIndex) GetPaginated(store sdk.KVStore, searchKey interface{}, pa
//
// CONTRACT: No writes may happen within a domain while an iterator exists over it.
func (i MultiKeyIndex) PrefixScan(store sdk.KVStore, startI interface{}, endI interface{}) (Iterator, error) {
start, err := getPrefixScanKeyBytes(startI)
if err != nil {
return nil, err
}
end, err := getPrefixScanKeyBytes(endI)
start, end, err := getStartEndBz(startI, endI)
if err != nil {
return nil, err
}

if start != nil && end != nil && bytes.Compare(start, end) >= 0 {
return NewInvalidIterator(), sdkerrors.Wrap(errors.ErrORMInvalidArgument, "start must be less than end")
}
pStore := prefix.NewStore(store, []byte{i.prefix})
it := pStore.Iterator(start, end)
return indexIterator{store: store, it: it, rowGetter: i.rowGetter, indexKey: i.indexKey}, nil
Expand All @@ -159,21 +152,33 @@ func (i MultiKeyIndex) PrefixScan(store sdk.KVStore, startI interface{}, endI in
//
// CONTRACT: No writes may happen within a domain while an iterator exists over it.
func (i MultiKeyIndex) ReversePrefixScan(store sdk.KVStore, startI interface{}, endI interface{}) (Iterator, error) {
start, err := getPrefixScanKeyBytes(startI)
start, end, err := getStartEndBz(startI, endI)
if err != nil {
return nil, err
}

pStore := prefix.NewStore(store, []byte{i.prefix})
it := pStore.ReverseIterator(start, end)
return indexIterator{store: store, it: it, rowGetter: i.rowGetter, indexKey: i.indexKey}, nil
}

// getStartEndBz gets the start and end bytes to be passed into the SDK store
// iterator.
func getStartEndBz(startI interface{}, endI interface{}) ([]byte, []byte, error) {
start, err := getPrefixScanKeyBytes(startI)
if err != nil {
return nil, nil, err
}
end, err := getPrefixScanKeyBytes(endI)
if err != nil {
return nil, err
return nil, nil, err
}

if start != nil && end != nil && bytes.Compare(start, end) >= 0 {
return NewInvalidIterator(), sdkerrors.Wrap(errors.ErrORMInvalidArgument, "start must be less than end")
return nil, nil, sdkerrors.Wrap(errors.ErrORMInvalidArgument, "start must be less than end")
}
pStore := prefix.NewStore(store, []byte{i.prefix})
it := pStore.ReverseIterator(start, end)
return indexIterator{store: store, it: it, rowGetter: i.rowGetter, indexKey: i.indexKey}, nil

return start, end, nil
}

func getPrefixScanKeyBytes(keyI interface{}) ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions x/group/internal/orm/primary_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ type PrimaryKeyed interface {
// the primary key. The PrimaryKey function will encode and concatenate
// the fields to build the primary key.
//
// PrimaryKey parts can be []byte, string, and integer types. []byte is
// PrimaryKey parts can be []byte, string, and uint64 types. []byte is
// encoded with a length prefix, strings are null-terminated, and
// integers are encoded using 8 byte big endian.
// uint64 are encoded using 8 byte big endian.
//
// IMPORTANT: []byte parts are encoded with a single byte length prefix,
// so cannot be longer than 255 bytes.
Expand Down
15 changes: 8 additions & 7 deletions x/group/internal/orm/spec/01_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ Regular CRUD operations can be performed on a table, these methods take a `sdk.K

The `table` struct does not:

* enforce uniqueness of the `RowID`
* enforce prefix uniqueness of keys, i.e. not allowing one key to be a prefix
of another
* optimize Gas usage conditions
- enforce uniqueness of the `RowID`
- enforce prefix uniqueness of keys, i.e. not allowing one key to be a prefix
of another
- optimize Gas usage conditions

The `table` struct is private, so that we only have custom tables built on top of it, that do satisfy these requirements.

`table` provides methods for exporting (using a [`PrefixScan` `Iterator`](03_iterator_pagination.md#iterator)) and importing genesis data. For the import to be successful, objects have to be aware of their primary key by implementing the [`PrimaryKeyed`](#primarykeyed) interface.
Expand Down Expand Up @@ -42,6 +43,6 @@ The primary key parts can be []byte, string, and `uint64` types.

Key parts, except the last part, follow these rules:

* []byte is encoded with a single byte length prefix
* strings are null-terminated
* `uint64` are encoded using 8 byte big endian.
- []byte is encoded with a single byte length prefix (which means the max []byte length is 255)
- strings are null-terminated
- `uint64` are encoded using 8 byte big endian.

0 comments on commit a4c5255

Please sign in to comment.