@@ -29,6 +29,22 @@ import (
2929 "github.com/XinFinOrg/XDPoSChain/rlp"
3030)
3131
32+ // WriteCanonicalHash stores the hash assigned to a canonical block number.
33+ func WriteCanonicalHash (db ethdb.KeyValueWriter , hash common.Hash , number uint64 ) {
34+ if err := db .Put (headerHashKey (number ), hash .Bytes ()); err != nil {
35+ log .Crit ("Failed to store number to hash mapping" , "err" , err )
36+ }
37+ }
38+
39+ // WriteHeaderNumber stores the hash->number mapping.
40+ func WriteHeaderNumber (db ethdb.KeyValueWriter , hash common.Hash , number uint64 ) {
41+ key := headerNumberKey (hash )
42+ enc := encodeBlockNumber (number )
43+ if err := db .Put (key , enc ); err != nil {
44+ log .Crit ("Failed to store hash to number mapping" , "err" , err )
45+ }
46+ }
47+
3248// ReadHeaderNumber returns the header number assigned to a hash.
3349func ReadHeaderNumber (db ethdb.KeyValueReader , hash common.Hash ) * uint64 {
3450 data , _ := db .Get (headerNumberKey (hash ))
@@ -39,6 +55,13 @@ func ReadHeaderNumber(db ethdb.KeyValueReader, hash common.Hash) *uint64 {
3955 return & number
4056}
4157
58+ // WriteHeadBlockHash stores the head block's hash.
59+ func WriteHeadBlockHash (db ethdb.KeyValueWriter , hash common.Hash ) {
60+ if err := db .Put (headBlockKey , hash .Bytes ()); err != nil {
61+ log .Crit ("Failed to store last block's hash" , "err" , err )
62+ }
63+ }
64+
4265// ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
4366func ReadBodyRLP (db ethdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
4467 // First try to look up the data in ancient database. Extra hash
@@ -100,6 +123,27 @@ func WriteBody(db ethdb.KeyValueWriter, hash common.Hash, number uint64, body *t
100123 WriteBodyRLP (db , hash , number , data )
101124}
102125
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+
103147// ReadReceiptsRLP retrieves all the transaction receipts belonging to a block in RLP encoding.
104148func ReadReceiptsRLP (db ethdb.Reader , hash common.Hash , number uint64 ) rlp.RawValue {
105149 // First try to look up the data in ancient database. Extra hash
@@ -195,6 +239,12 @@ func WriteReceipts(db ethdb.KeyValueWriter, hash common.Hash, number uint64, rec
195239 }
196240}
197241
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+
198248// storedReceiptRLP is the storage encoding of a receipt.
199249// Re-definition in core/types/receipt.go.
200250type storedReceiptRLP struct {
@@ -248,9 +298,9 @@ func deriveLogFields(receipts []*receiptLogs, hash common.Hash, number uint64, t
248298 return nil
249299}
250300
251- // ReadLogs retrieves the logs for all transactions in a block. The log fields
252- // are populated with metadata. In case the receipts or the block body
253- // are not found, a nil is returned .
301+ // ReadLogs retrieves the logs for all transactions in a block. In case
302+ // receipts is not found, a nil is returned.
303+ // Note: ReadLogs does not derive unstored log fields .
254304func ReadLogs (db ethdb.Reader , hash common.Hash , number uint64 ) [][]* types.Log {
255305 // Retrieve the flattened receipt slice
256306 data := ReadReceiptsRLP (db , hash , number )
@@ -263,15 +313,6 @@ func ReadLogs(db ethdb.Reader, hash common.Hash, number uint64) [][]*types.Log {
263313 return nil
264314 }
265315
266- body := ReadBody (db , hash , number )
267- if body == nil {
268- log .Error ("Missing body but have receipt" , "hash" , hash , "number" , number )
269- return nil
270- }
271- if err := deriveLogFields (receipts , hash , number , body .Transactions ); err != nil {
272- log .Error ("Failed to derive block receipts fields" , "hash" , hash , "number" , number , "err" , err )
273- return nil
274- }
275316 logs := make ([][]* types.Log , len (receipts ))
276317 for i , receipt := range receipts {
277318 logs [i ] = receipt .Logs
0 commit comments