@@ -62,6 +62,27 @@ func WriteHeadBlockHash(db ethdb.KeyValueWriter, hash common.Hash) {
6262 }
6363}
6464
65+ // WriteHeader stores a block header into the database and also stores the hash-
66+ // to-number mapping.
67+ func WriteHeader (db ethdb.KeyValueWriter , header * types.Header ) {
68+ var (
69+ hash = header .Hash ()
70+ number = header .Number .Uint64 ()
71+ )
72+ // Write the hash -> number mapping
73+ WriteHeaderNumber (db , hash , number )
74+
75+ // Write the encoded header
76+ data , err := rlp .EncodeToBytes (header )
77+ if err != nil {
78+ log .Crit ("Failed to RLP encode header" , "err" , err )
79+ }
80+ key := headerKey (number , hash )
81+ if err := db .Put (key , data ); err != nil {
82+ log .Crit ("Failed to store header" , "err" , err )
83+ }
84+ }
85+
6586// ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
6687func ReadBodyRLP (db ethdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
6788 // First try to look up the data in ancient database. Extra hash
@@ -123,27 +144,6 @@ func WriteBody(db ethdb.KeyValueWriter, hash common.Hash, number uint64, body *t
123144 WriteBodyRLP (db , hash , number , data )
124145}
125146
126- // WriteHeader stores a block header into the database and also stores the hash-
127- // to-number mapping.
128- func WriteHeader (db ethdb.KeyValueWriter , header * types.Header ) {
129- var (
130- hash = header .Hash ()
131- number = header .Number .Uint64 ()
132- )
133- // Write the hash -> number mapping
134- WriteHeaderNumber (db , hash , number )
135-
136- // Write the encoded header
137- data , err := rlp .EncodeToBytes (header )
138- if err != nil {
139- log .Crit ("Failed to RLP encode header" , "err" , err )
140- }
141- key := headerKey (number , hash )
142- if err := db .Put (key , data ); err != nil {
143- log .Crit ("Failed to store header" , "err" , err )
144- }
145- }
146-
147147// ReadReceiptsRLP retrieves all the transaction receipts belonging to a block in RLP encoding.
148148func ReadReceiptsRLP (db ethdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
149149 // First try to look up the data in ancient database. Extra hash
@@ -239,12 +239,6 @@ func WriteReceipts(db ethdb.KeyValueWriter, hash common.Hash, number uint64, rec
239239 }
240240}
241241
242- // WriteBlock serializes a block into the database, header and body separately.
243- func WriteBlock (db ethdb.KeyValueWriter , block * types.Block ) {
244- WriteBody (db , block .Hash (), block .NumberU64 (), block .Body ())
245- WriteHeader (db , block .Header ())
246- }
247-
248242// storedReceiptRLP is the storage encoding of a receipt.
249243// Re-definition in core/types/receipt.go.
250244type storedReceiptRLP struct {
@@ -319,3 +313,9 @@ func ReadLogs(db ethdb.Reader, hash common.Hash, number uint64) [][]*types.Log {
319313 }
320314 return logs
321315}
316+
317+ // WriteBlock serializes a block into the database, header and body separately.
318+ func WriteBlock (db ethdb.KeyValueWriter , block * types.Block ) {
319+ WriteBody (db , block .Hash (), block .NumberU64 (), block .Body ())
320+ WriteHeader (db , block .Header ())
321+ }
0 commit comments