Fix nil deference in ProcessHeadersPOS (#4155)

Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
This commit is contained in:
ledgerwatch 2022-05-15 17:09:27 +01:00 committed by GitHub
parent a9ce803c67
commit 7125593cf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import (
"context"
"fmt"
"math/rand"
"sort"
"sync"
"time"
@ -487,6 +488,7 @@ func (cs *MultyClient) blockHeaders(ctx context.Context, pkt eth.BlockHeadersPac
})
}
if cs.Hd.POSSync() {
sort.Sort(headerdownload.HeadersByHeightAndHash(csHeaders)) // Sorting by reverse order of block heights
tx, err := cs.db.BeginRo(ctx)
defer tx.Rollback()
if err != nil {

View File

@ -54,6 +54,25 @@ const POSPandaBanner = `
`
// Implements sort.Interface so we can sort the incoming header in the message by block height
type HeadersByHeightAndHash []ChainSegmentHeader
func (h HeadersByHeightAndHash) Len() int {
return len(h)
}
func (h HeadersByHeightAndHash) Less(i, j int) bool {
// Note - the ordering is the inverse ordering of the block heights
if h[i].Number != h[j].Number {
return h[i].Number > h[j].Number
}
return bytes.Compare(h[i].Hash[:], h[j].Hash[:]) > 0
}
func (h HeadersByHeightAndHash) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
// SingleHeaderAsSegment converts message containing 1 header into one singleton chain segment
func (hd *HeaderDownload) SingleHeaderAsSegment(headerRaw []byte, header *types.Header, penalizePoSBlocks bool) ([]ChainSegmentHeader, Penalty, error) {
hd.lock.RLock()
@ -529,9 +548,12 @@ func (hd *HeaderDownload) SetHeaderToDownloadPoS(hash common.Hash, height uint64
}
func (hd *HeaderDownload) ProcessHeadersPOS(csHeaders []ChainSegmentHeader, tx kv.Getter, peerId [64]byte) ([]PenaltyItem, error) {
if len(csHeaders) == 0 {
return nil, nil
}
hd.lock.Lock()
defer hd.lock.Unlock()
if len(csHeaders) == 0 {
if hd.posAnchor == nil {
return nil, nil
}
// We may have received answer from old request so not enough evidence for penalizing.