2020-01-28 02:04:43 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-11-03 21:18:15 +00:00
|
|
|
"strconv"
|
2020-01-28 02:04:43 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-09-18 17:26:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/async"
|
2021-09-08 10:41:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/transition"
|
2021-07-23 16:11:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2022-04-29 14:32:11 +00:00
|
|
|
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
|
2022-05-20 23:29:16 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/consensus-types/wrapper"
|
2021-09-23 15:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-10-01 20:17:57 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/time/slots"
|
2020-01-28 02:04:43 +00:00
|
|
|
)
|
|
|
|
|
2020-01-30 19:06:20 +00:00
|
|
|
// getAttPreState retrieves the att pre state by either from the cache or the DB.
|
2021-07-23 16:11:21 +00:00
|
|
|
func (s *Service) getAttPreState(ctx context.Context, c *ethpb.Checkpoint) (state.BeaconState, error) {
|
2020-11-03 21:18:15 +00:00
|
|
|
// Use a multilock to allow scoped holding of a mutex by a checkpoint root + epoch
|
|
|
|
// allowing us to behave smarter in terms of how this function is used concurrently.
|
2021-02-09 10:05:22 +00:00
|
|
|
epochKey := strconv.FormatUint(uint64(c.Epoch), 10 /* base 10 */)
|
2021-09-18 17:26:11 +00:00
|
|
|
lock := async.NewMultilock(string(c.Root) + epochKey)
|
2020-11-03 21:18:15 +00:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
cachedState, err := s.checkpointStateCache.StateByCheckpoint(c)
|
2020-01-30 19:06:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get cached checkpoint state")
|
|
|
|
}
|
2021-05-24 04:55:42 +00:00
|
|
|
if cachedState != nil && !cachedState.IsNil() {
|
2020-01-30 19:06:20 +00:00
|
|
|
return cachedState, nil
|
|
|
|
}
|
2020-03-16 19:07:07 +00:00
|
|
|
|
2021-03-17 18:36:56 +00:00
|
|
|
baseState, err := s.cfg.StateGen.StateByRoot(ctx, bytesutil.ToBytes32(c.Root))
|
2020-04-23 19:03:51 +00:00
|
|
|
if err != nil {
|
2020-09-02 02:52:36 +00:00
|
|
|
return nil, errors.Wrapf(err, "could not get pre state for epoch %d", c.Epoch)
|
2020-04-23 19:03:51 +00:00
|
|
|
}
|
2020-11-03 21:18:15 +00:00
|
|
|
|
2021-10-01 20:17:57 +00:00
|
|
|
epochStartSlot, err := slots.EpochStart(c.Epoch)
|
2020-09-02 02:52:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-25 14:52:18 +00:00
|
|
|
baseState, err = transition.ProcessSlotsIfPossible(ctx, baseState, epochStartSlot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not process slots up to epoch %d", c.Epoch)
|
2020-08-17 21:20:34 +00:00
|
|
|
}
|
2021-09-16 15:29:59 +00:00
|
|
|
|
2021-07-27 14:26:53 +00:00
|
|
|
// Sharing the same state across caches is perfectly fine here, the fetching
|
|
|
|
// of attestation prestate is by far the most accessed state fetching pattern in
|
|
|
|
// the beacon node. An extra state instance cached isn't an issue in the bigger
|
|
|
|
// picture.
|
|
|
|
if err := s.checkpointStateCache.AddCheckpointState(c, baseState); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not save checkpoint state to cache")
|
2020-01-30 19:06:20 +00:00
|
|
|
}
|
2020-02-02 05:23:05 +00:00
|
|
|
return baseState, nil
|
2020-11-03 21:18:15 +00:00
|
|
|
|
2020-01-28 02:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// verifyAttTargetEpoch validates attestation is from the current or previous epoch.
|
2021-12-07 17:52:39 +00:00
|
|
|
func verifyAttTargetEpoch(_ context.Context, genesisTime, nowTime uint64, c *ethpb.Checkpoint) error {
|
2021-02-16 07:45:34 +00:00
|
|
|
currentSlot := types.Slot((nowTime - genesisTime) / params.BeaconConfig().SecondsPerSlot)
|
2021-10-01 20:17:57 +00:00
|
|
|
currentEpoch := slots.ToEpoch(currentSlot)
|
2021-02-09 10:05:22 +00:00
|
|
|
var prevEpoch types.Epoch
|
2020-01-28 02:04:43 +00:00
|
|
|
// Prevents previous epoch under flow
|
|
|
|
if currentEpoch > 1 {
|
|
|
|
prevEpoch = currentEpoch - 1
|
|
|
|
}
|
|
|
|
if c.Epoch != prevEpoch && c.Epoch != currentEpoch {
|
|
|
|
return fmt.Errorf("target epoch %d does not match current epoch %d or prev epoch %d", c.Epoch, currentEpoch, prevEpoch)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyBeaconBlock verifies beacon head block is known and not from the future.
|
|
|
|
func (s *Service) verifyBeaconBlock(ctx context.Context, data *ethpb.AttestationData) error {
|
2020-07-14 15:46:00 +00:00
|
|
|
r := bytesutil.ToBytes32(data.BeaconBlockRoot)
|
2022-04-29 07:56:31 +00:00
|
|
|
b, err := s.getBlock(ctx, r)
|
2020-01-28 02:04:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-20 23:29:16 +00:00
|
|
|
if err := wrapper.BeaconBlockIsNil(b); err != nil {
|
2021-02-15 15:11:25 +00:00
|
|
|
return err
|
2020-01-28 02:04:43 +00:00
|
|
|
}
|
2021-05-26 16:19:54 +00:00
|
|
|
if b.Block().Slot() > data.Slot {
|
|
|
|
return fmt.Errorf("could not process attestation for future block, block.Slot=%d > attestation.Data.Slot=%d", b.Block().Slot(), data.Slot)
|
2020-01-28 02:04:43 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|