Skip to content

Commit 883dc22

Browse files
committed
Moved fork methods to a separate file
1 parent 2e21655 commit 883dc22

File tree

2 files changed

+113
-102
lines changed

2 files changed

+113
-102
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Linq;
3+
using Libplanet.Crypto;
4+
using Libplanet.Types.Blocks;
5+
using RocksDbSharp;
6+
7+
namespace Libplanet.RocksDBStore
8+
{
9+
public partial class RocksDBStore
10+
{
11+
/// <summary>
12+
/// Forks block indexes from <paramref name="sourceChainId"/> to
13+
/// <paramref name="destinationChainId"/>.
14+
/// </summary>
15+
/// <param name="sourceChainId">The chain ID of block indexes to fork.</param>
16+
/// <param name="destinationChainId">The chain ID of destination block indexes.</param>
17+
/// <param name="branchpoint">The branchpoint <see cref="Block"/> to fork.</param>
18+
/// <seealso cref="IterateIndexes(Guid, int, int?)"/>
19+
/// <seealso cref="AppendIndex(Guid, BlockHash)"/>
20+
public void ForkBlockIndexes(
21+
Guid sourceChainId,
22+
Guid destinationChainId,
23+
BlockHash branchpoint
24+
)
25+
{
26+
BlockHash[] bottoms = IterateIndexes(sourceChainId, 0, 1, true).ToArray();
27+
BlockHash? genesisHash = bottoms.Any() ? bottoms[0] : (BlockHash?)null;
28+
29+
if (genesisHash is null || branchpoint.Equals(genesisHash))
30+
{
31+
return;
32+
}
33+
34+
using var batch = new WriteBatch();
35+
foreach (Iterator k in IterateDb(_chainDb, IndexKey(destinationChainId)))
36+
{
37+
batch.Delete(k.Key());
38+
}
39+
40+
if (!(GetBlockIndex(branchpoint) is { } bpIndex))
41+
{
42+
return;
43+
}
44+
45+
_chainDb.Write(batch);
46+
47+
// Do fork from previous chain instead current if it's available and same as current.
48+
if (GetPreviousChainInfo(sourceChainId) is { } chainInfo &&
49+
chainInfo.Item2 == bpIndex)
50+
{
51+
ForkBlockIndexes(chainInfo.Item1, destinationChainId, branchpoint);
52+
return;
53+
}
54+
55+
_chainDb.Put(PreviousChainIdKey(destinationChainId), sourceChainId.ToByteArray());
56+
_chainDb.Put(
57+
PreviousChainIndexKey(destinationChainId),
58+
RocksDBStoreBitConverter.GetBytes(bpIndex)
59+
);
60+
_chainDb.Put(
61+
IndexCountKey(destinationChainId),
62+
RocksDBStoreBitConverter.GetBytes(bpIndex + 1)
63+
);
64+
65+
_chainDb.Put(ChainIdKey(destinationChainId), destinationChainId.ToByteArray());
66+
AddFork(sourceChainId, destinationChainId);
67+
}
68+
69+
/// <summary>
70+
/// Forks <see cref="Transaction"/> <see cref="Transaction.Nonce"/>s from
71+
/// <paramref name="sourceChainId"/> to <paramref name="destinationChainId"/>.
72+
/// </summary>
73+
/// <param name="sourceChainId">The chain <see cref="BlockChain.Id"/> of
74+
/// <see cref="Transaction"/> <see cref="Transaction.Nonce"/>s to fork.</param>
75+
/// <param name="destinationChainId">The chain <see cref="BlockChain.Id"/> of destination
76+
/// <see cref="Transaction"/> <see cref="Transaction.Nonce"/>s.</param>
77+
public void ForkTxNonces(Guid sourceChainId, Guid destinationChainId)
78+
{
79+
var writeBatch = new WriteBatch();
80+
bool exist = false;
81+
try
82+
{
83+
byte[] prefix = TxNonceKey(sourceChainId);
84+
foreach (Iterator it in IterateDb(_chainDb, prefix))
85+
{
86+
exist = true;
87+
Address address = new Address(it.Key().Skip(prefix.Length).ToArray());
88+
writeBatch.Put(TxNonceKey(destinationChainId, address), it.Value());
89+
if (writeBatch.Count() >= ForkWriteBatchSize)
90+
{
91+
_chainDb.Write(writeBatch);
92+
writeBatch.Dispose();
93+
writeBatch = new WriteBatch();
94+
}
95+
}
96+
}
97+
catch (Exception e)
98+
{
99+
LogUnexpectedException(nameof(ForkTxNonces), e);
100+
throw;
101+
}
102+
finally
103+
{
104+
if (exist)
105+
{
106+
_chainDb.Write(writeBatch);
107+
writeBatch.Dispose();
108+
}
109+
}
110+
}
111+
}
112+
}

src/Libplanet.RocksDBStore/RocksDBStore.cs

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ namespace Libplanet.RocksDBStore
8484
/// </list>
8585
/// </summary>
8686
/// <seealso cref="IStore"/>
87-
public class RocksDBStore : BaseStore
87+
public partial class RocksDBStore : BaseStore
8888
{
8989
private const string BlockDbRootPathName = "block";
9090
private const string BlockIndexDbName = "blockindex";
@@ -648,64 +648,6 @@ public override long AppendIndex(Guid chainId, BlockHash hash)
648648
return index;
649649
}
650650

