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 4, 2019
1 parent 3d512c8 commit 1795383
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,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
17 changes: 16 additions & 1 deletion Libplanet.Tests/Net/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -627,12 +627,27 @@ await Task.WhenAll(
}

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

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

// Swarm needs host or iceServers.
Assert.Throws<ArgumentException>(() =>
{
new Swarm(
new PrivateKey(),
1,
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 @@ -47,12 +47,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 @@ -120,15 +121,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 @@ -430,6 +429,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 1795383

Please sign in to comment.