Skip to content

Commit

Permalink
Fix Swarm() hangs
Browse files Browse the repository at this point in the history
  • Loading branch information
longfin committed Apr 2, 2019
1 parent 452b28c commit 4bdacac
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
5 changes: 4 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ To be released.
work around NAT so that `Peer`'s endpoints don't have to be multiple,
`Peer.Urls` was renamed to `Peer.EndPoint` and its type also was changed
from `IImmutableList<Uri>` to `DnsEndPoint`.
[[#120], [#123] by Yang Chun Ung, [#126], [#127], [#165]]
[[#120], [#123] by Yang Chun Ung, [#126], [#127], [#165], [#166]]
- `Swarm` became to ignore tip blocks of the same height (`Index`) that it
already has and deal with only longer (higher) blocks.
- Fixed a bug that occured when `Swarm` was handling multiple responses at the
same time.
- Fixed a bug that the `Swarm` constructor had hanged in certain runtimes
like Unity engine.
- Removed `AddressTransactionSet` which handles handle `Address` to
`IEnumerable<TxId>` indices, and the following methods in `IStore`:
- `IStore.IterateAddresses()`
Expand Down Expand Up @@ -141,6 +143,7 @@ To be released.
[#151]: https://github.com/planetarium/libplanet/pull/151
[#159]: https://github.com/planetarium/libplanet/pull/159
[#165]: https://github.com/planetarium/libplanet/issues/165
[#165]: https://github.com/planetarium/libplanet/pull/166
[RFC 5389]: https://tools.ietf.org/html/rfc5389
[RFC 5766]: https://tools.ietf.org/html/rfc5766

Expand Down
16 changes: 15 additions & 1 deletion Libplanet.Tests/Net/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,26 @@ await Task.WhenAll(
}

[Fact]
public void CanDenyNullParams()
public void ThrowArgumentExceptionInConstructor()
{
Assert.Throws<ArgumentNullException>(() =>
{
new Swarm(null);
});

// Swarm needs host or iceServers.
Assert.Throws<ArgumentException>(() =>
{
new Swarm(new PrivateKey());
});

// Swarm needs host or iceServers.
Assert.Throws<ArgumentException>(() =>
{
new Swarm(
new PrivateKey(),
iceServers: new IceServer[] { });
});
}

[Fact]
Expand Down
20 changes: 12 additions & 8 deletions Libplanet/Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ public partial class Swarm : ICollection<Peer>, IDisposable
private readonly AsyncLock _receiveMutex;
private readonly AsyncLock _blockSyncMutex;
private readonly string _host;
private readonly TurnClient _turnClient;
private readonly IList<IceServer> _iceServers;

private readonly ILogger _logger;

private TaskCompletionSource<object> _runningEvent;
private int? _listenPort;
private TurnClient _turnClient;

private NetMQQueue<Message> _replyQueue;

Expand Down Expand Up @@ -115,15 +116,13 @@ public Swarm(
EndPoint = new DnsEndPoint(_host, listenPort.Value);
}

if (iceServers != null)
{
_turnClient = IceServer.CreateTurnClient(iceServers).Result;
}

if (_host == null && _turnClient == null)
_iceServers = iceServers?.ToList();
if (_host == null && (_iceServers == null || !_iceServers.Any()))
{
throw new ArgumentException(
$"Swarm needs {nameof(host)} or {iceServers}.");
$"Swarm requires either {nameof(host)} or " +
$"{nameof(iceServers)}."
);
}

string loggerId = _privateKey.PublicKey.ToAddress().ToHex();
Expand Down Expand Up @@ -399,6 +398,11 @@ public async Task StartAsync<T>(
throw new SwarmException("Swarm is already running.");
}

if (_iceServers != null)
{
_turnClient = await IceServer.CreateTurnClient(_iceServers);
}

if (_listenPort == null)
{
_listenPort = _router.BindRandomPort("tcp://*");
Expand Down

0 comments on commit 4bdacac

Please sign in to comment.