prysm-pulse/beacon-chain/sync/subscriber_beacon_blocks.go
Nishant Das 97f6143a43
Add Remaining Gossip Changes (#9553)
* add changes

* add changes here in

* rem duplicate import

* fix topic not being set in test

* terence's review

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2021-09-10 02:36:31 +00:00

80 lines
2.3 KiB
Go

package sync
import (
"context"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/transition/interop"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/block"
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
wrapperv2 "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"google.golang.org/protobuf/proto"
)
func (s *Service) beaconBlockSubscriber(ctx context.Context, msg proto.Message) error {
signed, err := blockFromProto(msg)
if err != nil {
return err
}
if signed.IsNil() || signed.Block().IsNil() {
return errors.New("nil block")
}
s.setSeenBlockIndexSlot(signed.Block().Slot(), signed.Block().ProposerIndex())
block := signed.Block()
root, err := block.HashTreeRoot()
if err != nil {
return err
}
if err := s.cfg.Chain.ReceiveBlock(ctx, signed, root); err != nil {
interop.WriteBlockToDisk(signed, true /*failed*/)
s.setBadBlock(ctx, root)
return err
}
if !featureconfig.Get().CorrectlyPruneCanonicalAtts {
// Delete attestations from the block in the pool to avoid inclusion in future block.
if err := s.deleteAttsInPool(block.Body().Attestations()); err != nil {
log.Debugf("Could not delete attestations in pool: %v", err)
return nil
}
}
return err
}
// The input attestations are seen by the network, this deletes them from pool
// so proposers don't include them in a block for the future.
func (s *Service) deleteAttsInPool(atts []*ethpb.Attestation) error {
for _, att := range atts {
if helpers.IsAggregated(att) {
if err := s.cfg.AttPool.DeleteAggregatedAttestation(att); err != nil {
return err
}
} else {
// Ideally there's shouldn't be any unaggregated attestation in the block.
if err := s.cfg.AttPool.DeleteUnaggregatedAttestation(att); err != nil {
return err
}
}
}
return nil
}
func blockFromProto(msg proto.Message) (block.SignedBeaconBlock, error) {
switch t := msg.(type) {
case *ethpb.SignedBeaconBlock:
return wrapper.WrappedPhase0SignedBeaconBlock(t), nil
case *ethpb.SignedBeaconBlockAltair:
return wrapperv2.WrappedAltairSignedBeaconBlock(t)
default:
return nil, errors.Errorf("message has invalid underlying type: %T", msg)
}
}