forked from cosmos/ibc-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ics4_middleware.go
78 lines (61 loc) · 2.72 KB
/
ics4_middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package ibc_hooks
import (
// external libraries
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
// ibc-go
porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
)
var _ porttypes.ICS4Wrapper = &ICS4Middleware{}
type ICS4Middleware struct {
channel porttypes.ICS4Wrapper
// Hooks
Hooks Hooks
}
func NewICS4Middleware(channel porttypes.ICS4Wrapper, hooks Hooks) ICS4Middleware {
return ICS4Middleware{
channel: channel,
Hooks: hooks,
}
}
func (i ICS4Middleware) SendPacket(ctx sdk.Context, channelCap *capabilitytypes.Capability, sourcePort string, sourceChannel string, timeoutHeight ibcclienttypes.Height, timeoutTimestamp uint64, data []byte) (sequence uint64, err error) {
if hook, ok := i.Hooks.(SendPacketOverrideHooks); ok {
return hook.SendPacketOverride(i, ctx, channelCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data)
}
if hook, ok := i.Hooks.(SendPacketBeforeHooks); ok {
hook.SendPacketBeforeHook(ctx, channelCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data)
}
seq, err := i.channel.SendPacket(ctx, channelCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data)
if hook, ok := i.Hooks.(SendPacketAfterHooks); ok {
hook.SendPacketAfterHook(ctx, channelCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data, err)
}
return seq, err
}
func (i ICS4Middleware) WriteAcknowledgement(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet ibcexported.PacketI, ack ibcexported.Acknowledgement) error {
if hook, ok := i.Hooks.(WriteAcknowledgementOverrideHooks); ok {
return hook.WriteAcknowledgementOverride(i, ctx, chanCap, packet, ack)
}
if hook, ok := i.Hooks.(WriteAcknowledgementBeforeHooks); ok {
hook.WriteAcknowledgementBeforeHook(ctx, chanCap, packet, ack)
}
err := i.channel.WriteAcknowledgement(ctx, chanCap, packet, ack)
if hook, ok := i.Hooks.(WriteAcknowledgementAfterHooks); ok {
hook.WriteAcknowledgementAfterHook(ctx, chanCap, packet, ack, err)
}
return err
}
func (i ICS4Middleware) GetAppVersion(ctx sdk.Context, portID, channelID string) (string, bool) {
if hook, ok := i.Hooks.(GetAppVersionOverrideHooks); ok {
return hook.GetAppVersionOverride(i, ctx, portID, channelID)
}
if hook, ok := i.Hooks.(GetAppVersionBeforeHooks); ok {
hook.GetAppVersionBeforeHook(ctx, portID, channelID)
}
version, err := i.channel.GetAppVersion(ctx, portID, channelID)
if hook, ok := i.Hooks.(GetAppVersionAfterHooks); ok {
hook.GetAppVersionAfterHook(ctx, portID, channelID, version, err)
}
return version, err
}