diff --git a/docs/ibc/proto-docs.md b/docs/ibc/proto-docs.md index c7f1b1e9652..88a9b9d261f 100644 --- a/docs/ibc/proto-docs.md +++ b/docs/ibc/proto-docs.md @@ -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 `:` | diff --git a/modules/apps/transfer/client/cli/tx.go b/modules/apps/transfer/client/cli/tx.go index 614f56f6a66..49d5e57fd53 100644 --- a/modules/apps/transfer/client/cli/tx.go +++ b/modules/apps/transfer/client/cli/tx.go @@ -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() } diff --git a/modules/apps/transfer/keeper/params.go b/modules/apps/transfer/keeper/params.go index e618316cfb1..065bc68f3ab 100644 --- a/modules/apps/transfer/keeper/params.go +++ b/modules/apps/transfer/keeper/params.go @@ -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. diff --git a/modules/apps/transfer/keeper/relay.go b/modules/apps/transfer/keeper/relay.go index 0ed2fe77ccc..001e2ff59c8 100644 --- a/modules/apps/transfer/keeper/relay.go +++ b/modules/apps/transfer/keeper/relay.go @@ -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")) @@ -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) { diff --git a/modules/apps/transfer/simulation/genesis.go b/modules/apps/transfer/simulation/genesis.go index bd097eff49d..95cfd226b7b 100644 --- a/modules/apps/transfer/simulation/genesis.go +++ b/modules/apps/transfer/simulation/genesis.go @@ -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 @@ -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, "", " ") diff --git a/modules/apps/transfer/types/params.go b/modules/apps/transfer/types/params.go index 4ecdfab77e7..7a5001bc2e0 100644 --- a/modules/apps/transfer/types/params.go +++ b/modules/apps/transfer/types/params.go @@ -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" ) @@ -11,6 +13,8 @@ const ( DefaultSendEnabled = true // DefaultReceiveEnabled enabled DefaultReceiveEnabled = true + // DefaultSlashPrefix factory + DefaultSlashPrefix = "factory" ) var ( @@ -18,6 +22,8 @@ var ( 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 @@ -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 @@ -44,6 +51,10 @@ func (p Params) Validate() error { return err } + if err := validatePrefix(p.SlashPrefix); err != nil { + return err + } + return validateEnabled(p.ReceiveEnabled) } @@ -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), } } @@ -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 +} diff --git a/modules/apps/transfer/types/params_test.go b/modules/apps/transfer/types/params_test.go index 825efb825c1..a1d2e5bdb96 100644 --- a/modules/apps/transfer/types/params_test.go +++ b/modules/apps/transfer/types/params_test.go @@ -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()) } diff --git a/modules/apps/transfer/types/transfer.pb.go b/modules/apps/transfer/types/transfer.pb.go index 02d3d8e7e02..5e8157bc133 100644 --- a/modules/apps/transfer/types/transfer.pb.go +++ b/modules/apps/transfer/types/transfer.pb.go @@ -91,6 +91,10 @@ type Params struct { // receive_enabled enables or disables all cross-chain token transfers to this // chain. ReceiveEnabled bool `protobuf:"varint,2,opt,name=receive_enabled,json=receiveEnabled,proto3" json:"receive_enabled,omitempty" 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 `:` + SlashPrefix string `protobuf:"bytes,3,opt,name=slash_prefix,json=slashPrefix,proto3" json:"slash_prefix,omitempty" yaml:"slash_prefix"` } func (m *Params) Reset() { *m = Params{} } @@ -140,6 +144,13 @@ func (m *Params) GetReceiveEnabled() bool { return false } +func (m *Params) GetSlashPrefix() string { + if m != nil { + return m.SlashPrefix + } + return "" +} + func init() { proto.RegisterType((*DenomTrace)(nil), "ibc.applications.transfer.v1.DenomTrace") proto.RegisterType((*Params)(nil), "ibc.applications.transfer.v1.Params") @@ -150,26 +161,28 @@ func init() { } var fileDescriptor_5041673e96e97901 = []byte{ - // 300 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x5c, 0x90, 0xc1, 0x4a, 0x2b, 0x31, - 0x14, 0x86, 0x9b, 0x72, 0x29, 0xb7, 0x51, 0x14, 0xa2, 0x68, 0x29, 0x9a, 0xca, 0xac, 0x04, 0x71, - 0x42, 0x11, 0x11, 0xba, 0x11, 0xaa, 0xee, 0xb5, 0xb8, 0x72, 0x53, 0x92, 0xcc, 0x71, 0x1a, 0x98, - 0x4c, 0x86, 0x24, 0x1d, 0xe8, 0x23, 0xb8, 0xf3, 0xb1, 0x5c, 0x76, 0xe9, 0xaa, 0x48, 0xfb, 0x06, - 0x7d, 0x02, 0x99, 0xb4, 0x94, 0xc1, 0xdd, 0x7f, 0xce, 0xf9, 0xbe, 0xb3, 0xf8, 0xf1, 0x95, 0x12, - 0x92, 0xf1, 0xa2, 0xc8, 0x94, 0xe4, 0x5e, 0x99, 0xdc, 0x31, 0x6f, 0x79, 0xee, 0xde, 0xc1, 0xb2, - 0xb2, 0xbf, 0xcb, 0x71, 0x61, 0x8d, 0x37, 0xe4, 0x4c, 0x09, 0x19, 0xd7, 0xe1, 0x78, 0x07, 0x94, - 0xfd, 0xee, 0x71, 0x6a, 0x52, 0x13, 0x40, 0x56, 0xa5, 0x8d, 0x13, 0xdd, 0x63, 0xfc, 0x08, 0xb9, - 0xd1, 0xaf, 0x96, 0x4b, 0x20, 0x04, 0xff, 0x2b, 0xb8, 0x9f, 0x74, 0xd0, 0x05, 0xba, 0x6c, 0x8f, - 0x42, 0x26, 0xe7, 0x18, 0x0b, 0xee, 0x60, 0x9c, 0x54, 0x58, 0xa7, 0x19, 0x2e, 0xed, 0x6a, 0x13, - 0xbc, 0xe8, 0x03, 0xe1, 0xd6, 0x33, 0xb7, 0x5c, 0x3b, 0x32, 0xc0, 0xfb, 0x0e, 0xf2, 0x64, 0x0c, - 0x39, 0x17, 0x19, 0x24, 0xe1, 0xcb, 0xff, 0xe1, 0xe9, 0x7a, 0xd1, 0x3b, 0x9a, 0x71, 0x9d, 0x0d, - 0xa2, 0xfa, 0x35, 0x1a, 0xed, 0x55, 0xe3, 0xd3, 0x66, 0x22, 0x0f, 0xf8, 0xd0, 0x82, 0x04, 0x55, - 0xc2, 0x4e, 0x6f, 0x06, 0xbd, 0xbb, 0x5e, 0xf4, 0x4e, 0x36, 0xfa, 0x1f, 0x20, 0x1a, 0x1d, 0x6c, - 0x37, 0xdb, 0x27, 0xc3, 0x97, 0xaf, 0x25, 0x45, 0xf3, 0x25, 0x45, 0x3f, 0x4b, 0x8a, 0x3e, 0x57, - 0xb4, 0x31, 0x5f, 0xd1, 0xc6, 0xf7, 0x8a, 0x36, 0xde, 0xee, 0x52, 0xe5, 0x27, 0x53, 0x11, 0x4b, - 0xa3, 0x99, 0x34, 0x4e, 0x1b, 0xc7, 0x94, 0x90, 0xd7, 0xa9, 0x61, 0xe5, 0x2d, 0xd3, 0x26, 0x99, - 0x66, 0xe0, 0xaa, 0x9e, 0x6b, 0xfd, 0xfa, 0x59, 0x01, 0x4e, 0xb4, 0x42, 0x4d, 0x37, 0xbf, 0x01, - 0x00, 0x00, 0xff, 0xff, 0x6e, 0xa1, 0x51, 0x3a, 0x89, 0x01, 0x00, 0x00, + // 323 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x5c, 0x51, 0xc1, 0x4a, 0xc3, 0x40, + 0x14, 0x6c, 0xaa, 0x14, 0xbb, 0x8a, 0x42, 0x14, 0x2d, 0x45, 0x53, 0xc9, 0x49, 0x10, 0xb3, 0x14, + 0x11, 0xa1, 0x17, 0xa1, 0xea, 0xbd, 0x16, 0x4f, 0x5e, 0xca, 0xee, 0xe6, 0x35, 0x5d, 0x48, 0xb2, + 0xcb, 0xee, 0x36, 0xd8, 0xbf, 0xf0, 0xa7, 0x04, 0x8f, 0x3d, 0x7a, 0x2a, 0xd2, 0xfe, 0x41, 0xbf, + 0x40, 0x76, 0xab, 0x21, 0x7a, 0x9b, 0x79, 0x33, 0xf3, 0x18, 0xde, 0x43, 0x97, 0x9c, 0x32, 0x4c, + 0xa4, 0x4c, 0x39, 0x23, 0x86, 0x8b, 0x5c, 0x63, 0xa3, 0x48, 0xae, 0xc7, 0xa0, 0x70, 0xd1, 0x2d, + 0x71, 0x24, 0x95, 0x30, 0xc2, 0x3f, 0xe5, 0x94, 0x45, 0x55, 0x73, 0x54, 0x1a, 0x8a, 0x6e, 0xfb, + 0x28, 0x11, 0x89, 0x70, 0x46, 0x6c, 0xd1, 0x26, 0x13, 0xde, 0x21, 0xf4, 0x00, 0xb9, 0xc8, 0x9e, + 0x15, 0x61, 0xe0, 0xfb, 0x68, 0x5b, 0x12, 0x33, 0x69, 0x79, 0xe7, 0xde, 0x45, 0x73, 0xe8, 0xb0, + 0x7f, 0x86, 0x10, 0x25, 0x1a, 0x46, 0xb1, 0xb5, 0xb5, 0xea, 0x4e, 0x69, 0xda, 0x89, 0xcb, 0x85, + 0xef, 0x1e, 0x6a, 0x0c, 0x88, 0x22, 0x99, 0xf6, 0x7b, 0x68, 0x4f, 0x43, 0x1e, 0x8f, 0x20, 0x27, + 0x34, 0x85, 0xd8, 0x6d, 0xd9, 0xe9, 0x9f, 0xac, 0x17, 0x9d, 0xc3, 0x19, 0xc9, 0xd2, 0x5e, 0x58, + 0x55, 0xc3, 0xe1, 0xae, 0xa5, 0x8f, 0x1b, 0xe6, 0xdf, 0xa3, 0x03, 0x05, 0x0c, 0x78, 0x01, 0x65, + 0xbc, 0xee, 0xe2, 0xed, 0xf5, 0xa2, 0x73, 0xbc, 0x89, 0xff, 0x33, 0x84, 0xc3, 0xfd, 0x9f, 0xc9, + 0xef, 0x12, 0x5b, 0x20, 0x25, 0x7a, 0x32, 0x92, 0x0a, 0xc6, 0xfc, 0xb5, 0xb5, 0x65, 0xcb, 0xfe, + 0x29, 0x50, 0x51, 0x6d, 0x01, 0x4b, 0x07, 0x8e, 0xf5, 0x9f, 0x3e, 0x96, 0x81, 0x37, 0x5f, 0x06, + 0xde, 0xd7, 0x32, 0xf0, 0xde, 0x56, 0x41, 0x6d, 0xbe, 0x0a, 0x6a, 0x9f, 0xab, 0xa0, 0xf6, 0x72, + 0x9b, 0x70, 0x33, 0x99, 0xd2, 0x88, 0x89, 0x0c, 0x33, 0xa1, 0x33, 0xa1, 0x31, 0xa7, 0xec, 0x2a, + 0x11, 0xb8, 0xb8, 0xc1, 0x99, 0x88, 0xa7, 0x29, 0x68, 0xfb, 0xa3, 0xca, 0x6f, 0xcc, 0x4c, 0x82, + 0xa6, 0x0d, 0x77, 0xe2, 0xeb, 0xef, 0x00, 0x00, 0x00, 0xff, 0xff, 0x3c, 0x82, 0x89, 0x96, 0xc5, + 0x01, 0x00, 0x00, } func (m *DenomTrace) Marshal() (dAtA []byte, err error) { @@ -229,6 +242,13 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.SlashPrefix) > 0 { + i -= len(m.SlashPrefix) + copy(dAtA[i:], m.SlashPrefix) + i = encodeVarintTransfer(dAtA, i, uint64(len(m.SlashPrefix))) + i-- + dAtA[i] = 0x1a + } if m.ReceiveEnabled { i-- if m.ReceiveEnabled { @@ -292,6 +312,10 @@ func (m *Params) Size() (n int) { if m.ReceiveEnabled { n += 2 } + l = len(m.SlashPrefix) + if l > 0 { + n += 1 + l + sovTransfer(uint64(l)) + } return n } @@ -484,6 +508,38 @@ func (m *Params) Unmarshal(dAtA []byte) error { } } m.ReceiveEnabled = bool(v != 0) + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field SlashPrefix", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTransfer + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTransfer + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTransfer + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.SlashPrefix = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTransfer(dAtA[iNdEx:]) diff --git a/proto/ibc/applications/transfer/v1/transfer.proto b/proto/ibc/applications/transfer/v1/transfer.proto index 4c51c2b9e1c..c7e320c3903 100644 --- a/proto/ibc/applications/transfer/v1/transfer.proto +++ b/proto/ibc/applications/transfer/v1/transfer.proto @@ -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\""]; }