(ctx context.Context)
| 578 | } |
| 579 | |
| 580 | func (c *Chain) mainCycle(ctx context.Context) { |
| 581 | var timer = time.NewTimer(0) |
| 582 | defer func() { |
| 583 | if !timer.Stop() { |
| 584 | <-timer.C |
| 585 | } |
| 586 | }() |
| 587 | for { |
| 588 | select { |
| 589 | case <-timer.C: |
| 590 | // Try to fetch block at height `nextHeight-1` until enough peers are reachable |
| 591 | if err := c.blockingSyncCurrentHead(ctx, c.getRequiredConfirms()); err != nil { |
| 592 | log.WithError(err).Info("abort main cycle") |
| 593 | timer.Reset(0) |
| 594 | return |
| 595 | } |
| 596 | |
| 597 | var t, d = c.nextTick() |
| 598 | if d <= 0 { |
| 599 | // Try to produce block at `nextHeight` if it's my turn, and increase height by 1 |
| 600 | c.advanceNextHeight(t, d) |
| 601 | } else { |
| 602 | log.WithFields(log.Fields{ |
| 603 | "peer": c.getLocalBPInfo(), |
| 604 | "next_height": c.getNextHeight(), |
| 605 | "head_height": c.head().height, |
| 606 | "head_block": c.head().hash.Short(4), |
| 607 | "now_time": t.Format(time.RFC3339Nano), |
| 608 | "duration": d, |
| 609 | }).Debug("main cycle") |
| 610 | } |
| 611 | timer.Reset(d) |
| 612 | case <-ctx.Done(): |
| 613 | log.WithError(ctx.Err()).Info("abort main cycle") |
| 614 | return |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | func (c *Chain) blockingSyncCurrentHead(ctx context.Context, requiredReachable uint32) (err error) { |
| 620 | var ( |
nothing calls this directly
no test coverage detected