-
Notifications
You must be signed in to change notification settings - Fork 624
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
ics4 callbacks fee middleware #580
Changes from 15 commits
4128f95
aa2e7a9
f7c8b07
c22a416
c77ae26
dc07eb5
7edb13b
86a5288
0029208
f7e7b71
59ca024
0dc1097
053f8f2
1e3b296
6ed040b
e8ff6d4
0625fa1
917b1ef
45cdb0c
b7e3b2d
75919dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package keeper | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
|
@@ -12,7 +13,6 @@ import ( | |
"github.com/cosmos/ibc-go/modules/apps/29-fee/types" | ||
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" | ||
host "github.com/cosmos/ibc-go/modules/core/24-host" | ||
ibcexported "github.com/cosmos/ibc-go/modules/core/exported" | ||
) | ||
|
||
// Middleware must implement types.ChannelKeeper and types.PortKeeper expected interfaces | ||
|
@@ -77,22 +77,6 @@ func (k Keeper) GetFeeModuleAddress() sdk.AccAddress { | |
return k.authKeeper.GetModuleAddress(types.ModuleName) | ||
} | ||
|
||
// SendPacket wraps IBC ChannelKeeper's SendPacket function | ||
func (k Keeper) SendPacket(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet ibcexported.PacketI) error { | ||
return k.ics4Wrapper.SendPacket(ctx, chanCap, packet) | ||
} | ||
|
||
// WriteAcknowledgement wraps IBC ChannelKeeper's WriteAcknowledgement function | ||
func (k Keeper) WriteAcknowledgement( | ||
ctx sdk.Context, | ||
chanCap *capabilitytypes.Capability, | ||
packet ibcexported.PacketI, | ||
acknowledgement []byte, | ||
) error { | ||
return nil | ||
// return k.channelKeeper.WriteAcknowledgement(ctx, chanCap, packet, acknowledgement) | ||
} | ||
|
||
// SetFeeEnabled sets a flag to determine if fee handling logic should run for the given channel | ||
// identified by channel and port identifiers. | ||
func (k Keeper) SetFeeEnabled(ctx sdk.Context, portID, channelID string) { | ||
|
@@ -169,6 +153,7 @@ func (k Keeper) GetCounterpartyAddress(ctx sdk.Context, address string) (string, | |
return addr, true | ||
} | ||
|
||
// GetAllRelayerAddresses returns all registered relayer addresses | ||
func (k Keeper) GetAllRelayerAddresses(ctx sdk.Context) []*types.RegisteredRelayerAddress { | ||
store := ctx.KVStore(k.storeKey) | ||
iterator := sdk.KVStorePrefixIterator(store, []byte(types.RelayerAddressKeyPrefix)) | ||
|
@@ -189,6 +174,59 @@ func (k Keeper) GetAllRelayerAddresses(ctx sdk.Context) []*types.RegisteredRelay | |
return registeredAddrArr | ||
} | ||
|
||
// SetForwardRelayerAddress sets the forward relayer address during OnRecvPacket in case of async acknowledgement | ||
func (k Keeper) SetForwardRelayerAddress(ctx sdk.Context, packetId *channeltypes.PacketId, address string) { | ||
store := ctx.KVStore(k.storeKey) | ||
store.Set(types.KeyForwardRelayerAddress(packetId), []byte(address)) | ||
} | ||
|
||
// GetForwardRelayerAddress gets forward relayer address for a particular packet | ||
func (k Keeper) GetForwardRelayerAddress(ctx sdk.Context, packetId *channeltypes.PacketId) (string, bool) { | ||
store := ctx.KVStore(k.storeKey) | ||
key := types.KeyForwardRelayerAddress(packetId) | ||
if !store.Has(key) { | ||
return "", false | ||
} | ||
|
||
addr := string(store.Get(key)) | ||
return addr, true | ||
} | ||
|
||
AdityaSripal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// GetAllForwardRelayerAddresses returns all forward relayer addresses stored for async acknowledgements | ||
func (k Keeper) GetAllForwardRelayerAddresses(ctx sdk.Context) []*types.ForwardRelayerAddress { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you add a unit test for this? A followup pr is fine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll create an issue. Thanks |
||
store := ctx.KVStore(k.storeKey) | ||
iterator := sdk.KVStorePrefixIterator(store, []byte(types.ForwardRelayerPrefix)) | ||
defer iterator.Close() | ||
|
||
var forwardRelayerAddr []*types.ForwardRelayerAddress | ||
for ; iterator.Valid(); iterator.Next() { | ||
keySplit := strings.Split(string(iterator.Key()), "/") | ||
|
||
seq, err := strconv.ParseUint(keySplit[3], 0, 64) | ||
if err != nil { | ||
panic("Error parsing sequence") | ||
seantking marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
packetId := channeltypes.NewPacketId(keySplit[2], keySplit[1], seq) | ||
|
||
addr := &types.ForwardRelayerAddress{ | ||
Address: string(iterator.Value()), | ||
PacketId: packetId, | ||
} | ||
|
||
forwardRelayerAddr = append(forwardRelayerAddr, addr) | ||
} | ||
|
||
return forwardRelayerAddr | ||
} | ||
|
||
// Deletes the forwardRelayerAddr associated with the packetId | ||
func (k Keeper) DeleteForwardRelayerAddress(ctx sdk.Context, packetId *channeltypes.PacketId) { | ||
store := ctx.KVStore(k.storeKey) | ||
key := types.KeyForwardRelayerAddress(packetId) | ||
store.Delete(key) | ||
} | ||
|
||
// Stores a Fee for a given packet in state | ||
func (k Keeper) SetFeeInEscrow(ctx sdk.Context, fee *types.IdentifiedPacketFee) { | ||
store := ctx.KVStore(k.storeKey) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" | ||
|
||
"github.com/cosmos/ibc-go/modules/apps/29-fee/types" | ||
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" | ||
ibcexported "github.com/cosmos/ibc-go/modules/core/exported" | ||
) | ||
|
||
// SendPacket wraps IBC ChannelKeeper's SendPacket function | ||
func (k Keeper) SendPacket(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet ibcexported.PacketI) error { | ||
return k.ics4Wrapper.SendPacket(ctx, chanCap, packet) | ||
} | ||
|
||
// WriteAcknowledgement wraps IBC ChannelKeeper's WriteAcknowledgement function | ||
// ICS29 WriteAcknowledgement is used for asynchronous acknowledgements | ||
func (k Keeper) WriteAcknowledgement(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet ibcexported.PacketI, acknowledgement []byte) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AdityaSripal I guess I need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea because No need to store it if you're going to send it out immediately |
||
// retrieve the forward relayer that was stored in `onRecvPacket` | ||
packetId := channeltypes.NewPacketId(packet.GetSourceChannel(), packet.GetSourcePort(), packet.GetSequence()) | ||
relayer, found := k.GetForwardRelayerAddress(ctx, packetId) | ||
|
||
if found { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AdityaSripal Changed this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really necessary since this will no op if the key isn't set but doesn't matter either way There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok good to know, I was thinking it might panic. |
||
// delete the forward relayer address as it is no longer used | ||
k.DeleteForwardRelayerAddress(ctx, packetId) | ||
} | ||
|
||
AdityaSripal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ack := types.NewIncentivizedAcknowledgement(relayer, acknowledgement) | ||
bz := ack.Acknowledgement() | ||
|
||
// ics4Wrapper may be core IBC or higher-level middleware | ||
return k.ics4Wrapper.WriteAcknowledgement(ctx, chanCap, packet, bz) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package keeper_test | ||
|
||
import ( | ||
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" | ||
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestWriteAcknowledgementAsync() { | ||
testCases := []struct { | ||
name string | ||
malleate func() | ||
expPass bool | ||
}{ | ||
{ | ||
"success", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add a test case for the forward relayer address being set? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Added a check to ensure it gets successfully deleted if set, and no-ops otherwise. |
||
func() {}, | ||
true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
suite.Run(tc.name, func() { | ||
suite.SetupTest() | ||
|
||
// open incentivized channel | ||
suite.coordinator.Setup(suite.path) | ||
|
||
// build packet | ||
timeoutTimestamp := ^uint64(0) | ||
packet := channeltypes.NewPacket( | ||
[]byte("packetData"), | ||
1, | ||
suite.path.EndpointA.ChannelConfig.PortID, | ||
suite.path.EndpointA.ChannelID, | ||
suite.path.EndpointB.ChannelConfig.PortID, | ||
suite.path.EndpointB.ChannelID, | ||
clienttypes.ZeroHeight(), | ||
timeoutTimestamp, | ||
) | ||
|
||
ack := []byte("ack") | ||
chanCap := suite.chainB.GetChannelCapability(suite.path.EndpointB.ChannelConfig.PortID, suite.path.EndpointB.ChannelID) | ||
|
||
// malleate test case | ||
tc.malleate() | ||
|
||
err := suite.chainB.GetSimApp().IBCFeeKeeper.WriteAcknowledgement(suite.chainB.GetContext(), chanCap, packet, ack) | ||
|
||
if tc.expPass { | ||
suite.Require().NoError(err) | ||
} else { | ||
suite.Require().Error(err) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,14 @@ import ( | |
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// NewIncentivizedAcknowledgement creates a new instance of IncentivizedAcknowledgement | ||
func NewIncentivizedAcknowledgement(relayer string, ack []byte) IncentivizedAcknowledgement { | ||
return IncentivizedAcknowledgement{ | ||
Result: ack, | ||
ForwardRelayerAddress: relayer, | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You also need to add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @charleenfei has this on her branch. I'll copy it over |
||
// Success implements the Acknowledgement interface. The acknowledgement is | ||
// considered successful if the forward relayer address is empty. Otherwise it is | ||
// considered a failed acknowledgement. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a comment explaining this functionality. I didn't understand what it was doing until I read the godoc for SetForwardRelayerAddress
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed on comment