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

QBFT: prevent maximum request timeout overflow #1665

Merged
Merged
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
29 changes: 24 additions & 5 deletions consensus/istanbul/qbft/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,32 @@ func (c *core) newRoundChangeTimer() {
// set timeout based on the round number
baseTimeout := time.Duration(c.config.GetConfig(c.current.Sequence()).RequestTimeout) * time.Millisecond
round := c.current.Round().Uint64()

timeout := baseTimeout * time.Duration(math.Pow(2, float64(round)))

maxRequestTimeout := time.Duration(c.config.GetConfig(c.current.Sequence()).MaxRequestTimeoutSeconds) * time.Second

if maxRequestTimeout > time.Duration(0) && timeout > maxRequestTimeout {
timeout = maxRequestTimeout
// If the upper limit of the request timeout is capped by small maxRequestTimeout, round can be a quite large number,
// which leads to float64 overflow, making its value negative or zero forever after some point.
// In this case we cannot simply use math.Pow and have to implement a safeguard on our own, at the cost of performance (which is not important in this case).
var timeout time.Duration
if maxRequestTimeout > time.Duration(0) {
timeout = baseTimeout
for i := uint64(0); i < round; i++ {
timeout = timeout * 2
if timeout > maxRequestTimeout {
timeout = maxRequestTimeout
break
}
}
// prevent log storm when unexpected overflow happens
if timeout < baseTimeout {
c.currentLogger(true, nil).Error("QBFT: Possible request timeout overflow detected, setting timeout value to maxRequestTimeout",
"timeout", timeout.Seconds(),
"max_request_timeout", maxRequestTimeout.Seconds(),
)
timeout = maxRequestTimeout
}
} else {
// effectively impossible to observe overflow happen when maxRequestTimeout is disabled
timeout = baseTimeout * time.Duration(math.Pow(2, float64(round)))
}

c.currentLogger(true, nil).Trace("QBFT: start new ROUND-CHANGE timer", "timeout", timeout.Seconds())
Expand Down
Loading