Skip to content

Commit

Permalink
Merge pull request #1167 from moreal/bugfix/network/reset-block-deman…
Browse files Browse the repository at this point in the history
…d-on-correct-time

fix(net): reset block demand when not changed
  • Loading branch information
moreal authored and longfin committed Jan 29, 2021
1 parent 15ae43c commit 3870bc5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Libplanet.Tests/Menees.Analyzers.Settings.xml
Expand Up @@ -2,5 +2,5 @@
<Menees.Analyzers.Settings>
<MaxLineColumns>100</MaxLineColumns>
<MaxMethodLines>300</MaxMethodLines>
<MaxFileLines>2550</MaxFileLines>
<MaxFileLines>2600</MaxFileLines>
</Menees.Analyzers.Settings>
29 changes: 29 additions & 0 deletions Libplanet.Tests/Net/SwarmTest.cs
Expand Up @@ -2430,6 +2430,35 @@ public async Task BlockDemand()
}
}

[Fact]
public async Task ResetBlockDemandIfNotChanged()
{
var minerKey = new PrivateKey();
Swarm<DumbAction> sender = CreateSwarm();
Swarm<DumbAction> receiver = CreateSwarm();
for (var i = 0; i < 20; i++)
{
await sender.BlockChain.MineBlock(minerKey.ToAddress());
}

Block<DumbAction> 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<SHA256>(receiver.BlockDemand.Value.Header.Hash));
}

private async Task<Task> StartAsync<T>(
Swarm<T> swarm,
CancellationToken cancellationToken = default
Expand Down
20 changes: 13 additions & 7 deletions Libplanet/Net/Swarm.cs
Expand Up @@ -233,6 +233,8 @@ static Swarm()

internal AsyncAutoResetEvent FillBlocksAsyncFailed { get; } = new AsyncAutoResetEvent();

internal AsyncAutoResetEvent ProcessFillBlocksFinished { get; } = new AsyncAutoResetEvent();

internal SwarmOptions Options { get; }

/// <summary>
Expand Down Expand Up @@ -1716,8 +1718,9 @@ CancellationToken cancellationToken
continue;
}

BoundPeer peer = BlockDemand.Value.Peer;
var hash = new HashDigest<SHA256>(BlockDemand.Value.Header.Hash.ToArray());
BlockDemand blockDemand = BlockDemand.Value;
BoundPeer peer = blockDemand.Peer;
var hash = new HashDigest<SHA256>(blockDemand.Header.Hash.ToArray());

try
{
Expand Down Expand Up @@ -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();
}
}
}
Expand Down

0 comments on commit 3870bc5

Please sign in to comment.