Skip to content

Commit

Permalink
Moved fork methods to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Nov 28, 2024
1 parent 2e21655 commit 0c8963f
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 102 deletions.
113 changes: 113 additions & 0 deletions src/Libplanet.RocksDBStore/RocksDBStore.Fork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using System.Linq;
using Libplanet.Crypto;
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;

Check failure on line 5 in src/Libplanet.RocksDBStore/RocksDBStore.Fork.cs

View workflow job for this annotation

GitHub Actions / check-build

Remove this unnecessary 'using'.

Check failure on line 5 in src/Libplanet.RocksDBStore/RocksDBStore.Fork.cs

View workflow job for this annotation

GitHub Actions / check-build

Remove this unnecessary 'using'.

Check failure on line 5 in src/Libplanet.RocksDBStore/RocksDBStore.Fork.cs

View workflow job for this annotation

GitHub Actions / check-build

Remove this unnecessary 'using'.

Check failure on line 5 in src/Libplanet.RocksDBStore/RocksDBStore.Fork.cs

View workflow job for this annotation

GitHub Actions / check-build

Remove this unnecessary 'using'.
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();
}
}
}
}
}
103 changes: 1 addition & 102 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,64 +648,6 @@ public override long AppendIndex(Guid chainId, BlockHash hash)
return index;
}

/// <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);
}

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

/// <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();
}
}
}

/// <inheritdoc />
public override void PruneOutdatedChains(bool noopWithoutCanon = false)
{
Expand Down

0 comments on commit 0c8963f

Please sign in to comment.