Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an IBC prefix to transfer escrow addresses #7967

Merged
merged 22 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bb7498f
add IBC prefix to escrow addresses
colin-axner Nov 18, 2020
b7227b4
fix merge conflict
colin-axner Nov 18, 2020
6e4d099
use ADR 028 AddressHash construction
colin-axner Nov 19, 2020
b090898
Merge branch 'master' into colin/7737-prefix-escrow-addr
colin-axner Nov 19, 2020
af76261
remove extra space
colin-axner Nov 19, 2020
8cb1fee
Merge branch 'colin/7737-prefix-escrow-addr' of github.com:cosmos/cos…
colin-axner Nov 19, 2020
5fce47d
apply @AdityaSripal suggestion; prefix from ibc -> ibc-transfer-escro…
colin-axner Nov 19, 2020
7bffc31
Merge branch 'master' into colin/7737-prefix-escrow-addr
colin-axner Nov 19, 2020
91b68dd
Use alternative 1 proposed by @andrey-kuprianov
colin-axner Nov 24, 2020
ab19a4a
anticpate necessary build fix
colin-axner Nov 24, 2020
c57493b
Merge branch 'colin/7737-prefix-escrow-addr' of github.com:cosmos/cos…
colin-axner Nov 24, 2020
6731918
Merge branch 'master' into colin/7737-prefix-escrow-addr
colin-axner Nov 24, 2020
e366e9c
Merge branch 'master' into colin/7737-prefix-escrow-addr
colin-axner Nov 24, 2020
111ed79
Merge branch 'master' into colin/7737-prefix-escrow-addr
colin-axner Nov 24, 2020
d730ae2
add escrow address issue link into module.go
colin-axner Nov 24, 2020
3559e72
Merge branch 'colin/7737-prefix-escrow-addr' of github.com:cosmos/cos…
colin-axner Nov 24, 2020
c697887
Merge branch 'master' into colin/7737-prefix-escrow-addr
colin-axner Nov 25, 2020
c5ef4f9
Merge branch 'master' into colin/7737-prefix-escrow-addr
colin-axner Nov 25, 2020
c9e3a18
Update x/ibc/applications/transfer/types/keys.go
colin-axner Nov 26, 2020
272dd2a
Update x/ibc/applications/transfer/types/keys.go
colin-axner Nov 26, 2020
8a99065
Merge branch 'master' of github.com:cosmos/cosmos-sdk into colin/7737…
colin-axner Nov 26, 2020
0677e09
apply @andrey-kuprianov review suggestions
colin-axner Nov 26, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions x/ibc/applications/transfer/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"math"
"math/rand"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
Expand Down Expand Up @@ -182,30 +183,56 @@ func (am AppModule) WeightedOperations(_ module.SimulationState) []simtypes.Weig

//____________________________________________________________________________

