Skip to content

Commit

Permalink
fix: counterparty address must chain channelID
Browse files Browse the repository at this point in the history
  • Loading branch information
seantking committed Feb 16, 2022
1 parent 2c1ff0b commit 2e07ac5
Show file tree
Hide file tree
Showing 18 changed files with 206 additions and 98 deletions.
2 changes: 2 additions & 0 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ RegisteredRelayerAddress contains the address and counterparty address for a spe
| ----- | ---- | ----- | ----------- |
| `address` | [string](#string) | | |
| `counterparty_address` | [string](#string) | | |
| `channel_id` | [string](#string) | | |



Expand Down Expand Up @@ -1040,6 +1041,7 @@ MsgRegisterCounterpartyAddress is the request type for registering the counterpa
| ----- | ---- | ----- | ----------- |
| `address` | [string](#string) | | |
| `counterparty_address` | [string](#string) | | |
| `channel_id` | [string](#string) | | |



Expand Down
2 changes: 1 addition & 1 deletion modules/apps/29-fee/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (im IBCModule) OnRecvPacket(

ack := im.app.OnRecvPacket(ctx, packet, relayer)

forwardRelayer, found := im.keeper.GetCounterpartyAddress(ctx, relayer.String())
forwardRelayer, found := im.keeper.GetCounterpartyAddress(ctx, relayer.String(), packet.DestinationChannel)

// incase of async aknowledgement (ack == nil) store the ForwardRelayer address for use later
if ack == nil && found {
Expand Down
4 changes: 2 additions & 2 deletions modules/apps/29-fee/ibc_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func (suite *FeeTestSuite) TestOnRecvPacket() {
{
"source relayer is empty string",
func() {
suite.chainB.GetSimApp().IBCFeeKeeper.SetCounterpartyAddress(suite.chainB.GetContext(), suite.chainA.SenderAccount.GetAddress().String(), "")
suite.chainB.GetSimApp().IBCFeeKeeper.SetCounterpartyAddress(suite.chainB.GetContext(), suite.chainA.SenderAccount.GetAddress().String(), "", suite.path.EndpointB.ChannelID)
},
false,
true,
Expand Down Expand Up @@ -486,7 +486,7 @@ func (suite *FeeTestSuite) TestOnRecvPacket() {
cbs, ok := suite.chainB.App.GetIBCKeeper().Router.GetRoute(module)
suite.Require().True(ok)

suite.chainB.GetSimApp().IBCFeeKeeper.SetCounterpartyAddress(suite.chainB.GetContext(), suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String())
suite.chainB.GetSimApp().IBCFeeKeeper.SetCounterpartyAddress(suite.chainB.GetContext(), suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(), suite.path.EndpointB.ChannelID)

// malleate test case
tc.malleate()
Expand Down
2 changes: 1 addition & 1 deletion modules/apps/29-fee/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) {
}

for _, addr := range state.RegisteredRelayers {
k.SetCounterpartyAddress(ctx, addr.Address, addr.CounterpartyAddress)
k.SetCounterpartyAddress(ctx, addr.Address, addr.CounterpartyAddress, addr.ChannelId)
}

for _, forwardAddr := range state.ForwardRelayers {
Expand Down
5 changes: 3 additions & 2 deletions modules/apps/29-fee/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (suite *KeeperTestSuite) TestInitGenesis() {
{
Address: sender,
CounterpartyAddress: counterparty,
ChannelId: ibctesting.FirstChannelID,
},
},
}
Expand All @@ -62,7 +63,7 @@ func (suite *KeeperTestSuite) TestInitGenesis() {
suite.Require().True(isEnabled)

// check relayers
addr, found := suite.chainA.GetSimApp().IBCFeeKeeper.GetCounterpartyAddress(suite.chainA.GetContext(), sender)
addr, found := suite.chainA.GetSimApp().IBCFeeKeeper.GetCounterpartyAddress(suite.chainA.GetContext(), sender, ibctesting.FirstChannelID)
suite.Require().True(found)
suite.Require().Equal(genesisState.RegisteredRelayers[0].CounterpartyAddress, addr)
}
Expand All @@ -88,7 +89,7 @@ func (suite *KeeperTestSuite) TestExportGenesis() {
sender := suite.chainA.SenderAccount.GetAddress().String()
counterparty := suite.chainB.SenderAccount.GetAddress().String()
// set counterparty address
suite.chainA.GetSimApp().IBCFeeKeeper.SetCounterpartyAddress(suite.chainA.GetContext(), sender, counterparty)
suite.chainA.GetSimApp().IBCFeeKeeper.SetCounterpartyAddress(suite.chainA.GetContext(), sender, counterparty, ibctesting.FirstChannelID)

// set forward relayer address
suite.chainA.GetSimApp().IBCFeeKeeper.SetForwardRelayerAddress(suite.chainA.GetContext(), packetID, sender)
Expand Down
9 changes: 5 additions & 4 deletions modules/apps/29-fee/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ func (k Keeper) DisableAllChannels(ctx sdk.Context) {

// SetCounterpartyAddress maps the destination chain relayer address to the source relayer address
// The receiving chain must store the mapping from: address -> counterpartyAddress for the given channel
func (k Keeper) SetCounterpartyAddress(ctx sdk.Context, address, counterpartyAddress string) {
func (k Keeper) SetCounterpartyAddress(ctx sdk.Context, address, counterpartyAddress, channelID string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyRelayerAddress(address), []byte(counterpartyAddress))
store.Set(types.KeyRelayerAddress(address, channelID), []byte(counterpartyAddress))
}

// GetCounterpartyAddress gets the relayer counterparty address given a destination relayer address
func (k Keeper) GetCounterpartyAddress(ctx sdk.Context, address string) (string, bool) {
func (k Keeper) GetCounterpartyAddress(ctx sdk.Context, address, channelID string) (string, bool) {
store := ctx.KVStore(k.storeKey)
key := types.KeyRelayerAddress(address)
key := types.KeyRelayerAddress(address, channelID)

if !store.Has(key) {
return "", false
Expand All @@ -166,6 +166,7 @@ func (k Keeper) GetAllRelayerAddresses(ctx sdk.Context) []*types.RegisteredRelay
addr := &types.RegisteredRelayerAddress{
Address: keySplit[1],
CounterpartyAddress: string(iterator.Value()),
ChannelId: keySplit[2],
}

registeredAddrArr = append(registeredAddrArr, addr)
Expand Down
3 changes: 2 additions & 1 deletion modules/apps/29-fee/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,13 @@ func (suite *KeeperTestSuite) TestGetAllRelayerAddresses() {
sender := suite.chainA.SenderAccount.GetAddress().String()
counterparty := suite.chainB.SenderAccount.GetAddress().String()

suite.chainA.GetSimApp().IBCFeeKeeper.SetCounterpartyAddress(suite.chainA.GetContext(), sender, counterparty)
suite.chainA.GetSimApp().IBCFeeKeeper.SetCounterpartyAddress(suite.chainA.GetContext(), sender, counterparty, ibctesting.FirstChannelID)

expectedAddr := []*types.RegisteredRelayerAddress{
{
Address: sender,
CounterpartyAddress: counterparty,
ChannelId: ibctesting.FirstChannelID,
},
}

Expand Down
2 changes: 1 addition & 1 deletion modules/apps/29-fee/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (k Keeper) RegisterCounterpartyAddress(goCtx context.Context, msg *types.Ms
ctx := sdk.UnwrapSDKContext(goCtx)

k.SetCounterpartyAddress(
ctx, msg.Address, msg.CounterpartyAddress,
ctx, msg.Address, msg.CounterpartyAddress, msg.ChannelId,
)

k.Logger(ctx).Info("Registering counterparty address for relayer.", "Address:", msg.Address, "Counterparty Address:", msg.CounterpartyAddress)
Expand Down
5 changes: 3 additions & 2 deletions modules/apps/29-fee/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
"github.com/cosmos/ibc-go/v3/modules/apps/29-fee/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
ibctesting "github.com/cosmos/ibc-go/v3/testing"
)

func (suite *KeeperTestSuite) TestRegisterCounterpartyAddress() {
Expand Down Expand Up @@ -35,14 +36,14 @@ func (suite *KeeperTestSuite) TestRegisterCounterpartyAddress() {
sender = suite.chainA.SenderAccount.GetAddress().String()
counterparty = suite.chainB.SenderAccount.GetAddress().String()
tc.malleate()
msg := types.NewMsgRegisterCounterpartyAddress(sender, counterparty)
msg := types.NewMsgRegisterCounterpartyAddress(sender, counterparty, ibctesting.FirstChannelID)

_, err := suite.chainA.SendMsgs(msg)

if tc.expPass {
suite.Require().NoError(err) // message committed

counterpartyAddress, _ := suite.chainA.GetSimApp().IBCFeeKeeper.GetCounterpartyAddress(ctx, suite.chainA.SenderAccount.GetAddress().String())
counterpartyAddress, _ := suite.chainA.GetSimApp().IBCFeeKeeper.GetCounterpartyAddress(ctx, suite.chainA.SenderAccount.GetAddress().String(), ibctesting.FirstChannelID)
suite.Require().Equal(counterparty, counterpartyAddress)
} else {
suite.Require().Error(err)
Expand Down
2 changes: 1 addition & 1 deletion modules/apps/29-fee/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (suite *FeeTestSuite) TestFeeTransfer() {
// to differentiate from the chainA.SenderAccount for checking successful relay payouts
relayerAddress := suite.chainB.SenderAccount.GetAddress()

msgRegister := types.NewMsgRegisterCounterpartyAddress(suite.chainB.SenderAccount.GetAddress().String(), relayerAddress.String())
msgRegister := types.NewMsgRegisterCounterpartyAddress(suite.chainB.SenderAccount.GetAddress().String(), relayerAddress.String(), ibctesting.FirstChannelID)
_, err = suite.chainB.SendMsgs(msgRegister)
suite.Require().NoError(err) // message committed

Expand Down
122 changes: 87 additions & 35 deletions modules/apps/29-fee/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions modules/apps/29-fee/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func FeeEnabledKey(portID, channelID string) []byte {
}

// KeyRelayerAddress returns the key for relayer address -> counteryparty address mapping
func KeyRelayerAddress(address string) []byte {
return []byte(fmt.Sprintf("%s/%s", RelayerAddressKeyPrefix, address))
func KeyRelayerAddress(address, channelID string) []byte {
return []byte(fmt.Sprintf("%s/%s/%s", RelayerAddressKeyPrefix, address, channelID))
}

// KeyForwardRelayerAddress returns the key for packetID -> forwardAddress mapping
Expand Down
5 changes: 3 additions & 2 deletions modules/apps/29-fee/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
func TestKeyRelayerAddress(t *testing.T) {
var (
relayerAddress = "relayer_address"
channelID = "channel-0"
)

key := types.KeyRelayerAddress(relayerAddress)
require.Equal(t, string(key), fmt.Sprintf("%s/relayer_address", types.RelayerAddressKeyPrefix))
key := types.KeyRelayerAddress(relayerAddress, channelID)
require.Equal(t, string(key), fmt.Sprintf("%s/%s/%s", types.RelayerAddressKeyPrefix, relayerAddress, channelID))
}
3 changes: 2 additions & 1 deletion modules/apps/29-fee/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ const (
)

// NewMsgRegisterCounterpartyAddress creates a new instance of MsgRegisterCounterpartyAddress
func NewMsgRegisterCounterpartyAddress(address, counterpartyAddress string) *MsgRegisterCounterpartyAddress {
func NewMsgRegisterCounterpartyAddress(address, counterpartyAddress, channelID string) *MsgRegisterCounterpartyAddress {
return &MsgRegisterCounterpartyAddress{
Address: address,
CounterpartyAddress: counterpartyAddress,
ChannelId: channelID,
}
}

Expand Down
Loading

0 comments on commit 2e07ac5

Please sign in to comment.