2020-09-26 08:04:07 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
// VerifyWeakSubjectivityRoot verifies the weak subjectivity root in the service struct.
|
2021-08-19 18:00:57 +00:00
|
|
|
// Reference design: https://github.com/ethereum/consensus-specs/blob/master/specs/phase0/weak-subjectivity.md#weak-subjectivity-sync-procedure
|
2020-09-26 08:04:07 +00:00
|
|
|
func (s *Service) VerifyWeakSubjectivityRoot(ctx context.Context) error {
|
|
|
|
// TODO(7342): Remove the following to fully use weak subjectivity in production.
|
2021-04-06 14:32:49 +00:00
|
|
|
if s.cfg.WeakSubjectivityCheckpt == nil || len(s.cfg.WeakSubjectivityCheckpt.Root) == 0 || s.cfg.WeakSubjectivityCheckpt.Epoch == 0 {
|
2020-09-26 08:04:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do nothing if the weak subjectivity has previously been verified,
|
|
|
|
// or weak subjectivity epoch is higher than last finalized epoch.
|
|
|
|
if s.wsVerified {
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-06 14:32:49 +00:00
|
|
|
if s.cfg.WeakSubjectivityCheckpt.Epoch > s.finalizedCheckpt.Epoch {
|
2020-09-26 08:04:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 14:32:49 +00:00
|
|
|
r := bytesutil.ToBytes32(s.cfg.WeakSubjectivityCheckpt.Root)
|
|
|
|
log.Infof("Performing weak subjectivity check for root %#x in epoch %d", r, s.cfg.WeakSubjectivityCheckpt.Epoch)
|
2020-09-26 08:04:07 +00:00
|
|
|
// Save initial sync cached blocks to DB.
|
2021-03-17 18:36:56 +00:00
|
|
|
if err := s.cfg.BeaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
|
2020-09-26 08:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// A node should have the weak subjectivity block in the DB.
|
2021-03-17 18:36:56 +00:00
|
|
|
if !s.cfg.BeaconDB.HasBlock(ctx, r) {
|
2020-09-26 08:04:07 +00:00
|
|
|
return fmt.Errorf("node does not have root in DB: %#x", r)
|
|
|
|
}
|
|
|
|
|
2021-04-06 14:32:49 +00:00
|
|
|
startSlot, err := helpers.StartSlot(s.cfg.WeakSubjectivityCheckpt.Epoch)
|
2020-09-26 08:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// A node should have the weak subjectivity block corresponds to the correct epoch in the DB.
|
|
|
|
filter := filters.NewFilter().SetStartSlot(startSlot).SetEndSlot(startSlot + params.BeaconConfig().SlotsPerEpoch)
|
2021-03-17 18:36:56 +00:00
|
|
|
roots, err := s.cfg.BeaconDB.BlockRoots(ctx, filter)
|
2020-09-26 08:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, root := range roots {
|
|
|
|
if r == root {
|
|
|
|
log.Info("Weak subjectivity check has passed")
|
|
|
|
s.wsVerified = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 14:32:49 +00:00
|
|
|
return fmt.Errorf("node does not have root in db corresponding to epoch: %#x %d", r, s.cfg.WeakSubjectivityCheckpt.Epoch)
|
2020-09-26 08:04:07 +00:00
|
|
|
}
|