@@ -23,6 +23,7 @@ import (
2323
2424 "github.com/CortexFoundation/CortexTheseus/common"
2525 "github.com/CortexFoundation/CortexTheseus/core/types"
26+ "github.com/CortexFoundation/CortexTheseus/crypto"
2627 "github.com/CortexFoundation/CortexTheseus/db"
2728 "github.com/CortexFoundation/CortexTheseus/log"
2829 "github.com/CortexFoundation/CortexTheseus/params"
@@ -174,17 +175,23 @@ func WriteFastTrieProgress(db ctxcdb.KeyValueWriter, count uint64) {
174175// ReadHeaderRLP retrieves a block header in its raw RLP database encoding.
175176func ReadHeaderRLP (db ctxcdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
176177 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- }
178+ if len (data ) > 0 && crypto .Keccak256Hash (data ) == hash {
179+ return data
180+ }
181+ // Then try to look up the data in leveldb.
182+ data , _ = db .Get (headerKey (number , hash ))
183+ if len (data ) > 0 {
184+ return data
185+ }
186+ // In the background freezer is moving data from leveldb to flatten files.
187+ // So during the first check for ancient db, the data is not yet in there,
188+ // but when we reach into leveldb, the data was already moved. That would
189+ // result in a not found error.
190+ data , _ = db .Ancient (freezerHeaderTable , number )
191+ if len (data ) > 0 && crypto .Keccak256Hash (data ) == hash {
192+ return data
186193 }
187- return data
194+ return nil
188195}
189196
190197// HasHeader verifies the existence of a block header corresponding to the hash.
@@ -252,17 +259,29 @@ func deleteHeaderWithoutNumber(db ctxcdb.KeyValueWriter, hash common.Hash, numbe
252259// ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
253260func ReadBodyRLP (db ctxcdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
254261 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 )
262+ if len (data ) > 0 {
263+ h , _ := db .Ancient (freezerHashTable , number )
264+ if common .BytesToHash (h ) == hash {
265+ return data
266+ }
267+ }
268+ // Then try to look up the data in leveldb.
269+ data , _ = db .Get (blockBodyKey (number , hash ))
270+ if len (data ) > 0 {
271+ return data
272+ }
273+ // In the background freezer is moving data from leveldb to flatten files.
274+ // So during the first check for ancient db, the data is not yet in there,
275+ // but when we reach into leveldb, the data was already moved. That would
276+ // result in a not found error.
277+ data , _ = db .Ancient (freezerBodiesTable , number )
278+ if len (data ) > 0 {
279+ h , _ := db .Ancient (freezerHashTable , number )
280+ if common .BytesToHash (h ) == hash {
281+ return data
263282 }
264283 }
265- return data
284+ return nil
266285}
267286
268287// WriteBodyRLP stores an RLP encoded block body into the database.
@@ -316,17 +335,29 @@ func DeleteBody(db ctxcdb.KeyValueWriter, hash common.Hash, number uint64) {
316335// ReadTdRLP retrieves a block's total difficulty corresponding to the hash in RLP encoding.
317336func ReadTdRLP (db ctxcdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
318337 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 )
338+ if len (data ) > 0 {
339+ h , _ := db .Ancient (freezerHashTable , number )
340+ if common .BytesToHash (h ) == hash {
341+ return data
342+ }
343+ }
344+ // Then try to look up the data in leveldb.
345+ data , _ = db .Get (headerTDKey (number , hash ))
346+ if len (data ) > 0 {
347+ return data
348+ }
349+ // In the background freezer is moving data from leveldb to flatten files.
350+ // So during the first check for ancient db, the data is not yet in there,
351+ // but when we reach into leveldb, the data was already moved. That would
352+ // result in a not found error.
353+ data , _ = db .Ancient (freezerDifficultyTable , number )
354+ if len (data ) > 0 {
355+ h , _ := db .Ancient (freezerHashTable , number )
356+ if common .BytesToHash (h ) == hash {
357+ return data
327358 }
328359 }
329- return data
360+ return nil
330361}
331362
332363func ReadTd (db ctxcdb.Reader , hash common.Hash , number uint64 ) * big.Int {
@@ -372,17 +403,29 @@ func HasReceipts(db ctxcdb.Reader, hash common.Hash, number uint64) bool {
372403
373404func ReadReceiptsRLP (db ctxcdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
374405 data , _ := db .Ancient (freezerReceiptTable , number )
375- if len (data ) == 0 {
376- data , _ = db .Get (blockReceiptsKey (number , hash ))
377- // In the background freezer is moving data from leveldb to flatten files.
378- // So during the first check for ancient db, the data is not yet in there,
379- // but when we reach into leveldb, the data was already moved. That would
380- // result in a not found error.
381- if len (data ) == 0 {
382- data , _ = db .Ancient (freezerReceiptTable , number )
406+ if len (data ) > 0 {
407+ h , _ := db .Ancient (freezerHashTable , number )
408+ if common .BytesToHash (h ) == hash {
409+ return data
410+ }
411+ }
412+ // Then try to look up the data in leveldb.
413+ data , _ = db .Get (blockReceiptsKey (number , hash ))
414+ if len (data ) > 0 {
415+ return data
416+ }
417+ // In the background freezer is moving data from leveldb to flatten files.
418+ // So during the first check for ancient db, the data is not yet in there,
419+ // but when we reach into leveldb, the data was already moved. That would
420+ // result in a not found error.
421+ 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
383426 }
384427 }
385- return data
428+ return nil
386429}
387430
388431// WriteAncientBlock writes entire block data into ancient store and returns the total written size.
0 commit comments