accessors_indexes.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. package rawdb
  17. import (
  18. "bytes"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/ethdb"
  23. "github.com/ethereum/go-ethereum/log"
  24. "github.com/ethereum/go-ethereum/params"
  25. "github.com/ethereum/go-ethereum/rlp"
  26. )
  27. // ReadTxLookupEntry retrieves the positional metadata associated with a transaction
  28. // hash to allow retrieving the transaction or receipt by hash.
  29. func ReadTxLookupEntry(db ethdb.Reader, hash common.Hash) *uint64 {
  30. data, _ := db.Get(txLookupKey(hash))
  31. if len(data) == 0 {
  32. return nil
  33. }
  34. // Database v6 tx lookup just stores the block number
  35. if len(data) < common.HashLength {
  36. number := new(big.Int).SetBytes(data).Uint64()
  37. return &number
  38. }
  39. // Database v4-v5 tx lookup format just stores the hash
  40. if len(data) == common.HashLength {
  41. return ReadHeaderNumber(db, common.BytesToHash(data))
  42. }
  43. // Finally try database v3 tx lookup format
  44. var entry LegacyTxLookupEntry
  45. if err := rlp.DecodeBytes(data, &entry); err != nil {
  46. log.Error("Invalid transaction lookup entry RLP", "hash", hash, "blob", data, "err", err)
  47. return nil
  48. }
  49. return &entry.BlockIndex
  50. }
  51. // WriteTxLookupEntries stores a positional metadata for every transaction from
  52. // a block, enabling hash based transaction and receipt lookups.
  53. func WriteTxLookupEntries(db ethdb.KeyValueWriter, block *types.Block) {
  54. number := block.Number().Bytes()
  55. for _, tx := range block.Transactions() {
  56. if err := db.Put(txLookupKey(tx.Hash()), number); err != nil {
  57. log.Crit("Failed to store transaction lookup entry", "err", err)
  58. }
  59. }
  60. }
  61. // WriteTxLookupEntriesByHash is identical to WriteTxLookupEntries, but does not
  62. // require a full types.Block as input.
  63. func WriteTxLookupEntriesByHash(db ethdb.KeyValueWriter, number uint64, hashes []common.Hash) {
  64. numberBytes := new(big.Int).SetUint64(number).Bytes()
  65. for _, hash := range hashes {
  66. if err := db.Put(txLookupKey(hash), numberBytes); err != nil {
  67. log.Crit("Failed to store transaction lookup entry", "err", err)
  68. }
  69. }
  70. }
  71. // DeleteTxLookupEntry removes all transaction data associated with a hash.
  72. func DeleteTxLookupEntry(db ethdb.KeyValueWriter, hash common.Hash) {
  73. if err := db.Delete(txLookupKey(hash)); err != nil {
  74. log.Crit("Failed to delete transaction lookup entry", "err", err)
  75. }
  76. }
  77. // DeleteTxLookupEntries removes all transaction lookups for a given block.
  78. func DeleteTxLookupEntriesByHash(db ethdb.KeyValueWriter, hashes []common.Hash) {
  79. for _, hash := range hashes {
  80. if err := db.Delete(txLookupKey(hash)); err != nil {
  81. log.Crit("Failed to delete transaction lookup entry", "err", err)
  82. }
  83. }
  84. }
  85. // ReadTransaction retrieves a specific transaction from the database, along with
  86. // its added positional metadata.
  87. func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
  88. blockNumber := ReadTxLookupEntry(db, hash)
  89. if blockNumber == nil {
  90. return nil, common.Hash{}, 0, 0
  91. }
  92. blockHash := ReadCanonicalHash(db, *blockNumber)
  93. if blockHash == (common.Hash{}) {
  94. return nil, common.Hash{}, 0, 0
  95. }
  96. body := ReadBody(db, blockHash, *blockNumber)
  97. if body == nil {
  98. log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash)
  99. return nil, common.Hash{}, 0, 0
  100. }
  101. for txIndex, tx := range body.Transactions {
  102. if tx.Hash() == hash {
  103. return tx, blockHash, *blockNumber, uint64(txIndex)
  104. }
  105. }
  106. log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
  107. return nil, common.Hash{}, 0, 0
  108. }
  109. // ReadReceipt retrieves a specific transaction receipt from the database, along with
  110. // its added positional metadata.
  111. func ReadReceipt(db ethdb.Reader, hash common.Hash, config *params.ChainConfig) (*types.Receipt, common.Hash, uint64, uint64) {
  112. // Retrieve the context of the receipt based on the transaction hash
  113. blockNumber := ReadTxLookupEntry(db, hash)
  114. if blockNumber == nil {
  115. return nil, common.Hash{}, 0, 0
  116. }
  117. blockHash := ReadCanonicalHash(db, *blockNumber)
  118. if blockHash == (common.Hash{}) {
  119. return nil, common.Hash{}, 0, 0
  120. }
  121. // Read all the receipts from the block and return the one with the matching hash
  122. receipts := ReadReceipts(db, blockHash, *blockNumber, config)
  123. for receiptIndex, receipt := range receipts {
  124. if receipt.TxHash == hash {
  125. return receipt, blockHash, *blockNumber, uint64(receiptIndex)
  126. }
  127. }
  128. log.Error("Receipt not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
  129. return nil, common.Hash{}, 0, 0
  130. }
  131. // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
  132. // section and bit index from the.
  133. func ReadBloomBits(db ethdb.KeyValueReader, bit uint, section uint64, head common.Hash) ([]byte, error) {
  134. return db.Get(bloomBitsKey(bit, section, head))
  135. }
  136. // WriteBloomBits stores the compressed bloom bits vector belonging to the given
  137. // section and bit index.
  138. func WriteBloomBits(db ethdb.KeyValueWriter, bit uint, section uint64, head common.Hash, bits []byte) {
  139. if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil {
  140. log.Crit("Failed to store bloom bits", "err", err)
  141. }
  142. }
  143. // DeleteBloombits removes all compressed bloom bits vector belonging to the
  144. // given section range and bit index.
  145. func DeleteBloombits(db ethdb.Database, bit uint, from uint64, to uint64) {
  146. start, end := bloomBitsKey(bit, from, common.Hash{}), bloomBitsKey(bit, to, common.Hash{})
  147. it := db.NewIterator(nil, start)
  148. defer it.Release()
  149. for it.Next() {
  150. if bytes.Compare(it.Key(), end) >= 0 {
  151. break
  152. }
  153. if len(it.Key()) != len(bloomBitsPrefix)+2+8+32 {
  154. continue
  155. }
  156. db.Delete(it.Key())
  157. }
  158. if it.Error() != nil {
  159. log.Crit("Failed to delete bloom bits", "err", it.Error())
  160. }
  161. }