2024-01-16 08:23:02 +00:00
|
|
|
package bor
|
2023-12-28 15:52:49 +00:00
|
|
|
|
2024-01-04 09:44:57 +00:00
|
|
|
import (
|
2024-01-09 18:20:42 +00:00
|
|
|
"github.com/ledgerwatch/erigon/polygon/bor/borcfg"
|
2024-01-04 09:44:57 +00:00
|
|
|
)
|
2023-12-28 15:52:49 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
spanLength = 6400 // Number of blocks in a span
|
|
|
|
zerothSpanEnd = 255 // End block of 0th span
|
|
|
|
)
|
|
|
|
|
2024-01-16 08:23:02 +00:00
|
|
|
// SpanIDAt returns the corresponding span id for the given block number.
|
|
|
|
func SpanIDAt(blockNum uint64) uint64 {
|
2023-12-28 15:52:49 +00:00
|
|
|
if blockNum > zerothSpanEnd {
|
|
|
|
return 1 + (blockNum-zerothSpanEnd-1)/spanLength
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-01-16 08:23:02 +00:00
|
|
|
// SpanEndBlockNum returns the number of the last block in the given span.
|
|
|
|
func SpanEndBlockNum(spanID uint64) uint64 {
|
2023-12-28 17:49:31 +00:00
|
|
|
if spanID > 0 {
|
|
|
|
return spanID*spanLength + zerothSpanEnd
|
2023-12-28 15:52:49 +00:00
|
|
|
}
|
|
|
|
return zerothSpanEnd
|
|
|
|
}
|
|
|
|
|
2024-01-16 08:23:02 +00:00
|
|
|
// IsBlockInLastSprintOfSpan returns true if a block num is within the last sprint of a span and false otherwise.
|
|
|
|
func IsBlockInLastSprintOfSpan(blockNum uint64, config *borcfg.BorConfig) bool {
|
|
|
|
spanNum := SpanIDAt(blockNum)
|
|
|
|
endBlockNum := SpanEndBlockNum(spanNum)
|
2024-01-04 09:44:57 +00:00
|
|
|
sprintLen := config.CalculateSprintLength(blockNum)
|
2023-12-28 15:52:49 +00:00
|
|
|
startBlockNum := endBlockNum - sprintLen + 1
|
|
|
|
return startBlockNum <= blockNum && blockNum <= endBlockNum
|
|
|
|
}
|