2020-01-28 02:04:43 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-01-29 01:44:51 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/attestationutil"
|
2020-01-28 02:04:43 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
)
|
|
|
|
|
2020-01-30 19:06:20 +00:00
|
|
|
// getAttPreState retrieves the att pre state by either from the cache or the DB.
|
|
|
|
func (s *Service) getAttPreState(ctx context.Context, c *ethpb.Checkpoint) (*pb.BeaconState, error) {
|
|
|
|
s.checkpointStateLock.Lock()
|
|
|
|
defer s.checkpointStateLock.Unlock()
|
|
|
|
cachedState, err := s.checkpointState.StateByCheckpoint(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get cached checkpoint state")
|
|
|
|
}
|
|
|
|
if cachedState != nil {
|
|
|
|
return cachedState, nil
|
|
|
|
}
|
|
|
|
|
2020-01-28 02:04:43 +00:00
|
|
|
baseState, err := s.beaconDB.State(ctx, bytesutil.ToBytes32(c.Root))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not get pre state for slot %d", helpers.StartSlot(c.Epoch))
|
|
|
|
}
|
|
|
|
if baseState == nil {
|
|
|
|
return nil, fmt.Errorf("pre state of target block %d does not exist", helpers.StartSlot(c.Epoch))
|
|
|
|
}
|
2020-01-30 19:06:20 +00:00
|
|
|
|
|
|
|
stateCopy := proto.Clone(baseState).(*pb.BeaconState)
|
|
|
|
|
|
|
|
if helpers.StartSlot(c.Epoch) > stateCopy.Slot {
|
|
|
|
stateCopy, err = state.ProcessSlots(ctx, stateCopy, helpers.StartSlot(c.Epoch))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not process slots up to %d", helpers.StartSlot(c.Epoch))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.checkpointState.AddCheckpointState(&cache.CheckpointState{
|
|
|
|
Checkpoint: c,
|
|
|
|
State: stateCopy,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not saved checkpoint state to cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
return stateCopy, nil
|
2020-01-28 02:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// verifyAttTargetEpoch validates attestation is from the current or previous epoch.
|
|
|
|
func (s *Service) verifyAttTargetEpoch(ctx context.Context, genesisTime uint64, nowTime uint64, c *ethpb.Checkpoint) error {
|
|
|
|
currentSlot := (nowTime - genesisTime) / params.BeaconConfig().SecondsPerSlot
|
|
|
|
currentEpoch := helpers.SlotToEpoch(currentSlot)
|
|
|
|
var prevEpoch uint64
|
|
|
|
// 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 {
|
|
|
|
b, err := s.beaconDB.Block(ctx, bytesutil.ToBytes32(data.BeaconBlockRoot))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if b == nil || b.Block == nil {
|
|
|
|
return fmt.Errorf("beacon block %#x does not exist", bytesutil.Trunc(data.BeaconBlockRoot))
|
|
|
|
}
|
|
|
|
if b.Block.Slot > data.Slot {
|
|
|
|
return fmt.Errorf("could not process attestation for future block, %d > %d", b.Block.Slot, data.Slot)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyAttestation validates input attestation is valid.
|
|
|
|
func (s *Service) verifyAttestation(ctx context.Context, baseState *pb.BeaconState, a *ethpb.Attestation) (*ethpb.IndexedAttestation, error) {
|
|
|
|
committee, err := helpers.BeaconCommitteeFromState(baseState, a.Data.Slot, a.Data.CommitteeIndex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-29 01:44:51 +00:00
|
|
|
indexedAtt, err := attestationutil.ConvertToIndexed(ctx, a, committee)
|
2020-01-28 02:04:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not convert attestation to indexed attestation")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := blocks.VerifyIndexedAttestation(ctx, baseState, indexedAtt); err != nil {
|
|
|
|
|
|
|
|
// TODO(3603): Delete the following signature verify fallback when issue 3603 closes.
|
|
|
|
// When signature fails to verify with committee cache enabled at run time,
|
|
|
|
// the following re-runs the same signature verify routine without cache in play.
|
|
|
|
// This provides extra assurance that committee cache can't break run time.
|
|
|
|
if err == blocks.ErrSigFailedToVerify {
|
|
|
|
committee, err = helpers.BeaconCommitteeWithoutCache(baseState, a.Data.Slot, a.Data.CommitteeIndex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not convert attestation to indexed attestation without cache")
|
|
|
|
}
|
2020-01-29 01:44:51 +00:00
|
|
|
indexedAtt, err = attestationutil.ConvertToIndexed(ctx, a, committee)
|
2020-01-28 02:04:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not convert attestation to indexed attestation")
|
|
|
|
}
|
|
|
|
if err := blocks.VerifyIndexedAttestation(ctx, baseState, indexedAtt); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not verify indexed attestation without cache")
|
|
|
|
}
|
|
|
|
sigFailsToVerify.Inc()
|
|
|
|
return indexedAtt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.Wrap(err, "could not verify indexed attestation")
|
|
|
|
}
|
|
|
|
|
|
|
|
return indexedAtt, nil
|
|
|
|
}
|