Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access blocks and transactions of the chain with lock #583

Merged
merged 4 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ To be released.
became replaced by `LoadPlainValue(IValue)`.
- `AccountStateGetter` became to return `IValue`, not `object`.
- Added `BencodexExtension` static class.
- Removed `BlockChain<T>.Blocks`. [[#409], [#583]]
- Removed `BlockChain<T>.Transactions`. [[#409], [#583]]

### Added interfaces

- Added `BlockChain<T>.LongCount()` method. [[#575]]
- Added `BlockChain<T>[HashDigest<T>]` indexer. [[#409], [#583]]
- Added `BlockChain<T>.Contains(HashDigest<T>)` method. [[#409], [#583]]
- Added `BlockChain<T>.GetTransaction(TxId)` method. [[#409], [#583]]
- Added `BlockChain<T>.Contains(TxId)` method. [[#409], [#583]]

### Behavioral changes

Expand All @@ -54,6 +60,7 @@ To be released.

[#209]: https://github.com/planetarium/libplanet/issues/209
[#405]: https://github.com/planetarium/libplanet/issues/405
[#409]: https://github.com/planetarium/libplanet/issues/409
[#447]: https://github.com/planetarium/libplanet/issues/447
[#462]: https://github.com/planetarium/libplanet/issues/462
[#469]: https://github.com/planetarium/libplanet/pull/469
Expand All @@ -73,6 +80,7 @@ To be released.
[#566]: https://github.com/planetarium/libplanet/pull/566
[#575]: https://github.com/planetarium/libplanet/pull/575
[#576]: https://github.com/planetarium/libplanet/pull/576
[#583]: https://github.com/planetarium/libplanet/pull/583


Version 0.6.0
Expand Down
23 changes: 11 additions & 12 deletions Libplanet.Tests/Blockchain/BlockChainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,35 +164,34 @@ public async void Enumerate()
[Fact]
public void StageTransactions()
{
Assert.Empty(_blockChain.Transactions);
var txs = new HashSet<Transaction<DumbAction>>()
{
_fx.Transaction1,
_fx.Transaction2,
};
Assert.Empty(txs.Where(tx => _blockChain.Contains(tx.Id)));

_blockChain.StageTransactions(txs.ToImmutableHashSet());
Assert.Equal(
txs,
_blockChain.Transactions.Values.ToHashSet()
);
txs.Where(tx => _blockChain.Contains(tx.Id)).ToHashSet());
}

[Fact]
public void UnstageTransactions()
{
Assert.Empty(_blockChain.Transactions);
Assert.Empty(_blockChain.GetStagedTransactionIds());
var txs = new HashSet<Transaction<DumbAction>>()
{
_fx.Transaction1,
_fx.Transaction2,
};
_blockChain.StageTransactions(txs.ToImmutableHashSet());
HashSet<TxId> txIds = txs.Select(tx => tx.Id).ToHashSet();
HashSet<TxId> stagedTxIds = _blockChain.Store
.IterateStagedTransactionIds().ToHashSet();
HashSet<TxId> stagedTxIds = _blockChain.GetStagedTransactionIds().ToHashSet();
Assert.Equal(txIds, stagedTxIds);
_blockChain.UnstageTransactions(txs);
Assert.Empty(_blockChain.Store.IterateStagedTransactionIds());
Assert.Empty(_blockChain.GetStagedTransactionIds());
}

[Fact]
Expand Down Expand Up @@ -297,7 +296,7 @@ public void Append()

(Address[] addresses, Block<DumbAction>[] blocks) = MakeFixturesForAppendTests();

Assert.Empty(_blockChain.Blocks);
Assert.Empty(_blockChain);
Assert.Empty(MinerReward.RenderRecords.Value);
try
{
Expand Down Expand Up @@ -1034,10 +1033,10 @@ HashDigest<SHA256>[] ListStateReferences(Address address)
if (sr?.Item1 is HashDigest<SHA256> reference)
{
refs.Add(reference);
block = chain.Blocks[reference];
block = chain[reference];
if (block.PreviousHash is HashDigest<SHA256> prev)
{
block = chain.Blocks[prev];
block = chain[prev];
continue;
}
}
Expand Down Expand Up @@ -1481,7 +1480,7 @@ void BuildIndex(Guid id, Block<DumbAction> block)

// Build the store has incomplete states
Block<DumbAction> b = TestUtils.MineGenesis<DumbAction>();
chain.Blocks[b.Hash] = b;
store.PutBlock(b);
BuildIndex(chainId, b);
IImmutableDictionary<Address, IValue> dirty =
GetDirty(b.Evaluate(DateTimeOffset.UtcNow, _ => null));
Expand All @@ -1507,7 +1506,7 @@ void BuildIndex(Guid id, Block<DumbAction> block)
)
);
Assert.NotEmpty(dirty);
chain.Blocks[b.Hash] = b;
store.PutBlock(b);
store.StoreStateReference(
chainId,
dirty.Keys.ToImmutableHashSet(),
Expand Down
10 changes: 5 additions & 5 deletions Libplanet.Tests/Net/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,8 +1079,8 @@ public async Task BroadcastTx()
await swarmC.TxReceived.WaitAsync();
await swarmB.TxReceived.WaitAsync();

Assert.Equal(tx, chainB.Transactions[tx.Id]);
Assert.Equal(tx, chainC.Transactions[tx.Id]);
Assert.Equal(tx, chainB.GetTransaction(tx.Id));
Assert.Equal(tx, chainC.GetTransaction(tx.Id));
}
finally
{
Expand Down Expand Up @@ -1119,15 +1119,15 @@ public async Task BroadcastTxAsync()
await swarmA.AddPeersAsync(new[] { swarmB.AsPeer }, null);

await swarmB.TxReceived.WaitAsync();
Assert.Equal(tx, chainB.Transactions[tx.Id]);
Assert.Equal(tx, chainB.GetTransaction(tx.Id));

await swarmA.StopAsync();

// Re-Broadcast received tx swarmB to swarmC
await swarmB.AddPeersAsync(new[] { swarmC.AsPeer }, null);

await swarmC.TxReceived.WaitAsync();
Assert.Equal(tx, chainC.Transactions[tx.Id]);
Assert.Equal(tx, chainC.GetTransaction(tx.Id));
}
finally
{
Expand Down Expand Up @@ -1189,7 +1189,7 @@ public async Task BroadcastTxAsyncMany()

for (int i = 0; i < size; i++)
{
Assert.Equal(tx, blockchains[i].Transactions[tx.Id]);
Assert.Equal(tx, blockchains[i].GetTransaction(tx.Id));
}
}
finally
Expand Down
Loading