@@ -187,7 +187,7 @@ func (hd *HeaderDownload) extendUp(segment *ChainSegment, start, end int) error
187187 }
188188 }
189189
190- if attachmentLink .persisted {
190+ if _ , bad := hd . badHeaders [ attachmentLink . hash ]; ! bad && attachmentLink .persisted {
191191 link := hd .links [linkHeader .Hash ()]
192192 hd .insertList = append (hd .insertList , link )
193193 }
@@ -255,21 +255,21 @@ func (hd *HeaderDownload) extendDown(segment *ChainSegment, start, end int) (boo
255255}
256256
257257// Connect connects some working trees using anchors of some, and a link of another
258- func (hd * HeaderDownload ) connect (segment * ChainSegment , start , end int ) error {
258+ func (hd * HeaderDownload ) connect (segment * ChainSegment , start , end int ) ([] PenaltyItem , error ) {
259259 // Find attachment link again
260260 linkHeader := segment .Headers [end - 1 ]
261261 // Find attachement anchors again
262262 anchorHeader := segment .Headers [start ]
263263 attachmentLink , ok1 := hd .getLink (linkHeader .ParentHash )
264264 if ! ok1 {
265- return fmt .Errorf ("connect attachment link not found for %x" , linkHeader .ParentHash )
265+ return nil , fmt .Errorf ("connect attachment link not found for %x" , linkHeader .ParentHash )
266266 }
267267 if attachmentLink .preverified && len (attachmentLink .next ) > 0 {
268- return fmt .Errorf ("cannot connect to preverified link %d with children" , attachmentLink .blockHeight )
268+ return nil , fmt .Errorf ("cannot connect to preverified link %d with children" , attachmentLink .blockHeight )
269269 }
270270 anchor , ok2 := hd .anchors [anchorHeader .Hash ()]
271271 if ! ok2 {
272- return fmt .Errorf ("connect attachment anchors not found for %x" , anchorHeader .Hash ())
272+ return nil , fmt .Errorf ("connect attachment anchors not found for %x" , anchorHeader .Hash ())
273273 }
274274 anchorPreverified := false
275275 for _ , link := range anchor .links {
@@ -300,11 +300,15 @@ func (hd *HeaderDownload) connect(segment *ChainSegment, start, end int) error {
300300 // Mark the entire segment as preverified
301301 hd .markPreverified (prevLink )
302302 }
303- if attachmentLink .persisted {
303+ var penalties []PenaltyItem
304+ if _ , bad := hd .badHeaders [attachmentLink .hash ]; bad {
305+ hd .invalidateAnchor (anchor )
306+ penalties = append (penalties , PenaltyItem {Penalty : AbandonedAnchorPenalty , PeerID : anchor .peerID })
307+ } else if attachmentLink .persisted {
304308 link := hd .links [linkHeader .Hash ()]
305309 hd .insertList = append (hd .insertList , link )
306310 }
307- return nil
311+ return penalties , nil
308312}
309313
310314// if anchor will be abandoned - given peerID will get Penalty
@@ -826,7 +830,7 @@ func (hi *HeaderInserter) BestHeaderChanged() bool {
826830// it allows higher-level algo immediately request more headers without waiting all stages precessing,
827831// speeds up visibility of new blocks
828832// It remember peerID - then later - if anchors created from segments will abandoned - this peerID gonna get Penalty
829- func (hd * HeaderDownload ) ProcessSegment (segment * ChainSegment , newBlock bool , peerID string ) (requestMore bool ) {
833+ func (hd * HeaderDownload ) ProcessSegment (segment * ChainSegment , newBlock bool , peerID string ) (requestMore bool , penalties [] PenaltyItem ) {
830834 log .Debug ("processSegment" , "from" , segment .Headers [0 ].Number .Uint64 (), "to" , segment .Headers [len (segment .Headers )- 1 ].Number .Uint64 ())
831835 hd .lock .Lock ()
832836 defer hd .lock .Unlock ()
@@ -849,7 +853,8 @@ func (hd *HeaderDownload) ProcessSegment(segment *ChainSegment, newBlock bool, p
849853 if foundAnchor {
850854 if foundTip {
851855 // Connect
852- if err := hd .connect (segment , start , end ); err != nil {
856+ var err error
857+ if penalties , err = hd .connect (segment , start , end ); err != nil {
853858 log .Debug ("Connect failed" , "error" , err )
854859 return
855860 }
@@ -919,7 +924,7 @@ func (hd *HeaderDownload) ProcessSegment(segment *ChainSegment, newBlock bool, p
919924 default :
920925 }
921926
922- return hd .requestChaining && requestMore
927+ return hd .requestChaining && requestMore , penalties
923928}
924929
925930func (hd * HeaderDownload ) TopSeenHeight () uint64 {
0 commit comments