-
Notifications
You must be signed in to change notification settings - Fork 46
/
helpers.go
90 lines (72 loc) · 2.71 KB
/
helpers.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
package testutil
import (
"bytes"
"fmt"
"golang.org/x/exp/slices"
sdk "github.com/cosmos/cosmos-sdk/types"
channeltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
ibcgotesting "github.com/AltheaFoundation/althea-L1/ibcutils/testing"
)
// 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 RelayPacket(path *ibcgotesting.Path, packet channeltypes.Packet) (*sdk.Result, 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
if err := path.EndpointB.UpdateClient(); err != nil {
return nil, err
}
res, err := path.EndpointB.RecvPacketWithResult(packet)
if err != nil {
return nil, err
}
fmt.Printf("Acks: %v\n", path.EndpointB.Chain.App.GetIBCKeeper().ChannelKeeper.GetAllPacketAcks(path.EndpointB.Chain.GetContext()))
ack, err := ibcgotesting.ParseAckFromEvents(res.GetEvents())
if err != nil {
return nil, err
}
if err := path.EndpointA.AcknowledgePacket(packet, ack); err != nil {
return nil, err
}
return res, 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
if err := path.EndpointA.UpdateClient(); err != nil {
return nil, err
}
res, err := path.EndpointA.RecvPacketWithResult(packet)
if err != nil {
return nil, err
}
ack, err := ibcgotesting.ParseAckFromEvents(res.GetEvents())
if err != nil {
return nil, err
}
if err := path.EndpointB.AcknowledgePacket(packet, ack); err != nil {
return nil, err
}
return res, nil
}
return nil, fmt.Errorf("packet commitment does not exist on either endpoint for provided packet")
}
func FindEvent(events []sdk.Event, name string) sdk.Event {
index := slices.IndexFunc(events, func(e sdk.Event) bool { return e.Type == name })
if index == -1 {
// nolint: exhaustruct
return sdk.Event{}
}
return events[index]
}
func ExtractAttributes(event sdk.Event) map[string]string {
attrs := make(map[string]string)
if event.Attributes == nil {
return attrs
}
for _, a := range event.Attributes {
attrs[string(a.Key)] = string(a.Value)
}
return attrs
}