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 34538b7 commit 256974d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -56,6 +56,7 @@ To be released.
- Added `HashDigest.Satisfies()` method. [[#213]]
- `BlockPolicy<T>` constructor became to receive the `minimumDifficulty`
and the mining `difficultyBoundDivisor`. [[#213]]
- Added `BlockChain<T>.UnstageTransactions()` method. [[#223]]

### Behavioral changes

Expand Down Expand Up @@ -115,6 +116,7 @@ To be released.
[#215]: https://github.com/planetarium/libplanet/pull/215
[#216]: https://github.com/planetarium/libplanet/pull/216
[#217]: https://github.com/planetarium/libplanet/pull/217
[#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.Select(tx => tx.Id).ToList(),
_blockChain.Store.IterateStagedTransactionIds());
_blockChain.UnstageTransactions(txs);
Assert.Empty(_blockChain.Store.IterateStagedTransactionIds());
}

[Fact]
public void ProcessActions()
{
Expand Down
25 changes: 22 additions & 3 deletions Libplanet/Blockchain/BlockChain.cs
Expand Up @@ -265,18 +265,37 @@ 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)
{
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()
);
}

/// <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)
{
Store.UnstageTransactionIds(
transactions.Select(tx => tx.Id).ToImmutableHashSet());
}

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

0 comments on commit 256974d

Please sign in to comment.