Skip to content

Commit

Permalink
Merge pull request #1068 from limebell/refactor/block-demand
Browse files Browse the repository at this point in the history
Introduce BlockDemand in Swarm
  • Loading branch information
limebell authored Nov 3, 2020
2 parents 515f3ce + ad052b4 commit d2e33e9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 32 deletions.
27 changes: 27 additions & 0 deletions Libplanet/Net/BlockDemand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#nullable enable
using Libplanet.Blocks;

namespace Libplanet.Net
{
/// <summary>
/// Struct represents <see cref="Swarm{T}"/>'s block demand status.
/// </summary>
public readonly struct BlockDemand
{
/// <summary>
/// The <see cref="BlockHeader"/> of the block to request.
/// </summary>
public readonly BlockHeader Header;

/// <summary>
/// The <see cref="BoundPeer"/> to request block hash from.
/// </summary>
public readonly BoundPeer Peer;

public BlockDemand(BlockHeader header, BoundPeer peer)
{
Header = header;
Peer = peer;
}
}
}
18 changes: 0 additions & 18 deletions Libplanet/Net/BlockHashDemand.cs

This file was deleted.

7 changes: 3 additions & 4 deletions Libplanet/Net/Swarm.MessageHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Libplanet.Net.Messages;
using Libplanet.Store;
using Libplanet.Tx;
using Serilog;
using Serilog.Events;

namespace Libplanet.Net
Expand Down Expand Up @@ -185,12 +184,12 @@ private async Task ProcessBlockHeader(
{
if (IsDemandNeeded(header))
{
Log.Debug(
_logger.Debug(
"BlockHashDemand #{index} {blockHash} from {peer}.",
header.Index,
ByteUtil.Hex(header.Hash),
peer);
_demandBlockHash = new BlockHashDemand(header, peer);
BlockDemand = new BlockDemand(header, peer);
}
else
{
Expand All @@ -199,7 +198,7 @@ private async Task ProcessBlockHeader(
"(current: {Current}, demand: {Demand}, received: {Received});" +
$" {nameof(BlockHeaderMessage)} is ignored.",
BlockChain.Tip.Index,
_demandBlockHash?.Header.Index,
BlockDemand?.Header.Index,
header.Index);
}
}
Expand Down
25 changes: 15 additions & 10 deletions Libplanet/Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public partial class Swarm<T> : IDisposable

private CancellationTokenSource _workerCancellationTokenSource;
private CancellationToken _cancellationToken;

private BlockHashDemand? _demandBlockHash;
private ConcurrentDictionary<TxId, BoundPeer> _demandTxIds;

static Swarm()
Expand Down Expand Up @@ -206,6 +204,13 @@ public IDictionary<Peer, DateTimeOffset> LastSeenTimestamps

public AppProtocolVersion AppProtocolVersion => _appProtocolVersion;

/// <summary>
/// Information of <see cref="Swarm{T}"/>'s demand for new blocks.
/// It is null when the <see cref="Swarm{T}"/> does not have any block to demand.
/// <seealso cref="BlockDemand"/>
/// </summary>
public BlockDemand? BlockDemand { get; private set; }

internal ITransport Transport { get; private set; }

internal IProtocol Protocol => (Transport as NetMQTransport)?.Protocol;
Expand Down Expand Up @@ -361,7 +366,7 @@ public async Task StartAsync(
_cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(
_workerCancellationTokenSource.Token, cancellationToken
).Token;
_demandBlockHash = null;
BlockDemand = null;
_demandTxIds = new ConcurrentDictionary<TxId, BoundPeer>();
await Transport.StartAsync(_cancellationToken);

Expand Down Expand Up @@ -1671,8 +1676,8 @@ private void BroadcastTxIds(Address? except, IEnumerable<TxId> txIds)
private bool IsDemandNeeded(BlockHeader target)
{
return target.TotalDifficulty > BlockChain.Tip.TotalDifficulty &&
(_demandBlockHash is null ||
_demandBlockHash.Value.Header.TotalDifficulty < target.TotalDifficulty);
(BlockDemand is null ||
BlockDemand.Value.Header.TotalDifficulty < target.TotalDifficulty);
}

private async Task SyncPreviousBlocksAsync(
Expand Down Expand Up @@ -1898,15 +1903,15 @@ CancellationToken cancellationToken
{
while (!cancellationToken.IsCancellationRequested)
{
if (_demandBlockHash is null ||
_demandBlockHash.Value.Header.TotalDifficulty <= BlockChain.Tip.TotalDifficulty)
if (BlockDemand is null ||
BlockDemand.Value.Header.TotalDifficulty <= BlockChain.Tip.TotalDifficulty)
{
await Task.Delay(1, cancellationToken);
continue;
}

BoundPeer peer = _demandBlockHash.Value.Peer;
var hash = new HashDigest<SHA256>(_demandBlockHash.Value.Header.Hash.ToArray());
BoundPeer peer = BlockDemand.Value.Peer;
var hash = new HashDigest<SHA256>(BlockDemand.Value.Header.Hash.ToArray());

try
{
Expand Down Expand Up @@ -1947,7 +1952,7 @@ await SyncPreviousBlocksAsync(
{
using (await _blockSyncMutex.LockAsync(cancellationToken))
{
_demandBlockHash = null;
BlockDemand = null;
}
}
}
Expand Down

0 comments on commit d2e33e9

Please sign in to comment.