Skip to content

Commit

Permalink
Merge pull request #418 from longfin/bugfix/skip-preload
Browse files Browse the repository at this point in the history
Skip PreloadAsync when there is no suitable peer
  • Loading branch information
dahlia authored Aug 9, 2019
2 parents 54d91b8 + b99d066 commit 699e3ac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ To be released.
when the connection had reset by a remote peer. [[#414]]
- Fixed a bug that `Swarm<T>` had hung forever after a remote peer had
disconnected while receiving. [[#416]]
- Fixed a bug that `Swarm<T>.PreloadAsync()` had been processed even if there
is no appropriate peer. [[#418]]

[#319]: https://github.com/planetarium/libplanet/issues/319
[#343]: https://github.com/planetarium/libplanet/pull/343
Expand Down Expand Up @@ -119,6 +121,7 @@ To be released.
[#414]: https://github.com/planetarium/libplanet/pull/414
[#416]: https://github.com/planetarium/libplanet/pull/416
[#417]: https://github.com/planetarium/libplanet/pull/417
[#418]: https://github.com/planetarium/libplanet/pull/418
[LiteDB #1268]: https://github.com/mbdavid/LiteDB/issues/1268


Expand Down
15 changes: 11 additions & 4 deletions Libplanet/Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,17 @@ internal async Task PreloadAsync(
trustedStateValidators = ImmutableHashSet<Address>.Empty;
}

IList<(Peer, long? TipIndex)> peersWithHeight =
await DialToExistingPeers(cancellationToken).Select(pp =>
(pp.Item1, pp.Item2.TipIndex)
).ToListAsync(cancellationToken);

if (!peersWithHeight.Any())
{
_logger.Information("There is no appropriate peer for preloading.");
return;
}

Block<T> initialTip = _blockChain.Tip;

// As preloading takes long, the blockchain data can corrupt if a program suddenly
Expand All @@ -587,10 +598,6 @@ internal async Task PreloadAsync(
? _blockChain.Fork(tip.Hash)
: new BlockChain<T>(_blockChain.Policy, _blockChain.Store, Guid.NewGuid());

IList<(Peer, long? TipIndex)> peersWithHeight =
await DialToExistingPeers(cancellationToken).Select(pp =>
(pp.Item1, pp.Item2.TipIndex)
).ToListAsync(cancellationToken);
await SyncBehindsBlocksFromPeersAsync(
workspace,
peersWithHeight,
Expand Down

0 comments on commit 699e3ac

Please sign in to comment.