github.com/koko1123/flow-go-1@v0.29.6/consensus/hotstuff/pacemaker/timeout/config_test.go (about)

     1  package timeout
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestConstructor(t *testing.T) {
    12  	c, err := NewConfig(2200*time.Millisecond, 1200*time.Millisecond, 0.73, 1.5, 0.85, time.Second)
    13  	require.NoError(t, err)
    14  	require.Equal(t, float64(2200), c.ReplicaTimeout)
    15  	require.Equal(t, float64(1200), c.MinReplicaTimeout)
    16  	require.Equal(t, float64(0.73), c.VoteAggregationTimeoutFraction)
    17  	require.Equal(t, float64(1.5), c.TimeoutIncrease)
    18  	require.Equal(t, float64(0.85), c.TimeoutDecrease)
    19  	require.Equal(t, float64(1000), c.BlockRateDelayMS.Load())
    20  
    21  	// should not allow startReplicaTimeout < minReplicaTimeout
    22  	_, err = NewConfig(800*time.Millisecond, 1200*time.Millisecond, 0.73, 1.5, 0.85, time.Second)
    23  	require.Error(t, err)
    24  
    25  	// should not allow negative minReplicaTimeout
    26  	c, err = NewConfig(2200*time.Millisecond, -1200*time.Millisecond, 0.73, 1.5, 0.85, time.Second)
    27  	require.Error(t, err)
    28  
    29  	// should not allow voteAggregationTimeoutFraction to be 0 or larger than 1
    30  	c, err = NewConfig(2200*time.Millisecond, 1200*time.Millisecond, 0, 1.5, 0.85, time.Second)
    31  	require.Error(t, err)
    32  	c, err = NewConfig(2200*time.Millisecond, 1200*time.Millisecond, 1.00001, 1.5, 0.85, time.Second)
    33  	require.Error(t, err)
    34  
    35  	// should not allow timeoutIncrease to be 1.0 or smaller
    36  	c, err = NewConfig(2200*time.Millisecond, 1200*time.Millisecond, 0.73, 1.0, 0.85, time.Second)
    37  	require.Error(t, err)
    38  
    39  	// should not allow timeoutDecrease to be zero or 1.0
    40  	c, err = NewConfig(2200*time.Millisecond, 1200*time.Millisecond, 0.73, 1.5, 0, time.Second)
    41  	require.Error(t, err)
    42  	c, err = NewConfig(2200*time.Millisecond, 1200*time.Millisecond, 0.73, 1.5, 1, time.Second)
    43  	require.Error(t, err)
    44  
    45  	// should not allow blockRateDelay to be zero negative
    46  	c, err = NewConfig(2200*time.Millisecond, 1200*time.Millisecond, 0.73, 1.5, 0.85, -1*time.Nanosecond)
    47  	require.Error(t, err)
    48  }
    49  
    50  func TestDefaultConfig(t *testing.T) {
    51  	c := NewDefaultConfig()
    52  
    53  	require.Equal(t, float64(60000), c.ReplicaTimeout)
    54  	require.Equal(t, float64(2000), c.MinReplicaTimeout)
    55  	require.Equal(t, float64(0.5), c.VoteAggregationTimeoutFraction)
    56  	require.Equal(t, float64(2.0), c.TimeoutIncrease)
    57  	require.True(t, math.Abs(1/math.Sqrt(2)-c.TimeoutDecrease) < 1e-15) // need to allow for some numerical error
    58  	require.Equal(t, float64(0), c.BlockRateDelayMS.Load())
    59  }
    60  
    61  // TestStandardVoteAggregationTimeoutFraction tests the computation of the standard
    62  // value for `VoteAggregationTimeoutFraction`.
    63  func TestStandardVoteAggregationTimeoutFraction(t *testing.T) {
    64  	// test numerical computation for one specific parameter setting
    65  	f := StandardVoteAggregationTimeoutFraction(1200*time.Millisecond, 500*time.Millisecond)
    66  	require.Equal(t, (0.5*1200.0+500.0)/(1200.0+500.0), f)
    67  
    68  	// for no blockRateDelay, the standard value should be 0.5
    69  	f = StandardVoteAggregationTimeoutFraction(123456*time.Millisecond, 0)
    70  	require.Equal(t, 0.5, f)
    71  
    72  	// for _very_ large blockRateDelay, the standard value should converge to one
    73  	f = StandardVoteAggregationTimeoutFraction(123456*time.Millisecond, 10000*time.Hour)
    74  	require.True(t, f <= 1.0)
    75  	require.True(t, math.Abs(f-1.0) < 1e-5)
    76  }
    77  
    78  func TestStandardTimeoutDecreaseFactor(t *testing.T) {
    79  	timeoutIncreaseFactor := 2.0
    80  
    81  	offlineFraction := 1.0 / 3.0
    82  	f := StandardTimeoutDecreaseFactor(offlineFraction, timeoutIncreaseFactor)
    83  	expected := math.Pow(timeoutIncreaseFactor, offlineFraction) * math.Pow(f, 1.0-offlineFraction)
    84  	numericalError := math.Abs(expected - 1.0)
    85  	require.True(t, numericalError < 1e-15)
    86  
    87  	offlineFraction = 0.2
    88  	f = StandardTimeoutDecreaseFactor(offlineFraction, timeoutIncreaseFactor)
    89  	expected = math.Pow(timeoutIncreaseFactor, offlineFraction) * math.Pow(f, 1.0-offlineFraction)
    90  	numericalError = math.Abs(expected - 1.0)
    91  	require.True(t, numericalError < 1e-15)
    92  }