diff --git a/Libplanet.Tests/Menees.Analyzers.Settings.xml b/Libplanet.Tests/Menees.Analyzers.Settings.xml index 592faa78276..8af8c63e109 100644 --- a/Libplanet.Tests/Menees.Analyzers.Settings.xml +++ b/Libplanet.Tests/Menees.Analyzers.Settings.xml @@ -2,5 +2,5 @@ 100 300 - 2550 + 2600 diff --git a/Libplanet.Tests/Net/SwarmTest.cs b/Libplanet.Tests/Net/SwarmTest.cs index a4231f0abda..17959b1cf15 100644 --- a/Libplanet.Tests/Net/SwarmTest.cs +++ b/Libplanet.Tests/Net/SwarmTest.cs @@ -2430,6 +2430,35 @@ public async Task BlockDemand() } } + [Fact] + public async Task ResetBlockDemandIfNotChanged() + { + var minerKey = new PrivateKey(); + Swarm sender = CreateSwarm(); + Swarm receiver = CreateSwarm(); + for (var i = 0; i < 20; i++) + { + await sender.BlockChain.MineBlock(minerKey.ToAddress()); + } + + Block lowerBlock = sender.BlockChain[19], + higherBlock = sender.BlockChain[20]; + + await StartAsync(sender); + await StartAsync(receiver); + await BootstrapAsync(sender, receiver.AsPeer); + + sender.BroadcastBlock(lowerBlock); + await receiver.FillBlocksAsyncStarted.WaitAsync(); + sender.BroadcastBlock(higherBlock); + await receiver.ProcessFillBlocksFinished.WaitAsync(); + + Assert.NotNull(receiver.BlockDemand); + Assert.Equal( + higherBlock.Hash, + new HashDigest(receiver.BlockDemand.Value.Header.Hash)); + } + private async Task StartAsync( Swarm swarm, CancellationToken cancellationToken = default diff --git a/Libplanet/Net/Swarm.cs b/Libplanet/Net/Swarm.cs index e798cf19925..30b2a87efe5 100644 --- a/Libplanet/Net/Swarm.cs +++ b/Libplanet/Net/Swarm.cs @@ -233,6 +233,8 @@ static Swarm() internal AsyncAutoResetEvent FillBlocksAsyncFailed { get; } = new AsyncAutoResetEvent(); + internal AsyncAutoResetEvent ProcessFillBlocksFinished { get; } = new AsyncAutoResetEvent(); + internal SwarmOptions Options { get; } /// @@ -1716,8 +1718,9 @@ CancellationToken cancellationToken continue; } - BoundPeer peer = BlockDemand.Value.Peer; - var hash = new HashDigest(BlockDemand.Value.Header.Hash.ToArray()); + BlockDemand blockDemand = BlockDemand.Value; + BoundPeer peer = blockDemand.Peer; + var hash = new HashDigest(blockDemand.Header.Hash.ToArray()); try { @@ -1757,11 +1760,14 @@ CancellationToken cancellationToken { using (await _blockSyncMutex.LockAsync(cancellationToken)) { - // FIXME: Should only reset when BlockDemand has not changed - // from the beginning of this operation. - _logger.Debug($"{nameof(ProcessFillBlocks)}() finished. " + - $"Reset {nameof(BlockDemand)}..."); - BlockDemand = null; + _logger.Debug($"{nameof(ProcessFillBlocks)}() finished."); + if (BlockDemand.Equals(blockDemand)) + { + _logger.Debug($"Reset {nameof(BlockDemand)}..."); + BlockDemand = null; + } + + ProcessFillBlocksFinished.Set(); } } }