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-09-27 19:30:28 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2019-08-21 20:58:38 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
|
2019-12-07 17:57:26 +00:00
|
|
|
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
|
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"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared"
|
|
|
|
)
|
|
|
|
|
2019-12-17 01:53:55 +00:00
|
|
|
var _ = shared.Service(&Service{})
|
2019-08-16 17:13:04 +00:00
|
|
|
|
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.
|
2019-12-17 01:53:55 +00:00
|
|
|
func NewRegularSync(cfg *Config) *Service {
|
2019-12-16 17:00:15 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2019-12-17 01:53:55 +00:00
|
|
|
r := &Service{
|
2019-12-16 17:00:15 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
2019-09-21 16:43:18 +00:00
|
|
|
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-12-17 01:53:55 +00:00
|
|
|
// Service is responsible for handling all run time p2p related operations as the
|
2019-08-16 17:13:04 +00:00
|
|
|
// main entry point for network messages.
|
2019-12-17 01:53:55 +00:00
|
|
|
type Service struct {
|
2019-09-20 17:08:32 +00:00
|
|
|
ctx context.Context
|
2019-12-16 17:00:15 +00:00
|
|
|
cancel context.CancelFunc
|
2019-09-20 17:08:32 +00:00
|
|
|
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-12-17 01:53:55 +00:00
|
|
|
func (r *Service) 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-12-16 17:00:15 +00:00
|
|
|
r.processPendingBlocksQueue()
|
|
|
|
r.maintainPeerStatuses()
|
2019-08-16 17:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the regular sync service.
|
2019-12-17 01:53:55 +00:00
|
|
|
func (r *Service) Stop() error {
|
2019-12-16 17:00:15 +00:00
|
|
|
defer r.cancel()
|
2019-08-16 17:13:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status of the currently running regular sync service.
|
2019-12-17 01:53:55 +00:00
|
|
|
func (r *Service) 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
|
2020-01-02 08:09:28 +00:00
|
|
|
Resync() error
|
2019-08-21 20:58:38 +00:00
|
|
|
}
|