-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add decoder store to ics27 simulations
- Loading branch information
1 parent
0a8602c
commit aab882d
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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
59
modules/apps/27-interchain-accounts/simulation/decoder_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |