penalize naughty peers on PoS (#4060)

* penalize naughty peers

* lint
This commit is contained in:
Giulio rebuffo 2022-05-04 10:39:34 +02:00 committed by GitHub
parent f634314226
commit 6927ffc54b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 13 deletions

View File

@ -480,9 +480,13 @@ func (cs *ControlServerImpl) blockHeaders(ctx context.Context, pkt eth.BlockHead
return err
}
for _, segment := range segments {
if err := cs.Hd.ProcessSegmentPOS(segment, tx); err != nil {
penalties, err := cs.Hd.ProcessSegmentPOS(segment, tx, ConvertH512ToPeerID(peerID))
if err != nil {
return err
}
if len(penalties) > 0 {
cs.Penalize(ctx, penalties)
}
}
} else {
var canRequestMore bool

View File

@ -893,38 +893,45 @@ func (hd *HeaderDownload) SetHeaderToDownloadPoS(hash common.Hash, height uint64
}
}
func (hd *HeaderDownload) ProcessSegmentPOS(segment ChainSegment, tx kv.Getter) error {
func (hd *HeaderDownload) ProcessSegmentPOS(segment ChainSegment, tx kv.Getter, peerId [64]byte) ([]PenaltyItem, error) {
if len(segment) == 0 {
return nil
return nil, nil
}
// We may have received answer from old request so not enough evidence for penalizing.
if segment[0].Number != hd.posAnchor.blockHeight-1 {
return nil, nil
}
// Handle request after closing collectors
if hd.headersCollector == nil {
return nil, nil
}
hd.lock.Lock()
defer hd.lock.Unlock()
// Handle request after closing collectors
if hd.headersCollector == nil {
return nil
}
log.Trace("Collecting...", "from", segment[0].Number, "to", segment[len(segment)-1].Number, "len", len(segment))
for _, segmentFragment := range segment {
header := segmentFragment.Header
if header.Hash() != hd.posAnchor.parentHash {
return fmt.Errorf("unexpected hash %x (expected %x)", header.Hash(), hd.posAnchor.parentHash)
log.Warn("Unexpected header", "hash", header.Hash(), "expected", hd.posAnchor.parentHash)
return []PenaltyItem{{PeerID: peerId, Penalty: BadBlockPenalty}}, nil
}
headerNumber := header.Number.Uint64()
if err := hd.headersCollector.Collect(dbutils.HeaderKey(headerNumber, header.Hash()), segmentFragment.HeaderRaw); err != nil {
return err
return nil, err
}
hh, err := hd.headerReader.Header(context.Background(), tx, header.ParentHash, headerNumber-1)
if err != nil {
return err
return nil, err
}
if hh != nil {
log.Trace("Synced", "requestId", hd.requestId)
hd.posAnchor = nil
hd.posStatus = Synced
hd.BeaconRequestList.Interrupt(engineapi.Synced)
return nil
return nil, nil
}
hd.posAnchor = &Anchor{
@ -933,10 +940,10 @@ func (hd *HeaderDownload) ProcessSegmentPOS(segment ChainSegment, tx kv.Getter)
}
if headerNumber <= 1 {
return errors.New("wrong genesis in PoS sync")
return nil, errors.New("wrong genesis in PoS sync")
}
}
return nil
return nil, nil
}
// GrabAnnounces - returns all available announces and forget them