Skip to content

Commit 138ad7c

Browse files
authored
Move to leveldb block store, provenheader and chainrepo (#90)
* Change store, provenheader and chainrepo * Add lock to proven header * Fix some more tests * Limit the number of unconsumed block count in consensus manager * Fix tests * Move finalized repo to consensus ns
1 parent c0c8422 commit 138ad7c

35 files changed

Lines changed: 422 additions & 470 deletions

src/Blockcore.Features.BlockStore.Tests/BlockRepositoryTests.cs

Lines changed: 76 additions & 117 deletions
Large diffs are not rendered by default.

src/Blockcore.Features.BlockStore/BlockRepository.cs

Lines changed: 135 additions & 147 deletions
Large diffs are not rendered by default.

src/Blockcore.Features.BlockStore/Pruning/PrunedBlockRepository.cs

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading.Tasks;
44
using Blockcore.Utilities;
55
using DBreeze.DataTypes;
6+
using LevelDB;
67
using Microsoft.Extensions.Logging;
78
using NBitcoin;
89

@@ -31,10 +32,7 @@ public PrunedBlockRepository(IBlockRepository blockRepository, DBreezeSerializer
3132
/// <inheritdoc />
3233
public void Initialize()
3334
{
34-
using (DBreeze.Transactions.Transaction transaction = this.blockRepository.DBreeze.GetTransaction())
35-
{
36-
this.LoadPrunedTip(transaction);
37-
}
35+
this.LoadPrunedTip(this.blockRepository.Leveldb);
3836
}
3937

4038
/// <inheritdoc />
@@ -48,11 +46,7 @@ public void PruneAndCompactDatabase(ChainedHeader blockRepositoryTip, Network ne
4846

4947
this.PrunedTip = new HashHeightPair(genesis.GetHash(), 0);
5048

51-
using (DBreeze.Transactions.Transaction transaction = this.blockRepository.DBreeze.GetTransaction())
52-
{
53-
transaction.Insert(BlockRepository.CommonTableName, prunedTipKey, this.dBreezeSerializer.Serialize(this.PrunedTip));
54-
transaction.Commit();
55-
}
49+
this.blockRepository.Leveldb.Put(DBH.Key(BlockRepository.CommonTableName, prunedTipKey), this.dBreezeSerializer.Serialize(this.PrunedTip));
5650
}
5751

5852
if (nodeInitializing)
@@ -110,17 +104,13 @@ private void PrepareDatabaseForCompacting(ChainedHeader blockRepositoryTip)
110104
this.UpdatePrunedTip(blockRepositoryTip.GetAncestor(upperHeight));
111105
}
112106

113-
private void LoadPrunedTip(DBreeze.Transactions.Transaction dbreezeTransaction)
107+
private void LoadPrunedTip(DB leveldb)
114108
{
115109
if (this.PrunedTip == null)
116110
{
117-
dbreezeTransaction.ValuesLazyLoadingIsOn = false;
118-
119-
Row<byte[], byte[]> row = dbreezeTransaction.Select<byte[], byte[]>(BlockRepository.CommonTableName, prunedTipKey);
120-
if (row.Exists)
121-
this.PrunedTip = this.dBreezeSerializer.Deserialize<HashHeightPair>(row.Value);
122-
123-
dbreezeTransaction.ValuesLazyLoadingIsOn = true;
111+
byte[] row = leveldb.Get(DBH.Key(BlockRepository.CommonTableName, prunedTipKey));
112+
if (row != null)
113+
this.PrunedTip = this.dBreezeSerializer.Deserialize<HashHeightPair>(row);
124114
}
125115
}
126116

@@ -131,34 +121,6 @@ private void CompactDataBase()
131121
{
132122
Task task = Task.Run(() =>
133123
{
134-
using (DBreeze.Transactions.Transaction dbreezeTransaction = this.blockRepository.DBreeze.GetTransaction())
135-
{
136-
dbreezeTransaction.SynchronizeTables(BlockRepository.BlockTableName, BlockRepository.TransactionTableName);
137-
138-
var tempBlocks = dbreezeTransaction.SelectDictionary<byte[], byte[]>(BlockRepository.BlockTableName);
139-
140-
if (tempBlocks.Count != 0)
141-
{
142-
this.logger.LogInformation($"{tempBlocks.Count} blocks will be copied to the pruned table.");
143-
144-
dbreezeTransaction.RemoveAllKeys(BlockRepository.BlockTableName, true);
145-
dbreezeTransaction.InsertDictionary(BlockRepository.BlockTableName, tempBlocks, false);
146-
147-
var tempTransactions = dbreezeTransaction.SelectDictionary<byte[], byte[]>(BlockRepository.TransactionTableName);
148-
if (tempTransactions.Count != 0)
149-
{
150-
this.logger.LogInformation($"{tempTransactions.Count} transactions will be copied to the pruned table.");
151-
dbreezeTransaction.RemoveAllKeys(BlockRepository.TransactionTableName, true);
152-
dbreezeTransaction.InsertDictionary(BlockRepository.TransactionTableName, tempTransactions, false);
153-
}
154-
155-
// Save the hash and height of where the node was pruned up to.
156-
dbreezeTransaction.Insert(BlockRepository.CommonTableName, prunedTipKey, this.dBreezeSerializer.Serialize(this.PrunedTip));
157-
}
158-
159-
dbreezeTransaction.Commit();
160-
}
161-
162124
return Task.CompletedTask;
163125
});
164126
}
@@ -169,4 +131,4 @@ public void UpdatePrunedTip(ChainedHeader tip)
169131
this.PrunedTip = new HashHeightPair(tip);
170132
}
171133
}
172-
}
134+
}

