2021-12-11 00:07:10 +00:00
|
|
|
package parlia
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon/consensus"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
wiggleTimeBeforeFork = 500 * time.Millisecond // Random delay (per signer) to allow concurrent signers
|
|
|
|
fixedBackOffTimeBeforeFork = 200 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p *Parlia) delayForRamanujanFork(snap *Snapshot, header *types.Header) time.Duration {
|
|
|
|
delay := time.Until(time.Unix(int64(header.Time), 0)) // nolint: gosimple
|
2022-01-14 19:06:35 +00:00
|
|
|
if p.chainConfig.IsRamanujan(header.Number.Uint64()) {
|
2021-12-11 00:07:10 +00:00
|
|
|
return delay
|
|
|
|
}
|
|
|
|
if header.Difficulty.Cmp(diffNoTurn) == 0 {
|
|
|
|
// It's not our turn explicitly to sign, delay it a bit
|
|
|
|
wiggle := time.Duration(len(snap.Validators)/2+1) * wiggleTimeBeforeFork
|
2022-08-04 13:59:40 +00:00
|
|
|
delay += fixedBackOffTimeBeforeFork + time.Duration(rand.Int63n(int64(wiggle))) // nolint
|
2021-12-11 00:07:10 +00:00
|
|
|
}
|
|
|
|
return delay
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parlia) blockTimeForRamanujanFork(snap *Snapshot, header, parent *types.Header) uint64 {
|
|
|
|
blockTime := parent.Time + p.config.Period
|
2022-01-14 19:06:35 +00:00
|
|
|
if p.chainConfig.IsRamanujan(header.Number.Uint64()) {
|
2021-12-11 00:07:10 +00:00
|
|
|
blockTime = blockTime + backOffTime(snap, p.val)
|
|
|
|
}
|
|
|
|
return blockTime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parlia) blockTimeVerifyForRamanujanFork(snap *Snapshot, header, parent *types.Header) error {
|
2022-01-14 19:06:35 +00:00
|
|
|
if p.chainConfig.IsRamanujan(header.Number.Uint64()) {
|
2021-12-11 00:07:10 +00:00
|
|
|
if header.Time < parent.Time+p.config.Period+backOffTime(snap, header.Coinbase) {
|
|
|
|
return consensus.ErrFutureBlock
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|