Skip to content

Commit

Permalink
Fix incorrect processing of VerificationFreq parameter (livepeer#2740)
Browse files Browse the repository at this point in the history
* fix semantics of shouldSkipVerification

* go fmt

* Fix test to align with the meaning of verificationFreq parameter
  • Loading branch information
cyberj0g authored and eliteprox committed Feb 3, 2023
1 parent f1373af commit 988625f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion server/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func (bsm *BroadcastSessionsManager) shouldSkipVerification(sessions []*Broadcas
if !includesSession(sessions, bsm.verifiedSession) {
return false
}
return common.RandomUintUnder(bsm.VerificationFreq) == 0
return common.RandomUintUnder(bsm.VerificationFreq) != 0
}

func NewSessionManager(ctx context.Context, node *core.LivepeerNode, params *core.StreamParameters, sel BroadcastSessionsSelectorFactory) *BroadcastSessionsManager {
Expand Down
16 changes: 8 additions & 8 deletions server/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1796,8 +1796,9 @@ func TestVerifcationDoesntRunWhenNoVerifiedSessionPresent(t *testing.T) {
}

func TestVerifcationRunsBasedOnVerificationFrequency(t *testing.T) {
verificationFreq := 5
b := BroadcastSessionsManager{
VerificationFreq: 5, // Run approximately 1 in 5 times
VerificationFreq: uint(verificationFreq), // Verification should run approximately 1 in 5 times
}

var verifiedSession = &BroadcastSession{
Expand All @@ -1806,15 +1807,14 @@ func TestVerifcationRunsBasedOnVerificationFrequency(t *testing.T) {

b.verifiedSession = verifiedSession

var shouldRunCount int
for i := 0; i < 10000; i++ {
var shouldSkipCount int
numTests := 10000
for i := 0; i < numTests; i++ {
if b.shouldSkipVerification([]*BroadcastSession{verifiedSession}) {
shouldRunCount++
shouldSkipCount++
}
}

// Difficult to test a random function, so we just check that it's in
// the ball park of what we'd expect (1 in 5 of 10,000 tries)
require.Greater(t, shouldRunCount, 1000)
require.Less(t, shouldRunCount, 3000)
require.Greater(t, float32(shouldSkipCount), float32(numTests)*(1-2/float32(verificationFreq)))
require.Less(t, float32(shouldSkipCount), float32(numTests)*(1-0.5/float32(verificationFreq)))
}
2 changes: 1 addition & 1 deletion server/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,7 @@ func TestPush_MultipartReturnMultiSession(t *testing.T) {
sess3.OrchestratorScore = common.Score_Untrusted

bsm := bsmWithSessListExt([]*BroadcastSession{sess1}, []*BroadcastSession{sess3, sess2}, false)
bsm.VerificationFreq = math.MaxInt
bsm.VerificationFreq = 1
assert.Equal(0, bsm.untrustedPool.sus.count)
// hack: stop pool from refreshing
bsm.untrustedPool.refreshing = true
Expand Down

0 comments on commit 988625f

Please sign in to comment.