Browse Source

light: ensure trust level is strictly less than 1 (#6447)

pull/6454/head
Callum Waters 4 years ago
committed by GitHub
parent
commit
811dbe439f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 13 deletions
  1. +2
    -2
      light/verifier.go
  2. +12
    -11
      light/verifier_test.go

+ 2
- 2
light/verifier.go View File

@ -167,10 +167,10 @@ func Verify(
// ValidateTrustLevel checks that trustLevel is within the allowed range [1/3, // ValidateTrustLevel checks that trustLevel is within the allowed range [1/3,
// 1]. If not, it returns an error. 1/3 is the minimum amount of trust needed // 1]. If not, it returns an error. 1/3 is the minimum amount of trust needed
// which does not break the security model.
// which does not break the security model. Must be strictly less than 1.
func ValidateTrustLevel(lvl tmmath.Fraction) error { func ValidateTrustLevel(lvl tmmath.Fraction) error {
if lvl.Numerator*3 < lvl.Denominator || // < 1/3 if lvl.Numerator*3 < lvl.Denominator || // < 1/3
lvl.Numerator > lvl.Denominator || // > 1
lvl.Numerator >= lvl.Denominator || // >= 1
lvl.Denominator == 0 { lvl.Denominator == 0 {
return fmt.Errorf("trustLevel must be within [1/3, 1], given %v", lvl) return fmt.Errorf("trustLevel must be within [1/3, 1], given %v", lvl)
} }


+ 12
- 11
light/verifier_test.go View File

@ -311,25 +311,26 @@ func TestValidateTrustLevel(t *testing.T) {
valid bool valid bool
}{ }{
// valid // valid
0: {tmmath.Fraction{Numerator: 1, Denominator: 1}, true},
1: {tmmath.Fraction{Numerator: 1, Denominator: 3}, true},
2: {tmmath.Fraction{Numerator: 2, Denominator: 3}, true},
3: {tmmath.Fraction{Numerator: 3, Denominator: 3}, true},
4: {tmmath.Fraction{Numerator: 4, Denominator: 5}, true},
0: {tmmath.Fraction{Numerator: 1, Denominator: 3}, true},
1: {tmmath.Fraction{Numerator: 2, Denominator: 3}, true},
2: {tmmath.Fraction{Numerator: 4, Denominator: 5}, true},
3: {tmmath.Fraction{Numerator: 99, Denominator: 100}, true},
// invalid // invalid
4: {tmmath.Fraction{Numerator: 3, Denominator: 3}, false},
5: {tmmath.Fraction{Numerator: 6, Denominator: 5}, false}, 5: {tmmath.Fraction{Numerator: 6, Denominator: 5}, false},
6: {tmmath.Fraction{Numerator: 0, Denominator: 1}, false},
7: {tmmath.Fraction{Numerator: 0, Denominator: 0}, false},
8: {tmmath.Fraction{Numerator: 1, Denominator: 0}, false},
6: {tmmath.Fraction{Numerator: 3, Denominator: 10}, false},
7: {tmmath.Fraction{Numerator: 0, Denominator: 1}, false},
8: {tmmath.Fraction{Numerator: 0, Denominator: 0}, false},
9: {tmmath.Fraction{Numerator: 1, Denominator: 0}, false},
} }
for _, tc := range testCases {
for idx, tc := range testCases {
err := light.ValidateTrustLevel(tc.lvl) err := light.ValidateTrustLevel(tc.lvl)
if !tc.valid { if !tc.valid {
assert.Error(t, err)
assert.Error(t, err, idx)
} else { } else {
assert.NoError(t, err)
assert.NoError(t, err, idx)
} }
} }
} }

Loading…
Cancel
Save