mirror of
https://github.com/QuilibriumNetwork/ceremonyclient.git
synced 2026-02-22 02:47:26 +08:00
* wip: conversion of hotstuff from flow into Q-oriented model * bulk of tests * remaining non-integration tests * add integration test, adjust log interface, small tweaks * further adjustments, restore full pacemaker shape * add component lifecycle management+supervisor * further refinements * resolve timeout hanging * mostly finalized state for consensus * bulk of engine swap out * lifecycle-ify most types * wiring nearly complete, missing needed hooks for proposals * plugged in, vetting message validation paths * global consensus, plugged in and verified * app shard now wired in too * do not decode empty keys.yml (#456) * remove obsolete engine.maxFrames config parameter (#454) * default to Info log level unless debug is enabled (#453) * respect config's "logging" section params, remove obsolete single-file logging (#452) * Trivial code cleanup aiming to reduce Go compiler warnings (#451) * simplify range traversal * simplify channel read for single select case * delete rand.Seed() deprecated in Go 1.20 and no-op as of Go 1.24 * simplify range traversal * simplify channel read for single select case * remove redundant type from array * simplify range traversal * simplify channel read for single select case * RC slate * finalize 2.1.0.5 * Update comments in StrictMonotonicCounter Fix comment formatting and clarify description. --------- Co-authored-by: Black Swan <3999712+blacks1ne@users.noreply.github.com>
158 lines
4.8 KiB
Go
158 lines
4.8 KiB
Go
package timeout
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const (
|
|
minRepTimeout float64 = 100 // Milliseconds
|
|
maxRepTimeout float64 = 10000 // Milliseconds
|
|
timeoutAdjustmentFactor float64 = 1.5 // timeout duration adjustment factor
|
|
happyPathMaxRoundFailures uint64 = 3 // number of failed rounds before increasing timeouts
|
|
)
|
|
|
|
func initTimeoutController(t *testing.T) *Controller {
|
|
tc, err := NewConfig(
|
|
time.Duration(minRepTimeout*1e6),
|
|
time.Duration(maxRepTimeout*1e6),
|
|
timeoutAdjustmentFactor,
|
|
happyPathMaxRoundFailures,
|
|
time.Duration(maxRepTimeout*1e6),
|
|
)
|
|
if err != nil {
|
|
t.Fail()
|
|
}
|
|
return NewController(tc)
|
|
}
|
|
|
|
// Test_TimeoutInitialization timeouts are initialized and reported properly
|
|
func Test_TimeoutInitialization(t *testing.T) {
|
|
tc := initTimeoutController(t)
|
|
assert.Equal(t, tc.replicaTimeout(), minRepTimeout)
|
|
|
|
// verify that initially returned timeout channel is closed and `nil` is
|
|
// returned as `TimerInfo`
|
|
select {
|
|
case <-tc.Channel():
|
|
break
|
|
default:
|
|
assert.Fail(t, "timeout channel did not return")
|
|
}
|
|
tc.Channel()
|
|
}
|
|
|
|
// Test_TimeoutIncrease verifies that timeout increases exponentially
|
|
func Test_TimeoutIncrease(t *testing.T) {
|
|
tc := initTimeoutController(t)
|
|
|
|
// advance failed rounds beyond `happyPathMaxRoundFailures`;
|
|
for r := uint64(0); r < happyPathMaxRoundFailures; r++ {
|
|
tc.OnTimeout()
|
|
}
|
|
|
|
for r := 1; r <= 10; r += 1 {
|
|
tc.OnTimeout()
|
|
assert.Equal(t,
|
|
tc.replicaTimeout(),
|
|
minRepTimeout*math.Pow(timeoutAdjustmentFactor, float64(r)),
|
|
)
|
|
}
|
|
}
|
|
|
|
// Test_TimeoutDecrease verifies that timeout decreases exponentially
|
|
func Test_TimeoutDecrease(t *testing.T) {
|
|
tc := initTimeoutController(t)
|
|
|
|
// failed rounds counter
|
|
r := uint64(0)
|
|
|
|
// advance failed rounds beyond `happyPathMaxRoundFailures`; subsequent
|
|
// progress should reduce timeout again
|
|
for ; r <= happyPathMaxRoundFailures*2; r++ {
|
|
tc.OnTimeout()
|
|
}
|
|
for ; r > happyPathMaxRoundFailures; r-- {
|
|
tc.OnProgressBeforeTimeout()
|
|
assert.Equal(t,
|
|
tc.replicaTimeout(),
|
|
minRepTimeout*math.Pow(
|
|
timeoutAdjustmentFactor,
|
|
float64(r-1-happyPathMaxRoundFailures),
|
|
),
|
|
)
|
|
}
|
|
}
|
|
|
|
// Test_MinCutoff verifies that timeout does not decrease below minRepTimeout
|
|
func Test_MinCutoff(t *testing.T) {
|
|
tc := initTimeoutController(t)
|
|
|
|
for r := uint64(0); r < happyPathMaxRoundFailures; r++ {
|
|
tc.OnTimeout() // replica timeout doesn't increase since r < happyPathMaxRoundFailures.
|
|
}
|
|
|
|
tc.OnTimeout() // replica timeout increases 100 -> 3/2 * 100 = 150
|
|
tc.OnTimeout() // replica timeout increases 150 -> 3/2 * 150 = 225
|
|
tc.OnProgressBeforeTimeout() // replica timeout decreases 225 -> 180 * 2/3 = 150
|
|
tc.OnProgressBeforeTimeout() // replica timeout decreases 150 -> 153 * 2/3 = 100
|
|
tc.OnProgressBeforeTimeout() // replica timeout decreases 100 -> 100 * 2/3 = max(66.6, 100) = 100
|
|
|
|
tc.OnProgressBeforeTimeout()
|
|
assert.Equal(t, tc.replicaTimeout(), minRepTimeout)
|
|
}
|
|
|
|
// Test_MaxCutoff verifies that timeout does not increase beyond timeout cap
|
|
func Test_MaxCutoff(t *testing.T) {
|
|
tc := initTimeoutController(t)
|
|
|
|
// we update the following two values here in the test, which is a naive
|
|
// reference implementation
|
|
unboundedReferenceTimeout := minRepTimeout
|
|
r := -1 * int64(happyPathMaxRoundFailures) // only start increasing `unboundedReferenceTimeout` when this becomes positive
|
|
|
|
// add timeouts until our `unboundedReferenceTimeout` exceeds the limit
|
|
for {
|
|
tc.OnTimeout()
|
|
if r++; r > 0 {
|
|
unboundedReferenceTimeout *= timeoutAdjustmentFactor
|
|
}
|
|
if unboundedReferenceTimeout > maxRepTimeout {
|
|
assert.True(t, tc.replicaTimeout() <= maxRepTimeout)
|
|
return // end of test
|
|
}
|
|
}
|
|
}
|
|
|
|
// Test_CombinedIncreaseDecreaseDynamics verifies that timeout increases and decreases
|
|
// work as expected in combination
|
|
func Test_CombinedIncreaseDecreaseDynamics(t *testing.T) {
|
|
increase, decrease := true, false
|
|
testDynamicSequence := func(seq []bool) {
|
|
tc := initTimeoutController(t)
|
|
tc.cfg.HappyPathMaxRoundFailures = 0 // set happy path rounds to zero to simplify calculation
|
|
numberIncreases, numberDecreases := 0, 0
|
|
for _, increase := range seq {
|
|
if increase {
|
|
numberIncreases += 1
|
|
tc.OnTimeout()
|
|
} else {
|
|
numberDecreases += 1
|
|
tc.OnProgressBeforeTimeout()
|
|
}
|
|
}
|
|
|
|
expectedRepTimeout := minRepTimeout * math.Pow(timeoutAdjustmentFactor, float64(numberIncreases-numberDecreases))
|
|
numericalError := math.Abs(expectedRepTimeout - tc.replicaTimeout())
|
|
require.LessOrEqual(t, numericalError, 1.0) // at most one millisecond numerical error
|
|
}
|
|
|
|
testDynamicSequence([]bool{increase, increase, increase, decrease, decrease, decrease})
|
|
testDynamicSequence([]bool{increase, decrease, increase, decrease, increase, decrease})
|
|
testDynamicSequence([]bool{increase, increase, increase, increase, increase, decrease})
|
|
}
|