Skip to content

Commit

Permalink
Merge pull request #3920 from greymistcube/refactor/get-block-demand-…
Browse files Browse the repository at this point in the history
…hashes

♻️ Refactor `GetDemandBlockHashes()`
  • Loading branch information
greymistcube authored Aug 22, 2024
2 parents 1891fae + b23b7d3 commit cb2deed
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 192 deletions.
19 changes: 9 additions & 10 deletions src/Libplanet.Net/Swarm.BlockCandidate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,24 @@ private BlockChain AppendPreviousBlocks(
bool render,
IProgress<BlockSyncState> progress)
{
BlockChain workspace = blockChain;
BlockChain workspace;
List<Guid> scope = new List<Guid>();
bool forked = false;

Block oldTip = workspace.Tip;
Block newTip = candidate.Blocks.Last().Item1;
Block oldTip = blockChain.Tip;
List<(Block, BlockCommit)> blocks = candidate.Blocks.ToList();
Block branchpoint = FindBranchpoint(
oldTip,
newTip,
blocks.Select(pair => pair.Item1).ToList());

if (branchpoint.Equals(oldTip))
{
_logger.Debug(
"No need to fork. at {MethodName}()",
nameof(AppendPreviousBlocks));
workspace = blockChain;
}
else if (!workspace.ContainsBlock(branchpoint.Hash))
else if (!blockChain.ContainsBlock(branchpoint.Hash))
{
// FIXME: This behavior can unexpectedly terminate the swarm (and the game
// app) if it encounters a peer having a different blockchain, and therefore
Expand All @@ -179,15 +178,15 @@ private BlockChain AppendPreviousBlocks(
throw new InvalidGenesisBlockException(
msg,
branchpoint.Hash,
workspace.Genesis.Hash);
blockChain.Genesis.Hash);
}
else
{
_logger.Debug(
"Trying to fork... at {MethodName}()",
nameof(AppendPreviousBlocks)
);
workspace = workspace.Fork(branchpoint.Hash);
workspace = blockChain.Fork(branchpoint.Hash);
forked = true;
scope.Add(workspace.Id);
_logger.Debug(
Expand Down Expand Up @@ -274,8 +273,9 @@ private BlockChain AppendPreviousBlocks(
return workspace;
}

private Block FindBranchpoint(Block oldTip, Block newTip, List<Block> newBlocks)
private Block FindBranchpoint(Block oldTip, List<Block> newBlocks)
{
var newTip = newBlocks.Last();
while (oldTip.Index > newTip.Index &&
oldTip.PreviousHash is { } aPrev)
{
Expand Down Expand Up @@ -440,14 +440,13 @@ private async Task<bool> BlockCandidateDownload(
BlockLocator locator = blockChain.GetBlockLocator();
Block tip = blockChain.Tip;

IAsyncEnumerable<Tuple<long, BlockHash>> hashesAsync = GetBlockHashes(
List<(long, BlockHash)> hashes = await GetBlockHashes(
peer: peer,
locator: locator,
stop: stop.Hash,
timeout: null,
logSessionIds: (logSessionId, subSessionId),
cancellationToken: cancellationToken);
IEnumerable<Tuple<long, BlockHash>> hashes = await hashesAsync.ToArrayAsync();

if (!hashes.Any())
{
Expand Down
7 changes: 3 additions & 4 deletions src/Libplanet.Net/Swarm.BlockSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,13 @@ private async Task PullBlocksAsync(
completionPredicate: BlockChain.ContainsBlock,
window: InitialBlockDownloadWindow
);
var demandBlockHashes = GetDemandBlockHashes(
var demandBlockHashes = await GetDemandBlockHashes(
BlockChain,
peersWithBlockExcerpt,
chunkSize,
progress,
cancellationToken
).WithCancellation(cancellationToken);
await foreach ((long index, BlockHash hash) in demandBlockHashes)
cancellationToken);
foreach ((long index, BlockHash hash) in demandBlockHashes)
{
cancellationToken.ThrowIfCancellationRequested();

Expand Down
Loading

0 comments on commit cb2deed

Please sign in to comment.