accessors_indexes.go 5.8 KB

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