ibc: minor fixes from audit (#7807)

This commit is contained in:
Aditya 2020-11-04 12:16:20 +00:00 committed by GitHub
parent 286e9bfbef
commit 432afb274b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 10 deletions

View File

@ -14,18 +14,22 @@ import (
// CounterpartyHops returns the connection hops of the counterparty channel.
// The counterparty hops are stored in the inverse order as the channel's.
// NOTE: Since connectionHops only supports single connection channels for now,
// this function requires that connection hops only contain a single connection id
func (k Keeper) CounterpartyHops(ctx sdk.Context, ch types.Channel) ([]string, bool) {
counterPartyHops := make([]string, len(ch.ConnectionHops))
for i, hop := range ch.ConnectionHops {
conn, found := k.connectionKeeper.GetConnection(ctx, hop)
if !found {
return []string{}, false
}
counterPartyHops[len(counterPartyHops)-1-i] = conn.GetCounterparty().GetConnectionID()
// Return empty array if connection hops is more than one
// ConnectionHops length should be verified earlier
if len(ch.ConnectionHops) != 1 {
return []string{}, false
}
counterPartyHops := make([]string, 1)
hop := ch.ConnectionHops[0]
conn, found := k.connectionKeeper.GetConnection(ctx, hop)
if !found {
return []string{}, false
}
counterPartyHops[0] = conn.GetCounterparty().GetConnectionID()
return counterPartyHops, true
}

View File

@ -46,6 +46,12 @@ func (msg MsgChannelOpenInit) ValidateBasic() error {
if err := host.ChannelIdentifierValidator(msg.ChannelId); err != nil {
return sdkerrors.Wrap(err, "invalid channel ID")
}
if msg.Channel.State != INIT {
return sdkerrors.Wrapf(ErrInvalidChannelState,
"channel state must be INIT in MsgChannelOpenInit. expected: %s, got: %s",
INIT, msg.Channel.State,
)
}
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
@ -78,7 +84,7 @@ func NewMsgChannelOpenTry(
proofInit []byte, proofHeight clienttypes.Height, signer sdk.AccAddress,
) *MsgChannelOpenTry {
counterparty := NewCounterparty(counterpartyPortID, counterpartyChannelID)
channel := NewChannel(INIT, channelOrder, counterparty, connectionHops, version)
channel := NewChannel(TRYOPEN, channelOrder, counterparty, connectionHops, version)
return &MsgChannelOpenTry{
PortId: portID,
DesiredChannelId: desiredChannelID,
@ -118,6 +124,12 @@ func (msg MsgChannelOpenTry) ValidateBasic() error {
if msg.ProofHeight.IsZero() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidHeight, "proof height must be non-zero")
}
if msg.Channel.State != TRYOPEN {
return sdkerrors.Wrapf(ErrInvalidChannelState,
"channel state must be TRYOPEN in MsgChannelOpenTry. expected: %s, got: %s",
TRYOPEN, msg.Channel.State,
)
}
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)

View File

@ -105,6 +105,9 @@ func TestTypesTestSuite(t *testing.T) {
}
func (suite *TypesTestSuite) TestMsgChannelOpenInitValidateBasic() {
counterparty := types.NewCounterparty(cpportid, cpchanid)
tryOpenChannel := types.NewChannel(types.TRYOPEN, types.ORDERED, counterparty, connHops, version)
testCases := []struct {
name string
msg *types.MsgChannelOpenInit
@ -125,6 +128,7 @@ func (suite *TypesTestSuite) TestMsgChannelOpenInitValidateBasic() {
{"", types.NewMsgChannelOpenInit(portid, chanid, "", types.UNORDERED, connHops, cpportid, cpchanid, addr), true},
{"invalid counterparty port id", types.NewMsgChannelOpenInit(portid, chanid, version, types.UNORDERED, connHops, invalidPort, cpchanid, addr), false},
{"invalid counterparty channel id", types.NewMsgChannelOpenInit(portid, chanid, version, types.UNORDERED, connHops, cpportid, invalidChannel, addr), false},
{"channel not in INIT state", &types.MsgChannelOpenInit{portid, chanid, tryOpenChannel, addr.String()}, false},
}
for _, tc := range testCases {
@ -142,6 +146,9 @@ func (suite *TypesTestSuite) TestMsgChannelOpenInitValidateBasic() {
}
func (suite *TypesTestSuite) TestMsgChannelOpenTryValidateBasic() {
counterparty := types.NewCounterparty(cpportid, cpchanid)
initChannel := types.NewChannel(types.INIT, types.ORDERED, counterparty, connHops, version)
testCases := []struct {
name string
msg *types.MsgChannelOpenTry
@ -167,6 +174,7 @@ func (suite *TypesTestSuite) TestMsgChannelOpenTryValidateBasic() {
{"empty proof", types.NewMsgChannelOpenTry(portid, chanid, chanid, version, types.UNORDERED, connHops, cpportid, cpchanid, version, emptyProof, height, addr), false},
{"valid empty proved channel id", types.NewMsgChannelOpenTry(portid, chanid, "", version, types.ORDERED, connHops, cpportid, cpchanid, version, suite.proof, height, addr), true},
{"invalid proved channel id, doesn't match channel id", types.NewMsgChannelOpenTry(portid, chanid, "differentchannel", version, types.ORDERED, connHops, cpportid, cpchanid, version, suite.proof, height, addr), false},
{"channel not in TRYOPEN state", &types.MsgChannelOpenTry{portid, chanid, chanid, initChannel, version, suite.proof, height, addr.String()}, false},
}
for _, tc := range testCases {