sync synchronizes blocks and queries from the other peers.
()
| 715 | |
| 716 | // sync synchronizes blocks and queries from the other peers. |
| 717 | func (c *Chain) sync() (err error) { |
| 718 | le := c.logEntry() |
| 719 | le.Debug("synchronizing chain state") |
| 720 | defer func() { |
| 721 | c.stat() |
| 722 | c.pruneBlockCache() |
| 723 | c.ai.advance(c.rt.getMinValidHeight()) |
| 724 | }() |
| 725 | for { |
| 726 | now := c.rt.now() |
| 727 | height := c.rt.getHeightFromTime(now) |
| 728 | if now.Before(c.rt.chainInitTime) { |
| 729 | le.Debug("now time is before genesis time, waiting for genesis") |
| 730 | return |
| 731 | } |
| 732 | if c.rt.getNextTurn() > height { |
| 733 | break |
| 734 | } |
| 735 | for c.rt.getNextTurn() <= height { |
| 736 | if err = c.syncHead(); err != nil { |
| 737 | if err != ErrInitiating { |
| 738 | le.WithError(err).Errorf("failed to sync block at height %d", height) |
| 739 | return |
| 740 | } |
| 741 | // Skip sync and reset error |
| 742 | c.rt.SetNextTurn(height + 1) |
| 743 | err = nil |
| 744 | } else { |
| 745 | c.rt.IncNextTurn() |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | return |
| 750 | } |
| 751 | |
| 752 | func (c *Chain) processBlocks(ctx context.Context) { |
| 753 | var ( |
no test coverage detected