2024-01-12 15:11:01 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2024-01-15 11:36:25 +00:00
|
|
|
"fmt"
|
2024-01-12 15:11:01 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
lru "github.com/hashicorp/golang-lru/arc/v2"
|
|
|
|
|
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/eth/stagedsync"
|
|
|
|
"github.com/ledgerwatch/erigon/polygon/bor"
|
|
|
|
"github.com/ledgerwatch/erigon/polygon/bor/borcfg"
|
|
|
|
"github.com/ledgerwatch/erigon/polygon/bor/valset"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HeaderTimeValidator interface {
|
|
|
|
ValidateHeaderTime(header *types.Header, now time.Time, parent *types.Header) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type headerTimeValidatorImpl struct {
|
|
|
|
borConfig *borcfg.BorConfig
|
2024-01-15 11:36:25 +00:00
|
|
|
spans *SpansCache
|
|
|
|
validatorSetFactory func(headerNum uint64) validatorSetInterface
|
2024-01-12 15:11:01 +00:00
|
|
|
signaturesCache *lru.ARCCache[libcommon.Hash, libcommon.Address]
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHeaderTimeValidator(
|
|
|
|
borConfig *borcfg.BorConfig,
|
2024-01-15 11:36:25 +00:00
|
|
|
spans *SpansCache,
|
|
|
|
validatorSetFactory func(headerNum uint64) validatorSetInterface,
|
2024-01-12 15:11:01 +00:00
|
|
|
signaturesCache *lru.ARCCache[libcommon.Hash, libcommon.Address],
|
|
|
|
) HeaderTimeValidator {
|
|
|
|
if signaturesCache == nil {
|
|
|
|
var err error
|
|
|
|
signaturesCache, err = lru.NewARC[libcommon.Hash, libcommon.Address](stagedsync.InMemorySignatures)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl := headerTimeValidatorImpl{
|
|
|
|
borConfig: borConfig,
|
2024-01-15 11:36:25 +00:00
|
|
|
spans: spans,
|
2024-01-12 15:11:01 +00:00
|
|
|
validatorSetFactory: validatorSetFactory,
|
|
|
|
signaturesCache: signaturesCache,
|
|
|
|
}
|
|
|
|
|
|
|
|
if validatorSetFactory == nil {
|
|
|
|
impl.validatorSetFactory = impl.makeValidatorSet
|
|
|
|
}
|
|
|
|
|
|
|
|
return &impl
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:36:25 +00:00
|
|
|
func (impl *headerTimeValidatorImpl) makeValidatorSet(headerNum uint64) validatorSetInterface {
|
|
|
|
span := impl.spans.SpanAt(headerNum)
|
|
|
|
if span == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return valset.NewValidatorSet(span.ValidatorSet.Validators)
|
2024-01-12 15:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (impl *headerTimeValidatorImpl) ValidateHeaderTime(header *types.Header, now time.Time, parent *types.Header) error {
|
2024-01-15 11:36:25 +00:00
|
|
|
headerNum := header.Number.Uint64()
|
|
|
|
validatorSet := impl.validatorSetFactory(headerNum)
|
|
|
|
if validatorSet == nil {
|
|
|
|
return fmt.Errorf("headerTimeValidatorImpl.ValidateHeaderTime: no span at %d", headerNum)
|
|
|
|
}
|
2024-01-12 15:11:01 +00:00
|
|
|
|
2024-01-15 11:36:25 +00:00
|
|
|
sprintNum := impl.borConfig.CalculateSprintNumber(headerNum)
|
2024-01-12 15:11:01 +00:00
|
|
|
if sprintNum > 0 {
|
|
|
|
validatorSet.IncrementProposerPriority(int(sprintNum))
|
|
|
|
}
|
|
|
|
|
|
|
|
return bor.ValidateHeaderTime(header, now, parent, validatorSet, impl.borConfig, impl.signaturesCache)
|
|
|
|
}
|