Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R4R] minor fix for ramanujan upgrade #25

Merged
merged 3 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,11 @@ func backOffTime(snap *Snapshot, val common.Address) uint64 {
if snap.inturn(val) {
return 0
} else {
dis := snap.distanceToInTurn(val)
idx := snap.indexOfVal(val)
if idx < 0 {
// The backOffTime does not matter when a validator is not authorized.
return 0
}
s := rand.NewSource(int64(snap.Number))
r := rand.New(s)
n := len(snap.Validators)
Expand All @@ -1156,7 +1160,7 @@ func backOffTime(snap *Snapshot, val common.Address) uint64 {
r.Shuffle(n, func(i, j int) {
backOffSteps[i], backOffSteps[j] = backOffSteps[j], backOffSteps[i]
})
delay := initialBackOffTime + backOffSteps[dis]*wiggleTime
delay := initialBackOffTime + backOffSteps[idx]*wiggleTime
return delay
}
}
Expand Down
10 changes: 7 additions & 3 deletions consensus/parlia/ramanujanfork.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ const (
)

func (p *Parlia) delayForRamanujanFork(snap *Snapshot, header *types.Header) time.Duration {
delay := time.Unix(int64(header.Time), 0).Sub(time.Now()) // nolint: gosimple
delay := time.Until(time.Unix(int64(header.Time), 0)) // nolint: gosimple
if p.chainConfig.IsRamanujan(header.Number) {
return delay
}
wiggle := time.Duration(len(snap.Validators)/2+1) * wiggleTimeBeforeFork
return delay + time.Duration(fixedBackOffTimeBeforeFork) + time.Duration(rand.Int63n(int64(wiggle)))
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
delay += time.Duration(fixedBackOffTimeBeforeFork) + time.Duration(rand.Int63n(int64(wiggle)))
}
return delay
}

func (p *Parlia) blockTimeForRamanujanFork(snap *Snapshot, header, parent *types.Header) uint64 {
Expand Down
16 changes: 6 additions & 10 deletions consensus/parlia/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,14 @@ func (s *Snapshot) inturn(validator common.Address) bool {
return validators[offset] == validator
}

func (s *Snapshot) distanceToInTurn(validator common.Address) uint64 {
func (s *Snapshot) indexOfVal(validator common.Address) int {
validators := s.validators()
offset := (s.Number + 1) % uint64(len(validators))
idx := uint64(0)
for idx < uint64(len(validator)) && validators[idx] != validator {
idx++
}
if offset > idx {
return uint64(len(validators)) + idx - offset
} else {
return idx - offset
for idx, val := range validators {
if val == validator {
return idx
}
}
return -1
}

func (s *Snapshot) supposeValidator() common.Address {
Expand Down
20 changes: 10 additions & 10 deletions core/systemcontracts/upgrade.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ var (
PetersburgBlock: big.NewInt(0),
IstanbulBlock: big.NewInt(0),
MuirGlacierBlock: big.NewInt(0),
RamanujanBlock: big.NewInt(260942),
RamanujanBlock: big.NewInt(400),
Parlia: &ParliaConfig{
Period: 3,
Epoch: 200,
Expand Down