accessors_indexes.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // writeTxLookupEntry stores a positional metadata for a transaction,
  52. // enabling hash based transaction and receipt lookups.
  53. func writeTxLookupEntry(db ethdb.KeyValueWriter, hash common.Hash, numberBytes []byte) {
  54. if err := db.Put(txLookupKey(hash), numberBytes); err != nil {
  55. log.Crit("Failed to store transaction lookup entry", "err", err)
  56. }
  57. }
  58. // WriteTxLookupEntries is identical to WriteTxLookupEntry, but it works on
  59. // a list of hashes
  60. func WriteTxLookupEntries(db ethdb.KeyValueWriter, number uint64, hashes []common.Hash) {
  61. numberBytes := new(big.Int).SetUint64(number).Bytes()
  62. for _, hash := range hashes {
  63. writeTxLookupEntry(db, hash, numberBytes)
  64. }
  65. }
  66. // WriteTxLookupEntriesByBlock stores a positional metadata for every transaction from
  67. // a block, enabling hash based transaction and receipt lookups.
  68. func WriteTxLookupEntriesByBlock(db ethdb.KeyValueWriter, block *types.Block) {
  69. numberBytes := block.Number().Bytes()
  70. for _, tx := range block.Transactions() {
  71. writeTxLookupEntry(db, tx.Hash(), numberBytes)
  72. }
  73. }
  74. // DeleteTxLookupEntry removes all transaction data associated with a hash.
  75. func DeleteTxLookupEntry(db ethdb.KeyValueWriter, hash common.Hash) {
  76. if err := db.Delete(txLookupKey(hash)); err != nil {
  77. log.Crit("Failed to delete transaction lookup entry", "err", err)
  78. }
  79. }
  80. // DeleteTxLookupEntries removes all transaction lookups for a given block.
  81. func DeleteTxLookupEntries(db ethdb.KeyValueWriter, hashes []common.Hash) {
  82. for _, hash := range hashes {
  83. DeleteTxLookupEntry(db, hash)
  84. }
  85. }
  86. // ReadTransaction retrieves a specific transaction from the database, along with
  87. // its added positional metadata.
  88. func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
  89. blockNumber := ReadTxLookupEntry(db, hash)
  90. if blockNumber == nil {
  91. return nil, common.Hash{}, 0, 0
  92. }
  93. blockHash := ReadCanonicalHash(db, *blockNumber)
  94. if blockHash == (common.Hash{}) {
  95. return nil, common.Hash{}, 0, 0
  96. }
  97. body := ReadBody(db, blockHash, *blockNumber)
  98. if body == nil {
  99. log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash)
  100. return nil, common.Hash{}, 0, 0
  101. }
  102. for txIndex, tx := range body.Transactions {
  103. if tx.Hash() == hash {
  104. return tx, blockHash, *blockNumber, uint64(txIndex)
  105. }
  106. }
  107. log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
  108. return nil, common.Hash{}, 0, 0
  109. }
  110. // ReadReceipt retrieves a specific transaction receipt from the database, along with
  111. // its added positional metadata.
  112. func ReadReceipt(db ethdb.Reader, hash common.Hash, config *params.ChainConfig) (*types.Receipt, common.Hash, uint64, uint64) {
  113. // Retrieve the context of the receipt based on the transaction hash
  114. blockNumber := ReadTxLookupEntry(db, hash)
  115. if blockNumber == nil {
  116. return nil, common.Hash{}, 0, 0
  117. }
  118. blockHash := ReadCanonicalHash(db, *blockNumber)
  119. if blockHash == (common.Hash{}) {
  120. return nil, common.Hash{}, 0, 0
  121. }
  122. // Read all the receipts from the block and return the one with the matching hash
  123. receipts := ReadReceipts(db, blockHash, *blockNumber, config)
  124. for receiptIndex, receipt := range receipts {
  125. if receipt.TxHash == hash {
  126. return receipt, blockHash, *blockNumber, uint64(receiptIndex)
  127. }
  128. }
  129. log.Error("Receipt not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
  130. return nil, common.Hash{}, 0, 0
  131. }
  132. // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
  133. // section and bit index from the.
  134. func ReadBloomBits(db ethdb.KeyValueReader, bit uint, section uint64, head common.Hash) ([]byte, error) {
  135. return db.Get(bloomBitsKey(bit, section, head))
  136. }
  137. // WriteBloomBits stores the compressed bloom bits vector belonging to the given
  138. // section and bit index.
  139. func WriteBloomBits(db ethdb.KeyValueWriter, bit uint, section uint64, head common.Hash, bits []byte) {
  140. if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil {
  141. log.Crit("Failed to store bloom bits", "err", err)
  142. }
  143. }
  144. // DeleteBloombits removes all compressed bloom bits vector belonging to the
  145. // given section range and bit index.
  146. func DeleteBloombits(db ethdb.Database, bit uint, from uint64, to uint64) {
  147. start, end := bloomBitsKey(bit, from, common.Hash{}), bloomBitsKey(bit, to, common.Hash{})
  148. it := db.NewIterator(nil, start)
  149. defer it.Release()
  150. for it.Next() {
  151. if bytes.Compare(it.Key(), end) >= 0 {
  152. break
  153. }
  154. if len(it.Key()) != len(bloomBitsPrefix)+2+8+32 {
  155. continue
  156. }
  157. db.Delete(it.Key())
  158. }
  159. if it.Error() != nil {
  160. log.Crit("Failed to delete bloom bits", "err", it.Error())
  161. }
  162. }