accessors_indexes.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. for _, tx := range block.Transactions() {
  54. if err := db.Put(txLookupKey(tx.Hash()), block.Number().Bytes()); err != nil {
  55. log.Crit("Failed to store transaction lookup entry", "err", err)
  56. }
  57. }
  58. }
  59. // DeleteTxLookupEntry removes all transaction data associated with a hash.
  60. func DeleteTxLookupEntry(db ethdb.KeyValueWriter, hash common.Hash) {
  61. db.Delete(txLookupKey(hash))
  62. }
  63. // ReadTransaction retrieves a specific transaction from the database, along with
  64. // its added positional metadata.
  65. func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
  66. blockNumber := ReadTxLookupEntry(db, hash)
  67. if blockNumber == nil {
  68. return nil, common.Hash{}, 0, 0
  69. }
  70. blockHash := ReadCanonicalHash(db, *blockNumber)
  71. if blockHash == (common.Hash{}) {
  72. return nil, common.Hash{}, 0, 0
  73. }
  74. body := ReadBody(db, blockHash, *blockNumber)
  75. if body == nil {
  76. log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash)
  77. return nil, common.Hash{}, 0, 0
  78. }
  79. for txIndex, tx := range body.Transactions {
  80. if tx.Hash() == hash {
  81. return tx, blockHash, *blockNumber, uint64(txIndex)
  82. }
  83. }
  84. log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
  85. return nil, common.Hash{}, 0, 0
  86. }
  87. // ReadReceipt retrieves a specific transaction receipt from the database, along with
  88. // its added positional metadata.
  89. func ReadReceipt(db ethdb.Reader, hash common.Hash, config *params.ChainConfig) (*types.Receipt, common.Hash, uint64, uint64) {
  90. // Retrieve the context of the receipt based on the transaction hash
  91. blockNumber := ReadTxLookupEntry(db, hash)
  92. if blockNumber == nil {
  93. return nil, common.Hash{}, 0, 0
  94. }
  95. blockHash := ReadCanonicalHash(db, *blockNumber)
  96. if blockHash == (common.Hash{}) {
  97. return nil, common.Hash{}, 0, 0
  98. }
  99. // Read all the receipts from the block and return the one with the matching hash
  100. receipts := ReadReceipts(db, blockHash, *blockNumber, config)
  101. for receiptIndex, receipt := range receipts {
  102. if receipt.TxHash == hash {
  103. return receipt, blockHash, *blockNumber, uint64(receiptIndex)
  104. }
  105. }
  106. log.Error("Receipt not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
  107. return nil, common.Hash{}, 0, 0
  108. }
  109. // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
  110. // section and bit index from the.
  111. func ReadBloomBits(db ethdb.KeyValueReader, bit uint, section uint64, head common.Hash) ([]byte, error) {
  112. return db.Get(bloomBitsKey(bit, section, head))
  113. }
  114. // WriteBloomBits stores the compressed bloom bits vector belonging to the given
  115. // section and bit index.
  116. func WriteBloomBits(db ethdb.KeyValueWriter, bit uint, section uint64, head common.Hash, bits []byte) {
  117. if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil {
  118. log.Crit("Failed to store bloom bits", "err", err)
  119. }
  120. }