src/Blockcore.Features.Consensus.Tests/ProvenBlockHeaders/ProvenBlockHeaderRepositoryTests.cs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading.Tasks;
45
using Blockcore.Features.Consensus.ProvenBlockHeaders;
@@ -9,6 +10,7 @@
910
using DBreeze;
1011
using DBreeze.Utils;
1112
using FluentAssertions;
13+
using LevelDB;
1214
using Microsoft.Extensions.Logging;
1315
using Moq;
1416
using NBitcoin;
@@ -20,8 +22,8 @@ public class ProvenBlockHeaderRepositoryTests : LogsTestBase
2022
{
2123
private readonly Mock<ILoggerFactory> loggerFactory;
2224
private readonly DBreezeSerializer dBreezeSerializer;
23-
private const string ProvenBlockHeaderTable = "ProvenBlockHeader";
24-
private const string BlockHashTable = "BlockHashHeight";
25+
private static readonly byte ProvenBlockHeaderTable = 1;
26+
private static readonly byte BlockHashHeightTable = 2;
2527

2628
public ProvenBlockHeaderRepositoryTests() : base(KnownNetworks.StratisTest)
2729
{
@@ -58,14 +60,10 @@ public async Task PutAsync_WritesProvenBlockHeaderAndSavesBlockHashAsync()
5860
await repo.PutAsync(items, blockHashHeightPair);
5961
}
6062

61-
using (var engine = new DBreezeEngine(folder))
63+
using (var engine = new DB(new Options() { CreateIfMissing = true }, folder))
6264
{
63-
DBreeze.Transactions.Transaction txn = engine.GetTransaction();
64-
txn.SynchronizeTables(ProvenBlockHeaderTable);
65-
txn.ValuesLazyLoadingIsOn = false;
66-
67-
var headerOut = this.dBreezeSerializer.Deserialize<ProvenBlockHeader>(txn.Select<byte[], byte[]>(ProvenBlockHeaderTable, blockHashHeightPair.Height.ToBytes()).Value);
68-
var hashHeightPairOut = this.DBreezeSerializer.Deserialize<HashHeightPair>(txn.Select<byte[], byte[]>(BlockHashTable, new byte[0].ToBytes()).Value);
65+
var headerOut = this.dBreezeSerializer.Deserialize<ProvenBlockHeader>(engine.Get(DBH.Key(ProvenBlockHeaderTable, BitConverter.GetBytes(blockHashHeightPair.Height))));
66+
var hashHeightPairOut = this.DBreezeSerializer.Deserialize<HashHeightPair>(engine.Get(DBH.Key(BlockHashHeightTable, new byte[] { 1 })));
6967

7068
headerOut.Should().NotBeNull();
7169
headerOut.GetHash().Should().Be(provenBlockHeaderIn.GetHash());
@@ -93,13 +91,13 @@ public async Task PutAsync_Inserts_MultipleProvenBlockHeadersAsync()
9391
}
9492

9593
// Check the ProvenBlockHeader exists in the database.
96-
using (var engine = new DBreezeEngine(folder))
94+
using (var engine = new DB(new Options() { CreateIfMissing = true }, folder))
9795
{
98-
DBreeze.Transactions.Transaction txn = engine.GetTransaction();
99-
txn.SynchronizeTables(ProvenBlockHeaderTable);
100-
txn.ValuesLazyLoadingIsOn = false;
101-
102-
var headersOut = txn.SelectDictionary<byte[], byte[]>(ProvenBlockHeaderTable);
96+
var headersOut = new Dictionary<byte[], byte[]>();
97+
var enumeator = engine.GetEnumerator();
98+
while (enumeator.MoveNext())
99+
if (enumeator.Current.Key[0] == ProvenBlockHeaderTable)
100+
headersOut.Add(enumeator.Current.Key, enumeator.Current.Value);
103101

104102
headersOut.Keys.Count.Should().Be(2);
105103
this.dBreezeSerializer.Deserialize<ProvenBlockHeader>(headersOut.First().Value).GetHash().Should().Be(items[0].GetHash());
@@ -116,11 +114,9 @@ public async Task GetAsync_ReadsProvenBlockHeaderAsync()
116114

117115
int blockHeight = 1;
118116

119-
using (var engine = new DBreezeEngine(folder))
117+
using (var engine = new DB(new Options() { CreateIfMissing = true }, folder))
120118
{
121-
DBreeze.Transactions.Transaction txn = engine.GetTransaction();
122-
txn.Insert<byte[], byte[]>(ProvenBlockHeaderTable, blockHeight.ToBytes(), this.dBreezeSerializer.Serialize(headerIn));
123-
txn.Commit();
119+
engine.Put(DBH.Key(ProvenBlockHeaderTable, BitConverter.GetBytes(blockHeight)), this.dBreezeSerializer.Serialize(headerIn));
124120
}
125121

126122
// Query the repository for the item that was inserted in the above code.
@@ -138,12 +134,10 @@ public async Task GetAsync_WithWrongBlockHeightReturnsNullAsync()
138134
{
139135
string folder = CreateTestDir(this);
140136

141-
using (var engine = new DBreezeEngine(folder))
137+
using (var engine = new DB(new Options() { CreateIfMissing = true }, folder))
142138
{
143-
DBreeze.Transactions.Transaction txn = engine.GetTransaction();
144-
txn.Insert<byte[], byte[]>(ProvenBlockHeaderTable, 1.ToBytes(), this.dBreezeSerializer.Serialize(CreateNewProvenBlockHeaderMock()));
145-
txn.Insert<byte[], byte[]>(BlockHashTable, new byte[0], this.DBreezeSerializer.Serialize(new HashHeightPair(new uint256(), 1)));
146-
txn.Commit();
139+
engine.Put(DBH.Key(ProvenBlockHeaderTable, BitConverter.GetBytes(1)), this.dBreezeSerializer.Serialize(CreateNewProvenBlockHeaderMock()));
140+
engine.Put(DBH.Key(BlockHashHeightTable, new byte[0]), this.DBreezeSerializer.Serialize(new HashHeightPair(new uint256(), 1)));
147141
}
148142

149143
using (ProvenBlockHeaderRepository repo = this.SetupRepository(this.Network, folder))
@@ -159,7 +153,7 @@ public async Task GetAsync_WithWrongBlockHeightReturnsNullAsync()
159153
}
160154

161155
[Fact]
162-
public async Task PutAsync_Add_Ten_ProvenBlockHeaders_Dispose_On_Initialise_Repo_TipHeight_Should_Be_At_Last_Saved_TipAsync()
156+
public async Task PutAsync_DisposeOnInitialise_ShouldBeAtLastSavedTipAsync()
163157
{
164158
string folder = CreateTestDir(this);
165159

@@ -195,4 +189,4 @@ private ProvenBlockHeaderRepository SetupRepository(Network network, string fold
195189
return repo;
196190
}
197191
}
198-
}
192+
}

0 commit comments

Comments
 (0)