Skip to content

Commit

Permalink
Change trust threshold (#46)
Browse files Browse the repository at this point in the history
Signed-off-by: Naohiro Yoshida <[email protected]>
  • Loading branch information
yoshidan authored Sep 27, 2024
1 parent dd09549 commit 3fffbac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 5 additions & 1 deletion module/validator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion module/validator_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}))
Expand All @@ -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())
}

0 comments on commit 3fffbac

Please sign in to comment.