ramanujanfork.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package parlia
  2. import (
  3. "math/rand"
  4. "time"
  5. "github.com/ethereum/go-ethereum/consensus"
  6. "github.com/ethereum/go-ethereum/core/types"
  7. )
  8. const (
  9. wiggleTimeBeforeFork = 500 * time.Millisecond // Random delay (per signer) to allow concurrent signers
  10. fixedBackOffTimeBeforeFork = 200 * time.Millisecond
  11. )
  12. func (p *Parlia) delayForRamanujanFork(snap *Snapshot, header *types.Header) time.Duration {
  13. delay := time.Unix(int64(header.Time), 0).Sub(time.Now()) // nolint: gosimple
  14. if p.chainConfig.IsRamanujan(header.Number) {
  15. return delay
  16. }
  17. wiggle := time.Duration(len(snap.Validators)/2+1) * wiggleTimeBeforeFork
  18. return delay + time.Duration(fixedBackOffTimeBeforeFork) + time.Duration(rand.Int63n(int64(wiggle)))
  19. }
  20. func (p *Parlia) blockTimeForRamanujanFork(snap *Snapshot, header, parent *types.Header) uint64 {
  21. blockTime := parent.Time + p.config.Period
  22. if p.chainConfig.IsRamanujan(header.Number) {
  23. blockTime = blockTime + backOffTime(snap, p.val)
  24. }
  25. return blockTime
  26. }
  27. func (p *Parlia) blockTimeVerifyForRamanujanFork(snap *Snapshot, header, parent *types.Header) error {
  28. if p.chainConfig.IsRamanujan(header.Number) {
  29. if header.Time < parent.Time+p.config.Period+backOffTime(snap, header.Coinbase) {
  30. return consensus.ErrFutureBlock
  31. }
  32. }
  33. return nil
  34. }