Skip to content

Commit

Permalink
WIP: handle factory/ tokens
Browse files Browse the repository at this point in the history
receive fixes

change separator

prefixfix

skip receive ParseDenomTrace

CLI factory/ support

Move prefix config to params

fix validation

bump ConsensusVersion
  • Loading branch information
codehans committed Jan 31, 2023
1 parent c0acd5b commit 65873a5
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 33 deletions.
1 change: 1 addition & 0 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,7 @@ parameter for the denomination to false.
| ----- | ---- | ----- | ----------- |
| `send_enabled` | [bool](#bool) | | send_enabled enables or disables all cross-chain token transfers from this chain. |
| `receive_enabled` | [bool](#bool) | | receive_enabled enables or disables all cross-chain token transfers to this chain. |
| `slash_prefix` | [string](#string) | | In order to provide backward compatibility with IBC transfers to other IBCv3 chains, we can provide a prefix for denoms containing `/` so that they are escaped on send and receive with `:` |



Expand Down
2 changes: 1 addition & 1 deletion modules/apps/transfer/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ corresponding to the counterparty channel. Any timeout set to 0 is disabled.`),
return err
}

if !strings.HasPrefix(coin.Denom, "ibc/") {
if !strings.HasPrefix(coin.Denom, "ibc/") && !strings.HasPrefix(coin.Denom, "factory/") {
denomTrace := types.ParseDenomTrace(coin.Denom)
coin.Denom = denomTrace.IBCDenom()
}
Expand Down
9 changes: 8 additions & 1 deletion modules/apps/transfer/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ func (k Keeper) GetReceiveEnabled(ctx sdk.Context) bool {
return res
}

// GetSlashPrefix retrieves the slash prefix from the paramstore
func (k Keeper) GetSlashPrefix(ctx sdk.Context) string {
var res string
k.paramSpace.Get(ctx, types.KeySlashPrefix, &res)
return res
}

// GetParams returns the total set of ibc-transfer parameters.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(k.GetSendEnabled(ctx), k.GetReceiveEnabled(ctx))
return types.NewParams(k.GetSendEnabled(ctx), k.GetReceiveEnabled(ctx), k.GetSlashPrefix(ctx))
}

// SetParams sets the total set of ibc-transfer parameters.
Expand Down
24 changes: 17 additions & 7 deletions modules/apps/transfer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func (k Keeper) sendTransfer(
); err != nil {
return 0, err
}
prefix := k.GetSlashPrefix(ctx)
if prefix != "" && strings.HasPrefix(fullDenomPath, prefix+"/") {
fullDenomPath = strings.ReplaceAll(fullDenomPath, "/", ":")
}

} else {
labels = append(labels, telemetry.NewLabel(coretypes.LabelSource, "false"))
Expand Down Expand Up @@ -265,14 +269,20 @@ func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, data t
unprefixedDenom := data.Denom[len(voucherPrefix):]

// coin denomination used in sending from the escrow address
denom := unprefixedDenom

// The denomination used to send the coins is either the native denom or the hash of the path
// if the denomination is not native.
denomTrace := types.ParseDenomTrace(unprefixedDenom)
if denomTrace.Path != "" {
denom = denomTrace.IBCDenom()
var denom string
prefix := k.GetSlashPrefix(ctx)
if prefix != "" && strings.HasPrefix(unprefixedDenom, prefix+":") {
denom = strings.ReplaceAll(unprefixedDenom, ":", "/")
} else {
denom = unprefixedDenom
// The denomination used to send the coins is either the native denom or the hash of the path
// if the denomination is not native.
denomTrace := types.ParseDenomTrace(unprefixedDenom)
if denomTrace.Path != "" {
denom = denomTrace.IBCDenom()
}
}

token := sdk.NewCoin(denom, transferAmount)

if k.bankKeeper.BlockedAddr(receiver) {
Expand Down
12 changes: 11 additions & 1 deletion modules/apps/transfer/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func RadomEnabled(r *rand.Rand) bool {
return r.Int63n(101) <= 75
}

func RandomPrefix(r *rand.Rand) string {
return simtypes.RandStringOfLength(r, 10)
}

// RandomizedGenState generates a random GenesisState for transfer.
func RandomizedGenState(simState *module.SimulationState) {
var portID string
Expand All @@ -40,10 +44,16 @@ func RandomizedGenState(simState *module.SimulationState) {
func(r *rand.Rand) { receiveEnabled = RadomEnabled(r) },
)

var slashPrefix string
simState.AppParams.GetOrGenerate(
simState.Cdc, string(types.KeySlashPrefix), &slashPrefix, simState.Rand,
func(r *rand.Rand) { slashPrefix = RandomPrefix(r) },
)

transferGenesis := types.GenesisState{
PortId: portID,
DenomTraces: types.Traces{},
Params: types.NewParams(sendEnabled, receiveEnabled),
Params: types.NewParams(sendEnabled, receiveEnabled, slashPrefix),
}

bz, err := json.MarshalIndent(&transferGenesis, "", " ")
Expand Down
34 changes: 32 additions & 2 deletions modules/apps/transfer/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package types

import (
"fmt"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

Expand All @@ -11,13 +13,17 @@ const (
DefaultSendEnabled = true
// DefaultReceiveEnabled enabled
DefaultReceiveEnabled = true
// DefaultSlashPrefix factory
DefaultSlashPrefix = "factory"
)

var (
// KeySendEnabled is store's key for SendEnabled Params
KeySendEnabled = []byte("SendEnabled")
// KeyReceiveEnabled is store's key for ReceiveEnabled Params
KeyReceiveEnabled = []byte("ReceiveEnabled")
// KeySlashPrefix is store's key for SlashPrefix Params
KeySlashPrefix = []byte("SlashPrefix")
)

// ParamKeyTable type declaration for parameters
Expand All @@ -26,16 +32,17 @@ func ParamKeyTable() paramtypes.KeyTable {
}

// NewParams creates a new parameter configuration for the ibc transfer module
func NewParams(enableSend, enableReceive bool) Params {
func NewParams(enableSend, enableReceive bool, slashPrefix string) Params {
return Params{
SendEnabled: enableSend,
ReceiveEnabled: enableReceive,
SlashPrefix: slashPrefix,
}
}

// DefaultParams is the default parameter configuration for the ibc-transfer module
func DefaultParams() Params {
return NewParams(DefaultSendEnabled, DefaultReceiveEnabled)
return NewParams(DefaultSendEnabled, DefaultReceiveEnabled, DefaultSlashPrefix)
}

// Validate all ibc-transfer module parameters
Expand All @@ -44,6 +51,10 @@ func (p Params) Validate() error {
return err
}

if err := validatePrefix(p.SlashPrefix); err != nil {
return err
}

return validateEnabled(p.ReceiveEnabled)
}

Expand All @@ -52,6 +63,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeySendEnabled, p.SendEnabled, validateEnabled),
paramtypes.NewParamSetPair(KeyReceiveEnabled, p.ReceiveEnabled, validateEnabled),
paramtypes.NewParamSetPair(KeySlashPrefix, p.SlashPrefix, validatePrefix),
}
}

Expand All @@ -63,3 +75,21 @@ func validateEnabled(i interface{}) error {

return nil
}

func validatePrefix(i interface{}) error {
p, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

err := sdk.ValidateDenom(p)
if err != nil {
return err
}

if strings.Contains(p, "/") {
return fmt.Errorf("invalid parameter type: %T", i)
}

return nil
}
3 changes: 2 additions & 1 deletion modules/apps/transfer/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import (

func TestValidateParams(t *testing.T) {
require.NoError(t, DefaultParams().Validate())
require.NoError(t, NewParams(true, false).Validate())
require.NoError(t, NewParams(true, false, "factory").Validate())
require.Error(t, NewParams(true, false, "factory/").Validate())
}
96 changes: 76 additions & 20 deletions modules/apps/transfer/types/transfer.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions proto/ibc/applications/transfer/v1/transfer.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ message Params {
// receive_enabled enables or disables all cross-chain token transfers to this
// chain.
bool receive_enabled = 2 [(gogoproto.moretags) = "yaml:\"receive_enabled\""];
// In order to provide backward compatibility with IBC transfers to other
// IBCv3 chains, we can provide a prefix for denoms containing `/` so that they
// are escaped on send and receive with `:`
string slash_prefix = 3 [(gogoproto.moretags) = "yaml:\"slash_prefix\""];
}

0 comments on commit 65873a5

Please sign in to comment.