Navigation Menu

Skip to content

Commit

Permalink
Enhance IBD
Browse files Browse the repository at this point in the history
- Add ForkBlockIndexes to IStore and LiteDBStore for fast fork
  • Loading branch information
longfin committed Aug 21, 2019
1 parent 703b925 commit fc4bb70
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -36,6 +36,7 @@ To be released.
- `IStore.StoreStateReference<T>()` became to take the hash and index of the
block instead of the block itself. [[#420]]
- Therefore, it also became to lose type parameter `T`.
- Added `IStore.ForkBlockIndexes()` method. [[#420]]

### Added interfaces

Expand Down
9 changes: 9 additions & 0 deletions Libplanet.Tests/Store/StoreTracker.cs
Expand Up @@ -200,6 +200,15 @@ AddressStateMap states
sourceNamespace, destinationNamespace, branchPoint, addressesToStrip);
}

public void ForkBlockIndexes(
string sourceNamespace,
string destinationNamespace,
HashDigest<SHA256> branchPoint)
{
_logs.Add((nameof(ForkBlockIndexes), null, null));
_store.ForkBlockIndexes(sourceNamespace, destinationNamespace, branchPoint);
}

public IEnumerable<KeyValuePair<Address, long>> ListTxNonces(string @namespace)
{
_logs.Add((nameof(ListTxNonces), @namespace, null));
Expand Down
9 changes: 1 addition & 8 deletions Libplanet/Blockchain/BlockChain.cs
Expand Up @@ -815,15 +815,8 @@ internal BlockChain<T> Fork(HashDigest<SHA256> point)
try
{
_rwlock.EnterReadLock();
foreach (var index in Store.IterateIndex(id))
{
Store.AppendIndex(forkedId, index);
if (index.Equals(point))
{
break;
}
}

Store.ForkBlockIndexes(id, forkedId, point);
Block<T> pointBlock = Blocks[point];

var addressesToStrip = new HashSet<Address>();
Expand Down
6 changes: 6 additions & 0 deletions Libplanet/Store/BaseStore.cs
Expand Up @@ -38,6 +38,12 @@ long index
HashDigest<SHA256> hash
);

/// <inheritdoc />
public abstract void ForkBlockIndexes(
string sourceNamespace,
string destinationNamespace,
HashDigest<SHA256> branchPoint);

/// <inheritdoc />
public abstract IEnumerable<Address> ListAddresses(string @namespace);

Expand Down
20 changes: 20 additions & 0 deletions Libplanet/Store/IStore.cs
Expand Up @@ -53,6 +53,26 @@ public interface IStore

bool DeleteIndex(string @namespace, HashDigest<SHA256> hash);

/// <summary>
/// Forks block indexes from
/// <paramref name="sourceNamespace"/> to
/// <paramref name="destinationNamespace"/>.
/// </summary>
/// <param name="sourceNamespace">The namespace of block indexes to
/// fork.</param>
/// <param name="destinationNamespace">The namespace of destination
/// block indexes.</param>
/// <param name="branchPoint">The branch point <see cref="Block{T}"/>
/// to fork.</param>
/// <exception cref="NamespaceNotFoundException">Thrown when the given
/// <paramref name="sourceNamespace"/> does not exist.</exception>
/// <seealso cref="IterateIndex(string, int, int?)"/>
/// <seealso cref="AppendIndex(string, HashDigest{SHA256})"/>
void ForkBlockIndexes(
string sourceNamespace,
string destinationNamespace,
HashDigest<SHA256> branchPoint);

/// <summary>
/// Deletes an index, tx nonces, and state references in the given
/// <paramref name="namespace"/>.
Expand Down
13 changes: 13 additions & 0 deletions Libplanet/Store/LiteDBStore.cs
Expand Up @@ -191,6 +191,19 @@ public override bool DeleteIndex(string @namespace, HashDigest<SHA256> hash)
return deleted > 0;
}

/// <inheritdoc/>
public override void ForkBlockIndexes(
string sourceNamespace,
string destinationNamespace,
HashDigest<SHA256> branchPoint)
{
LiteCollection<HashDoc> srcColl = IndexCollection(sourceNamespace);
LiteCollection<HashDoc> destColl = IndexCollection(destinationNamespace);

destColl.InsertBulk(srcColl.FindAll().TakeWhile(i => !i.Hash.Equals(branchPoint)));
AppendIndex(destinationNamespace, branchPoint);
}

/// <inheritdoc/>
public override IEnumerable<Address> ListAddresses(string @namespace)
{
Expand Down

0 comments on commit fc4bb70

Please sign in to comment.