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

Removed IStore.ForkBlockIndexes() and IStore.ForkTxNonces() #4001

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ To be released.
- Changed `IMessageCodec.Encode(MessageContent, PrivateKey,
AppProtocolVersion, BoundPeer, DateTimeOffset, byte[]?)` to
`IMessageCodec.Encode(Message, PrivateKey)`. [[#3997]]
- Removed `IStore.ForkBlockIndexes()` and `IStore.ForkTxNonces()`
interface methods. [[#4001]]

### Backward-incompatible network protocol changes

Expand All @@ -29,6 +31,7 @@ To be released.
### CLI tools

[#3997]: https://github.com/planetarium/libplanet/pull/3997
[#4001]: https://github.com/planetarium/libplanet/pull/3997


Version 5.4.0
Expand Down
112 changes: 112 additions & 0 deletions src/Libplanet.RocksDBStore/RocksDBStore.Fork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using System.Linq;
using Libplanet.Crypto;
using Libplanet.Types.Blocks;
using RocksDbSharp;

namespace Libplanet.RocksDBStore
{
public partial class RocksDBStore
{
/// <summary>
/// Forks block indexes from <paramref name="sourceChainId"/> to
/// <paramref name="destinationChainId"/>.
/// </summary>
/// <param name="sourceChainId">The chain ID of block indexes to fork.</param>
/// <param name="destinationChainId">The chain ID of destination block indexes.</param>
/// <param name="branchpoint">The branchpoint <see cref="Block"/> to fork.</param>
/// <seealso cref="IterateIndexes(Guid, int, int?)"/>
/// <seealso cref="AppendIndex(Guid, BlockHash)"/>
public void ForkBlockIndexes(
Guid sourceChainId,
Guid destinationChainId,
BlockHash branchpoint
)
{
BlockHash[] bottoms = IterateIndexes(sourceChainId, 0, 1, true).ToArray();
BlockHash? genesisHash = bottoms.Any() ? bottoms[0] : (BlockHash?)null;

if (genesisHash is null || branchpoint.Equals(genesisHash))
{
return;
}

using var batch = new WriteBatch();
foreach (Iterator k in IterateDb(_chainDb, IndexKey(destinationChainId)))
{
batch.Delete(k.Key());
}

if (!(GetBlockIndex(branchpoint) is { } bpIndex))
{
return;
}

_chainDb.Write(batch);

// Do fork from previous chain instead current if it's available and same as current.
if (GetPreviousChainInfo(sourceChainId) is { } chainInfo &&
chainInfo.Item2 == bpIndex)
{
ForkBlockIndexes(chainInfo.Item1, destinationChainId, branchpoint);
return;
}

_chainDb.Put(PreviousChainIdKey(destinationChainId), sourceChainId.ToByteArray());
_chainDb.Put(
PreviousChainIndexKey(destinationChainId),
RocksDBStoreBitConverter.GetBytes(bpIndex)
);
_chainDb.Put(
IndexCountKey(destinationChainId),
RocksDBStoreBitConverter.GetBytes(bpIndex + 1)
);

_chainDb.Put(ChainIdKey(destinationChainId), destinationChainId.ToByteArray());
AddFork(sourceChainId, destinationChainId);
}

/// <summary>
/// Forks <see cref="Transaction"/> <see cref="Transaction.Nonce"/>s from
/// <paramref name="sourceChainId"/> to <paramref name="destinationChainId"/>.
/// </summary>
/// <param name="sourceChainId">The chain <see cref="BlockChain.Id"/> of
/// <see cref="Transaction"/> <see cref="Transaction.Nonce"/>s to fork.</param>
/// <param name="destinationChainId">The chain <see cref="BlockChain.Id"/> of destination
/// <see cref="Transaction"/> <see cref="Transaction.Nonce"/>s.</param>
public void ForkTxNonces(Guid sourceChainId, Guid destinationChainId)
{
var writeBatch = new WriteBatch();
bool exist = false;
try
{
byte[] prefix = TxNonceKey(sourceChainId);
foreach (Iterator it in IterateDb(_chainDb, prefix))
{
exist = true;
Address address = new Address(it.Key().Skip(prefix.Length).ToArray());
writeBatch.Put(TxNonceKey(destinationChainId, address), it.Value());
if (writeBatch.Count() >= ForkWriteBatchSize)
{
_chainDb.Write(writeBatch);
writeBatch.Dispose();
writeBatch = new WriteBatch();
}
}
}
catch (Exception e)
{
LogUnexpectedException(nameof(ForkTxNonces), e);
throw;
}
finally
{
if (exist)
{
_chainDb.Write(writeBatch);
writeBatch.Dispose();
}
}
}
}
}
88 changes: 1 addition & 87 deletions src/Libplanet.RocksDBStore/RocksDBStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace Libplanet.RocksDBStore
/// </list>
/// </summary>
/// <seealso cref="IStore"/>
public class RocksDBStore : BaseStore
public partial class RocksDBStore : BaseStore
{
private const string BlockDbRootPathName = "block";
private const string BlockIndexDbName = "blockindex";
Expand Down Expand Up @@ -648,56 +648,6 @@ public override long AppendIndex(Guid chainId, BlockHash hash)
return index;
}

/// <inheritdoc cref="BaseStore.ForkBlockIndexes(Guid, Guid, BlockHash)"/>
public override void ForkBlockIndexes(
Guid sourceChainId,
Guid destinationChainId,
BlockHash branchpoint
)
{
BlockHash[] bottoms = IterateIndexes(sourceChainId, 0, 1, true).ToArray();
BlockHash? genesisHash = bottoms.Any() ? bottoms[0] : (BlockHash?)null;

if (genesisHash is null || branchpoint.Equals(genesisHash))
{
return;
}

using var batch = new WriteBatch();
foreach (Iterator k in IterateDb(_chainDb, IndexKey(destinationChainId)))
{
batch.Delete(k.Key());
}

if (!(GetBlockIndex(branchpoint) is { } bpIndex))
{
return;
}

_chainDb.Write(batch);

// Do fork from previous chain instead current if it's available and same as current.
if (GetPreviousChainInfo(sourceChainId) is { } chainInfo &&
chainInfo.Item2 == bpIndex)
{
ForkBlockIndexes(chainInfo.Item1, destinationChainId, branchpoint);
return;
}

_chainDb.Put(PreviousChainIdKey(destinationChainId), sourceChainId.ToByteArray());
_chainDb.Put(
PreviousChainIndexKey(destinationChainId),
RocksDBStoreBitConverter.GetBytes(bpIndex)
);
_chainDb.Put(
IndexCountKey(destinationChainId),
RocksDBStoreBitConverter.GetBytes(bpIndex + 1)
);

_chainDb.Put(ChainIdKey(destinationChainId), destinationChainId.ToByteArray());
AddFork(sourceChainId, destinationChainId);
}

/// <inheritdoc/>
public override Transaction? GetTransaction(TxId txid)
{
Expand Down Expand Up @@ -1137,42 +1087,6 @@ public override void Dispose()
}
}

/// <inheritdoc/>
public override void ForkTxNonces(Guid sourceChainId, Guid destinationChainId)
{
var writeBatch = new WriteBatch();
bool exist = false;
try
{
byte[] prefix = TxNonceKey(sourceChainId);
foreach (Iterator it in IterateDb(_chainDb, prefix))
{
exist = true;
Address address = new Address(it.Key().Skip(prefix.Length).ToArray());
writeBatch.Put(TxNonceKey(destinationChainId, address), it.Value());
if (writeBatch.Count() >= ForkWriteBatchSize)
{
_chainDb.Write(writeBatch);
writeBatch.Dispose();
writeBatch = new WriteBatch();
}
}
}
catch (Exception e)
{
LogUnexpectedException(nameof(ForkTxNonces), e);
throw;
}
finally
{
if (exist)
{
_chainDb.Write(writeBatch);
writeBatch.Dispose();
}
}
}

/// <inheritdoc />
public override void PruneOutdatedChains(bool noopWithoutCanon = false)
{
Expand Down
10 changes: 0 additions & 10 deletions src/Libplanet.Store/BaseStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ public abstract class BaseStore : IStore
/// <inheritdoc/>
public abstract long AppendIndex(Guid chainId, BlockHash hash);

/// <inheritdoc/>
public abstract void ForkBlockIndexes(
Guid sourceChainId,
Guid destinationChainId,
BlockHash branchpoint
);

public abstract Transaction? GetTransaction(TxId txid);

public abstract void PutTransaction(Transaction tx);
Expand Down Expand Up @@ -171,9 +164,6 @@ public virtual long CountBlocks()
/// <inheritdoc/>
public abstract void Dispose();

/// <inheritdoc/>
public abstract void ForkTxNonces(Guid sourceChainId, Guid destinationChainId);

/// <inheritdoc/>
public abstract void PruneOutdatedChains(bool noopWithoutCanon = false);

Expand Down
32 changes: 0 additions & 32 deletions src/Libplanet.Store/DefaultStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,30 +317,6 @@ public override long AppendIndex(Guid chainId, BlockHash hash)
return IndexCollection(chainId).Insert(new HashDoc { Hash = hash }) - 1;
}

/// <inheritdoc cref="BaseStore.ForkBlockIndexes(Guid, Guid, BlockHash)"/>
public override void ForkBlockIndexes(
Guid sourceChainId,
Guid destinationChainId,
BlockHash branchpoint)
{
LiteCollection<HashDoc> srcColl = IndexCollection(sourceChainId);
LiteCollection<HashDoc> destColl = IndexCollection(destinationChainId);

BlockHash? genesisHash = IterateIndexes(sourceChainId, 0, 1)
.Cast<BlockHash?>()
.FirstOrDefault();

if (genesisHash is null || branchpoint.Equals(genesisHash))
{
return;
}

destColl.Delete(Query.All());
destColl.InsertBulk(srcColl.FindAll().TakeWhile(i => !i.Hash.Equals(branchpoint)));

AppendIndex(destinationChainId, branchpoint);
}

/// <inheritdoc/>
public override Transaction? GetTransaction(TxId txid)
{
Expand Down Expand Up @@ -624,14 +600,6 @@ public override void IncreaseTxNonce(Guid chainId, Address signer, long delta =
collection.Upsert(docId, new BsonDocument() { ["v"] = new BsonValue(nextNonce) });
}

/// <inheritdoc/>
public override void ForkTxNonces(Guid sourceChainId, Guid destinationChainId)
{
LiteCollection<BsonDocument> srcColl = TxNonceCollection(sourceChainId);
LiteCollection<BsonDocument> destColl = TxNonceCollection(destinationChainId);
destColl.InsertBulk(srcColl.FindAll());
}

/// <inheritdoc/>
public override void PruneOutdatedChains(bool noopWithoutCanon = false)
{
Expand Down
25 changes: 0 additions & 25 deletions src/Libplanet.Store/IStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,6 @@ public interface IStore : IDisposable
/// <returns>The index of the appended block.</returns>
long AppendIndex(Guid chainId, BlockHash hash);

/// <summary>
/// Forks block indexes from
/// <paramref name="sourceChainId"/> to
/// <paramref name="destinationChainId"/>.
/// </summary>
/// <param name="sourceChainId">The chain ID of block indexes to
/// fork.</param>
/// <param name="destinationChainId">The chain ID of destination
/// block indexes.</param>
/// <param name="branchpoint">The branchpoint <see cref="Block"/> to fork.</param>
/// <seealso cref="IterateIndexes(Guid, int, int?)"/>
/// <seealso cref="AppendIndex(Guid, BlockHash)"/>
void ForkBlockIndexes(Guid sourceChainId, Guid destinationChainId, BlockHash branchpoint);

Transaction? GetTransaction(TxId txid);

/// <summary>
Expand Down Expand Up @@ -271,17 +257,6 @@ public interface IStore : IDisposable

long CountBlocks();

/// <summary>
/// Forks <see cref="Transaction"/> <see cref="Transaction.Nonce"/>s from
/// <paramref name="sourceChainId"/> to
/// <paramref name="destinationChainId"/>.
/// </summary>
/// <param name="sourceChainId">The chain <see cref="BlockChain.Id"/> of
/// <see cref="Transaction"/> <see cref="Transaction.Nonce"/>s to fork.</param>
/// <param name="destinationChainId">The chain <see cref="BlockChain.Id"/> of destination
/// <see cref="Transaction"/> <see cref="Transaction.Nonce"/>s.</param>
void ForkTxNonces(Guid sourceChainId, Guid destinationChainId);

/// <summary>
/// Delete all non-canonical chains.
/// </summary>
Expand Down
21 changes: 0 additions & 21 deletions src/Libplanet.Store/MemoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,6 @@ long IStore.AppendIndex(Guid chainId, BlockHash hash)
return list.Count - 1;
}

public void ForkBlockIndexes(
Guid sourceChainId,
Guid destinationChainId,
BlockHash branchpoint
)
{
if (_indices.TryGetValue(sourceChainId, out ImmutableTrieList<BlockHash>? source))
{
int bpIndex = source.FindIndex(branchpoint.Equals);
_indices[destinationChainId] = source.GetRange(0, bpIndex + 1);
}
}

Transaction? IStore.GetTransaction(TxId txid) =>
_txs.TryGetValue(txid, out Transaction? untyped) && untyped is Transaction tx
? tx
Expand Down Expand Up @@ -267,14 +254,6 @@ bool IStore.ContainsTransaction(TxId txId) =>
long IStore.CountBlocks() =>
_blocks.Count;

void IStore.ForkTxNonces(Guid sourceChainId, Guid destinationChainId)
{
if (_txNonces.TryGetValue(sourceChainId, out ConcurrentDictionary<Address, long>? dict))
{
_txNonces[destinationChainId] = new ConcurrentDictionary<Address, long>(dict);
}
}

void IStore.PruneOutdatedChains(bool noopWithoutCanon)
{
if (!(_canonicalChainId is { } ccid))
Expand Down
Loading
Loading