Skip to content

Commit

Permalink
BootstrapOptions added to SwarmOptions
Browse files Browse the repository at this point in the history
Default removed from searchDepth
  • Loading branch information
greymistcube committed May 30, 2022
1 parent 2086fb9 commit 831c2b5
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 28 deletions.
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(
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

0 comments on commit 831c2b5

Please sign in to comment.