accessors_indexes.go 4.9 KB

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