From b9844024b4cb0de9afd264701c279eacb7e1a2c5 Mon Sep 17 00:00:00 2001 From: Victor Farazdagi Date: Wed, 14 Oct 2020 04:44:46 +0300 Subject: [PATCH] Update comments describing init-sync process (#7521) * updates comments * fetcher mode from config Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> --- .../sync/initial-sync/blocks_fetcher.go | 1 + beacon-chain/sync/initial-sync/round_robin.go | 27 ++++++++----------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/beacon-chain/sync/initial-sync/blocks_fetcher.go b/beacon-chain/sync/initial-sync/blocks_fetcher.go index 0f9aaf88f..9fddf2f96 100644 --- a/beacon-chain/sync/initial-sync/blocks_fetcher.go +++ b/beacon-chain/sync/initial-sync/blocks_fetcher.go @@ -133,6 +133,7 @@ func newBlocksFetcher(ctx context.Context, cfg *blocksFetcherConfig) *blocksFetc fetchRequests: make(chan *fetchRequestParams, maxPendingRequests), fetchResponses: make(chan *fetchRequestResponse, maxPendingRequests), capacityWeight: capacityWeight, + mode: cfg.mode, quit: make(chan struct{}), } } diff --git a/beacon-chain/sync/initial-sync/round_robin.go b/beacon-chain/sync/initial-sync/round_robin.go index fd72c5304..00bcb6b4e 100644 --- a/beacon-chain/sync/initial-sync/round_robin.go +++ b/beacon-chain/sync/initial-sync/round_robin.go @@ -26,18 +26,17 @@ const ( // blockReceiverFn defines block receiving function. type blockReceiverFn func(ctx context.Context, block *eth.SignedBeaconBlock, blockRoot [32]byte) error +// batchBlockReceiverFn defines batch receiving function. type batchBlockReceiverFn func(ctx context.Context, blks []*eth.SignedBeaconBlock, roots [][32]byte) error -// Round Robin sync looks at the latest peer statuses and syncs with the highest -// finalized peer. +// Round Robin sync looks at the latest peer statuses and syncs up to the highest known epoch. // // Step 1 - Sync to finalized epoch. -// Sync with peers of lowest finalized root with epoch greater than head state. +// Sync with peers having the majority on best finalized epoch greater than node's head state. // // Step 2 - Sync to head from finalized epoch. -// Using the finalized root as the head_block_root and the epoch start slot -// after the finalized epoch, request blocks to head from some subset of peers -// where step = 1. +// Using enough peers (at least, MinimumSyncPeers*2, for example) obtain best non-finalized epoch, +// known to majority of the peers, and keep fetching blocks, up until that epoch is reached. func (s *Service) roundRobinSync(genesis time.Time) error { ctx, cancel := context.WithCancel(s.ctx) defer cancel() @@ -75,16 +74,13 @@ func (s *Service) roundRobinSync(genesis time.Time) error { log.WithError(err).Debug("Error stopping queue") } + // Already at head, no need for 2nd phase. if s.chain.HeadSlot() == helpers.SlotsSince(genesis) { return nil } - // Step 2 - sync to head from any single peer. - // This step might need to be improved for cases where there has been a long period since - // finality. This step is less important than syncing to finality in terms of threat - // mitigation. We are already convinced that we are on the correct finalized chain. Any blocks - // we receive there after must build on the finalized chain or be considered invalid during - // fork choice resolution / block processing. + // Step 2 - sync to head from majority of peers (from no less than MinimumSyncPeers*2 peers) having the same + // world view on non-finalized epoch. queue = newBlocksQueue(ctx, &blocksQueueConfig{ p2p: s.p2p, headFetcher: s.chain, @@ -114,16 +110,16 @@ func (s *Service) processFetchedData( ctx context.Context, genesis time.Time, startSlot uint64, data *blocksQueueFetchedData) { defer s.updatePeerScorerStats(data.pid, startSlot) - blockReceiver := s.chain.ReceiveBlockInitialSync - batchReceiver := s.chain.ReceiveBlockBatch - // Use Batch Block Verify to process and verify batches directly. if featureconfig.Get().BatchBlockVerify { + batchReceiver := s.chain.ReceiveBlockBatch if err := s.processBatchedBlocks(ctx, genesis, data.blocks, batchReceiver); err != nil { log.WithError(err).Debug("Batch is not processed") } return } + + blockReceiver := s.chain.ReceiveBlockInitialSync for _, blk := range data.blocks { if err := s.processBlock(ctx, genesis, blk, blockReceiver); err != nil { log.WithError(err).Debug("Block is not processed") @@ -138,7 +134,6 @@ func (s *Service) processFetchedDataRegSync( defer s.updatePeerScorerStats(data.pid, startSlot) blockReceiver := s.chain.ReceiveBlock - for _, blk := range data.blocks { if err := s.processBlock(ctx, genesis, blk, blockReceiver); err != nil { log.WithError(err).Debug("Block is not processed")