ramanujanfork.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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.Until(time.Unix(int64(header.Time), 0)) // nolint: gosimple
  14. if p.chainConfig.IsRamanujan(header.Number) {
  15. return delay
  16. }
  17. if header.Difficulty.Cmp(diffNoTurn) == 0 {
  18. // It's not our turn explicitly to sign, delay it a bit
  19. wiggle := time.Duration(len(snap.Validators)/2+1) * wiggleTimeBeforeFork
  20. delay += fixedBackOffTimeBeforeFork + time.Duration(rand.Int63n(int64(wiggle)))
  21. }
  22. return delay
  23. }
  24. func (p *Parlia) blockTimeForRamanujanFork(snap *Snapshot, header, parent *types.Header) uint64 {
  25. blockTime := parent.Time + p.config.Period
  26. if p.chainConfig.IsRamanujan(header.Number) {
  27. blockTime = blockTime + backOffTime(snap, p.val)
  28. }
  29. return blockTime
  30. }
  31. func (p *Parlia) blockTimeVerifyForRamanujanFork(snap *Snapshot, header, parent *types.Header) error {
  32. if p.chainConfig.IsRamanujan(header.Number) {
  33. if header.Time < parent.Time+p.config.Period+backOffTime(snap, header.Coinbase) {
  34. return consensus.ErrFutureBlock
  35. }
  36. }
  37. return nil
  38. }