2020-12-31 09:48:25 +01:00
|
|
|
// Copyright (c) 2017-2020 The Bitcoin Core developers
|
2017-12-08 10:19:57 -08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <index/txindex.h>
|
2021-08-04 20:55:31 +02:00
|
|
|
|
|
|
|
#include <index/disktxpos.h>
|
2021-04-18 17:09:48 +02:00
|
|
|
#include <node/blockstorage.h>
|
2018-10-22 15:51:11 -07:00
|
|
|
#include <util/system.h>
|
2017-12-08 10:19:57 -08:00
|
|
|
#include <validation.h>
|
2017-12-08 10:42:31 -08:00
|
|
|
|
2021-05-01 13:49:37 +02:00
|
|
|
constexpr uint8_t DB_TXINDEX{'t'};
|
2018-05-15 17:20:17 -07:00
|
|
|
|
2017-12-08 11:29:59 -08:00
|
|
|
std::unique_ptr<TxIndex> g_txindex;
|
|
|
|
|
2019-01-17 14:08:21 -05:00
|
|
|
|
|
|
|
/** Access to the txindex database (indexes/txindex/) */
|
2018-05-15 17:20:17 -07:00
|
|
|
class TxIndex::DB : public BaseIndex::DB
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit DB(size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
|
|
|
|
|
|
|
|
/// Read the disk location of the transaction data with the given hash. Returns false if the
|
|
|
|
/// transaction hash is not indexed.
|
|
|
|
bool ReadTxPos(const uint256& txid, CDiskTxPos& pos) const;
|
|
|
|
|
|
|
|
/// Write a batch of transaction positions to the DB.
|
|
|
|
bool WriteTxs(const std::vector<std::pair<uint256, CDiskTxPos>>& v_pos);
|
|
|
|
};
|
|
|
|
|
|
|
|
TxIndex::DB::DB(size_t n_cache_size, bool f_memory, bool f_wipe) :
|
2021-05-04 13:00:25 +02:00
|
|
|
BaseIndex::DB(gArgs.GetDataDirNet() / "indexes" / "txindex", n_cache_size, f_memory, f_wipe)
|
2018-05-15 17:20:17 -07:00
|
|
|
{}
|
|
|
|
|
|
|
|
bool TxIndex::DB::ReadTxPos(const uint256 &txid, CDiskTxPos& pos) const
|
|
|
|
{
|
|
|
|
return Read(std::make_pair(DB_TXINDEX, txid), pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TxIndex::DB::WriteTxs(const std::vector<std::pair<uint256, CDiskTxPos>>& v_pos)
|
|
|
|
{
|
|
|
|
CDBBatch batch(*this);
|
|
|
|
for (const auto& tuple : v_pos) {
|
|
|
|
batch.Write(std::make_pair(DB_TXINDEX, tuple.first), tuple.second);
|
|
|
|
}
|
|
|
|
return WriteBatch(batch);
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:26:49 -07:00
|
|
|
TxIndex::TxIndex(size_t n_cache_size, bool f_memory, bool f_wipe)
|
2021-03-10 17:28:08 +08:00
|
|
|
: m_db(std::make_unique<TxIndex::DB>(n_cache_size, f_memory, f_wipe))
|
2018-05-15 17:26:49 -07:00
|
|
|
{}
|
2017-12-08 10:19:57 -08:00
|
|
|
|
2018-05-15 17:20:17 -07:00
|
|
|
TxIndex::~TxIndex() {}
|
|
|
|
|
2017-12-08 10:19:57 -08:00
|
|
|
bool TxIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
|
|
|
|
{
|
2018-08-27 12:28:35 -07:00
|
|
|
// Exclude genesis block transaction because outputs are not spendable.
|
|
|
|
if (pindex->nHeight == 0) return true;
|
|
|
|
|
2017-12-08 10:19:57 -08:00
|
|
|
CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
|
|
|
|
std::vector<std::pair<uint256, CDiskTxPos>> vPos;
|
|
|
|
vPos.reserve(block.vtx.size());
|
|
|
|
for (const auto& tx : block.vtx) {
|
|
|
|
vPos.emplace_back(tx->GetHash(), pos);
|
2018-06-22 18:27:18 +00:00
|
|
|
pos.nTxOffset += ::GetSerializeSize(*tx, CLIENT_VERSION);
|
2017-12-08 10:19:57 -08:00
|
|
|
}
|
|
|
|
return m_db->WriteTxs(vPos);
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:20:17 -07:00
|
|
|
BaseIndex::DB& TxIndex::GetDB() const { return *m_db; }
|
2018-05-15 14:47:37 -07:00
|
|
|
|
2018-03-30 00:39:08 -07:00
|
|
|
bool TxIndex::FindTx(const uint256& tx_hash, uint256& block_hash, CTransactionRef& tx) const
|
2017-12-08 10:19:57 -08:00
|
|
|
{
|
2018-03-30 00:39:08 -07:00
|
|
|
CDiskTxPos postx;
|
|
|
|
if (!m_db->ReadTxPos(tx_hash, postx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
|
|
|
|
if (file.IsNull()) {
|
|
|
|
return error("%s: OpenBlockFile failed", __func__);
|
|
|
|
}
|
|
|
|
CBlockHeader header;
|
|
|
|
try {
|
|
|
|
file >> header;
|
2018-05-02 12:12:55 +02:00
|
|
|
if (fseek(file.Get(), postx.nTxOffset, SEEK_CUR)) {
|
|
|
|
return error("%s: fseek(...) failed", __func__);
|
|
|
|
}
|
2018-03-30 00:39:08 -07:00
|
|
|
file >> tx;
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
return error("%s: Deserialize or I/O error - %s", __func__, e.what());
|
|
|
|
}
|
|
|
|
if (tx->GetHash() != tx_hash) {
|
|
|
|
return error("%s: txid mismatch", __func__);
|
|
|
|
}
|
|
|
|
block_hash = header.GetHash();
|
|
|
|
return true;
|
2017-12-08 10:19:57 -08:00
|
|
|
}
|