2014-11-11 10:52:43 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-12-17 02:47:57 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
2014-11-19 10:53:46 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-11-11 10:52:43 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2014-11-18 22:03:02 +01:00
|
|
|
#include "primitives/block.h"
|
|
|
|
#include "primitives/transaction.h"
|
2014-11-11 10:52:43 +01:00
|
|
|
#include "main.h"
|
2014-11-19 10:53:46 +01:00
|
|
|
#include "rpcserver.h"
|
|
|
|
#include "streams.h"
|
2014-11-18 16:30:51 +01:00
|
|
|
#include "sync.h"
|
2014-12-01 12:38:42 +01:00
|
|
|
#include "txmempool.h"
|
2014-11-19 10:53:46 +01:00
|
|
|
#include "utilstrencodings.h"
|
|
|
|
#include "version.h"
|
|
|
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
2014-12-01 12:38:42 +01:00
|
|
|
#include <boost/dynamic_bitset.hpp>
|
2014-11-11 10:52:43 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace json_spirit;
|
|
|
|
|
2015-05-27 15:56:16 +02:00
|
|
|
static const int MAX_GETUTXOS_OUTPOINTS = 15; //allow a max of 15 outpoints to be queried at once
|
2014-12-01 12:38:42 +01:00
|
|
|
|
2014-11-11 10:52:43 +01:00
|
|
|
enum RetFormat {
|
2014-11-26 14:17:33 +01:00
|
|
|
RF_UNDEF,
|
2014-11-11 10:52:43 +01:00
|
|
|
RF_BINARY,
|
|
|
|
RF_HEX,
|
|
|
|
RF_JSON,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
enum RetFormat rf;
|
2014-11-26 14:17:33 +01:00
|
|
|
const char* name;
|
2014-11-11 10:52:43 +01:00
|
|
|
} rf_names[] = {
|
2014-11-26 14:17:33 +01:00
|
|
|
{RF_UNDEF, ""},
|
|
|
|
{RF_BINARY, "bin"},
|
|
|
|
{RF_HEX, "hex"},
|
|
|
|
{RF_JSON, "json"},
|
2014-11-11 10:52:43 +01:00
|
|
|
};
|
|
|
|
|
2014-12-01 12:38:42 +01:00
|
|
|
struct CCoin {
|
|
|
|
uint32_t nTxVer; // Don't call this nVersion, that name has a special meaning inside IMPLEMENT_SERIALIZE
|
|
|
|
uint32_t nHeight;
|
|
|
|
CTxOut out;
|
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
|
|
|
|
{
|
|
|
|
READWRITE(nTxVer);
|
|
|
|
READWRITE(nHeight);
|
|
|
|
READWRITE(out);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-26 14:17:33 +01:00
|
|
|
class RestErr
|
|
|
|
{
|
2014-11-11 10:52:43 +01:00
|
|
|
public:
|
|
|
|
enum HTTPStatusCode status;
|
|
|
|
string message;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry);
|
2015-05-13 21:29:19 +02:00
|
|
|
extern UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false);
|
2014-12-01 12:38:42 +01:00
|
|
|
extern void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out, bool fIncludeHex);
|
2014-11-11 10:52:43 +01:00
|
|
|
|
|
|
|
static RestErr RESTERR(enum HTTPStatusCode status, string message)
|
|
|
|
{
|
|
|
|
RestErr re;
|
|
|
|
re.status = status;
|
|
|
|
re.message = message;
|
|
|
|
return re;
|
|
|
|
}
|
|
|
|
|
2014-11-26 14:17:33 +01:00
|
|
|
static enum RetFormat ParseDataFormat(vector<string>& params, const string strReq)
|
2014-11-11 10:52:43 +01:00
|
|
|
{
|
2014-11-26 14:17:33 +01:00
|
|
|
boost::split(params, strReq, boost::is_any_of("."));
|
|
|
|
if (params.size() > 1) {
|
|
|
|
for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++)
|
|
|
|
if (params[1] == rf_names[i].name)
|
|
|
|
return rf_names[i].rf;
|
|
|
|
}
|
2014-11-11 10:52:43 +01:00
|
|
|
|
|
|
|
return rf_names[0].rf;
|
|
|
|
}
|
|
|
|
|
2014-11-26 14:17:33 +01:00
|
|
|
static string AvailableDataFormatsString()
|
|
|
|
{
|
|
|
|
string formats = "";
|
|
|
|
for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++)
|
|
|
|
if (strlen(rf_names[i].name) > 0) {
|
|
|
|
formats.append(".");
|
|
|
|
formats.append(rf_names[i].name);
|
|
|
|
formats.append(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (formats.length() > 0)
|
|
|
|
return formats.substr(0, formats.length() - 2);
|
|
|
|
|
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
2014-11-18 16:27:45 +01:00
|
|
|
static bool ParseHashStr(const string& strReq, uint256& v)
|
2014-11-11 10:52:43 +01:00
|
|
|
{
|
|
|
|
if (!IsHex(strReq) || (strReq.size() != 64))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
v.SetHex(strReq);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-08 13:44:49 +01:00
|
|
|
static bool rest_headers(AcceptedConnection* conn,
|
2014-12-01 12:38:42 +01:00
|
|
|
const std::string& strURIPart,
|
|
|
|
const std::string& strRequest,
|
2014-12-16 13:45:27 +01:00
|
|
|
const std::map<std::string, std::string>& mapHeaders,
|
2014-12-08 13:44:49 +01:00
|
|
|
bool fRun)
|
|
|
|
{
|
|
|
|
vector<string> params;
|
2014-12-01 12:38:42 +01:00
|
|
|
const RetFormat rf = ParseDataFormat(params, strURIPart);
|
2014-12-08 13:44:49 +01:00
|
|
|
vector<string> path;
|
|
|
|
boost::split(path, params[0], boost::is_any_of("/"));
|
|
|
|
|
|
|
|
if (path.size() != 2)
|
|
|
|
throw RESTERR(HTTP_BAD_REQUEST, "No header count specified. Use /rest/headers/<count>/<hash>.<ext>.");
|
|
|
|
|
|
|
|
long count = strtol(path[0].c_str(), NULL, 10);
|
|
|
|
if (count < 1 || count > 2000)
|
|
|
|
throw RESTERR(HTTP_BAD_REQUEST, "Header count out of range: " + path[0]);
|
|
|
|
|
|
|
|
string hashStr = path[1];
|
|
|
|
uint256 hash;
|
|
|
|
if (!ParseHashStr(hashStr, hash))
|
|
|
|
throw RESTERR(HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
|
|
|
|
|
|
|
|
std::vector<CBlockHeader> headers;
|
|
|
|
headers.reserve(count);
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
|
|
|
BlockMap::const_iterator it = mapBlockIndex.find(hash);
|
|
|
|
const CBlockIndex *pindex = (it != mapBlockIndex.end()) ? it->second : NULL;
|
|
|
|
while (pindex != NULL && chainActive.Contains(pindex)) {
|
|
|
|
headers.push_back(pindex->GetBlockHeader());
|
|
|
|
if (headers.size() == (unsigned long)count)
|
|
|
|
break;
|
|
|
|
pindex = chainActive.Next(pindex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
BOOST_FOREACH(const CBlockHeader &header, headers) {
|
|
|
|
ssHeader << header;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (rf) {
|
|
|
|
case RF_BINARY: {
|
|
|
|
string binaryHeader = ssHeader.str();
|
|
|
|
conn->stream() << HTTPReplyHeader(HTTP_OK, fRun, binaryHeader.size(), "application/octet-stream") << binaryHeader << std::flush;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RF_HEX: {
|
|
|
|
string strHex = HexStr(ssHeader.begin(), ssHeader.end()) + "\n";
|
|
|
|
conn->stream() << HTTPReply(HTTP_OK, strHex, fRun, false, "text/plain") << std::flush;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
throw RESTERR(HTTP_NOT_FOUND, "output format not found (available: .bin, .hex)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// not reached
|
|
|
|
return true; // continue to process further HTTP reqs on this cxn
|
|
|
|
}
|
|
|
|
|
2014-11-26 14:17:33 +01:00
|
|
|
static bool rest_block(AcceptedConnection* conn,
|
2014-12-01 12:38:42 +01:00
|
|
|
const std::string& strURIPart,
|
|
|
|
const std::string& strRequest,
|
2014-12-16 13:45:27 +01:00
|
|
|
const std::map<std::string, std::string>& mapHeaders,
|
2014-11-28 20:32:52 +01:00
|
|
|
bool fRun,
|
|
|
|
bool showTxDetails)
|
2014-11-11 10:52:43 +01:00
|
|
|
{
|
|
|
|
vector<string> params;
|
2014-12-01 12:38:42 +01:00
|
|
|
const RetFormat rf = ParseDataFormat(params, strURIPart);
|
2014-11-11 10:52:43 +01:00
|
|
|
|
|
|
|
string hashStr = params[0];
|
|
|
|
uint256 hash;
|
|
|
|
if (!ParseHashStr(hashStr, hash))
|
|
|
|
throw RESTERR(HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
|
|
|
|
|
|
|
|
CBlock block;
|
2014-11-18 16:30:51 +01:00
|
|
|
CBlockIndex* pblockindex = NULL;
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
|
|
|
if (mapBlockIndex.count(hash) == 0)
|
|
|
|
throw RESTERR(HTTP_NOT_FOUND, hashStr + " not found");
|
|
|
|
|
|
|
|
pblockindex = mapBlockIndex[hash];
|
2015-04-27 05:18:24 +02:00
|
|
|
if (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0)
|
|
|
|
throw RESTERR(HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
|
|
|
|
|
2014-11-18 16:30:51 +01:00
|
|
|
if (!ReadBlockFromDisk(block, pblockindex))
|
|
|
|
throw RESTERR(HTTP_NOT_FOUND, hashStr + " not found");
|
|
|
|
}
|
2014-11-11 10:52:43 +01:00
|
|
|
|
|
|
|
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
ssBlock << block;
|
|
|
|
|
|
|
|
switch (rf) {
|
|
|
|
case RF_BINARY: {
|
|
|
|
string binaryBlock = ssBlock.str();
|
2014-11-20 14:04:16 +01:00
|
|
|
conn->stream() << HTTPReplyHeader(HTTP_OK, fRun, binaryBlock.size(), "application/octet-stream") << binaryBlock << std::flush;
|
2014-11-11 10:52:43 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RF_HEX: {
|
2014-11-26 14:17:33 +01:00
|
|
|
string strHex = HexStr(ssBlock.begin(), ssBlock.end()) + "\n";
|
2014-11-11 10:52:43 +01:00
|
|
|
conn->stream() << HTTPReply(HTTP_OK, strHex, fRun, false, "text/plain") << std::flush;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RF_JSON: {
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue objBlock = blockToJSON(block, pblockindex, showTxDetails);
|
2015-05-10 13:35:44 +02:00
|
|
|
string strJSON = objBlock.write() + "\n";
|
2014-11-11 10:52:43 +01:00
|
|
|
conn->stream() << HTTPReply(HTTP_OK, strJSON, fRun) << std::flush;
|
|
|
|
return true;
|
2014-11-26 14:17:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
throw RESTERR(HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
|
|
|
|
}
|
2014-11-11 10:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// not reached
|
2014-11-26 14:17:33 +01:00
|
|
|
return true; // continue to process further HTTP reqs on this cxn
|
2014-11-11 10:52:43 +01:00
|
|
|
}
|
|
|
|
|
2014-11-28 20:32:52 +01:00
|
|
|
static bool rest_block_extended(AcceptedConnection* conn,
|
2014-12-01 12:38:42 +01:00
|
|
|
const std::string& strURIPart,
|
|
|
|
const std::string& strRequest,
|
2014-12-16 13:45:27 +01:00
|
|
|
const std::map<std::string, std::string>& mapHeaders,
|
2014-11-28 20:32:52 +01:00
|
|
|
bool fRun)
|
|
|
|
{
|
2014-12-01 12:38:42 +01:00
|
|
|
return rest_block(conn, strURIPart, strRequest, mapHeaders, fRun, true);
|
2014-11-28 20:32:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool rest_block_notxdetails(AcceptedConnection* conn,
|
2014-12-01 12:38:42 +01:00
|
|
|
const std::string& strURIPart,
|
|
|
|
const std::string& strRequest,
|
2014-12-16 13:45:27 +01:00
|
|
|
const std::map<std::string, std::string>& mapHeaders,
|
2014-11-28 20:32:52 +01:00
|
|
|
bool fRun)
|
|
|
|
{
|
2014-12-01 12:38:42 +01:00
|
|
|
return rest_block(conn, strURIPart, strRequest, mapHeaders, fRun, false);
|
2014-11-28 20:32:52 +01:00
|
|
|
}
|
|
|
|
|
2014-12-27 13:18:36 +01:00
|
|
|
static bool rest_chaininfo(AcceptedConnection* conn,
|
2014-12-01 12:38:42 +01:00
|
|
|
const std::string& strURIPart,
|
|
|
|
const std::string& strRequest,
|
|
|
|
const std::map<std::string, std::string>& mapHeaders,
|
|
|
|
bool fRun)
|
2014-12-27 13:18:36 +01:00
|
|
|
{
|
|
|
|
vector<string> params;
|
2014-12-01 12:38:42 +01:00
|
|
|
const RetFormat rf = ParseDataFormat(params, strURIPart);
|
2015-05-27 09:41:14 +02:00
|
|
|
|
2014-12-27 13:18:36 +01:00
|
|
|
switch (rf) {
|
|
|
|
case RF_JSON: {
|
2015-05-10 14:48:35 +02:00
|
|
|
UniValue rpcParams(UniValue::VARR);
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue chainInfoObject = getblockchaininfo(rpcParams, false);
|
2015-05-10 13:35:44 +02:00
|
|
|
string strJSON = chainInfoObject.write() + "\n";
|
2014-12-27 13:18:36 +01:00
|
|
|
conn->stream() << HTTPReply(HTTP_OK, strJSON, fRun) << std::flush;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw RESTERR(HTTP_NOT_FOUND, "output format not found (available: json)");
|
|
|
|
}
|
|
|
|
}
|
2015-05-27 09:41:14 +02:00
|
|
|
|
2014-12-27 13:18:36 +01:00
|
|
|
// not reached
|
|
|
|
return true; // continue to process further HTTP reqs on this cxn
|
|
|
|
}
|
|
|
|
|
2014-11-26 14:17:33 +01:00
|
|
|
static bool rest_tx(AcceptedConnection* conn,
|
2014-12-01 12:38:42 +01:00
|
|
|
const std::string& strURIPart,
|
|
|
|
const std::string& strRequest,
|
2014-12-16 13:45:27 +01:00
|
|
|
const std::map<std::string, std::string>& mapHeaders,
|
2014-11-11 10:52:43 +01:00
|
|
|
bool fRun)
|
|
|
|
{
|
|
|
|
vector<string> params;
|
2014-12-01 12:38:42 +01:00
|
|
|
const RetFormat rf = ParseDataFormat(params, strURIPart);
|
2014-11-11 10:52:43 +01:00
|
|
|
|
|
|
|
string hashStr = params[0];
|
|
|
|
uint256 hash;
|
|
|
|
if (!ParseHashStr(hashStr, hash))
|
|
|
|
throw RESTERR(HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
|
|
|
|
|
|
|
|
CTransaction tx;
|
2014-12-15 09:11:16 +01:00
|
|
|
uint256 hashBlock = uint256();
|
2014-11-11 10:52:43 +01:00
|
|
|
if (!GetTransaction(hash, tx, hashBlock, true))
|
|
|
|
throw RESTERR(HTTP_NOT_FOUND, hashStr + " not found");
|
|
|
|
|
|
|
|
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
ssTx << tx;
|
|
|
|
|
|
|
|
switch (rf) {
|
|
|
|
case RF_BINARY: {
|
|
|
|
string binaryTx = ssTx.str();
|
2014-11-20 14:04:16 +01:00
|
|
|
conn->stream() << HTTPReplyHeader(HTTP_OK, fRun, binaryTx.size(), "application/octet-stream") << binaryTx << std::flush;
|
2014-11-11 10:52:43 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RF_HEX: {
|
2014-11-26 14:17:33 +01:00
|
|
|
string strHex = HexStr(ssTx.begin(), ssTx.end()) + "\n";
|
2014-11-11 10:52:43 +01:00
|
|
|
conn->stream() << HTTPReply(HTTP_OK, strHex, fRun, false, "text/plain") << std::flush;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RF_JSON: {
|
2015-05-10 14:48:35 +02:00
|
|
|
UniValue objTx(UniValue::VOBJ);
|
2014-11-11 10:52:43 +01:00
|
|
|
TxToJSON(tx, hashBlock, objTx);
|
2015-05-10 13:35:44 +02:00
|
|
|
string strJSON = objTx.write() + "\n";
|
2014-11-11 10:52:43 +01:00
|
|
|
conn->stream() << HTTPReply(HTTP_OK, strJSON, fRun) << std::flush;
|
|
|
|
return true;
|
2014-11-19 10:53:46 +01:00
|
|
|
}
|
2014-11-26 14:17:33 +01:00
|
|
|
|
|
|
|
default: {
|
|
|
|
throw RESTERR(HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
|
|
|
|
}
|
2014-11-11 10:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// not reached
|
2014-11-26 14:17:33 +01:00
|
|
|
return true; // continue to process further HTTP reqs on this cxn
|
2014-11-11 10:52:43 +01:00
|
|
|
}
|
|
|
|
|
2014-12-01 12:38:42 +01:00
|
|
|
static bool rest_getutxos(AcceptedConnection* conn,
|
|
|
|
const std::string& strURIPart,
|
|
|
|
const std::string& strRequest,
|
|
|
|
const std::map<std::string, std::string>& mapHeaders,
|
|
|
|
bool fRun)
|
|
|
|
{
|
|
|
|
vector<string> params;
|
|
|
|
enum RetFormat rf = ParseDataFormat(params, strURIPart);
|
|
|
|
|
2015-05-27 15:56:16 +02:00
|
|
|
vector<string> uriParts;
|
|
|
|
if (params.size() > 0 && params[0].length() > 1)
|
|
|
|
{
|
|
|
|
std::string strUriParams = params[0].substr(1);
|
|
|
|
boost::split(uriParts, strUriParams, boost::is_any_of("/"));
|
|
|
|
}
|
|
|
|
|
2014-12-01 12:38:42 +01:00
|
|
|
// throw exception in case of a empty request
|
2015-05-27 15:56:16 +02:00
|
|
|
if (strRequest.length() == 0 && uriParts.size() == 0)
|
2014-12-01 12:38:42 +01:00
|
|
|
throw RESTERR(HTTP_INTERNAL_SERVER_ERROR, "Error: empty request");
|
|
|
|
|
2015-05-27 15:56:16 +02:00
|
|
|
bool fInputParsed = false;
|
2014-12-01 12:38:42 +01:00
|
|
|
bool fCheckMemPool = false;
|
|
|
|
vector<COutPoint> vOutPoints;
|
|
|
|
|
|
|
|
// parse/deserialize input
|
|
|
|
// input-format = output-format, rest/getutxos/bin requires binary input, gives binary output, ...
|
2015-05-27 09:41:14 +02:00
|
|
|
|
2015-05-27 15:56:16 +02:00
|
|
|
if (uriParts.size() > 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
//inputs is sent over URI scheme (/rest/getutxos/checkmempool/txid1-n/txid2-n/...)
|
|
|
|
if (uriParts.size() > 0 && uriParts[0] == "checkmempool")
|
|
|
|
fCheckMemPool = true;
|
|
|
|
|
|
|
|
for (size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
|
|
|
|
{
|
|
|
|
uint256 txid;
|
|
|
|
int32_t nOutput;
|
|
|
|
std::string strTxid = uriParts[i].substr(0, uriParts[i].find("-"));
|
|
|
|
std::string strOutput = uriParts[i].substr(uriParts[i].find("-")+1);
|
|
|
|
|
|
|
|
if (!ParseInt32(strOutput, &nOutput) || !IsHex(strTxid))
|
|
|
|
throw RESTERR(HTTP_INTERNAL_SERVER_ERROR, "Parse error");
|
|
|
|
|
|
|
|
txid.SetHex(strTxid);
|
|
|
|
vOutPoints.push_back(COutPoint(txid, (uint32_t)nOutput));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vOutPoints.size() > 0)
|
|
|
|
fInputParsed = true;
|
|
|
|
else
|
|
|
|
throw RESTERR(HTTP_INTERNAL_SERVER_ERROR, "Error: empty request");
|
|
|
|
}
|
|
|
|
|
2014-12-01 12:38:42 +01:00
|
|
|
string strRequestMutable = strRequest; //convert const string to string for allowing hex to bin converting
|
2015-05-27 09:41:14 +02:00
|
|
|
|
2014-12-01 12:38:42 +01:00
|
|
|
switch (rf) {
|
|
|
|
case RF_HEX: {
|
|
|
|
// convert hex to bin, continue then with bin part
|
|
|
|
std::vector<unsigned char> strRequestV = ParseHex(strRequest);
|
|
|
|
strRequestMutable.assign(strRequestV.begin(), strRequestV.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
case RF_BINARY: {
|
|
|
|
try {
|
2015-05-27 15:56:16 +02:00
|
|
|
//deserialize only if user sent a request
|
|
|
|
if (strRequestMutable.size() > 0)
|
|
|
|
{
|
|
|
|
if (fInputParsed) //don't allow sending input over URI and HTTP RAW DATA
|
|
|
|
throw RESTERR(HTTP_INTERNAL_SERVER_ERROR, "Combination of URI scheme inputs and raw post data is not allowed");
|
|
|
|
|
|
|
|
CDataStream oss(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
oss << strRequestMutable;
|
|
|
|
oss >> fCheckMemPool;
|
|
|
|
oss >> vOutPoints;
|
|
|
|
}
|
2014-12-01 12:38:42 +01:00
|
|
|
} catch (const std::ios_base::failure& e) {
|
|
|
|
// abort in case of unreadable binary data
|
|
|
|
throw RESTERR(HTTP_INTERNAL_SERVER_ERROR, "Parse error");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RF_JSON: {
|
2015-05-27 15:56:16 +02:00
|
|
|
if (!fInputParsed)
|
|
|
|
throw RESTERR(HTTP_INTERNAL_SERVER_ERROR, "Error: empty request");
|
2014-12-01 12:38:42 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw RESTERR(HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// limit max outpoints
|
|
|
|
if (vOutPoints.size() > MAX_GETUTXOS_OUTPOINTS)
|
|
|
|
throw RESTERR(HTTP_INTERNAL_SERVER_ERROR, strprintf("Error: max outpoints exceeded (max: %d, tried: %d)", MAX_GETUTXOS_OUTPOINTS, vOutPoints.size()));
|
|
|
|
|
|
|
|
// check spentness and form a bitmap (as well as a JSON capable human-readble string representation)
|
|
|
|
vector<unsigned char> bitmap;
|
|
|
|
vector<CCoin> outs;
|
|
|
|
std::string bitmapStringRepresentation;
|
|
|
|
boost::dynamic_bitset<unsigned char> hits(vOutPoints.size());
|
|
|
|
{
|
|
|
|
LOCK2(cs_main, mempool.cs);
|
|
|
|
|
|
|
|
CCoinsView viewDummy;
|
|
|
|
CCoinsViewCache view(&viewDummy);
|
|
|
|
|
|
|
|
CCoinsViewCache& viewChain = *pcoinsTip;
|
|
|
|
CCoinsViewMemPool viewMempool(&viewChain, mempool);
|
|
|
|
|
|
|
|
if (fCheckMemPool)
|
|
|
|
view.SetBackend(viewMempool); // switch cache backend to db+mempool in case user likes to query mempool
|
|
|
|
|
|
|
|
for (size_t i = 0; i < vOutPoints.size(); i++) {
|
|
|
|
CCoins coins;
|
|
|
|
uint256 hash = vOutPoints[i].hash;
|
|
|
|
if (view.GetCoins(hash, coins)) {
|
|
|
|
mempool.pruneSpent(hash, coins);
|
|
|
|
if (coins.IsAvailable(vOutPoints[i].n)) {
|
|
|
|
hits[i] = true;
|
|
|
|
// Safe to index into vout here because IsAvailable checked if it's off the end of the array, or if
|
|
|
|
// n is valid but points to an already spent output (IsNull).
|
|
|
|
CCoin coin;
|
|
|
|
coin.nTxVer = coins.nVersion;
|
|
|
|
coin.nHeight = coins.nHeight;
|
|
|
|
coin.out = coins.vout.at(vOutPoints[i].n);
|
|
|
|
assert(!coin.out.IsNull());
|
|
|
|
outs.push_back(coin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bitmapStringRepresentation.append(hits[i] ? "1" : "0"); // form a binary string representation (human-readable for json output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
boost::to_block_range(hits, std::back_inserter(bitmap));
|
|
|
|
|
|
|
|
switch (rf) {
|
|
|
|
case RF_BINARY: {
|
|
|
|
// serialize data
|
|
|
|
// use exact same output as mentioned in Bip64
|
|
|
|
CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
ssGetUTXOResponse << chainActive.Height() << chainActive.Tip()->GetBlockHash() << bitmap << outs;
|
|
|
|
string ssGetUTXOResponseString = ssGetUTXOResponse.str();
|
|
|
|
|
|
|
|
conn->stream() << HTTPReplyHeader(HTTP_OK, fRun, ssGetUTXOResponseString.size(), "application/octet-stream") << ssGetUTXOResponseString << std::flush;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RF_HEX: {
|
|
|
|
CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
ssGetUTXOResponse << chainActive.Height() << chainActive.Tip()->GetBlockHash() << bitmap << outs;
|
|
|
|
string strHex = HexStr(ssGetUTXOResponse.begin(), ssGetUTXOResponse.end()) + "\n";
|
|
|
|
|
|
|
|
conn->stream() << HTTPReply(HTTP_OK, strHex, fRun, false, "text/plain") << std::flush;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RF_JSON: {
|
2015-05-10 14:48:35 +02:00
|
|
|
UniValue objGetUTXOResponse(UniValue::VOBJ);
|
2014-12-01 12:38:42 +01:00
|
|
|
|
|
|
|
// pack in some essentials
|
|
|
|
// use more or less the same output as mentioned in Bip64
|
|
|
|
objGetUTXOResponse.push_back(Pair("chainHeight", chainActive.Height()));
|
|
|
|
objGetUTXOResponse.push_back(Pair("chaintipHash", chainActive.Tip()->GetBlockHash().GetHex()));
|
|
|
|
objGetUTXOResponse.push_back(Pair("bitmap", bitmapStringRepresentation));
|
|
|
|
|
2015-05-10 14:48:35 +02:00
|
|
|
UniValue utxos(UniValue::VARR);
|
2014-12-01 12:38:42 +01:00
|
|
|
BOOST_FOREACH (const CCoin& coin, outs) {
|
2015-05-10 14:48:35 +02:00
|
|
|
UniValue utxo(UniValue::VOBJ);
|
2014-12-01 12:38:42 +01:00
|
|
|
utxo.push_back(Pair("txvers", (int32_t)coin.nTxVer));
|
|
|
|
utxo.push_back(Pair("height", (int32_t)coin.nHeight));
|
|
|
|
utxo.push_back(Pair("value", ValueFromAmount(coin.out.nValue)));
|
|
|
|
|
|
|
|
// include the script in a json output
|
2015-05-10 14:48:35 +02:00
|
|
|
UniValue o(UniValue::VOBJ);
|
2014-12-01 12:38:42 +01:00
|
|
|
ScriptPubKeyToJSON(coin.out.scriptPubKey, o, true);
|
|
|
|
utxo.push_back(Pair("scriptPubKey", o));
|
|
|
|
utxos.push_back(utxo);
|
|
|
|
}
|
|
|
|
objGetUTXOResponse.push_back(Pair("utxos", utxos));
|
|
|
|
|
|
|
|
// return json string
|
2015-05-10 13:35:44 +02:00
|
|
|
string strJSON = objGetUTXOResponse.write() + "\n";
|
2014-12-01 12:38:42 +01:00
|
|
|
conn->stream() << HTTPReply(HTTP_OK, strJSON, fRun) << std::flush;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw RESTERR(HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// not reached
|
|
|
|
return true; // continue to process further HTTP reqs on this cxn
|
|
|
|
}
|
|
|
|
|
2014-11-11 10:52:43 +01:00
|
|
|
static const struct {
|
2014-11-26 14:17:33 +01:00
|
|
|
const char* prefix;
|
|
|
|
bool (*handler)(AcceptedConnection* conn,
|
2014-12-01 12:38:42 +01:00
|
|
|
const std::string& strURIPart,
|
|
|
|
const std::string& strRequest,
|
2014-12-16 13:45:27 +01:00
|
|
|
const std::map<std::string, std::string>& mapHeaders,
|
2014-11-11 10:52:43 +01:00
|
|
|
bool fRun);
|
|
|
|
} uri_prefixes[] = {
|
2014-11-26 14:17:33 +01:00
|
|
|
{"/rest/tx/", rest_tx},
|
2014-11-28 20:32:52 +01:00
|
|
|
{"/rest/block/notxdetails/", rest_block_notxdetails},
|
|
|
|
{"/rest/block/", rest_block_extended},
|
2014-12-27 13:18:36 +01:00
|
|
|
{"/rest/chaininfo", rest_chaininfo},
|
2014-12-08 13:44:49 +01:00
|
|
|
{"/rest/headers/", rest_headers},
|
2014-12-01 12:38:42 +01:00
|
|
|
{"/rest/getutxos", rest_getutxos},
|
2014-11-11 10:52:43 +01:00
|
|
|
};
|
|
|
|
|
2014-11-26 14:17:33 +01:00
|
|
|
bool HTTPReq_REST(AcceptedConnection* conn,
|
2014-12-16 13:45:27 +01:00
|
|
|
const std::string& strURI,
|
2014-12-01 12:38:42 +01:00
|
|
|
const string& strRequest,
|
2014-12-16 13:45:27 +01:00
|
|
|
const std::map<std::string, std::string>& mapHeaders,
|
2014-11-11 10:52:43 +01:00
|
|
|
bool fRun)
|
|
|
|
{
|
|
|
|
try {
|
2014-11-26 13:51:02 +01:00
|
|
|
std::string statusmessage;
|
2014-11-26 14:17:33 +01:00
|
|
|
if (RPCIsInWarmup(&statusmessage))
|
|
|
|
throw RESTERR(HTTP_SERVICE_UNAVAILABLE, "Service temporarily unavailable: " + statusmessage);
|
|
|
|
|
2014-11-11 10:52:43 +01:00
|
|
|
for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++) {
|
|
|
|
unsigned int plen = strlen(uri_prefixes[i].prefix);
|
|
|
|
if (strURI.substr(0, plen) == uri_prefixes[i].prefix) {
|
2014-12-01 12:38:42 +01:00
|
|
|
string strURIPart = strURI.substr(plen);
|
|
|
|
return uri_prefixes[i].handler(conn, strURIPart, strRequest, mapHeaders, fRun);
|
2014-11-11 10:52:43 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-16 13:45:27 +01:00
|
|
|
} catch (const RestErr& re) {
|
2014-11-11 10:52:43 +01:00
|
|
|
conn->stream() << HTTPReply(re.status, re.message + "\r\n", false, false, "text/plain") << std::flush;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-18 16:27:45 +01:00
|
|
|
conn->stream() << HTTPError(HTTP_NOT_FOUND, false) << std::flush;
|
2014-11-11 10:52:43 +01:00
|
|
|
return false;
|
|
|
|
}
|