From 00f43bb554548739c12562bbc47dc466bbc8757f Mon Sep 17 00:00:00 2001 From: Seunghun Lee Date: Wed, 11 Dec 2019 21:28:49 +0900 Subject: [PATCH] Do not stage already mined transactions --- CHANGES.md | 2 ++ Libplanet.Tests/Net/SwarmTest.cs | 51 ++++++++++++++++++++++++++++++ Libplanet/Blockchain/BlockChain.cs | 5 +++ Menees.Analyzers.Settings.xml | 2 +- 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 899bfa130bc..f0704baa6ab 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -86,6 +86,7 @@ To be released. - Fixed a bug where `Swarm` hadn't stopped properly. [[#709]] - Fixed a bug where `BlockChain.GetNextTxNonce()` had returned invalid tx nonce. [[#718]] + - Fixed a bug where mined transactions were staged again. [[#719]] [#662]: https://github.com/planetarium/libplanet/pull/662 [#665]: https://github.com/planetarium/libplanet/pull/665 @@ -103,6 +104,7 @@ To be released. [#706]: https://github.com/planetarium/libplanet/pull/706 [#709]: https://github.com/planetarium/libplanet/pull/709 [#718]: https://github.com/planetarium/libplanet/pull/718 +[#719]: https://github.com/planetarium/libplanet/pull/719 Version 0.7.0 diff --git a/Libplanet.Tests/Net/SwarmTest.cs b/Libplanet.Tests/Net/SwarmTest.cs index 0a727424614..92e23ee036b 100644 --- a/Libplanet.Tests/Net/SwarmTest.cs +++ b/Libplanet.Tests/Net/SwarmTest.cs @@ -769,6 +769,57 @@ public async Task BroadcastTx() } } + [Fact(Timeout = Timeout)] + public async Task BroadcastTxWhileMining() + { + Swarm swarmA = _swarms[0]; + Swarm swarmC = _swarms[2]; + + BlockChain chainA = _blockchains[0]; + BlockChain chainC = _blockchains[2]; + + var privateKey = new PrivateKey(); + var address = privateKey.PublicKey.ToAddress(); + + var txs = Enumerable.Range(0, 10).Select(_ => + chainA.MakeTransaction(new PrivateKey(), new[] { new DumbAction(address, "foo") })) + .ToArray(); + + try + { + await StartAsync(swarmA); + await StartAsync(swarmC); + + await swarmC.AddPeersAsync(new[] { swarmA.AsPeer }, null); + + for (var i = 0; i < 100; i++) + { + swarmA.BroadcastTxs(txs); + } + + var t = Task.Run(async () => + { + for (var i = 0; i < 10; i++) + { + await chainC.MineBlock(_fx1.Address1); + } + }); + + await swarmC.TxReceived.WaitAsync(); + await t; + + for (var i = 0; i < 10; i++) + { + Assert.True(chainC.Store.ContainsTransaction(txs[i].Id)); + } + } + finally + { + await StopAsync(swarmA); + await StopAsync(swarmC); + } + } + [Fact(Timeout = Timeout)] public async Task BroadcastTxAsync() { diff --git a/Libplanet/Blockchain/BlockChain.cs b/Libplanet/Blockchain/BlockChain.cs index 570ea253a88..f9d2be247d8 100644 --- a/Libplanet/Blockchain/BlockChain.cs +++ b/Libplanet/Blockchain/BlockChain.cs @@ -499,6 +499,11 @@ public void StageTransactions(IImmutableSet> transactions) try { + // FIXME: We need to create a separate pool to handle transactions. + transactions = transactions + .Where(tx => !_transactions.ContainsKey(tx.Id)) + .ToImmutableHashSet(); + foreach (Transaction tx in transactions) { _transactions[tx.Id] = tx; diff --git a/Menees.Analyzers.Settings.xml b/Menees.Analyzers.Settings.xml index cd5540e026c..d94834c2170 100644 --- a/Menees.Analyzers.Settings.xml +++ b/Menees.Analyzers.Settings.xml @@ -2,5 +2,5 @@ 100 200 - 2600 + 2700