Skip to content

Commit

Permalink
Merge tag '0.9.5' into merge-0.9.5
Browse files Browse the repository at this point in the history
  • Loading branch information
earlbread committed Jun 12, 2020
2 parents 02daf27 + b8b5b25 commit 1b1e5e8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
11 changes: 11 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ To be released.
[#902]: https://github.com/planetarium/libplanet/pull/902


Version 0.9.5
-------------

Released on June 12, 2020.

- Fixed a bug that had not properly received block hashes after the chain had reorged.
[[#880], [#905]]

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


Version 0.9.4
--------------

Expand Down
45 changes: 45 additions & 0 deletions Libplanet.Tests/Net/SwarmTest.Preload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,51 @@ public async Task GetDemandBlockHashes()
Assert.Equal(expectedBlocks, demands);
}

[Fact]
public async Task PreloadAfterReorg()
{
Swarm<DumbAction> minerSwarm = _swarms[0];
Swarm<DumbAction> receiverSwarm = _swarms[1];

BlockChain<DumbAction> minerChain = _blockchains[0];
BlockChain<DumbAction> receiverChain = _blockchains[1];

foreach (int i in Enumerable.Range(0, 25))
{
Block<DumbAction> block = await minerChain.MineBlock(_fx1.Address1);
receiverChain.Append(block);
}

var receiverForked = receiverChain.Fork(receiverChain[5].Hash);
foreach (int i in Enumerable.Range(0, 20))
{
await receiverForked.MineBlock(_fx1.Address1);
}

receiverChain.Swap(receiverForked, false);

foreach (int i in Enumerable.Range(0, 1))
{
await minerChain.MineBlock(_fx1.Address1);
}

minerSwarm.FindNextHashesChunkSize = 1;
try
{
await StartAsync(minerSwarm);
await receiverSwarm.AddPeersAsync(new[] { minerSwarm.AsPeer }, null);
await receiverSwarm.PreloadAsync(
trustedStateValidators: new[] { minerSwarm.Address }.ToImmutableHashSet()
);
}
finally
{
await StopAsync(minerSwarm);
}

Assert.Equal(minerChain.BlockHashes, receiverChain.BlockHashes);
}

[Fact(Timeout = Timeout)]
public async Task GetDemandBlockHashesDuringReorg()
{
Expand Down
13 changes: 12 additions & 1 deletion Libplanet/Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,9 @@ [EnumeratorCancellation] CancellationToken cancellationToken
i++;
long peerIndex = peerHeight ?? -1;

long branchIndex = -1;
HashDigest<SHA256> branchPoint = default;

// FIXME: The following condition should be fixed together when the issue #459 is
// fixed. https://github.com/planetarium/libplanet/issues/459
if (peer is null || currentTipIndex >= peerIndex)
Expand Down Expand Up @@ -1103,6 +1106,14 @@ [EnumeratorCancellation] CancellationToken cancellationToken

IAsyncEnumerable<Tuple<long, HashDigest<SHA256>>> blockHashes =
GetBlockHashes(peer, locator, null, cancellationToken);

if (branchIndex == -1 &&
await blockHashes.FirstAsync(cancellationToken) is { } t)
{
t.Deconstruct(out branchIndex, out branchPoint);
totalBlocksToDownload = peerIndex - branchIndex;
}

await foreach (Tuple<long, HashDigest<SHA256>> pair in blockHashes)
{
pair.Deconstruct(out long dlIndex, out HashDigest<SHA256> dlHash);
Expand All @@ -1113,7 +1124,7 @@ [EnumeratorCancellation] CancellationToken cancellationToken
dlHash
);

if (downloaded.Contains(dlHash) || blockChain.ContainsBlock(dlHash))
if (downloaded.Contains(dlHash) || dlHash.Equals(branchPoint))
{
continue;
}
Expand Down

0 comments on commit 1b1e5e8

Please sign in to comment.