-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
389 lines (339 loc) · 11 KB
/
main.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package main
import (
"context"
"crypto/ecdsa"
"log"
"math/big"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/pkg/errors"
"github.com/jannikluhn/terminal/node/contracts/endpointcontract"
"github.com/jannikluhn/terminal/node/contracts/oracle"
)
const sinkAddressHex = "0xBe0B0f08A599F07699E98A9D001084e97b9a900A"
const sinkOracleAddressHex = "0xFA33c8EF8b5c4f3003361c876a298D1DB61ccA4e"
const sinkPrivateKeyHex = "b0057716d5917badaf911b193b12b910811c1497b5bada8d7711f758981c3773"
const sourceAddressHex = "0x4fd74C68a8c68610Bd40BdC550f195bD367Ff238"
const sourceOracleAddressHex = "0x14b47DBb4b0F7bff4205F4D1f0c1BEEBCE47b33C"
const sourcePrivateKeyHex = "b0057716d5917badaf911b193b12b910811c1497b5bada8d7711f758981c3774"
const sinkRpcUrl = "ws://127.0.0.1:8546/"
const sourceRpcUrl = "ws://127.0.0.1:8556/"
type Connection struct {
Client *ethclient.Client
ChainID uint64
EndpointContract *endpointcontract.Endpointcontract
OracleContract *oracle.Oracle
PrivateKey *ecdsa.PrivateKey
}
func NewConnection(ctx context.Context, rpcUrl string, endpointContractAddress common.Address, oracleContractAddress common.Address, privateKey *ecdsa.PrivateKey) (Connection, error) {
client, err := ethclient.Dial(rpcUrl)
if err != nil {
return Connection{}, errors.Wrapf(err, "failed to connect to Ethereum node at %s", rpcUrl)
}
chainID, err := client.ChainID(ctx)
if err != nil {
return Connection{}, errors.Wrap(err, "failed to query chain id")
}
endpointContract, err := endpointcontract.NewEndpointcontract(endpointContractAddress, client)
if err != nil {
return Connection{}, errors.Wrap(err, "faild to create endpoint contract instance")
}
oracleContract, err := oracle.NewOracle(oracleContractAddress, client)
if err != nil {
return Connection{}, errors.Wrap(err, "faild to create oracle contract instance")
}
return Connection{
Client: client,
ChainID: chainID.Uint64(),
EndpointContract: endpointContract,
OracleContract: oracleContract,
PrivateKey: privateKey,
}, nil
}
func (conn *Connection) TransactOpts() (*bind.TransactOpts, error) {
opts, err := bind.NewKeyedTransactorWithChainID(conn.PrivateKey, new(big.Int).SetUint64(conn.ChainID))
if err != nil {
return nil, errors.Wrapf(err, "failed to create transactor")
}
return opts, nil
}
func main() {
err := run()
if err != nil {
log.Fatalf("Fatal error:", err)
}
}
type Transfer struct {
FromEndpoint endpointcontract.Endpoint
TransactionHash common.Hash
BlockNumber uint64
LogIndex uint64
ToEndpoint endpointcontract.Endpoint
Receiver common.Address
Amount *big.Int
}
type Request struct {
StartRequestEvent *oracle.OracleStartRequest
ChainID uint64
}
func (r *Request) TransferIdentifier() oracle.OracleTransferIdentifier {
return oracle.OracleTransferIdentifier{
ChainID: r.StartRequestEvent.ChaindID,
BlockNumber: r.StartRequestEvent.BlockNumber,
LogIndex: uint32(r.StartRequestEvent.LogIndex),
TxHash: r.StartRequestEvent.TxHash,
}
}
func (t *Transfer) TransferIdentifier() oracle.OracleTransferIdentifier {
return oracle.OracleTransferIdentifier{
ChainID: t.FromEndpoint.ChainID,
BlockNumber: t.BlockNumber,
TxHash: t.TransactionHash,
LogIndex: uint32(t.LogIndex),
}
}
func NewTransferFromRequestEvent(chainID uint64, ev *endpointcontract.EndpointcontractTransferRequested) Transfer {
return Transfer{
FromEndpoint: endpointcontract.Endpoint{
ChainID: chainID,
ContractAddress: ev.Raw.Address,
},
TransactionHash: ev.Raw.TxHash,
BlockNumber: ev.Raw.BlockNumber,
LogIndex: uint64(ev.Raw.Index),
ToEndpoint: ev.Endpoint,
Receiver: ev.Receiver,
Amount: ev.Amount,
}
}
func listenForTransfers(ctx context.Context, conn Connection) (chan Transfer, chan error) {
requestChannel := make(chan Transfer)
errorChannel := make(chan error)
go func() {
fail := func(err error) {
errorChannel <- err
close(errorChannel)
close(requestChannel)
}
watchOpts := &bind.WatchOpts{
Context: ctx,
Start: nil,
}
transferRequestedChannel := make(chan *endpointcontract.EndpointcontractTransferRequested)
sub, err := conn.EndpointContract.EndpointcontractFilterer.WatchTransferRequested(watchOpts, transferRequestedChannel)
if err != nil {
fail(errors.Wrap(err, "faild to subscribe to TransferRequested events"))
return
}
for {
select {
case err := <-sub.Err():
fail(errors.Wrap(err, "event subscription error"))
return
case ev := <-transferRequestedChannel:
transfer := NewTransferFromRequestEvent(conn.ChainID, ev)
requestChannel <- transfer
case <-ctx.Done():
close(errorChannel)
close(requestChannel)
return
}
}
}()
return requestChannel, errorChannel
}
func processTransfer(ctx context.Context, connections map[uint64]Connection, transfer Transfer) error {
toConn, ok := connections[transfer.ToEndpoint.ChainID]
if !ok {
return errors.Errorf("received transfer request to unknown chain id %d: %+v", transfer.ToEndpoint.ChainID, transfer)
}
// check if request is already handled by someone else
callOpts := &bind.CallOpts{
Pending: false,
Context: ctx,
}
exists, err := toConn.OracleContract.RequestExists(callOpts, transfer.TransferIdentifier())
if err != nil {
return errors.Wrapf(err, "failed to check if transfer %+v already exists", transfer)
}
if exists {
log.Printf("Transfer %+v is already handled", transfer)
return nil
}
transactOpts, err := toConn.TransactOpts()
if err != nil {
return err
}
tx, err := toConn.OracleContract.StartRequest(transactOpts, transfer.TransferIdentifier(), transfer.Amount, transfer.Receiver)
if err != nil {
return errors.Wrapf(err, "error starting request for transfer %+v", transfer)
}
log.Printf("Sent StartRequest tx with hash %s for transfer %+v", tx.Hash(), transfer)
return nil
}
func listenForRequests(ctx context.Context, conn Connection) (chan Request, chan error) {
requestChannel := make(chan Request)
errorChannel := make(chan error)
go func() {
fail := func(err error) {
errorChannel <- err
close(errorChannel)
close(requestChannel)
}
watchOpts := &bind.WatchOpts{
Context: ctx,
Start: nil,
}
eventChannel := make(chan *oracle.OracleStartRequest)
sub, err := conn.OracleContract.OracleFilterer.WatchStartRequest(watchOpts, eventChannel)
if err != nil {
fail(errors.Wrap(err, "faild to subscribe to TransferRequested events"))
return
}
for {
select {
case err := <-sub.Err():
fail(errors.Wrap(err, "event subscription error"))
return
case ev := <-eventChannel:
request := Request{
StartRequestEvent: ev,
ChainID: conn.ChainID,
}
requestChannel <- request
case <-ctx.Done():
close(errorChannel)
close(requestChannel)
return
}
}
}()
return requestChannel, errorChannel
}
func watchRequest(ctx context.Context, connections map[uint64]Connection, request Request) {
log.Println("Start monitoring request")
for {
done, err := watchRequestStep(ctx, connections, request)
if err != nil {
log.Printf("error watching request:", err)
return
}
if done {
log.Printf("Done watching request")
return
}
time.Sleep(1 * time.Second)
}
}
func watchRequestStep(ctx context.Context, connections map[uint64]Connection, request Request) (bool, error) {
conn, ok := connections[request.ChainID]
if !ok {
return false, errors.Errorf("unknown chain id %d", request.ChainID)
}
block, err := conn.Client.BlockByNumber(ctx, nil)
if err != nil {
return false, errors.Wrapf(err, "failed to query block")
}
callOpts := &bind.CallOpts{
Pending: true,
Context: ctx,
}
requestStatus, err := conn.OracleContract.GetRequest(callOpts, request.TransferIdentifier())
if err != nil {
return false, errors.Wrapf(err, "failed to query request status")
}
challengePeriod, err := conn.OracleContract.ChallengePeriod(callOpts)
if err != nil {
return false, errors.Wrapf(err, "failed to query challenge period")
}
if requestStatus.Claimed {
return true, nil
}
if requestStatus.LastChallengeTime+challengePeriod >= uint32(block.Time()) {
return false, nil
}
transactOpts, err := conn.TransactOpts()
if err != nil {
return false, err
}
tx, err := conn.OracleContract.ClaimRequest(transactOpts, request.TransferIdentifier())
if err != nil {
return false, err
}
log.Printf("Claiming request: %s", tx.Hash())
return true, nil
}
func run() error {
ctx := context.Background()
sinkPrivateKey, err := crypto.HexToECDSA(sinkPrivateKeyHex)
if err != nil {
return errors.Wrapf(err, "got invalid private key")
}
sourcePrivateKey, err := crypto.HexToECDSA(sourcePrivateKeyHex)
if err != nil {
return errors.Wrapf(err, "got invalid private key")
}
sinkConnection, err := NewConnection(
ctx,
sinkRpcUrl,
common.HexToAddress(sinkAddressHex),
common.HexToAddress(sinkOracleAddressHex),
sinkPrivateKey,
)
if err != nil {
return err
}
sourceConnection, err := NewConnection(
ctx,
sourceRpcUrl,
common.HexToAddress(sourceAddressHex),
common.HexToAddress(sourceOracleAddressHex),
sourcePrivateKey,
)
if err != nil {
return err
}
if sinkConnection.ChainID == sourceConnection.ChainID {
return errors.Errorf("got two endpoint connections with same chain id %d", sinkConnection.ChainID)
}
connections := make(map[uint64]Connection)
connections[sinkConnection.ChainID] = sinkConnection
connections[sourceConnection.ChainID] = sourceConnection
cancelCtx, cancelFn := context.WithCancel(ctx)
defer cancelFn()
sinkTransferChannel, sinkTransferErrorChannel := listenForTransfers(cancelCtx, sinkConnection)
sourceTransferChannel, sourceTransferErrorChannel := listenForTransfers(cancelCtx, sourceConnection)
sinkRequestChannel, sinkRequestErrorChannel := listenForRequests(cancelCtx, sinkConnection)
sourceRequestChannel, sourceRequestErrorChannel := listenForRequests(cancelCtx, sourceConnection)
for {
select {
case err := <-sinkTransferErrorChannel:
return errors.Wrap(err, "error listening to sink transfers")
case err := <-sourceTransferErrorChannel:
return errors.Wrap(err, "error listening to source transfers")
case err := <-sinkRequestErrorChannel:
return errors.Wrap(err, "error listening to sink requests")
case err := <-sourceRequestErrorChannel:
return errors.Wrap(err, "error listening to source requests")
case transfer := <-sinkTransferChannel:
err := processTransfer(cancelCtx, connections, transfer)
if err != nil {
log.Printf("failed to process transfer: %s", err)
}
case transfer := <-sourceTransferChannel:
err := processTransfer(cancelCtx, connections, transfer)
if err != nil {
log.Printf("failed to process transfer: %s", err)
}
case request := <-sinkRequestChannel:
go watchRequest(cancelCtx, connections, request)
case request := <-sourceRequestChannel:
go watchRequest(cancelCtx, connections, request)
case <-time.After(5 * time.Second):
log.Println("listening for events")
}
}
}