Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(net): reset block demand when not changed #1167

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -2429,6 +2429,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 @@ -1732,8 +1734,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 @@ -1773,11 +1776,14 @@ CancellationToken cancellationToken
{
using (await _blockSyncMutex.LockAsync(cancellationToken))
{
// FIXME: Should only reset when BlockDemand has not changed
// from the beginning of this operation.
Comment on lines -1776 to -1777
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@limebell I removed the lines also 🙏

_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