accessors_indexes.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "github.com/ethereum/go-ethereum/common"
  19. "github.com/ethereum/go-ethereum/core/types"
  20. "github.com/ethereum/go-ethereum/ethdb"
  21. "github.com/ethereum/go-ethereum/log"
  22. "github.com/ethereum/go-ethereum/params"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. // ReadTxLookupEntry retrieves the positional metadata associated with a transaction
  26. // hash to allow retrieving the transaction or receipt by hash.
  27. func ReadTxLookupEntry(db ethdb.Reader, hash common.Hash) common.Hash {
  28. data, _ := db.Get(txLookupKey(hash))
  29. if len(data) == 0 {
  30. return common.Hash{}
  31. }
  32. if len(data) == common.HashLength {
  33. return common.BytesToHash(data)
  34. }
  35. // Probably it's legacy txlookup entry data, try to decode it.
  36. var entry LegacyTxLookupEntry
  37. if err := rlp.DecodeBytes(data, &entry); err != nil {
  38. log.Error("Invalid transaction lookup entry RLP", "hash", hash, "blob", data, "err", err)
  39. return common.Hash{}
  40. }
  41. return entry.BlockHash
  42. }
  43. // WriteTxLookupEntries stores a positional metadata for every transaction from
  44. // a block, enabling hash based transaction and receipt lookups.
  45. func WriteTxLookupEntries(db ethdb.Writer, block *types.Block) {
  46. for _, tx := range block.Transactions() {
  47. if err := db.Put(txLookupKey(tx.Hash()), block.Hash().Bytes()); err != nil {
  48. log.Crit("Failed to store transaction lookup entry", "err", err)
  49. }
  50. }
  51. }
  52. // DeleteTxLookupEntry removes all transaction data associated with a hash.
  53. func DeleteTxLookupEntry(db ethdb.Writer, hash common.Hash) {
  54. db.Delete(txLookupKey(hash))
  55. }
  56. // ReadTransaction retrieves a specific transaction from the database, along with
  57. // its added positional metadata.
  58. func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
  59. blockHash := ReadTxLookupEntry(db, hash)
  60. if blockHash == (common.Hash{}) {
  61. return nil, common.Hash{}, 0, 0
  62. }
  63. blockNumber := ReadHeaderNumber(db, blockHash)
  64. if blockNumber == nil {
  65. return nil, common.Hash{}, 0, 0
  66. }
  67. body := ReadBody(db, blockHash, *blockNumber)
  68. if body == nil {
  69. log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash)
  70. return nil, common.Hash{}, 0, 0
  71. }
  72. for txIndex, tx := range body.Transactions {
  73. if tx.Hash() == hash {
  74. return tx, blockHash, *blockNumber, uint64(txIndex)
  75. }
  76. }
  77. log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
  78. return nil, common.Hash{}, 0, 0
  79. }
  80. // ReadReceipt retrieves a specific transaction receipt from the database, along with
  81. // its added positional metadata.
  82. func ReadReceipt(db ethdb.Reader, hash common.Hash, config *params.ChainConfig) (*types.Receipt, common.Hash, uint64, uint64) {
  83. // Retrieve the context of the receipt based on the transaction hash
  84. blockHash := ReadTxLookupEntry(db, hash)
  85. if blockHash == (common.Hash{}) {
  86. return nil, common.Hash{}, 0, 0
  87. }
  88. blockNumber := ReadHeaderNumber(db, blockHash)
  89. if blockNumber == nil {
  90. return nil, common.Hash{}, 0, 0
  91. }
  92. // Read all the receipts from the block and return the one with the matching hash
  93. receipts := ReadReceipts(db, blockHash, *blockNumber, config)
  94. for receiptIndex, receipt := range receipts {
  95. if receipt.TxHash == hash {
  96. return receipt, blockHash, *blockNumber, uint64(receiptIndex)
  97. }
  98. }
  99. log.Error("Receipt not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
  100. return nil, common.Hash{}, 0, 0
  101. }
  102. // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
  103. // section and bit index from the.
  104. func ReadBloomBits(db ethdb.Reader, bit uint, section uint64, head common.Hash) ([]byte, error) {
  105. return db.Get(bloomBitsKey(bit, section, head))
  106. }
  107. // WriteBloomBits stores the compressed bloom bits vector belonging to the given
  108. // section and bit index.
  109. func WriteBloomBits(db ethdb.Writer, bit uint, section uint64, head common.Hash, bits []byte) {
  110. if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil {
  111. log.Crit("Failed to store bloom bits", "err", err)
  112. }
  113. }