Skip to content

Commit

Permalink
feat(bpos): calculate offset by arbiters count
Browse files Browse the repository at this point in the history
  • Loading branch information
RainFallsSilent committed Jun 15, 2023
1 parent a8a616b commit fcb2e4b
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions dpos/manager/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package manager

import (
"bytes"
"math"
"time"

"github.com/elastos/Elastos.ELA/common"
Expand Down Expand Up @@ -48,10 +49,12 @@ func (v *view) ChangeView(viewOffset *uint32, now time.Time) {
//offset, offsetTime := v.calculateOffsetTimeV0(v.viewStartTime, now)
//*viewOffset += uint32(offset)

offset, offsetTime := v.calculateOffsetTimeV1(*viewOffset, v.viewStartTime, now)
arbitersCount := v.arbitrators.GetArbitersCount()

offset, offsetTime := v.calculateOffsetTimeV1(*viewOffset, v.viewStartTime, now, uint32(arbitersCount))
*viewOffset = uint32(offset)

//offset, offsetTime := v.calculateOffsetTimeV2(*viewOffset, v.viewStartTime, now)
//offset, offsetTime := v.calculateOffsetTimeV2(*viewOffset, v.viewStartTime, now, uint32(arbitersCount))
//*viewOffset = uint32(offset)

v.viewStartTime = now.Add(-offsetTime)
Expand All @@ -76,39 +79,32 @@ func (v *view) calculateOffsetTimeV0(startTime time.Time,
return uint32(offset), offsetTime
}

func (v *view) calculateOffsetTimeV1(currentViewOffset uint32, startTime time.Time, now time.Time) (uint32, time.Duration) {
step := uint32(3)
func (v *view) calculateOffsetTimeV1(currentViewOffset uint32, startTime time.Time,
now time.Time, arbitersCount uint32) (uint32, time.Duration) {
step := float64(3)
duration := now.Sub(startTime)
offsetSeconds := 5 * time.Second
currentOffset := currentViewOffset

offsetSeconds := time.Duration(5*math.Pow(step, float64(currentOffset/arbitersCount))) * time.Second
for duration >= offsetSeconds {
if currentOffset < 36 {
currentOffset++
} else {
currentOffset += 1 << ((currentOffset - 36) / 36)
}
currentOffset++
duration -= offsetSeconds
offsetSeconds = time.Duration((currentOffset/36+1)*5*step) * time.Second
offsetSeconds = time.Duration(5*math.Pow(step, float64(currentOffset/arbitersCount))) * time.Second
}

return currentOffset, duration
}

func (v *view) calculateOffsetTimeV2(currentViewOffset uint32, startTime time.Time, now time.Time) (uint32, time.Duration) {
func (v *view) calculateOffsetTimeV2(currentViewOffset uint32, startTime time.Time,
now time.Time, arbitersCount uint32) (uint32, time.Duration) {
duration := now.Sub(startTime)
offsetSeconds := 5 * time.Second
currentOffset := currentViewOffset

offsetSeconds := time.Duration(5+(currentOffset-arbitersCount)) * time.Second
for duration >= offsetSeconds {
if currentOffset < 36 {
currentOffset++
} else {
currentOffset++
duration -= time.Second
}
currentOffset++
duration -= offsetSeconds
offsetSeconds = time.Duration((currentOffset/36+1)*5+currentOffset-36) * time.Second
offsetSeconds = time.Duration(5+(currentOffset-arbitersCount)) * time.Second
}

return currentOffset, duration
Expand Down

0 comments on commit fcb2e4b

Please sign in to comment.