Skip to content

Commit

Permalink
Simplified FindBranchpoint() for better comprehension
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Sep 6, 2024
1 parent 2e03da3 commit 0ae6b36
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 34 deletions.
11 changes: 3 additions & 8 deletions src/Libplanet.Net/Swarm.MessageHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,13 @@ private Task ProcessMessageHandlerAsync(Message message)
"Received a {MessageType} message locator [{LocatorHead}]",
nameof(GetBlockHashesMsg),
getBlockHashes.Locator.Hash);
BlockChain.FindNextHashes(
IReadOnlyList<BlockHash> hashes = BlockChain.FindNextHashes(
getBlockHashes.Locator,
FindNextHashesChunkSize
).Deconstruct(
out long? offset,
out IReadOnlyList<BlockHash> hashes
);
FindNextHashesChunkSize);
_logger.Debug(
"Found {HashCount} hashes after the branchpoint (offset: {Offset}) " +
"Found {HashCount} hashes after the branchpoint " +
"with locator [{LocatorHead}]",
hashes.Count,
offset,
getBlockHashes.Locator.Hash);
var reply = new BlockHashesMsg(hashes);

Expand Down
20 changes: 7 additions & 13 deletions src/Libplanet/Blockchain/BlockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ public IImmutableSet<TxId> GetStagedTransactionIds()
/// including the branch point <see cref="BlockHash"/>. If no branch point is found,
/// returns a tuple of <see langword="null"/> and an empty array of
/// <see cref="BlockHash"/>es.</returns>
public Tuple<long?, IReadOnlyList<BlockHash>> FindNextHashes(
public IReadOnlyList<BlockHash> FindNextHashes(
BlockLocator locator,
int count = 500)
{
Expand All @@ -675,12 +675,12 @@ public IImmutableSet<TxId> GetStagedTransactionIds()

if (!(FindBranchpoint(locator) is { } branchpoint))
{
return new Tuple<long?, IReadOnlyList<BlockHash>>(null, Array.Empty<BlockHash>());
return Array.Empty<BlockHash>();
}

if (!(Store.GetBlockIndex(branchpoint) is { } branchpointIndex))
{
return new Tuple<long?, IReadOnlyList<BlockHash>>(null, Array.Empty<BlockHash>());
return Array.Empty<BlockHash>();
}

var result = new List<BlockHash>();
Expand All @@ -705,7 +705,7 @@ public IImmutableSet<TxId> GetStagedTransactionIds()
Store.ListChainIds().Count(),
stopwatch.ElapsedMilliseconds);

return new Tuple<long?, IReadOnlyList<BlockHash>>(branchpointIndex, result);
return result;
}

/// <summary>
Expand Down Expand Up @@ -1194,19 +1194,13 @@ internal void AppendStateRootHashPreceded(
{
_rwlock.EnterReadLock();

_logger.Debug(
"Finding a branchpoint with locator [{LocatorHead}]",
locator.Hash);
BlockHash hash = locator.Hash;
if (_blocks.ContainsKey(hash)
&& _blocks[hash] is Block block
&& hash.Equals(Store.IndexBlockHash(Id, block.Index)))
if (ContainsBlock(locator.Hash))
{
_logger.Debug(
"Found a branchpoint with locator [{LocatorHead}]: {Hash}",
locator.Hash,
hash);
return hash;
locator.Hash);
return locator.Hash;
}

_logger.Debug(
Expand Down
17 changes: 4 additions & 13 deletions test/Libplanet.Tests/Blockchain/BlockChainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,9 @@ public void RenderActionsAfterAppendComplete()
public void FindNextHashes()
{
var key = new PrivateKey();
long? offsetIndex;
IReadOnlyList<BlockHash> hashes;

_blockChain.FindNextHashes(
new BlockLocator(_blockChain.Genesis.Hash))
.Deconstruct(out offsetIndex, out hashes);
hashes = _blockChain.FindNextHashes(new BlockLocator(_blockChain.Genesis.Hash));
Assert.Single(hashes);
Assert.Equal(_blockChain.Genesis.Hash, hashes.First());
var block0 = _blockChain.Genesis;
Expand All @@ -451,19 +448,13 @@ public void FindNextHashes()
key, lastCommit: CreateBlockCommit(_blockChain.Tip));
_blockChain.Append(block3, CreateBlockCommit(block3));

_blockChain.FindNextHashes(new BlockLocator(block0.Hash))
.Deconstruct(out offsetIndex, out hashes);
Assert.Equal(0, offsetIndex);
hashes = _blockChain.FindNextHashes(new BlockLocator(block0.Hash));
Assert.Equal(new[] { block0.Hash, block1.Hash, block2.Hash, block3.Hash }, hashes);

_blockChain.FindNextHashes(new BlockLocator(block1.Hash))
.Deconstruct(out offsetIndex, out hashes);
Assert.Equal(1, offsetIndex);
hashes = _blockChain.FindNextHashes(new BlockLocator(block1.Hash));
Assert.Equal(new[] { block1.Hash, block2.Hash, block3.Hash }, hashes);

_blockChain.FindNextHashes(new BlockLocator(block0.Hash), count: 2)
.Deconstruct(out offsetIndex, out hashes);
Assert.Equal(0, offsetIndex);
hashes = _blockChain.FindNextHashes(new BlockLocator(block0.Hash), count: 2);
Assert.Equal(new[] { block0.Hash, block1.Hash }, hashes);
}

Expand Down

0 comments on commit 0ae6b36

Please sign in to comment.