-
Notifications
You must be signed in to change notification settings - Fork 2
/
path.go
139 lines (111 loc) · 4.15 KB
/
path.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package ibctesting
import (
"bytes"
"fmt"
channeltypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types"
)
// Path contains two endpoints representing two chains connected over IBC
type Path struct {
EndpointA *Endpoint
EndpointB *Endpoint
}
// NewPath constructs an endpoint for each chain using the default values
// for the endpoints. Each endpoint is updated to have a pointer to the
// counterparty endpoint.
func NewPath(chainA, chainB TestChainI) *Path {
endpointA := NewDefaultEndpoint(chainA)
endpointB := NewDefaultEndpoint(chainB)
endpointA.Counterparty = endpointB
endpointB.Counterparty = endpointA
return &Path{
EndpointA: endpointA,
EndpointB: endpointB,
}
}
// SetChannelOrdered sets the channel order for both endpoints to ORDERED.
func (path *Path) SetChannelOrdered() {
path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED
path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED
}
// RelayPacket attempts to relay the packet first on EndpointA and then on EndpointB
// if EndpointA does not contain a packet commitment for that packet. An error is returned
// if a relay step fails or the packet commitment does not exist on either endpoint.
func (path *Path) RelayPacket(packet channeltypes.Packet, ack []byte) error {
pc := path.EndpointA.Chain.App().GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointA.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointA.Chain.App().AppCodec(), packet)) {
// packet found, relay from A to B
path.EndpointB.UpdateClient()
if err := path.EndpointB.RecvPacket(packet); err != nil {
return err
}
if ack == nil {
res, err := path.EndpointB.RecvPacketWithResult(packet)
if err != nil {
return err
}
ack2, err := ParseAckFromEvents(res.Events)
if err != nil {
return err
}
ack = ack2
}
if err := path.EndpointA.AcknowledgePacket(packet, ack); err != nil {
return err
}
return nil
}
pc = path.EndpointB.Chain.App().GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointB.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointB.Chain.App().AppCodec(), packet)) {
// packet found, relay B to A
path.EndpointA.UpdateClient()
if err := path.EndpointA.RecvPacket(packet); err != nil {
return err
}
if err := path.EndpointB.AcknowledgePacket(packet, ack); err != nil {
return err
}
return nil
}
return fmt.Errorf("packet commitment does not exist on either endpoint for provided packet")
}
func (path *Path) RelayPacketV4(packet channeltypes.Packet) error {
pc := path.EndpointA.Chain.GetSimApp().GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointA.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointA.Chain.GetSimApp().AppCodec(), packet)) {
// packet found, relay from A to B
if err := path.EndpointB.UpdateClient(); err != nil {
return err
}
res, err := path.EndpointB.RecvPacketWithResult(packet)
if err != nil {
return err
}
ack, err := ParseAckFromEvents(res.Events)
if err != nil {
return err
}
if err := path.EndpointA.AcknowledgePacket(packet, ack); err != nil {
return err
}
return nil
}
pc = path.EndpointB.Chain.GetSimApp().GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointB.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointB.Chain.GetSimApp().AppCodec(), packet)) {
// packet found, relay B to A
if err := path.EndpointA.UpdateClient(); err != nil {
return err
}
res, err := path.EndpointA.RecvPacketWithResult(packet)
if err != nil {
return err
}
ack, err := ParseAckFromEvents(res.Events)
if err != nil {
return err
}
if err := path.EndpointB.AcknowledgePacket(packet, ack); err != nil {
return err
}
return nil
}
return fmt.Errorf("packet commitment does not exist on either endpoint for provided packet")
}