accessors_indexes.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/log"
  21. "github.com/ethereum/go-ethereum/rlp"
  22. )
  23. // ReadTxLookupEntry retrieves the positional metadata associated with a transaction
  24. // hash to allow retrieving the transaction or receipt by hash.
  25. func ReadTxLookupEntry(db DatabaseReader, hash common.Hash) (common.Hash, uint64, uint64) {
  26. data, _ := db.Get(txLookupKey(hash))
  27. if len(data) == 0 {
  28. return common.Hash{}, 0, 0
  29. }
  30. var entry TxLookupEntry
  31. if err := rlp.DecodeBytes(data, &entry); err != nil {
  32. log.Error("Invalid transaction lookup entry RLP", "hash", hash, "err", err)
  33. return common.Hash{}, 0, 0
  34. }
  35. return entry.BlockHash, entry.BlockIndex, entry.Index
  36. }
  37. // WriteTxLookupEntries stores a positional metadata for every transaction from
  38. // a block, enabling hash based transaction and receipt lookups.
  39. func WriteTxLookupEntries(db DatabaseWriter, block *types.Block) {
  40. for i, tx := range block.Transactions() {
  41. entry := TxLookupEntry{
  42. BlockHash: block.Hash(),
  43. BlockIndex: block.NumberU64(),
  44. Index: uint64(i),
  45. }
  46. data, err := rlp.EncodeToBytes(entry)
  47. if err != nil {
  48. log.Crit("Failed to encode transaction lookup entry", "err", err)
  49. }
  50. if err := db.Put(txLookupKey(tx.Hash()), data); err != nil {
  51. log.Crit("Failed to store transaction lookup entry", "err", err)
  52. }
  53. }
  54. }
  55. // DeleteTxLookupEntry removes all transaction data associated with a hash.
  56. func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) {
  57. db.Delete(txLookupKey(hash))
  58. }
  59. // ReadTransaction retrieves a specific transaction from the database, along with
  60. // its added positional metadata.
  61. func ReadTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
  62. blockHash, blockNumber, txIndex := ReadTxLookupEntry(db, hash)
  63. if blockHash == (common.Hash{}) {
  64. return nil, common.Hash{}, 0, 0
  65. }
  66. body := ReadBody(db, blockHash, blockNumber)
  67. if body == nil || len(body.Transactions) <= int(txIndex) {
  68. log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash, "index", txIndex)
  69. return nil, common.Hash{}, 0, 0
  70. }
  71. return body.Transactions[txIndex], blockHash, blockNumber, txIndex
  72. }
  73. // ReadReceipt retrieves a specific transaction receipt from the database, along with
  74. // its added positional metadata.
  75. func ReadReceipt(db DatabaseReader, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) {
  76. blockHash, blockNumber, receiptIndex := ReadTxLookupEntry(db, hash)
  77. if blockHash == (common.Hash{}) {
  78. return nil, common.Hash{}, 0, 0
  79. }
  80. receipts := ReadReceipts(db, blockHash, blockNumber)
  81. if len(receipts) <= int(receiptIndex) {
  82. log.Error("Receipt refereced missing", "number", blockNumber, "hash", blockHash, "index", receiptIndex)
  83. return nil, common.Hash{}, 0, 0
  84. }
  85. return receipts[receiptIndex], blockHash, blockNumber, receiptIndex
  86. }
  87. // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
  88. // section and bit index from the.
  89. func ReadBloomBits(db DatabaseReader, bit uint, section uint64, head common.Hash) ([]byte, error) {
  90. return db.Get(bloomBitsKey(bit, section, head))
  91. }
  92. // WriteBloomBits stores the compressed bloom bits vector belonging to the given
  93. // section and bit index.
  94. func WriteBloomBits(db DatabaseWriter, bit uint, section uint64, head common.Hash, bits []byte) {
  95. if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil {
  96. log.Crit("Failed to store bloom bits", "err", err)
  97. }
  98. }