Bitcoin ABC  0.26.3
P2P Digital Currency
txindex.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2018 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <index/txindex.h>
6 
7 #include <chain.h>
8 #include <index/disktxpos.h>
9 #include <node/blockstorage.h>
10 #include <util/system.h>
11 #include <validation.h>
12 
14 
15 constexpr char DB_TXINDEX = 't';
16 
17 std::unique_ptr<TxIndex> g_txindex;
18 
20 class TxIndex::DB : public BaseIndex::DB {
21 public:
22  explicit DB(size_t n_cache_size, bool f_memory = false,
23  bool f_wipe = false);
24 
27  bool ReadTxPos(const TxId &txid, CDiskTxPos &pos) const;
28 
30  bool WriteTxs(const std::vector<std::pair<TxId, CDiskTxPos>> &v_pos);
31 };
32 
33 TxIndex::DB::DB(size_t n_cache_size, bool f_memory, bool f_wipe)
34  : BaseIndex::DB(gArgs.GetDataDirNet() / "indexes" / "txindex", n_cache_size,
35  f_memory, f_wipe) {}
36 
37 bool TxIndex::DB::ReadTxPos(const TxId &txid, CDiskTxPos &pos) const {
38  return Read(std::make_pair(DB_TXINDEX, txid), pos);
39 }
40 
42  const std::vector<std::pair<TxId, CDiskTxPos>> &v_pos) {
43  CDBBatch batch(*this);
44  for (const auto &tuple : v_pos) {
45  batch.Write(std::make_pair(DB_TXINDEX, tuple.first), tuple.second);
46  }
47  return WriteBatch(batch);
48 }
49 
50 TxIndex::TxIndex(size_t n_cache_size, bool f_memory, bool f_wipe)
51  : m_db(std::make_unique<TxIndex::DB>(n_cache_size, f_memory, f_wipe)) {}
52 
54 
55 bool TxIndex::WriteBlock(const CBlock &block, const CBlockIndex *pindex) {
56  // Exclude genesis block transaction because outputs are not spendable.
57  if (pindex->nHeight == 0) {
58  return true;
59  }
60 
61  CDiskTxPos pos(WITH_LOCK(::cs_main, return pindex->GetBlockPos()),
62  GetSizeOfCompactSize(block.vtx.size()));
63  std::vector<std::pair<TxId, CDiskTxPos>> vPos;
64  vPos.reserve(block.vtx.size());
65  for (const auto &tx : block.vtx) {
66  vPos.emplace_back(tx->GetId(), pos);
68  }
69  return m_db->WriteTxs(vPos);
70 }
71 
73  return *m_db;
74 }
75 
76 bool TxIndex::FindTx(const TxId &txid, BlockHash &block_hash,
77  CTransactionRef &tx) const {
78  CDiskTxPos postx;
79  if (!m_db->ReadTxPos(txid, postx)) {
80  return false;
81  }
82 
83  CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
84  if (file.IsNull()) {
85  return error("%s: OpenBlockFile failed", __func__);
86  }
87  CBlockHeader header;
88  try {
89  file >> header;
90  if (fseek(file.Get(), postx.nTxOffset, SEEK_CUR)) {
91  return error("%s: fseek(...) failed", __func__);
92  }
93  file >> tx;
94  } catch (const std::exception &e) {
95  return error("%s: Deserialize or I/O error - %s", __func__, e.what());
96  }
97  if (tx->GetId() != txid) {
98  return error("%s: txid mismatch", __func__);
99  }
100  block_hash = header.GetHash();
101  return true;
102 }
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:111
The database stores a block locator of the chain the database is synced to so that the TxIndex can ef...
Definition: base.h:36
Base class for indices of blockchain data.
Definition: base.h:27
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:581
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:624
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:627
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
BlockHash GetHash() const
Definition: block.cpp:11
Definition: block.h:55
std::vector< CTransactionRef > vtx
Definition: block.h:58
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:26
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:39
FlatFilePos GetBlockPos() const EXCLUSIVE_LOCKS_REQUIRED(
Definition: blockindex.h:114
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:48
void Write(const K &key, const V &value)
Definition: dbwrapper.h:73
Access to the txindex database (indexes/txindex/)
Definition: txindex.cpp:20
bool WriteTxs(const std::vector< std::pair< TxId, CDiskTxPos >> &v_pos)
Write a batch of transaction positions to the DB.
Definition: txindex.cpp:41
DB(size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
Definition: txindex.cpp:33
bool ReadTxPos(const TxId &txid, CDiskTxPos &pos) const
Read the disk location of the transaction data with the given ID.
Definition: txindex.cpp:37
TxIndex is used to look up transactions included in the blockchain by ID.
Definition: txindex.h:20
TxIndex(size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
Constructs the index, which becomes available to be queried.
Definition: txindex.cpp:50
bool FindTx(const TxId &txid, BlockHash &block_hash, CTransactionRef &tx) const
Look up a transaction by identifier.
Definition: txindex.cpp:76
BaseIndex::DB & GetDB() const override
Definition: txindex.cpp:72
virtual ~TxIndex() override
Definition: txindex.cpp:53
bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override
Write update index entries for a newly connected block.
Definition: txindex.cpp:55
const std::unique_ptr< DB > m_db
Definition: txindex.h:22
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:38
FILE * OpenBlockFile(const FlatFilePos &pos, bool fReadOnly)
Open a block file (blk?????.dat)
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:319
uint32_t GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 byt...
Definition: serialize.h:391
@ SER_DISK
Definition: serialize.h:167
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1259
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
unsigned int nTxOffset
Definition: disktxpos.h:12
A TxId is the identifier of a transaction.
Definition: txid.h:14
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:276
ArgsManager gArgs
Definition: system.cpp:77
bool error(const char *fmt, const Args &...args)
Definition: system.h:46
constexpr char DB_TXINDEX
Definition: txindex.cpp:15
std::unique_ptr< TxIndex > g_txindex
The global transaction index, used in GetTransaction. May be null.
Definition: txindex.cpp:17