github.com/theQRL/go-zond@v0.2.1/core/rawdb/accessors_chain.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rawdb
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/binary"
    22  	"errors"
    23  	"fmt"
    24  	"math/big"
    25  	"slices"
    26  
    27  	"github.com/theQRL/go-zond/common"
    28  	"github.com/theQRL/go-zond/core/types"
    29  	"github.com/theQRL/go-zond/crypto"
    30  	"github.com/theQRL/go-zond/log"
    31  	"github.com/theQRL/go-zond/params"
    32  	"github.com/theQRL/go-zond/rlp"
    33  	"github.com/theQRL/go-zond/zonddb"
    34  )
    35  
    36  // ReadCanonicalHash retrieves the hash assigned to a canonical block number.
    37  func ReadCanonicalHash(db zonddb.Reader, number uint64) common.Hash {
    38  	var data []byte
    39  	db.ReadAncients(func(reader zonddb.AncientReaderOp) error {
    40  		data, _ = reader.Ancient(ChainFreezerHashTable, number)
    41  		if len(data) == 0 {
    42  			// Get it by hash from leveldb
    43  			data, _ = db.Get(headerHashKey(number))
    44  		}
    45  		return nil
    46  	})
    47  	return common.BytesToHash(data)
    48  }
    49  
    50  // WriteCanonicalHash stores the hash assigned to a canonical block number.
    51  func WriteCanonicalHash(db zonddb.KeyValueWriter, hash common.Hash, number uint64) {
    52  	if err := db.Put(headerHashKey(number), hash.Bytes()); err != nil {
    53  		log.Crit("Failed to store number to hash mapping", "err", err)
    54  	}
    55  }
    56  
    57  // DeleteCanonicalHash removes the number to hash canonical mapping.
    58  func DeleteCanonicalHash(db zonddb.KeyValueWriter, number uint64) {
    59  	if err := db.Delete(headerHashKey(number)); err != nil {
    60  		log.Crit("Failed to delete number to hash mapping", "err", err)
    61  	}
    62  }
    63  
    64  // ReadAllHashes retrieves all the hashes assigned to blocks at a certain heights,
    65  // both canonical and reorged forks included.
    66  func ReadAllHashes(db zonddb.Iteratee, number uint64) []common.Hash {
    67  	prefix := headerKeyPrefix(number)
    68  
    69  	hashes := make([]common.Hash, 0, 1)
    70  	it := db.NewIterator(prefix, nil)
    71  	defer it.Release()
    72  
    73  	for it.Next() {
    74  		if key := it.Key(); len(key) == len(prefix)+32 {
    75  			hashes = append(hashes, common.BytesToHash(key[len(key)-32:]))
    76  		}
    77  	}
    78  	return hashes
    79  }
    80  
    81  type NumberHash struct {
    82  	Number uint64
    83  	Hash   common.Hash
    84  }
    85  
    86  // ReadAllHashesInRange retrieves all the hashes assigned to blocks at certain
    87  // heights, both canonical and reorged forks included.
    88  // This method considers both limits to be _inclusive_.
    89  func ReadAllHashesInRange(db zonddb.Iteratee, first, last uint64) []*NumberHash {
    90  	var (
    91  		start     = encodeBlockNumber(first)
    92  		keyLength = len(headerPrefix) + 8 + 32
    93  		hashes    = make([]*NumberHash, 0, 1+last-first)
    94  		it        = db.NewIterator(headerPrefix, start)
    95  	)
    96  	defer it.Release()
    97  	for it.Next() {
    98  		key := it.Key()
    99  		if len(key) != keyLength {
   100  			continue
   101  		}
   102  		num := binary.BigEndian.Uint64(key[len(headerPrefix) : len(headerPrefix)+8])
   103  		if num > last {
   104  			break
   105  		}
   106  		hash := common.BytesToHash(key[len(key)-32:])
   107  		hashes = append(hashes, &NumberHash{num, hash})
   108  	}
   109  	return hashes
   110  }
   111  
   112  // ReadAllCanonicalHashes retrieves all canonical number and hash mappings at the
   113  // certain chain range. If the accumulated entries reaches the given threshold,
   114  // abort the iteration and return the semi-finish result.
   115  func ReadAllCanonicalHashes(db zonddb.Iteratee, from uint64, to uint64, limit int) ([]uint64, []common.Hash) {
   116  	// Short circuit if the limit is 0.
   117  	if limit == 0 {
   118  		return nil, nil
   119  	}
   120  	var (
   121  		numbers []uint64
   122  		hashes  []common.Hash
   123  	)
   124  	// Construct the key prefix of start point.
   125  	start, end := headerHashKey(from), headerHashKey(to)
   126  	it := db.NewIterator(nil, start)
   127  	defer it.Release()
   128  
   129  	for it.Next() {
   130  		if bytes.Compare(it.Key(), end) >= 0 {
   131  			break
   132  		}
   133  		if key := it.Key(); len(key) == len(headerPrefix)+8+1 && bytes.Equal(key[len(key)-1:], headerHashSuffix) {
   134  			numbers = append(numbers, binary.BigEndian.Uint64(key[len(headerPrefix):len(headerPrefix)+8]))
   135  			hashes = append(hashes, common.BytesToHash(it.Value()))
   136  			// If the accumulated entries reaches the limit threshold, return.
   137  			if len(numbers) >= limit {
   138  				break
   139  			}
   140  		}
   141  	}
   142  	return numbers, hashes
   143  }
   144  
   145  // ReadHeaderNumber returns the header number assigned to a hash.
   146  func ReadHeaderNumber(db zonddb.KeyValueReader, hash common.Hash) *uint64 {
   147  	data, _ := db.Get(headerNumberKey(hash))
   148  	if len(data) != 8 {
   149  		return nil
   150  	}
   151  	number := binary.BigEndian.Uint64(data)
   152  	return &number
   153  }
   154  
   155  // WriteHeaderNumber stores the hash->number mapping.
   156  func WriteHeaderNumber(db zonddb.KeyValueWriter, hash common.Hash, number uint64) {
   157  	key := headerNumberKey(hash)
   158  	enc := encodeBlockNumber(number)
   159  	if err := db.Put(key, enc); err != nil {
   160  		log.Crit("Failed to store hash to number mapping", "err", err)
   161  	}
   162  }
   163  
   164  // DeleteHeaderNumber removes hash->number mapping.
   165  func DeleteHeaderNumber(db zonddb.KeyValueWriter, hash common.Hash) {
   166  	if err := db.Delete(headerNumberKey(hash)); err != nil {
   167  		log.Crit("Failed to delete hash to number mapping", "err", err)
   168  	}
   169  }
   170  
   171  // ReadHeadHeaderHash retrieves the hash of the current canonical head header.
   172  func ReadHeadHeaderHash(db zonddb.KeyValueReader) common.Hash {
   173  	data, _ := db.Get(headHeaderKey)
   174  	if len(data) == 0 {
   175  		return common.Hash{}
   176  	}
   177  	return common.BytesToHash(data)
   178  }
   179  
   180  // WriteHeadHeaderHash stores the hash of the current canonical head header.
   181  func WriteHeadHeaderHash(db zonddb.KeyValueWriter, hash common.Hash) {
   182  	if err := db.Put(headHeaderKey, hash.Bytes()); err != nil {
   183  		log.Crit("Failed to store last header's hash", "err", err)
   184  	}
   185  }
   186  
   187  // ReadHeadBlockHash retrieves the hash of the current canonical head block.
   188  func ReadHeadBlockHash(db zonddb.KeyValueReader) common.Hash {
   189  	data, _ := db.Get(headBlockKey)
   190  	if len(data) == 0 {
   191  		return common.Hash{}
   192  	}
   193  	return common.BytesToHash(data)
   194  }
   195  
   196  // WriteHeadBlockHash stores the head block's hash.
   197  func WriteHeadBlockHash(db zonddb.KeyValueWriter, hash common.Hash) {
   198  	if err := db.Put(headBlockKey, hash.Bytes()); err != nil {
   199  		log.Crit("Failed to store last block's hash", "err", err)
   200  	}
   201  }
   202  
   203  // ReadHeadFastBlockHash retrieves the hash of the current fast-sync head block.
   204  func ReadHeadFastBlockHash(db zonddb.KeyValueReader) common.Hash {
   205  	data, _ := db.Get(headFastBlockKey)
   206  	if len(data) == 0 {
   207  		return common.Hash{}
   208  	}
   209  	return common.BytesToHash(data)
   210  }
   211  
   212  // WriteHeadFastBlockHash stores the hash of the current fast-sync head block.
   213  func WriteHeadFastBlockHash(db zonddb.KeyValueWriter, hash common.Hash) {
   214  	if err := db.Put(headFastBlockKey, hash.Bytes()); err != nil {
   215  		log.Crit("Failed to store last fast block's hash", "err", err)
   216  	}
   217  }
   218  
   219  // ReadFinalizedBlockHash retrieves the hash of the finalized block.
   220  func ReadFinalizedBlockHash(db zonddb.KeyValueReader) common.Hash {
   221  	data, _ := db.Get(headFinalizedBlockKey)
   222  	if len(data) == 0 {
   223  		return common.Hash{}
   224  	}
   225  	return common.BytesToHash(data)
   226  }
   227  
   228  // WriteFinalizedBlockHash stores the hash of the finalized block.
   229  func WriteFinalizedBlockHash(db zonddb.KeyValueWriter, hash common.Hash) {
   230  	if err := db.Put(headFinalizedBlockKey, hash.Bytes()); err != nil {
   231  		log.Crit("Failed to store last finalized block's hash", "err", err)
   232  	}
   233  }
   234  
   235  // ReadLastPivotNumber retrieves the number of the last pivot block. If the node
   236  // full synced, the last pivot will always be nil.
   237  func ReadLastPivotNumber(db zonddb.KeyValueReader) *uint64 {
   238  	data, _ := db.Get(lastPivotKey)
   239  	if len(data) == 0 {
   240  		return nil
   241  	}
   242  	var pivot uint64
   243  	if err := rlp.DecodeBytes(data, &pivot); err != nil {
   244  		log.Error("Invalid pivot block number in database", "err", err)
   245  		return nil
   246  	}
   247  	return &pivot
   248  }
   249  
   250  // WriteLastPivotNumber stores the number of the last pivot block.
   251  func WriteLastPivotNumber(db zonddb.KeyValueWriter, pivot uint64) {
   252  	enc, err := rlp.EncodeToBytes(pivot)
   253  	if err != nil {
   254  		log.Crit("Failed to encode pivot block number", "err", err)
   255  	}
   256  	if err := db.Put(lastPivotKey, enc); err != nil {
   257  		log.Crit("Failed to store pivot block number", "err", err)
   258  	}
   259  }
   260  
   261  // ReadTxIndexTail retrieves the number of oldest indexed block
   262  // whose transaction indices has been indexed.
   263  func ReadTxIndexTail(db zonddb.KeyValueReader) *uint64 {
   264  	data, _ := db.Get(txIndexTailKey)
   265  	if len(data) != 8 {
   266  		return nil
   267  	}
   268  	number := binary.BigEndian.Uint64(data)
   269  	return &number
   270  }
   271  
   272  // WriteTxIndexTail stores the number of oldest indexed block
   273  // into database.
   274  func WriteTxIndexTail(db zonddb.KeyValueWriter, number uint64) {
   275  	if err := db.Put(txIndexTailKey, encodeBlockNumber(number)); err != nil {
   276  		log.Crit("Failed to store the transaction index tail", "err", err)
   277  	}
   278  }
   279  
   280  // ReadFastTxLookupLimit retrieves the tx lookup limit used in fast sync.
   281  func ReadFastTxLookupLimit(db zonddb.KeyValueReader) *uint64 {
   282  	data, _ := db.Get(fastTxLookupLimitKey)
   283  	if len(data) != 8 {
   284  		return nil
   285  	}
   286  	number := binary.BigEndian.Uint64(data)
   287  	return &number
   288  }
   289  
   290  // WriteFastTxLookupLimit stores the txlookup limit used in fast sync into database.
   291  func WriteFastTxLookupLimit(db zonddb.KeyValueWriter, number uint64) {
   292  	if err := db.Put(fastTxLookupLimitKey, encodeBlockNumber(number)); err != nil {
   293  		log.Crit("Failed to store transaction lookup limit for fast sync", "err", err)
   294  	}
   295  }
   296  
   297  // ReadHeaderRange returns the rlp-encoded headers, starting at 'number', and going
   298  // backwards towards genesis. This method assumes that the caller already has
   299  // placed a cap on count, to prevent DoS issues.
   300  // Since this method operates in head-towards-genesis mode, it will return an empty
   301  // slice in case the head ('number') is missing. Hence, the caller must ensure that
   302  // the head ('number') argument is actually an existing header.
   303  //
   304  // N.B: Since the input is a number, as opposed to a hash, it's implicit that
   305  // this method only operates on canon headers.
   306  func ReadHeaderRange(db zonddb.Reader, number uint64, count uint64) []rlp.RawValue {
   307  	var rlpHeaders []rlp.RawValue
   308  	if count == 0 {
   309  		return rlpHeaders
   310  	}
   311  	i := number
   312  	if count-1 > number {
   313  		// It's ok to request block 0, 1 item
   314  		count = number + 1
   315  	}
   316  	limit, _ := db.Ancients()
   317  	// First read live blocks
   318  	if i >= limit {
   319  		// If we need to read live blocks, we need to figure out the hash first
   320  		hash := ReadCanonicalHash(db, number)
   321  		for ; i >= limit && count > 0; i-- {
   322  			if data, _ := db.Get(headerKey(i, hash)); len(data) > 0 {
   323  				rlpHeaders = append(rlpHeaders, data)
   324  				// Get the parent hash for next query
   325  				hash = types.HeaderParentHashFromRLP(data)
   326  			} else {
   327  				break // Maybe got moved to ancients
   328  			}
   329  			count--
   330  		}
   331  	}
   332  	if count == 0 {
   333  		return rlpHeaders
   334  	}
   335  	// read remaining from ancients
   336  	max := count * 700
   337  	data, err := db.AncientRange(ChainFreezerHeaderTable, i+1-count, count, max)
   338  	if err == nil && uint64(len(data)) == count {
   339  		// the data is on the order [h, h+1, .., n] -- reordering needed
   340  		for i := range data {
   341  			rlpHeaders = append(rlpHeaders, data[len(data)-1-i])
   342  		}
   343  	}
   344  	return rlpHeaders
   345  }
   346  
   347  // ReadHeaderRLP retrieves a block header in its raw RLP database encoding.
   348  func ReadHeaderRLP(db zonddb.Reader, hash common.Hash, number uint64) rlp.RawValue {
   349  	var data []byte
   350  	db.ReadAncients(func(reader zonddb.AncientReaderOp) error {
   351  		// First try to look up the data in ancient database. Extra hash
   352  		// comparison is necessary since ancient database only maintains
   353  		// the canonical data.
   354  		data, _ = reader.Ancient(ChainFreezerHeaderTable, number)
   355  		if len(data) > 0 && crypto.Keccak256Hash(data) == hash {
   356  			return nil
   357  		}
   358  		// If not, try reading from leveldb
   359  		data, _ = db.Get(headerKey(number, hash))
   360  		return nil
   361  	})
   362  	return data
   363  }
   364  
   365  // HasHeader verifies the existence of a block header corresponding to the hash.
   366  func HasHeader(db zonddb.Reader, hash common.Hash, number uint64) bool {
   367  	if isCanon(db, number, hash) {
   368  		return true
   369  	}
   370  	if has, err := db.Has(headerKey(number, hash)); !has || err != nil {
   371  		return false
   372  	}
   373  	return true
   374  }
   375  
   376  // ReadHeader retrieves the block header corresponding to the hash.
   377  func ReadHeader(db zonddb.Reader, hash common.Hash, number uint64) *types.Header {
   378  	data := ReadHeaderRLP(db, hash, number)
   379  	if len(data) == 0 {
   380  		return nil
   381  	}
   382  	header := new(types.Header)
   383  	if err := rlp.DecodeBytes(data, header); err != nil {
   384  		log.Error("Invalid block header RLP", "hash", hash, "err", err)
   385  		return nil
   386  	}
   387  	return header
   388  }
   389  
   390  // WriteHeader stores a block header into the database and also stores the hash-
   391  // to-number mapping.
   392  func WriteHeader(db zonddb.KeyValueWriter, header *types.Header) {
   393  	var (
   394  		hash   = header.Hash()
   395  		number = header.Number.Uint64()
   396  	)
   397  	// Write the hash -> number mapping
   398  	WriteHeaderNumber(db, hash, number)
   399  
   400  	// Write the encoded header
   401  	data, err := rlp.EncodeToBytes(header)
   402  	if err != nil {
   403  		log.Crit("Failed to RLP encode header", "err", err)
   404  	}
   405  	key := headerKey(number, hash)
   406  	if err := db.Put(key, data); err != nil {
   407  		log.Crit("Failed to store header", "err", err)
   408  	}
   409  }
   410  
   411  // DeleteHeader removes all block header data associated with a hash.
   412  func DeleteHeader(db zonddb.KeyValueWriter, hash common.Hash, number uint64) {
   413  	deleteHeaderWithoutNumber(db, hash, number)
   414  	if err := db.Delete(headerNumberKey(hash)); err != nil {
   415  		log.Crit("Failed to delete hash to number mapping", "err", err)
   416  	}
   417  }
   418  
   419  // deleteHeaderWithoutNumber removes only the block header but does not remove
   420  // the hash to number mapping.
   421  func deleteHeaderWithoutNumber(db zonddb.KeyValueWriter, hash common.Hash, number uint64) {
   422  	if err := db.Delete(headerKey(number, hash)); err != nil {
   423  		log.Crit("Failed to delete header", "err", err)
   424  	}
   425  }
   426  
   427  // isCanon is an internal utility method, to check whether the given number/hash
   428  // is part of the ancient (canon) set.
   429  func isCanon(reader zonddb.AncientReaderOp, number uint64, hash common.Hash) bool {
   430  	h, err := reader.Ancient(ChainFreezerHashTable, number)
   431  	if err != nil {
   432  		return false
   433  	}
   434  	return bytes.Equal(h, hash[:])
   435  }
   436  
   437  // ReadBodyRLP retrieves the block body (transactions) in RLP encoding.
   438  func ReadBodyRLP(db zonddb.Reader, hash common.Hash, number uint64) rlp.RawValue {
   439  	// First try to look up the data in ancient database. Extra hash
   440  	// comparison is necessary since ancient database only maintains
   441  	// the canonical data.
   442  	var data []byte
   443  	db.ReadAncients(func(reader zonddb.AncientReaderOp) error {
   444  		// Check if the data is in ancients
   445  		if isCanon(reader, number, hash) {
   446  			data, _ = reader.Ancient(ChainFreezerBodiesTable, number)
   447  			return nil
   448  		}
   449  		// If not, try reading from leveldb
   450  		data, _ = db.Get(blockBodyKey(number, hash))
   451  		return nil
   452  	})
   453  	return data
   454  }
   455  
   456  // ReadCanonicalBodyRLP retrieves the block body (transactions) for the canonical
   457  // block at number, in RLP encoding.
   458  func ReadCanonicalBodyRLP(db zonddb.Reader, number uint64) rlp.RawValue {
   459  	var data []byte
   460  	db.ReadAncients(func(reader zonddb.AncientReaderOp) error {
   461  		data, _ = reader.Ancient(ChainFreezerBodiesTable, number)
   462  		if len(data) > 0 {
   463  			return nil
   464  		}
   465  		// Block is not in ancients, read from leveldb by hash and number.
   466  		// Note: ReadCanonicalHash cannot be used here because it also
   467  		// calls ReadAncients internally.
   468  		hash, _ := db.Get(headerHashKey(number))
   469  		data, _ = db.Get(blockBodyKey(number, common.BytesToHash(hash)))
   470  		return nil
   471  	})
   472  	return data
   473  }
   474  
   475  // WriteBodyRLP stores an RLP encoded block body into the database.
   476  func WriteBodyRLP(db zonddb.KeyValueWriter, hash common.Hash, number uint64, rlp rlp.RawValue) {
   477  	if err := db.Put(blockBodyKey(number, hash), rlp); err != nil {
   478  		log.Crit("Failed to store block body", "err", err)
   479  	}
   480  }
   481  
   482  // HasBody verifies the existence of a block body corresponding to the hash.
   483  func HasBody(db zonddb.Reader, hash common.Hash, number uint64) bool {
   484  	if isCanon(db, number, hash) {
   485  		return true
   486  	}
   487  	if has, err := db.Has(blockBodyKey(number, hash)); !has || err != nil {
   488  		return false
   489  	}
   490  	return true
   491  }
   492  
   493  // ReadBody retrieves the block body corresponding to the hash.
   494  func ReadBody(db zonddb.Reader, hash common.Hash, number uint64) *types.Body {
   495  	data := ReadBodyRLP(db, hash, number)
   496  	if len(data) == 0 {
   497  		return nil
   498  	}
   499  	body := new(types.Body)
   500  	if err := rlp.DecodeBytes(data, body); err != nil {
   501  		log.Error("Invalid block body RLP", "hash", hash, "err", err)
   502  		return nil
   503  	}
   504  	return body
   505  }
   506  
   507  // WriteBody stores a block body into the database.
   508  func WriteBody(db zonddb.KeyValueWriter, hash common.Hash, number uint64, body *types.Body) {
   509  	data, err := rlp.EncodeToBytes(body)
   510  	if err != nil {
   511  		log.Crit("Failed to RLP encode body", "err", err)
   512  	}
   513  	WriteBodyRLP(db, hash, number, data)
   514  }
   515  
   516  // DeleteBody removes all block body data associated with a hash.
   517  func DeleteBody(db zonddb.KeyValueWriter, hash common.Hash, number uint64) {
   518  	if err := db.Delete(blockBodyKey(number, hash)); err != nil {
   519  		log.Crit("Failed to delete block body", "err", err)
   520  	}
   521  }
   522  
   523  // HasReceipts verifies the existence of all the transaction receipts belonging
   524  // to a block.
   525  func HasReceipts(db zonddb.Reader, hash common.Hash, number uint64) bool {
   526  	if isCanon(db, number, hash) {
   527  		return true
   528  	}
   529  	if has, err := db.Has(blockReceiptsKey(number, hash)); !has || err != nil {
   530  		return false
   531  	}
   532  	return true
   533  }
   534  
   535  // ReadReceiptsRLP retrieves all the transaction receipts belonging to a block in RLP encoding.
   536  func ReadReceiptsRLP(db zonddb.Reader, hash common.Hash, number uint64) rlp.RawValue {
   537  	var data []byte
   538  	db.ReadAncients(func(reader zonddb.AncientReaderOp) error {
   539  		// Check if the data is in ancients
   540  		if isCanon(reader, number, hash) {
   541  			data, _ = reader.Ancient(ChainFreezerReceiptTable, number)
   542  			return nil
   543  		}
   544  		// If not, try reading from leveldb
   545  		data, _ = db.Get(blockReceiptsKey(number, hash))
   546  		return nil
   547  	})
   548  	return data
   549  }
   550  
   551  // ReadRawReceipts retrieves all the transaction receipts belonging to a block.
   552  // The receipt metadata fields are not guaranteed to be populated, so they
   553  // should not be used. Use ReadReceipts instead if the metadata is needed.
   554  func ReadRawReceipts(db zonddb.Reader, hash common.Hash, number uint64) types.Receipts {
   555  	// Retrieve the flattened receipt slice
   556  	data := ReadReceiptsRLP(db, hash, number)
   557  	if len(data) == 0 {
   558  		return nil
   559  	}
   560  	// Convert the receipts from their storage form to their internal representation
   561  	storageReceipts := []*types.ReceiptForStorage{}
   562  	if err := rlp.DecodeBytes(data, &storageReceipts); err != nil {
   563  		log.Error("Invalid receipt array RLP", "hash", hash, "err", err)
   564  		return nil
   565  	}
   566  	receipts := make(types.Receipts, len(storageReceipts))
   567  	for i, storageReceipt := range storageReceipts {
   568  		receipts[i] = (*types.Receipt)(storageReceipt)
   569  	}
   570  	return receipts
   571  }
   572  
   573  // ReadReceipts retrieves all the transaction receipts belonging to a block, including
   574  // its corresponding metadata fields. If it is unable to populate these metadata
   575  // fields then nil is returned.
   576  //
   577  // The current implementation populates these metadata fields by reading the receipts'
   578  // corresponding block body, so if the block body is not found it will return nil even
   579  // if the receipt itself is stored.
   580  func ReadReceipts(db zonddb.Reader, hash common.Hash, number uint64, time uint64, config *params.ChainConfig) types.Receipts {
   581  	// We're deriving many fields from the block body, retrieve beside the receipt
   582  	receipts := ReadRawReceipts(db, hash, number)
   583  	if receipts == nil {
   584  		return nil
   585  	}
   586  	body := ReadBody(db, hash, number)
   587  	if body == nil {
   588  		log.Error("Missing body but have receipt", "hash", hash, "number", number)
   589  		return nil
   590  	}
   591  	header := ReadHeader(db, hash, number)
   592  
   593  	var baseFee *big.Int
   594  	if header == nil {
   595  		baseFee = big.NewInt(0)
   596  	} else {
   597  		baseFee = header.BaseFee
   598  	}
   599  	if err := receipts.DeriveFields(config, hash, number, time, baseFee, body.Transactions); err != nil {
   600  		log.Error("Failed to derive block receipts fields", "hash", hash, "number", number, "err", err)
   601  		return nil
   602  	}
   603  	return receipts
   604  }
   605  
   606  // WriteReceipts stores all the transaction receipts belonging to a block.
   607  func WriteReceipts(db zonddb.KeyValueWriter, hash common.Hash, number uint64, receipts types.Receipts) {
   608  	// Convert the receipts into their storage form and serialize them
   609  	storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
   610  	for i, receipt := range receipts {
   611  		storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
   612  	}
   613  	bytes, err := rlp.EncodeToBytes(storageReceipts)
   614  	if err != nil {
   615  		log.Crit("Failed to encode block receipts", "err", err)
   616  	}
   617  	// Store the flattened receipt slice
   618  	if err := db.Put(blockReceiptsKey(number, hash), bytes); err != nil {
   619  		log.Crit("Failed to store block receipts", "err", err)
   620  	}
   621  }
   622  
   623  // DeleteReceipts removes all receipt data associated with a block hash.
   624  func DeleteReceipts(db zonddb.KeyValueWriter, hash common.Hash, number uint64) {
   625  	if err := db.Delete(blockReceiptsKey(number, hash)); err != nil {
   626  		log.Crit("Failed to delete block receipts", "err", err)
   627  	}
   628  }
   629  
   630  // storedReceiptRLP is the storage encoding of a receipt.
   631  // Re-definition in core/types/receipt.go.
   632  // TODO: Re-use the existing definition.
   633  type storedReceiptRLP struct {
   634  	PostStateOrStatus []byte
   635  	CumulativeGasUsed uint64
   636  	Logs              []*types.Log
   637  }
   638  
   639  // ReceiptLogs is a barebone version of ReceiptForStorage which only keeps
   640  // the list of logs. When decoding a stored receipt into this object we
   641  // avoid creating the bloom filter.
   642  type receiptLogs struct {
   643  	Logs []*types.Log
   644  }
   645  
   646  // DecodeRLP implements rlp.Decoder.
   647  func (r *receiptLogs) DecodeRLP(s *rlp.Stream) error {
   648  	var stored storedReceiptRLP
   649  	if err := s.Decode(&stored); err != nil {
   650  		return err
   651  	}
   652  	r.Logs = stored.Logs
   653  	return nil
   654  }
   655  
   656  // DeriveLogFields fills the logs in receiptLogs with information such as block number, txhash, etc.
   657  func deriveLogFields(receipts []*receiptLogs, hash common.Hash, number uint64, txs types.Transactions) error {
   658  	logIndex := uint(0)
   659  	if len(txs) != len(receipts) {
   660  		return errors.New("transaction and receipt count mismatch")
   661  	}
   662  	for i := 0; i < len(receipts); i++ {
   663  		txHash := txs[i].Hash()
   664  		// The derived log fields can simply be set from the block and transaction
   665  		for j := 0; j < len(receipts[i].Logs); j++ {
   666  			receipts[i].Logs[j].BlockNumber = number
   667  			receipts[i].Logs[j].BlockHash = hash
   668  			receipts[i].Logs[j].TxHash = txHash
   669  			receipts[i].Logs[j].TxIndex = uint(i)
   670  			receipts[i].Logs[j].Index = logIndex
   671  			logIndex++
   672  		}
   673  	}
   674  	return nil
   675  }
   676  
   677  // ReadLogs retrieves the logs for all transactions in a block. In case
   678  // receipts is not found, a nil is returned.
   679  // Note: ReadLogs does not derive unstored log fields.
   680  func ReadLogs(db zonddb.Reader, hash common.Hash, number uint64) [][]*types.Log {
   681  	// Retrieve the flattened receipt slice
   682  	data := ReadReceiptsRLP(db, hash, number)
   683  	if len(data) == 0 {
   684  		return nil
   685  	}
   686  	receipts := []*receiptLogs{}
   687  	if err := rlp.DecodeBytes(data, &receipts); err != nil {
   688  		log.Error("Invalid receipt array RLP", "hash", hash, "err", err)
   689  		return nil
   690  	}
   691  
   692  	logs := make([][]*types.Log, len(receipts))
   693  	for i, receipt := range receipts {
   694  		logs[i] = receipt.Logs
   695  	}
   696  	return logs
   697  }
   698  
   699  // ReadBlock retrieves an entire block corresponding to the hash, assembling it
   700  // back from the stored header and body. If either the header or body could not
   701  // be retrieved nil is returned.
   702  //
   703  // Note, due to concurrent download of header and block body the header and thus
   704  // canonical hash can be stored in the database but the body data not (yet).
   705  func ReadBlock(db zonddb.Reader, hash common.Hash, number uint64) *types.Block {
   706  	header := ReadHeader(db, hash, number)
   707  	if header == nil {
   708  		return nil
   709  	}
   710  	body := ReadBody(db, hash, number)
   711  	if body == nil {
   712  		return nil
   713  	}
   714  	return types.NewBlockWithHeader(header).WithBody(*body)
   715  }
   716  
   717  // WriteBlock serializes a block into the database, header and body separately.
   718  func WriteBlock(db zonddb.KeyValueWriter, block *types.Block) {
   719  	WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
   720  	WriteHeader(db, block.Header())
   721  }
   722  
   723  // WriteAncientBlocks writes entire block data into ancient store and returns the total written size.
   724  func WriteAncientBlocks(db zonddb.AncientWriter, blocks []*types.Block, receipts []types.Receipts) (int64, error) {
   725  	var (
   726  		stReceipts []*types.ReceiptForStorage
   727  	)
   728  	return db.ModifyAncients(func(op zonddb.AncientWriteOp) error {
   729  		for i, block := range blocks {
   730  			// Convert receipts to storage format.
   731  			stReceipts = stReceipts[:0]
   732  			for _, receipt := range receipts[i] {
   733  				stReceipts = append(stReceipts, (*types.ReceiptForStorage)(receipt))
   734  			}
   735  			header := block.Header()
   736  			if err := writeAncientBlock(op, block, header, stReceipts); err != nil {
   737  				return err
   738  			}
   739  		}
   740  		return nil
   741  	})
   742  }
   743  
   744  func writeAncientBlock(op zonddb.AncientWriteOp, block *types.Block, header *types.Header, receipts []*types.ReceiptForStorage) error {
   745  	num := block.NumberU64()
   746  	if err := op.AppendRaw(ChainFreezerHashTable, num, block.Hash().Bytes()); err != nil {
   747  		return fmt.Errorf("can't add block %d hash: %v", num, err)
   748  	}
   749  	if err := op.Append(ChainFreezerHeaderTable, num, header); err != nil {
   750  		return fmt.Errorf("can't append block header %d: %v", num, err)
   751  	}
   752  	if err := op.Append(ChainFreezerBodiesTable, num, block.Body()); err != nil {
   753  		return fmt.Errorf("can't append block body %d: %v", num, err)
   754  	}
   755  	if err := op.Append(ChainFreezerReceiptTable, num, receipts); err != nil {
   756  		return fmt.Errorf("can't append block %d receipts: %v", num, err)
   757  	}
   758  	return nil
   759  }
   760  
   761  // DeleteBlock removes all block data associated with a hash.
   762  func DeleteBlock(db zonddb.KeyValueWriter, hash common.Hash, number uint64) {
   763  	DeleteReceipts(db, hash, number)
   764  	DeleteHeader(db, hash, number)
   765  	DeleteBody(db, hash, number)
   766  }
   767  
   768  // DeleteBlockWithoutNumber removes all block data associated with a hash, except
   769  // the hash to number mapping.
   770  func DeleteBlockWithoutNumber(db zonddb.KeyValueWriter, hash common.Hash, number uint64) {
   771  	DeleteReceipts(db, hash, number)
   772  	deleteHeaderWithoutNumber(db, hash, number)
   773  	DeleteBody(db, hash, number)
   774  }
   775  
   776  const badBlockToKeep = 10
   777  
   778  type badBlock struct {
   779  	Header *types.Header
   780  	Body   *types.Body
   781  }
   782  
   783  // ReadBadBlock retrieves the bad block with the corresponding block hash.
   784  func ReadBadBlock(db zonddb.Reader, hash common.Hash) *types.Block {
   785  	blob, err := db.Get(badBlockKey)
   786  	if err != nil {
   787  		return nil
   788  	}
   789  	var badBlocks []*badBlock
   790  	if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
   791  		return nil
   792  	}
   793  	for _, bad := range badBlocks {
   794  		if bad.Header.Hash() == hash {
   795  			block := types.NewBlockWithHeader(bad.Header)
   796  			if bad.Body != nil {
   797  				block = block.WithBody(*bad.Body)
   798  			}
   799  			return block
   800  		}
   801  	}
   802  	return nil
   803  }
   804  
   805  // ReadAllBadBlocks retrieves all the bad blocks in the database.
   806  // All returned blocks are sorted in reverse order by number.
   807  func ReadAllBadBlocks(db zonddb.Reader) []*types.Block {
   808  	blob, err := db.Get(badBlockKey)
   809  	if err != nil {
   810  		return nil
   811  	}
   812  	var badBlocks []*badBlock
   813  	if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
   814  		return nil
   815  	}
   816  	var blocks []*types.Block
   817  	for _, bad := range badBlocks {
   818  		block := types.NewBlockWithHeader(bad.Header)
   819  		if bad.Body != nil {
   820  			block = block.WithBody(*bad.Body)
   821  		}
   822  		blocks = append(blocks, block)
   823  	}
   824  	return blocks
   825  }
   826  
   827  // WriteBadBlock serializes the bad block into the database. If the cumulated
   828  // bad blocks exceeds the limitation, the oldest will be dropped.
   829  func WriteBadBlock(db zonddb.KeyValueStore, block *types.Block) {
   830  	blob, err := db.Get(badBlockKey)
   831  	if err != nil {
   832  		log.Warn("Failed to load old bad blocks", "error", err)
   833  	}
   834  	var badBlocks []*badBlock
   835  	if len(blob) > 0 {
   836  		if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
   837  			log.Crit("Failed to decode old bad blocks", "error", err)
   838  		}
   839  	}
   840  	for _, b := range badBlocks {
   841  		if b.Header.Number.Uint64() == block.NumberU64() && b.Header.Hash() == block.Hash() {
   842  			log.Info("Skip duplicated bad block", "number", block.NumberU64(), "hash", block.Hash())
   843  			return
   844  		}
   845  	}
   846  	badBlocks = append(badBlocks, &badBlock{
   847  		Header: block.Header(),
   848  		Body:   block.Body(),
   849  	})
   850  	slices.SortFunc(badBlocks, func(a, b *badBlock) int {
   851  		// Note: sorting in descending number order.
   852  		return -a.Header.Number.Cmp(b.Header.Number)
   853  	})
   854  	if len(badBlocks) > badBlockToKeep {
   855  		badBlocks = badBlocks[:badBlockToKeep]
   856  	}
   857  	data, err := rlp.EncodeToBytes(badBlocks)
   858  	if err != nil {
   859  		log.Crit("Failed to encode bad blocks", "err", err)
   860  	}
   861  	if err := db.Put(badBlockKey, data); err != nil {
   862  		log.Crit("Failed to write bad blocks", "err", err)
   863  	}
   864  }
   865  
   866  // DeleteBadBlocks deletes all the bad blocks from the database
   867  func DeleteBadBlocks(db zonddb.KeyValueWriter) {
   868  	if err := db.Delete(badBlockKey); err != nil {
   869  		log.Crit("Failed to delete bad blocks", "err", err)
   870  	}
   871  }
   872  
   873  // FindCommonAncestor returns the last common ancestor of two block headers
   874  func FindCommonAncestor(db zonddb.Reader, a, b *types.Header) *types.Header {
   875  	for bn := b.Number.Uint64(); a.Number.Uint64() > bn; {
   876  		a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1)
   877  		if a == nil {
   878  			return nil
   879  		}
   880  	}
   881  	for an := a.Number.Uint64(); an < b.Number.Uint64(); {
   882  		b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1)
   883  		if b == nil {
   884  			return nil
   885  		}
   886  	}
   887  	for a.Hash() != b.Hash() {
   888  		a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1)
   889  		if a == nil {
   890  			return nil
   891  		}
   892  		b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1)
   893  		if b == nil {
   894  			return nil
   895  		}
   896  	}
   897  	return a
   898  }
   899  
   900  // ReadHeadHeader returns the current canonical head header.
   901  func ReadHeadHeader(db zonddb.Reader) *types.Header {
   902  	headHeaderHash := ReadHeadHeaderHash(db)
   903  	if headHeaderHash == (common.Hash{}) {
   904  		return nil
   905  	}
   906  	headHeaderNumber := ReadHeaderNumber(db, headHeaderHash)
   907  	if headHeaderNumber == nil {
   908  		return nil
   909  	}
   910  	return ReadHeader(db, headHeaderHash, *headHeaderNumber)
   911  }
   912  
   913  // ReadHeadBlock returns the current canonical head block.
   914  func ReadHeadBlock(db zonddb.Reader) *types.Block {
   915  	headBlockHash := ReadHeadBlockHash(db)
   916  	if headBlockHash == (common.Hash{}) {
   917  		return nil
   918  	}
   919  	headBlockNumber := ReadHeaderNumber(db, headBlockHash)
   920  	if headBlockNumber == nil {
   921  		return nil
   922  	}
   923  	return ReadBlock(db, headBlockHash, *headBlockNumber)
   924  }