Remove preverified hashes (#4088)

Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
This commit is contained in:
ledgerwatch 2022-05-06 15:15:01 +01:00 committed by GitHub
parent 3dc77e7d24
commit ba987cf262
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 4 additions and 179270 deletions

View File

@ -356,9 +356,6 @@ func NewControlServer(db kv.RwDB, nodeName string, chainConfig *params.ChainConf
if err := hd.RecoverFromDb(db); err != nil {
return nil, fmt.Errorf("recovery from DB failed: %w", err)
}
preverifiedHashes, preverifiedHeight := headerdownload.InitPreverifiedHashes(chainConfig.ChainName)
hd.SetPreverifiedHashes(preverifiedHashes, preverifiedHeight)
bd := bodydownload.NewBodyDownload(window /* outstandingLimit */, engine)
cs := &ControlServerImpl{

View File

@ -211,7 +211,7 @@ func InsertChain(ethereum *eth.Ethereum, chain *core.ChainPack) error {
sentryControlServer.Bd.AddMinedBlock(b)
}
sentryControlServer.Hd.MarkAllPreverified()
sentryControlServer.Hd.MarkAllVerified()
_, err := stages.StageLoopStep(ethereum.SentryCtx(), ethereum.ChainDB(), ethereum.StagedSync(), highestSeenHeader, ethereum.Notifications(), initialCycle, sentryControlServer.UpdateHead, nil)
if err != nil {

View File

@ -26,7 +26,6 @@ import (
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/params/networkname"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/turbo/engineapi"
)
@ -235,18 +234,7 @@ func (hd *HeaderDownload) removeUpwards(toRemove []*Link) {
}
}
func (hd *HeaderDownload) MarkPreverified(link *Link) {
// Go through all parent links that are not preverified and mark them too
for link != nil && !link.persisted {
if !link.verified {
link.verified = true
hd.moveLinkToQueue(link, InsertQueueID)
}
link = hd.links[link.header.ParentHash]
}
}
func (hd *HeaderDownload) MarkAllPreverified() {
func (hd *HeaderDownload) MarkAllVerified() {
hd.lock.Lock()
defer hd.lock.Unlock()
for hd.verifyQueue.Len() > 0 {
@ -285,9 +273,6 @@ func (hd *HeaderDownload) extendUp(segment ChainSegment, attachmentLink *Link) {
}
prevLink.next = append(prevLink.next, link)
prevLink = link
if _, ok := hd.preverifiedHashes[link.hash]; ok {
hd.MarkPreverified(link)
}
}
}
@ -298,13 +283,6 @@ func (hd *HeaderDownload) extendDown(segment ChainSegment, anchor *Anchor) bool
hd.invalidateAnchor(anchor, "anchor below preverified")
return false
}
anchorPreverified := false
for _, link := range anchor.links {
if link.verified {
anchorPreverified = true
break
}
}
newAnchorH := segment[len(segment)-1]
newAnchorHeader := newAnchorH.Header
var newAnchor *Anchor
@ -332,16 +310,9 @@ func (hd *HeaderDownload) extendDown(segment ChainSegment, anchor *Anchor) bool
prevLink.next = append(prevLink.next, link)
}
prevLink = link
if _, ok := hd.preverifiedHashes[link.hash]; ok {
hd.MarkPreverified(link)
}
}
prevLink.next = anchor.links
anchor.links = nil
if anchorPreverified {
// Mark the entire segment as preverified
hd.MarkPreverified(prevLink)
}
return !preExisting
}
@ -352,13 +323,6 @@ func (hd *HeaderDownload) connect(segment ChainSegment, attachmentLink *Link, an
hd.invalidateAnchor(anchor, "anchor below preverified")
return penalties
}
anchorPreverified := false
for _, link := range anchor.links {
if link.verified {
anchorPreverified = true
break
}
}
hd.removeAnchor(anchor)
// Iterate over headers backwards (from parents towards children)
prevLink := attachmentLink
@ -374,16 +338,9 @@ func (hd *HeaderDownload) connect(segment ChainSegment, attachmentLink *Link, an
}
prevLink.next = append(prevLink.next, link)
prevLink = link
if _, ok := hd.preverifiedHashes[link.hash]; ok {
hd.MarkPreverified(link)
}
}
prevLink.next = anchor.links
anchor.links = nil
if anchorPreverified {
// Mark the entire segment as preverified
hd.MarkPreverified(prevLink)
}
if _, bad := hd.badHeaders[attachmentLink.hash]; bad {
hd.invalidateAnchor(anchor, "descendant of a known bad block")
penalties = append(penalties, PenaltyItem{Penalty: AbandonedAnchorPenalty, PeerID: anchor.peerID})
@ -433,9 +390,6 @@ func (hd *HeaderDownload) newAnchor(segment ChainSegment, peerID [64]byte) bool
prevLink.next = append(prevLink.next, link)
}
prevLink = link
if _, ok := hd.preverifiedHashes[link.hash]; ok {
hd.MarkPreverified(link)
}
}
return !preExisting
}
@ -536,46 +490,6 @@ func (hd *HeaderDownload) anchorState() string {
return strings.Join(ss, "\n")
}
func InitPreverifiedHashes(chain string) (map[common.Hash]struct{}, uint64) {
var encodings []string
var height uint64
switch chain {
case networkname.MainnetChainName:
encodings = mainnetPreverifiedHashes
height = mainnetPreverifiedHeight
case networkname.RopstenChainName:
encodings = ropstenPreverifiedHashes
height = ropstenPreverifiedHeight
case networkname.SepoliaChainName:
encodings = sepoliaPreverifiedHashes
height = sepoliaPreverifiedHeight
case networkname.GoerliChainName:
encodings = goerliPreverifiedHashes
height = goerliPreverifiedHeight
default:
log.Debug("Preverified hashes not found for", "chain", chain)
return nil, 0
}
return DecodeHashes(encodings), height
}
func DecodeHashes(encodings []string) map[common.Hash]struct{} {
hashes := make(map[common.Hash]struct{}, len(encodings))
for _, encoding := range encodings {
hashes[common.HexToHash(encoding)] = struct{}{}
}
return hashes
}
func (hd *HeaderDownload) SetPreverifiedHashes(preverifiedHashes map[common.Hash]struct{}, preverifiedHeight uint64) {
hd.lock.Lock()
defer hd.lock.Unlock()
hd.preverifiedHashes = preverifiedHashes
hd.preverifiedHeight = preverifiedHeight
}
func (hd *HeaderDownload) RecoverFromDb(db kv.RoDB) error {
hd.lock.Lock()
defer hd.lock.Unlock()
@ -1398,10 +1312,6 @@ func (hd *HeaderDownload) AddMinedHeader(header *types.Header) error {
func (hd *HeaderDownload) AddHeaderFromSnapshot(tx kv.Tx, n uint64, r interfaces.FullBlockReader) error {
hd.lock.Lock()
defer hd.lock.Unlock()
addPreVerifiedHashes := len(hd.preverifiedHashes) == 0
if addPreVerifiedHashes && hd.preverifiedHashes == nil {
hd.preverifiedHashes = map[common.Hash]struct{}{}
}
for i := n; i > 0 && hd.persistedLinkQueue.Len() < hd.persistedLinkLimit; i-- {
header, err := r.HeaderByNumber(context.Background(), tx, i)
@ -1423,9 +1333,6 @@ func (hd *HeaderDownload) AddHeaderFromSnapshot(tx kv.Tx, n uint64, r interfaces
}
link := hd.addHeaderAsLink(h, true /* persisted */)
link.verified = true
if addPreVerifiedHashes {
hd.preverifiedHashes[h.Hash] = struct{}{}
}
}
if hd.highestInDb < n {
hd.highestInDb = n

View File

@ -257,9 +257,8 @@ const ( // SyncStatus values
type HeaderDownload struct {
badHeaders map[common.Hash]struct{}
anchors map[common.Hash]*Anchor // Mapping from parentHash to collection of anchors
preverifiedHashes map[common.Hash]struct{} // Set of hashes that are known to belong to canonical chain
links map[common.Hash]*Link // Links by header hash
anchors map[common.Hash]*Anchor // Mapping from parentHash to collection of anchors
links map[common.Hash]*Link // Links by header hash
engine consensus.Engine
verifyQueue InsertQueue // Priority queue of non-peristed links ready for verification
insertQueue InsertQueue // Priority queue of non-persisted links that are verified and can be inserted
@ -320,7 +319,6 @@ func NewHeaderDownload(
linkLimit: linkLimit - persistentLinkLimit,
anchorLimit: anchorLimit,
engine: engine,
preverifiedHashes: make(map[common.Hash]struct{}),
links: make(map[common.Hash]*Link),
anchorQueue: &AnchorQueue{},
seenAnnounces: NewSeenAnnounces(),

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff