@@ -30,8 +30,11 @@ import (
3030)
3131
3232// ReadCanonicalHash retrieves the hash assigned to a canonical block number.
33- func ReadCanonicalHash (db ethdb.Reader , number uint64 ) common.Hash {
34- data , _ := db .Get (headerHashKey (number ))
33+ func ReadCanonicalHash (db ethdb.AncientReader , number uint64 ) common.Hash {
34+ data , _ := db .Ancient ("hashes" , number )
35+ if len (data ) == 0 {
36+ data , _ = db .Get (headerHashKey (number ))
37+ }
3538 if len (data ) == 0 {
3639 return common.Hash {}
3740 }
@@ -52,6 +55,24 @@ func DeleteCanonicalHash(db ethdb.Writer, number uint64) {
5255 }
5356}
5457
58+ // readAllHashes retrieves all the hashes assigned to blocks at a certain heights,
59+ // both canonical and reorged forks included.
60+ //
61+ // This method is a helper for the chain reader. It should never be exposed to the
62+ // outside world.
63+ func readAllHashes (db ethdb.Iteratee , number uint64 ) []common.Hash {
64+ prefix := headerKeyPrefix (number )
65+
66+ hashes := make ([]common.Hash , 0 , 1 )
67+ it := db .NewIteratorWithPrefix (prefix )
68+ for it .Next () {
69+ if key := it .Key (); len (key ) == len (prefix )+ 32 {
70+ hashes = append (hashes , common .BytesToHash (key [len (key )- 32 :]))
71+ }
72+ }
73+ return hashes
74+ }
75+
5576// ReadHeaderNumber returns the header number assigned to a hash.
5677func ReadHeaderNumber (db ethdb.Reader , hash common.Hash ) * uint64 {
5778 data , _ := db .Get (headerNumberKey (hash ))
@@ -129,21 +150,27 @@ func WriteFastTrieProgress(db ethdb.Writer, count uint64) {
129150}
130151
131152// ReadHeaderRLP retrieves a block header in its raw RLP database encoding.
132- func ReadHeaderRLP (db ethdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
133- data , _ := db .Get (headerKey (number , hash ))
153+ func ReadHeaderRLP (db ethdb.AncientReader , hash common.Hash , number uint64 ) rlp.RawValue {
154+ data , _ := db .Ancient ("headers" , number )
155+ if len (data ) == 0 {
156+ data , _ = db .Get (headerKey (number , hash ))
157+ }
134158 return data
135159}
136160
137161// HasHeader verifies the existence of a block header corresponding to the hash.
138- func HasHeader (db ethdb.Reader , hash common.Hash , number uint64 ) bool {
162+ func HasHeader (db ethdb.AncientReader , hash common.Hash , number uint64 ) bool {
163+ if has , err := db .Ancient ("hashes" , number ); err == nil && common .BytesToHash (has ) == hash {
164+ return true
165+ }
139166 if has , err := db .Has (headerKey (number , hash )); ! has || err != nil {
140167 return false
141168 }
142169 return true
143170}
144171
145172// ReadHeader retrieves the block header corresponding to the hash.
146- func ReadHeader (db ethdb.Reader , hash common.Hash , number uint64 ) * types.Header {
173+ func ReadHeader (db ethdb.AncientReader , hash common.Hash , number uint64 ) * types.Header {
147174 data := ReadHeaderRLP (db , hash , number )
148175 if len (data ) == 0 {
149176 return nil
@@ -197,8 +224,11 @@ func deleteHeaderWithoutNumber(db ethdb.Writer, hash common.Hash, number uint64)
197224}
198225
199226// ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
200- func ReadBodyRLP (db ethdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
201- data , _ := db .Get (blockBodyKey (number , hash ))
227+ func ReadBodyRLP (db ethdb.AncientReader , hash common.Hash , number uint64 ) rlp.RawValue {
228+ data , _ := db .Ancient ("bodies" , number )
229+ if len (data ) == 0 {
230+ data , _ = db .Get (blockBodyKey (number , hash ))
231+ }
202232 return data
203233}
204234
@@ -210,15 +240,18 @@ func WriteBodyRLP(db ethdb.Writer, hash common.Hash, number uint64, rlp rlp.RawV
210240}
211241
212242// HasBody verifies the existence of a block body corresponding to the hash.
213- func HasBody (db ethdb.Reader , hash common.Hash , number uint64 ) bool {
243+ func HasBody (db ethdb.AncientReader , hash common.Hash , number uint64 ) bool {
244+ if has , err := db .Ancient ("hashes" , number ); err == nil && common .BytesToHash (has ) == hash {
245+ return true
246+ }
214247 if has , err := db .Has (blockBodyKey (number , hash )); ! has || err != nil {
215248 return false
216249 }
217250 return true
218251}
219252
220253// ReadBody retrieves the block body corresponding to the hash.
221- func ReadBody (db ethdb.Reader , hash common.Hash , number uint64 ) * types.Body {
254+ func ReadBody (db ethdb.AncientReader , hash common.Hash , number uint64 ) * types.Body {
222255 data := ReadBodyRLP (db , hash , number )
223256 if len (data ) == 0 {
224257 return nil
@@ -248,13 +281,16 @@ func DeleteBody(db ethdb.Writer, hash common.Hash, number uint64) {
248281}
249282
250283// ReadTdRLP retrieves a block's total difficulty corresponding to the hash in RLP encoding.
251- func ReadTdRLP (db ethdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
252- data , _ := db .Get (headerTDKey (number , hash ))
284+ func ReadTdRLP (db ethdb.AncientReader , hash common.Hash , number uint64 ) rlp.RawValue {
285+ data , _ := db .Ancient ("diffs" , number )
286+ if len (data ) == 0 {
287+ data , _ = db .Get (headerTDKey (number , hash ))
288+ }
253289 return data
254290}
255291
256292// ReadTd retrieves a block's total difficulty corresponding to the hash.
257- func ReadTd (db ethdb.Reader , hash common.Hash , number uint64 ) * big.Int {
293+ func ReadTd (db ethdb.AncientReader , hash common.Hash , number uint64 ) * big.Int {
258294 data := ReadTdRLP (db , hash , number )
259295 if len (data ) == 0 {
260296 return nil
@@ -287,23 +323,29 @@ func DeleteTd(db ethdb.Writer, hash common.Hash, number uint64) {
287323
288324// HasReceipts verifies the existence of all the transaction receipts belonging
289325// to a block.
290- func HasReceipts (db ethdb.Reader , hash common.Hash , number uint64 ) bool {
326+ func HasReceipts (db ethdb.AncientReader , hash common.Hash , number uint64 ) bool {
327+ if has , err := db .Ancient ("hashes" , number ); err == nil && common .BytesToHash (has ) == hash {
328+ return true
329+ }
291330 if has , err := db .Has (blockReceiptsKey (number , hash )); ! has || err != nil {
292331 return false
293332 }
294333 return true
295334}
296335
297336// ReadReceiptsRLP retrieves all the transaction receipts belonging to a block in RLP encoding.
298- func ReadReceiptsRLP (db ethdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
299- data , _ := db .Get (blockReceiptsKey (number , hash ))
337+ func ReadReceiptsRLP (db ethdb.AncientReader , hash common.Hash , number uint64 ) rlp.RawValue {
338+ data , _ := db .Ancient ("receipts" , number )
339+ if len (data ) == 0 {
340+ data , _ = db .Get (blockReceiptsKey (number , hash ))
341+ }
300342 return data
301343}
302344
303345// ReadRawReceipts retrieves all the transaction receipts belonging to a block.
304346// The receipt metadata fields are not guaranteed to be populated, so they
305347// should not be used. Use ReadReceipts instead if the metadata is needed.
306- func ReadRawReceipts (db ethdb.Reader , hash common.Hash , number uint64 ) types.Receipts {
348+ func ReadRawReceipts (db ethdb.AncientReader , hash common.Hash , number uint64 ) types.Receipts {
307349 // Retrieve the flattened receipt slice
308350 data := ReadReceiptsRLP (db , hash , number )
309351 if len (data ) == 0 {
@@ -329,7 +371,7 @@ func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Rec
329371// The current implementation populates these metadata fields by reading the receipts'
330372// corresponding block body, so if the block body is not found it will return nil even
331373// if the receipt itself is stored.
332- func ReadReceipts (db ethdb.Reader , hash common.Hash , number uint64 , config * params.ChainConfig ) types.Receipts {
374+ func ReadReceipts (db ethdb.AncientReader , hash common.Hash , number uint64 , config * params.ChainConfig ) types.Receipts {
333375 // We're deriving many fields from the block body, retrieve beside the receipt
334376 receipts := ReadRawReceipts (db , hash , number )
335377 if receipts == nil {
@@ -377,7 +419,7 @@ func DeleteReceipts(db ethdb.Writer, hash common.Hash, number uint64) {
377419//
378420// Note, due to concurrent download of header and block body the header and thus
379421// canonical hash can be stored in the database but the body data not (yet).
380- func ReadBlock (db ethdb.Reader , hash common.Hash , number uint64 ) * types.Block {
422+ func ReadBlock (db ethdb.AncientReader , hash common.Hash , number uint64 ) * types.Block {
381423 header := ReadHeader (db , hash , number )
382424 if header == nil {
383425 return nil
@@ -413,7 +455,7 @@ func deleteBlockWithoutNumber(db ethdb.Writer, hash common.Hash, number uint64)
413455}
414456
415457// FindCommonAncestor returns the last common ancestor of two block headers
416- func FindCommonAncestor (db ethdb.Reader , a , b * types.Header ) * types.Header {
458+ func FindCommonAncestor (db ethdb.AncientReader , a , b * types.Header ) * types.Header {
417459 for bn := b .Number .Uint64 (); a .Number .Uint64 () > bn ; {
418460 a = ReadHeader (db , a .ParentHash , a .Number .Uint64 ()- 1 )
419461 if a == nil {
0 commit comments