2020-04-29 17:40:33 +00:00
|
|
|
// Package sync includes all chain-synchronization logic for the beacon node,
|
|
|
|
// including gossip-sub validators for blocks, attestations, and other p2p
|
|
|
|
// messages, as well as ability to process and respond to block requests
|
|
|
|
// by peers.
|
2019-08-16 17:13:04 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-08-29 16:32:52 +00:00
|
|
|
"sync"
|
2020-03-23 14:41:47 +00:00
|
|
|
"time"
|
2019-08-16 17:13:04 +00:00
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru"
|
2021-09-02 02:26:38 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2023-05-19 16:59:13 +00:00
|
|
|
libp2pcore "github.com/libp2p/go-libp2p/core"
|
2022-10-07 07:24:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
2020-11-19 05:15:58 +00:00
|
|
|
gcache "github.com/patrickmn/go-cache"
|
2019-09-27 19:30:28 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/async"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/async/abool"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/async/event"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/blockchain"
|
|
|
|
blockfeed "github.com/prysmaticlabs/prysm/v4/beacon-chain/core/feed/block"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/feed/operation"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/db"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/execution"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/operations/attestations"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/operations/blstoexec"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/operations/slashings"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/operations/synccommittee"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/operations/voluntaryexits"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/p2p"
|
2023-05-03 04:34:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/startup"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state/stategen"
|
|
|
|
lruwrpr "github.com/prysmaticlabs/prysm/v4/cache/lru"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
2023-06-15 19:38:08 +00:00
|
|
|
leakybucket "github.com/prysmaticlabs/prysm/v4/container/leaky-bucket"
|
2023-03-17 18:52:56 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/runtime"
|
|
|
|
prysmTime "github.com/prysmaticlabs/prysm/v4/time"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/time/slots"
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
|
|
|
|
2021-09-17 19:20:50 +00:00
|
|
|
var _ runtime.Service = (*Service)(nil)
|
2019-08-16 17:13:04 +00:00
|
|
|
|
2023-05-19 16:59:13 +00:00
|
|
|
const rangeLimit uint64 = 1024
|
2020-04-14 20:27:03 +00:00
|
|
|
const seenBlockSize = 1000
|
2023-05-18 16:13:18 +00:00
|
|
|
const seenBlobSize = seenBlockSize * 4 // Each block can have max 4 blobs. Worst case 164kB for cache.
|
2021-08-26 21:37:35 +00:00
|
|
|
const seenUnaggregatedAttSize = 20000
|
|
|
|
const seenAggregatedAttSize = 1024
|
2021-09-03 04:06:54 +00:00
|
|
|
const seenSyncMsgSize = 1000 // Maximum of 512 sync committee members, 1000 is a safe amount.
|
|
|
|
const seenSyncContributionSize = 512 // Maximum of SYNC_COMMITTEE_SIZE as specified by the spec.
|
2020-04-14 20:27:03 +00:00
|
|
|
const seenExitSize = 100
|
|
|
|
const seenProposerSlashingSize = 100
|
2020-07-27 11:57:19 +00:00
|
|
|
const badBlockSize = 1000
|
2020-07-03 15:25:32 +00:00
|
|
|
const syncMetricsInterval = 10 * time.Second
|
|
|
|
|
2021-06-17 00:17:52 +00:00
|
|
|
var (
|
|
|
|
// Seconds in one epoch.
|
|
|
|
pendingBlockExpTime = time.Duration(params.BeaconConfig().SlotsPerEpoch.Mul(params.BeaconConfig().SecondsPerSlot)) * time.Second
|
|
|
|
// time to allow processing early blocks.
|
2021-09-15 00:09:04 +00:00
|
|
|
earlyBlockProcessingTolerance = slots.MultiplySlotBy(2)
|
2021-06-17 00:17:52 +00:00
|
|
|
// time to allow processing early attestations.
|
|
|
|
earlyAttestationProcessingTolerance = params.BeaconNetworkConfig().MaximumGossipClockDisparity
|
2021-09-02 02:26:38 +00:00
|
|
|
errWrongMessage = errors.New("wrong pubsub message")
|
|
|
|
errNilMessage = errors.New("nil pubsub message")
|
2021-06-17 00:17:52 +00:00
|
|
|
)
|
2020-11-19 05:15:58 +00:00
|
|
|
|
2021-09-02 02:26:38 +00:00
|
|
|
// Common type for functional p2p validation options.
|
2021-09-26 01:06:48 +00:00
|
|
|
type validationFn func(ctx context.Context) (pubsub.ValidationResult, error)
|
2021-09-02 02:26:38 +00:00
|
|
|
|
2021-11-05 19:08:58 +00:00
|
|
|
// config to hold dependencies for the sync service.
|
|
|
|
type config struct {
|
2022-07-13 17:18:30 +00:00
|
|
|
attestationNotifier operation.Notifier
|
|
|
|
p2p p2p.P2P
|
|
|
|
beaconDB db.NoHeadAccessDatabase
|
|
|
|
attPool attestations.Pool
|
|
|
|
exitPool voluntaryexits.PoolManager
|
|
|
|
slashingPool slashings.PoolManager
|
|
|
|
syncCommsPool synccommittee.Pool
|
2022-11-26 19:07:05 +00:00
|
|
|
blsToExecPool blstoexec.PoolManager
|
2022-07-13 17:18:30 +00:00
|
|
|
chain blockchainService
|
|
|
|
initialSync Checker
|
|
|
|
blockNotifier blockfeed.Notifier
|
|
|
|
operationNotifier operation.Notifier
|
2022-08-01 14:43:47 +00:00
|
|
|
executionPayloadReconstructor execution.ExecutionPayloadReconstructor
|
2022-07-13 17:18:30 +00:00
|
|
|
stateGen *stategen.State
|
|
|
|
slasherAttestationsFeed *event.Feed
|
|
|
|
slasherBlockHeadersFeed *event.Feed
|
2023-05-03 04:34:01 +00:00
|
|
|
clock *startup.Clock
|
2019-08-23 17:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This defines the interface for interacting with block chain service
|
|
|
|
type blockchainService interface {
|
|
|
|
blockchain.BlockReceiver
|
2023-08-18 00:03:31 +00:00
|
|
|
blockchain.BlobReceiver
|
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
|
2020-02-02 01:42:29 +00:00
|
|
|
blockchain.TimeFetcher
|
2020-04-19 02:32:53 +00:00
|
|
|
blockchain.GenesisFetcher
|
2020-06-09 22:40:48 +00:00
|
|
|
blockchain.CanonicalFetcher
|
2022-05-12 17:23:45 +00:00
|
|
|
blockchain.OptimisticModeFetcher
|
2022-05-07 15:56:34 +00:00
|
|
|
blockchain.SlashingReceiver
|
2023-03-20 18:51:42 +00:00
|
|
|
blockchain.ForkchoiceFetcher
|
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 {
|
2021-11-05 19:08:58 +00:00
|
|
|
cfg *config
|
2021-08-26 21:37:35 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
slotToPendingBlocks *gcache.Cache
|
|
|
|
seenPendingBlocks map[[32]byte]bool
|
|
|
|
blkRootToPendingAtts map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof
|
2021-09-02 06:10:02 +00:00
|
|
|
subHandler *subTopicHandler
|
2021-08-26 21:37:35 +00:00
|
|
|
pendingAttsLock sync.RWMutex
|
|
|
|
pendingQueueLock sync.RWMutex
|
|
|
|
chainStarted *abool.AtomicBool
|
|
|
|
validateBlockLock sync.RWMutex
|
|
|
|
rateLimiter *limiter
|
|
|
|
seenBlockLock sync.RWMutex
|
|
|
|
seenBlockCache *lru.Cache
|
2023-05-18 16:13:18 +00:00
|
|
|
seenBlobLock sync.RWMutex
|
|
|
|
seenBlobCache *lru.Cache
|
2021-08-26 21:37:35 +00:00
|
|
|
seenAggregatedAttestationLock sync.RWMutex
|
|
|
|
seenAggregatedAttestationCache *lru.Cache
|
|
|
|
seenUnAggregatedAttestationLock sync.RWMutex
|
|
|
|
seenUnAggregatedAttestationCache *lru.Cache
|
|
|
|
seenExitLock sync.RWMutex
|
|
|
|
seenExitCache *lru.Cache
|
|
|
|
seenProposerSlashingLock sync.RWMutex
|
|
|
|
seenProposerSlashingCache *lru.Cache
|
|
|
|
seenAttesterSlashingLock sync.RWMutex
|
|
|
|
seenAttesterSlashingCache map[uint64]bool
|
2021-09-02 02:26:38 +00:00
|
|
|
seenSyncMessageLock sync.RWMutex
|
|
|
|
seenSyncMessageCache *lru.Cache
|
2021-09-03 04:06:54 +00:00
|
|
|
seenSyncContributionLock sync.RWMutex
|
|
|
|
seenSyncContributionCache *lru.Cache
|
2021-08-26 21:37:35 +00:00
|
|
|
badBlockCache *lru.Cache
|
|
|
|
badBlockLock sync.RWMutex
|
2022-05-25 05:40:06 +00:00
|
|
|
syncContributionBitsOverlapLock sync.RWMutex
|
|
|
|
syncContributionBitsOverlapCache *lru.Cache
|
2021-09-15 05:49:50 +00:00
|
|
|
signatureChan chan *signatureVerifier
|
2023-05-03 04:34:01 +00:00
|
|
|
clockWaiter startup.ClockWaiter
|
|
|
|
initialSyncComplete chan struct{}
|
2019-08-16 17:13:04 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 17:45:22 +00:00
|
|
|
// NewService initializes new regular sync service.
|
2021-11-05 19:08:58 +00:00
|
|
|
func NewService(ctx context.Context, opts ...Option) *Service {
|
2020-11-19 05:15:58 +00:00
|
|
|
c := gcache.New(pendingBlockExpTime /* exp time */, 2*pendingBlockExpTime /* prune time */)
|
2020-09-09 09:48:52 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-04-20 04:04:45 +00:00
|
|
|
r := &Service{
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
2020-11-03 19:12:24 +00:00
|
|
|
chainStarted: abool.New(),
|
2023-05-10 04:09:15 +00:00
|
|
|
cfg: &config{clock: startup.NewClock(time.Unix(0, 0), [32]byte{})},
|
2020-11-19 05:15:58 +00:00
|
|
|
slotToPendingBlocks: c,
|
2020-04-20 04:04:45 +00:00
|
|
|
seenPendingBlocks: make(map[[32]byte]bool),
|
|
|
|
blkRootToPendingAtts: make(map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof),
|
2021-09-15 05:49:50 +00:00
|
|
|
signatureChan: make(chan *signatureVerifier, verifierLimit),
|
2020-04-20 04:04:45 +00:00
|
|
|
}
|
2021-11-05 19:08:58 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(r); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.subHandler = newSubTopicHandler()
|
|
|
|
r.rateLimiter = newRateLimiter(r.cfg.p2p)
|
2022-04-16 02:45:35 +00:00
|
|
|
r.initCaches()
|
2020-04-20 04:04:45 +00:00
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2019-08-30 20:15:40 +00:00
|
|
|
// Start the regular sync service.
|
2020-06-22 20:37:48 +00:00
|
|
|
func (s *Service) Start() {
|
2023-05-03 04:34:01 +00:00
|
|
|
go s.verifierRoutine()
|
|
|
|
go s.registerHandlers()
|
|
|
|
|
2021-11-05 19:08:58 +00:00
|
|
|
s.cfg.p2p.AddConnectionHandler(s.reValidatePeer, s.sendGoodbye)
|
|
|
|
s.cfg.p2p.AddDisconnectionHandler(func(_ context.Context, _ peer.ID) error {
|
2020-06-25 10:07:31 +00:00
|
|
|
// no-op
|
|
|
|
return nil
|
|
|
|
})
|
2021-11-05 19:08:58 +00:00
|
|
|
s.cfg.p2p.AddPingMethod(s.sendPingRequest)
|
2020-06-22 20:37:48 +00:00
|
|
|
s.processPendingBlocksQueue()
|
|
|
|
s.processPendingAttsQueue()
|
|
|
|
s.maintainPeerStatuses()
|
2022-08-21 13:31:40 +00:00
|
|
|
s.resyncIfBehind()
|
2020-04-14 20:27:03 +00:00
|
|
|
|
|
|
|
// Update sync metrics.
|
2021-09-18 17:26:11 +00:00
|
|
|
async.RunEvery(s.ctx, syncMetricsInterval, s.updateMetrics)
|
2019-08-16 17:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the regular sync service.
|
2020-06-22 20:37:48 +00:00
|
|
|
func (s *Service) Stop() error {
|
2020-06-22 11:23:23 +00:00
|
|
|
defer func() {
|
2020-07-17 08:58:51 +00:00
|
|
|
if s.rateLimiter != nil {
|
|
|
|
s.rateLimiter.free()
|
2020-06-22 11:23:23 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-11-04 07:24:29 +00:00
|
|
|
// Removing RPC Stream handlers.
|
2021-11-05 19:08:58 +00:00
|
|
|
for _, p := range s.cfg.p2p.Host().Mux().Protocols() {
|
|
|
|
s.cfg.p2p.Host().RemoveStreamHandler(protocol.ID(p))
|
2020-11-04 07:24:29 +00:00
|
|
|
}
|
|
|
|
// Deregister Topic Subscribers.
|
2021-11-05 19:08:58 +00:00
|
|
|
for _, t := range s.cfg.p2p.PubSub().GetTopics() {
|
2021-09-02 06:10:02 +00:00
|
|
|
s.unSubscribeFromTopic(t)
|
2020-11-04 07:24:29 +00:00
|
|
|
}
|
2020-06-22 20:37:48 +00:00
|
|
|
defer s.cancel()
|
2019-08-16 17:13:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status of the currently running regular sync service.
|
2020-06-22 20:37:48 +00:00
|
|
|
func (s *Service) Status() error {
|
2020-08-21 19:09:52 +00:00
|
|
|
// If our head slot is on a previous epoch and our peers are reporting their head block are
|
|
|
|
// in the most recent epoch, then we might be out of sync.
|
2023-05-03 04:34:01 +00:00
|
|
|
if headEpoch := slots.ToEpoch(s.cfg.chain.HeadSlot()); headEpoch+1 < slots.ToEpoch(s.cfg.clock.CurrentSlot()) &&
|
2021-11-05 19:08:58 +00:00
|
|
|
headEpoch+1 < s.cfg.p2p.Peers().HighestEpoch() {
|
2020-08-21 19:09:52 +00:00
|
|
|
return errors.New("out of sync")
|
2019-09-27 19:30:28 +00:00
|
|
|
}
|
2019-08-16 17:13:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-08-21 20:58:38 +00:00
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
// This initializes the caches to update seen beacon objects coming in from the wire
|
|
|
|
// and prevent DoS.
|
2021-09-02 10:36:54 +00:00
|
|
|
func (s *Service) initCaches() {
|
|
|
|
s.seenBlockCache = lruwrpr.New(seenBlockSize)
|
2023-05-18 16:13:18 +00:00
|
|
|
s.seenBlobCache = lruwrpr.New(seenBlobSize)
|
2021-09-02 10:36:54 +00:00
|
|
|
s.seenAggregatedAttestationCache = lruwrpr.New(seenAggregatedAttSize)
|
|
|
|
s.seenUnAggregatedAttestationCache = lruwrpr.New(seenUnaggregatedAttSize)
|
|
|
|
s.seenSyncMessageCache = lruwrpr.New(seenSyncMsgSize)
|
2021-09-03 04:06:54 +00:00
|
|
|
s.seenSyncContributionCache = lruwrpr.New(seenSyncContributionSize)
|
2022-05-25 05:40:06 +00:00
|
|
|
s.syncContributionBitsOverlapCache = lruwrpr.New(seenSyncContributionSize)
|
2021-09-02 10:36:54 +00:00
|
|
|
s.seenExitCache = lruwrpr.New(seenExitSize)
|
2021-01-26 17:24:34 +00:00
|
|
|
s.seenAttesterSlashingCache = make(map[uint64]bool)
|
2021-09-02 10:36:54 +00:00
|
|
|
s.seenProposerSlashingCache = lruwrpr.New(seenProposerSlashingSize)
|
|
|
|
s.badBlockCache = lruwrpr.New(badBlockSize)
|
2020-04-14 20:27:03 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 04:34:01 +00:00
|
|
|
func (s *Service) waitForChainStart() {
|
|
|
|
clock, err := s.clockWaiter.WaitForClock(s.ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("sync service failed to receive genesis data")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.cfg.clock = clock
|
|
|
|
startTime := clock.GenesisTime()
|
|
|
|
log.WithField("starttime", startTime).Debug("Received state initialized event")
|
|
|
|
// Register respective rpc handlers at state initialized event.
|
|
|
|
s.registerRPCHandlers()
|
|
|
|
// Wait for chainstart in separate routine.
|
|
|
|
if startTime.After(prysmTime.Now()) {
|
|
|
|
time.Sleep(prysmTime.Until(startTime))
|
|
|
|
}
|
|
|
|
log.WithField("starttime", startTime).Debug("Chain started in sync service")
|
|
|
|
s.markForChainStart()
|
|
|
|
}
|
2020-06-12 13:23:29 +00:00
|
|
|
|
2023-05-03 04:34:01 +00:00
|
|
|
func (s *Service) registerHandlers() {
|
|
|
|
s.waitForChainStart()
|
|
|
|
select {
|
|
|
|
case <-s.initialSyncComplete:
|
|
|
|
// Register respective pubsub handlers at state synced event.
|
|
|
|
digest, err := s.currentForkDigest()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Could not retrieve current fork digest")
|
2020-05-28 05:39:40 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-03 04:34:01 +00:00
|
|
|
currentEpoch := slots.ToEpoch(slots.CurrentSlot(uint64(s.cfg.clock.GenesisTime().Unix())))
|
|
|
|
s.registerSubscribers(currentEpoch, digest)
|
|
|
|
go s.forkWatcher()
|
|
|
|
return
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
log.Debug("Context closed, exiting goroutine")
|
|
|
|
return
|
2020-05-28 05:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 16:59:13 +00:00
|
|
|
func (s *Service) writeErrorResponseToStream(responseCode byte, reason string, stream libp2pcore.Stream) {
|
|
|
|
writeErrorResponseToStream(responseCode, reason, stream, s.cfg.p2p)
|
|
|
|
}
|
|
|
|
|
2023-06-15 19:38:08 +00:00
|
|
|
func (s *Service) setRateCollector(topic string, c *leakybucket.Collector) {
|
|
|
|
s.rateLimiter.limiterMap[topic] = c
|
|
|
|
}
|
|
|
|
|
2020-10-15 14:03:48 +00:00
|
|
|
// marks the chain as having started.
|
|
|
|
func (s *Service) markForChainStart() {
|
2020-11-03 19:12:24 +00:00
|
|
|
s.chainStarted.Set()
|
2020-10-15 14:03:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 04:34:01 +00:00
|
|
|
func (s *Service) chainIsStarted() bool {
|
|
|
|
return s.chainStarted.IsSet()
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-01-06 20:11:20 +00:00
|
|
|
Initialized() bool
|
2019-08-21 20:58:38 +00:00
|
|
|
Syncing() bool
|
2021-06-15 15:28:49 +00:00
|
|
|
Synced() bool
|
2019-08-21 20:58:38 +00:00
|
|
|
Status() error
|
2020-01-02 08:09:28 +00:00
|
|
|
Resync() error
|
2019-08-21 20:58:38 +00:00
|
|
|
}
|