Skip to content

Commit

Permalink
Merge pull request #218 from longfin/feature/swarm-broadcast
Browse files Browse the repository at this point in the history
Fix Swarm broadcasting
  • Loading branch information
longfin authored May 9, 2019
2 parents 0384134 + c198bc9 commit 49ac8da
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 199 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ matrix:
- os: osx
osx_image: xcode10.1
language: csharp
dotnet: 2.2.106
dotnet: 2.2.203
mono: none
cache:
directories:
Expand Down Expand Up @@ -172,11 +172,12 @@ script:
- |
if [[ "$TRAVIS_OS_NAME" = "windows" && "$CODECOV_TOKEN" != "" ]]; then
dotnet build -c Debug
dotnet build-server shutdown
solution_abspath="$(cygpath -w "$(pwd)"/Libplanet.sln)"
dotCover_config="$(cat .travis.dotCover.xml)"
echo "${dotCover_config//\$SOLUTION/$solution_abspath}" \
> dotCover.xml
travis_wait dotCover.exe cover dotCover.xml
travis_wait 60 dotCover.exe cover dotCover.xml /p:UseSharedCompilation=false
# Upload report to Codecov.io
codecov.exe -f Libplanet.cov.xml -t "$CODECOV_TOKEN"
fi
Expand Down
11 changes: 11 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ To be released.
- `Swarm` class now does not implement `IEquatable<Swarm>` anymore and
its `Equals(object)` method and `GetHashCode()` method became to have
default behavior of `object` class. [[#216]]
- Also, `Swarm` class now does not implement `IDisposable` too. Thus
`Swarm.Dispose()` was removed too. [[#218]]
- `Swarm` became to use a queue to maintain internal messages. [[#218]]
- The broadcasting methods are no more `async`, so they are renamed
as below.
- `Swarm.BroadcastBlocksAsync()``Swarm.BroadcastBlocks()`
- `Swarm.BroadcastTxsAsync()``Swarm.BroadcastTxs()`
- The type of `Block<T>.Difficulty` is changed to `long` instead of `int`, and
related classes method parameters and field types have changed accordingly.
- Removed `HashDigest.HasLeadingZeroBits()` method. [[#213]]
Expand Down Expand Up @@ -57,6 +64,9 @@ To be released.
- `BlockPolicy<T>` constructor became to receive the `minimumDifficulty`
and the mining `difficultyBoundDivisor`. [[#213]]
- Added `BlockChain<T>.UnstageTransactions()` method. [[#223]]
- `Swarm` constructor became to receive a `linger` (or `millisecondsLinger`)
parameter. This purposes to determine how long to wait for pending
messages when a `Swarm` instance is requested to terminate.

### Behavioral changes

Expand Down Expand Up @@ -117,6 +127,7 @@ To be released.
[#215]: https://github.com/planetarium/libplanet/pull/215
[#216]: https://github.com/planetarium/libplanet/pull/216
[#217]: https://github.com/planetarium/libplanet/pull/217
[#218]: https://github.com/planetarium/libplanet/pull/218
[#223]: https://github.com/planetarium/libplanet/pull/223
[#231]: https://github.com/planetarium/libplanet/pull/231

Expand Down
65 changes: 36 additions & 29 deletions Libplanet.Tests/Net/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace Libplanet.Tests.Net
{
public class SwarmTest : IDisposable
{
private const int Timeout = 60 * 1000;

private readonly FileStoreFixture _fx1;
private readonly FileStoreFixture _fx2;
private readonly FileStoreFixture _fx3;
Expand Down Expand Up @@ -77,11 +79,11 @@ public void Dispose()

foreach (Swarm s in _swarms)
{
s.Dispose();
s.StopAsync().Wait();
}
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task CanNotStartTwice()
{
Swarm swarm = _swarms[0];
Expand All @@ -98,7 +100,7 @@ public async Task CanNotStartTwice()
await swarm.StopAsync();
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task CanStop()
{
Swarm swarm = _swarms[0];
Expand All @@ -114,7 +116,7 @@ public async Task CanStop()
await task;
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task CanWaitForRunning()
{
Swarm swarm = _swarms[0];
Expand Down Expand Up @@ -142,7 +144,7 @@ public async Task CanWaitForRunning()
Assert.False(swarm.Running);
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task BroadcastWhileMining()
{
Swarm a = _swarms[0];
Expand All @@ -166,11 +168,11 @@ CancellationToken cancellationToken
Log.Debug(
$"Block mined. " +
$"[Swarm: {swarm.Address}, Block: {block.Hash}]");
await swarm.BroadcastBlocksAsync(new[] { block });
swarm.BroadcastBlocks(new[] { block });
await Task.Delay(delay);
}
await swarm.BroadcastBlocksAsync(new[] { chain.Last() });
swarm.BroadcastBlocks(new[] { chain.Last() });
Log.Debug("Mining complete.");
});
}
Expand Down Expand Up @@ -208,7 +210,7 @@ CancellationToken cancellationToken
chainB.AsEnumerable().ToHashSet());
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task CanExchangePeer()
{
Swarm a = _swarms[0];
Expand Down Expand Up @@ -265,7 +267,7 @@ public async Task CanExchangePeer()
}
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task WorksAsCollection()
{
Swarm a = _swarms[0];
Expand Down Expand Up @@ -305,10 +307,12 @@ await Task.WhenAll(

a.CopyTo(peers, 1);

Assert.Equal(new Peer[] { null, b.AsPeer, c.AsPeer }, peers);
Assert.Equal(
new HashSet<Peer> { null, b.AsPeer, c.AsPeer },
peers.ToHashSet());
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task DetectAppProtocolVersion()
{
var a = new Swarm(
Expand Down Expand Up @@ -358,7 +362,7 @@ public async Task DetectAppProtocolVersion()
}
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task HandleDifferentAppProtocolVersion()
{
var isCalled = false;
Expand Down Expand Up @@ -396,8 +400,8 @@ void GameHandler(object sender, DifferentProtocolVersionEventArgs e)
}
}

[Fact]
public async Task CanBeCancelled()
[Fact(Timeout = Timeout)]
public async Task Cancel()
{
Swarm swarm = _swarms[0];
BlockChain<DumbAction> chain = _blockchains[0];
Expand All @@ -410,11 +414,11 @@ public async Task CanBeCancelled()
);

cts.Cancel();
await Assert.ThrowsAsync<TaskCanceledException>(async () => await task);
await task;
Assert.False(swarm.Running);
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task CanGetBlock()
{
Swarm swarmA = _swarms[0];
Expand Down Expand Up @@ -476,7 +480,7 @@ await Task.WhenAll(
}
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task GetTx()
{
Swarm swarmA = _swarms[0];
Expand Down Expand Up @@ -518,7 +522,7 @@ await Task.WhenAll(
}
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task BroadcastTx()
{
Swarm swarmA = _swarms[0];
Expand Down Expand Up @@ -550,7 +554,7 @@ public async Task BroadcastTx()
await EnsureExchange(swarmA, swarmC);
await EnsureExchange(swarmB, swarmC);

await swarmA.BroadcastTxsAsync(new[] { tx });
swarmA.BroadcastTxs(new[] { tx });

await swarmC.TxReceived.WaitAsync();
await swarmB.TxReceived.WaitAsync();
Expand All @@ -567,7 +571,7 @@ await Task.WhenAll(
}
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task CanBroadcastBlock()
{
Swarm swarmA = _swarms[0];
Expand Down Expand Up @@ -608,7 +612,7 @@ public async Task CanBroadcastBlock()
await EnsureExchange(swarmA, swarmC);
await EnsureExchange(swarmB, swarmC);

await swarmB.BroadcastBlocksAsync(new[] { chainB.Last() });
swarmB.BroadcastBlocks(new[] { chainB.Last() });

await swarmC.BlockReceived.WaitAsync();
await swarmA.BlockReceived.WaitAsync();
Expand All @@ -619,7 +623,7 @@ public async Task CanBroadcastBlock()
// than chainA
Assert.NotEqual(chainB.AsEnumerable(), chainA);

await swarmA.BroadcastBlocksAsync(new[] { chainA.Last() });
swarmA.BroadcastBlocks(new[] { chainA.Last() });

await swarmB.BlockReceived.WaitAsync();
await swarmC.BlockReceived.WaitAsync();
Expand All @@ -636,7 +640,7 @@ await Task.WhenAll(
}
}

[Fact]
[Fact(Timeout = Timeout)]
public void ThrowArgumentExceptionInConstructor()
{
Assert.Throws<ArgumentNullException>(() =>
Expand All @@ -660,7 +664,7 @@ public void ThrowArgumentExceptionInConstructor()
});
}

[Fact]
[Fact(Timeout = Timeout)]
public void CanResolveEndPoint()
{
var expected = new DnsEndPoint("1.2.3.4", 5678);
Expand All @@ -674,7 +678,7 @@ public void CanResolveEndPoint()
Assert.Equal(expected, s.AsPeer.EndPoint);
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task CanStopGracefullyWhileStarting()
{
Swarm a = _swarms[0];
Expand All @@ -687,7 +691,7 @@ public async Task CanStopGracefullyWhileStarting()
await Task.WhenAll(a.StopAsync(), t);
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task AsPeerThrowSwarmExceptionWhenUnbound()
{
Swarm swarm = new Swarm(
Expand Down Expand Up @@ -744,7 +748,7 @@ await Task.WhenAll(
}
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task InitialBlockDownload()
{
Swarm minerSwarm = _swarms[0];
Expand Down Expand Up @@ -777,7 +781,7 @@ await Task.WhenAll(
}
}

[Fact]
[Fact(Timeout = Timeout)]
public async Task Preload()
{
Swarm minerSwarm = _swarms[0];
Expand All @@ -794,7 +798,10 @@ public async Task Preload()
var actualStates = new List<BlockDownloadState>();
var progress = new Progress<BlockDownloadState>(state =>
{
actualStates.Add(state);
lock (actualStates)
{
actualStates.Add(state);
}
});

try
Expand Down
Loading

0 comments on commit 49ac8da

Please sign in to comment.