Skip to content

Commit a2d75f5

Browse files
authored
Unnecessary Node Count Parameter in Blacklist Method (#287)
* unnecessary node cunt variable * add test blacklist
1 parent 35bd7a2 commit a2d75f5

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

blacklist.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,12 @@ func (bl *Blacklist) FromBytes(buff []byte) error {
428428
return nil
429429
}
430430

431-
func (bl *Blacklist) VerifyProposedBlacklist(candidateBlacklist Blacklist, nodeCount int, round uint64) error {
432-
if candidateBlacklist.NodeCount != uint16(nodeCount) {
433-
return fmt.Errorf("%s, expected %d, got %d", errBlacklistInvalidNodeCount, nodeCount, candidateBlacklist.NodeCount)
431+
func (bl *Blacklist) VerifyProposedBlacklist(candidateBlacklist Blacklist, round uint64) error {
432+
if candidateBlacklist.NodeCount != bl.NodeCount {
433+
return fmt.Errorf("%s, expected %d, got %d", errBlacklistInvalidNodeCount, bl.NodeCount, candidateBlacklist.NodeCount)
434434
}
435435
// 1) First thing we check that the updates even make sense.
436-
if err := bl.verifyBlacklistUpdates(candidateBlacklist.Updates, nodeCount); err != nil {
436+
if err := bl.verifyBlacklistUpdates(candidateBlacklist.Updates); err != nil {
437437
return fmt.Errorf("%s: %w", errBlacklistInvalidUpdates, err)
438438
}
439439
updates := candidateBlacklist.Updates
@@ -447,15 +447,15 @@ func (bl *Blacklist) VerifyProposedBlacklist(candidateBlacklist Blacklist, nodeC
447447
return nil
448448
}
449449

450-
func (bl *Blacklist) verifyBlacklistUpdates(updates []BlacklistUpdate, nodeCount int) error {
450+
func (bl *Blacklist) verifyBlacklistUpdates(updates []BlacklistUpdate) error {
451451
seen := make(map[uint16]struct{})
452-
if len(updates) > nodeCount {
453-
return fmt.Errorf("%w: %d, only %d nodes exist", errBlacklistTooManyUpdates, len(updates), nodeCount)
452+
if len(updates) > int(bl.NodeCount) {
453+
return fmt.Errorf("%w: %d, only %d nodes exist", errBlacklistTooManyUpdates, len(updates), bl.NodeCount)
454454
}
455455
for _, update := range updates {
456-
if int(update.NodeIndex) >= nodeCount {
456+
if update.NodeIndex >= bl.NodeCount {
457457
return fmt.Errorf("%w: %d, needs to be in [%d, %d]",
458-
errBlacklistInvalidNodeIndex, update.NodeIndex, 0, nodeCount-1)
458+
errBlacklistInvalidNodeIndex, update.NodeIndex, 0, bl.NodeCount-1)
459459
}
460460

461461
if _, exists := seen[update.NodeIndex]; exists {

blacklist_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestBlacklistVerifyProposedBlacklist(t *testing.T) {
9191
},
9292
} {
9393
t.Run(testCase.name, func(t *testing.T) {
94-
err := testCase.blacklist.VerifyProposedBlacklist(testCase.proposedBlacklist, testCase.nodeCount, testCase.round)
94+
err := testCase.blacklist.VerifyProposedBlacklist(testCase.proposedBlacklist, testCase.round)
9595
require.ErrorContains(t, err, testCase.expectedErr.Error())
9696
})
9797
}
@@ -581,6 +581,10 @@ func TestUpdateBytesEqualsLen(t *testing.T) {
581581
}
582582

583583
func TestVerifyBlacklistUpdates(t *testing.T) {
584+
testBlacklist := Blacklist{
585+
NodeCount: 4,
586+
}
587+
584588
for _, testCase := range []struct {
585589
name string
586590
Blacklist Blacklist
@@ -604,13 +608,15 @@ func TestVerifyBlacklistUpdates(t *testing.T) {
604608
{Type: 3, NodeIndex: 1},
605609
},
606610
expectedErr: errBlacklistInvalidUpdateType,
611+
Blacklist: testBlacklist,
607612
},
608613
{
609614
name: "invalid index",
610615
updates: []BlacklistUpdate{
611616
{Type: BlacklistOpType_NodeRedeemed, NodeIndex: 4},
612617
},
613618
expectedErr: errBlacklistInvalidNodeIndex,
619+
Blacklist: testBlacklist,
614620
},
615621
{
616622
name: "double vote",
@@ -620,6 +626,7 @@ func TestVerifyBlacklistUpdates(t *testing.T) {
620626
{Type: BlacklistOpType_NodeSuspected, NodeIndex: 3},
621627
},
622628
expectedErr: errBlacklistNodeIndexAlreadyUpdated,
629+
Blacklist: testBlacklist,
623630
},
624631
{
625632
name: "already blacklisted",
@@ -646,7 +653,7 @@ func TestVerifyBlacklistUpdates(t *testing.T) {
646653
},
647654
} {
648655
t.Run(testCase.name, func(t *testing.T) {
649-
err := testCase.Blacklist.verifyBlacklistUpdates(testCase.updates, 4)
656+
err := testCase.Blacklist.verifyBlacklistUpdates(testCase.updates)
650657
require.ErrorContains(t, err, testCase.expectedErr.Error())
651658
})
652659
}
@@ -797,7 +804,7 @@ func simulateRound(t *testing.T, blrsi blacklistRoundSimulationInput) Blacklist
797804

798805
newBlacklist := prevBlacklist.ApplyUpdates(updates, round)
799806

800-
err := prevBlacklist.VerifyProposedBlacklist(newBlacklist, nodeCount, round)
807+
err := prevBlacklist.VerifyProposedBlacklist(newBlacklist, round)
801808
require.NoError(t, err, "round %d", round)
802809

803810
return newBlacklist

epoch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1938,7 +1938,7 @@ func (e *Epoch) verifyProposalMetadataAndBlacklist(block Block) bool {
19381938
prevBlacklist = prevBlock.Blacklist()
19391939
}
19401940

1941-
if err := prevBlacklist.VerifyProposedBlacklist(block.Blacklist(), len(e.nodes), e.round); err != nil {
1941+
if err := prevBlacklist.VerifyProposedBlacklist(block.Blacklist(), e.round); err != nil {
19421942
e.Logger.Debug("Block contains an invalid blacklist", zap.Error(err))
19431943
return false
19441944
}

0 commit comments

Comments
 (0)