Skip to content

Commit

Permalink
Added worker task to broadcast chain tip repeatedly.
Browse files Browse the repository at this point in the history
  • Loading branch information
longfin committed Jun 23, 2021
1 parent 2eaab5a commit 355c606
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ To be released.
[[#1294], [#1328]]
- Added `IStore.DeleteTxIdBlockHashIndex(TxId, BlockHash)` method.
[[#1294], [#1328]]
- Added `IStore.IterateTxIdBlockHashIndex(TxId)` method.
- Added `IStore.IterateTxIdBlockHashIndex(TxId)` method.
[[#1294], [#1328]]
- `Swarm<T>.StartAsync()` method became to receive `broadcastBlockInterval`
(or `millisecondsBroadcastBlockInterval`) parameter. [[#TBD]]

### Backward-incompatible network protocol changes

Expand Down
41 changes: 40 additions & 1 deletion Libplanet/Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,12 @@ public async Task StopAsync(
/// the dial-up is cancelled after this timeout, and it tries another peer.
/// If <c>null</c> is given it never gives up dial-ups.
/// </param>
/// <param name="millisecondsBroadcastTxInterval">
/// <param name="millisecondsBroadcastBlockInterval">
/// The time period of exchange of staged transactions.
/// </param>
/// <param name="millisecondsBroadcastTxInterval">
/// The time period of broadcasting chain tip.
/// </param>
/// <param name="cancellationToken">
/// A cancellation token used to propagate notification that this
/// operation should be canceled.
Expand All @@ -287,11 +290,13 @@ public async Task StopAsync(
/// /> method too.</remarks>
public async Task StartAsync(
int millisecondsDialTimeout = 15000,
int millisecondsBroadcastBlockInterval = 5000,
int millisecondsBroadcastTxInterval = 5000,
CancellationToken cancellationToken = default(CancellationToken))
{
await StartAsync(
TimeSpan.FromMilliseconds(millisecondsDialTimeout),
TimeSpan.FromMilliseconds(millisecondsBroadcastBlockInterval),
TimeSpan.FromMilliseconds(millisecondsBroadcastTxInterval),
cancellationToken
);
Expand All @@ -305,6 +310,8 @@ await StartAsync(
/// the dial-up is cancelled after this timeout, and it tries another peer.
/// If <c>null</c> is given it never gives up dial-ups.
/// </param>
/// <param name="broadcastBlockInterval">The time period of broadcasting chain tip.
/// </param>
/// <param name="broadcastTxInterval">The time period of exchange of staged transactions.
/// </param>
/// <param name="cancellationToken">
Expand All @@ -324,6 +331,7 @@ await StartAsync(
/// /> method too.</remarks>
public async Task StartAsync(
TimeSpan dialTimeout,
TimeSpan broadcastBlockInterval,
TimeSpan broadcastTxInterval,
CancellationToken cancellationToken = default(CancellationToken))
{
Expand Down Expand Up @@ -355,6 +363,7 @@ public async Task StartAsync(
_cancellationToken));
tasks.Add(RebuildConnectionAsync(TimeSpan.FromMinutes(30), _cancellationToken));
tasks.Add(Transport.RunAsync(_cancellationToken));
tasks.Add(BroadcastBlockAsync(broadcastBlockInterval, _cancellationToken));
tasks.Add(BroadcastTxAsync(broadcastTxInterval, _cancellationToken));
tasks.Add(ProcessFillBlocks(dialTimeout, _cancellationToken));
tasks.Add(ProcessFillTxs(_cancellationToken));
Expand Down Expand Up @@ -1443,6 +1452,36 @@ private void PreloadExecuteActions(
spent);
}

private async Task BroadcastBlockAsync(
TimeSpan broadcastBlockInterval,
CancellationToken cancellationToken)
{
const string fname = nameof(BroadcastBlockAsync);
while (!cancellationToken.IsCancellationRequested)
{
try
{
await Task.Delay(broadcastBlockInterval, cancellationToken);
if (BlockChain.Tip is { } tip)
{
BroadcastBlock(tip);
}
}
catch (OperationCanceledException e)
{
_logger.Warning(e, $"{fname}() is canceled.");
throw;
}
catch (Exception e)
{
_logger.Error(
e,
$"An unexpected exception occurred during {fname}(): {e}"
);
}
}
}

private async Task BroadcastTxAsync(
TimeSpan broadcastTxInterval,
CancellationToken cancellationToken)
Expand Down

0 comments on commit 355c606

Please sign in to comment.