Skip to content

Commit

Permalink
Merge branch 'main' into marko/15463
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Apr 11, 2023
2 parents baf9840 + 7ab0dfc commit 68a6890
Show file tree
Hide file tree
Showing 28 changed files with 230 additions and 354 deletions.
2 changes: 1 addition & 1 deletion baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestBaseApp_BlockGas(t *testing.T) {
require.Equal(t, []byte("ok"), okValue)
}
// check block gas is always consumed
baseGas := uint64(51732) // baseGas is the gas consumed before tx msg
baseGas := uint64(50702) // baseGas is the gas consumed before tx msg
expGasConsumed := addUint64Saturating(tc.gasToConsume, baseGas)
if expGasConsumed > txtypes.MaxGasWanted {
// capped by gasLimit
Expand Down
3 changes: 2 additions & 1 deletion client/rpc/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rpc_test
import (
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/types/address"
"strconv"
"testing"

Expand Down Expand Up @@ -112,7 +113,7 @@ func (s *IntegrationTestSuite) TestQueryABCIHeight() {
req := abci.RequestQuery{
Path: fmt.Sprintf("store/%s/key", banktypes.StoreKey),
Height: tc.reqHeight,
Data: banktypes.CreateAccountBalancesPrefix(val.Address),
Data: address.MustLengthPrefix(val.Address),
Prove: true,
}

Expand Down
15 changes: 0 additions & 15 deletions fuzz/tests/x_bank_types_addressfrombalancesstore_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module github.com/cosmos/cosmos-sdk

require (
cosmossdk.io/api v0.4.0
cosmossdk.io/collections v0.0.0-20230309163709-87da587416ba
cosmossdk.io/collections v0.0.0-20230411101845-3d1a0b8840e4
cosmossdk.io/core v0.6.1
cosmossdk.io/depinject v1.0.0-alpha.3
cosmossdk.io/errors v1.0.0-beta.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
cosmossdk.io/api v0.4.0 h1:x90DmdidP6EhzktAa/6/IofSHidDnPjahdlrUvyQZQw=
cosmossdk.io/api v0.4.0/go.mod h1:TWDzBhUBhI1LhSf2XSYpfIBf6D4mbLu/fvzvDfhcaYM=
cosmossdk.io/collections v0.0.0-20230309163709-87da587416ba h1:S4PYij/tX3Op/hwenVEN9D+M27JRcwSwVqE3UA0BnwM=
cosmossdk.io/collections v0.0.0-20230309163709-87da587416ba/go.mod h1:lpS+G8bGC2anqzWdndTzjnQnuMO/qAcgZUkGJp4i3rc=
cosmossdk.io/collections v0.0.0-20230411101845-3d1a0b8840e4 h1:QQZ0Qz8Gy/EmUNMRiHkUPG3BMA6OqEBp67IsfKETXIU=
cosmossdk.io/collections v0.0.0-20230411101845-3d1a0b8840e4/go.mod h1:/vS4ugR7ad3IciUd5TQuP2Ldz3NukHK2u/l5xTxXbbE=
cosmossdk.io/core v0.6.1 h1:OBy7TI2W+/gyn2z40vVvruK3di+cAluinA6cybFbE7s=
cosmossdk.io/core v0.6.1/go.mod h1:g3MMBCBXtxbDWBURDVnJE7XML4BG5qENhs0gzkcpuFA=
cosmossdk.io/depinject v1.0.0-alpha.3 h1:6evFIgj//Y3w09bqOUOzEpFj5tsxBqdc5CfkO7z+zfw=
Expand Down
2 changes: 1 addition & 1 deletion simapp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/storage v1.30.0 // indirect
cosmossdk.io/collections v0.0.0-20230309163709-87da587416ba // indirect
cosmossdk.io/collections v0.0.0-20230411101845-3d1a0b8840e4 // indirect
cosmossdk.io/errors v1.0.0-beta.7 // indirect
cosmossdk.io/x/tx v0.5.1-0.20230407182919-057d2e09bd63 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions simapp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ cloud.google.com/go/workflows v1.6.0/go.mod h1:6t9F5h/unJz41YqfBmqSASJSXccBLtD1V
cloud.google.com/go/workflows v1.7.0/go.mod h1:JhSrZuVZWuiDfKEFxU0/F1PQjmpnpcoISEXH2bcHC3M=
cosmossdk.io/client/v2 v2.0.0-20230309163709-87da587416ba h1:LuPHCncU2KLMNPItFECs709uo46I9wSu2fAWYVCx+/U=
cosmossdk.io/client/v2 v2.0.0-20230309163709-87da587416ba/go.mod h1:SXdwqO7cN5htalh/lhXWP8V4zKtBrhhcSTU+ytuEtmM=
cosmossdk.io/collections v0.0.0-20230309163709-87da587416ba h1:S4PYij/tX3Op/hwenVEN9D+M27JRcwSwVqE3UA0BnwM=
cosmossdk.io/collections v0.0.0-20230309163709-87da587416ba/go.mod h1:lpS+G8bGC2anqzWdndTzjnQnuMO/qAcgZUkGJp4i3rc=
cosmossdk.io/collections v0.0.0-20230411101845-3d1a0b8840e4 h1:QQZ0Qz8Gy/EmUNMRiHkUPG3BMA6OqEBp67IsfKETXIU=
cosmossdk.io/collections v0.0.0-20230411101845-3d1a0b8840e4/go.mod h1:/vS4ugR7ad3IciUd5TQuP2Ldz3NukHK2u/l5xTxXbbE=
cosmossdk.io/core v0.6.1 h1:OBy7TI2W+/gyn2z40vVvruK3di+cAluinA6cybFbE7s=
cosmossdk.io/core v0.6.1/go.mod h1:g3MMBCBXtxbDWBURDVnJE7XML4BG5qENhs0gzkcpuFA=
cosmossdk.io/depinject v1.0.0-alpha.3 h1:6evFIgj//Y3w09bqOUOzEpFj5tsxBqdc5CfkO7z+zfw=
Expand Down
2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ require (
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/storage v1.30.0 // indirect
cosmossdk.io/client/v2 v2.0.0-20230309163709-87da587416ba // indirect
cosmossdk.io/collections v0.0.0-20230309163709-87da587416ba // indirect
cosmossdk.io/collections v0.0.0-20230411101845-3d1a0b8840e4 // indirect
cosmossdk.io/core v0.6.1 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
Expand Down
4 changes: 2 additions & 2 deletions tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ cloud.google.com/go/workflows v1.6.0/go.mod h1:6t9F5h/unJz41YqfBmqSASJSXccBLtD1V
cloud.google.com/go/workflows v1.7.0/go.mod h1:JhSrZuVZWuiDfKEFxU0/F1PQjmpnpcoISEXH2bcHC3M=
cosmossdk.io/client/v2 v2.0.0-20230309163709-87da587416ba h1:LuPHCncU2KLMNPItFECs709uo46I9wSu2fAWYVCx+/U=
cosmossdk.io/client/v2 v2.0.0-20230309163709-87da587416ba/go.mod h1:SXdwqO7cN5htalh/lhXWP8V4zKtBrhhcSTU+ytuEtmM=
cosmossdk.io/collections v0.0.0-20230309163709-87da587416ba h1:S4PYij/tX3Op/hwenVEN9D+M27JRcwSwVqE3UA0BnwM=
cosmossdk.io/collections v0.0.0-20230309163709-87da587416ba/go.mod h1:lpS+G8bGC2anqzWdndTzjnQnuMO/qAcgZUkGJp4i3rc=
cosmossdk.io/collections v0.0.0-20230411101845-3d1a0b8840e4 h1:QQZ0Qz8Gy/EmUNMRiHkUPG3BMA6OqEBp67IsfKETXIU=
cosmossdk.io/collections v0.0.0-20230411101845-3d1a0b8840e4/go.mod h1:/vS4ugR7ad3IciUd5TQuP2Ldz3NukHK2u/l5xTxXbbE=
cosmossdk.io/core v0.6.1 h1:OBy7TI2W+/gyn2z40vVvruK3di+cAluinA6cybFbE7s=
cosmossdk.io/core v0.6.1/go.mod h1:g3MMBCBXtxbDWBURDVnJE7XML4BG5qENhs0gzkcpuFA=
cosmossdk.io/depinject v1.0.0-alpha.3 h1:6evFIgj//Y3w09bqOUOzEpFj5tsxBqdc5CfkO7z+zfw=
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/bank/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,5 +478,5 @@ func TestGRPCDenomOwners(t *testing.T) {
req := &banktypes.QueryDenomOwnersRequest{
Denom: coin1.GetDenom(),
}
testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.DenomOwners, 2525, false)
testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.DenomOwners, 2516, false)
}
2 changes: 1 addition & 1 deletion testutil/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type TestContext struct {
CMS store.CommitMultiStore
}

func DefaultContextWithDB(t *testing.T, key, tkey storetypes.StoreKey) TestContext {
func DefaultContextWithDB(t testing.TB, key, tkey storetypes.StoreKey) TestContext {
db := dbm.NewMemDB()
cms := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
cms.MountStoreWithDB(key, storetypes.StoreTypeIAVL, db)
Expand Down
34 changes: 34 additions & 0 deletions types/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,40 @@ func (a genericAddressKey[T]) SizeNonTerminal(key T) int {
return collections.BytesKey.SizeNonTerminal(key)
}

// Deprecated: genericAddressIndexKey is a special key codec used to retain state backwards compatibility
// when a generic address key (be: AccAddress, ValAddress, ConsAddress), is used as an index key.
// More docs can be found in the AddressKeyAsIndexKey function.
type genericAddressIndexKey[T addressUnion] struct {
collcodec.KeyCodec[T]
}

func (g genericAddressIndexKey[T]) Encode(buffer []byte, key T) (int, error) {
return g.EncodeNonTerminal(buffer, key)
}

func (g genericAddressIndexKey[T]) Decode(buffer []byte) (int, T, error) {
return g.DecodeNonTerminal(buffer)
}

func (g genericAddressIndexKey[T]) Size(key T) int { return g.SizeNonTerminal(key) }

func (g genericAddressIndexKey[T]) KeyType() string { return "index_key/" + g.KeyCodec.KeyType() }

// Deprecated: AddressKeyAsIndexKey implements an SDK backwards compatible indexing key encoder
// for addresses.
// The status quo in the SDK is that address keys are length prefixed even when they're the
// last part of a composite key. This should never be used unless to retain state compatibility.
// For example, a composite key composed of `[string, address]` in theory would need you only to
// define a way to understand when the string part finishes, we usually do this by appending a null
// byte to the string, then when you know when the string part finishes, it's logical that the
// part which remains is the address key. In the SDK instead we prepend to the address key its
// length too.
func AddressKeyAsIndexKey[T addressUnion](keyCodec collcodec.KeyCodec[T]) collcodec.KeyCodec[T] {
return genericAddressIndexKey[T]{
keyCodec,
}
}

// Collection Codecs

type intValueCodec struct{}
Expand Down
4 changes: 4 additions & 0 deletions types/collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ func TestCollectionsCorrectness(t *testing.T) {
t.Run("ConsAddress", func(t *testing.T) {
colltest.TestKeyCodec(t, ConsAddressKey, ConsAddress{0x32, 0x0, 0x0, 0x3})
})

t.Run("AddressIndexingKey", func(t *testing.T) {
colltest.TestKeyCodec(t, AddressKeyAsIndexKey(AccAddressKey), AccAddress{0x2, 0x5, 0x8})
})
}
62 changes: 43 additions & 19 deletions types/query/collections_pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ package query

import (
"context"
"errors"
"fmt"

"cosmossdk.io/collections"
collcodec "cosmossdk.io/collections/codec"
storetypes "cosmossdk.io/store/types"
"errors"
"fmt"
)

// WithCollectionPaginationPairPrefix applies a prefix to a collection, whose key is a collection.Pair,
// being paginated that needs prefixing.
func WithCollectionPaginationPairPrefix[K1, K2 any](prefix K1) func(o *CollectionsPaginateOptions[collections.Pair[K1, K2]]) {
return func(o *CollectionsPaginateOptions[collections.Pair[K1, K2]]) {
prefix := collections.PairPrefix[K1, K2](prefix)
o.Prefix = &prefix
}
}

// CollectionsPaginateOptions provides extra options for pagination in collections.
type CollectionsPaginateOptions[K any] struct {
// Prefix allows to optionally set a prefix for the pagination.
Expand Down Expand Up @@ -41,7 +49,7 @@ func CollectionFilteredPaginate[K, V any, C Collection[K, V]](
ctx context.Context,
coll C,
pageReq *PageRequest,
predicateFunc func(key K, value V) (include bool),
predicateFunc func(key K, value V) (include bool, err error),
opts ...func(opt *CollectionsPaginateOptions[K]),
) ([]collections.KeyValue[K, V], *PageResponse, error) {
if pageReq == nil {
Expand Down Expand Up @@ -89,7 +97,7 @@ func CollectionFilteredPaginate[K, V any, C Collection[K, V]](
}
// invalid iter error is ignored to retain Paginate behavior
if errors.Is(err, collections.ErrInvalidIterator) {
return results, pageRes, nil
return results, new(PageResponse), nil
}
// strip the prefix from next key
if len(pageRes.NextKey) != 0 && prefix != nil {
Expand All @@ -108,7 +116,7 @@ func collFilteredPaginateNoKey[K, V any, C Collection[K, V]](
offset uint64,
limit uint64,
countTotal bool,
predicateFunc func(K, V) bool,
predicateFunc func(K, V) (bool, error),
) ([]collections.KeyValue[K, V], *PageResponse, error) {
iterator, err := getCollIter[K, V](ctx, coll, prefix, nil, reverse)
if err != nil {
Expand Down Expand Up @@ -137,12 +145,17 @@ func collFilteredPaginateNoKey[K, V any, C Collection[K, V]](
// if no predicate function is specified then we just include the result
if predicateFunc == nil {
results = append(results, kv)
count++
// if predicate function is defined we check if the result matches the filtering criteria
} else if predicateFunc(kv.Key, kv.Value) {
results = append(results, kv)
count++
} else {
include, err := predicateFunc(kv.Key, kv.Value)
if err != nil {
return nil, nil, err
}
if include {
results = append(results, kv)
}
}
count++
// second case, we found all the objects specified within the limit
case count == limit:
key, err := iterator.Key()
Expand Down Expand Up @@ -200,7 +213,7 @@ func collFilteredPaginateByKey[K, V any, C Collection[K, V]](
key []byte,
reverse bool,
limit uint64,
predicateFunc func(K, V) bool,
predicateFunc func(K, V) (bool, error),
) ([]collections.KeyValue[K, V], *PageResponse, error) {
iterator, err := getCollIter[K, V](ctx, coll, prefix, key, reverse)
if err != nil {
Expand Down Expand Up @@ -237,13 +250,18 @@ func collFilteredPaginateByKey[K, V any, C Collection[K, V]](
// if no predicate is specified then we just append the result
if predicateFunc == nil {
results = append(results, kv)
count++
// if predicate is applied we execute the predicate function
// and append only if predicateFunc yields true.
} else if predicateFunc(kv.Key, kv.Value) {
results = append(results, kv)
count++
} else {
include, err := predicateFunc(kv.Key, kv.Value)
if err != nil {
return nil, nil, err
}
if include {
results = append(results, kv)
}
}
count++
}

return results, &PageResponse{
Expand All @@ -258,14 +276,20 @@ func encodeCollKey[K, V any, C Collection[K, V]](coll C, key K) ([]byte, error)
return buffer, err
}

func getCollIter[K, V any, C Collection[K, V]](ctx context.Context, coll C, prefix, start []byte, reverse bool) (collections.Iterator[K, V], error) {
func getCollIter[K, V any, C Collection[K, V]](ctx context.Context, coll C, prefix []byte, start []byte, reverse bool) (collections.Iterator[K, V], error) {
// TODO: maybe can be simplified
if reverse {
var end []byte
if prefix != nil {
start = storetypes.PrefixEndBytes(append(prefix, start...))
end = prefix
}
return coll.IterateRaw(ctx, end, start, collections.OrderDescending)
}
var end []byte
if prefix != nil {
start = append(prefix, start...)
end = storetypes.PrefixEndBytes(prefix)
}
if reverse {
return coll.IterateRaw(ctx, nil, start, collections.OrderDescending)
}
return coll.IterateRaw(ctx, start, end, collections.OrderAscending)
}
16 changes: 7 additions & 9 deletions types/query/collections_pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestCollectionPagination(t *testing.T) {
type test struct {
req *PageRequest
expResp *PageResponse
filter func(key, value uint64) bool
filter func(key, value uint64) (bool, error)
expResults []collections.KeyValue[uint64, uint64]
wantErr error
}
Expand Down Expand Up @@ -99,15 +99,14 @@ func TestCollectionPagination(t *testing.T) {
Limit: 3,
},
expResp: &PageResponse{
NextKey: encodeKey(5),
NextKey: encodeKey(3),
},
filter: func(key, value uint64) bool {
return key%2 == 0
filter: func(key, value uint64) (bool, error) {
return key%2 == 0, nil
},
expResults: []collections.KeyValue[uint64, uint64]{
{Key: 0, Value: 0},
{Key: 2, Value: 2},
{Key: 4, Value: 4},
},
},
"filtered with key": {
Expand All @@ -116,15 +115,14 @@ func TestCollectionPagination(t *testing.T) {
Limit: 3,
},
expResp: &PageResponse{
NextKey: encodeKey(7),
NextKey: encodeKey(5),
},
filter: func(key, value uint64) bool {
return key%2 == 0
filter: func(key, value uint64) (bool, error) {
return key%2 == 0, nil
},
expResults: []collections.KeyValue[uint64, uint64]{
{Key: 2, Value: 2},
{Key: 4, Value: 4},
{Key: 6, Value: 6},
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion types/query/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (s *paginationTestSuite) TestReversePagination() {
request := types.NewQueryAllBalancesRequest(addr1, pageReq, false)
res1, err := queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res1.Balances.Len(), 2)
s.Require().Equal(2, res1.Balances.Len())
s.Require().NotNil(res1.Pagination.NextKey)

s.T().Log("verify paginate with custom limit and countTotal, Reverse false")
Expand Down
8 changes: 6 additions & 2 deletions x/bank/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"cosmossdk.io/collections"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -24,8 +25,11 @@ func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
for _, balance := range genState.Balances {
addr := balance.GetAddress()

if err := k.initBalances(ctx, addr, balance.Coins); err != nil {
panic(fmt.Errorf("error on setting balances %w", err))
for _, coin := range balance.Coins {
err := k.Balances.Set(ctx, collections.Join(addr, coin.Denom), coin.Amount)
if err != nil {
panic(err)
}
}

totalSupply = totalSupply.Add(balance.Coins...)
Expand Down
Loading

0 comments on commit 68a6890

Please sign in to comment.