Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cool-develope committed Oct 31, 2024
1 parent 2d7f0b6 commit b6c6da7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
4 changes: 2 additions & 2 deletions collections/indexes/reverse_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func NewReversePair[Value, K1, K2 any](
}
if o.uncheckedValue {
return &ReversePair[K1, K2, Value]{
refKeys: collections.NewKeySet(sb, prefix, name, collections.PairKeyCodec(pkc.KeyCodec2(), pkc.KeyCodec1()), collections.WithKeySetUncheckedValue()),
refKeys: collections.NewKeySet(sb, prefix, name, collections.PairKeyCodec(pkc.KeyCodec2(), pkc.KeyCodec1()), collections.WithKeySetUncheckedValue(), collections.WithKeySetSecondaryIndex()),
}
}

mi := &ReversePair[K1, K2, Value]{
refKeys: collections.NewKeySet(sb, prefix, name, collections.PairKeyCodec(pkc.KeyCodec2(), pkc.KeyCodec1())),
refKeys: collections.NewKeySet(sb, prefix, name, collections.PairKeyCodec(pkc.KeyCodec2(), pkc.KeyCodec1()), collections.WithKeySetSecondaryIndex()),
}

return mi
Expand Down
14 changes: 12 additions & 2 deletions collections/keyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ func WithKeySetUncheckedValue() func(opt *keySetOptions) {
}
}

type keySetOptions struct{ uncheckedValue bool }
// WithKeySetSecondaryIndex changes the behavior of the KeySet to be a secondary index.
func WithKeySetSecondaryIndex() func(opt *keySetOptions) {
return func(opt *keySetOptions) {
opt.isSecondaryIndex = true
}
}

type keySetOptions struct {
uncheckedValue bool
isSecondaryIndex bool
}

// KeySet builds on top of a Map and represents a collection retaining only a set
// of keys and no value. It can be used, for example, in an allow list.
Expand All @@ -44,7 +54,7 @@ func NewKeySet[K any](
if o.uncheckedValue {
vc = codec.NewAltValueCodec(vc, func(_ []byte) (NoValue, error) { return NoValue{}, nil })
}
return (KeySet[K])(NewMap(schema, prefix, name, keyCodec, vc))
return (KeySet[K])(NewMap(schema, prefix, name, keyCodec, vc, WithMapSecondaryIndex(o.isSecondaryIndex)))

This comment has been minimized.

Copy link
@aaronc

aaronc Oct 31, 2024

Member

Does this need to be public? Would it be ever be used by a developer outside of collections?

}

// Set adds the key to the KeySet. Errors on encoding problems.
Expand Down
27 changes: 22 additions & 5 deletions collections/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ type Map[K, V any] struct {
isSecondaryIndex bool
}

// WithMapSecondaryIndex changes the behavior of the Map to be a secondary index.
func WithMapSecondaryIndex(isSecondaryIndex bool) func(opt *mapOptions) {
return func(opt *mapOptions) {
opt.isSecondaryIndex = isSecondaryIndex
}
}

type mapOptions struct {
isSecondaryIndex bool
}

// NewMap returns a Map given a StoreKey, a Prefix, human-readable name and the relative value and key encoders.
// Name and prefix must be unique within the schema and name must match the format specified by NameRegex, or
// else this method will panic.
Expand All @@ -36,13 +47,19 @@ func NewMap[K, V any](
name string,
keyCodec codec.KeyCodec[K],
valueCodec codec.ValueCodec[V],
options ...func(opt *mapOptions),
) Map[K, V] {
o := new(mapOptions)
for _, opt := range options {
opt(o)
}
m := Map[K, V]{
kc: keyCodec,
vc: valueCodec,
sa: schemaBuilder.schema.storeAccessor,
prefix: prefix.Bytes(),
name: name,
kc: keyCodec,
vc: valueCodec,
sa: schemaBuilder.schema.storeAccessor,
prefix: prefix.Bytes(),
name: name,
isSecondaryIndex: o.isSecondaryIndex,
}
schemaBuilder.addCollection(collectionImpl[K, V]{m})
return m
Expand Down
7 changes: 0 additions & 7 deletions x/bank/keeper/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"cosmossdk.io/core/appmodule"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
"cosmossdk.io/schema"
"cosmossdk.io/x/bank/types"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -257,9 +256,3 @@ func (k BaseViewKeeper) ValidateBalance(ctx context.Context, addr sdk.AccAddress

return nil
}

// ModuleCodec implements `schema.HasModuleCodec` interface.
// It allows the indexer to decode the module's KVPairUpdate.
func (k BaseViewKeeper) ModuleCodec() (schema.ModuleCodec, error) {
return k.Schema.ModuleCodec(collections.IndexingOptions{})
}
8 changes: 8 additions & 0 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"github.com/spf13/cobra"
"google.golang.org/grpc"

"cosmossdk.io/collections"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/registry"
"cosmossdk.io/schema"
"cosmossdk.io/x/bank/client/cli"
"cosmossdk.io/x/bank/keeper"
"cosmossdk.io/x/bank/simulation"
Expand Down Expand Up @@ -175,3 +177,9 @@ func (am AppModule) WeightedOperationsX(weights simsx.WeightSource, reg simsx.Re
reg.Add(weights.Get("msg_send", 100), simulation.MsgSendFactory())
reg.Add(weights.Get("msg_multisend", 10), simulation.MsgMultiSendFactory())
}

// ModuleCodec implements `schema.HasModuleCodec` interface.
// It allows the indexer to decode the module's KVPairUpdate.
func (am AppModule) ModuleCodec() (schema.ModuleCodec, error) {
return am.keeper.(keeper.BaseKeeper).Schema.ModuleCodec(collections.IndexingOptions{})
}

0 comments on commit b6c6da7

Please sign in to comment.