1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using System . Threading . Tasks ;
45using Blockcore . Features . Consensus . ProvenBlockHeaders ;
910using DBreeze ;
1011using DBreeze . Utils ;
1112using FluentAssertions ;
13+ using LevelDB ;
1214using Microsoft . Extensions . Logging ;
1315using Moq ;
1416using 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