Skip to content

Commit

Permalink
Merge pull request #905 from earlbread/fix-demand-block-hashes
Browse files Browse the repository at this point in the history
Demand block hashes from branch point
  • Loading branch information
earlbread authored Jun 12, 2020
2 parents 3455740 + bc6a641 commit f4c1068
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Version 0.9.5

To be released.

- 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 @@ -1208,6 +1208,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 @@ -1055,6 +1055,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 @@ -1092,6 +1095,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 @@ -1102,7 +1113,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 f4c1068

Please sign in to comment.