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

context: add simple rtt estimator #140

Merged
merged 1 commit into from
Jan 10, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Behaviour changes:
Improvements:
* timer adjustment for most of the consensus time, more accurate block
intervals (#55)
* timer adjustment for network roundtrip time (#140)

Bugs fixed:
* inappropriate log on attempt to construct Commit for anti-MEV enabled WatchOnly
Expand Down
4 changes: 4 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ type Context[H Hash] struct {
lastBlockTime time.Time // Wall clock time of when we started (as in PrepareRequest) creating the last block (used for timer adjustments).
lastBlockIndex uint32
lastBlockView byte

prepareSentTime time.Time
rttEstimates rtt
}

// N returns total number of validators.
Expand Down Expand Up @@ -236,6 +239,7 @@ func (c *Context[H]) PreBlock() PreBlock[H] {

func (c *Context[H]) reset(view byte, ts uint64) {
c.MyIndex = -1
c.prepareSentTime = time.Time{}
c.lastBlockTimestamp = ts

if view == 0 {
Expand Down
5 changes: 5 additions & 0 deletions dbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (d *DBFT[H]) initializeConsensus(view byte, ts uint64) {
var ts = d.Timer.Now()
var diff = ts.Sub(d.lastBlockTime)
timeout -= diff
timeout -= d.rttEstimates.avg / 2
timeout = max(0, timeout)
}
d.changeTimer(timeout)
Expand Down Expand Up @@ -482,6 +483,10 @@ func (d *DBFT[H]) onPrepareResponse(msg ConsensusPayload[H]) {
}
}

if d.IsPrimary() && !d.prepareSentTime.IsZero() && !d.recovering {
d.rttEstimates.addTime(time.Since(d.prepareSentTime))
}

d.extendTimer(2)

if !d.Context.WatchOnly() && !d.CommitSent() && (!d.isAntiMEVExtensionEnabled() || !d.PreCommitSent()) && d.RequestSentOrReceived() {
Expand Down
26 changes: 26 additions & 0 deletions rtt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dbft

import (
"time"
)

const rttLength = 7 * 10 // 10 rounds with 7 nodes

type rtt struct {
times [rttLength]time.Duration
idx int
avg time.Duration
}

func (r *rtt) addTime(t time.Duration) {
var old = r.times[r.idx]

if old != 0 {
t = min(t, 2*old) // Too long delays should be normalized, we don't want to overshoot.
}

Check warning on line 20 in rtt.go

View check run for this annotation

Codecov / codecov/patch

rtt.go#L19-L20

Added lines #L19 - L20 were not covered by tests

r.avg = r.avg + (t-old)/time.Duration(len(r.times))
r.avg = max(0, r.avg) // Can't be less than zero.
r.times[r.idx] = t
r.idx = (r.idx + 1) % len(r.times)
}
2 changes: 2 additions & 0 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func (d *DBFT[H]) sendPrepareRequest() {
d.PreparationPayloads[d.MyIndex] = msg
d.broadcast(msg)

d.prepareSentTime = d.Timer.Now()

delay := d.SecondsPerBlock << (d.ViewNumber + 1)
if d.ViewNumber == 0 {
delay -= d.SecondsPerBlock
Expand Down
Loading