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: add decoder store to ics27 simulations (backport #2158) #2163

Merged
merged 1 commit into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions modules/apps/27-interchain-accounts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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"
Expand Down Expand Up @@ -196,3 +197,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()
}
28 changes: 28 additions & 0 deletions modules/apps/27-interchain-accounts/simulation/decoder.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
}
59 changes: 59 additions & 0 deletions modules/apps/27-interchain-accounts/simulation/decoder_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}