Skip to content

Commit

Permalink
Merge pull request #247 from longfin/bugfix/asyncio-force-dotnet
Browse files Browse the repository at this point in the history
Force dotnet if running on mono
  • Loading branch information
longfin authored May 22, 2019
2 parents 9a2e95f + e5938ce commit 24657dd
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .azure-pipelines/mono.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ steps:
displayName: xunit.console.exe *.Tests.dll
inputs:
script: |
mono xunit.runner.console.*/tools/net472/xunit.console.exe \
mono xunit.runner.console.*/tools/net461/xunit.console.exe \
*.Tests/bin/${{ parameters.configuration }}/net*/*.Tests.dll
env:
TURN_SERVER_URL: ${{ parameters.turnServerUrl }}
Expand Down
8 changes: 1 addition & 7 deletions .azure-pipelines/windows-net461.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,9 @@ steps:
targetType: inline
script: |
${{ parameters.testPrefix }} \
xunit.runner.console.*/tools/net472/xunit.console.exe \
xunit.runner.console.*/tools/net461/xunit.console.exe \
*.Tests/bin/$(configuration)/net*/*.Tests.dll \
${{ parameters.testArguments }}
# FIXME: For unknown reason, on Windows tests depending on TURN_SERVER_URL
# seems to never end or to take too long time. We should diagnose
# this and make Windows builds to run these tests too.
# FIXME: For unknown reason, on Windows + Mono SwarmTests seem never end.
# We should diagnose this and make Windows builds to run these
# tests too.
env:
TURN_SERVER_URL: ${{ parameters.turnServerUrl }}
timeoutInMinutes: 10
9 changes: 8 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ To be released.
- `BlockChain<T>.Append()` method became to throw `InvalidTxNonceException`
when the `Transaction<T>.Nonce` does not correspond to its `Signer`'s
current nonce. [[#246]]

- `Swarm` became to enforce `ForceDotNet.Force()` in [AsyncIO] while it's running on
Mono runtime. [[#247]]

### Bug fixes

- Fixed a bug that TURN relay had been disconnected when being connected for
Expand Down Expand Up @@ -140,7 +142,10 @@ To be released.
unnecessary race conditions. [[#217]]
- Fixed a bug that `Swarm` could not properly communicate with `Peer` behind
NAT. [[#240]]
- Fixed a bug that `BlockChain<T>.FindNextHashes()` throws
`ArgumentOutOfRangeException` when chain is empty.

[AsyncIO]: https://github.com/somdoron/AsyncIO
[Ethereum Homestead algorithm]: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.md
[#31]: https://github.com/planetarium/libplanet/issues/31
[#133]: https://github.com/planetarium/libplanet/issues/133
Expand Down Expand Up @@ -171,6 +176,8 @@ To be released.
[#240]: https://github.com/planetarium/libplanet/pull/240
[#241]: https://github.com/planetarium/libplanet/pull/241
[#243]: https://github.com/planetarium/libplanet/pull/243
[#246]: https://github.com/planetarium/libplanet/pull/246
[#247]: https://github.com/planetarium/libplanet/pull/247
[#251]: https://github.com/planetarium/libplanet/pull/251


Expand Down
6 changes: 5 additions & 1 deletion Libplanet.Tests/Blockchain/BlockChainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Security.Cryptography;
using Libplanet.Action;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
Expand Down Expand Up @@ -300,8 +301,11 @@ public void AppendValidatesBlock()
}

[Fact]
public void CanFindNextHashes()
public void FindNextHashes()
{
Assert.Empty(_blockChain.FindNextHashes(
new BlockLocator(new HashDigest<SHA256>[] { })
));
_blockChain.Append(_fx.Block1);
var block0 = _fx.Block1;
var block1 = _blockChain.MineBlock(_fx.Address1);
Expand Down
60 changes: 10 additions & 50 deletions Libplanet.Tests/Net/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public void Dispose()
{
s.StopAsync().Wait(DisposeTimeout);
}

if (!(Type.GetType("Mono.Runtime") is null))
{
NetMQConfig.Cleanup(false);
}
}

[Fact(Timeout = Timeout)]
Expand Down Expand Up @@ -269,51 +274,6 @@ public async Task CanExchangePeer()
}
}

[Fact(Timeout = Timeout)]
public async Task WorksAsCollection()
{
Swarm a = _swarms[0];
Swarm b = _swarms[1];
Swarm c = _swarms[2];

// Obtaining swarm's endpoint...
await Task.WhenAll(
StartAsync(a, _blockchains[0]),
StartAsync(b, _blockchains[1]),
StartAsync(c, _blockchains[2]));

Assert.Empty(a);
Assert.Empty(b);
Assert.Empty(c);

a.Add(b.AsPeer);
a.Add(c.AsPeer);
Assert.Contains(b.AsPeer, a);
Assert.Contains(c.AsPeer, a);

Peer[] peers = null;
Assert.Throws<ArgumentNullException>(() =>
{
a.CopyTo(peers, 0);
});

peers = new Peer[3];
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
a.CopyTo(peers, -1);
});
Assert.Throws<ArgumentException>(() =>
{
a.CopyTo(peers, 2);
});

a.CopyTo(peers, 1);

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

[Fact(Timeout = Timeout)]
public async Task DetectAppProtocolVersion()
{
Expand Down Expand Up @@ -348,8 +308,8 @@ public async Task DetectAppProtocolVersion()

foreach (var peer in peers)
{
a.Add(peer);
b.Add(peer);
await a.AddPeersAsync(new[] { peer });
await b.AddPeersAsync(new[] { peer });
}

Assert.Equal(new[] { c.AsPeer }, a.ToArray());
Expand Down Expand Up @@ -391,7 +351,7 @@ void GameHandler(object sender, DifferentProtocolVersionEventArgs e)
await StartAsync(a, chain);
await StartAsync(b, chain);

a.Add(b.AsPeer);
await a.AddPeersAsync(new[] { b.AsPeer });

Assert.True(isCalled);
}
Expand Down Expand Up @@ -768,7 +728,7 @@ public async Task InitialBlockDownload()
try
{
await StartAsync(minerSwarm, minerChain);
receiverSwarm.Add(minerSwarm.AsPeer);
await receiverSwarm.AddPeersAsync(new[] { minerSwarm.AsPeer });

await StartAsync(receiverSwarm, receiverChain);

Expand Down Expand Up @@ -810,7 +770,7 @@ public async Task Preload()
try
{
await StartAsync(minerSwarm, minerChain);
receiverSwarm.Add(minerSwarm.AsPeer);
await receiverSwarm.AddPeersAsync(new[] { minerSwarm.AsPeer });

await receiverSwarm.PreloadAsync(receiverChain, progress);

Expand Down
5 changes: 5 additions & 0 deletions Libplanet/Blockchain/BlockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,11 @@ internal IEnumerable<HashDigest<SHA256>> FindNextHashes(

HashDigest<SHA256>? tip = Store.IndexBlockHash(
Id.ToString(), -1);
if (tip is null)
{
yield break;
}

HashDigest<SHA256>? currentHash = FindBranchPoint(locator);

while (currentHash != null && count > 0)
Expand Down
29 changes: 26 additions & 3 deletions Libplanet/Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using AsyncIO;
using Libplanet.Action;
using Libplanet.Blockchain;
using Libplanet.Blocks;
Expand Down Expand Up @@ -65,6 +66,14 @@ public class Swarm : ICollection<Peer>
private CancellationTokenSource _workerCancellationTokenSource;
private IPAddress _publicIPAddress;

static Swarm()
{
if (!(Type.GetType("Mono.Runtime") is null))
{
ForceDotNet.Force();
}
}

public Swarm(
PrivateKey privateKey,
int appProtocolVersion,
Expand Down Expand Up @@ -243,7 +252,7 @@ public async Task<ISet<Peer>> AddPeersAsync(
DateTimeOffset? timestamp = null,
CancellationToken cancellationToken = default(CancellationToken))
{
if (timestamp == null)
if (timestamp is null)
{
timestamp = DateTimeOffset.UtcNow;
}
Expand Down Expand Up @@ -306,6 +315,10 @@ public async Task<ISet<Peer>> AddPeersAsync(
);
}
}
else
{
_peers[peer] = timestamp.Value;
}
}

return addedPeers;
Expand Down Expand Up @@ -1083,9 +1096,19 @@ private async Task<BlockChain<T>> SyncPreviousBlocksAsync<T>(
_logger.Debug("Trying to find branchpoint...");
BlockLocator locator = blockChain.GetBlockLocator();
_logger.Debug($"Locator's count: {locator.Count()}");
IEnumerable<HashDigest<SHA256>> hashes =
IEnumerable<HashDigest<SHA256>> hashes = (
await GetBlockHashesAsync(
peer, locator, stop, cancellationToken);
peer, locator, stop, cancellationToken)
).ToArray();

if (!hashes.Any())
{
_logger.Debug(
$"Peer[{peer}] didn't return any hashes. " +
$"ignored.");
return blockChain;
}

HashDigest<SHA256> branchPoint = hashes.First();

_logger.Debug(
Expand Down
7 changes: 1 addition & 6 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,7 @@ jobs:
configuration: $(configuration)
turnServerUrl: $(turnServerUrl)
testPrefix: '"$PROGRAMFILES/Mono/bin/mono.exe"'
testArguments: |
-appdomains denied \
-noclass Libplanet.Tests.Net.SwarmTest
# FIXME: For unknown reason, on Windows + Mono SwarmTests seem never end.
# We should diagnose this and make Windows builds to run these
# tests too.
testArguments: -appdomains denied
timeoutInMinutes: 30

- job: Linux_NETCore
Expand Down

0 comments on commit 24657dd

Please sign in to comment.