accessors_indexes.go 4.6 KB

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