2018-12-10 05:26:44 +00:00
|
|
|
package sync
|
2018-11-19 01:59:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-21 18:00:36 +00:00
|
|
|
"time"
|
2018-11-19 01:59:11 +00:00
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
2018-11-19 01:59:11 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/p2p"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
var queryLog = logrus.WithField("prefix", "syncQuerier")
|
2018-11-19 01:59:11 +00:00
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
// QuerierConfig defines the configurable properties of SyncQuerier.
|
|
|
|
type QuerierConfig struct {
|
2018-11-19 01:59:11 +00:00
|
|
|
ResponseBufferSize int
|
|
|
|
P2P p2pAPI
|
2018-12-01 22:09:12 +00:00
|
|
|
BeaconDB *db.BeaconDB
|
2018-11-19 01:59:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
// DefaultQuerierConfig provides the default configuration for a sync service.
|
2018-11-19 01:59:11 +00:00
|
|
|
// ResponseBufferSize determines that buffer size of the `responseBuf` channel.
|
2018-12-10 05:26:44 +00:00
|
|
|
func DefaultQuerierConfig() *QuerierConfig {
|
|
|
|
return &QuerierConfig{
|
2018-11-19 01:59:11 +00:00
|
|
|
ResponseBufferSize: 100,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
// Querier defines the main class in this package.
|
2018-11-19 01:59:11 +00:00
|
|
|
// See the package comments for a general description of the service's functions.
|
2018-12-10 05:26:44 +00:00
|
|
|
type Querier struct {
|
2018-11-19 01:59:11 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
p2p p2pAPI
|
2018-12-01 22:09:12 +00:00
|
|
|
db *db.BeaconDB
|
2018-11-19 01:59:11 +00:00
|
|
|
curentHeadSlot uint64
|
|
|
|
currentHeadHash []byte
|
|
|
|
responseBuf chan p2p.Message
|
|
|
|
}
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
// NewQuerierService constructs a new Sync Querier Service.
|
2018-11-19 01:59:11 +00:00
|
|
|
// This method is normally called by the main node.
|
2018-12-10 05:26:44 +00:00
|
|
|
func NewQuerierService(ctx context.Context,
|
|
|
|
cfg *QuerierConfig,
|
|
|
|
) *Querier {
|
2018-11-19 01:59:11 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
responseBuf := make(chan p2p.Message, cfg.ResponseBufferSize)
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
return &Querier{
|
2018-11-19 01:59:11 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
p2p: cfg.P2P,
|
|
|
|
db: cfg.BeaconDB,
|
|
|
|
responseBuf: responseBuf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start begins the goroutine.
|
2018-12-10 05:26:44 +00:00
|
|
|
func (q *Querier) Start() {
|
|
|
|
q.run()
|
2018-11-19 01:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop kills the sync querier goroutine.
|
2018-12-10 05:26:44 +00:00
|
|
|
func (q *Querier) Stop() error {
|
|
|
|
queryLog.Info("Stopping service")
|
|
|
|
q.cancel()
|
2018-11-19 01:59:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
func (q *Querier) run() {
|
|
|
|
responseSub := q.p2p.Subscribe(&pb.ChainHeadResponse{}, q.responseBuf)
|
2018-11-21 18:00:36 +00:00
|
|
|
|
|
|
|
// Ticker so that service will keep on requesting for chain head
|
|
|
|
// until they get a response.
|
|
|
|
ticker := time.NewTicker(1 * time.Second)
|
|
|
|
|
2018-11-19 01:59:11 +00:00
|
|
|
defer func() {
|
|
|
|
responseSub.Unsubscribe()
|
2018-12-10 05:26:44 +00:00
|
|
|
close(q.responseBuf)
|
2018-11-21 18:00:36 +00:00
|
|
|
ticker.Stop()
|
2018-11-19 01:59:11 +00:00
|
|
|
}()
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
q.RequestLatestHead()
|
2018-11-19 01:59:11 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2018-12-10 05:26:44 +00:00
|
|
|
case <-q.ctx.Done():
|
|
|
|
queryLog.Info("Exiting goroutine")
|
2018-11-19 01:59:11 +00:00
|
|
|
return
|
2018-11-21 18:00:36 +00:00
|
|
|
case <-ticker.C:
|
2018-12-10 05:26:44 +00:00
|
|
|
q.RequestLatestHead()
|
|
|
|
case msg := <-q.responseBuf:
|
2018-11-19 01:59:11 +00:00
|
|
|
response := msg.Data.(*pb.ChainHeadResponse)
|
2018-12-10 05:26:44 +00:00
|
|
|
queryLog.Infof("Latest chain head is at slot: %d and hash %#x", response.Slot, response.Hash)
|
|
|
|
q.curentHeadSlot = response.Slot
|
|
|
|
q.currentHeadHash = response.Hash
|
2018-11-21 18:00:36 +00:00
|
|
|
|
|
|
|
ticker.Stop()
|
2018-11-19 01:59:11 +00:00
|
|
|
responseSub.Unsubscribe()
|
2018-12-10 05:26:44 +00:00
|
|
|
q.cancel()
|
2018-11-19 01:59:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestLatestHead broadcasts out a request for all
|
|
|
|
// the latest chain heads from the node's peers.
|
2018-12-10 05:26:44 +00:00
|
|
|
func (q *Querier) RequestLatestHead() {
|
2018-11-19 01:59:11 +00:00
|
|
|
request := &pb.ChainHeadRequest{}
|
2018-12-10 05:26:44 +00:00
|
|
|
q.p2p.Broadcast(request)
|
2018-11-19 01:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsSynced checks if the node is cuurently synced with the
|
|
|
|
// rest of the network.
|
2018-12-10 05:26:44 +00:00
|
|
|
func (q *Querier) IsSynced() (bool, error) {
|
2019-01-21 09:34:11 +00:00
|
|
|
block, err := q.db.ChainHead()
|
2018-11-19 01:59:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2019-01-05 03:58:19 +00:00
|
|
|
if block.Slot >= q.curentHeadSlot {
|
2018-11-19 01:59:11 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|