-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It is possible that a span update happens during a milestone. A headers slice might cross to the new span. Also if 2 forks evolve simulaneously, a shorter fork can still be in the previous span. In these cases we need access to the previous span to calculate difficulty and validate header times. SpansCache will keep recent spans. The cache will be updated on new span events from the heimdall. The cache is pruned on new milestone events and in practice no more than 2 spans are kept. The header difficulty calculation and time validation depends on having a span for that header in the cache.
- Loading branch information
1 parent
8d4d4d8
commit c5b75d0
Showing
6 changed files
with
96 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package sync | ||
|
||
import heimdallspan "github.com/ledgerwatch/erigon/polygon/heimdall/span" | ||
|
||
type SpansCache struct { | ||
spans map[uint64]*heimdallspan.HeimdallSpan | ||
} | ||
|
||
func NewSpansCache() *SpansCache { | ||
return &SpansCache{ | ||
spans: make(map[uint64]*heimdallspan.HeimdallSpan), | ||
} | ||
} | ||
|
||
func (cache *SpansCache) Add(span *heimdallspan.HeimdallSpan) { | ||
cache.spans[span.StartBlock] = span | ||
} | ||
|
||
// SpanAt finds a span that contains blockNum. | ||
func (cache *SpansCache) SpanAt(blockNum uint64) *heimdallspan.HeimdallSpan { | ||
for _, span := range cache.spans { | ||
if (span.StartBlock <= blockNum) && (blockNum <= span.EndBlock) { | ||
return span | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Prune removes spans that ended before blockNum. | ||
func (cache *SpansCache) Prune(blockNum uint64) { | ||
for key, span := range cache.spans { | ||
if span.EndBlock < blockNum { | ||
delete(cache.spans, key) | ||
} | ||
} | ||
} |