Skip to content

Commit

Permalink
Merge pull request #330 from LATOKEN/binary-broadcast-spam-bug-fix
Browse files Browse the repository at this point in the history
Binary broadcast spam bug fix
  • Loading branch information
tbssajal authored Dec 23, 2022
2 parents d5b5e02 + 96e0e62 commit 7ad48a5
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Lachain.Consensus/BinaryAgreement/BinaryBroadcast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private void HandleBValMessage(int sender, BValMessage bval)
return;
}

_receivedValues[sender].Add(b == 1);
_receivedValues[sender] = _receivedValues[sender].Add(b == 1);
++_receivedCount[b];
InvokeReceivedExternalMessage(sender, new ConsensusMessage { Bval = bval });

Expand Down
46 changes: 46 additions & 0 deletions test/Lachain.ConsensusTest/BinaryBroadcastSpammer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Lachain.Logger;
using Lachain.Consensus.BinaryAgreement;
using Lachain.Consensus;
using Lachain.Consensus.Messages;
using Lachain.Proto;
using Lachain.Utility.Utils;

namespace Lachain.ConsensusTest
{
public class BinaryBroadcastSpammer : BinaryBroadcast
{
private readonly BinaryBroadcastId _broadcastId;
public BinaryBroadcastSpammer(
BinaryBroadcastId broadcastId, IPublicConsensusKeySet wallet, IConsensusBroadcaster broadcaster)
: base(broadcastId, wallet, broadcaster)
{
_broadcastId = broadcastId;
}

public void SpamBVal(bool bval)
{
var b = bval ? 1 : 0;
var msg = CreateBValMessage(b);
for (int i = 0 ; i < N ; i++) Broadcaster.Broadcast(msg);
}

[MethodImpl(MethodImplOptions.Synchronized)]
private ConsensusMessage CreateBValMessage(int value)
{
var message = new ConsensusMessage
{
Bval = new BValMessage
{
Agreement = _broadcastId.Agreement,
Epoch = _broadcastId.Epoch,
Value = value == 1
}
};
return message;
}
}
}
38 changes: 38 additions & 0 deletions test/Lachain.ConsensusTest/BinaryBroadcastTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ private void SetupSomeSilent(int n, int f)
}
}

private void SetupOneSpammer(int n , int f, int spammer)
{
SetUp(n, f);
for (uint i = 0; i < n; ++i)
{
if (i == spammer) continue;
_broadcasts[i] = new BinaryBroadcast(new BinaryBroadcastId(0, 0, 0), _publicKeys, _broadcasters[i]);
}
_broadcasts[spammer] = new BinaryBroadcastSpammer(new BinaryBroadcastId(0, 0, 0), _publicKeys, _broadcasters[spammer]);
for (var i = 0 ; i < n ; i++)
{
_broadcasters[i].RegisterProtocols(new[] {_broadcasts[i], _resultInterceptors[i]});
}
}

[Test]
public void TestBinaryBroadcastAllOne_7_2()
{
Expand Down Expand Up @@ -183,5 +198,28 @@ public void TestRandomValues()
Assert.Greater(received[i].Count, 0, "all correct nodes should output something");
}
}

[Test]
public void TestOneSpammer_7_2()
{
const int n = 7, f = 2, spammerId = 6;
SetupOneSpammer(n, f, spammerId);
for (var i = 0; i < n; ++i)
_broadcasters[i].InternalRequest(new ProtocolRequest<BinaryBroadcastId, bool>(
_resultInterceptors[i].Id, (_broadcasts[i].Id as BinaryBroadcastId)!, true
));
// spammer will spam unwanted value 'false' N times to each validator
(_broadcasts[spammerId] as BinaryBroadcastSpammer)!.SpamBVal(false);

for (var i = 0; i < n; ++i) _broadcasts[i].WaitFinish();

for (var i = 0; i < n; ++i)
{
Assert.IsTrue(_broadcasts[i].Terminated, $"protocol {i} did not terminated");
Assert.AreEqual(_resultInterceptors[i].ResultSet, 1, $"protocol {i} emitted result not once");
Assert.AreEqual(_resultInterceptors[i].ResultSet, _resultInterceptors[i].Result.Count);
Assert.AreEqual(new BoolSet(true), _resultInterceptors[i].Result[0]);
}
}
}
}

0 comments on commit 7ad48a5

Please sign in to comment.