Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential mem-leak from hotstuff #1514

Merged
merged 3 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions consensus/hotstuff/voteaggregator/vote_aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ func (va *VoteAggregator) StoreProposerVote(vote *model.Vote) bool {
return false
}
va.proposerVotes[vote.BlockID] = vote
// update viewToBlockIDSet
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use updateState here, even though that will that also update the viewToBlockIDSet which is used for pruning.

The reason is that the proposer Vote might be invalid, which we don't want to update the state with.

However, the viewToBlockIDSet is safe to update alone, since it's purely used for pruning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proposer Vote might be invalid

Good observation. From the perspective of VoteAggregator, the proposer vote is still unchecked. A double voting attempt cannot be attributed without checking the vote first.

I was thinking that the EventHander always validates the proposer vote first:

_, err := v.ValidateVote(proposal.ProposerVote(), block)

Nevertheless, this happens outside of the VoteAggregator and we should probably not rely on external components for critical validity checks.

blockIDSet, exists := va.viewToBlockIDSet[vote.View]
if exists {
blockIDSet[vote.BlockID] = struct{}{}
} else {
blockIDSet = make(map[flow.Identifier]struct{})
blockIDSet[vote.BlockID] = struct{}{}
va.viewToBlockIDSet[vote.View] = blockIDSet
}
return true
}

Expand Down
21 changes: 21 additions & 0 deletions consensus/hotstuff/voteaggregator/vote_aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,27 @@ func (as *AggregatorSuite) TestNonePruneAfterBlock() {
require.Equal(as.T(), 3, votingStatusLen)
}

// receive the block for view 2,3,4,5
// prune by view 5, should be all pruned
func (as *AggregatorSuite) TestPruneAll() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case was able to reproduce the issue. Without the fix, this test will fail

pruneView := uint64(5)
for i := 2; i <= 5; i++ {
view := uint64(i)
bp := newMockBlock(as, view, as.participants[i].NodeID)
as.aggregator.StoreProposerVote(bp.ProposerVote())
}

require.Len(as.T(), as.aggregator.proposerVotes, 4)

as.aggregator.PruneByView(pruneView)
// proposerVotes should be all pruned, otherwise, there is memory leak
require.Len(as.T(), as.aggregator.proposerVotes, 0)
require.Len(as.T(), as.aggregator.blockIDToVotingStatus, 0)
require.Len(as.T(), as.aggregator.createdQC, 0)
require.Len(as.T(), as.aggregator.viewToVoteID, 0)
require.Len(as.T(), as.aggregator.viewToBlockIDSet, 0)
}

// RANDOM BEACON
// if there are 7 nodes, it requires 4 votes for random beacon,
// and receives the block from proposer who has 80% stake,
Expand Down