2024-01-04 09:44:57 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2024-01-15 11:36:25 +00:00
|
|
|
"fmt"
|
|
|
|
|
2024-01-04 09:44:57 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru/arc/v2"
|
|
|
|
|
2024-01-08 14:55:43 +00:00
|
|
|
"github.com/ledgerwatch/erigon/eth/stagedsync"
|
|
|
|
|
2024-01-04 09:44:57 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
2024-01-09 18:20:42 +00:00
|
|
|
"github.com/ledgerwatch/erigon/polygon/bor"
|
|
|
|
"github.com/ledgerwatch/erigon/polygon/bor/borcfg"
|
|
|
|
"github.com/ledgerwatch/erigon/polygon/bor/valset"
|
2024-01-04 09:44:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DifficultyCalculator interface {
|
|
|
|
HeaderDifficulty(header *types.Header) (uint64, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type difficultyCalculatorImpl struct {
|
2024-01-08 14:55:43 +00:00
|
|
|
borConfig *borcfg.BorConfig
|
2024-01-15 11:36:25 +00:00
|
|
|
spans *SpansCache
|
|
|
|
validatorSetFactory func(headerNum uint64) validatorSetInterface
|
2024-01-08 14:55:43 +00:00
|
|
|
signaturesCache *lru.ARCCache[libcommon.Hash, libcommon.Address]
|
2024-01-04 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDifficultyCalculator(
|
|
|
|
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],
|
2024-01-04 09:44:57 +00:00
|
|
|
) DifficultyCalculator {
|
2024-01-12 15:11:01 +00:00
|
|
|
if signaturesCache == nil {
|
|
|
|
var err error
|
|
|
|
signaturesCache, err = lru.NewARC[libcommon.Hash, libcommon.Address](stagedsync.InMemorySignatures)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-01-04 09:44:57 +00:00
|
|
|
}
|
2024-01-12 15:11:01 +00:00
|
|
|
|
2024-01-08 14:55:43 +00:00
|
|
|
impl := difficultyCalculatorImpl{
|
|
|
|
borConfig: borConfig,
|
2024-01-15 11:36:25 +00:00
|
|
|
spans: spans,
|
2024-01-08 14:55:43 +00:00
|
|
|
validatorSetFactory: validatorSetFactory,
|
|
|
|
signaturesCache: signaturesCache,
|
2024-01-04 09:44:57 +00:00
|
|
|
}
|
2024-01-08 14:55:43 +00:00
|
|
|
|
|
|
|
if validatorSetFactory == nil {
|
|
|
|
impl.validatorSetFactory = impl.makeValidatorSet
|
|
|
|
}
|
|
|
|
|
|
|
|
return &impl
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:36:25 +00:00
|
|
|
func (impl *difficultyCalculatorImpl) makeValidatorSet(headerNum uint64) validatorSetInterface {
|
|
|
|
span := impl.spans.SpanAt(headerNum)
|
|
|
|
if span == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return valset.NewValidatorSet(span.ValidatorSet.Validators)
|
2024-01-04 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (impl *difficultyCalculatorImpl) HeaderDifficulty(header *types.Header) (uint64, error) {
|
|
|
|
signer, err := bor.Ecrecover(header, impl.signaturesCache, impl.borConfig)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2024-01-08 14:55:43 +00:00
|
|
|
return impl.signerDifficulty(signer, header.Number.Uint64())
|
|
|
|
}
|
2024-01-04 09:44:57 +00:00
|
|
|
|
2024-01-08 14:55:43 +00:00
|
|
|
func (impl *difficultyCalculatorImpl) signerDifficulty(signer libcommon.Address, headerNum uint64) (uint64, error) {
|
2024-01-15 11:36:25 +00:00
|
|
|
validatorSet := impl.validatorSetFactory(headerNum)
|
|
|
|
if validatorSet == nil {
|
|
|
|
return 0, fmt.Errorf("difficultyCalculatorImpl.signerDifficulty: no span at %d", headerNum)
|
|
|
|
}
|
2024-01-04 09:44:57 +00:00
|
|
|
|
2024-01-08 14:55:43 +00:00
|
|
|
sprintNum := impl.borConfig.CalculateSprintNumber(headerNum)
|
|
|
|
if sprintNum > 0 {
|
2024-01-11 01:45:48 +00:00
|
|
|
validatorSet.IncrementProposerPriority(int(sprintNum))
|
2024-01-04 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return validatorSet.Difficulty(signer)
|
|
|
|
}
|