Skip to content

Commit

Permalink
Access only blocks to related to render
Browse files Browse the repository at this point in the history
  • Loading branch information
earlbread committed Oct 11, 2019
1 parent 3172471 commit a373a75
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ To be released.

### Added interfaces

- Added `BlockChain.LongCount()` method. [[#575]]

### Behavioral changes

- Improved performance of `StoreExtension.LookupStateReference<T>()` method.
Expand All @@ -32,6 +34,8 @@ To be released.
already received when blockchain is empty. [[#550], [#562]]
- Fixed a bug that `Swarm<T>` had thrown `SocketException` with a message
`Operation on non-blocking socket would block`. [[#405], [#485]]
- Fixed a bug that accessed all blocks from the genesis block when a swap
occurred. [[#575]]

[#405]: https://github.com/planetarium/libplanet/issues/405
[#447]: https://github.com/planetarium/libplanet/issues/447
Expand All @@ -47,6 +51,7 @@ To be released.
[#563]: https://github.com/planetarium/libplanet/pull/563
[#209]: https://github.com/planetarium/libplanet/issues/209
[#561]: https://github.com/planetarium/libplanet/pull/561
[#575]: https://github.com/planetarium/libplanet/pull/575


Version 0.6.0
Expand Down
21 changes: 15 additions & 6 deletions Libplanet/Blockchain/BlockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ public Block<T> this[long index]
}
}

/// <summary>
/// Returns an <see cref="long"/> that represents the number of elements in the
/// <see cref="BlockChain{T}"/>.
/// </summary>
/// <returns>A number that represents how many elements in the <see cref="BlockChain{T}"/>.
/// </returns>
public long LongCount() => Store.CountIndex(Id);

public void Validate(
IReadOnlyList<Block<T>> blocks,
DateTimeOffset currentTime
Expand Down Expand Up @@ -956,7 +964,7 @@ internal void Swap(BlockChain<T> other, bool render)
if (render && !(Tip is null || other.Tip is null))
{
long shorterHeight =
Math.Min(this.LongCount(), other.LongCount()) - 1;
Math.Min(LongCount(), other.LongCount()) - 1;
for (
Block<T> t = this[shorterHeight], o = other[shorterHeight];
t.PreviousHash is HashDigest<SHA256> tp &&
Expand Down Expand Up @@ -1030,12 +1038,13 @@ t.PreviousHash is HashDigest<SHA256> tp &&
if (render)
{
// Render actions that had been behind.
IEnumerable<Block<T>> blocksToRender =
topmostCommon is Block<T> branchPoint
? this.SkipWhile(b => b.Index <= branchPoint.Index)
: this;
foreach (Block<T> b in blocksToRender)
long startToRenderIndex = topmostCommon is Block<T> branchPoint
? branchPoint.Index + 1
: 0;

for (var i = startToRenderIndex; i <= Tip.Index; i++)
{
Block<T> b = this[i];
List<ActionEvaluation> evaluations = b.EvaluateActionsPerTx(a =>
GetState(a, b.PreviousHash).GetValueOrDefault(a))
.Select(te => te.Item2).ToList();
Expand Down

0 comments on commit a373a75

Please sign in to comment.