forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ibc-transfer: sims updates (cosmos#7123)
* ibc-transfer: sims updates * params and genesis * sim tests * update module.go * remove dontcover * Update x/ibc-transfer/simulation/decoder.go Co-authored-by: colin axnér <[email protected]> Co-authored-by: colin axnér <[email protected]>
- Loading branch information
1 parent
a6defab
commit 0234ad3
Showing
12 changed files
with
233 additions
and
18 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
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,35 @@ | ||
package keeper | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/x/ibc-transfer/types" | ||
) | ||
|
||
// UnmarshalDenomTrace attempts to decode and return an DenomTrace object from | ||
// raw encoded bytes. | ||
func (k Keeper) UnmarshalDenomTrace(bz []byte) (types.DenomTrace, error) { | ||
var denomTrace types.DenomTrace | ||
if err := k.cdc.UnmarshalBinaryBare(bz, &denomTrace); err != nil { | ||
return types.DenomTrace{}, err | ||
} | ||
return denomTrace, nil | ||
} | ||
|
||
// MustUnmarshalDenomTrace attempts to decode and return an DenomTrace object from | ||
// raw encoded bytes. It panics on error. | ||
func (k Keeper) MustUnmarshalDenomTrace(bz []byte) types.DenomTrace { | ||
var denomTrace types.DenomTrace | ||
k.cdc.MustUnmarshalBinaryBare(bz, &denomTrace) | ||
return denomTrace | ||
} | ||
|
||
// MarshalDenomTrace attempts to encode an DenomTrace object and returns the | ||
// raw encoded bytes. | ||
func (k Keeper) MarshalDenomTrace(denomTrace types.DenomTrace) ([]byte, error) { | ||
return k.cdc.MarshalBinaryBare(&denomTrace) | ||
} | ||
|
||
// MustMarshalDenomTrace attempts to encode an DenomTrace object and returns the | ||
// raw encoded bytes. It panics on error. | ||
func (k Keeper) MustMarshalDenomTrace(denomTrace types.DenomTrace) []byte { | ||
return k.cdc.MustMarshalBinaryBare(&denomTrace) | ||
} |
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
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,33 @@ | ||
package simulation | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/types/kv" | ||
"github.com/cosmos/cosmos-sdk/x/ibc-transfer/types" | ||
) | ||
|
||
// TransferUnmarshaler defines the expected encoding store functions. | ||
type TransferUnmarshaler interface { | ||
MustUnmarshalDenomTrace([]byte) types.DenomTrace | ||
} | ||
|
||
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's | ||
// Value to the corresponding DenomTrace type. | ||
func NewDecodeStore(cdc TransferUnmarshaler) func(kvA, kvB kv.Pair) string { | ||
return func(kvA, kvB kv.Pair) string { | ||
switch { | ||
case bytes.Equal(kvA.Key[:1], types.PortKey): | ||
return fmt.Sprintf("Port A: %s\nPort B: %s", string(kvA.Value), string(kvB.Value)) | ||
|
||
case bytes.Equal(kvA.Key[:1], types.DenomTraceKey): | ||
denomTraceA := cdc.MustUnmarshalDenomTrace(kvA.Value) | ||
denomTraceB := cdc.MustUnmarshalDenomTrace(kvB.Value) | ||
return fmt.Sprintf("DenomTrace A: %s\nDenomTrace B: %s", denomTraceA.IBCDenom(), denomTraceB.IBCDenom()) | ||
|
||
default: | ||
panic(fmt.Sprintf("invalid %s key prefix %X", types.ModuleName, kvA.Key[:1])) | ||
} | ||
} | ||
} |
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/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp" | ||
"github.com/cosmos/cosmos-sdk/types/kv" | ||
"github.com/cosmos/cosmos-sdk/x/ibc-transfer/simulation" | ||
"github.com/cosmos/cosmos-sdk/x/ibc-transfer/types" | ||
) | ||
|
||
func TestDecodeStore(t *testing.T) { | ||
app := simapp.Setup(false) | ||
dec := simulation.NewDecodeStore(app.TransferKeeper) | ||
|
||
trace := types.DenomTrace{ | ||
BaseDenom: "uatom", | ||
Path: "transfer/channelToA", | ||
} | ||
|
||
kvPairs := kv.Pairs{ | ||
Pairs: []kv.Pair{ | ||
{ | ||
Key: types.PortKey, | ||
Value: []byte(types.PortID), | ||
}, | ||
{ | ||
Key: types.DenomTraceKey, | ||
Value: app.TransferKeeper.MustMarshalDenomTrace(trace), | ||
}, | ||
{ | ||
Key: []byte{0x99}, | ||
Value: []byte{0x99}, | ||
}, | ||
}, | ||
} | ||
tests := []struct { | ||
name string | ||
expectedLog string | ||
}{ | ||
{"PortID", fmt.Sprintf("Port A: %s\nPort B: %s", types.PortID, types.PortID)}, | ||
{"DenomTrace", fmt.Sprintf("DenomTrace A: %s\nDenomTrace B: %s", trace.IBCDenom(), trace.IBCDenom())}, | ||
{"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) | ||
} | ||
}) | ||
} | ||
} |
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
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,32 @@ | ||
package simulation | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
|
||
gogotypes "github.com/gogo/protobuf/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/x/simulation" | ||
|
||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation" | ||
"github.com/cosmos/cosmos-sdk/x/ibc-transfer/types" | ||
) | ||
|
||
// ParamChanges defines the parameters that can be modified by param change proposals | ||
// on the simulation | ||
func ParamChanges(r *rand.Rand) []simtypes.ParamChange { | ||
return []simtypes.ParamChange{ | ||
simulation.NewSimParamChange(types.ModuleName, string(types.KeySendEnabled), | ||
func(r *rand.Rand) string { | ||
sendEnabled := RadomEnabled(r) | ||
return fmt.Sprintf("%s", types.ModuleCdc.MustMarshalJSON(&gogotypes.BoolValue{Value: sendEnabled})) | ||
}, | ||
), | ||
simulation.NewSimParamChange(types.ModuleName, string(types.KeyReceiveEnabled), | ||
func(r *rand.Rand) string { | ||
receiveEnabled := RadomEnabled(r) | ||
return fmt.Sprintf("%s", types.ModuleCdc.MustMarshalJSON(&gogotypes.BoolValue{Value: receiveEnabled})) | ||
}, | ||
), | ||
} | ||
} |
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,36 @@ | ||
package simulation_test | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/x/ibc-transfer/simulation" | ||
) | ||
|
||
func TestParamChanges(t *testing.T) { | ||
s := rand.NewSource(1) | ||
r := rand.New(s) | ||
|
||
expected := []struct { | ||
composedKey string | ||
key string | ||
simValue string | ||
subspace string | ||
}{ | ||
{"transfer/SendEnabled", "SendEnabled", "false", "transfer"}, | ||
{"transfer/ReceiveEnabled", "ReceiveEnabled", "true", "transfer"}, | ||
} | ||
|
||
paramChanges := simulation.ParamChanges(r) | ||
|
||
require.Len(t, paramChanges, 2) | ||
|
||
for i, p := range paramChanges { | ||
require.Equal(t, expected[i].composedKey, p.ComposedKey()) | ||
require.Equal(t, expected[i].key, p.Key()) | ||
require.Equal(t, expected[i].simValue, p.SimValue()(r), p.Key()) | ||
require.Equal(t, expected[i].subspace, p.Subspace()) | ||
} | ||
} |