14
14
using Nethermind . Blockchain . Receipts ;
15
15
using Nethermind . Core ;
16
16
using Nethermind . Core . Crypto ;
17
- using Nethermind . Core . Events ;
18
17
using Nethermind . Core . Extensions ;
19
18
using Nethermind . Db ;
20
19
using Nethermind . Int256 ;
@@ -33,8 +32,6 @@ public class ReceiptMigration : IDatabaseMigration, IReceiptsMigration
33
32
private readonly ILogger _logger ;
34
33
private CancellationTokenSource ? _cancellationTokenSource ;
35
34
internal Task ? _migrationTask ;
36
- private Stopwatch ? _stopwatch ;
37
- private long _toBlock ;
38
35
39
36
private readonly ProgressLogger _progressLogger ;
40
37
[ NotNull ]
@@ -89,13 +86,24 @@ ILogManager logManager
89
86
_progressLogger = new ProgressLogger ( "Receipts migration" , logManager ) ;
90
87
}
91
88
92
- public async Task < bool > Run ( long blockNumber )
89
+ // Actually start running it.
90
+ public async Task < bool > Run ( long from , long to )
93
91
{
94
92
_cancellationTokenSource ? . Cancel ( ) ;
95
- await ( _migrationTask ?? Task . CompletedTask ) ;
93
+ try
94
+ {
95
+ await ( _migrationTask ?? Task . CompletedTask ) ;
96
+ }
97
+ catch ( OperationCanceledException )
98
+ {
99
+ }
100
+
96
101
_cancellationTokenSource = new CancellationTokenSource ( ) ;
97
- _receiptStorage . MigratedBlockNumber = Math . Min ( Math . Max ( _receiptStorage . MigratedBlockNumber , blockNumber ) , ( _blockTree . Head ? . Number ?? 0 ) + 1 ) ;
98
- _migrationTask = DoRun ( _cancellationTokenSource . Token ) ;
102
+ _migrationTask = Task . Run ( async ( ) =>
103
+ {
104
+ await _syncModeSelector . WaitUntilMode ( CanMigrate , _cancellationTokenSource . Token ) ;
105
+ RunMigration ( from , to , false , _cancellationTokenSource . Token ) ;
106
+ } ) ;
99
107
return _receiptConfig . StoreReceipts && _receiptConfig . ReceiptsMigration ;
100
108
}
101
109
public async Task Run ( CancellationToken cancellationToken )
@@ -105,25 +113,9 @@ public async Task Run(CancellationToken cancellationToken)
105
113
if ( _receiptConfig . ReceiptsMigration )
106
114
{
107
115
ResetMigrationIndexIfNeeded ( ) ;
108
- await DoRun ( cancellationToken ) ;
109
- }
110
- }
111
- }
112
-
113
- private async Task DoRun ( CancellationToken cancellationToken )
114
- {
115
- if ( _receiptConfig . StoreReceipts )
116
- {
117
- if ( ! CanMigrate ( _syncModeSelector . Current ) )
118
- {
119
- await Wait . ForEventCondition < SyncModeChangedEventArgs > (
120
- cancellationToken ,
121
- ( e ) => _syncModeSelector . Changed += e ,
122
- ( e ) => _syncModeSelector . Changed -= e ,
123
- ( arg ) => CanMigrate ( arg . Current ) ) ;
116
+ await _syncModeSelector . WaitUntilMode ( CanMigrate , cancellationToken ) ;
117
+ RunIfNeeded ( cancellationToken ) ;
124
118
}
125
-
126
- RunIfNeeded ( cancellationToken ) ;
127
119
}
128
120
}
129
121
@@ -137,21 +129,16 @@ private void RunIfNeeded(CancellationToken cancellationToken)
137
129
? _blockTree . Head ? . Number ?? 0
138
130
: _blockTree . BestKnownNumber
139
131
: _receiptStorage . MigratedBlockNumber - 1 ;
140
- _toBlock = migrateToBlockNumber ;
141
-
142
- _logger . Warn ( $ "Running migration to { _toBlock } ") ;
143
132
144
- if ( _toBlock > 0 )
133
+ if ( migrateToBlockNumber > 0 )
145
134
{
146
- _stopwatch = Stopwatch . StartNew ( ) ;
147
135
try
148
136
{
149
- RunMigration ( cancellationToken ) ;
137
+ RunMigration ( 0 , migrateToBlockNumber , true , cancellationToken ) ;
150
138
}
151
139
catch ( Exception e )
152
140
{
153
- _stopwatch . Stop ( ) ;
154
- _logger . Error ( GetLogMessage ( "failed" , $ "Error: { e } ") , e ) ;
141
+ _logger . Error ( "Error running receipt migration" , e ) ;
155
142
}
156
143
}
157
144
else
@@ -160,19 +147,20 @@ private void RunIfNeeded(CancellationToken cancellationToken)
160
147
}
161
148
}
162
149
163
- private void RunMigration ( CancellationToken token )
150
+ private void RunMigration ( long from , long to , bool updateReceiptMigrationPointer , CancellationToken token )
164
151
{
165
- long synced = 1 ;
152
+ from = Math . Min ( from , to ) ;
153
+ long synced = 0 ;
166
154
167
- _progressLogger . Reset ( synced , _toBlock ) ;
155
+ if ( _logger . IsWarn ) _logger . Warn ( $ "Running migration from { from } to { to } " ) ;
168
156
169
- if ( _logger . IsInfo ) _logger . Info ( GetLogMessage ( "started" ) ) ;
157
+ _progressLogger . Reset ( synced , to - from + 1 ) ;
170
158
171
159
using Timer timer = new ( 1000 ) ;
172
160
timer . Enabled = true ;
173
161
timer . Elapsed += ( _ , _ ) =>
174
162
{
175
- if ( _logger . IsInfo ) _logger . Info ( GetLogMessage ( "in progress" ) ) ;
163
+ _progressLogger . LogProgress ( ) ;
176
164
} ;
177
165
178
166
try
@@ -183,7 +171,8 @@ private void RunMigration(CancellationToken token)
183
171
parallelism = Environment . ProcessorCount ;
184
172
}
185
173
186
- GetBlockBodiesForMigration ( token ) . AsParallel ( ) . WithDegreeOfParallelism ( parallelism ) . ForAll ( ( item ) =>
174
+ GetBlockBodiesForMigration ( from , to , updateReceiptMigrationPointer , token )
175
+ . AsParallel ( ) . WithDegreeOfParallelism ( parallelism ) . ForAll ( ( item ) =>
187
176
{
188
177
( long blockNum , Hash256 blockHash ) = item ;
189
178
Block ? block = _blockTree . FindBlock ( blockHash ! , BlockTreeLookupOptions . None ) ;
@@ -204,30 +193,29 @@ private void RunMigration(CancellationToken token)
204
193
205
194
if ( ! token . IsCancellationRequested )
206
195
{
207
- if ( _logger . IsInfo ) _logger . Info ( GetLogMessage ( "Compacting receipts database" ) ) ;
196
+ if ( _logger . IsInfo ) _logger . Info ( "Compacting receipts database" ) ;
208
197
_receiptsDb . Compact ( ) ;
209
- if ( _logger . IsInfo ) _logger . Info ( GetLogMessage ( "Compacting receipts tx index database" ) ) ;
198
+ if ( _logger . IsInfo ) _logger . Info ( "Compacting receipts tx index database" ) ;
210
199
_txIndexDb . Compact ( ) ;
211
- if ( _logger . IsInfo ) _logger . Info ( GetLogMessage ( "Compacting receipts block database" ) ) ;
200
+ if ( _logger . IsInfo ) _logger . Info ( "Compacting receipts block database" ) ;
212
201
_receiptsBlockDb . Compact ( ) ;
213
202
}
214
203
}
215
204
finally
216
205
{
217
206
_progressLogger . MarkEnd ( ) ;
218
- _stopwatch ? . Stop ( ) ;
219
207
timer . Stop ( ) ;
220
208
}
221
209
222
210
if ( ! token . IsCancellationRequested )
223
211
{
224
- if ( _logger . IsInfo ) _logger . Info ( GetLogMessage ( " finished") ) ;
212
+ if ( _logger . IsInfo ) _logger . Info ( "Receipt migration finished") ;
225
213
}
226
214
}
227
215
228
216
Block GetMissingBlock ( long i , Hash256 ? blockHash )
229
217
{
230
- if ( _logger . IsDebug ) _logger . Debug ( GetLogMessage ( "warning" , $ "Block { i } not found. Logs will not be searchable for this block.") ) ;
218
+ if ( _logger . IsDebug ) _logger . Debug ( $ "Block { i } not found. Logs will not be searchable for this block.") ;
231
219
Block emptyBlock = EmptyBlock . Get ( ) ;
232
220
emptyBlock . Header . Number = i ;
233
221
emptyBlock . Header . Hash = blockHash ;
@@ -239,7 +227,7 @@ static void ReturnMissingBlock(Block emptyBlock)
239
227
EmptyBlock . Return ( emptyBlock ) ;
240
228
}
241
229
242
- IEnumerable < ( long , Hash256 ) > GetBlockBodiesForMigration ( CancellationToken token )
230
+ IEnumerable < ( long , Hash256 ) > GetBlockBodiesForMigration ( long from , long to , bool updateReceiptMigrationPointer , CancellationToken token )
243
231
{
244
232
bool TryGetMainChainBlockHashFromLevel ( long number , out Hash256 ? blockHash )
245
233
{
@@ -266,11 +254,11 @@ bool TryGetMainChainBlockHashFromLevel(long number, out Hash256? blockHash)
266
254
}
267
255
}
268
256
269
- for ( long i = _toBlock - 1 ; i > 0 ; i -- )
257
+ for ( long i = to ; i >= from ; i -- )
270
258
{
271
259
if ( token . IsCancellationRequested )
272
260
{
273
- if ( _logger . IsInfo ) _logger . Info ( GetLogMessage ( " cancelled") ) ;
261
+ if ( _logger . IsInfo ) _logger . Info ( "Receipt migration cancelled") ;
274
262
yield break ;
275
263
}
276
264
@@ -279,7 +267,7 @@ bool TryGetMainChainBlockHashFromLevel(long number, out Hash256? blockHash)
279
267
yield return ( i , blockHash ! ) ;
280
268
}
281
269
282
- if ( _receiptStorage . MigratedBlockNumber > i )
270
+ if ( updateReceiptMigrationPointer && _receiptStorage . MigratedBlockNumber > i )
283
271
{
284
272
_receiptStorage . MigratedBlockNumber = i ;
285
273
}
@@ -295,11 +283,12 @@ private void MigrateBlock(Block block)
295
283
296
284
if ( notNullReceipts . Length == 0 ) return ;
297
285
286
+ // This should set the new rlp and tx index depending on config.
298
287
_receiptStorage . Insert ( block , notNullReceipts ) ;
299
288
300
- // I guess some old schema need this
289
+ // It used to be that the tx index is stored in the default column so we are moving it into transactions column
301
290
{
302
- using IWriteBatch writeBatch = _receiptsDb . StartWriteBatch ( ) . GetColumnBatch ( ReceiptsColumns . Transactions ) ;
291
+ using IWriteBatch writeBatch = _receiptsDb . StartWriteBatch ( ) . GetColumnBatch ( ReceiptsColumns . Default ) ;
303
292
for ( int i = 0 ; i < notNullReceipts . Length ; i ++ )
304
293
{
305
294
writeBatch [ notNullReceipts [ i ] . TxHash ! . Bytes ] = null ;
@@ -324,8 +313,7 @@ private void MigrateBlock(Block block)
324
313
if ( notNullReceipts . Length != receipts . Length )
325
314
{
326
315
if ( _logger . IsWarn )
327
- _logger . Warn ( GetLogMessage ( "warning" ,
328
- $ "Block { block . ToString ( Block . Format . FullHashAndNumber ) } is missing { receipts . Length - notNullReceipts . Length } of { receipts . Length } receipts!") ) ;
316
+ _logger . Warn ( $ "Block { block . ToString ( Block . Format . FullHashAndNumber ) } is missing { receipts . Length - notNullReceipts . Length } of { receipts . Length } receipts!") ;
329
317
}
330
318
}
331
319
@@ -382,13 +370,6 @@ private bool IsMigrationNeeded(long blockNumber, Hash256 blockHash, TxReceipt[]
382
370
return _receiptConfig . CompactReceiptStore != isCompactEncoding ;
383
371
}
384
372
385
- private string GetLogMessage ( string status , string ? suffix = null )
386
- {
387
- string message = $ "ReceiptsDb migration { status } | { _stopwatch ? . Elapsed : d\\:hh\\:mm\\:ss} | { _progressLogger . CurrentValue . ToString ( ) . PadLeft ( _toBlock . ToString ( ) . Length ) } / { _toBlock } blocks migrated. | current { _progressLogger . CurrentPerSecond : F2} Blk/s | total { _progressLogger . TotalPerSecond : F2} Blk/s. { suffix } ";
388
- _progressLogger . SetMeasuringPoint ( ) ;
389
- return message ;
390
- }
391
-
392
373
private class EmptyBlockObjectPolicy : IPooledObjectPolicy < Block >
393
374
{
394
375
public Block Create ( )
0 commit comments