forked from datachainlab/ethereum-ibc-relay-chain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
195 lines (170 loc) · 5.36 KB
/
events.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package ethereum
import (
"fmt"
"math/big"
"strings"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/hyperledger-labs/yui-relayer/core"
"github.com/neoiss/ethereum-ibc-relay-chain/pkg/contract/ibchandler"
)
var (
abiIBCHandler abi.ABI
abiSendPacket,
abiRecvPacket,
abiWriteAcknowledgement abi.Event
)
func init() {
var err error
abiIBCHandler, err = abi.JSON(strings.NewReader(ibchandler.IbchandlerABI))
if err != nil {
panic(err)
}
abiSendPacket = abiIBCHandler.Events["SendPacket"]
abiRecvPacket = abiIBCHandler.Events["RecvPacket"]
abiWriteAcknowledgement = abiIBCHandler.Events["WriteAcknowledgement"]
}
func (chain *Chain) findSentPackets(ctx core.QueryContext, fromHeight uint64) (core.PacketInfoList, error) {
var dstPortID, dstChannelID string
if channel, found, err := chain.ibcHandler.GetChannel(
chain.callOptsFromQueryContext(ctx),
chain.Path().PortID,
chain.Path().ChannelID,
); err != nil {
return nil, err
} else if !found {
return nil, fmt.Errorf("channel not found: sourcePortID=%v sourceChannel=%v", chain.Path().PortID, chain.Path().ChannelID)
} else {
dstPortID = channel.Counterparty.PortId
dstChannelID = channel.Counterparty.ChannelId
}
query := ethereum.FilterQuery{
FromBlock: big.NewInt(int64(fromHeight)),
ToBlock: big.NewInt(int64(ctx.Height().GetRevisionHeight())),
Addresses: []common.Address{
chain.config.IBCAddress(),
},
Topics: [][]common.Hash{{
abiSendPacket.ID,
}},
}
logs, err := chain.client.FilterLogs(ctx.Context(), query)
if err != nil {
return nil, err
}
var packets core.PacketInfoList
for _, log := range logs {
height := clienttypes.NewHeight(0, log.BlockNumber)
var sendPacket ibchandler.IbchandlerSendPacket
if err := abiIBCHandler.UnpackIntoInterface(&sendPacket, "SendPacket", log.Data); err != nil {
return nil, err
}
packet := &core.PacketInfo{
Packet: channeltypes.NewPacket(
sendPacket.Data,
sendPacket.Sequence,
sendPacket.SourcePort,
sendPacket.SourceChannel,
dstPortID,
dstChannelID,
clienttypes.Height(sendPacket.TimeoutHeight),
sendPacket.TimeoutTimestamp,
),
EventHeight: height,
}
packets = append(packets, packet)
}
return packets, nil
}
func (chain *Chain) findReceivedPackets(ctx core.QueryContext, fromHeight uint64) (core.PacketInfoList, error) {
recvPacketEvents, err := chain.findRecvPacketEvents(ctx, fromHeight)
if err != nil {
return nil, err
} else if len(recvPacketEvents) == 0 {
return nil, nil
}
writeAckEvents, err := chain.findWriteAckEvents(ctx, recvPacketEvents[0].Raw.BlockNumber)
if err != nil {
return nil, err
} else if len(writeAckEvents) == 0 {
return nil, nil
}
var packets core.PacketInfoList
for _, rp := range recvPacketEvents {
for _, wa := range writeAckEvents {
if rp.Packet.Sequence == wa.Sequence {
packets = append(packets, &core.PacketInfo{
Packet: channeltypes.Packet{
Sequence: rp.Packet.Sequence,
SourcePort: rp.Packet.SourcePort,
SourceChannel: rp.Packet.SourceChannel,
DestinationPort: rp.Packet.DestinationPort,
DestinationChannel: rp.Packet.DestinationChannel,
Data: rp.Packet.Data,
TimeoutHeight: clienttypes.Height(rp.Packet.TimeoutHeight),
TimeoutTimestamp: rp.Packet.TimeoutTimestamp,
},
Acknowledgement: wa.Acknowledgement, // todo 这里的 ack 的格式是否正确?是否需要处理?
EventHeight: clienttypes.NewHeight(0, rp.Raw.BlockNumber),
})
break
}
}
}
return packets, nil
}
func (chain *Chain) findRecvPacketEvents(ctx core.QueryContext, fromHeight uint64) ([]ibchandler.IbchandlerRecvPacket, error) {
query := ethereum.FilterQuery{
FromBlock: big.NewInt(int64(fromHeight)),
ToBlock: big.NewInt(int64(ctx.Height().GetRevisionHeight())),
Addresses: []common.Address{
chain.config.IBCAddress(),
},
Topics: [][]common.Hash{{
abiRecvPacket.ID,
}},
}
logs, err := chain.client.FilterLogs(ctx.Context(), query)
if err != nil {
return nil, err
}
var events []ibchandler.IbchandlerRecvPacket
for _, log := range logs {
var event ibchandler.IbchandlerRecvPacket
if err := abiIBCHandler.UnpackIntoInterface(&event, "RecvPacket", log.Data); err != nil {
return nil, err
}
event.Raw = log
events = append(events, event)
}
return events, nil
}
func (chain *Chain) findWriteAckEvents(ctx core.QueryContext, fromHeight uint64) ([]ibchandler.IbchandlerWriteAcknowledgement, error) {
query := ethereum.FilterQuery{
FromBlock: big.NewInt(int64(fromHeight)),
ToBlock: big.NewInt(int64(ctx.Height().GetRevisionHeight())),
Addresses: []common.Address{
chain.config.IBCAddress(),
},
Topics: [][]common.Hash{{
abiWriteAcknowledgement.ID,
}},
}
logs, err := chain.client.FilterLogs(ctx.Context(), query)
if err != nil {
return nil, err
}
var events []ibchandler.IbchandlerWriteAcknowledgement
for _, log := range logs {
var event ibchandler.IbchandlerWriteAcknowledgement
if err := abiIBCHandler.UnpackIntoInterface(&event, "WriteAcknowledgement", log.Data); err != nil {
return nil, err
}
event.Raw = log
events = append(events, event)
}
return events, nil
}