From 21020740248bb7f4b27de22feb8137cc36eb799b Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Mon, 24 Jul 2023 17:46:54 +0200 Subject: [PATCH] feat(collections): implement `appmodule.HasGenesis` interface (#17021) --- collections/CHANGELOG.md | 8 ++++++-- collections/genesis.go | 8 ++++++++ collections/schema.go | 6 ++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/collections/CHANGELOG.md b/collections/CHANGELOG.md index 9b04bc86e9cd..291ec0b9a478 100644 --- a/collections/CHANGELOG.md +++ b/collections/CHANGELOG.md @@ -35,14 +35,18 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#17024](https://github.com/cosmos/cosmos-sdk/pull/17024) - Introduces `Triple`, a composite key with three keys. +### Improvements + +* [#17021](https://github.com/cosmos/cosmos-sdk/pull/17021) Make collections implement the `appmodule.HasGenesis` interface. + ## [v0.3.0](https://github.com/cosmos/cosmos-sdk/releases/tag/collections%2Fv0.3.0) ### Features * [#16074](https://github.com/cosmos/cosmos-sdk/pull/16607) - Introduces `Clear` method for `Map` and `KeySet` * [#16773](https://github.com/cosmos/cosmos-sdk/pull/16773) - * Adds `AltValueCodec` which provides a way to decode a value in two ways. - * Adds the possibility to specify an alternative way to decode the values of `KeySet`, `indexes.Multi`, `indexes.ReversePair`. + * Adds `AltValueCodec` which provides a way to decode a value in two ways. + * Adds the possibility to specify an alternative way to decode the values of `KeySet`, `indexes.Multi`, `indexes.ReversePair`. ## [v0.2.0](https://github.com/cosmos/cosmos-sdk/releases/tag/collections%2Fv0.2.0) diff --git a/collections/genesis.go b/collections/genesis.go index 0c593e4adfd8..c2db579b1126 100644 --- a/collections/genesis.go +++ b/collections/genesis.go @@ -3,6 +3,7 @@ package collections import ( "context" "encoding/json" + "errors" "fmt" "io" ) @@ -39,6 +40,13 @@ func (m Map[K, V]) exportGenesis(ctx context.Context, writer io.Writer) error { it, err := m.Iterate(ctx, nil) if err != nil { + // if the iterator is invalid, the state can be empty + // and we can just return an empty array. + if errors.Is(err, ErrInvalidIterator) { + _, err = io.WriteString(writer, "]") + return err + } + return err } defer it.Close() diff --git a/collections/schema.go b/collections/schema.go index e05058f144c1..8cc851166f61 100644 --- a/collections/schema.go +++ b/collections/schema.go @@ -161,6 +161,12 @@ func NewSchemaFromAccessor(accessor func(context.Context) store.KVStore) Schema } } +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (s Schema) IsOnePerModuleType() {} + +// IsAppModule implements the appmodule.AppModule interface. +func (s Schema) IsAppModule() {} + // DefaultGenesis implements the appmodule.HasGenesis.DefaultGenesis method. func (s Schema) DefaultGenesis(target appmodule.GenesisTarget) error { for _, name := range s.collectionsOrdered {