2020-08-07 17:06:29 +08:00
|
|
|
package parlia
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
|
|
|
"github.com/ethereum/go-ethereum/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 {
|
2020-08-09 12:37:39 +08:00
|
|
|
delay := time.Until(time.Unix(int64(header.Time), 0)) // nolint: gosimple
|
2020-08-07 17:06:29 +08:00
|
|
|
if p.chainConfig.IsRamanujan(header.Number) {
|
|
|
|
return delay
|
|
|
|
}
|
2020-08-09 12:37:39 +08:00
|
|
|
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
|
2021-09-28 16:03:38 +08:00
|
|
|
delay += fixedBackOffTimeBeforeFork + time.Duration(rand.Int63n(int64(wiggle)))
|
2020-08-09 12:37:39 +08:00
|
|
|
}
|
|
|
|
return delay
|
2020-08-07 17:06:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parlia) blockTimeForRamanujanFork(snap *Snapshot, header, parent *types.Header) uint64 {
|
|
|
|
blockTime := parent.Time + p.config.Period
|
|
|
|
if p.chainConfig.IsRamanujan(header.Number) {
|
2023-03-01 16:47:38 +08:00
|
|
|
blockTime = blockTime + p.backOffTime(snap, header, p.val)
|
2020-08-07 17:06:29 +08:00
|
|
|
}
|
|
|
|
return blockTime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parlia) blockTimeVerifyForRamanujanFork(snap *Snapshot, header, parent *types.Header) error {
|
|
|
|
if p.chainConfig.IsRamanujan(header.Number) {
|
2023-03-01 16:47:38 +08:00
|
|
|
if header.Time < parent.Time+p.config.Period+p.backOffTime(snap, header, header.Coinbase) {
|
2020-08-07 17:06:29 +08:00
|
|
|
return consensus.ErrFutureBlock
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|