Files

139 lines
4.3 KiB
C++
Raw Permalink Normal View History

2019-09-23 11:07:45 +02:00
/*
* This file is part of the Flowee project
2021-06-20 22:44:44 +02:00
* Copyright (C) 2019 Tom Zander <tom@flowee.org>
2019-09-23 11:07:45 +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/>.
*/
#ifndef BITCOREPROXY_H
#define BITCOREPROXY_H
#include <QString>
2019-09-23 11:07:45 +02:00
#include <Blockchain.h>
#include <QJsonObject>
#include <httpengine/server.h>
2019-10-05 16:01:52 +02:00
#include <boost/unordered_map.hpp>
2019-09-23 11:07:45 +02:00
class BitCoreWebRequest;
struct RequestString
{
RequestString(const QString &path);
QString anonPath() const;
QString wholePath;
QString chain;
QString network;
QString request;
QString post;
};
2019-10-05 16:01:52 +02:00
struct TxRef {
TxRef(int blockHeight, int offsetInBlock)
: blockHeight(blockHeight),
offsetInBlock(offsetInBlock)
{
}
int blockHeight = 0, offsetInBlock = 0;
QMap<int, QPair<int, int> > spentOutputs; // output-index to blockHeight, offset pair
};
2019-09-23 11:07:45 +02:00
class BitcoreWebRequest : public HttpEngine::WebRequest, public Blockchain::Search
{
Q_OBJECT
public:
BitcoreWebRequest(qintptr socketDescriptor, std::function<void(HttpEngine::WebRequest*)> &handler);
2019-10-08 10:26:22 +02:00
~BitcoreWebRequest() override;
2019-09-23 11:07:45 +02:00
enum {
Unset,
TxForHeight,
TxForBlockHash,
TxForTxId,
TxForTxIdAuthHead,
TxForTxIdCoins,
AddressTxs,
AddressUnspentOutputs,
AddressBalance,
} answerType = Unset;
QJsonObject &map();
// Blockchain::Search interface
void finished(int unfinishedJobs) override;
2020-09-02 14:03:01 +02:00
void transactionAdded(const Blockchain::Transaction &transaction, int resultIndex) override;
2019-09-23 11:07:45 +02:00
void txIdResolved(int jobId, int blockHeight, int offsetInBlock) override;
void spentOutputResolved(int jobId, int blockHeight, int offsetInBlock) override;
2019-10-05 16:01:52 +02:00
void addressUsedInOutput(int blockHeight, int offsetInBlock, int outIndex) override;
2020-01-17 19:44:29 +01:00
void utxoLookup(int jobId, int blockHeight, int offsetInBlock, int outindex, bool unspent, int64_t amount, Streaming::ConstBuffer outputScript) override;
2020-09-02 14:03:01 +02:00
void aborted(const Blockchain::ServiceUnavailableException &) override;
2019-09-23 11:07:45 +02:00
QJsonObject m_map;
private slots:
void threadSafeFinished();
private:
// add things like 'network', 'chain' and '_id'
void addDefaults(QJsonObject &node);
boost::unordered_map<uint256, int, HashShortener> blockHeights;
2019-10-05 16:01:52 +02:00
// remembers the transactions we looked up and the outpoints we were interested in and who spent those.
// key: pair of blockHeight to offsetInBlock (aka transaction)
// value: map of outindex to a pair indicating the spending transaction
2020-01-17 19:44:29 +01:00
// when the target-pair is (-1, 0) then we have no idea who spent the output.
// * (-2, 0) => output is unspent.
// * (120, 81) => tx indicated spends the output
2019-10-05 16:01:52 +02:00
std::map<std::pair<int,int>, std::map<int, std::pair<int, int>> > txRefs;
2019-10-08 10:26:22 +02:00
#ifdef BENCH
QDateTime startTime;
#endif
2019-09-23 11:07:45 +02:00
};
class BitcoreProxy : public QObject, public Blockchain::SearchEngine
2019-09-23 11:07:45 +02:00
{
Q_OBJECT
public:
BitcoreProxy();
// http engine callback
void onIncomingConnection(HttpEngine::WebRequest *request);
void parseConfig(const std::string &confFile) override;
2019-09-23 11:07:45 +02:00
void initializeHubConnection(NetworkConnection connection, const std::string &hubVersion) override;
2019-09-23 11:07:45 +02:00
public slots:
void onReparseConfig();
2019-09-23 11:07:45 +02:00
private:
void returnEnabledChains(HttpEngine::WebRequest *request) const;
void requestTransactionInfo(const RequestString &rs, BitcoreWebRequest *request);
void requestAddressInfo(const RequestString &rs, BitcoreWebRequest *request);
void requestBlockInfo(const RequestString &rs, BitcoreWebRequest *request);
void returnFeeSuggestion(const RequestString &rs, BitcoreWebRequest *request);
void returnDailyTransactions(const RequestString &rs, BitcoreWebRequest *request);
private:
void findServices(const QString &configFile);
2019-09-23 11:07:45 +02:00
};
#endif