Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

BootstrapOptions added to SwarmOptions #2024

Merged
merged 3 commits into from May 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGES.md
Expand Up @@ -10,12 +10,26 @@ To be released.

### Backward-incompatible API changes

- (Libplanet.Net) Removed `SwarmOptions.BootstrapDialTimeout`.
Use `SwarmOptions.BootstrapOptions.DialTimeout` instead. [[#2024]]
- (Libplanet.Net) Added `Swarm<T>.BootstrapAsync(CancellationToken)`
which utilizes values stored in `BootstrapOptions`. [[#2024]]
- (Libplanet.Net) Parameter name `depth` changed to `searchDepth` for
`Swarm<T>.BootstrapAsync(IEnumerable<Peer>, TimeSpan?, int,
CancellationToken)` and made non-optional. [[#2024]]
- (Libplanet.Net) `Swarm<T>.BootstrapAsync(IEnumerable<Peer>, depth,
CancellationToken)` removed. [[#2024]]

### Backward-incompatible network protocol changes

### Backward-incompatible storage format changes

### Added APIs

- (Libplanet.Net) Added `BootstrapOptions` class. [[#2024]]
- (Libplanet.Net) Added `BootstrapOptions` property to `SwarmOptions`.
[[#2024]]

### Behavioral changes

### Bug fixes
Expand All @@ -24,6 +38,8 @@ To be released.

### CLI tools

[#2024]: https://github.com/planetarium/libplanet/pull/2024


Version 0.36.1
--------------
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Explorer.Executable/Program.cs
Expand Up @@ -17,6 +17,7 @@
using Libplanet.Explorer.Interfaces;
using Libplanet.Explorer.Store;
using Libplanet.Net;
using Libplanet.Net.Protocols;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Libplanet.Tx;
Expand Down Expand Up @@ -370,6 +371,7 @@ private static BlockPolicy<T> LoadBlockPolicy<T>(Options options)
await swarm.BootstrapAsync(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using newly introduced Swarm<T>.BootstrapAsync(CancellationToken) here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. See 7e5fe07. We probably need to overhaul Program.cs at some point. 馃檭

seeds,
TimeSpan.FromMilliseconds(5000),
Kademlia.MaxDepth,
cancellationToken: cancellationToken
);
}
Expand Down
4 changes: 3 additions & 1 deletion Libplanet.Net.Tests/SwarmTest.cs
Expand Up @@ -225,7 +225,8 @@ public async Task BootstrapException()
await Assert.ThrowsAsync<PeerDiscoveryException>(
() => swarmB.BootstrapAsync(
new[] { swarmA.AsPeer },
TimeSpan.FromMilliseconds(3000)));
TimeSpan.FromMilliseconds(3000),
Kademlia.MaxDepth));

await StartAsync(swarmA);
}
Expand Down Expand Up @@ -1813,6 +1814,7 @@ private Task StopAsync<T>(Swarm<T> swarm)
await swarm.BootstrapAsync(
seeds,
dialTimeout: TimeSpan.FromSeconds(3),
searchDepth: Kademlia.MaxDepth,
cancellationToken: cancellationToken);
}

Expand Down
34 changes: 34 additions & 0 deletions Libplanet.Net/BootstrapOptions.cs
@@ -0,0 +1,34 @@
using System;
using System.Collections.Immutable;
using Libplanet.Net.Messages;
using Libplanet.Net.Protocols;
using Libplanet.Net.Transports;

namespace Libplanet.Net
{
public class BootstrapOptions
{
public const int DefaultDialTimeout = 15;

/// <summary>
/// Determines how long an <see cref="ITransport"/> should wait before timimg out
/// when dialing peers for either <see cref="Pong"/>, <see cref="Neighbors"/>,
/// or <see cref="ChainStatus"/> during a bootstrapping phase. Generally, a more relaxed
/// <see cref="TimeSpan"/> is used compared to <see cref="TimeoutOptions.DialTimeout"/>.
/// Set to <see cref="DefaultDialTimeout"/> seconds by default.
/// </summary>
/// <seealso cref="TimeoutOptions.DialTimeout"/>
public TimeSpan DialTimeout { get; set; }
= TimeSpan.FromSeconds(DefaultDialTimeout);

/// <summary>
/// The list of seed peers to connect to.
/// </summary>
public ImmutableList<BoundPeer> SeedPeers { get; set; } = ImmutableList<BoundPeer>.Empty;

/// <summary>
/// Determines the depth of the search when discovering neighbors for the local node.
/// </summary>
public int SearchDepth { get; set; } = Kademlia.MaxDepth;
}
}
22 changes: 8 additions & 14 deletions Libplanet.Net/Swarm.cs
Expand Up @@ -397,23 +397,17 @@ public async Task StartAsync(CancellationToken cancellationToken = default)
/// <summary>
/// Join to the peer-to-peer network using seed peers.
/// </summary>
/// <param name="seedPeers">List of seed peers.</param>
/// <param name="depth">Depth to find neighbors of current <see cref="Peer"/>
/// from seed peers.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification
/// that this operation should be canceled.</param>
/// <returns>An awaitable task without value.</returns>
/// <exception cref="SwarmException">Thrown when this <see cref="Swarm{T}"/> instance is
/// not <see cref="Running"/>.</exception>
public async Task BootstrapAsync(
IEnumerable<Peer> seedPeers,
int depth = Kademlia.MaxDepth,
CancellationToken cancellationToken = default)
public async Task BootstrapAsync(CancellationToken cancellationToken = default)
{
await BootstrapAsync(
seedPeers: seedPeers,
dialTimeout: Options.TimeoutOptions.BootstrapDialTimeout,
depth: depth,
seedPeers: Options.BootstrapOptions.SeedPeers,
dialTimeout: Options.BootstrapOptions.DialTimeout,
searchDepth: Options.BootstrapOptions.SearchDepth,
cancellationToken: cancellationToken);
}

Expand All @@ -422,8 +416,8 @@ public async Task StartAsync(CancellationToken cancellationToken = default)
/// </summary>
/// <param name="seedPeers">List of seed peers.</param>
/// <param name="dialTimeout">Timeout for connecting to peers.</param>
/// <param name="depth">Depth to find neighbors of current <see cref="Peer"/>
/// from seed peers.</param>
/// <param name="searchDepth">Maximum recursion depth when finding neighbors of
/// current <see cref="Peer"/> from seed peers.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification
/// that this operation should be canceled.</param>
/// <returns>An awaitable task without value.</returns>
Expand All @@ -432,7 +426,7 @@ public async Task StartAsync(CancellationToken cancellationToken = default)
public async Task BootstrapAsync(
IEnumerable<Peer> seedPeers,
TimeSpan? dialTimeout,
int depth = Kademlia.MaxDepth,
int searchDepth,
CancellationToken cancellationToken = default(CancellationToken))
{
if (seedPeers is null)
Expand All @@ -451,7 +445,7 @@ public async Task StartAsync(CancellationToken cancellationToken = default)
await PeerDiscovery.BootstrapAsync(
peers,
dialTimeout,
depth,
searchDepth,
cancellationToken);

if (!Transport.Running)
Expand Down
5 changes: 5 additions & 0 deletions Libplanet.Net/SwarmOptions.cs
Expand Up @@ -126,6 +126,11 @@ public enum TransportType : byte
/// </summary>
public TransportType Type { get; set; } = TransportType.TcpTransport;

/// <summary>
/// Various options for the default bootstrap behavior of <see cref="Swarm{T}"/>.
/// </summary>
public BootstrapOptions BootstrapOptions { get; set; } = new BootstrapOptions();

/// <summary>
/// Various timeout options for sending and receiving <see cref="Message"/>s through
/// an <see cref="ITransport"/>.
Expand Down
14 changes: 1 addition & 13 deletions Libplanet.Net/TimeoutOptions.cs
Expand Up @@ -15,7 +15,6 @@ namespace Libplanet.Net
public class TimeoutOptions
{
public const int DefaultMaxTimeout = 150;
public const int DefaultBootstrapDialTimeout = 15;
public const int DefaultPreloadDialTimeout = 5;
public const int DefaultDialTimeout = 1;
public const int DefaultGetBlockHashesTimeout = 30;
Expand All @@ -32,17 +31,6 @@ public class TimeoutOptions
public TimeSpan MaxTimeout { get; set; }
= TimeSpan.FromSeconds(DefaultMaxTimeout);

/// <summary>
/// Determines how long an <see cref="ITransport"/> should wait before timimg out
/// when dialing peers for either <see cref="Pong"/>, <see cref="Neighbors"/>,
/// or <see cref="ChainStatus"/> during a bootstrapping phase. Generally, a more relaxed
/// <see cref="TimeSpan"/> is used compared to <see cref="DialTimeout"/>.
/// Set to <see cref="DefaultBootstrapDialTimeout"/> seconds by default.
/// </summary>
/// <seealso cref="DialTimeout"/>
public TimeSpan BootstrapDialTimeout { get; set; }
= TimeSpan.FromSeconds(DefaultBootstrapDialTimeout);

/// <summary>
/// Determines how long an <see cref="ITransport"/> should wait before timimg out
/// when dialing peers for either <see cref="Pong"/>, <see cref="Neighbors"/>,
Expand All @@ -60,7 +48,7 @@ public class TimeoutOptions
/// or <see cref="ChainStatus"/> for a long running process.
/// Set to <see cref="DefaultDialTimeout"/> seconds by default.
/// </summary>
/// <seealso cref="BootstrapDialTimeout"/>
/// <seealso cref="BootstrapOptions.DialTimeout"/>
/// <seealso cref="PreloadDialTimeout"/>
public TimeSpan DialTimeout { get; set; }
= TimeSpan.FromSeconds(DefaultDialTimeout);
Expand Down