mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
0db690df75
* update naming * replace with updated version * more changes * fixed all tests * build and lint * regen protos * fix test * remove outdated code * prestons review * add chunk size * more fixes to chunked responses * handle eof * fix all tests * abstract into common method * add comment * preston's comments * preston's review * preston's review * lint * add encoding methods * gaz * simplify * simplify * lint * change naming * update * handle eof separately * Apply suggestions from code review Co-Authored-By: Preston Van Loon <preston@prysmaticlabs.com> * remove def * preston's review * preston's review * add unit tests * add delay to fix test
180 lines
4.4 KiB
Go
180 lines
4.4 KiB
Go
package initialsync
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"time"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/sync"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/sync/peerstatus"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
eth "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/shared"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/roughtime"
|
|
)
|
|
|
|
var _ = shared.Service(&InitialSync{})
|
|
|
|
type blockchainService interface {
|
|
blockchain.BlockReceiver
|
|
blockchain.HeadFetcher
|
|
blockchain.ChainFeeds
|
|
}
|
|
|
|
const (
|
|
minHelloCount = 1 // TODO(3147): Set this to more than 1, maybe configure from flag?
|
|
handshakePollingInterval = 5 * time.Second // Polling interval for checking the number of received handshakes.
|
|
)
|
|
|
|
// Config to set up the initial sync service.
|
|
type Config struct {
|
|
P2P p2p.P2P
|
|
DB db.Database
|
|
Chain blockchainService
|
|
}
|
|
|
|
// InitialSync service.
|
|
type InitialSync struct {
|
|
chain blockchainService
|
|
p2p p2p.P2P
|
|
synced bool
|
|
chainStarted bool
|
|
}
|
|
|
|
// NewInitialSync configures the initial sync service responsible for bringing the node up to the
|
|
// latest head of the blockchain.
|
|
func NewInitialSync(cfg *Config) *InitialSync {
|
|
return &InitialSync{
|
|
chain: cfg.Chain,
|
|
p2p: cfg.P2P,
|
|
}
|
|
}
|
|
|
|
// Start the initial sync service.
|
|
func (s *InitialSync) Start() {
|
|
ch := make(chan time.Time)
|
|
sub := s.chain.StateInitializedFeed().Subscribe(ch)
|
|
defer sub.Unsubscribe()
|
|
|
|
// Wait until chain start.
|
|
genesis := <-ch
|
|
if genesis.After(roughtime.Now()) {
|
|
time.Sleep(roughtime.Until(genesis))
|
|
}
|
|
s.chainStarted = true
|
|
currentSlot := slotsSinceGenesis(genesis)
|
|
if helpers.SlotToEpoch(currentSlot) == 0 {
|
|
log.Info("Chain started within the last epoch. Not syncing.")
|
|
s.synced = true
|
|
return
|
|
}
|
|
|
|
// Are we already in sync, or close to it?
|
|
if helpers.SlotToEpoch(s.chain.HeadSlot()) == helpers.SlotToEpoch(currentSlot) {
|
|
log.Info("Already synced to the current epoch.")
|
|
s.synced = true
|
|
return
|
|
}
|
|
|
|
// Every 5 sec, report handshake count.
|
|
for {
|
|
helloCount := peerstatus.Count()
|
|
log.WithField(
|
|
"hellos",
|
|
fmt.Sprintf("%d/%d", helloCount, minHelloCount),
|
|
).Info("Waiting for enough peer handshakes before syncing.")
|
|
|
|
if helloCount >= minHelloCount {
|
|
break
|
|
}
|
|
time.Sleep(handshakePollingInterval)
|
|
}
|
|
|
|
pid, best := bestStatus()
|
|
|
|
var last *eth.BeaconBlock
|
|
for headSlot := s.chain.HeadSlot(); headSlot < slotsSinceGenesis(genesis); {
|
|
req := &pb.BeaconBlocksByRangeRequest{
|
|
StartSlot: headSlot + 1,
|
|
HeadBlockRoot: s.chain.HeadRoot(),
|
|
Count: 64,
|
|
Step: 1,
|
|
}
|
|
|
|
log.WithField("data", fmt.Sprintf("%+v", req)).Info("Sending msg")
|
|
|
|
strm, err := s.p2p.Send(context.Background(), req, pid)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
for i := 0; i < int(req.Count); i++ {
|
|
blk, err := sync.ReadChunkedBlock(strm, s.p2p)
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
log.Error(err)
|
|
break
|
|
}
|
|
if blk.Slot <= headSlot {
|
|
continue
|
|
}
|
|
if blk.Slot < helpers.StartSlot(best.FinalizedEpoch+1) {
|
|
if err := s.chain.ReceiveBlockNoPubsubForkchoice(context.Background(), blk); err != nil {
|
|
panic(err)
|
|
}
|
|
} else {
|
|
if err := s.chain.ReceiveBlockNoPubsub(context.Background(), blk); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
last = blk
|
|
}
|
|
headSlot = s.chain.HeadSlot()
|
|
}
|
|
|
|
// Force a fork choice update since fork choice was not run during initial sync.
|
|
if err := s.chain.ReceiveBlockNoPubsub(context.Background(), last); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
log.Infof("Synced up to %d", s.chain.HeadSlot())
|
|
s.synced = true
|
|
}
|
|
|
|
// Stop initial sync.
|
|
func (s *InitialSync) Stop() error {
|
|
return nil
|
|
}
|
|
|
|
// Status of initial sync.
|
|
func (s *InitialSync) Status() error {
|
|
if !s.synced && s.chainStarted {
|
|
return errors.New("syncing")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Syncing returns true if initial sync is still running.
|
|
func (s *InitialSync) Syncing() bool {
|
|
return !s.synced
|
|
}
|
|
|
|
func bestStatus() (peer.ID, *pb.Status) {
|
|
keys := peerstatus.Keys()
|
|
return keys[0], peerstatus.Get(keys[0])
|
|
}
|
|
|
|
func slotsSinceGenesis(genesisTime time.Time) uint64 {
|
|
return uint64(roughtime.Since(genesisTime).Seconds()) / params.BeaconConfig().SecondsPerSlot
|
|
}
|