Files

111 lines
3.9 KiB
C++
Raw Permalink Normal View History

2019-06-11 17:28:16 +02:00
/*
* This file is part of the Flowee project
2021-02-06 23:09:03 +01:00
* Copyright (C) 2019-2021 Tom Zander <tom@flowee.org>
2019-06-11 17:28:16 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SpentOuputIndexer.h"
2019-06-26 21:39:07 +02:00
#include "Indexer.h"
2019-06-11 17:28:16 +02:00
2019-06-26 21:39:07 +02:00
#include <Message.h>
#include <APIProtocol.h>
#include <streaming/MessageParser.h>
2025-02-08 19:05:26 +01:00
SpentOutputIndexer::SpentOutputIndexer(boost::asio::io_context &context, const boost::filesystem::path &basedir, Indexer *indexer)
: m_txdb(context, basedir),
2019-06-26 21:39:07 +02:00
m_dataSource(indexer)
2019-06-11 17:28:16 +02:00
{
2019-06-12 19:52:07 +02:00
UnspentOutputDatabase::setChangeCountCausesStore(50000);
2019-06-26 21:39:07 +02:00
assert(indexer);
2019-06-11 17:28:16 +02:00
}
2019-06-26 21:39:07 +02:00
int SpentOutputIndexer::blockheight() const
2019-06-11 17:28:16 +02:00
{
return m_txdb.blockheight();
}
2019-06-26 21:39:07 +02:00
uint256 SpentOutputIndexer::blockId() const
2019-06-11 17:28:16 +02:00
{
return m_txdb.blockId();
}
2019-06-26 21:39:07 +02:00
void SpentOutputIndexer::blockFinished(int blockheight, const uint256 &blockId)
2019-06-11 17:28:16 +02:00
{
m_txdb.blockFinished(blockheight, blockId);
}
2019-06-26 21:39:07 +02:00
void SpentOutputIndexer::insertSpentTransaction(const uint256 &prevTxId, int prevOutIndex, int blockHeight, int offsetInBlock)
2019-06-11 17:28:16 +02:00
{
m_txdb.insert(prevTxId, prevOutIndex, blockHeight, offsetInBlock);
}
2019-06-26 21:39:07 +02:00
SpentOutputIndexer::TxData SpentOutputIndexer::findSpendingTx(const uint256 &txid, int output) const
2019-06-11 17:28:16 +02:00
{
TxData answer;
auto item = m_txdb.find(txid, output);
if (item.isValid()) {
answer.blockHeight = item.blockHeight();
answer.offsetInBlock = item.offsetInBlock();
}
return answer;
}
2019-06-26 21:39:07 +02:00
void SpentOutputIndexer::run()
{
assert(m_dataSource);
while (!isInterruptionRequested()) {
logDebug() << "want block" << m_txdb.blockheight() + 1;
2019-12-12 16:02:08 +01:00
int tipOfChain;
Message message = m_dataSource->nextBlock(m_txdb.blockheight() + 1, &tipOfChain);
2019-06-26 21:39:07 +02:00
if (message.body().size() == 0)
continue;
int txOffsetInBlock = 0;
uint256 blockId;
int blockHeight = -1;
uint256 prevTxId;
bool gotPrevTxId = false;
Streaming::MessageParser parser(message.body());
while (parser.next() == Streaming::FoundTag) {
if (parser.tag() == Api::BlockChain::BlockHeight) {
blockHeight = parser.intData();
2021-02-06 23:09:03 +01:00
assert(blockHeight == m_txdb.blockheight() + 1);
2019-06-26 21:39:07 +02:00
} else if (parser.tag() == Api::BlockChain::BlockHash) {
blockId = parser.uint256Data();
} else if (parser.tag() == Api::BlockChain::Separator) {
txOffsetInBlock = 0;
} else if (parser.tag() == Api::BlockChain::Tx_OffsetInBlock) {
txOffsetInBlock = parser.intData();
} else if (txOffsetInBlock > 90 && parser.tag() == Api::BlockChain::Tx_IN_TxId) {
prevTxId = parser.uint256Data();
gotPrevTxId = true;
} else if (txOffsetInBlock > 90 && parser.tag() == Api::BlockChain::Tx_IN_OutIndex) {
2021-02-06 23:09:03 +01:00
assert(gotPrevTxId);
2019-06-26 21:39:07 +02:00
gotPrevTxId = false;
2021-02-06 23:09:03 +01:00
assert(!prevTxId.IsNull());
assert(parser.isInt());
assert(blockHeight >= 0);
assert(txOffsetInBlock > 80);
2019-06-26 21:39:07 +02:00
m_txdb.insert(prevTxId, parser.intData(), blockHeight, txOffsetInBlock);
}
}
assert(blockHeight > 0);
assert(!blockId.IsNull());
m_txdb.blockFinished(blockHeight, blockId);
2019-12-12 16:02:08 +01:00
if (blockHeight == tipOfChain)
m_txdb.saveCaches();
2019-06-26 21:39:07 +02:00
}
}