@@ -23,6 +23,7 @@ import (
23
23
24
24
"github.com/ethereum/go-ethereum/common"
25
25
"github.com/ethereum/go-ethereum/core/types"
26
+ "github.com/ethereum/go-ethereum/crypto"
26
27
"github.com/ethereum/go-ethereum/ethdb"
27
28
"github.com/ethereum/go-ethereum/log"
28
29
"github.com/ethereum/go-ethereum/params"
@@ -173,18 +174,27 @@ func WriteFastTrieProgress(db ethdb.KeyValueWriter, count uint64) {
173
174
174
175
// ReadHeaderRLP retrieves a block header in its raw RLP database encoding.
175
176
func ReadHeaderRLP (db ethdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
177
+ // First try to look up the data in ancient database. Extra hash
178
+ // comparison is necessary since ancient database only maintains
179
+ // the canonical data.
176
180
data , _ := db .Ancient (freezerHeaderTable , number )
177
- if len (data ) == 0 {
178
- data , _ = db .Get (headerKey (number , hash ))
179
- // In the background freezer is moving data from leveldb to flatten files.
180
- // So during the first check for ancient db, the data is not yet in there,
181
- // but when we reach into leveldb, the data was already moved. That would
182
- // result in a not found error.
183
- if len (data ) == 0 {
184
- data , _ = db .Ancient (freezerHeaderTable , number )
185
- }
181
+ if len (data ) > 0 && crypto .Keccak256Hash (data ) == hash {
182
+ return data
183
+ }
184
+ // Then try to look up the data in leveldb.
185
+ data , _ = db .Get (headerKey (number , hash ))
186
+ if len (data ) > 0 {
187
+ return data
188
+ }
189
+ // In the background freezer is moving data from leveldb to flatten files.
190
+ // So during the first check for ancient db, the data is not yet in there,
191
+ // but when we reach into leveldb, the data was already moved. That would
192
+ // result in a not found error.
193
+ data , _ = db .Ancient (freezerHeaderTable , number )
194
+ if len (data ) > 0 && crypto .Keccak256Hash (data ) == hash {
195
+ return data
186
196
}
187
- return data
197
+ return nil // Can't find the data anywhere.
188
198
}
189
199
190
200
// HasHeader verifies the existence of a block header corresponding to the hash.
@@ -251,18 +261,33 @@ func deleteHeaderWithoutNumber(db ethdb.KeyValueWriter, hash common.Hash, number
251
261
252
262
// ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
253
263
func ReadBodyRLP (db ethdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
264
+ // First try to look up the data in ancient database. Extra hash
265
+ // comparison is necessary since ancient database only maintains
266
+ // the canonical data.
254
267
data , _ := db .Ancient (freezerBodiesTable , number )
255
- if len (data ) == 0 {
256
- data , _ = db .Get (blockBodyKey (number , hash ))
257
- // In the background freezer is moving data from leveldb to flatten files.
258
- // So during the first check for ancient db, the data is not yet in there,
259
- // but when we reach into leveldb, the data was already moved. That would
260
- // result in a not found error.
261
- if len (data ) == 0 {
262
- data , _ = db .Ancient (freezerBodiesTable , number )
268
+ if len (data ) > 0 {
269
+ h , _ := db .Ancient (freezerHashTable , number )
270
+ if common .BytesToHash (h ) == hash {
271
+ return data
272
+ }
273
+ }
274
+ // Then try to look up the data in leveldb.
275
+ data , _ = db .Get (blockBodyKey (number , hash ))
276
+ if len (data ) > 0 {
277
+ return data
278
+ }
279
+ // In the background freezer is moving data from leveldb to flatten files.
280
+ // So during the first check for ancient db, the data is not yet in there,
281
+ // but when we reach into leveldb, the data was already moved. That would
282
+ // result in a not found error.
283
+ data , _ = db .Ancient (freezerBodiesTable , number )
284
+ if len (data ) > 0 {
285
+ h , _ := db .Ancient (freezerHashTable , number )
286
+ if common .BytesToHash (h ) == hash {
287
+ return data
263
288
}
264
289
}
265
- return data
290
+ return nil // Can't find the data anywhere.
266
291
}
267
292
268
293
// WriteBodyRLP stores an RLP encoded block body into the database.
@@ -315,18 +340,33 @@ func DeleteBody(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
315
340
316
341
// ReadTdRLP retrieves a block's total difficulty corresponding to the hash in RLP encoding.
317
342
func ReadTdRLP (db ethdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
343
+ // First try to look up the data in ancient database. Extra hash
344
+ // comparison is necessary since ancient database only maintains
345
+ // the canonical data.
318
346
data , _ := db .Ancient (freezerDifficultyTable , number )
319
- if len (data ) == 0 {
320
- data , _ = db .Get (headerTDKey (number , hash ))
321
- // In the background freezer is moving data from leveldb to flatten files.
322
- // So during the first check for ancient db, the data is not yet in there,
323
- // but when we reach into leveldb, the data was already moved. That would
324
- // result in a not found error.
325
- if len (data ) == 0 {
326
- data , _ = db .Ancient (freezerDifficultyTable , number )
347
+ if len (data ) > 0 {
348
+ h , _ := db .Ancient (freezerHashTable , number )
349
+ if common .BytesToHash (h ) == hash {
350
+ return data
351
+ }
352
+ }
353
+ // Then try to look up the data in leveldb.
354
+ data , _ = db .Get (headerTDKey (number , hash ))
355
+ if len (data ) > 0 {
356
+ return data
357
+ }
358
+ // In the background freezer is moving data from leveldb to flatten files.
359
+ // So during the first check for ancient db, the data is not yet in there,
360
+ // but when we reach into leveldb, the data was already moved. That would
361
+ // result in a not found error.
362
+ data , _ = db .Ancient (freezerDifficultyTable , number )
363
+ if len (data ) > 0 {
364
+ h , _ := db .Ancient (freezerHashTable , number )
365
+ if common .BytesToHash (h ) == hash {
366
+ return data
327
367
}
328
368
}
329
- return data
369
+ return nil // Can't find the data anywhere.
330
370
}
331
371
332
372
// ReadTd retrieves a block's total difficulty corresponding to the hash.
@@ -375,18 +415,33 @@ func HasReceipts(db ethdb.Reader, hash common.Hash, number uint64) bool {
375
415
376
416
// ReadReceiptsRLP retrieves all the transaction receipts belonging to a block in RLP encoding.
377
417
func ReadReceiptsRLP (db ethdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
418
+ // First try to look up the data in ancient database. Extra hash
419
+ // comparison is necessary since ancient database only maintains
420
+ // the canonical data.
378
421
data , _ := db .Ancient (freezerReceiptTable , number )
379
- if len (data ) == 0 {
380
- data , _ = db .Get (blockReceiptsKey (number , hash ))
381
- // In the background freezer is moving data from leveldb to flatten files.
382
- // So during the first check for ancient db, the data is not yet in there,
383
- // but when we reach into leveldb, the data was already moved. That would
384
- // result in a not found error.
385
- if len (data ) == 0 {
386
- data , _ = db .Ancient (freezerReceiptTable , number )
422
+ if len (data ) > 0 {
423
+ h , _ := db .Ancient (freezerHashTable , number )
424
+ if common .BytesToHash (h ) == hash {
425
+ return data
426
+ }
427
+ }
428
+ // Then try to look up the data in leveldb.
429
+ data , _ = db .Get (blockReceiptsKey (number , hash ))
430
+ if len (data ) > 0 {
431
+ return data
432
+ }
433
+ // In the background freezer is moving data from leveldb to flatten files.
434
+ // So during the first check for ancient db, the data is not yet in there,
435
+ // but when we reach into leveldb, the data was already moved. That would
436
+ // result in a not found error.
437
+ data , _ = db .Ancient (freezerReceiptTable , number )
438
+ if len (data ) > 0 {
439
+ h , _ := db .Ancient (freezerHashTable , number )
440
+ if common .BytesToHash (h ) == hash {
441
+ return data
387
442
}
388
443
}
389
- return data
444
+ return nil // Can't find the data anywhere.
390
445
}
391
446
392
447
// ReadRawReceipts retrieves all the transaction receipts belonging to a block.
0 commit comments