forked from ava-labs/ortelius
/
producer_cchain.go
659 lines (555 loc) · 15 KB
/
producer_cchain.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// (c) 2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package stream
import (
"context"
"encoding/json"
"fmt"
"io"
"math/big"
"strings"
"sync"
"time"
"github.com/corpetty/avalanchego/ids"
cblock "github.com/corpetty/ortelius/models"
"github.com/corpetty/avalanchego/utils/wrappers"
"github.com/corpetty/ortelius/utils"
"github.com/corpetty/coreth/core/types"
"github.com/corpetty/avalanchego/utils/hashing"
"github.com/corpetty/coreth"
"github.com/corpetty/coreth/ethclient"
"github.com/corpetty/coreth/rpc"
"github.com/corpetty/ortelius/services"
"github.com/corpetty/ortelius/cfg"
"github.com/corpetty/ortelius/services/metrics"
)
const (
rpcTimeout = time.Minute
dbReadTimeout = 10 * time.Second
dbWriteTimeout = time.Minute
readRPCTimeout = 500 * time.Millisecond
blocksToQueue = 25
)
type ProducerCChain struct {
id string
sc *services.Control
// metrics
metricProcessedCountKey string
metricSuccessCountKey string
metricFailureCountKey string
rpcClient *rpc.Client
ethClient *ethclient.Client
conns *services.Connections
block *big.Int
conf cfg.Config
// Concurrency control
quitCh chan struct{}
doneCh chan struct{}
topic string
topicTrc string
blockCount *big.Int
ethClientLock sync.Mutex
}
func NewProducerCChain() utils.ListenCloserFactory {
return func(sc *services.Control, conf cfg.Config, _ int, _ int) utils.ListenCloser {
topicName := fmt.Sprintf("%d-%s-cchain", conf.NetworkID, conf.CchainID)
topicTrcName := fmt.Sprintf("%d-%s-cchain-trc", conf.NetworkID, conf.CchainID)
p := &ProducerCChain{
topic: topicName,
topicTrc: topicTrcName,
conf: conf,
sc: sc,
metricProcessedCountKey: fmt.Sprintf("produce_records_processed_%s_cchain", conf.CchainID),
metricSuccessCountKey: fmt.Sprintf("produce_records_success_%s_cchain", conf.CchainID),
metricFailureCountKey: fmt.Sprintf("produce_records_failure_%s_cchain", conf.CchainID),
id: fmt.Sprintf("producer %d %s cchain", conf.NetworkID, conf.CchainID),
quitCh: make(chan struct{}),
doneCh: make(chan struct{}),
}
metrics.Prometheus.CounterInit(p.metricProcessedCountKey, "records processed")
metrics.Prometheus.CounterInit(p.metricSuccessCountKey, "records success")
metrics.Prometheus.CounterInit(p.metricFailureCountKey, "records failure")
sc.InitProduceMetrics()
return p
}
}
// Close shuts down the producer
func (p *ProducerCChain) Close() error {
close(p.quitCh)
<-p.doneCh
return nil
}
func (p *ProducerCChain) ID() string {
return p.id
}
type TracerParam struct {
Tracer string `json:"tracer"`
}
func (p *ProducerCChain) readBlockFromRPC(blockNumber *big.Int) (*types.Block, []*cblock.TransactionTrace, error) {
p.ethClientLock.Lock()
defer p.ethClientLock.Unlock()
ctx, cancelCTX := context.WithTimeout(context.Background(), rpcTimeout)
defer cancelCTX()
bl, err := p.ethClient.BlockByNumber(ctx, blockNumber)
if err == coreth.NotFound {
return nil, nil, ErrNoMessage
}
if err != nil {
return nil, nil, err
}
txTraces := make([]*cblock.TransactionTrace, 0, len(bl.Transactions()))
for _, tx := range bl.Transactions() {
txh := tx.Hash().Hex()
if !strings.HasPrefix(txh, "0x") {
txh = "0x" + txh
}
var results []interface{}
err = p.rpcClient.CallContext(ctx, &results, "debug_traceTransaction", txh, TracerParam{Tracer: utils.Tracer})
if err != nil {
return nil, nil, err
}
for ipos, result := range results {
traceBits, err := json.Marshal(result)
if err != nil {
return nil, nil, err
}
txTraces = append(txTraces,
&cblock.TransactionTrace{
Hash: txh,
Idx: uint32(ipos),
Trace: traceBits,
},
)
}
}
return bl, txTraces, nil
}
func (p *ProducerCChain) updateTxPool(txPool *services.TxPool) error {
sess := p.conns.DB().NewSessionForEventReceiver(p.conns.StreamDBDedup().NewJob("update-tx-pool"))
ctx, cancelCtx := context.WithTimeout(context.Background(), dbWriteTimeout)
defer cancelCtx()
return p.sc.Persist.InsertTxPool(ctx, sess, txPool)
}
func (p *ProducerCChain) updateBlock(blockNumber *big.Int, updateTime time.Time) error {
sess := p.conns.DB().NewSessionForEventReceiver(p.conns.StreamDBDedup().NewJob("update-block"))
ctx, cancelCtx := context.WithTimeout(context.Background(), dbWriteTimeout)
defer cancelCtx()
cvmBlocks := &services.CvmBlocks{
Block: blockNumber.String(),
CreatedAt: updateTime,
}
return p.sc.Persist.InsertCvmBlocks(ctx, sess, cvmBlocks)
}
func (p *ProducerCChain) fetchLatest() (uint64, error) {
ctx, cancelCTX := context.WithTimeout(context.Background(), rpcTimeout)
defer cancelCTX()
return p.ethClient.BlockNumber(ctx)
}
type localBlockObject struct {
block *types.Block
time time.Time
traces []*cblock.TransactionTrace
}
func (p *ProducerCChain) ProcessNextMessage() error {
current := big.NewInt(0).Set(p.block)
localBlocks := make([]*localBlockObject, 0, blocksToQueue)
consumeBlock := func() error {
if len(localBlocks) == 0 {
return nil
}
defer func() {
localBlocks = make([]*localBlockObject, 0, blocksToQueue)
}()
for _, bl := range localBlocks {
wp := &WorkPacketCChain{localBlock: bl}
err := p.processWork(wp)
if err != nil {
return err
}
}
for _, bl := range localBlocks {
err := p.updateBlock(bl.block.Number(), time.Now().UTC())
if err != nil {
return err
}
p.block.Set(bl.block.Number())
}
return nil
}
defer func() {
err := consumeBlock()
if err != nil {
p.sc.Log.Warn("consume block error %v", err)
}
}()
for !p.isStopping() {
lblock, err := p.fetchLatest()
if err != nil {
time.Sleep(readRPCTimeout)
return err
}
lblocknext := big.NewInt(0).SetUint64(lblock)
if lblocknext.Cmp(current) <= 0 {
time.Sleep(readRPCTimeout)
return ErrNoMessage
}
for !p.isStopping() && lblocknext.Cmp(current) > 0 {
ncurrent := big.NewInt(0).Add(current, big.NewInt(1))
bl, traces, err := p.readBlockFromRPC(ncurrent)
if err != nil {
time.Sleep(readRPCTimeout)
return err
}
_ = metrics.Prometheus.CounterInc(p.metricProcessedCountKey)
_ = metrics.Prometheus.CounterInc(services.MetricProduceProcessedCountKey)
localBlocks = append(localBlocks, &localBlockObject{block: bl, traces: traces, time: time.Now().UTC()})
if len(localBlocks) > blocksToQueue {
err = consumeBlock()
if err != nil {
return err
}
}
current = big.NewInt(0).Add(current, big.NewInt(1))
}
err = consumeBlock()
if err != nil {
return err
}
}
return nil
}
func (p *ProducerCChain) Failure() {
_ = metrics.Prometheus.CounterInc(p.metricFailureCountKey)
_ = metrics.Prometheus.CounterInc(services.MetricProduceFailureCountKey)
}
func (p *ProducerCChain) Success() {
_ = metrics.Prometheus.CounterInc(p.metricSuccessCountKey)
_ = metrics.Prometheus.CounterInc(services.MetricProduceSuccessCountKey)
}
func (p *ProducerCChain) getBlock() error {
var err error
job := p.conns.Stream().NewJob("get-block")
sess := p.conns.DB().NewSessionForEventReceiver(job)
ctx, cancelCtx := context.WithTimeout(context.Background(), dbReadTimeout)
defer cancelCtx()
type MaxBlock struct {
Block string
BlockCount string
}
maxBlock := MaxBlock{}
_, err = sess.Select(
"cast(case when max(block) is null then -1 else max(block) end as char) as block",
"cast(count(*) as char) as block_count",
).
From(services.TableCvmBlocks).
LoadContext(ctx, &maxBlock)
if err != nil {
return err
}
mblock, ok := big.NewInt(0).SetString(maxBlock.Block, 10)
if !ok {
return fmt.Errorf("invalid block %s", maxBlock.Block)
}
cblock, ok := big.NewInt(0).SetString(maxBlock.BlockCount, 10)
if !ok {
return fmt.Errorf("invalid block %s", maxBlock.BlockCount)
}
p.block = mblock
p.blockCount = cblock
p.sc.Log.Info("starting processing block %s cnt %s", p.block.String(), p.blockCount.String())
return nil
}
func (p *ProducerCChain) Listen() error {
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
p.sc.Log.Info("Started worker manager for cchain")
defer p.sc.Log.Info("Exiting worker manager for cchain")
defer wg.Done()
// Keep running the worker until we're asked to stop
var err error
for !p.isStopping() {
err = p.runProcessor()
// If there was an error we want to log it, and iff we are not stopping
// we want to add a retry delay.
if err != nil {
p.sc.Log.Error("Error running worker: %s", err.Error())
}
if p.isStopping() {
return
}
if err != nil {
<-time.After(processorFailureRetryInterval)
}
}
}()
// Wait for all workers to finish
wg.Wait()
p.sc.Log.Info("All workers stopped")
close(p.doneCh)
return nil
}
// isStopping returns true iff quitCh has been signaled
func (p *ProducerCChain) isStopping() bool {
select {
case <-p.quitCh:
return true
default:
return false
}
}
func (p *ProducerCChain) init() error {
conns, err := p.sc.DatabaseOnly()
if err != nil {
return err
}
p.conns = conns
err = p.getBlock()
if err != nil {
return err
}
rc, err := rpc.Dial(p.conf.Producer.CChainRPC)
if err != nil {
return err
}
p.rpcClient = rc
p.ethClient = ethclient.NewClient(rc)
return nil
}
func (p *ProducerCChain) processorClose() error {
p.sc.Log.Info("close %s", p.id)
if p.rpcClient != nil {
p.rpcClient.Close()
}
errs := wrappers.Errs{}
if p.conns != nil {
errs.Add(p.conns.Close())
}
return errs.Err
}
// runProcessor starts the processing loop for the backend and closes it when
// finished
func (p *ProducerCChain) runProcessor() error {
if p.isStopping() {
p.sc.Log.Info("Not starting worker for cchain because we're stopping")
return nil
}
p.sc.Log.Info("Starting worker for cchain")
defer p.sc.Log.Info("Exiting worker for cchain")
defer func() {
err := p.processorClose()
if err != nil {
p.sc.Log.Warn("Stopping worker for cchain %w", err)
}
}()
err := p.init()
if err != nil {
return err
}
pblockp1 := big.NewInt(0).Add(p.block, big.NewInt(1))
if p.blockCount.Cmp(pblockp1) < 0 {
go p.catchupBlock(pblockp1)
}
// Create a closure that processes the next message from the backend
var (
successes int
failures int
nomsg int
processNextMessage = func() error {
err := p.ProcessNextMessage()
switch err {
case nil:
successes++
p.Success()
return nil
// This error is expected when the upstream service isn't producing
case context.DeadlineExceeded:
nomsg++
p.sc.Log.Debug("context deadline exceeded")
return nil
case ErrNoMessage:
nomsg++
p.sc.Log.Debug("no message")
return nil
case io.EOF:
p.sc.Log.Error("EOF")
return io.EOF
default:
if strings.HasPrefix(err.Error(), "404 Not Found") {
p.sc.Log.Warn("%s", err.Error())
return nil
}
if strings.HasPrefix(err.Error(), "503 Service Unavailable") {
p.sc.Log.Warn("%s", err.Error())
return nil
}
if strings.HasSuffix(err.Error(), "connect: connection refused") {
p.sc.Log.Warn("%s", err.Error())
return nil
}
if strings.HasSuffix(err.Error(), "read: connection reset by peer") {
p.sc.Log.Warn("%s", err.Error())
return nil
}
failures++
p.Failure()
p.sc.Log.Error("Unknown error: %v", err)
return err
}
}
)
id := p.ID()
t := time.NewTicker(30 * time.Second)
tdoneCh := make(chan struct{})
defer func() {
t.Stop()
close(tdoneCh)
}()
// Log run statistics periodically until asked to stop
go func() {
for {
select {
case <-t.C:
p.sc.Log.Info("IProcessor %s successes=%d failures=%d nomsg=%d", id, successes, failures, nomsg)
if p.isStopping() {
return
}
case <-tdoneCh:
return
}
}
}()
// Process messages until asked to stop
for !p.isStopping() {
err := processNextMessage()
if err != nil {
return err
}
}
return nil
}
type WorkPacketCChain struct {
localBlock *localBlockObject
}
func (p *ProducerCChain) processWork(wp *WorkPacketCChain) error {
cblk, err := cblock.New(wp.localBlock.block)
if err != nil {
return err
}
if cblk == nil {
return fmt.Errorf("cblock is nil")
}
// wipe before re-encoding
cblk.Txs = nil
block, err := json.Marshal(cblk)
if err != nil {
return err
}
id, err := ids.ToID(hashing.ComputeHash256(block))
if err != nil {
return err
}
txPool := &services.TxPool{
NetworkID: p.conf.NetworkID,
ChainID: p.conf.CchainID,
MsgKey: id.String(),
Serialization: block,
Processed: 0,
Topic: p.topic,
CreatedAt: wp.localBlock.time,
}
err = txPool.ComputeID()
if err != nil {
return err
}
err = p.updateTxPool(txPool)
if err != nil {
return err
}
for _, txTranactionTraces := range wp.localBlock.traces {
txTransactionTracesBits, err := json.Marshal(txTranactionTraces)
if err != nil {
return err
}
id, err := ids.ToID(hashing.ComputeHash256(txTransactionTracesBits))
if err != nil {
return err
}
txPool := &services.TxPool{
NetworkID: p.conf.NetworkID,
ChainID: p.conf.CchainID,
MsgKey: id.String(),
Serialization: txTransactionTracesBits,
Processed: 0,
Topic: p.topicTrc,
CreatedAt: wp.localBlock.time,
}
err = txPool.ComputeID()
if err != nil {
return err
}
err = p.updateTxPool(txPool)
if err != nil {
return err
}
}
return nil
}
func (p *ProducerCChain) catchupBlock(catchupBlock *big.Int) {
conns, err := p.sc.DatabaseOnly()
if err != nil {
p.sc.Log.Warn("catchupBock %v", err)
return
}
defer func() {
_ = conns.Close()
}()
sess := p.conns.DB().NewSessionForEventReceiver(p.conns.StreamDBDedup().NewJob("catchup-block"))
ctx := context.Background()
startBlock := big.NewInt(0)
endBlock := big.NewInt(0)
for endBlock.Cmp(catchupBlock) < 0 {
endBlock = big.NewInt(0).Add(startBlock, big.NewInt(100000))
if endBlock.Cmp(catchupBlock) >= 0 {
endBlock.Set(catchupBlock)
}
var cvmBlocks []*services.CvmBlocks
_, err = sess.Select(
"block",
).From(services.TableCvmBlocks).
Where("block >= "+startBlock.String()+" and block < "+endBlock.String()).
LoadContext(ctx, &cvmBlocks)
if err != nil {
p.sc.Log.Warn("catchupBock %v", err)
return
}
blockMap := make(map[string]struct{})
for _, bl := range cvmBlocks {
blockMap[bl.Block] = struct{}{}
}
for startBlock.Cmp(endBlock) < 0 {
if _, ok := blockMap[startBlock.String()]; !ok {
p.sc.Log.Info("refill %v", startBlock.String())
bl, traces, err := p.readBlockFromRPC(startBlock)
if err != nil {
p.sc.Log.Warn("catchupBock %v", err)
return
}
localBlockObject := &localBlockObject{block: bl, traces: traces, time: time.Now().UTC()}
wp := &WorkPacketCChain{localBlock: localBlockObject}
err = p.processWork(wp)
if err != nil {
p.sc.Log.Warn("catchupBock %v", err)
return
}
err = p.updateBlock(startBlock, time.Now().UTC())
if err != nil {
p.sc.Log.Warn("catchupBock %v", err)
return
}
}
startBlock = big.NewInt(0).Add(startBlock, big.NewInt(1))
}
}
p.sc.Log.Info("catchup complete")
}