2019-04-10 14:30:30 +02:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
2021-06-20 22:44:44 +02:00
|
|
|
* Copyright (C) 2019-2020 Tom Zander <tom@flowee.org>
|
2019-04-10 14:30:30 +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 "IndexerClient.h"
|
|
|
|
|
|
|
|
|
|
#include <Logger.h>
|
|
|
|
|
#include <APIProtocol.h>
|
|
|
|
|
#include <qcoreapplication.h>
|
|
|
|
|
|
|
|
|
|
#include <streaming/MessageBuilder.h>
|
|
|
|
|
#include <streaming/MessageParser.h>
|
|
|
|
|
|
2019-04-11 14:59:14 +02:00
|
|
|
#include <base58.h>
|
|
|
|
|
#include <cashaddr.h>
|
|
|
|
|
|
2020-05-14 22:10:17 +02:00
|
|
|
namespace {
|
|
|
|
|
int digits(int number) {
|
|
|
|
|
int rc = 1;
|
|
|
|
|
while (number >= 10) {
|
|
|
|
|
number /= 10;
|
|
|
|
|
rc++;
|
|
|
|
|
}
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 14:30:30 +02:00
|
|
|
IndexerClient::IndexerClient()
|
2025-02-08 19:05:26 +01:00
|
|
|
: m_network(m_workers.ioContext())
|
2019-04-10 14:30:30 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexerClient::resolve(const QString &lookup)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(m_indexConnection.isValid());
|
|
|
|
|
if (lookup.size() == 64 || (lookup.size() == 66 && lookup.startsWith("0x"))) {
|
|
|
|
|
uint256 hash = uint256S(lookup.toStdString().c_str());
|
|
|
|
|
Streaming::MessageBuilder builder(Streaming::NoHeader, 40);
|
|
|
|
|
builder.add(Api::Indexer::TxId, hash);
|
|
|
|
|
m_indexConnection.send(builder.message(Api::IndexerService,
|
|
|
|
|
Api::Indexer::FindTransaction));
|
|
|
|
|
}
|
2019-04-11 14:59:14 +02:00
|
|
|
|
2019-10-20 12:19:00 +02:00
|
|
|
CashAddress::Content c;
|
2019-04-11 14:59:14 +02:00
|
|
|
CBase58Data old; // legacy address encoding
|
|
|
|
|
if (old.SetString(lookup.toStdString())) {
|
2019-10-20 12:19:00 +02:00
|
|
|
if (old.isMainnetPkh()) {
|
2019-04-11 14:59:14 +02:00
|
|
|
Streaming::MessageBuilder builder(Streaming::NoHeader, 40);
|
2019-10-16 22:42:44 +02:00
|
|
|
builder.addByteArray(Api::Indexer::BitcoinP2PKHAddress, old.data().data(), 20);
|
2019-04-11 14:59:14 +02:00
|
|
|
m_indexConnection.send(builder.message(Api::IndexerService,
|
|
|
|
|
Api::Indexer::FindAddress));
|
2019-10-20 12:19:00 +02:00
|
|
|
} else if (old.isMainnetSh()) {
|
2026-02-09 15:23:48 +01:00
|
|
|
c.type = CashAddress::ScriptType;
|
2019-10-20 12:19:00 +02:00
|
|
|
c.hash = old.data();
|
|
|
|
|
} else {
|
|
|
|
|
logCritical() << "Argument type not understood.";
|
|
|
|
|
return;
|
2019-04-11 14:59:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-10-20 12:19:00 +02:00
|
|
|
else {
|
2026-02-09 15:23:48 +01:00
|
|
|
c = CashAddress::decode(lookup.toStdString(), "bitcoincash");
|
2019-10-20 12:19:00 +02:00
|
|
|
}
|
2019-04-11 14:59:14 +02:00
|
|
|
|
2019-10-20 12:19:00 +02:00
|
|
|
if (c.hash.size() == 20) {
|
2019-04-11 14:59:14 +02:00
|
|
|
Streaming::MessageBuilder builder(Streaming::NoHeader, 40);
|
2019-10-20 12:19:00 +02:00
|
|
|
builder.add(Api::Indexer::BitcoinScriptHashed, CashAddress::createHashedOutputScript(c));
|
|
|
|
|
m_indexConnection.send(builder.message(Api::IndexerService, Api::Indexer::FindAddress));
|
2019-04-11 14:59:14 +02:00
|
|
|
}
|
2019-04-10 14:30:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexerClient::tryConnectIndexer(const EndPoint &ep)
|
|
|
|
|
{
|
2019-10-20 12:19:00 +02:00
|
|
|
m_indexConnection = m_network.connection(ep);
|
2019-04-10 14:30:30 +02:00
|
|
|
if (!m_indexConnection.isValid())
|
|
|
|
|
throw std::runtime_error("Invalid Endpoint, can't create connection");
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
m_indexConnection.setOnConnected(std::bind(&IndexerClient::indexerConnected, this, std::placeholders::_1));
|
|
|
|
|
#endif
|
2019-06-06 17:31:23 +02:00
|
|
|
m_indexConnection.setOnDisconnected(std::bind(&IndexerClient::indexerDisconnected, this));
|
2019-04-10 14:30:30 +02:00
|
|
|
m_indexConnection.setOnIncomingMessage(std::bind(&IndexerClient::onIncomingIndexerMessage, this, std::placeholders::_1));
|
|
|
|
|
m_indexConnection.connect();
|
|
|
|
|
}
|
2019-10-20 12:19:00 +02:00
|
|
|
|
2019-04-10 14:30:30 +02:00
|
|
|
void IndexerClient::tryConnectHub(const EndPoint &ep)
|
|
|
|
|
{
|
2019-10-20 12:19:00 +02:00
|
|
|
m_hubConnection = m_network.connection(ep);
|
2019-04-10 14:30:30 +02:00
|
|
|
if (!m_hubConnection.isValid())
|
|
|
|
|
throw std::runtime_error("Invalid Endpoint, can't create connection");
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
m_hubConnection.setOnConnected(std::bind(&IndexerClient::hubConnected, this, std::placeholders::_1));
|
|
|
|
|
m_hubConnection.setOnDisconnected(std::bind(&IndexerClient::hubDisconnected, this));
|
|
|
|
|
#endif
|
|
|
|
|
m_hubConnection.setOnIncomingMessage(std::bind(&IndexerClient::onIncomingHubMessage, this, std::placeholders::_1));
|
|
|
|
|
m_hubConnection.connect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexerClient::hubConnected(const EndPoint &)
|
|
|
|
|
{
|
|
|
|
|
logDebug() << "Hub connection established";
|
2019-04-10 17:55:08 +02:00
|
|
|
m_hubConnection.send(Message(Api::APIService, Api::Meta::Version));
|
2019-04-10 14:30:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexerClient::hubDisconnected()
|
|
|
|
|
{
|
|
|
|
|
logDebug() << "Hub disconnected";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexerClient::onIncomingHubMessage(const Message &message)
|
|
|
|
|
{
|
2019-04-10 17:55:08 +02:00
|
|
|
if (message.serviceId() == Api::BlockChainService) {
|
|
|
|
|
if (message.messageId() == Api::BlockChain::GetTransactionReply) {
|
|
|
|
|
Streaming::MessageParser parser(message);
|
|
|
|
|
while (parser.next() == Streaming::FoundTag) {
|
|
|
|
|
if (parser.tag() == Api::BlockChain::GenericByteData) {
|
|
|
|
|
auto blob = parser.bytesDataBuffer();
|
|
|
|
|
QByteArray tx(blob.begin(), blob.size());
|
2020-05-14 21:43:48 +02:00
|
|
|
logCritical() << "Transaction follows. Tx-Size:" << tx.size() << "bytes";
|
|
|
|
|
if (tx.size() > 1500) {
|
|
|
|
|
logCritical() << "Large transaction. Use -v to display";
|
|
|
|
|
logInfo() << tx.toHex().constData();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
logCritical() << tx.toHex().constData();
|
|
|
|
|
}
|
2019-04-10 17:55:08 +02:00
|
|
|
QCoreApplication::quit();
|
|
|
|
|
}
|
2019-04-11 14:59:14 +02:00
|
|
|
else if (parser.tag() == Api::BlockChain::TxId) {
|
2020-05-14 21:43:48 +02:00
|
|
|
logCritical() << message.headerInt(Api::RequestId) << " -> " << parser.uint256Data();
|
2019-04-11 14:59:14 +02:00
|
|
|
if (--m_txIdsRequested == 0)
|
|
|
|
|
QCoreApplication::quit();
|
|
|
|
|
}
|
2019-04-10 17:55:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-10 14:30:30 +02:00
|
|
|
}
|
|
|
|
|
|
2019-11-29 11:07:54 +01:00
|
|
|
void IndexerClient::indexerConnected(const EndPoint &)
|
2019-04-10 14:30:30 +02:00
|
|
|
{
|
2020-07-27 21:27:26 +02:00
|
|
|
m_indexConnection.send(Message(Api::IndexerService, Api::Indexer::Version));
|
2019-04-10 17:55:08 +02:00
|
|
|
m_indexConnection.send(Message(Api::IndexerService, Api::Indexer::GetAvailableIndexers));
|
2019-04-10 14:30:30 +02:00
|
|
|
logDebug() << "Indexer connection established";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexerClient::indexerDisconnected()
|
|
|
|
|
{
|
|
|
|
|
logDebug() << "Indexer disconnected";
|
|
|
|
|
QCoreApplication::quit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IndexerClient::onIncomingIndexerMessage(const Message &message)
|
|
|
|
|
{
|
|
|
|
|
if (message.serviceId() == Api::IndexerService) {
|
|
|
|
|
if (message.messageId() == Api::Indexer::FindTransactionReply) {
|
|
|
|
|
int blockHeight = -1, offsetInBlock = 0;
|
|
|
|
|
Streaming::MessageParser parser(message);
|
|
|
|
|
while (parser.next() == Streaming::FoundTag) {
|
|
|
|
|
if (parser.tag() == Api::Indexer::BlockHeight)
|
|
|
|
|
blockHeight = parser.intData();
|
2019-04-11 14:59:14 +02:00
|
|
|
else if (parser.tag() == Api::Indexer::OffsetInBlock)
|
2019-04-10 14:30:30 +02:00
|
|
|
offsetInBlock = parser.intData();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-14 21:43:48 +02:00
|
|
|
logCritical().nospace() << "Transaction location is: [block=" << blockHeight << "+" << offsetInBlock << "]";
|
2019-04-10 17:55:08 +02:00
|
|
|
if (blockHeight > 0 && offsetInBlock > 80 && m_hubConnection.isValid()) {
|
2019-04-10 14:30:30 +02:00
|
|
|
Streaming::MessageBuilder builder(Streaming::NoHeader, 20);
|
|
|
|
|
builder.add(Api::BlockChain::BlockHeight, blockHeight);
|
2019-04-10 17:55:08 +02:00
|
|
|
builder.add(Api::BlockChain::Tx_OffsetInBlock, offsetInBlock);
|
2019-04-10 14:30:30 +02:00
|
|
|
m_hubConnection.send(builder.message(Api::BlockChainService, Api::BlockChain::GetTransaction));
|
2019-04-10 17:55:08 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
QCoreApplication::quit();
|
2019-04-10 14:30:30 +02:00
|
|
|
}
|
2019-04-11 14:59:14 +02:00
|
|
|
else if (message.messageId() == Api::Indexer::FindAddressReply) {
|
|
|
|
|
int usageId = 0;
|
|
|
|
|
Streaming::MessageParser parser(message);
|
2020-05-14 22:10:17 +02:00
|
|
|
while (parser.next() == Streaming::FoundTag) {
|
|
|
|
|
if (parser.tag() == Api::Indexer::Separator)
|
|
|
|
|
usageId++;
|
|
|
|
|
}
|
|
|
|
|
const int width = digits(usageId);
|
|
|
|
|
|
|
|
|
|
usageId = 0;
|
|
|
|
|
int blockHeight = -1, offsetInBlock = 0, index = 0;
|
|
|
|
|
parser = Streaming::MessageParser(message);
|
2019-04-11 14:59:14 +02:00
|
|
|
while (parser.next() == Streaming::FoundTag) {
|
|
|
|
|
if (parser.tag() == Api::Indexer::BlockHeight)
|
|
|
|
|
blockHeight = parser.intData();
|
|
|
|
|
else if (parser.tag() == Api::Indexer::OffsetInBlock)
|
|
|
|
|
offsetInBlock = parser.intData();
|
|
|
|
|
else if (parser.tag() == Api::Indexer::OutIndex)
|
|
|
|
|
index = parser.intData();
|
|
|
|
|
else if (parser.tag() == Api::Indexer::Separator) {
|
2020-05-14 22:10:17 +02:00
|
|
|
auto log = logCritical().nospace();
|
|
|
|
|
for (auto i = digits(++usageId); i < width; ++i)
|
|
|
|
|
log << 0;
|
|
|
|
|
log << usageId;
|
|
|
|
|
log << "] Address touches [block=" << blockHeight << "+" << offsetInBlock;
|
|
|
|
|
log << "|" << index << "]";
|
2019-04-11 14:59:14 +02:00
|
|
|
if (blockHeight > 0 && offsetInBlock > 80 && m_hubConnection.isValid()) {
|
|
|
|
|
Streaming::MessageBuilder builder(Streaming::NoHeader, 20);
|
|
|
|
|
builder.add(Api::BlockChain::BlockHeight, blockHeight);
|
|
|
|
|
builder.add(Api::BlockChain::Tx_OffsetInBlock, offsetInBlock);
|
|
|
|
|
builder.add(Api::BlockChain::Include_TxId, true);
|
|
|
|
|
Message m = builder.message(Api::BlockChainService, Api::BlockChain::GetTransaction);
|
|
|
|
|
m.setHeaderInt(Api::RequestId, usageId);
|
|
|
|
|
m_hubConnection.send(m);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-20 21:53:44 +02:00
|
|
|
if (!m_hubConnection.isValid() || (blockHeight == -1))
|
2019-04-11 14:59:14 +02:00
|
|
|
QCoreApplication::quit();
|
2020-05-14 21:43:48 +02:00
|
|
|
m_txIdsRequested += usageId;
|
2019-04-11 14:59:14 +02:00
|
|
|
}
|
2019-06-06 17:31:23 +02:00
|
|
|
else if (message.messageId() == Api::Indexer::GetAvailableIndexersReply) {
|
|
|
|
|
Streaming::MessageParser parser(message);
|
|
|
|
|
while (parser.next() == Streaming::FoundTag) {
|
|
|
|
|
if (parser.tag() == Api::Indexer::AddressIndexer)
|
2020-05-14 21:43:48 +02:00
|
|
|
logInfo() << "Info: remote indexer has Address Index";
|
2019-06-06 17:31:23 +02:00
|
|
|
else if (parser.tag() == Api::Indexer::TxIdIndexer)
|
2020-05-14 21:43:48 +02:00
|
|
|
logInfo() << "Info: remote indexer has TXID Index";
|
2019-11-29 11:07:54 +01:00
|
|
|
else if (parser.tag() == Api::Indexer::SpentOutputIndexer)
|
2020-05-14 21:43:48 +02:00
|
|
|
logInfo() << "Info: remote indexer has SpentOutput Index";
|
2019-06-06 17:31:23 +02:00
|
|
|
}
|
2020-07-27 21:27:26 +02:00
|
|
|
}
|
|
|
|
|
else if (message.messageId() == Api::Indexer::VersionReply) {
|
|
|
|
|
Streaming::MessageParser parser(message);
|
|
|
|
|
while (parser.next() == Streaming::FoundTag) {
|
|
|
|
|
if (parser.tag() == Api::Indexer::GenericByteData)
|
|
|
|
|
logCritical() << "Info: remote indexer at version" << parser.stringData();
|
|
|
|
|
}
|
2019-06-06 17:31:23 +02:00
|
|
|
} else
|
2019-04-11 14:59:14 +02:00
|
|
|
Streaming::MessageParser::debugMessage(message);
|
2019-04-10 14:30:30 +02:00
|
|
|
}
|
2019-04-10 17:55:08 +02:00
|
|
|
else
|
|
|
|
|
Streaming::MessageParser::debugMessage(message);
|
2019-04-10 14:30:30 +02:00
|
|
|
}
|