From 17c2cdef1df50d9438287fcc4aa8a373f2c8e9f8 Mon Sep 17 00:00:00 2001 From: Seunghun Lee Date: Mon, 27 Jul 2020 16:49:29 +0900 Subject: [PATCH 1/7] Add PeerChainStatus --- Libplanet/Net/PeerChainStatus.cs | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Libplanet/Net/PeerChainStatus.cs diff --git a/Libplanet/Net/PeerChainStatus.cs b/Libplanet/Net/PeerChainStatus.cs new file mode 100644 index 0000000000..30a8f69b27 --- /dev/null +++ b/Libplanet/Net/PeerChainStatus.cs @@ -0,0 +1,38 @@ +using System.Numerics; + +namespace Libplanet.Net +{ + /// + /// The blockchain status of . + /// + public readonly struct PeerChainStatus + { + public PeerChainStatus(BoundPeer peer, long tipIndex, BigInteger totalDifficulty) + { + Peer = peer; + TipIndex = tipIndex; + TotalDifficulty = totalDifficulty; + } + + /// + /// The peer with chain. + /// + public BoundPeer Peer { get; } + + /// + /// The blockchain tip of the . + /// + public long TipIndex { get; } + + /// + /// The total difficulty of the blockchain of the . + /// + public BigInteger TotalDifficulty { get; } + + /// + public override string ToString() + { + return $"{Peer}, {TipIndex}, {TotalDifficulty}"; + } + } +} From 870dc84da9f6dc195fd7f6f0002797ed5040804f Mon Sep 17 00:00:00 2001 From: Seunghun Lee Date: Mon, 27 Jul 2020 16:49:50 +0900 Subject: [PATCH 2/7] Add Swarm.GetPeerChainStatus() --- Libplanet.Tests/Net/SwarmTest.cs | 48 ++++++++++++++++++++++++++++++++ Libplanet/Net/Swarm.cs | 20 +++++++++++++ 2 files changed, 68 insertions(+) diff --git a/Libplanet.Tests/Net/SwarmTest.cs b/Libplanet.Tests/Net/SwarmTest.cs index d62f7a0926..ef4f22d0c4 100644 --- a/Libplanet.Tests/Net/SwarmTest.cs +++ b/Libplanet.Tests/Net/SwarmTest.cs @@ -2016,6 +2016,54 @@ public async Task DoNotFillMultipleTimes() } } + [Fact(Timeout = Timeout)] + public async Task GetPeerChainStatus() + { + Swarm swarm1 = _swarms[0]; + Swarm swarm2 = _swarms[1]; + Swarm swarm3 = _swarms[2]; + + var peerStatus = await swarm1.GetPeerChainStatus(TimeSpan.FromSeconds(1), default); + Assert.Empty(peerStatus); + + try + { + await StartAsync(swarm2); + await StartAsync(swarm3); + + await BootstrapAsync(swarm1, swarm2.AsPeer); + + peerStatus = await swarm1.GetPeerChainStatus(TimeSpan.FromSeconds(1), default); + Assert.Equal( + new PeerChainStatus((BoundPeer)swarm2.AsPeer, 0, 0), + peerStatus.First() + ); + + await swarm2.BlockChain.MineBlock(_fx1.Address1); + peerStatus = await swarm1.GetPeerChainStatus(TimeSpan.FromSeconds(1), default); + Assert.Equal( + new PeerChainStatus((BoundPeer)swarm2.AsPeer, 1, 1024), + peerStatus.First() + ); + + await BootstrapAsync(swarm1, swarm3.AsPeer); + peerStatus = await swarm1.GetPeerChainStatus(TimeSpan.FromSeconds(1), default); + Assert.Equal( + new[] + { + new PeerChainStatus((BoundPeer)swarm2.AsPeer, 1, 1024), + new PeerChainStatus((BoundPeer)swarm3.AsPeer, 0, 0), + }, + peerStatus.ToArray() + ); + } + finally + { + await StopAsync(swarm2); + await StopAsync(swarm3); + } + } + private async Task StartAsync( Swarm swarm, CancellationToken cancellationToken = default diff --git a/Libplanet/Net/Swarm.cs b/Libplanet/Net/Swarm.cs index c34ef0f852..bfd80b9c2f 100644 --- a/Libplanet/Net/Swarm.cs +++ b/Libplanet/Net/Swarm.cs @@ -404,6 +404,26 @@ public string TraceTable() return Transport is null ? string.Empty : (Transport as NetMQTransport)?.Trace(); } + /// + /// Gets the of the connected . + /// + /// A timeout value for dialing. + /// + /// A cancellation token used to propagate notification that this + /// operation should be canceled. + /// + /// of the connected . + public async Task> GetPeerChainStatus( + TimeSpan? dialTimeout, + CancellationToken cancellationToken + ) + { + return (await DialToExistingPeers(dialTimeout, cancellationToken)) + .Where(pp => !(pp.Item1 is null || pp.Item2 is null)) + .Select(pp => + new PeerChainStatus(pp.Item1, pp.Item2.TipIndex, pp.Item2.TotalDifficulty)); + } + /// /// Preemptively downloads blocks from registered s. /// From 00cc320746173c6caf0b61d12ef5eec1b7d41666 Mon Sep 17 00:00:00 2001 From: Seunghun Lee Date: Mon, 27 Jul 2020 16:50:26 +0900 Subject: [PATCH 3/7] Add PeerChainStatus and Swarm.GetPeerChainStatus() --- CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 3cd939e874..64b57fe367 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -74,6 +74,8 @@ To be released. - Added `BlockChain.GetBalance()` method. [[#861], [#900]] - Added `Block.TotalDifficulty` property. [[#666], [#917]] - Added `SwarmOptions` class. [[#926]] + - Added `PeerChainStatus` struct. [[#936]] + - Added `Swarm.GetPeerChainStatus()` method. [[#936]] ### Behavioral changes @@ -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 From 945d6d3e34d4bf63e397e5556e5c40cb43845192 Mon Sep 17 00:00:00 2001 From: Seunghun Lee Date: Tue, 28 Jul 2020 19:18:34 +0900 Subject: [PATCH 4/7] Fix Array to HashSet --- Libplanet.Tests/Net/SwarmTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Libplanet.Tests/Net/SwarmTest.cs b/Libplanet.Tests/Net/SwarmTest.cs index ef4f22d0c4..f80b6fcd68 100644 --- a/Libplanet.Tests/Net/SwarmTest.cs +++ b/Libplanet.Tests/Net/SwarmTest.cs @@ -2053,8 +2053,8 @@ public async Task GetPeerChainStatus() { new PeerChainStatus((BoundPeer)swarm2.AsPeer, 1, 1024), new PeerChainStatus((BoundPeer)swarm3.AsPeer, 0, 0), - }, - peerStatus.ToArray() + }.ToHashSet(), + peerStatus.ToHashSet() ); } finally From ba4595317bbb179fb3c3cd8241e24ef5e45bf40f Mon Sep 17 00:00:00 2001 From: Seunghun Lee Date: Wed, 29 Jul 2020 12:55:19 +0900 Subject: [PATCH 5/7] Rename GetPeerChainStatus to GetPeerChainStatusAsync --- CHANGES.md | 2 +- Libplanet.Tests/Net/SwarmTest.cs | 10 +++++----- Libplanet/Net/Swarm.cs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 64b57fe367..b173b22d4d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -75,7 +75,7 @@ To be released. - Added `Block.TotalDifficulty` property. [[#666], [#917]] - Added `SwarmOptions` class. [[#926]] - Added `PeerChainStatus` struct. [[#936]] - - Added `Swarm.GetPeerChainStatus()` method. [[#936]] + - Added `Swarm.GetPeerChainStatusAsync()` method. [[#936]] ### Behavioral changes diff --git a/Libplanet.Tests/Net/SwarmTest.cs b/Libplanet.Tests/Net/SwarmTest.cs index f80b6fcd68..4ca56a3cce 100644 --- a/Libplanet.Tests/Net/SwarmTest.cs +++ b/Libplanet.Tests/Net/SwarmTest.cs @@ -2017,13 +2017,13 @@ public async Task DoNotFillMultipleTimes() } [Fact(Timeout = Timeout)] - public async Task GetPeerChainStatus() + public async Task GetPeerChainStatusAsync() { Swarm swarm1 = _swarms[0]; Swarm swarm2 = _swarms[1]; Swarm swarm3 = _swarms[2]; - var peerStatus = await swarm1.GetPeerChainStatus(TimeSpan.FromSeconds(1), default); + var peerStatus = await swarm1.GetPeerChainStatusAsync(TimeSpan.FromSeconds(1), default); Assert.Empty(peerStatus); try @@ -2033,21 +2033,21 @@ public async Task GetPeerChainStatus() await BootstrapAsync(swarm1, swarm2.AsPeer); - peerStatus = await swarm1.GetPeerChainStatus(TimeSpan.FromSeconds(1), default); + peerStatus = await swarm1.GetPeerChainStatusAsync(TimeSpan.FromSeconds(1), default); Assert.Equal( new PeerChainStatus((BoundPeer)swarm2.AsPeer, 0, 0), peerStatus.First() ); await swarm2.BlockChain.MineBlock(_fx1.Address1); - peerStatus = await swarm1.GetPeerChainStatus(TimeSpan.FromSeconds(1), default); + peerStatus = await swarm1.GetPeerChainStatusAsync(TimeSpan.FromSeconds(1), default); Assert.Equal( new PeerChainStatus((BoundPeer)swarm2.AsPeer, 1, 1024), peerStatus.First() ); await BootstrapAsync(swarm1, swarm3.AsPeer); - peerStatus = await swarm1.GetPeerChainStatus(TimeSpan.FromSeconds(1), default); + peerStatus = await swarm1.GetPeerChainStatusAsync(TimeSpan.FromSeconds(1), default); Assert.Equal( new[] { diff --git a/Libplanet/Net/Swarm.cs b/Libplanet/Net/Swarm.cs index bfd80b9c2f..e0e8384786 100644 --- a/Libplanet/Net/Swarm.cs +++ b/Libplanet/Net/Swarm.cs @@ -413,7 +413,7 @@ public string TraceTable() /// operation should be canceled. /// /// of the connected . - public async Task> GetPeerChainStatus( + public async Task> GetPeerChainStatusAsync( TimeSpan? dialTimeout, CancellationToken cancellationToken ) From 6b0d12cb99713adbc3a156e3006a271e86c25ec1 Mon Sep 17 00:00:00 2001 From: Seunghun Lee Date: Wed, 29 Jul 2020 14:34:40 +0900 Subject: [PATCH 6/7] Rename PeerChainStatus to PeerChainState --- CHANGES.md | 4 +-- Libplanet.Tests/Net/SwarmTest.cs | 28 +++++++++++-------- .../{PeerChainStatus.cs => PeerChainState.cs} | 6 ++-- Libplanet/Net/Swarm.cs | 8 +++--- 4 files changed, 25 insertions(+), 21 deletions(-) rename Libplanet/Net/{PeerChainStatus.cs => PeerChainState.cs} (80%) diff --git a/CHANGES.md b/CHANGES.md index b173b22d4d..0b2367b38c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -74,8 +74,8 @@ To be released. - Added `BlockChain.GetBalance()` method. [[#861], [#900]] - Added `Block.TotalDifficulty` property. [[#666], [#917]] - Added `SwarmOptions` class. [[#926]] - - Added `PeerChainStatus` struct. [[#936]] - - Added `Swarm.GetPeerChainStatusAsync()` method. [[#936]] + - Added `PeerChainState` struct. [[#936]] + - Added `Swarm.GetPeerChainStateAsync()` method. [[#936]] ### Behavioral changes diff --git a/Libplanet.Tests/Net/SwarmTest.cs b/Libplanet.Tests/Net/SwarmTest.cs index 4ca56a3cce..9fdd2cfe44 100644 --- a/Libplanet.Tests/Net/SwarmTest.cs +++ b/Libplanet.Tests/Net/SwarmTest.cs @@ -2023,8 +2023,9 @@ public async Task GetPeerChainStatusAsync() Swarm swarm2 = _swarms[1]; Swarm swarm3 = _swarms[2]; - var peerStatus = await swarm1.GetPeerChainStatusAsync(TimeSpan.FromSeconds(1), default); - Assert.Empty(peerStatus); + var peerChainState = await swarm1.GetPeerChainStateAsync( + TimeSpan.FromSeconds(1), default); + Assert.Empty(peerChainState); try { @@ -2033,28 +2034,31 @@ public async Task GetPeerChainStatusAsync() await BootstrapAsync(swarm1, swarm2.AsPeer); - peerStatus = await swarm1.GetPeerChainStatusAsync(TimeSpan.FromSeconds(1), default); + peerChainState = await swarm1.GetPeerChainStateAsync( + TimeSpan.FromSeconds(1), default); Assert.Equal( - new PeerChainStatus((BoundPeer)swarm2.AsPeer, 0, 0), - peerStatus.First() + new PeerChainState((BoundPeer)swarm2.AsPeer, 0, 0), + peerChainState.First() ); await swarm2.BlockChain.MineBlock(_fx1.Address1); - peerStatus = await swarm1.GetPeerChainStatusAsync(TimeSpan.FromSeconds(1), default); + peerChainState = await swarm1.GetPeerChainStateAsync( + TimeSpan.FromSeconds(1), default); Assert.Equal( - new PeerChainStatus((BoundPeer)swarm2.AsPeer, 1, 1024), - peerStatus.First() + new PeerChainState((BoundPeer)swarm2.AsPeer, 1, 1024), + peerChainState.First() ); await BootstrapAsync(swarm1, swarm3.AsPeer); - peerStatus = await swarm1.GetPeerChainStatusAsync(TimeSpan.FromSeconds(1), default); + peerChainState = await swarm1.GetPeerChainStateAsync( + TimeSpan.FromSeconds(1), default); Assert.Equal( new[] { - new PeerChainStatus((BoundPeer)swarm2.AsPeer, 1, 1024), - new PeerChainStatus((BoundPeer)swarm3.AsPeer, 0, 0), + new PeerChainState((BoundPeer)swarm2.AsPeer, 1, 1024), + new PeerChainState((BoundPeer)swarm3.AsPeer, 0, 0), }.ToHashSet(), - peerStatus.ToHashSet() + peerChainState.ToHashSet() ); } finally diff --git a/Libplanet/Net/PeerChainStatus.cs b/Libplanet/Net/PeerChainState.cs similarity index 80% rename from Libplanet/Net/PeerChainStatus.cs rename to Libplanet/Net/PeerChainState.cs index 30a8f69b27..a336ab4f4c 100644 --- a/Libplanet/Net/PeerChainStatus.cs +++ b/Libplanet/Net/PeerChainState.cs @@ -3,11 +3,11 @@ namespace Libplanet.Net { /// - /// The blockchain status of . + /// The blockchain state of . /// - public readonly struct PeerChainStatus + public readonly struct PeerChainState { - public PeerChainStatus(BoundPeer peer, long tipIndex, BigInteger totalDifficulty) + public PeerChainState(BoundPeer peer, long tipIndex, BigInteger totalDifficulty) { Peer = peer; TipIndex = tipIndex; diff --git a/Libplanet/Net/Swarm.cs b/Libplanet/Net/Swarm.cs index e0e8384786..89befe2dc4 100644 --- a/Libplanet/Net/Swarm.cs +++ b/Libplanet/Net/Swarm.cs @@ -405,15 +405,15 @@ public string TraceTable() } /// - /// Gets the of the connected . + /// Gets the of the connected . /// /// A timeout value for dialing. /// /// A cancellation token used to propagate notification that this /// operation should be canceled. /// - /// of the connected . - public async Task> GetPeerChainStatusAsync( + /// of the connected . + public async Task> GetPeerChainStateAsync( TimeSpan? dialTimeout, CancellationToken cancellationToken ) @@ -421,7 +421,7 @@ CancellationToken cancellationToken return (await DialToExistingPeers(dialTimeout, cancellationToken)) .Where(pp => !(pp.Item1 is null || pp.Item2 is null)) .Select(pp => - new PeerChainStatus(pp.Item1, pp.Item2.TipIndex, pp.Item2.TotalDifficulty)); + new PeerChainState(pp.Item1, pp.Item2.TipIndex, pp.Item2.TotalDifficulty)); } /// From 4073220c1a539672921eb34aef8b1eeea92275c1 Mon Sep 17 00:00:00 2001 From: Seunghun Lee Date: Wed, 29 Jul 2020 16:21:57 +0900 Subject: [PATCH 7/7] Rename GetPeerChainStatusAsync test to GetPeerChainStateAsync MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Hong Minhee (洪 民憙) --- Libplanet.Tests/Net/SwarmTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libplanet.Tests/Net/SwarmTest.cs b/Libplanet.Tests/Net/SwarmTest.cs index 9fdd2cfe44..bed92db58a 100644 --- a/Libplanet.Tests/Net/SwarmTest.cs +++ b/Libplanet.Tests/Net/SwarmTest.cs @@ -2017,7 +2017,7 @@ public async Task DoNotFillMultipleTimes() } [Fact(Timeout = Timeout)] - public async Task GetPeerChainStatusAsync() + public async Task GetPeerChainStateAsync() { Swarm swarm1 = _swarms[0]; Swarm swarm2 = _swarms[1];