// OnChanOpenInit implements the IBCModule interface
func (am AppModule) OnChanOpenInit(
// ValidateTransferChannelParams does validation of a newly created transfer channel. A transfer
// channel must be UNORDERED, use the correct port (by default 'transfer'), and use the current
// supported version. Only 2^32 channels are allowed to be created.
func ValidateTransferChannelParams(
ctx sdk.Context,
keeper keeper.Keeper,
order channeltypes.Order,
connectionHops []string,
portID string,
channelID string,
chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
version string,
) error {
// NOTE: for escrow address security only 2^32 channels are allowed to be created
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved
andrey-kuprianov marked this conversation as resolved.
Show resolved Hide resolved
// Issue: https://github.com/cosmos/cosmos-sdk/issues/7737
channelSequence, err := channeltypes.ParseChannelSequence(channelID)
if err != nil {
return err
}
if channelSequence > math.MaxUint32 {
return sdkerrors.Wrapf(types.ErrMaxTransferChannels, "channel sequence %d is greater than max allowed transfer channels %d", channelSequence, math.MaxUint32)
}
if order != channeltypes.UNORDERED {
return sdkerrors.Wrapf(channeltypes.ErrInvalidChannelOrdering, "expected %s channel, got %s ", channeltypes.UNORDERED, order)
}

// Require portID is the portID transfer module is bound to
boundPort := am.keeper.GetPort(ctx)
boundPort := keeper.GetPort(ctx)
if boundPort != portID {
return sdkerrors.Wrapf(porttypes.ErrInvalidPort, "invalid port: %s, expected %s", portID, boundPort)
}

if version != types.Version {
return sdkerrors.Wrapf(types.ErrInvalidVersion, "got %s, expected %s", version, types.Version)
}
return nil
}

// OnChanOpenInit implements the IBCModule interface
func (am AppModule) OnChanOpenInit(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID string,
channelID string,
chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
version string,
) error {
if err := ValidateTransferChannelParams(ctx, am.keeper, order, portID, channelID, version); err != nil {
return err
}

// Claim channel capability passed back by IBC module
if err := am.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
Expand All @@ -227,18 +254,8 @@ func (am AppModule) OnChanOpenTry(
version,
counterpartyVersion string,
) error {
if order != channeltypes.UNORDERED {
return sdkerrors.Wrapf(channeltypes.ErrInvalidChannelOrdering, "expected %s channel, got %s ", channeltypes.UNORDERED, order)
}

// Require portID is the portID transfer module is bound to
boundPort := am.keeper.GetPort(ctx)
if boundPort != portID {
return sdkerrors.Wrapf(porttypes.ErrInvalidPort, "invalid port: %s, expected %s", portID, boundPort)
}

if version != types.Version {
return sdkerrors.Wrapf(types.ErrInvalidVersion, "got: %s, expected %s", version, types.Version)
if err := ValidateTransferChannelParams(ctx, am.keeper, order, portID, channelID, version); err != nil {
return err
}

if counterpartyVersion != types.Version {
Expand Down
12 changes: 12 additions & 0 deletions x/ibc/applications/transfer/module_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package transfer_test

import (
"math"

capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
"github.com/cosmos/cosmos-sdk/x/ibc/applications/transfer/types"
channeltypes "github.com/cosmos/cosmos-sdk/x/ibc/core/04-channel/types"
Expand All @@ -26,6 +28,11 @@ func (suite *TransferTestSuite) TestOnChanOpenInit() {
{
"success", func() {}, true,
},
{
"max channels reached", func() {
testChannel.ID = channeltypes.FormatChannelIdentifier(math.MaxUint32 + 1)
}, false,
},
{
"invalid order - ORDERED", func() {
channel.Ordering = channeltypes.ORDERED
Expand Down Expand Up @@ -109,6 +116,11 @@ func (suite *TransferTestSuite) TestOnChanOpenTry() {
{
"success", func() {}, true,
},
{
"max channels reached", func() {
testChannel.ID = channeltypes.FormatChannelIdentifier(math.MaxUint32 + 1)
}, false,
},
{
"capability already claimed in INIT should pass", func() {
err := suite.chainA.App.ScopedTransferKeeper.ClaimCapability(suite.chainA.GetContext(), chanCap, host.ChannelCapabilityPath(testChannel.PortID, testChannel.ID))
Expand Down
1 change: 1 addition & 0 deletions x/ibc/applications/transfer/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ var (
ErrTraceNotFound = sdkerrors.Register(ModuleName, 6, "denomination trace not found")
ErrSendDisabled = sdkerrors.Register(ModuleName, 7, "fungible token transfers from this chain are disabled")
ErrReceiveDisabled = sdkerrors.Register(ModuleName, 8, "fungible token transfers to this chain are disabled")
ErrMaxTransferChannels = sdkerrors.Register(ModuleName, 9, "max transfer channels")
)
22 changes: 14 additions & 8 deletions x/ibc/applications/transfer/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package types

import (
"crypto/sha256"
"fmt"

"github.com/tendermint/tendermint/crypto"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -39,11 +38,18 @@ var (
DenomTraceKey = []byte{0x02}
)

// GetEscrowAddress returns the escrow address for the specified channel
//
// CONTRACT: this assumes that there's only one bank bridge module that owns the
// port associated with the channel ID so that the address created is actually
// unique.
// GetEscrowAddress returns the escrow address for the specified channel.
// The escrow address follows the format as outlined in ADR 028:
// https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-028-public-key-addresses.md
func GetEscrowAddress(portID, channelID string) sdk.AccAddress {
return sdk.AccAddress(crypto.AddressHash([]byte(fmt.Sprintf("%s/%s", portID, channelID))))
// a slash is used to create domain separation between port and channel identifiers to
// prevent address collisions between escrow addresses created for different channels
contents := fmt.Sprintf("%s/%s", portID, channelID)

// ADR 028 AddressHash construction
preImage := []byte(Version)
preImage = append(preImage, 0)
preImage = append(preImage, contents...)
hash := sha256.Sum256(preImage)
return hash[:20]
}