Skip to content

Commit 3fffbac

Browse files
authored
Change trust threshold (#46)
Signed-off-by: Naohiro Yoshida <[email protected]>
1 parent dd09549 commit 3fffbac

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

module/validator_set.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ func (v Validators) Contains(other Validators) bool {
2323
}
2424
}
2525
}
26-
required := ceilDiv(len(v), 3)
26+
required := v.threshold()
2727
return count >= required
2828
}
2929

30+
func (v Validators) threshold() int {
31+
return len(v) - ceilDiv(len(v)*2, 3) + 1
32+
}
33+
3034
func ceilDiv(x, y int) int {
3135
if y == 0 {
3236
return 0

module/validator_set_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (ts *ValidatorSetTestSuite) TestCheckpoint() {
9090
ts.Equal(int(validator.Checkpoint(9)), 99)
9191
}
9292

93-
func (ts *ValidatorSetTestSuite) TestValidator() {
93+
func (ts *ValidatorSetTestSuite) TestTrustValidator() {
9494
trusted := Validators([][]byte{{1}, {2}, {3}, {4}, {5}})
9595
ts.True(trusted.Contains([][]byte{{1}, {2}, {3}, {4}, {5}}))
9696
ts.True(trusted.Contains([][]byte{{1}, {2}, {3}, {4}, {5}, {10}, {11}, {12}, {13}, {14}}))
@@ -104,5 +104,17 @@ func (ts *ValidatorSetTestSuite) TestValidator() {
104104
ts.False(trusted.Contains([][]byte{{1}, {10}, {11}, {12}, {13}, {14}}))
105105
ts.False(trusted.Contains([][]byte{}))
106106
ts.False(trusted.Contains([][]byte{{10}, {11}, {12}, {13}, {14}}))
107+
}
107108

109+
func (ts *ValidatorSetTestSuite) TestThreshold() {
110+
ts.Equal(1, Validators(make([][]byte, 1)).threshold())
111+
ts.Equal(1, Validators(make([][]byte, 2)).threshold())
112+
ts.Equal(2, Validators(make([][]byte, 3)).threshold())
113+
ts.Equal(2, Validators(make([][]byte, 4)).threshold())
114+
ts.Equal(2, Validators(make([][]byte, 5)).threshold())
115+
ts.Equal(3, Validators(make([][]byte, 6)).threshold())
116+
ts.Equal(3, Validators(make([][]byte, 7)).threshold())
117+
ts.Equal(3, Validators(make([][]byte, 8)).threshold())
118+
ts.Equal(4, Validators(make([][]byte, 9)).threshold())
119+
ts.Equal(8, Validators(make([][]byte, 21)).threshold())
108120
}

0 commit comments

Comments
 (0)