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 57a0abf
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions dpos/manager/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,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 +78,41 @@ 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) {
func (v *view) calculateOffsetTimeV1(currentViewOffset uint32, startTime time.Time,
now time.Time, arbitersCount uint32) (uint32, time.Duration) {
step := uint32(3)
duration := now.Sub(startTime)
offsetSeconds := 5 * time.Second
currentOffset := currentViewOffset

for duration >= offsetSeconds {
if currentOffset < 36 {
if currentOffset < arbitersCount {
currentOffset++
} else {
currentOffset += 1 << ((currentOffset - 36) / 36)
currentOffset += 1 << ((currentOffset - arbitersCount) / arbitersCount)
}
duration -= offsetSeconds
offsetSeconds = time.Duration((currentOffset/36+1)*5*step) * time.Second
offsetSeconds = time.Duration((currentOffset/arbitersCount+1)*5*step) * 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

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

return currentOffset, duration
Expand Down

0 comments on commit 57a0abf

Please sign in to comment.