diff --git a/module/validator_set.go b/module/validator_set.go index ebddaea..fff240b 100644 --- a/module/validator_set.go +++ b/module/validator_set.go @@ -23,10 +23,14 @@ func (v Validators) Contains(other Validators) bool { } } } - required := ceilDiv(len(v), 3) + required := v.threshold() return count >= required } +func (v Validators) threshold() int { + return len(v) - ceilDiv(len(v)*2, 3) + 1 +} + func ceilDiv(x, y int) int { if y == 0 { return 0 diff --git a/module/validator_set_test.go b/module/validator_set_test.go index 0d09b12..555300c 100644 --- a/module/validator_set_test.go +++ b/module/validator_set_test.go @@ -90,7 +90,7 @@ func (ts *ValidatorSetTestSuite) TestCheckpoint() { ts.Equal(int(validator.Checkpoint(9)), 99) } -func (ts *ValidatorSetTestSuite) TestValidator() { +func (ts *ValidatorSetTestSuite) TestTrustValidator() { trusted := Validators([][]byte{{1}, {2}, {3}, {4}, {5}}) ts.True(trusted.Contains([][]byte{{1}, {2}, {3}, {4}, {5}})) ts.True(trusted.Contains([][]byte{{1}, {2}, {3}, {4}, {5}, {10}, {11}, {12}, {13}, {14}})) @@ -104,5 +104,17 @@ func (ts *ValidatorSetTestSuite) TestValidator() { ts.False(trusted.Contains([][]byte{{1}, {10}, {11}, {12}, {13}, {14}})) ts.False(trusted.Contains([][]byte{})) ts.False(trusted.Contains([][]byte{{10}, {11}, {12}, {13}, {14}})) +} +func (ts *ValidatorSetTestSuite) TestThreshold() { + ts.Equal(1, Validators(make([][]byte, 1)).threshold()) + ts.Equal(1, Validators(make([][]byte, 2)).threshold()) + ts.Equal(2, Validators(make([][]byte, 3)).threshold()) + ts.Equal(2, Validators(make([][]byte, 4)).threshold()) + ts.Equal(2, Validators(make([][]byte, 5)).threshold()) + ts.Equal(3, Validators(make([][]byte, 6)).threshold()) + ts.Equal(3, Validators(make([][]byte, 7)).threshold()) + ts.Equal(3, Validators(make([][]byte, 8)).threshold()) + ts.Equal(4, Validators(make([][]byte, 9)).threshold()) + ts.Equal(8, Validators(make([][]byte, 21)).threshold()) }