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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PeerChainState #936

Merged
merged 7 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ To be released.
- Added `BlockChain<T>.GetBalance()` method. [[#861], [#900]]
- Added `Block<T>.TotalDifficulty` property. [[#666], [#917]]
- Added `SwarmOptions` class. [[#926]]
- Added `PeerChainState` struct. [[#936]]
- Added `Swarm<T>.GetPeerChainStateAsync()` method. [[#936]]

### Behavioral changes

Expand Down Expand Up @@ -154,6 +156,7 @@ To be released.
[#930]: https://github.com/planetarium/libplanet/pull/930
[#932]: https://github.com/planetarium/libplanet/pull/932
[#933]: https://github.com/planetarium/libplanet/pull/933
[#936]: https://github.com/planetarium/libplanet/pull/936
[sleep mode]: https://en.wikipedia.org/wiki/Sleep_mode


Expand Down
52 changes: 52 additions & 0 deletions Libplanet.Tests/Net/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2016,6 +2016,58 @@ public async Task DoNotFillMultipleTimes()
}
}

[Fact(Timeout = Timeout)]
public async Task GetPeerChainStateAsync()
{
Swarm<DumbAction> swarm1 = _swarms[0];
Swarm<DumbAction> swarm2 = _swarms[1];
Swarm<DumbAction> swarm3 = _swarms[2];

var peerChainState = await swarm1.GetPeerChainStateAsync(
TimeSpan.FromSeconds(1), default);
Assert.Empty(peerChainState);

try
{
await StartAsync(swarm2);
await StartAsync(swarm3);

await BootstrapAsync(swarm1, swarm2.AsPeer);

peerChainState = await swarm1.GetPeerChainStateAsync(
TimeSpan.FromSeconds(1), default);
Assert.Equal(
new PeerChainState((BoundPeer)swarm2.AsPeer, 0, 0),
peerChainState.First()
);

await swarm2.BlockChain.MineBlock(_fx1.Address1);
peerChainState = await swarm1.GetPeerChainStateAsync(
TimeSpan.FromSeconds(1), default);
Assert.Equal(
new PeerChainState((BoundPeer)swarm2.AsPeer, 1, 1024),
peerChainState.First()
);

await BootstrapAsync(swarm1, swarm3.AsPeer);
peerChainState = await swarm1.GetPeerChainStateAsync(
TimeSpan.FromSeconds(1), default);
Assert.Equal(
new[]
{
new PeerChainState((BoundPeer)swarm2.AsPeer, 1, 1024),
new PeerChainState((BoundPeer)swarm3.AsPeer, 0, 0),
}.ToHashSet(),
peerChainState.ToHashSet()
);
}
finally
{
await StopAsync(swarm2);
await StopAsync(swarm3);
}
}

private async Task<Task> StartAsync<T>(
Swarm<T> swarm,
CancellationToken cancellationToken = default
Expand Down
38 changes: 38 additions & 0 deletions Libplanet/Net/PeerChainState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Numerics;

namespace Libplanet.Net
{
/// <summary>
/// The blockchain state of <see cref="BoundPeer"/>.
/// </summary>
public readonly struct PeerChainState
{
public PeerChainState(BoundPeer peer, long tipIndex, BigInteger totalDifficulty)
{
Peer = peer;
TipIndex = tipIndex;
TotalDifficulty = totalDifficulty;
}

/// <summary>
/// The peer with chain.
/// </summary>
public BoundPeer Peer { get; }

/// <summary>
/// The blockchain tip of the <see cref="Peer"/>.
/// </summary>
public long TipIndex { get; }

/// <summary>
/// The total difficulty of the blockchain of the <see cref="Peer"/>.
/// </summary>
public BigInteger TotalDifficulty { get; }

/// <inheritdoc />
public override string ToString()
{
return $"{Peer}, {TipIndex}, {TotalDifficulty}";
}
}
}
20 changes: 20 additions & 0 deletions Libplanet/Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,26 @@ public string TraceTable()
return Transport is null ? string.Empty : (Transport as NetMQTransport)?.Trace();
}

/// <summary>
/// Gets the <see cref="PeerChainState"/> of the connected <see cref="Peers"/>.
/// </summary>
/// <param name="dialTimeout">A timeout value for dialing.</param>
/// <param name="cancellationToken">
/// A cancellation token used to propagate notification that this
/// operation should be canceled.
/// </param>
/// <returns><see cref="PeerChainState"/> of the connected <see cref="Peers"/>.</returns>
public async Task<IEnumerable<PeerChainState>> GetPeerChainStateAsync(
TimeSpan? dialTimeout,
CancellationToken cancellationToken
)
{
return (await DialToExistingPeers(dialTimeout, cancellationToken))
.Where(pp => !(pp.Item1 is null || pp.Item2 is null))
.Select(pp =>
new PeerChainState(pp.Item1, pp.Item2.TipIndex, pp.Item2.TotalDifficulty));
}

/// <summary>
/// Preemptively downloads blocks from registered <see cref="Peer"/>s.
/// </summary>
Expand Down