2019-08-23 01:00:55 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
2019-08-24 23:56:40 +00:00
|
|
|
"bytes"
|
2019-08-23 01:00:55 +00:00
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-08-28 03:19:47 +00:00
|
|
|
"github.com/prysmaticlabs/go-ssz"
|
2019-08-23 01:00:55 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
2019-08-23 17:48:40 +00:00
|
|
|
// AttestationReceiver interface defines the methods of chain service receive and processing new attestations.
|
|
|
|
type AttestationReceiver interface {
|
|
|
|
ReceiveAttestation(ctx context.Context, att *ethpb.Attestation) error
|
|
|
|
ReceiveAttestationNoPubsub(ctx context.Context, att *ethpb.Attestation) error
|
|
|
|
}
|
|
|
|
|
2019-08-23 01:00:55 +00:00
|
|
|
// ReceiveAttestation is a function that defines the operations that are preformed on
|
|
|
|
// attestation that is received from regular sync. The operations consist of:
|
|
|
|
// 1. Gossip attestation to other peers
|
|
|
|
// 2. Validate attestation, update validator's latest vote
|
|
|
|
// 3. Apply fork choice to the processed attestation
|
|
|
|
// 4. Save latest head info
|
|
|
|
func (c *ChainService) ReceiveAttestation(ctx context.Context, att *ethpb.Attestation) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "beacon-chain.blockchain.ReceiveAttestation")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
// Broadcast the new attestation to the network.
|
|
|
|
if err := c.p2p.Broadcast(ctx, att); err != nil {
|
|
|
|
return errors.Wrap(err, "could not broadcast attestation")
|
|
|
|
}
|
2019-08-28 03:19:47 +00:00
|
|
|
|
|
|
|
attRoot, err := ssz.HashTreeRoot(att)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to hash attestation")
|
|
|
|
}
|
|
|
|
|
2019-08-23 01:00:55 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
2019-08-28 15:29:45 +00:00
|
|
|
"attRoot": hex.EncodeToString(attRoot[:]),
|
2019-08-23 01:00:55 +00:00
|
|
|
"attDataRoot": hex.EncodeToString(att.Data.BeaconBlockRoot),
|
2019-08-24 13:26:25 +00:00
|
|
|
}).Debug("Broadcasting attestation")
|
2019-08-23 01:00:55 +00:00
|
|
|
|
2019-08-23 18:18:39 +00:00
|
|
|
if err := c.ReceiveAttestationNoPubsub(ctx, att); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
processedAtt.Inc()
|
|
|
|
return nil
|
2019-08-23 01:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReceiveAttestationNoPubsub is a function that defines the operations that are preformed on
|
|
|
|
// attestation that is received from regular sync. The operations consist of:
|
|
|
|
// 1. Validate attestation, update validator's latest vote
|
|
|
|
// 2. Apply fork choice to the processed attestation
|
|
|
|
// 3. Save latest head info
|
|
|
|
func (c *ChainService) ReceiveAttestationNoPubsub(ctx context.Context, att *ethpb.Attestation) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "beacon-chain.blockchain.ReceiveAttestationNoPubsub")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
// Update forkchoice store for the new attestation
|
2019-08-28 17:26:07 +00:00
|
|
|
attSlot, err := c.forkChoiceStore.OnAttestation(ctx, att)
|
|
|
|
if err != nil {
|
2019-08-23 01:00:55 +00:00
|
|
|
return errors.Wrap(err, "could not process block from fork choice service")
|
|
|
|
}
|
2019-08-28 17:26:07 +00:00
|
|
|
|
2019-08-23 01:00:55 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
2019-08-28 17:26:07 +00:00
|
|
|
"attSlot": attSlot,
|
2019-08-23 01:00:55 +00:00
|
|
|
"attDataRoot": hex.EncodeToString(att.Data.BeaconBlockRoot),
|
2019-08-24 13:26:25 +00:00
|
|
|
}).Debug("Finished updating fork choice store for attestation")
|
2019-08-23 01:00:55 +00:00
|
|
|
|
|
|
|
// Run fork choice for head block after updating fork choice store.
|
|
|
|
headRoot, err := c.forkChoiceStore.Head(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not get head from fork choice service")
|
|
|
|
}
|
|
|
|
headBlk, err := c.beaconDB.Block(ctx, bytesutil.ToBytes32(headRoot))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not compute state from block head")
|
|
|
|
}
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"headSlot": headBlk.Slot,
|
|
|
|
"headRoot": hex.EncodeToString(headRoot),
|
2019-08-24 23:56:40 +00:00
|
|
|
}).Debug("Finished applying fork choice for attestation")
|
|
|
|
|
|
|
|
// Skip checking for competing attestation's target roots at epoch boundary.
|
2019-08-28 17:26:07 +00:00
|
|
|
if !helpers.IsEpochStart(attSlot) {
|
2019-08-24 23:56:40 +00:00
|
|
|
targetRoot, err := helpers.BlockRoot(c.headState, att.Data.Target.Epoch)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not get target root for epoch %d", att.Data.Target.Epoch)
|
|
|
|
}
|
|
|
|
isCompetingAtts(targetRoot, att.Data.Target.Root[:])
|
|
|
|
}
|
2019-08-23 18:18:39 +00:00
|
|
|
|
2019-08-23 01:00:55 +00:00
|
|
|
// Save head info after running fork choice.
|
|
|
|
if err := c.saveHead(ctx, headBlk, bytesutil.ToBytes32(headRoot)); err != nil {
|
|
|
|
return errors.Wrap(err, "could not save head")
|
|
|
|
}
|
|
|
|
|
2019-08-23 18:18:39 +00:00
|
|
|
processedAttNoPubsub.Inc()
|
2019-08-23 01:00:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-24 23:56:40 +00:00
|
|
|
// This checks if the attestation is from a competing chain, emits warning and updates metrics.
|
|
|
|
func isCompetingAtts(headTargetRoot []byte, attTargetRoot []byte) {
|
|
|
|
if !bytes.Equal(attTargetRoot, headTargetRoot) {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"attTargetRoot": hex.EncodeToString(attTargetRoot),
|
|
|
|
"headTargetRoot": hex.EncodeToString(headTargetRoot),
|
|
|
|
}).Warn("target heads different from new attestation")
|
|
|
|
competingAtts.Inc()
|
|
|
|
}
|
|
|
|
}
|