github.com/phillinzzz/newBsc@v1.1.6/consensus/parlia/ramanujanfork.go (about)

     1  package parlia
     2  
     3  import (
     4  	"math/rand"
     5  	"time"
     6  
     7  	"github.com/phillinzzz/newBsc/consensus"
     8  	"github.com/phillinzzz/newBsc/core/types"
     9  )
    10  
    11  const (
    12  	wiggleTimeBeforeFork       = 500 * time.Millisecond // Random delay (per signer) to allow concurrent signers
    13  	fixedBackOffTimeBeforeFork = 200 * time.Millisecond
    14  )
    15  
    16  func (p *Parlia) delayForRamanujanFork(snap *Snapshot, header *types.Header) time.Duration {
    17  	delay := time.Until(time.Unix(int64(header.Time), 0)) // nolint: gosimple
    18  	if p.chainConfig.IsRamanujan(header.Number) {
    19  		return delay
    20  	}
    21  	if header.Difficulty.Cmp(diffNoTurn) == 0 {
    22  		// It's not our turn explicitly to sign, delay it a bit
    23  		wiggle := time.Duration(len(snap.Validators)/2+1) * wiggleTimeBeforeFork
    24  		delay += fixedBackOffTimeBeforeFork + time.Duration(rand.Int63n(int64(wiggle)))
    25  	}
    26  	return delay
    27  }
    28  
    29  func (p *Parlia) blockTimeForRamanujanFork(snap *Snapshot, header, parent *types.Header) uint64 {
    30  	blockTime := parent.Time + p.config.Period
    31  	if p.chainConfig.IsRamanujan(header.Number) {
    32  		blockTime = blockTime + backOffTime(snap, p.val)
    33  	}
    34  	return blockTime
    35  }
    36  
    37  func (p *Parlia) blockTimeVerifyForRamanujanFork(snap *Snapshot, header, parent *types.Header) error {
    38  	if p.chainConfig.IsRamanujan(header.Number) {
    39  		if header.Time < parent.Time+p.config.Period+backOffTime(snap, header.Coinbase) {
    40  			return consensus.ErrFutureBlock
    41  		}
    42  	}
    43  	return nil
    44  }