Skip to content

Commit

Permalink
Do not stage already mined transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
earlbread committed Dec 11, 2019
1 parent d6f49a1 commit 377778d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -86,6 +86,7 @@ To be released.
- Fixed a bug where `Swarm<T>` hadn't stopped properly. [[#709]]
- Fixed a bug where `BlockChain<T>.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
Expand All @@ -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
Expand Down
51 changes: 51 additions & 0 deletions Libplanet.Tests/Net/SwarmTest.cs
Expand Up @@ -769,6 +769,57 @@ public async Task BroadcastTx()
}
}

[Fact(Timeout = Timeout)]
public async Task BroadcastTxWhileMining()
{
Swarm<DumbAction> swarmA = _swarms[0];
Swarm<DumbAction> swarmC = _swarms[2];

BlockChain<DumbAction> chainA = _blockchains[0];
BlockChain<DumbAction> 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()
{
Expand Down
5 changes: 5 additions & 0 deletions Libplanet/Blockchain/BlockChain.cs
Expand Up @@ -499,6 +499,11 @@ public void StageTransactions(IImmutableSet<Transaction<T>> transactions)

try
{
// FIXME: We need to create a separate pool to handle transactions.
transactions = transactions
.Where(tx => !_transactions.ContainsKey(tx.Id))
.ToImmutableHashSet();

foreach (Transaction<T> tx in transactions)
{
_transactions[tx.Id] = tx;
Expand Down

0 comments on commit 377778d

Please sign in to comment.