2019-08-16 17:13:04 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-08-29 16:32:52 +00:00
|
|
|
"sync"
|
2019-08-16 17:13:04 +00:00
|
|
|
|
2019-08-29 16:32:52 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-09-27 19:30:28 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-08-21 20:58:38 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
|
2019-11-23 03:35:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/statefeed"
|
2019-08-16 20:03:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
2019-08-18 15:33:58 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/operations"
|
2019-08-16 17:13:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
2019-08-29 16:32:52 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-09-20 17:08:32 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
2019-08-16 17:13:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = shared.Service(&RegularSync{})
|
|
|
|
|
2019-08-21 16:04:00 +00:00
|
|
|
// Config to set up the regular sync service.
|
|
|
|
type Config struct {
|
2019-11-23 03:35:47 +00:00
|
|
|
P2P p2p.P2P
|
|
|
|
DB db.Database
|
|
|
|
Operations *operations.Service
|
|
|
|
Chain blockchainService
|
|
|
|
InitialSync Checker
|
|
|
|
StateNotifier statefeed.Notifier
|
2019-08-23 17:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This defines the interface for interacting with block chain service
|
|
|
|
type blockchainService interface {
|
|
|
|
blockchain.BlockReceiver
|
2019-09-09 21:13:50 +00:00
|
|
|
blockchain.HeadFetcher
|
|
|
|
blockchain.FinalizationFetcher
|
2019-10-29 05:23:55 +00:00
|
|
|
blockchain.ForkFetcher
|
2019-08-23 19:46:04 +00:00
|
|
|
blockchain.AttestationReceiver
|
2019-10-18 03:30:14 +00:00
|
|
|
blockchain.GenesisTimeFetcher
|
2019-08-21 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRegularSync service.
|
|
|
|
func NewRegularSync(cfg *Config) *RegularSync {
|
2019-08-30 20:15:40 +00:00
|
|
|
r := &RegularSync{
|
2019-09-21 16:43:18 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
db: cfg.DB,
|
|
|
|
p2p: cfg.P2P,
|
|
|
|
operations: cfg.Operations,
|
|
|
|
chain: cfg.Chain,
|
|
|
|
initialSync: cfg.InitialSync,
|
2019-09-20 17:08:32 +00:00
|
|
|
slotToPendingBlocks: make(map[uint64]*ethpb.BeaconBlock),
|
|
|
|
seenPendingBlocks: make(map[[32]byte]bool),
|
2019-11-23 03:35:47 +00:00
|
|
|
stateNotifier: cfg.StateNotifier,
|
2019-08-21 16:04:00 +00:00
|
|
|
}
|
2019-08-30 20:15:40 +00:00
|
|
|
|
|
|
|
r.registerRPCHandlers()
|
|
|
|
r.registerSubscribers()
|
|
|
|
|
|
|
|
return r
|
2019-08-21 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 17:13:04 +00:00
|
|
|
// RegularSync service is responsible for handling all run time p2p related operations as the
|
|
|
|
// main entry point for network messages.
|
|
|
|
type RegularSync struct {
|
2019-09-20 17:08:32 +00:00
|
|
|
ctx context.Context
|
|
|
|
p2p p2p.P2P
|
|
|
|
db db.Database
|
|
|
|
operations *operations.Service
|
|
|
|
chain blockchainService
|
|
|
|
slotToPendingBlocks map[uint64]*ethpb.BeaconBlock
|
|
|
|
seenPendingBlocks map[[32]byte]bool
|
|
|
|
pendingQueueLock sync.RWMutex
|
|
|
|
chainStarted bool
|
2019-09-21 16:43:18 +00:00
|
|
|
initialSync Checker
|
2019-09-24 14:56:50 +00:00
|
|
|
validateBlockLock sync.RWMutex
|
2019-11-23 03:35:47 +00:00
|
|
|
stateNotifier statefeed.Notifier
|
2019-08-16 17:13:04 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 20:15:40 +00:00
|
|
|
// Start the regular sync service.
|
2019-08-16 17:13:04 +00:00
|
|
|
func (r *RegularSync) Start() {
|
2019-09-20 06:27:28 +00:00
|
|
|
r.p2p.AddConnectionHandler(r.sendRPCStatusRequest)
|
2019-09-18 20:02:34 +00:00
|
|
|
r.p2p.AddDisconnectionHandler(r.removeDisconnectedPeerStatus)
|
2019-09-20 17:08:32 +00:00
|
|
|
go r.processPendingBlocksQueue()
|
2019-09-25 17:00:04 +00:00
|
|
|
go r.maintainPeerStatuses()
|
2019-08-16 17:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the regular sync service.
|
|
|
|
func (r *RegularSync) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status of the currently running regular sync service.
|
|
|
|
func (r *RegularSync) Status() error {
|
2019-09-29 06:42:09 +00:00
|
|
|
if r.chainStarted && r.initialSync.Syncing() {
|
2019-09-27 19:30:28 +00:00
|
|
|
return errors.New("waiting for initial sync")
|
|
|
|
}
|
2019-08-16 17:13:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-08-21 20:58:38 +00:00
|
|
|
|
|
|
|
// Checker defines a struct which can verify whether a node is currently
|
|
|
|
// synchronizing a chain with the rest of peers in the network.
|
|
|
|
type Checker interface {
|
|
|
|
Syncing() bool
|
|
|
|
Status() error
|
|
|
|
}
|
2019-08-30 20:15:40 +00:00
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
// StatusTracker interface for accessing the status / handshake messages received so far.
|
|
|
|
type StatusTracker interface {
|
|
|
|
PeerStatuses() map[peer.ID]*pb.Status
|
2019-08-30 20:15:40 +00:00
|
|
|
}
|