Skip to content

Commit a4a4e28

Browse files
authored
chore(pos): add a 🐳 distribution test case (#1163)
1 parent 55714d8 commit a4a4e28

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

pos/sched_test.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,22 @@ func TestScheduler_IsScheduled(t *testing.T) {
7070
}
7171

7272
func TestScheduler_Distribution(t *testing.T) {
73+
// Reduce tolerance my increasing iterations to achieve a higher level of accuracy
74+
// e.g., 1 million usually gets all tolerances down to about 2% (i.e., 0.02)
7375
iterations := 100_000
74-
tolerance := 0.04 // 4% tolerance, reduce by increasing iterations, 2% works for 1million
75-
7676
type stakeFunc func(index int, acc thor.Address) *big.Int
7777

7878
// pseudo-random number generator
7979
randReader := mathrand.New(mathrand.NewSource(412342)) //nolint:gosec
8080

8181
var testCases = []struct {
82-
name string
83-
stakes stakeFunc
82+
name string
83+
stakes stakeFunc
84+
tolerance float64
8485
}{
8586
{
86-
name: "some_big_some_small",
87+
name: "some_big_some_small",
88+
tolerance: 0.03,
8789
stakes: func(index int, acc thor.Address) *big.Int {
8890
if index%2 == 0 {
8991
return big.NewInt(2000)
@@ -92,13 +94,15 @@ func TestScheduler_Distribution(t *testing.T) {
9294
},
9395
},
9496
{
95-
name: "all_same",
97+
name: "all_same",
98+
tolerance: 0.03,
9699
stakes: func(index int, acc thor.Address) *big.Int {
97100
return big.NewInt(1000)
98101
},
99102
},
100103
{
101-
name: "pseudo_random_weight",
104+
name: "pseudo_random_weight",
105+
tolerance: 0.04,
102106
stakes: func(index int, acc thor.Address) *big.Int {
103107
eth := big.NewInt(1)
104108
millionEth := new(big.Int).Mul(big.NewInt(1e6), eth)
@@ -115,11 +119,22 @@ func TestScheduler_Distribution(t *testing.T) {
115119
},
116120
},
117121
{
118-
name: "increasing",
122+
name: "increasing",
123+
tolerance: 0.02,
119124
stakes: func(index int, acc thor.Address) *big.Int {
120125
return new(big.Int).SetInt64((int64(index) + 1) * 1000)
121126
},
122127
},
128+
{
129+
name: "some whales",
130+
tolerance: 0.07, // less than 0.01 with 10m iterations
131+
stakes: func(index int, acc thor.Address) *big.Int {
132+
if index < 3 {
133+
return big.NewInt(1200)
134+
}
135+
return big.NewInt(50)
136+
},
137+
},
123138
}
124139

125140
for _, tc := range testCases {
@@ -163,7 +178,7 @@ func TestScheduler_Distribution(t *testing.T) {
163178
expectedCountFloat := new(big.Float).Mul(weight, big.NewFloat(float64(iterations)))
164179
expectedCount, _ := expectedCountFloat.Int64()
165180

166-
diff := float64(expectedCount) * tolerance
181+
diff := float64(expectedCount) * tc.tolerance
167182
diffPercent := (float64(expectedCount) - float64(count)) / float64(expectedCount)
168183
addr := thor.BytesToAddress(id[:])
169184

0 commit comments

Comments
 (0)