From 4e4e1dea4781aec12d668649cf0ef476c1304d32 Mon Sep 17 00:00:00 2001 From: moreal Date: Tue, 14 Jan 2020 23:15:08 +0900 Subject: [PATCH 1/3] Distinguish the starting stage of Swarm clearly --- Libplanet.Benchmarks/SwarmBenchmark.cs | 2 +- Libplanet.Tests/Net/SwarmTest.cs | 3 ++- Libplanet/Net/Swarm.cs | 25 +------------------------ 3 files changed, 4 insertions(+), 26 deletions(-) diff --git a/Libplanet.Benchmarks/SwarmBenchmark.cs b/Libplanet.Benchmarks/SwarmBenchmark.cs index b2e7a5af60..402d23be26 100644 --- a/Libplanet.Benchmarks/SwarmBenchmark.cs +++ b/Libplanet.Benchmarks/SwarmBenchmark.cs @@ -194,7 +194,7 @@ private async Task StartAsync( ) where T : IAction, new() { - Task task = swarm.StartAsync(200, 200, null, cancellationToken); + Task task = swarm.StartAsync(200, 200, cancellationToken); await swarm.WaitForRunningAsync(); return task; } diff --git a/Libplanet.Tests/Net/SwarmTest.cs b/Libplanet.Tests/Net/SwarmTest.cs index bf587becdc..0d65c112ba 100644 --- a/Libplanet.Tests/Net/SwarmTest.cs +++ b/Libplanet.Tests/Net/SwarmTest.cs @@ -394,6 +394,7 @@ public async Task AutoConnectAfterStart() Assert.Empty(swarmB.Peers); await StartAsync(swarmA); + await Task.Delay(100); Assert.Contains(swarmA.AsPeer, swarmB.Peers); } finally @@ -2765,7 +2766,7 @@ private async Task StartAsync( ) where T : IAction, new() { - Task task = swarm.StartAsync(200, 200, null, cancellationToken); + Task task = swarm.StartAsync(200, 200, cancellationToken); await swarm.WaitForRunningAsync(); return task; } diff --git a/Libplanet/Net/Swarm.cs b/Libplanet/Net/Swarm.cs index 66f2143858..d142123f0e 100644 --- a/Libplanet/Net/Swarm.cs +++ b/Libplanet/Net/Swarm.cs @@ -223,13 +223,11 @@ public async Task StopAsync( public async Task StartAsync( int millisecondsDialTimeout = 15000, int millisecondsBroadcastTxInterval = 5000, - EventHandler preloadBlockDownloadFailed = null, CancellationToken cancellationToken = default(CancellationToken)) { await StartAsync( TimeSpan.FromMilliseconds(millisecondsDialTimeout), TimeSpan.FromMilliseconds(millisecondsBroadcastTxInterval), - preloadBlockDownloadFailed: preloadBlockDownloadFailed, cancellationToken ); } @@ -242,13 +240,7 @@ await StartAsync( /// /// The time period of exchange of staged transactions. /// - /// - /// The triggered when - /// - /// fails to download blocks. - /// - /// /// + /// /// A cancellation token used to propagate notification that this /// operation should be canceled. /// @@ -267,7 +259,6 @@ await StartAsync( public async Task StartAsync( TimeSpan dialTimeout, TimeSpan broadcastTxInterval, - EventHandler preloadBlockDownloadFailed = null, CancellationToken cancellationToken = default(CancellationToken)) { var tasks = new List(); @@ -280,15 +271,6 @@ public async Task StartAsync( _logger.Debug("Starting swarm..."); _logger.Debug("Peer information : {Peer}", AsPeer); - using (await _runningMutex.LockAsync()) - { - await PreloadAsync( - dialTimeout: dialTimeout, - render: true, - cancellationToken: _cancellationToken - ); - } - try { tasks.Add(_transport.RunAsync(_cancellationToken)); @@ -407,11 +389,6 @@ public string TraceTable() /// A task without value. /// You only can await until the method is completed. /// - /// This does not render downloaded s, but fills states only. - /// If you want to render all s from the genesis block to the recent - /// blocks use - /// - /// method instead. /// Thrown when the given the block downloading is /// failed and if is null. #pragma warning restore MEN002 // Line is too long From e6256efa4a9d656209cebe8d12e503514d335f45 Mon Sep 17 00:00:00 2001 From: moreal Date: Wed, 15 Jan 2020 18:48:23 +0900 Subject: [PATCH 2/3] Remove render parameter in PreloadAsync --- Libplanet.Tests/Net/SwarmTest.cs | 4 +- Libplanet/Net/Swarm.cs | 73 +++++++++++--------------------- 2 files changed, 27 insertions(+), 50 deletions(-) diff --git a/Libplanet.Tests/Net/SwarmTest.cs b/Libplanet.Tests/Net/SwarmTest.cs index 0d65c112ba..da4dd496e6 100644 --- a/Libplanet.Tests/Net/SwarmTest.cs +++ b/Libplanet.Tests/Net/SwarmTest.cs @@ -1404,7 +1404,7 @@ public async Task InitialBlockDownloadStates() var trustedStateValidators = new[] { minerSwarm.Address }.ToImmutableHashSet(); await receiverSwarm.PreloadAsync(trustedStateValidators: trustedStateValidators); - await receiverSwarm.PreloadAsync(true); + await receiverSwarm.PreloadAsync(); var state = receiverChain.GetState(address); Assert.Equal((Text)"foo,bar,baz", state); @@ -2413,7 +2413,7 @@ public async Task HandleReorgInSynchronizing() await BootstrapAsync(miner2, miner1.AsPeer); await BootstrapAsync(receiver, miner1.AsPeer); - var t = receiver.PreloadAsync(render: true); + var t = receiver.PreloadAsync(); await miner1.BlockChain.MineBlock(miner1.Address); await miner2.BlockChain.MineBlock(miner2.Address); Block latest = await miner2.BlockChain.MineBlock(miner2.Address); diff --git a/Libplanet/Net/Swarm.cs b/Libplanet/Net/Swarm.cs index d142123f0e..28328fdf72 100644 --- a/Libplanet/Net/Swarm.cs +++ b/Libplanet/Net/Swarm.cs @@ -392,45 +392,7 @@ public string TraceTable() /// Thrown when the given the block downloading is /// failed and if is null. #pragma warning restore MEN002 // Line is too long - public Task PreloadAsync( - TimeSpan? dialTimeout = null, - IProgress progress = null, - IImmutableSet
trustedStateValidators = null, - EventHandler blockDownloadFailed = null, - CancellationToken cancellationToken = default(CancellationToken) - ) - { - return PreloadAsync( - render: false, - dialTimeout: dialTimeout, - progress: progress, - trustedStateValidators: trustedStateValidators, - blockDownloadFailed: blockDownloadFailed, - cancellationToken: cancellationToken - ); - } - - public async Task FindSpecificPeerAsync( - Address target, - Address searchAddress, - int depth, - BoundPeer viaPeer, - TimeSpan? timeout, - CancellationToken cancellationToken) - { - NetMQTransport netMQTransport = (NetMQTransport)_transport; - return await netMQTransport.FindSpecificPeerAsync( - target, - searchAddress, - depth, - viaPeer, - timeout, - cancellationToken); - } - - // FIXME: It is not guaranteed that states will be reported in order. see issue #436, #430 - internal async Task PreloadAsync( - bool render, + public async Task PreloadAsync( TimeSpan? dialTimeout = null, IProgress progress = null, IImmutableSet
trustedStateValidators = null, @@ -483,12 +445,15 @@ await DialToExistingPeers(dialTimeout, cancellationToken) "Try to download blocks from {EndPoint}@{Address}.", peerWithHeight.Peer.EndPoint, peerWithHeight.Peer.Address.ToHex()); + + // FIXME: It is not guaranteed that states will be reported in order. + // see issue #436, #430 await SyncBehindsBlocksFromPeerAsync( workspace, peerWithHeight, progress, cancellationToken, - render + false ); } catch (Exception e) @@ -533,14 +498,6 @@ await SyncBehindsBlocksFromPeerAsync( // it doesn't need to receive states from other peers at all. return; } - else if (render) - { - // If it's already rendered by SyncBehindsBlocksFromPeersAsync() method - // it means states are already calculated so that it does not need to receive - // calculated states from trusted peers. - complete = true; - return; - } long height = workspace.Tip.Index; @@ -552,6 +509,8 @@ await SyncBehindsBlocksFromPeerAsync( .OrderByDescending(pair => pair.Item2) .Select(pair => (pair.Item1, workspace[pair.Item2.Value].Hash)); + // FIXME: It is not guaranteed that states will be reported in order. + // see issue #436, #430 long? receivedStateHeight = await SyncRecentStatesFromTrustedPeersAsync( workspace, progress, @@ -610,6 +569,24 @@ await SyncBehindsBlocksFromPeerAsync( } } + public async Task FindSpecificPeerAsync( + Address target, + Address searchAddress, + int depth, + BoundPeer viaPeer, + TimeSpan? timeout, + CancellationToken cancellationToken) + { + NetMQTransport netMQTransport = (NetMQTransport)_transport; + return await netMQTransport.FindSpecificPeerAsync( + target, + searchAddress, + depth, + viaPeer, + timeout, + cancellationToken); + } + internal async Task AddPeersAsync( IEnumerable peers, TimeSpan? timeout, From 53fe205e97d5860e3af43fa4ccce172bd9bcf219 Mon Sep 17 00:00:00 2001 From: moreal Date: Tue, 14 Jan 2020 23:23:21 +0900 Subject: [PATCH 3/3] Update changelog --- CHANGES.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index c391e8ef21..d3e3417b07 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -112,6 +112,8 @@ To be released. relaying proxy concurrently. [[#744]] - `Swarm` became to throw `InvalidGenesisBlockException` when receiving block from the nodes that have a different genesis block. [[#746]] + - `Swarm` became to distinguish the starting stages clearly. In other words, + `Swarm.StartAsync()` became not to call `Swarm.PreloadAsync()`. [[#735], [#760]] ### Bug fixes @@ -179,6 +181,7 @@ To be released. [#728]: https://github.com/planetarium/libplanet/pull/728 [#732]: https://github.com/planetarium/libplanet/pull/732 [#734]: https://github.com/planetarium/libplanet/pull/734 +[#735]: https://github.com/planetarium/libplanet/issues/735 [#736]: https://github.com/planetarium/libplanet/pull/736 [#739]: https://github.com/planetarium/libplanet/pull/739 [#744]: https://github.com/planetarium/libplanet/pull/744 @@ -189,6 +192,7 @@ To be released. [#757]: https://github.com/planetarium/libplanet/pull/757 [#758]: https://github.com/planetarium/libplanet/pull/758 [#759]: https://github.com/planetarium/libplanet/pull/759 +[#760]: https://github.com/planetarium/libplanet/pull/760 Version 0.7.0