Class Swarm<T>
Namespace: Libplanet.Net
Assembly: Libplanet.dll
Syntax
public class Swarm<T> : IDisposable where T : IAction, new()
Type Parameters
| Name | Description |
|---|---|
| T |
Constructors
| Improve this Doc View SourceSwarm(BlockChain<T>, PrivateKey, AppProtocolVersion, Int32, String, Nullable<Int32>, IEnumerable<IceServer>, DifferentAppProtocolVersionEncountered, IEnumerable<PublicKey>, SwarmOptions)
Creates a Swarm<T>. This constructor in only itself does not start any communication with the network.
Declaration
public Swarm(BlockChain<T> blockChain, PrivateKey privateKey, AppProtocolVersion appProtocolVersion, int workers = 5, string host = null, int? listenPort = null, IEnumerable<IceServer> iceServers = null, DifferentAppProtocolVersionEncountered differentAppProtocolVersionEncountered = null, IEnumerable<PublicKey> trustedAppProtocolVersionSigners = null, SwarmOptions options = null)
Parameters
| Type | Name | Description |
|---|---|---|
| BlockChain<T> | blockChain | A blockchain to publicize on the network. |
| PrivateKey | privateKey | A private key to sign messages. The public part of this key become a part of its end address for being pointed by peers. |
| AppProtocolVersion | appProtocolVersion | An app protocol to comply. |
| Int32 | workers | The number of background workers (i.e., threads). |
| String | host | A hostname to be a part of a public endpoint, that peers use when they connect to this node. Note that this is not a hostname to listen to; Swarm<T> always listens to 0.0.0.0 & ::/0. |
| Nullable<Int32> | listenPort | A port number to listen to. |
| IEnumerable<IceServer> | iceServers | ICE servers to use for TURN/STUN. Purposes to traverse NAT. |
| DifferentAppProtocolVersionEncountered | differentAppProtocolVersionEncountered | A delegate called back when a peer
with one different from |
| IEnumerable<PublicKey> | trustedAppProtocolVersionSigners | PublicKeys of parties
to trust AppProtocolVersions they signed. To trust any party, pass
|
| SwarmOptions | options | Options for Swarm<T>. |
Properties
| Improve this Doc View SourceAddress
Declaration
public Address Address { get; }
Property Value
| Type | Description |
|---|---|
| Address |
AppProtocolVersion
Declaration
public AppProtocolVersion AppProtocolVersion { get; }
Property Value
| Type | Description |
|---|---|
| AppProtocolVersion |
AsPeer
Declaration
public Peer AsPeer { get; }
Property Value
| Type | Description |
|---|---|
| Peer |
BlockChain
The BlockChain<T> instance this Swarm<T> instance synchronizes with.
Declaration
public BlockChain<T> BlockChain { get; }
Property Value
| Type | Description |
|---|---|
| BlockChain<T> |
EndPoint
Declaration
public DnsEndPoint EndPoint { get; }
Property Value
| Type | Description |
|---|---|
| DnsEndPoint |
LastMessageTimestamp
The last time when any message was arrived.
It can be null if no message has been arrived yet.
Declaration
public DateTimeOffset? LastMessageTimestamp { get; }
Property Value
| Type | Description |
|---|---|
| Nullable<DateTimeOffset> |
LastSeenTimestamps
Declaration
public IDictionary<Peer, DateTimeOffset> LastSeenTimestamps { get; }
Property Value
| Type | Description |
|---|---|
| IDictionary<Peer, DateTimeOffset> |
Peers
Declaration
public IEnumerable<BoundPeer> Peers { get; }
Property Value
| Type | Description |
|---|---|
| IEnumerable<BoundPeer> |
Running
Declaration
public bool Running { get; }
Property Value
| Type | Description |
|---|---|
| Boolean |
TrustedAppProtocolVersionSigners
Declaration
public IImmutableSet<PublicKey> TrustedAppProtocolVersionSigners { get; }
Property Value
| Type | Description |
|---|---|
| IImmutableSet<PublicKey> |
Methods
| Improve this Doc View SourceBootstrapAsync(IEnumerable<Peer>, Double, Double, Int32, CancellationToken)
Declaration
public Task BootstrapAsync(IEnumerable<Peer> seedPeers, double pingSeedTimeout, double findPeerTimeout, int depth = null, CancellationToken cancellationToken = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<Peer> | seedPeers | |
| Double | pingSeedTimeout | |
| Double | findPeerTimeout | |
| Int32 | depth | |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |
BootstrapAsync(IEnumerable<Peer>, Nullable<TimeSpan>, Nullable<TimeSpan>, Int32, CancellationToken)
Join to the peer-to-peer network using seed peers.
Declaration
public Task BootstrapAsync(IEnumerable<Peer> seedPeers, TimeSpan? pingSeedTimeout, TimeSpan? findNeighborsTimeout, int depth = null, CancellationToken cancellationToken = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<Peer> | seedPeers | List of seed peers. |
| Nullable<TimeSpan> | pingSeedTimeout | Timeout for connecting to seed peers. |
| Nullable<TimeSpan> | findNeighborsTimeout | Timeout for requesting neighbors. |
| Int32 | depth | Depth to find neighbors of current Peer from seed peers. |
| CancellationToken | cancellationToken | A cancellation token used to propagate notification that this operation should be canceled. |
Returns
| Type | Description |
|---|---|
| Task | An awaitable task without value. |
Exceptions
| Type | Condition |
|---|---|
| SwarmException |
BroadcastBlock(Block<T>)
Declaration
public void BroadcastBlock(Block<T> block)
Parameters
| Type | Name | Description |
|---|---|---|
| Block<T> | block |
BroadcastTxs(IEnumerable<Transaction<T>>)
Declaration
public void BroadcastTxs(IEnumerable<Transaction<T>> txs)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<Transaction<T>> | txs |
CheckAllPeersAsync(Nullable<TimeSpan>, CancellationToken)
Validates all Peers in the routing table by sending a simple message.
Declaration
public Task CheckAllPeersAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Nullable<TimeSpan> | timeout | Timeout for this operation. If it is set to |
| CancellationToken | cancellationToken | A cancellation token used to propagate notification that this operation should be canceled. |
Returns
| Type | Description |
|---|---|
| Task | An awaitable task without value. |
Dispose()
Declaration
public void Dispose()
Finalize()
Declaration
protected void Finalize()
FindSpecificPeerAsync(Address, Int32, Nullable<TimeSpan>, CancellationToken)
Declaration
public Task<BoundPeer> FindSpecificPeerAsync(Address target, int depth = 3, TimeSpan? timeout = null, CancellationToken cancellationToken = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Address | target | The Address to find. |
| Int32 | depth | Target depth of recursive operation. If -1 is given,
will recursive until the closest BoundPeer to the
|
| Nullable<TimeSpan> | timeout |
|
| CancellationToken | cancellationToken | A cancellation token used to propagate notification that this operation should be canceled. |
Returns
| Type | Description |
|---|---|
| Task<BoundPeer> | A BoundPeer with Address of |
GetPeerChainStateAsync(Nullable<TimeSpan>, CancellationToken)
Gets the PeerChainState of the connected Peers.
Declaration
public Task<IEnumerable<PeerChainState>> GetPeerChainStateAsync(TimeSpan? dialTimeout, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| Nullable<TimeSpan> | dialTimeout | When the Swarm<T> tries to dial each peer in Peers,
the dial-up is cancelled after this timeout, and it tries another peer.
If |
| CancellationToken | cancellationToken | A cancellation token used to propagate notification that this operation should be canceled. |
Returns
| Type | Description |
|---|---|
| Task<IEnumerable<PeerChainState>> | PeerChainState of the connected Peers. |
GetTrustedStateCompleterAsync(IImmutableSet<Address>, Nullable<TimeSpan>, Nullable<StateCompleterSet<T>>, CancellationToken)
Gets state completers that complement incomplete block states by trusting
(i.e., download-and-reusing) states calculated by the given
trustedStateValidators. If no trustworthy states are available
incomplete states are complemented by recalculating
(i.e., Recalculate).
Declaration
public Task<StateCompleterSet<T>> GetTrustedStateCompleterAsync(IImmutableSet<Address> trustedStateValidators, TimeSpan? dialTimeout = null, StateCompleterSet<T>? fallbackCompleterSet = null, CancellationToken cancellationToken = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IImmutableSet<Address> | trustedStateValidators | If any peer in this set is reachable and there are no block states in the current node Swarm<T> tries to receive the needed states from that trusted peer, which is also calculated by that peer. If it fails to find or receive trustworthy states from peers it recalculates (i.e., Recalculate) states by executing actions by itself instead. Note that this option is intended to be exposed to end users through a feasible user interface so that they can decide whom to trust for themselves. |
| Nullable<TimeSpan> | dialTimeout | When the Swarm<T> tries to dial each peer in |
| Nullable<StateCompleterSet<T>> | fallbackCompleterSet | If no |
| CancellationToken | cancellationToken | A cancellation token to observe while waiting for dial-ups. |
Returns
| Type | Description |
|---|---|
| Task<StateCompleterSet<T>> | A set of state completers. |
PreloadAsync(Nullable<TimeSpan>, IProgress<PreloadState>, IImmutableSet<Address>, CancellationToken)
Preemptively downloads blocks from registered Peers.
Declaration
public Task PreloadAsync(TimeSpan? dialTimeout = null, IProgress<PreloadState> progress = null, IImmutableSet<Address> trustedStateValidators = null, CancellationToken cancellationToken = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Nullable<TimeSpan> | dialTimeout | When the Swarm<T> tries to dial each peer in Peers,
the dial-up is cancelled after this timeout, and it tries another peer.
If |
| IProgress<PreloadState> | progress | An instance that receives progress updates for block downloads. |
| IImmutableSet<Address> | trustedStateValidators | If any peer in this set is reachable, Swarm<T> receives the latest states of the major blockchain from that trusted peer, which is also calculated by that peer, instead of autonomously calculating the states from scratch. Note that this option is intended to be exposed to end users through a feasible user interface so that they can decide whom to trust for themselves. |
| CancellationToken | cancellationToken | A cancellation token used to propagate notification that this operation should be canceled. |
Returns
| Type | Description |
|---|---|
| Task | A task without value.
You only can |
Remarks
This does not render downloaded IActions, but fills states only.
StartAsync(Int32, Int32, IImmutableSet<Address>, CancellationToken)
Starts to periodically synchronize the BlockChain.
Declaration
public Task StartAsync(int millisecondsDialTimeout = 15000, int millisecondsBroadcastTxInterval = 5000, IImmutableSet<Address> trustedStateValidators = null, CancellationToken cancellationToken = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | millisecondsDialTimeout | When the Swarm<T> tries to dial each peer in Peers,
the dial-up is cancelled after this timeout, and it tries another peer.
If |
| Int32 | millisecondsBroadcastTxInterval | The time period of exchange of staged transactions. |
| IImmutableSet<Address> | trustedStateValidators | If any peer in this set is reachable, Swarm<T> receives the latest states of the major blockchain from that trusted peer, which is also calculated by that peer, instead of autonomously calculating the states from scratch. Note that this option is intended to be exposed to end users through a feasible user interface so that they can decide whom to trust for themselves. |
| CancellationToken | cancellationToken | A cancellation token used to propagate notification that this operation should be canceled. |
Returns
| Type | Description |
|---|---|
| Task | An awaitable task without value. |
Remarks
If the BlockChain has no blocks at all or there are long behind blocks to caught in the network this method could lead to unexpected behaviors, because this tries to render all actions in the behind blocks so that there are a lot of calls to methods of Renderers in a short period of time. This can lead a game startup slow. If you want to omit rendering of these actions in the behind blocks use PreloadAsync(Nullable<TimeSpan>, IProgress<PreloadState>, IImmutableSet<Address>, CancellationToken) method too.
Exceptions
| Type | Condition |
|---|---|
| SwarmException |
StartAsync(TimeSpan, TimeSpan, IImmutableSet<Address>, CancellationToken)
Starts to periodically synchronize the BlockChain.
Declaration
public Task StartAsync(TimeSpan dialTimeout, TimeSpan broadcastTxInterval, IImmutableSet<Address> trustedStateValidators = null, CancellationToken cancellationToken = null)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | dialTimeout | When the Swarm<T> tries to dial each peer in Peers,
the dial-up is cancelled after this timeout, and it tries another peer.
If |
| TimeSpan | broadcastTxInterval | The time period of exchange of staged transactions. |
| IImmutableSet<Address> | trustedStateValidators | If any peer in this set is reachable, Swarm<T> receives the latest states of the major blockchain from that trusted peer, which is also calculated by that peer, instead of autonomously calculating the states from scratch. Note that this option is intended to be exposed to end users through a feasible user interface so that they can decide whom to trust for themselves. |
| CancellationToken | cancellationToken | A cancellation token used to propagate notification that this operation should be canceled. |
Returns
| Type | Description |
|---|---|
| Task | An awaitable task without value. |
Remarks
If the BlockChain has no blocks at all or there are long behind blocks to caught in the network this method could lead to unexpected behaviors, because this tries to render all actions in the behind blocks so that there are a lot of calls to methods of Renderers in a short period of time. This can lead a game startup slow. If you want to omit rendering of these actions in the behind blocks use PreloadAsync(Nullable<TimeSpan>, IProgress<PreloadState>, IImmutableSet<Address>, CancellationToken) method too.
Exceptions
| Type | Condition |
|---|---|
| SwarmException |
StopAsync(CancellationToken)
Declaration
public Task StopAsync(CancellationToken cancellationToken = null)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |
StopAsync(TimeSpan, CancellationToken)
Declaration
public Task StopAsync(TimeSpan waitFor, CancellationToken cancellationToken = null)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | waitFor | |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |
TraceTable()
Declaration
public string TraceTable()
Returns
| Type | Description |
|---|---|
| String |
WaitForRunningAsync()
Waits until this Swarm<T> instance gets started to run.
Declaration
public Task WaitForRunningAsync()
Returns
| Type | Description |
|---|---|
| Task | A |