From 7f68d51de1645f7cb529259a149a3d9f0733c0fd Mon Sep 17 00:00:00 2001 From: codehans <94654388+codehans@users.noreply.github.com> Date: Wed, 14 Sep 2022 14:26:41 +0100 Subject: [PATCH] WIP: handle factory/ tokens receive fixes change separator prefixfix skip receive ParseDenomTrace CLI factory/ support Move prefix config to params fix validation bump ConsensusVersion --- docs/ibc/proto-docs.md | 1 + modules/apps/transfer/client/cli/tx.go | 2 +- modules/apps/transfer/keeper/params.go | 9 +- modules/apps/transfer/keeper/relay.go | 24 +++-- modules/apps/transfer/simulation/genesis.go | 12 ++- modules/apps/transfer/types/params.go | 34 ++++++- modules/apps/transfer/types/params_test.go | 3 +- modules/apps/transfer/types/transfer.pb.go | 96 +++++++++++++++---- .../applications/transfer/v1/transfer.proto | 4 + 9 files changed, 152 insertions(+), 33 deletions(-) diff --git a/docs/ibc/proto-docs.md b/docs/ibc/proto-docs.md index c41d56fdc99..3493c4e9f66 100644 --- a/docs/ibc/proto-docs.md +++ b/docs/ibc/proto-docs.md @@ -544,6 +544,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 817405b98ec..fc4b13cdb2e 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 b88a1b93b69..b9f3a0c857f 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 6fcd135c86a..dfa6a814bdf 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 a74277d1671..d5466548a54 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 5ed7c9bd835..653c2aad3f4 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, 0xe9, 0x42, 0xe8, 0x46, 0xa8, 0xba, 0xd7, 0xe2, 0xca, 0x4d, 0x49, 0x32, 0xc7, 0x69, 0x60, - 0x32, 0x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 0x56, 0x14, 0xfd, 0xac, 0x28, 0xfa, 0x5c, - 0xd3, 0xc6, 0x62, 0x4d, 0x1b, 0xdf, 0x6b, 0xda, 0x78, 0xbb, 0x4b, 0x95, 0x9f, 0xce, 0x44, 0x2c, - 0x8d, 0x66, 0xd2, 0x38, 0x6d, 0x1c, 0x53, 0x42, 0xde, 0xa6, 0x86, 0x95, 0x03, 0xa6, 0x4d, 0x32, - 0xcb, 0xc0, 0x55, 0x3d, 0xd7, 0xfa, 0xf5, 0xf3, 0x02, 0x9c, 0x68, 0x85, 0x9a, 0x06, 0xbf, 0x01, - 0x00, 0x00, 0xff, 0xff, 0xae, 0xdf, 0x93, 0x8e, 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, 0x94, + 0x1e, 0x84, 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, 0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d0, 0xf6, 0x47, 0x95, 0xdf, 0x98, 0xb9, 0x04, + 0x4d, 0x1b, 0xee, 0xc4, 0xbd, 0xef, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfc, 0xfc, 0x4b, 0x22, 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 7a99485c589..08093d1c55e 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\""]; }