Skip to content

Commit

Permalink
test(consensus): test for ConsensusReactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee committed Oct 24, 2021
1 parent 2cd3b98 commit afb80c1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
42 changes: 42 additions & 0 deletions Libplanet.Tests/Net/Consensus/ConsensusReactorTest.cs
@@ -0,0 +1,42 @@
using Libplanet.Net.Consensus;
using Libplanet.Net.Messages;
using Xunit;

namespace Libplanet.Tests.Net.Consensus
{
public class ConsensusReactorTest
{
[Fact]
public void ProcessPropose()
{
ConsensusReactor reactor = new ConsensusReactor(0, new MockedConsensusBroadCaster());
var data = new byte[] { 0x01 };
ConsensusPropose propose = new ConsensusPropose(0, 0, new byte[] { 0x01 });
reactor.ProcessMessage(propose);

ConsensusState state = reactor.GetState();
Assert.Equal(data, state.Data);
Assert.Equal(ConsensusState.RoundStep.RoundStepPrevoteWait, state.Step);
}

[Fact]
public void ProcessProposeWrongRound()
{
ConsensusReactor reactor = new ConsensusReactor(0, new MockedConsensusBroadCaster());
var data = new byte[] { 0x01 };
ConsensusPropose propose = new ConsensusPropose(0, 1, new byte[] { 0x01 });
reactor.ProcessMessage(propose);

ConsensusState state = reactor.GetState();
Assert.Equal(new byte[] { }, state.Data);
Assert.Equal(ConsensusState.RoundStep.RoundStepNewRound, state.Step);
}

public class MockedConsensusBroadCaster : IConsensusBroadcaster
{
public void BroadcastMessage(ConsensusMessage message)
{
}
}
}
}
10 changes: 6 additions & 4 deletions Libplanet.Tests/Net/SwarmTest.Fixtures.cs
Expand Up @@ -74,7 +74,8 @@ public partial class SwarmTest
IEnumerable<IceServer> iceServers = null,
DifferentAppProtocolVersionEncountered differentAppProtocolVersionEncountered = null,
IEnumerable<PublicKey> trustedAppProtocolVersionSigners = null,
SwarmOptions options = null)
SwarmOptions options = null,
long nodeId = 0)
{
var policy = new BlockPolicy<DumbAction>(new MinerReward(1));
var fx = new DefaultStoreFixture(memory: true, blockAction: policy.BlockAction);
Expand All @@ -100,8 +101,8 @@ public partial class SwarmTest
IEnumerable<IceServer> iceServers = null,
DifferentAppProtocolVersionEncountered differentAppProtocolVersionEncountered = null,
IEnumerable<PublicKey> trustedAppProtocolVersionSigners = null,
SwarmOptions options = null
)
SwarmOptions options = null,
long nodeId = 0)
where T : IAction, new()
{
if (host is null && !(iceServers?.Any() ?? false))
Expand All @@ -119,7 +120,8 @@ public partial class SwarmTest
iceServers,
differentAppProtocolVersionEncountered,
trustedAppProtocolVersionSigners,
options);
options,
nodeId: nodeId);
_finalizers.Add(async () =>
{
await StopAsync(swarm);
Expand Down
19 changes: 11 additions & 8 deletions Libplanet/Net/Consensus/ConsensusReactor.cs
Expand Up @@ -49,12 +49,7 @@ public async Task ReactConsensusAsync(int tick, CancellationToken token)
}
}

private void BroadcastMessage(ConsensusMessage message)
{
_broadcaster.BroadcastMessage(message);
}

private void ProcessMessage(ConsensusMessage message)
internal void ProcessMessage(ConsensusMessage message)
{
switch (message)
{
Expand All @@ -72,6 +67,16 @@ private void ProcessMessage(ConsensusMessage message)
}
}

internal ConsensusState GetState()
{
return _state;
}

private void BroadcastMessage(ConsensusMessage message)
{
_broadcaster.BroadcastMessage(message);
}

private void Propose(ConsensusPropose propose)
{
switch (propose)
Expand All @@ -85,7 +90,6 @@ private void Propose(ConsensusPropose propose)
}

_lock.EnterWriteLock();
_lock.EnterReadLock();
_state.Data = propose.Data;
_state.Vote23Count = _state.VoteCount = 0;
_state.Step = ConsensusState.RoundStep.RoundStepPrevote;
Expand All @@ -102,7 +106,6 @@ private void Propose(ConsensusPropose propose)
}

_state.Step = ConsensusState.RoundStep.RoundStepPrevoteWait;
_lock.EnterReadLock();
_lock.ExitWriteLock();
}

Expand Down
6 changes: 4 additions & 2 deletions Libplanet/Net/Swarm.cs
Expand Up @@ -70,6 +70,7 @@ public partial class Swarm<T> : IDisposable
/// <c>null</c>, which is default.</param>
/// <param name="options">Options for <see cref="Swarm{T}"/>.</param>
/// <param name="consensusReactor">The reactor for consensus.</param>
/// <param name="nodeId">Node Id.</param>
public Swarm(
BlockChain<T> blockChain,
PrivateKey privateKey,
Expand All @@ -81,7 +82,8 @@ public partial class Swarm<T> : IDisposable
DifferentAppProtocolVersionEncountered differentAppProtocolVersionEncountered = null,
IEnumerable<PublicKey> trustedAppProtocolVersionSigners = null,
SwarmOptions options = null,
IConsensusReactor consensusReactor = null)
IConsensusReactor consensusReactor = null,
long nodeId = 0)
{
BlockChain = blockChain ?? throw new ArgumentNullException(nameof(blockChain));
_store = BlockChain.Store;
Expand Down Expand Up @@ -122,7 +124,7 @@ public partial class Swarm<T> : IDisposable
Transport.ProcessMessageHandler += ProcessMessageHandler;
PeerDiscovery = new KademliaProtocol(RoutingTable, Transport, Address);
ConsensusReactor = consensusReactor ?? new ConsensusReactor(
0,
nodeId,
new ConsensusBroadcaster(Transport));
}

Expand Down

0 comments on commit afb80c1

Please sign in to comment.