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

ics29:fix: store source address for query later on WriteAck #912

Merged
merged 8 commits into from
Feb 23, 2022
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 @@ -200,7 +200,7 @@ func (im IBCModule) OnRecvPacket(

// incase of async aknowledgement (ack == nil) store the ForwardRelayer address for use later
if ack == nil && found {
im.keeper.SetForwardRelayerAddress(ctx, channeltypes.NewPacketId(packet.GetSourceChannel(), packet.GetSourcePort(), packet.GetSequence()), forwardRelayer)
im.keeper.SetForwardRelayerAddress(ctx, channeltypes.NewPacketId(packet.GetSourceChannel(), packet.GetSourcePort(), packet.GetSequence()), relayer.String())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're technically not setting the ForwardRelayerAddress anymore, I'm wondering if we should have a different setter for this use case specifically.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes maybe we should name it something else?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

side note: we can add a test for this after #927 is merged

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this mapping only used for async acks? Maybe the function name should take that into account? SetRelayerAddressForAsyncAck

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with SetRelayerAddressForAsyncAck & respective getter fn

}

return types.NewIncentivizedAcknowledgement(forwardRelayer, ack.Acknowledgement())
Expand Down
9 changes: 8 additions & 1 deletion modules/apps/29-fee/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"

"github.com/cosmos/ibc-go/v3/modules/apps/29-fee/types"
Expand All @@ -19,11 +20,17 @@ func (k Keeper) SendPacket(ctx sdk.Context, chanCap *capabilitytypes.Capability,
func (k Keeper) WriteAcknowledgement(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet ibcexported.PacketI, acknowledgement []byte) error {
// retrieve the forward relayer that was stored in `onRecvPacket`
packetId := channeltypes.NewPacketId(packet.GetSourceChannel(), packet.GetSourcePort(), packet.GetSequence())

// relayer address returned here is the
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
relayer, _ := k.GetForwardRelayerAddress(ctx, packetId)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AdityaSripal Can you remind me again why you don't want to error here? I'm throwing an error below if not found, wondering if you want to omit this also?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should throw an error here since that should never happen.

forwardRelayer, found := k.GetCounterpartyAddress(ctx, relayer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to ignore the error here.

This is because its possible that relayer has not registered counterparty address. If they haven't then, we just write acknowledgement with an empty relayer address.

We don't want to prevent writeacknowledgement from succeeding because relayer hasn't registered counterparty address.

This effectively means that recv_fee will get refunded and not incentivized, but that's the relayer's fault

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be commented in the code ^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if !found {
return sdkerrors.Wrapf(types.ErrCounterpartyAddressEmpty, "counterparty address not found for address: %s", forwardRelayer)
}

k.DeleteForwardRelayerAddress(ctx, packetId)
colin-axner marked this conversation as resolved.
Show resolved Hide resolved

ack := types.NewIncentivizedAcknowledgement(relayer, acknowledgement)
ack := types.NewIncentivizedAcknowledgement(forwardRelayer, acknowledgement)

// ics4Wrapper may be core IBC or higher-level middleware
return k.ics4Wrapper.WriteAcknowledgement(ctx, chanCap, packet, ack)
Expand Down
9 changes: 6 additions & 3 deletions modules/apps/29-fee/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ func (suite *KeeperTestSuite) TestWriteAcknowledgementAsync() {
}{
{
"success",
func() {},
func() {
suite.chainB.GetSimApp().IBCFeeKeeper.SetForwardRelayerAddress(suite.chainB.GetContext(), channeltypes.NewPacketId(suite.path.EndpointA.ChannelID, suite.path.EndpointA.ChannelConfig.PortID, 1), suite.chainA.SenderAccount.GetAddress().String())
suite.chainB.GetSimApp().IBCFeeKeeper.SetCounterpartyAddress(suite.chainB.GetContext(), suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String())
},
true,
},
{
"forward relayer address is successfully deleted",
"counterparty address not set",
func() {
suite.chainB.GetSimApp().IBCFeeKeeper.SetForwardRelayerAddress(suite.chainB.GetContext(), channeltypes.NewPacketId(suite.path.EndpointA.ChannelID, suite.path.EndpointA.ChannelConfig.PortID, 1), suite.chainA.SenderAccount.GetAddress().String())
},
true,
false,
},
}

Expand Down
22 changes: 18 additions & 4 deletions modules/apps/29-fee/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package types

import (
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

host "github.com/cosmos/ibc-go/v3/modules/core/24-host"
)

// NewGenesisState creates a 29-fee GenesisState instance.
func NewGenesisState(identifiedFees []IdentifiedPacketFee, feeEnabledChannels []*FeeEnabledChannel, registeredRelayers []*RegisteredRelayerAddress) *GenesisState {
func NewGenesisState(identifiedFees []IdentifiedPacketFee, feeEnabledChannels []*FeeEnabledChannel, registeredRelayers []*RegisteredRelayerAddress, forwardRelayers []*ForwardRelayerAddress) *GenesisState {
return &GenesisState{
IdentifiedFees: identifiedFees,
FeeEnabledChannels: feeEnabledChannels,
RegisteredRelayers: registeredRelayers,
ForwardRelayers: forwardRelayers,
}
}

Expand All @@ -22,6 +25,7 @@ func DefaultGenesisState() *GenesisState {
IdentifiedFees: []IdentifiedPacketFee{},
FeeEnabledChannels: []*FeeEnabledChannel{},
RegisteredRelayers: []*RegisteredRelayerAddress{},
ForwardRelayers: []*ForwardRelayerAddress{},
}
}

Expand All @@ -48,15 +52,25 @@ func (gs GenesisState) Validate() error {

// Validate RegisteredRelayers
for _, rel := range gs.RegisteredRelayers {
_, err := sdk.AccAddressFromBech32(rel.Address)
if err != nil {
if _, err := sdk.AccAddressFromBech32(rel.Address); err != nil {
return sdkerrors.Wrap(err, "failed to convert source relayer address into sdk.AccAddress")
}

if rel.CounterpartyAddress == "" {
if strings.TrimSpace(rel.CounterpartyAddress) == "" {
return ErrCounterpartyAddressEmpty
}
}

// Validate ForwardRelayers
for _, rel := range gs.ForwardRelayers {
if _, err := sdk.AccAddressFromBech32(rel.Address); err != nil {
return sdkerrors.Wrap(err, "failed to convert forward relayer address into sdk.AccAddress")
}

if err := rel.PacketId.Validate(); err != nil {
return err
}
}

return nil
}
42 changes: 33 additions & 9 deletions modules/apps/29-fee/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ var (

func TestValidateGenesis(t *testing.T) {
var (
packetId channeltypes.PacketId
fee types.Fee
refundAcc string
sender string
counterparty string
portID string
channelID string
seq uint64
packetId channeltypes.PacketId
fee types.Fee
refundAcc string
sender string
forwardAddr string
counterparty string
portID string
channelID string
packetChannelID string
seq uint64
)

testCases := []struct {
Expand Down Expand Up @@ -118,7 +120,21 @@ func TestValidateGenesis(t *testing.T) {
{
"invalid RegisteredRelayers: invalid counterparty",
func() {
counterparty = ""
counterparty = " "
},
false,
},
{
"invalid ForwardRelayerAddress: invalid forwardAddr",
func() {
forwardAddr = ""
},
false,
},
{
"invalid ForwardRelayerAddress: invalid packet",
func() {
packetChannelID = "1"
},
false,
},
Expand All @@ -127,6 +143,7 @@ func TestValidateGenesis(t *testing.T) {
for _, tc := range testCases {
portID = transfertypes.PortID
channelID = ibctesting.FirstChannelID
packetChannelID = ibctesting.FirstChannelID
seq = uint64(1)

// build PacketId & Fee
Expand All @@ -146,6 +163,7 @@ func TestValidateGenesis(t *testing.T) {
// relayer addresses
sender = addr1
counterparty = addr2
forwardAddr = addr2

tc.malleate()

Expand All @@ -170,6 +188,12 @@ func TestValidateGenesis(t *testing.T) {
CounterpartyAddress: counterparty,
},
},
ForwardRelayers: []*types.ForwardRelayerAddress{
{
Address: forwardAddr,
PacketId: channeltypes.NewPacketId(packetChannelID, portID, 1),
},
},
}

err := genState.Validate()
Expand Down