Skip to content

Commit

Permalink
Add BlockChain<T>.UnstageTransactions()
Browse files Browse the repository at this point in the history
  • Loading branch information
earlbread committed Apr 29, 2019
1 parent b3a0ceb commit 2b79a7b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -57,6 +57,7 @@ To be released.
- Fixed `BlockChain<T>.GetStates()` had descended to the bottom
(i.e., the genesis block) where a given `Address` refers to
a nonexistent account (i.e., never used before). [[#189], [#192]]
- Added `BlockChain<T>.UnstageTransactions()` method. [[#223]]
- Improved the read throughput of `BlockChain<T>.Append()`.
- Fixed a bug that a TURN connection had turned unavailable after
it once failed to parse a message (due to a corrupted packet).
Expand Down Expand Up @@ -92,6 +93,7 @@ To be released.
[#213]: https://github.com/planetarium/libplanet/pull/213
[#215]: https://github.com/planetarium/libplanet/pull/215
[#216]: https://github.com/planetarium/libplanet/pull/216
[#223]: https://github.com/planetarium/libplanet/pull/223


Version 0.2.2
Expand Down
19 changes: 18 additions & 1 deletion Libplanet.Tests/Blockchain/BlockChainTest.cs
Expand Up @@ -67,7 +67,7 @@ public void CanIterate()
}

[Fact]
public void CanStateTransactions()
public void StageTransactions()
{
Assert.Empty(_blockChain.Transactions);
var txs = new HashSet<Transaction<DumbAction>>()
Expand All @@ -82,6 +82,23 @@ public void CanStateTransactions()
);
}

[Fact]
public void UnstageTransactions()
{
Assert.Empty(_blockChain.Transactions);
var txs = new HashSet<Transaction<DumbAction>>()
{
_fx.Transaction1,
_fx.Transaction2,
};
_blockChain.StageTransactions(txs);
Assert.Equal(
txs,
_blockChain.Transactions.Values.ToHashSet());
_blockChain.UnstageTransactions(txs);
Assert.Empty(_blockChain.Transactions);
}

[Fact]
public void ProcessActions()
{
Expand Down
30 changes: 27 additions & 3 deletions Libplanet/Blockchain/BlockChain.cs
Expand Up @@ -265,19 +265,26 @@ bool IsPossiblyIn(BitArray addr, BitArray maskPattern)
public void Append(Block<T> block, DateTimeOffset currentTime) =>
Append(block, currentTime, render: true);

public void StageTransactions(ISet<Transaction<T>> txs)
/// <summary>
/// Adds <paramref name="transactions"/> to the pending list so that
/// a next <see cref="Block{T}"/> to be mined contains these
/// <paramref name="transactions"/>.
/// </summary>
/// <param name="transactions">
/// <see cref="Transaction{T}"/>s to add to the pending list.</param>
public void StageTransactions(ISet<Transaction<T>> transactions)
{
try
{
_rwlock.EnterWriteLock();

foreach (Transaction<T> tx in txs)
foreach (Transaction<T> tx in transactions)
{
Transactions[tx.Id] = tx;
}

Store.StageTransactionIds(
txs.Select(tx => tx.Id).ToImmutableHashSet()
transactions.Select(tx => tx.Id).ToImmutableHashSet()
);
}
finally
Expand All @@ -286,6 +293,23 @@ public void StageTransactions(ISet<Transaction<T>> txs)
}
}

/// <summary>
/// Removes <paramref name="transactions"/> from the pending list.
/// </summary>
/// <param name="transactions"><see cref="Transaction{T}"/>s
/// to remove from the pending list.</param>
/// <seealso cref="StageTransactions"/>
public void UnstageTransactions(ISet<Transaction<T>> transactions)
{
foreach (Transaction<T> tx in transactions)
{
Transactions.Remove(tx.Id);
}

Store.UnstageTransactionIds(
transactions.Select(tx => tx.Id).ToImmutableHashSet());
}

public Block<T> MineBlock(
Address miner,
DateTimeOffset currentTime
Expand Down

0 comments on commit 2b79a7b

Please sign in to comment.