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 the canonical chain is deleted #675

Merged
merged 3 commits into from
Nov 14, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ To be released.
### Behavioral changes

### Bug fixes
- Fixed a bug where the canonical chain could be deleted if `Swarm<T>` failed
to download blocks due to network connection. [[#675]]

[#662]: https://github.com/planetarium/libplanet/pull/662
[#675]: https://github.com/planetarium/libplanet/pull/675


Version 0.7.0
Expand Down
34 changes: 34 additions & 0 deletions Libplanet.Tests/Net/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2388,6 +2388,40 @@ public async Task CreateNewChainWhenBranchPointNotExist()
}
}

[Fact(Timeout = Timeout)]
public async Task DoNotDeleteCanonicalChainWhenBlockDownloadFailed()
{
var chainA = _blockchains[0];
var chainB = _blockchains[1];

var swarmA = _swarms[0];
var swarmB = _swarms[1];

var genesis = await chainA.MineBlock(_fx1.Address1);
chainB.Append(genesis);

var block = await chainA.MineBlock(_fx1.Address1);

try
{
await StartAsync(swarmA);
await StartAsync(swarmB);
await BootstrapAsync(swarmA, swarmB.AsPeer);

swarmA.BroadcastBlocks(new[] { block });
await swarmB.FillBlocksAsyncStarted.WaitAsync();
await swarmA.StopAsync();
await swarmB.FillBlocksAsyncFailed.WaitAsync();

Assert.NotEmpty(chainB.GetState(_fx1.Address1));
}
finally
{
await swarmA.StopAsync();
await swarmB.StopAsync();
}
}

private static async Task<(Address, Block<DumbAction>[])>
MakeFixtureBlocksForPreloadAsyncCancellationTest()
{
Expand Down
21 changes: 8 additions & 13 deletions Libplanet/Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ private set

internal int FindNextHashesChunkSize { get; set; } = 500;

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

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

/// <summary>
/// Waits until this <see cref="Swarm{T}"/> instance gets started to run.
/// </summary>
Expand Down Expand Up @@ -1560,18 +1564,7 @@ CancellationToken cancellationToken
);
break;
}

// We can't recover with OperationCanceledException and
// ObjectDisposedException. so just re-throw them.
catch (ObjectDisposedException)
{
throw;
}
catch (OperationCanceledException)
{
throw;
}
dahlia marked this conversation as resolved.
Show resolved Hide resolved
catch (Exception e)
catch (TimeoutException e)
{
if (retry > 0)
{
Expand Down Expand Up @@ -1678,6 +1671,7 @@ private async Task<BlockChain<T>> FillBlocksAsync(
CancellationToken cancellationToken
)
{
FillBlocksAsyncStarted.Set();
BlockChain<T> workspace = blockChain;
var scope = new List<Guid>();

Expand Down Expand Up @@ -1791,12 +1785,13 @@ await GetBlocksAsync(peer, hashesAsArray)
}
catch
{
if (!(workspace is null))
if (!(workspace is null) && !workspace.Id.Equals(BlockChain.Id))
{
IStore store = blockChain.Store;
store.DeleteChainId(workspace.Id);
}

FillBlocksAsyncFailed.Set();
throw;
}
finally
Expand Down
2 changes: 1 addition & 1 deletion Menees.Analyzers.Settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<Menees.Analyzers.Settings>
<MaxLineColumns>100</MaxLineColumns>
<MaxMethodLines>200</MaxMethodLines>
<MaxFileLines>2500</MaxFileLines>
<MaxFileLines>2600</MaxFileLines>
</Menees.Analyzers.Settings>