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-08-21 20:58:38 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
|
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-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 {
|
|
|
|
P2P p2p.P2P
|
|
|
|
DB db.Database
|
|
|
|
Operations *operations.Service
|
2019-08-23 17:48:40 +00:00
|
|
|
Chain blockchainService
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-23 19:46:04 +00:00
|
|
|
blockchain.AttestationReceiver
|
2019-09-17 21:14:51 +00:00
|
|
|
blockchain.ChainFeeds
|
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-08-29 16:32:52 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
db: cfg.DB,
|
|
|
|
p2p: cfg.P2P,
|
|
|
|
operations: cfg.Operations,
|
|
|
|
chain: cfg.Chain,
|
|
|
|
helloTracker: make(map[peer.ID]*pb.Hello),
|
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-08-29 16:32:52 +00:00
|
|
|
ctx context.Context
|
|
|
|
p2p p2p.P2P
|
|
|
|
db db.Database
|
|
|
|
operations *operations.Service
|
|
|
|
chain blockchainService
|
|
|
|
helloTracker map[peer.ID]*pb.Hello
|
|
|
|
helloTrackerLock sync.RWMutex
|
2019-09-17 21:14:51 +00:00
|
|
|
chainStarted bool
|
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-08-29 16:32:52 +00:00
|
|
|
r.p2p.AddConnectionHandler(r.sendRPCHelloRequest)
|
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 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-21 20:58:38 +00:00
|
|
|
|
|
|
|
// Syncing returns true if the node is currently syncing with the network.
|
|
|
|
func (r *RegularSync) Syncing() bool {
|
|
|
|
// TODO(3147): Use real value.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-08-30 20:15:40 +00:00
|
|
|
// Hellos returns the map of hello messages received so far.
|
|
|
|
func (r *RegularSync) Hellos() map[peer.ID]*pb.Hello {
|
|
|
|
r.helloTrackerLock.RLock()
|
|
|
|
defer r.helloTrackerLock.RUnlock()
|
|
|
|
return r.helloTracker
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// HelloTracker interface for accessing the hello / handshake messages received so far.
|
|
|
|
type HelloTracker interface {
|
|
|
|
Hellos() map[peer.ID]*pb.Hello
|
|
|
|
}
|