From 3a9b253b75215f8bdca1ed86475eb7f8167d4a7d Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 31 Aug 2022 19:48:08 +0200 Subject: [PATCH] feat: add decoder store to ics27 simulations (#2158) (#2164) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description ref: #1352, #2151 --- Before we can merge this PR, please make sure that all the following items have been checked off. If any of the checklist items are not applicable, please leave them but write a little note why. - [ ] Targeted PR against correct branch (see [CONTRIBUTING.md](https://github.com/cosmos/ibc-go/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] Linked to Github issue with discussion and accepted design OR link to spec that describes this work. - [ ] Code follows the [module structure standards](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules/structure.md). - [ ] Wrote unit and integration [tests](https://github.com/cosmos/ibc-go/blob/master/CONTRIBUTING.md#testing) - [ ] Updated relevant documentation (`docs/`) or specification (`x//spec/`) - [ ] Added relevant `godoc` [comments](https://blog.golang.org/godoc-documenting-go-code). - [ ] Added a relevant changelog entry to the `Unreleased` section in `CHANGELOG.md` - [ ] Re-reviewed `Files changed` in the Github PR explorer - [ ] Review `Codecov Report` in the comment section below once CI passes (cherry picked from commit 23a7515cf51f1322d00b3ddec7ffe8833b7efffe) Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> --- modules/apps/27-interchain-accounts/module.go | 8 +++ .../simulation/decoder.go | 28 +++++++++ .../simulation/decoder_test.go | 59 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 modules/apps/27-interchain-accounts/simulation/decoder.go create mode 100644 modules/apps/27-interchain-accounts/simulation/decoder_test.go diff --git a/modules/apps/27-interchain-accounts/module.go b/modules/apps/27-interchain-accounts/module.go index c37c25c081b..fb33eccd500 100644 --- a/modules/apps/27-interchain-accounts/module.go +++ b/modules/apps/27-interchain-accounts/module.go @@ -21,6 +21,7 @@ import ( "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/host" hostkeeper "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/host/keeper" hosttypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/host/types" + "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/simulation" "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types" porttypes "github.com/cosmos/ibc-go/v5/modules/core/05-port/types" ibchost "github.com/cosmos/ibc-go/v5/modules/core/24-host" @@ -199,3 +200,10 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{} } + +// AppModuleSimulation functions + +// RegisterStoreDecoder registers a decoder for interchain accounts module's types +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[types.StoreKey] = simulation.NewDecodeStore() +} diff --git a/modules/apps/27-interchain-accounts/simulation/decoder.go b/modules/apps/27-interchain-accounts/simulation/decoder.go new file mode 100644 index 00000000000..706a4c3cbee --- /dev/null +++ b/modules/apps/27-interchain-accounts/simulation/decoder.go @@ -0,0 +1,28 @@ +package simulation + +import ( + "bytes" + "fmt" + + "github.com/cosmos/cosmos-sdk/types/kv" + + "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types" +) + +// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's +// Value to the corresponding DenomTrace type. +func NewDecodeStore() func(kvA, kvB kv.Pair) string { + return func(kvA, kvB kv.Pair) string { + switch { + case bytes.Equal(kvA.Key[:len(types.PortKeyPrefix)], []byte(types.PortKeyPrefix)): + return fmt.Sprintf("Port A: %s\nPort B: %s", string(kvA.Value), string(kvB.Value)) + case bytes.Equal(kvA.Key[:len(types.OwnerKeyPrefix)], []byte(types.OwnerKeyPrefix)): + return fmt.Sprintf("Owner A: %s\nOwner B: %s", string(kvA.Value), string(kvB.Value)) + case bytes.Equal(kvA.Key[:len(types.ActiveChannelKeyPrefix)], []byte(types.ActiveChannelKeyPrefix)): + return fmt.Sprintf("ActiveChannel A: %s\nActiveChannel B: %s", string(kvA.Value), string(kvB.Value)) + + default: + panic(fmt.Sprintf("invalid %s key prefix %s", types.ModuleName, kvA.Key)) + } + } +} diff --git a/modules/apps/27-interchain-accounts/simulation/decoder_test.go b/modules/apps/27-interchain-accounts/simulation/decoder_test.go new file mode 100644 index 00000000000..e46124920ad --- /dev/null +++ b/modules/apps/27-interchain-accounts/simulation/decoder_test.go @@ -0,0 +1,59 @@ +package simulation_test + +import ( + "fmt" + "testing" + + "github.com/cosmos/cosmos-sdk/types/kv" + "github.com/stretchr/testify/require" + + "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/simulation" + "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types" + ibctesting "github.com/cosmos/ibc-go/v5/testing" +) + +func TestDecodeStore(t *testing.T) { + var ( + owner = "owner" + channelID = ibctesting.FirstChannelID + ) + + dec := simulation.NewDecodeStore() + + kvPairs := kv.Pairs{ + Pairs: []kv.Pair{ + { + Key: []byte(types.PortKeyPrefix), + Value: []byte(types.PortID), + }, + { + Key: []byte(types.OwnerKeyPrefix), + Value: []byte("owner"), + }, + { + Key: []byte(types.ActiveChannelKeyPrefix), + Value: []byte("channel-0"), + }, + }, + } + tests := []struct { + name string + expectedLog string + }{ + {"PortID", fmt.Sprintf("Port A: %s\nPort B: %s", types.PortID, types.PortID)}, + {"Owner", fmt.Sprintf("Owner A: %s\nOwner B: %s", owner, owner)}, + {"ActiveChannel", fmt.Sprintf("ActiveChannel A: %s\nActiveChannel B: %s", channelID, channelID)}, + {"other", ""}, + } + + for i, tt := range tests { + i, tt := i, tt + t.Run(tt.name, func(t *testing.T) { + if i == len(tests)-1 { + require.Panics(t, func() { dec(kvPairs.Pairs[i], kvPairs.Pairs[i]) }, tt.name) + } else { + require.Equal(t, tt.expectedLog, dec(kvPairs.Pairs[i], kvPairs.Pairs[i]), tt.name) + } + }) + } +}