Skip to content

Commit

Permalink
fix: Fix bug on gossip denial
Browse files Browse the repository at this point in the history
  • Loading branch information
OnedgeLee committed Nov 24, 2023
1 parent f043a08 commit 64549cf
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions Libplanet.Net/Consensus/GossipConsensusMessageCommunicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void PublishMessage(ConsensusMsg message)
public void OnStartHeight(long height)
{
_height = height;
_peerCatchupRounds.Clear();
Gossip.ClearDenySet();
}

Expand All @@ -86,6 +87,7 @@ private void ValidateMessageToReceive(Message message)
{
if (message.Content is ConsensusVoteMsg voteMsg)
{
FilterDifferentHeightVote(voteMsg);
FilterHigherRoundVoteSpam(voteMsg, message.Remote);
}
}
Expand All @@ -98,10 +100,33 @@ private void ValidateMessageToReceive(Message message)
/// <param name="content"><see cref="MessageContent"/> to validate.</param>
private void ValidateMessageToSend(MessageContent content)
{
if (content is ConsensusVoteMsg voteMsg && voteMsg.Round > _round)
if (content is ConsensusVoteMsg voteMsg)
{
if (voteMsg.Height != _height)
{
throw new InvalidConsensusMessageException(
$"Cannot send vote of height different from context's", voteMsg);
}

if (voteMsg.Round > _round)
{
throw new InvalidConsensusMessageException(
$"Cannot send vote of round higher than context's", voteMsg);
}
}
}

/// <summary>
/// Filter logic for different height <see cref="ConsensusVoteMsg"/>s.
/// </summary>
/// <param name="voteMsg"><see cref="ConsensusVoteMsg"/> to filter.</param>
private void FilterDifferentHeightVote(ConsensusVoteMsg voteMsg)
{
if (voteMsg.Height != _height)
{
throw new InvalidConsensusMessageException(
$"Cannot send vote of round higher than context", voteMsg);
$"Filtered vote from different height: {voteMsg.Height}",
voteMsg);
}
}

Expand All @@ -113,7 +138,8 @@ private void ValidateMessageToSend(MessageContent content)
/// </param>
private void FilterHigherRoundVoteSpam(ConsensusVoteMsg voteMsg, BoundPeer peer)
{
if (voteMsg.Round > _round)
if (voteMsg.Height == _height &&
voteMsg.Round > _round)
{
if (!_peerCatchupRounds.ContainsKey(peer))
{
Expand All @@ -130,7 +156,8 @@ private void FilterHigherRoundVoteSpam(ConsensusVoteMsg voteMsg, BoundPeer peer)
{
Gossip.DenyPeer(peer);
throw new InvalidConsensusMessageException(
$"Repetitively found higher rounds, add {peer} to deny set",
$"Add {peer} to deny set, since repetitively found higher rounds: " +
$"{string.Join(", ", _peerCatchupRounds[peer])}",
voteMsg);
}
}
Expand Down

0 comments on commit 64549cf

Please sign in to comment.