651-
/// <summary>
652-
/// Forks block indexes from <paramref name="sourceChainId"/> to
653-
/// <paramref name="destinationChainId"/>.
654-
/// </summary>
655-
/// <param name="sourceChainId">The chain ID of block indexes to fork.</param>
656-
/// <param name="destinationChainId">The chain ID of destination block indexes.</param>
657-
/// <param name="branchpoint">The branchpoint <see cref="Block"/> to fork.</param>
658-
/// <seealso cref="IterateIndexes(Guid, int, int?)"/>
659-
/// <seealso cref="AppendIndex(Guid, BlockHash)"/>
660-
public void ForkBlockIndexes(
661-
Guid sourceChainId,
662-
Guid destinationChainId,
663-
BlockHash branchpoint
664-
)
665-
{
666-
BlockHash[] bottoms = IterateIndexes(sourceChainId, 0, 1, true).ToArray();
667-
BlockHash? genesisHash = bottoms.Any() ? bottoms[0] : (BlockHash?)null;
668-
669-
if (genesisHash is null || branchpoint.Equals(genesisHash))
670-
{
671-
return;
672-
}
673-
674-
using var batch = new WriteBatch();
675-
foreach (Iterator k in IterateDb(_chainDb, IndexKey(destinationChainId)))
676-
{
677-
batch.Delete(k.Key());
678-
}
679-
680-
if (!(GetBlockIndex(branchpoint) is { } bpIndex))
681-
{
682-
return;
683-
}
684-
685-
_chainDb.Write(batch);
686-
687-
// Do fork from previous chain instead current if it's available and same as current.
688-
if (GetPreviousChainInfo(sourceChainId) is { } chainInfo &&
689-
chainInfo.Item2 == bpIndex)
690-
{
691-
ForkBlockIndexes(chainInfo.Item1, destinationChainId, branchpoint);
692-
return;
693-
}
694-
695-
_chainDb.Put(PreviousChainIdKey(destinationChainId), sourceChainId.ToByteArray());
696-
_chainDb.Put(
697-
PreviousChainIndexKey(destinationChainId),
698-
RocksDBStoreBitConverter.GetBytes(bpIndex)
699-
);
700-
_chainDb.Put(
701-
IndexCountKey(destinationChainId),
702-
RocksDBStoreBitConverter.GetBytes(bpIndex + 1)
703-
);
704-
705-
_chainDb.Put(ChainIdKey(destinationChainId), destinationChainId.ToByteArray());
706-
AddFork(sourceChainId, destinationChainId);
707-
}
708-
709651
/// <inheritdoc/>
710652
public override Transaction? GetTransaction(TxId txid)
711653
{
@@ -1145,49 +1087,6 @@ public override void Dispose()
11451087
}
11461088
}
11471089

1148-
/// <summary>
1149-
/// Forks <see cref="Transaction"/> <see cref="Transaction.Nonce"/>s from
1150-
/// <paramref name="sourceChainId"/> to <paramref name="destinationChainId"/>.
1151-
/// </summary>
1152-
/// <param name="sourceChainId">The chain <see cref="BlockChain.Id"/> of
1153-
/// <see cref="Transaction"/> <see cref="Transaction.Nonce"/>s to fork.</param>
1154-
/// <param name="destinationChainId">The chain <see cref="BlockChain.Id"/> of destination
1155-
/// <see cref="Transaction"/> <see cref="Transaction.Nonce"/>s.</param>
1156-
public void ForkTxNonces(Guid sourceChainId, Guid destinationChainId)
1157-
{
1158-
var writeBatch = new WriteBatch();
1159-
bool exist = false;
1160-
try
1161-
{
1162-
byte[] prefix = TxNonceKey(sourceChainId);
1163-
foreach (Iterator it in IterateDb(_chainDb, prefix))
1164-
{
1165-
exist = true;
1166-
Address address = new Address(it.Key().Skip(prefix.Length).ToArray());
1167-
writeBatch.Put(TxNonceKey(destinationChainId, address), it.Value());
1168-
if (writeBatch.Count() >= ForkWriteBatchSize)
1169-
{
1170-
_chainDb.Write(writeBatch);
1171-
writeBatch.Dispose();
1172-
writeBatch = new WriteBatch();
1173-
}
1174-
}
1175-
}
1176-
catch (Exception e)
1177-
{
1178-
LogUnexpectedException(nameof(ForkTxNonces), e);
1179-
throw;
1180-
}
1181-
finally
1182-
{
1183-
if (exist)
1184-
{
1185-
_chainDb.Write(writeBatch);
1186-
writeBatch.Dispose();
1187-
}
1188-
}
1189-
}
1190-
11911090
/// <inheritdoc />
11921091
public override void PruneOutdatedChains(bool noopWithoutCanon = false)
11931092
{

0 commit comments

Comments
 (0)