From daedc9f14c1d97f0061feec5de2713e820012756 Mon Sep 17 00:00:00 2001 From: codehans <94654388+codehans@users.noreply.github.com> Date: Wed, 12 Apr 2023 08:58:20 +0100 Subject: [PATCH] IBC mapping for tokenfactory denoms --- modules/apps/transfer/client/cli/tx.go | 2 +- modules/apps/transfer/keeper/keeper_test.go | 8 +- modules/apps/transfer/keeper/relay.go | 25 +++-- modules/apps/transfer/simulation/genesis.go | 12 ++- modules/apps/transfer/types/params.go | 7 +- modules/apps/transfer/types/params_legacy.go | 36 +++++++ .../apps/transfer/types/params_legacy_test.go | 15 +++ modules/apps/transfer/types/transfer.pb.go | 95 +++++++++++++++---- .../applications/transfer/v1/transfer.proto | 8 +- 9 files changed, 174 insertions(+), 34 deletions(-) create mode 100644 modules/apps/transfer/types/params_legacy_test.go diff --git a/modules/apps/transfer/client/cli/tx.go b/modules/apps/transfer/client/cli/tx.go index 7f2e0dc9852..e401a7888d9 100644 --- a/modules/apps/transfer/client/cli/tx.go +++ b/modules/apps/transfer/client/cli/tx.go @@ -56,7 +56,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/keeper_test.go b/modules/apps/transfer/keeper/keeper_test.go index 2e164f05c5b..3e24777341b 100644 --- a/modules/apps/transfer/keeper/keeper_test.go +++ b/modules/apps/transfer/keeper/keeper_test.go @@ -297,10 +297,10 @@ func (suite *KeeperTestSuite) TestParams() { expPass bool }{ // it is not possible to set invalid booleans - {"success: set params false-false", types.NewParams(false, false), true}, - {"success: set params false-true", types.NewParams(false, true), true}, - {"success: set params true-false", types.NewParams(true, false), true}, - {"success: set params true-true", types.NewParams(true, true), true}, + {"success: set params false-false", types.NewParams(false, false, "factory"), true}, + {"success: set params false-true", types.NewParams(false, true, "factory"), true}, + {"success: set params true-false", types.NewParams(true, false, "factory"), true}, + {"success: set params true-true", types.NewParams(true, true, "factory"), true}, } for _, tc := range testCases { diff --git a/modules/apps/transfer/keeper/relay.go b/modules/apps/transfer/keeper/relay.go index abe804d0718..9727944d923 100644 --- a/modules/apps/transfer/keeper/relay.go +++ b/modules/apps/transfer/keeper/relay.go @@ -110,6 +110,11 @@ func (k Keeper) sendTransfer( return 0, err } + prefix := k.GetParams(ctx).SlashPrefix + if prefix != "" && strings.HasPrefix(fullDenomPath, prefix+"/") { + fullDenomPath = strings.ReplaceAll(fullDenomPath, "/", ":") + } + } else { labels = append(labels, telemetry.NewLabel(coretypes.LabelSource, "false")) @@ -205,14 +210,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.IsNativeDenom() { - denom = denomTrace.IBCDenom() + var denom string + prefix := k.GetParams(ctx).SlashPrefix + 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 fded62be9a2..7f6382467d8 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( + 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 8707cfbc3de..c39c51879ee 100644 --- a/modules/apps/transfer/types/params.go +++ b/modules/apps/transfer/types/params.go @@ -5,17 +5,20 @@ const ( DefaultSendEnabled = true // DefaultReceiveEnabled enabled DefaultReceiveEnabled = true + // DefaultSlashPrefix factory + DefaultSlashPrefix = "factory" ) // 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) } diff --git a/modules/apps/transfer/types/params_legacy.go b/modules/apps/transfer/types/params_legacy.go index 716043c3040..20316e9f9ea 100644 --- a/modules/apps/transfer/types/params_legacy.go +++ b/modules/apps/transfer/types/params_legacy.go @@ -8,7 +8,9 @@ package types import ( "fmt" + "strings" + sdk "github.com/cosmos/cosmos-sdk/types" paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" ) @@ -17,6 +19,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 @@ -29,9 +33,23 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { return paramtypes.ParamSetPairs{ paramtypes.NewParamSetPair(KeySendEnabled, &p.SendEnabled, validateEnabledTypeLegacy), paramtypes.NewParamSetPair(KeyReceiveEnabled, &p.ReceiveEnabled, validateEnabledTypeLegacy), + paramtypes.NewParamSetPair(KeySlashPrefix, &p.SlashPrefix, validatePrefixLegacy), } } +// Validate all ibc-transfer module parameters +func (p Params) Validate() error { + if err := validateEnabledTypeLegacy(p.SendEnabled); err != nil { + return err + } + + if err := validatePrefixLegacy(p.SlashPrefix); err != nil { + return err + } + + return validateEnabledTypeLegacy(p.ReceiveEnabled) +} + func validateEnabledTypeLegacy(i interface{}) error { _, ok := i.(bool) if !ok { @@ -40,3 +58,21 @@ func validateEnabledTypeLegacy(i interface{}) error { return nil } + +func validatePrefixLegacy(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_legacy_test.go b/modules/apps/transfer/types/params_legacy_test.go new file mode 100644 index 00000000000..ba47efa10c1 --- /dev/null +++ b/modules/apps/transfer/types/params_legacy_test.go @@ -0,0 +1,15 @@ +package types_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types" +) + +func TestValidateParams(t *testing.T) { + require.NoError(t, types.DefaultParams().Validate()) + require.NoError(t, types.NewParams(true, false, "factory").Validate()) + require.Error(t, types.NewParams(true, false, "factory/").Validate()) +} diff --git a/modules/apps/transfer/types/transfer.pb.go b/modules/apps/transfer/types/transfer.pb.go index 99bfa07a587..432762cd601 100644 --- a/modules/apps/transfer/types/transfer.pb.go +++ b/modules/apps/transfer/types/transfer.pb.go @@ -89,7 +89,11 @@ type Params struct { SendEnabled bool `protobuf:"varint,1,opt,name=send_enabled,json=sendEnabled,proto3" json:"send_enabled,omitempty"` // 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"` + 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{} } @@ -139,6 +143,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") @@ -149,23 +160,28 @@ func init() { } var fileDescriptor_5041673e96e97901 = []byte{ - // 256 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x4c, 0x90, 0x3f, 0x4b, 0x03, 0x31, - 0x18, 0x87, 0x9b, 0x22, 0xc5, 0x46, 0x51, 0xc8, 0xd4, 0x41, 0x83, 0x76, 0x51, 0x10, 0x2f, 0x14, - 0x07, 0xdd, 0x04, 0xd1, 0x5d, 0x4b, 0x27, 0x97, 0x92, 0xe4, 0x5e, 0xdb, 0xc0, 0xe5, 0x0f, 0x79, - 0xd3, 0x03, 0xbf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0xc0, 0xae, 0xcd, 0x5e, 0x93, 0xf4, 0x19, 0x00, 0xd5, 0x28, - 0xe7, 0xb8, 0xfb, 0x0b, 0x00, 0x00, 0xff, 0xff, 0x24, 0x39, 0xe1, 0x5f, 0x3d, 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, + 0x0f, 0x85, 0x5e, 0x84, 0xaa, 0xf7, 0x5a, 0x3c, 0x79, 0x29, 0xbb, 0x9b, 0xd7, 0x74, 0x21, 0xc9, + 0x2e, 0xbb, 0xdb, 0x60, 0xff, 0xc2, 0x9f, 0x12, 0x3c, 0xf6, 0xe8, 0xa9, 0x48, 0xfb, 0x07, 0xfd, + 0x02, 0xd9, 0xad, 0x86, 0xe8, 0x6d, 0xe6, 0xcd, 0xcc, 0x63, 0x78, 0x0f, 0x5d, 0x73, 0xca, 0x30, + 0x91, 0x32, 0xe5, 0x8c, 0x18, 0x2e, 0x72, 0x8d, 0x8d, 0x22, 0xb9, 0x9e, 0x80, 0xc2, 0x45, 0xb7, + 0xc4, 0x91, 0x54, 0xc2, 0x08, 0xff, 0x9c, 0x53, 0x16, 0x55, 0xcd, 0x51, 0x69, 0x28, 0xba, 0xed, + 0x93, 0x44, 0x24, 0xc2, 0x19, 0xb1, 0x45, 0xdb, 0x4c, 0x78, 0x87, 0xd0, 0x03, 0xe4, 0x22, 0x7b, + 0x56, 0x84, 0x81, 0xef, 0xa3, 0x5d, 0x49, 0xcc, 0xb4, 0xe5, 0x5d, 0x7a, 0x57, 0xcd, 0x91, 0xc3, + 0xfe, 0x05, 0x42, 0x94, 0x68, 0x18, 0xc7, 0xd6, 0xd6, 0xaa, 0x3b, 0xa5, 0x69, 0x27, 0x2e, 0x17, + 0xbe, 0x7b, 0xa8, 0x31, 0x24, 0x8a, 0x64, 0xda, 0xef, 0xa3, 0x03, 0x0d, 0x79, 0x3c, 0x86, 0x9c, + 0xd0, 0x14, 0x62, 0xb7, 0x65, 0x6f, 0x70, 0xb6, 0x59, 0x76, 0x8e, 0xe7, 0x24, 0x4b, 0xfb, 0x61, + 0x55, 0x0d, 0x47, 0xfb, 0x96, 0x3e, 0x6e, 0x99, 0x7f, 0x8f, 0x8e, 0x14, 0x30, 0xe0, 0x05, 0x94, + 0xf1, 0xba, 0x8b, 0xb7, 0x37, 0xcb, 0xce, 0xe9, 0x36, 0xfe, 0xcf, 0x10, 0x8e, 0x0e, 0x7f, 0x26, + 0xbf, 0x4b, 0x6c, 0x81, 0x94, 0xe8, 0xe9, 0x58, 0x2a, 0x98, 0xf0, 0xd7, 0xd6, 0x8e, 0x2d, 0xfb, + 0xa7, 0x40, 0x45, 0xb5, 0x05, 0x2c, 0x1d, 0x3a, 0x36, 0x78, 0xfa, 0x58, 0x05, 0xde, 0x62, 0x15, + 0x78, 0x5f, 0xab, 0xc0, 0x7b, 0x5b, 0x07, 0xb5, 0xc5, 0x3a, 0xa8, 0x7d, 0xae, 0x83, 0xda, 0x4b, + 0x2f, 0xe1, 0x66, 0x3a, 0xa3, 0x11, 0x13, 0x19, 0x66, 0x42, 0x67, 0x42, 0x63, 0x4e, 0xd9, 0x4d, + 0x22, 0x70, 0xd1, 0xc3, 0x99, 0x88, 0x67, 0x29, 0x68, 0xfb, 0xa3, 0xca, 0x6f, 0xcc, 0x5c, 0x82, + 0xa6, 0x0d, 0x77, 0xe2, 0xdb, 0xef, 0x00, 0x00, 0x00, 0xff, 0xff, 0x43, 0xaa, 0x18, 0x4c, 0xc5, + 0x01, 0x00, 0x00, } func (m *DenomTrace) Marshal() (dAtA []byte, err error) { @@ -225,6 +241,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 { @@ -288,6 +311,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 } @@ -480,6 +507,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 7f77237621f..bf9abdd05ad 100644 --- a/proto/ibc/applications/transfer/v1/transfer.proto +++ b/proto/ibc/applications/transfer/v1/transfer.proto @@ -2,6 +2,8 @@ syntax = "proto3"; package ibc.applications.transfer.v1; +import "gogoproto/gogo.proto"; + option go_package = "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"; // DenomTrace contains the base denomination for ICS20 fungible tokens and the @@ -24,5 +26,9 @@ message Params { bool send_enabled = 1; // receive_enabled enables or disables all cross-chain token transfers to this // chain. - bool receive_enabled = 2; + 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\""]; }