prysm-pulse/beacon-chain/sync/subscriber_beacon_blocks.go

46 lines
1.3 KiB
Go
Raw Normal View History

package sync
import (
"context"
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state/interop"
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)
headState := r.chain.HeadState()
// Ignore block older than last finalized checkpoint.
if block.Slot < helpers.StartSlot(headState.FinalizedCheckpoint.Epoch) {
log.Debugf("Received a block older than finalized checkpoint, %d < %d",
block.Slot, helpers.StartSlot(headState.FinalizedCheckpoint.Epoch))
return nil
}
blockRoot, err := ssz.SigningRoot(block)
if err != nil {
log.Errorf("Could not sign root block: %v", err)
return nil
}
// 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()
return nil
}
err = r.chain.ReceiveBlockNoPubsub(ctx, block)
if err != nil {
interop.WriteBlockToDisk(block, true /*failed*/)
}
return err
}