2019-08-22 18:11:52 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2019-09-24 04:46:40 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state/interop"
|
2019-08-22 18:11:52 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (r *RegularSync) beaconBlockSubscriber(ctx context.Context, msg proto.Message) error {
|
|
|
|
block := msg.(*ethpb.BeaconBlock)
|
|
|
|
|
2019-11-19 16:12:50 +00:00
|
|
|
headState, err := r.chain.HeadState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Head state is not available: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-22 18:11:52 +00:00
|
|
|
// Ignore block older than last finalized checkpoint.
|
2019-08-28 15:29:45 +00:00
|
|
|
if block.Slot < helpers.StartSlot(headState.FinalizedCheckpoint.Epoch) {
|
2019-10-01 20:05:17 +00:00
|
|
|
log.Debugf("Received a block older than finalized checkpoint, %d < %d",
|
2019-08-28 15:29:45 +00:00
|
|
|
block.Slot, helpers.StartSlot(headState.FinalizedCheckpoint.Epoch))
|
2019-08-22 18:11:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-20 17:08:32 +00:00
|
|
|
blockRoot, err := ssz.SigningRoot(block)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not sign root block: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-22 18:11:52 +00:00
|
|
|
|
2019-09-20 17:08:32 +00:00
|
|
|
// Handle block when the parent is unknown
|
|
|
|
if !r.db.HasBlock(ctx, bytesutil.ToBytes32(block.ParentRoot)) {
|
|
|
|
r.pendingQueueLock.Lock()
|
|
|
|
r.slotToPendingBlocks[block.Slot] = block
|
|
|
|
r.seenPendingBlocks[blockRoot] = true
|
|
|
|
r.pendingQueueLock.Unlock()
|
2019-08-22 18:11:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-24 04:46:40 +00:00
|
|
|
err = r.chain.ReceiveBlockNoPubsub(ctx, block)
|
|
|
|
if err != nil {
|
|
|
|
interop.WriteBlockToDisk(block, true /*failed*/)
|
|
|
|
}
|
|
|
|
return err
|
2019-08-22 18:11:52 +00:00
|
|
|
}
|