2021-12-30 19:36:57 +02:00
|
|
|
// Copyright (c) 2017-2021 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-11-12 10:06:00 -05:00
|
|
|
using node::OpenBlockFile;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-01-13 07:57:54 -05:00
|
|
|
TxIndex::TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
|
2022-08-31 16:43:58 +01:00
|
|
|
: BaseIndex(std::move(chain), "txindex"), 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
|
|
|
|
2022-05-11 16:02:15 +01:00
|
|
|
TxIndex::~TxIndex() = default;
|
2018-05-15 17:20:17 -07:00
|
|
|
|
2022-01-17 20:33:16 -05:00
|
|
|
bool TxIndex::CustomAppend(const interfaces::BlockInfo& block)
|
2017-12-08 10:19:57 -08:00
|
|
|
{
|
2018-08-27 12:28:35 -07:00
|
|
|
// Exclude genesis block transaction because outputs are not spendable.
|
2022-01-17 20:33:16 -05:00
|
|
|
if (block.height == 0) return true;
|
2018-08-27 12:28:35 -07:00
|
|
|
|
2022-01-17 20:33:16 -05:00
|
|
|
assert(block.data);
|
|
|
|
CDiskTxPos pos({block.file_number, block.data_pos}, GetSizeOfCompactSize(block.data->vtx.size()));
|
2017-12-08 10:19:57 -08:00
|
|
|
std::vector<std::pair<uint256, CDiskTxPos>> vPos;
|
2022-01-17 20:33:16 -05:00
|
|
|
vPos.reserve(block.data->vtx.size());
|
|
|
|
for (const auto& tx : block.data->vtx) {
|
2017-12-08 10:19:57 -08:00
|
|
|
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
|
|
|
}
|