prysm-pulse/beacon-chain/sync/service.go

108 lines
2.9 KiB
Go
Raw Normal View History

package sync
import (
"context"
"sync"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/operations"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
"github.com/prysmaticlabs/prysm/shared"
)
2019-12-17 01:53:55 +00:00
var _ = shared.Service(&Service{})
// Config to set up the regular sync service.
type Config struct {
P2P p2p.P2P
DB db.Database
Operations *operations.Service
Chain blockchainService
InitialSync Checker
StateNotifier statefeed.Notifier
}
// This defines the interface for interacting with block chain service
type blockchainService interface {
blockchain.BlockReceiver
[Interop] Improve RPC Codebase + Start Beacon Chain With Mock ETH1 Values (#3407) * add main.go * interop readme * proper visibility * standardize and abstract into simpler funcs * formatting * no os pkg * add test * no panics anywhere, properly and nicely handle errors * proper comments * fix broken test * readme * comment * recommend ssz * install * tool now works * README * build * readme * 64 validators * rem print * register the no powchain flag * work on mock eth1 start * common interface * getting closer with the interface defs * only two uses of powchain * remove powchain dependency * remove powchain dependency * common powchain interface * proper comment in case of flag * proper args into rpc services * rename fields * pass in mock flag into RPC * conforms to iface * use client instead of block fetcher iface * broken tests * block fetcher * finalized * resolved broken build * fix build * comment * fix tests * tests pass * resolved confs * took them out * rename into smaller interfaces * resolve some confs * ensure tests pass * properly utilize mock instead of localized mock * res lint * lint * finish test for mock eth1data * run gazelle * include flag again * fix broken build * disable powchain * dont dial eth1 nodes * reenable pow * use smaller interfaces, standardize naming * abstract mock into its own package * faulty mock lint * fix stutter in lint * rpc tests all passing * use mocks for operations * no more mocks in the entire rpc package * no mock * viz * testonly
2019-09-09 21:13:50 +00:00
blockchain.HeadFetcher
blockchain.FinalizationFetcher
blockchain.ForkFetcher
blockchain.AttestationReceiver
blockchain.GenesisTimeFetcher
}
// NewRegularSync service.
2019-12-17 01:53:55 +00:00
func NewRegularSync(cfg *Config) *Service {
ctx, cancel := context.WithCancel(context.Background())
2019-12-17 01:53:55 +00:00
r := &Service{
ctx: ctx,
cancel: cancel,
db: cfg.DB,
p2p: cfg.P2P,
operations: cfg.Operations,
chain: cfg.Chain,
initialSync: cfg.InitialSync,
slotToPendingBlocks: make(map[uint64]*ethpb.BeaconBlock),
seenPendingBlocks: make(map[[32]byte]bool),
stateNotifier: cfg.StateNotifier,
}
r.registerRPCHandlers()
r.registerSubscribers()
return r
}
2019-12-17 01:53:55 +00:00
// Service is responsible for handling all run time p2p related operations as the
// main entry point for network messages.
2019-12-17 01:53:55 +00:00
type Service struct {
ctx context.Context
cancel context.CancelFunc
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
initialSync Checker
validateBlockLock sync.RWMutex
stateNotifier statefeed.Notifier
}
// Start the regular sync service.
2019-12-17 01:53:55 +00:00
func (r *Service) Start() {
r.p2p.AddConnectionHandler(r.sendRPCStatusRequest)
r.p2p.AddDisconnectionHandler(r.removeDisconnectedPeerStatus)
r.processPendingBlocksQueue()
r.maintainPeerStatuses()
}
// Stop the regular sync service.
2019-12-17 01:53:55 +00:00
func (r *Service) Stop() error {
defer r.cancel()
return nil
}
// Status of the currently running regular sync service.
2019-12-17 01:53:55 +00:00
func (r *Service) Status() error {
if r.chainStarted && r.initialSync.Syncing() {
return errors.New("waiting for initial sync")
}
return nil
}
// 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
Resync() error
}