Skip to content

Commit

Permalink
dbft: adjust timer setting by the block processing time
Browse files Browse the repository at this point in the history
Make block timing more accurate under load. BlockIndex is needed to
distinguish between consensus-produced block and the one added via regular
block addition mechanism.

See neo-project/neo#1959 and neo-project/neo#1960.
  • Loading branch information
roman-khimov committed Sep 25, 2020
1 parent 4c0e262 commit c641e01
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func (d *DBFT) checkCommit() {
return
}

d.lastBlockIndex = d.BlockIndex
d.lastBlockTime = d.Timer.Now()
d.block = d.CreateBlock()
hash := d.block.Hash()

Expand Down
4 changes: 4 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dbft

import (
"math/rand"
"time"

"github.com/nspcc-dev/dbft/block"
"github.com/nspcc-dev/dbft/crypto"
Expand Down Expand Up @@ -63,6 +64,9 @@ type Context struct {
// LastSeenMessage array stores the height of the last seen message, for each validator.
// if this node never heard from validator i, LastSeenMessage[i] will be -1.
LastSeenMessage []*timer.HV

lastBlockTime time.Time
lastBlockIndex uint32
}

// N returns total number of validators.
Expand Down
8 changes: 8 additions & 0 deletions dbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ func (d *DBFT) InitializeConsensus(view byte) {
} else {
timeout = d.SecondsPerBlock << (d.ViewNumber + 1)
}
if d.lastBlockIndex+1 == d.BlockIndex {
var ts = d.Timer.Now()
var diff = ts.Sub(d.lastBlockTime)
timeout -= diff
if timeout < 0 {
timeout = 0
}
}
d.changeTimer(timeout)
}

Expand Down

0 comments on commit c641e01

Please sign in to comment.