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(collections): add alternative value codec #16773

Merged
merged 7 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
cleanups
  • Loading branch information
unknown unknown committed Jun 29, 2023
commit 3507c75b498b29cd5be8f0ecb4327b4d42a3b965
2 changes: 1 addition & 1 deletion collections/codec/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (b boolKey[T]) Decode(buffer []byte) (int, T, error) {
}
}

func (b boolKey[T]) Size(key T) int { return 1 }
func (b boolKey[T]) Size(_ T) int { return 1 }

func (b boolKey[T]) EncodeJSON(value T) ([]byte, error) {
return json.Marshal(value)
Expand Down
5 changes: 5 additions & 0 deletions collections/colltest/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func TestKeyCodec[T any](t *testing.T, keyCodec codec.KeyCodec[T], key T) {
decoded, err := keyCodec.DecodeJSON(keyJSON)
require.NoError(t, err)
require.Equal(t, key, decoded, "json encoding and decoding did not produce the same results")

// check type
require.NotEmpty(t, keyCodec.KeyType())
// check string
_ = keyCodec.Stringify(key)
}

// TestValueCodec asserts the correct behavior of a ValueCodec over the type T.
Expand Down
28 changes: 27 additions & 1 deletion collections/indexes/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ import (
"cosmossdk.io/collections/codec"
)

type multiOptions struct {
uncheckedValue bool
}

// WithMultiUncheckedValue is an option that can be passed to NewMulti to
// ignore index values different from '[]byte{}' and continue with the operation.
// This should be used only to behave nicely in case you have used values different
// from '[]byte{}' in your storage before migrating to collections. Refer to
// WithKeySetUncheckedValue for more information.
func WithMultiUncheckedValue() func(*multiOptions) {
return func(o *multiOptions) {
o.uncheckedValue = true
}
}

// Multi defines the most common index. It can be used to create a reference between
// a field of value and its primary key. Multiple primary keys can be mapped to the same
// reference key as the index does not enforce uniqueness constraints.
Expand All @@ -27,10 +42,21 @@ func NewMulti[ReferenceKey, PrimaryKey, Value any](
refCodec codec.KeyCodec[ReferenceKey],
pkCodec codec.KeyCodec[PrimaryKey],
getRefKeyFunc func(pk PrimaryKey, value Value) (ReferenceKey, error),
options ...func(*multiOptions),
) *Multi[ReferenceKey, PrimaryKey, Value] {
ks := collections.NewKeySet(schema, prefix, name, collections.PairKeyCodec(refCodec, pkCodec))

o := new(multiOptions)
for _, opt := range options {
opt(o)
}
if o.uncheckedValue {
ks = collections.NewKeySet(schema, prefix, name, collections.PairKeyCodec(refCodec, pkCodec), collections.WithKeySetUncheckedValue())
}

return &Multi[ReferenceKey, PrimaryKey, Value]{
getRefKey: getRefKeyFunc,
refKeys: collections.NewKeySet(schema, prefix, name, collections.PairKeyCodec(refCodec, pkCodec)),
refKeys: ks,
}
}

Expand Down
49 changes: 49 additions & 0 deletions collections/indexes/multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,52 @@ func TestMultiIndex(t *testing.T) {
require.False(t, iter.Valid())
require.NoError(t, iter.Close())
}

func TestMultiUnchecked(t *testing.T) {
sk, ctx := deps()
schema := collections.NewSchemaBuilder(sk)

uncheckedMi := NewMulti(schema, collections.NewPrefix("prefix"), "multi_index", collections.StringKey, collections.Uint64Key, func(_ uint64, value company) (string, error) {
return value.City, nil
}, WithMultiUncheckedValue())

mi := NewMulti(schema, collections.NewPrefix("prefix"), "multi_index", collections.StringKey, collections.Uint64Key, func(_ uint64, value company) (string, error) {
return value.City, nil
})

rawKey, err := collections.EncodeKeyWithPrefix(
collections.NewPrefix("prefix"),
uncheckedMi.KeyCodec(),
collections.Join("milan", uint64(2)))
require.NoError(t, err)

// set value to be something different from []byte{}
require.NoError(t, sk.OpenKVStore(ctx).Set(rawKey, []byte("something")))

// normal multi index will fail.
err = mi.Walk(ctx, nil, func(indexingKey string, indexedKey uint64) (stop bool, err error) {
return true, err
})
require.ErrorIs(t, err, collections.ErrEncoding)

// unchecked multi index will not fail.
err = uncheckedMi.Walk(ctx, nil, func(indexingKey string, indexedKey uint64) (stop bool, err error) {
require.Equal(t, "milan", indexingKey)
require.Equal(t, uint64(2), indexedKey)
return true, err
})
require.NoError(t, err)

// unchecked multi will also reset the value
err = mi.Reference(ctx, 2, company{City: "milan"}, func() (company, error) {
return company{
City: "milan",
}, nil
})
require.NoError(t, err)

// value reset to []byte{}
rawValue, err := sk.OpenKVStore(ctx).Get(rawKey)
require.NoError(t, err)
require.Equal(t, []byte{}, rawValue)
}
29 changes: 27 additions & 2 deletions collections/indexes/reverse_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ import (
"cosmossdk.io/collections/codec"
)

type reversePairOptions struct {
uncheckedValue bool
}

// WithReversePairUncheckedValue is an option that can be passed to NewReversePair to
// ignore index values different from '[]byte{}' and continue with the operation.
// This should be used only if you are migrating to collections and have used a different
// placeholder value in your storage index keys.
// Refer to WithKeySetUncheckedValue for more information.
func WithReversePairUncheckedValue() func(*reversePairOptions) {
return func(o *reversePairOptions) {
o.uncheckedValue = true
}
}

// ReversePair is an index that is used with collections.Pair keys. It indexes objects by their second part of the key.
// When the value is being indexed by collections.IndexedMap then ReversePair will create a relationship between
// the second part of the primary key and the first part.
Expand All @@ -31,10 +46,20 @@ func NewReversePair[Value, K1, K2 any](
prefix collections.Prefix,
name string,
pairCodec codec.KeyCodec[collections.Pair[K1, K2]],
options ...func(*reversePairOptions),
) *ReversePair[K1, K2, Value] {
pkc := pairCodec.(pairKeyCodec[K1, K2])
ks := collections.NewKeySet(sb, prefix, name, collections.PairKeyCodec(pkc.KeyCodec2(), pkc.KeyCodec1()))
o := new(reversePairOptions)
for _, option := range options {
option(o)
}
if o.uncheckedValue {
ks = collections.NewKeySet(sb, prefix, name, collections.PairKeyCodec(pkc.KeyCodec2(), pkc.KeyCodec1()), collections.WithKeySetUncheckedValue())
}

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

return mi
Expand Down Expand Up @@ -67,7 +92,7 @@ func (i *ReversePair[K1, K2, Value]) Unreference(ctx context.Context, pk collect
func (i *ReversePair[K1, K2, Value]) Walk(
ctx context.Context,
ranger collections.Ranger[collections.Pair[K2, K1]],
walkFunc func(indexingKey K2, indexedKey K1) (stop bool, err error),
walkFunc func(indexedKey K2, indexingKey K1) (stop bool, err error),
) error {
return i.refKeys.Walk(ctx, ranger, func(key collections.Pair[K2, K1]) (bool, error) {
return walkFunc(key.K1(), key.K2())
Expand Down
30 changes: 30 additions & 0 deletions collections/indexes/reverse_pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,33 @@ func TestReversePair(t *testing.T) {
_, err = indexedMap.Indexes.Denom.MatchExact(ctx, "atom")
require.ErrorIs(t, collections.ErrInvalidIterator, err)
}

func TestUncheckedReversePair(t *testing.T) {
sk, ctx := deps()
sb := collections.NewSchemaBuilder(sk)
// we create an indexed map that maps balances, which are saved as
// key: Pair[Address, Denom]
// value: Amount
keyCodec := collections.PairKeyCodec(collections.StringKey, collections.StringKey)

uncheckedRp := NewReversePair[Amount](sb, collections.NewPrefix("denom_index"), "denom_index", keyCodec, WithReversePairUncheckedValue())
rp := NewReversePair[Amount](sb, collections.NewPrefix("denom_index"), "denom_index", keyCodec)

rawKey, err := collections.EncodeKeyWithPrefix(collections.NewPrefix("denom_index"), uncheckedRp.KeyCodec(), collections.Join("address1", "atom"))
require.NoError(t, err)

require.NoError(t, sk.OpenKVStore(ctx).Set(rawKey, []byte("i should not be here")))

// normal reverse pair fails
err = rp.Walk(ctx, nil, func(denom string, address string) (bool, error) {
return false, nil
})
require.ErrorIs(t, err, collections.ErrEncoding)

// unchecked reverse pair succeeds
err = uncheckedRp.Walk(ctx, nil, func(indexingKey string, indexedKey string) (stop bool, err error) {
t.Log(indexingKey, indexedKey)
return false, nil
})
require.NoError(t, err)
}
36 changes: 33 additions & 3 deletions collections/keyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,43 @@ import (
"cosmossdk.io/collections/codec"
)

// WithKeySetUncheckedValue changes the behaviour of the KeySet when it encounters
// a value different from '[]byte{}', by default the KeySet errors when this happens.
// This option allows to ignore the value and continue with the operation, in turn
// the value will be cleared out and set to '[]byte{}'.
// You should never use this option if you're creating a new state object from scratch.
// This should be used only to behave nicely in case you have used values different
// from '[]byte{}' in your storage before migrating to collections.
func WithKeySetUncheckedValue() func(opt *keySetOptions) {
return func(opt *keySetOptions) {
opt.uncheckedValue = true
}
}

type keySetOptions struct{ uncheckedValue 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.
type KeySet[K any] Map[K, NoValue]

// NewKeySet returns a KeySet given a Schema, Prefix a human name for the collection
// and a KeyCodec for the key K.
func NewKeySet[K any](schema *SchemaBuilder, prefix Prefix, name string, keyCodec codec.KeyCodec[K]) KeySet[K] {
return (KeySet[K])(NewMap(schema, prefix, name, keyCodec, noValueCodec))
func NewKeySet[K any](
schema *SchemaBuilder,
prefix Prefix,
name string,
keyCodec codec.KeyCodec[K],
options ...func(opt *keySetOptions),
) KeySet[K] {
o := new(keySetOptions)
for _, opt := range options {
opt(o)
}
vc := noValueCodec
if o.uncheckedValue {
vc = codec.NewAltValueCodec(vc, func(_ []byte) (NoValue, error) { return NoValue{}, nil })
}
return (KeySet[K])(NewMap(schema, prefix, name, keyCodec, vc))
}

// Set adds the key to the KeySet. Errors on encoding problems.
Expand Down Expand Up @@ -79,6 +108,7 @@ var noValueCodec codec.ValueCodec[NoValue] = NoValue{}

const noValueValueType = "no_value"

// NoValue is a type that can be used to represent a non-existing value.
type NoValue struct{}

func (n NoValue) EncodeJSON(_ NoValue) ([]byte, error) {
Expand All @@ -98,7 +128,7 @@ func (NoValue) Encode(_ NoValue) ([]byte, error) {

func (NoValue) Decode(b []byte) (NoValue, error) {
if !bytes.Equal(b, []byte{}) {
return NoValue{}, fmt.Errorf("%w: invalid value, wanted an empty non-nil byte slice", ErrEncoding)
return NoValue{}, fmt.Errorf("%w: invalid value, wanted an empty non-nil byte slice, got: %x", ErrEncoding, b)
}
return NoValue{}, nil
}
Expand Down
31 changes: 31 additions & 0 deletions collections/keyset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,34 @@ func Test_noValue(t *testing.T) {
_, err = noValueCodec.Decode([]byte("bad"))
require.ErrorIs(t, err, ErrEncoding)
}

func TestUncheckedKeySet(t *testing.T) {
sk, ctx := deps()
schema := NewSchemaBuilder(sk)
uncheckedKs := NewKeySet(schema, NewPrefix("keyset"), "keyset", StringKey, WithKeySetUncheckedValue())
ks := NewKeySet(schema, NewPrefix("keyset"), "keyset", StringKey)
// we set a NoValue unfriendly value.
require.NoError(t, sk.OpenKVStore(ctx).Set([]byte("keyset1"), []byte("A")))
require.NoError(t, sk.OpenKVStore(ctx).Set([]byte("keyset2"), []byte("B")))

// the standard KeySet errors here, because it doesn't like the fact that the value is []byte("A")
// and not []byte{}.
err := ks.Walk(ctx, nil, func(key string) (stop bool, err error) {
return true, nil
})
require.ErrorIs(t, err, ErrEncoding)

// the unchecked KeySet doesn't care about the value, so it works.
err = uncheckedKs.Walk(ctx, nil, func(key string) (stop bool, err error) {
require.Equal(t, "1", key)
return true, nil
})
require.NoError(t, err)

// now we set it again
require.NoError(t, uncheckedKs.Set(ctx, "1"))
// and we will see that the value which was []byte("A") has been cleared to be []byte{}
raw, err := sk.OpenKVStore(ctx).Get([]byte("keyset1"))
require.NoError(t, err)
require.Equal(t, []byte{}, raw)
}