accessors_indexes.go 4.5 KB

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