scripted-diff: rename RetFormat to RESTResponseFormat

As RetFormat is now exposed in a header, it is renamed to the more
understandable RESTResponseFormat
-BEGIN VERIFY SCRIPT-
s() { sed -i 's/RetFormat/RESTResponseFormat/g' $1; }
s src/rest.cpp
s src/rest.h
-END VERIFY SCRIPT-
This commit is contained in:
stickies-v 2022-01-27 21:31:35 +00:00
parent 9f1c54787c
commit c1aad1b3b9
No known key found for this signature in database
GPG key ID: 5CB1CE6E5E66A757
2 changed files with 45 additions and 45 deletions

View file

@ -44,13 +44,13 @@ static const size_t MAX_GETUTXOS_OUTPOINTS = 15; //allow a max of 15 outpoints t
static constexpr unsigned int MAX_REST_HEADERS_RESULTS = 2000; static constexpr unsigned int MAX_REST_HEADERS_RESULTS = 2000;
static const struct { static const struct {
RetFormat rf; RESTResponseFormat rf;
const char* name; const char* name;
} rf_names[] = { } rf_names[] = {
{RetFormat::UNDEF, ""}, {RESTResponseFormat::UNDEF, ""},
{RetFormat::BINARY, "bin"}, {RESTResponseFormat::BINARY, "bin"},
{RetFormat::HEX, "hex"}, {RESTResponseFormat::HEX, "hex"},
{RetFormat::JSON, "json"}, {RESTResponseFormat::JSON, "json"},
}; };
struct CCoin { struct CCoin {
@ -133,7 +133,7 @@ static ChainstateManager* GetChainman(const std::any& context, HTTPRequest* req)
return node_context->chainman.get(); return node_context->chainman.get();
} }
RetFormat ParseDataFormat(std::string& param, const std::string& strReq) RESTResponseFormat ParseDataFormat(std::string& param, const std::string& strReq)
{ {
const std::string::size_type pos = strReq.rfind('.'); const std::string::size_type pos = strReq.rfind('.');
if (pos == std::string::npos) if (pos == std::string::npos)
@ -187,7 +187,7 @@ static bool rest_headers(const std::any& context,
if (!CheckWarmup(req)) if (!CheckWarmup(req))
return false; return false;
std::string param; std::string param;
const RetFormat rf = ParseDataFormat(param, strURIPart); const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
std::vector<std::string> path; std::vector<std::string> path;
boost::split(path, param, boost::is_any_of("/")); boost::split(path, param, boost::is_any_of("/"));
@ -225,7 +225,7 @@ static bool rest_headers(const std::any& context,
} }
switch (rf) { switch (rf) {
case RetFormat::BINARY: { case RESTResponseFormat::BINARY: {
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
for (const CBlockIndex *pindex : headers) { for (const CBlockIndex *pindex : headers) {
ssHeader << pindex->GetBlockHeader(); ssHeader << pindex->GetBlockHeader();
@ -237,7 +237,7 @@ static bool rest_headers(const std::any& context,
return true; return true;
} }
case RetFormat::HEX: { case RESTResponseFormat::HEX: {
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
for (const CBlockIndex *pindex : headers) { for (const CBlockIndex *pindex : headers) {
ssHeader << pindex->GetBlockHeader(); ssHeader << pindex->GetBlockHeader();
@ -248,7 +248,7 @@ static bool rest_headers(const std::any& context,
req->WriteReply(HTTP_OK, strHex); req->WriteReply(HTTP_OK, strHex);
return true; return true;
} }
case RetFormat::JSON: { case RESTResponseFormat::JSON: {
UniValue jsonHeaders(UniValue::VARR); UniValue jsonHeaders(UniValue::VARR);
for (const CBlockIndex *pindex : headers) { for (const CBlockIndex *pindex : headers) {
jsonHeaders.push_back(blockheaderToJSON(tip, pindex)); jsonHeaders.push_back(blockheaderToJSON(tip, pindex));
@ -272,7 +272,7 @@ static bool rest_block(const std::any& context,
if (!CheckWarmup(req)) if (!CheckWarmup(req))
return false; return false;
std::string hashStr; std::string hashStr;
const RetFormat rf = ParseDataFormat(hashStr, strURIPart); const RESTResponseFormat rf = ParseDataFormat(hashStr, strURIPart);
uint256 hash; uint256 hash;
if (!ParseHashStr(hashStr, hash)) if (!ParseHashStr(hashStr, hash))
@ -300,7 +300,7 @@ static bool rest_block(const std::any& context,
} }
switch (rf) { switch (rf) {
case RetFormat::BINARY: { case RESTResponseFormat::BINARY: {
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
ssBlock << block; ssBlock << block;
std::string binaryBlock = ssBlock.str(); std::string binaryBlock = ssBlock.str();
@ -309,7 +309,7 @@ static bool rest_block(const std::any& context,
return true; return true;
} }
case RetFormat::HEX: { case RESTResponseFormat::HEX: {
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
ssBlock << block; ssBlock << block;
std::string strHex = HexStr(ssBlock) + "\n"; std::string strHex = HexStr(ssBlock) + "\n";
@ -318,7 +318,7 @@ static bool rest_block(const std::any& context,
return true; return true;
} }
case RetFormat::JSON: { case RESTResponseFormat::JSON: {
UniValue objBlock = blockToJSON(block, tip, pblockindex, tx_verbosity); UniValue objBlock = blockToJSON(block, tip, pblockindex, tx_verbosity);
std::string strJSON = objBlock.write() + "\n"; std::string strJSON = objBlock.write() + "\n";
req->WriteHeader("Content-Type", "application/json"); req->WriteHeader("Content-Type", "application/json");
@ -347,7 +347,7 @@ static bool rest_filter_header(const std::any& context, HTTPRequest* req, const
if (!CheckWarmup(req)) return false; if (!CheckWarmup(req)) return false;
std::string param; std::string param;
const RetFormat rf = ParseDataFormat(param, strURIPart); const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
std::vector<std::string> uri_parts; std::vector<std::string> uri_parts;
boost::split(uri_parts, param, boost::is_any_of("/")); boost::split(uri_parts, param, boost::is_any_of("/"));
@ -413,7 +413,7 @@ static bool rest_filter_header(const std::any& context, HTTPRequest* req, const
} }
switch (rf) { switch (rf) {
case RetFormat::BINARY: { case RESTResponseFormat::BINARY: {
CDataStream ssHeader{SER_NETWORK, PROTOCOL_VERSION}; CDataStream ssHeader{SER_NETWORK, PROTOCOL_VERSION};
for (const uint256& header : filter_headers) { for (const uint256& header : filter_headers) {
ssHeader << header; ssHeader << header;
@ -424,7 +424,7 @@ static bool rest_filter_header(const std::any& context, HTTPRequest* req, const
req->WriteReply(HTTP_OK, binaryHeader); req->WriteReply(HTTP_OK, binaryHeader);
return true; return true;
} }
case RetFormat::HEX: { case RESTResponseFormat::HEX: {
CDataStream ssHeader{SER_NETWORK, PROTOCOL_VERSION}; CDataStream ssHeader{SER_NETWORK, PROTOCOL_VERSION};
for (const uint256& header : filter_headers) { for (const uint256& header : filter_headers) {
ssHeader << header; ssHeader << header;
@ -435,7 +435,7 @@ static bool rest_filter_header(const std::any& context, HTTPRequest* req, const
req->WriteReply(HTTP_OK, strHex); req->WriteReply(HTTP_OK, strHex);
return true; return true;
} }
case RetFormat::JSON: { case RESTResponseFormat::JSON: {
UniValue jsonHeaders(UniValue::VARR); UniValue jsonHeaders(UniValue::VARR);
for (const uint256& header : filter_headers) { for (const uint256& header : filter_headers) {
jsonHeaders.push_back(header.GetHex()); jsonHeaders.push_back(header.GetHex());
@ -457,7 +457,7 @@ static bool rest_block_filter(const std::any& context, HTTPRequest* req, const s
if (!CheckWarmup(req)) return false; if (!CheckWarmup(req)) return false;
std::string param; std::string param;
const RetFormat rf = ParseDataFormat(param, strURIPart); const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
// request is sent over URI scheme /rest/blockfilter/filtertype/blockhash // request is sent over URI scheme /rest/blockfilter/filtertype/blockhash
std::vector<std::string> uri_parts; std::vector<std::string> uri_parts;
@ -513,7 +513,7 @@ static bool rest_block_filter(const std::any& context, HTTPRequest* req, const s
} }
switch (rf) { switch (rf) {
case RetFormat::BINARY: { case RESTResponseFormat::BINARY: {
CDataStream ssResp{SER_NETWORK, PROTOCOL_VERSION}; CDataStream ssResp{SER_NETWORK, PROTOCOL_VERSION};
ssResp << filter; ssResp << filter;
@ -522,7 +522,7 @@ static bool rest_block_filter(const std::any& context, HTTPRequest* req, const s
req->WriteReply(HTTP_OK, binaryResp); req->WriteReply(HTTP_OK, binaryResp);
return true; return true;
} }
case RetFormat::HEX: { case RESTResponseFormat::HEX: {
CDataStream ssResp{SER_NETWORK, PROTOCOL_VERSION}; CDataStream ssResp{SER_NETWORK, PROTOCOL_VERSION};
ssResp << filter; ssResp << filter;
@ -531,7 +531,7 @@ static bool rest_block_filter(const std::any& context, HTTPRequest* req, const s
req->WriteReply(HTTP_OK, strHex); req->WriteReply(HTTP_OK, strHex);
return true; return true;
} }
case RetFormat::JSON: { case RESTResponseFormat::JSON: {
UniValue ret(UniValue::VOBJ); UniValue ret(UniValue::VOBJ);
ret.pushKV("filter", HexStr(filter.GetEncodedFilter())); ret.pushKV("filter", HexStr(filter.GetEncodedFilter()));
std::string strJSON = ret.write() + "\n"; std::string strJSON = ret.write() + "\n";
@ -553,10 +553,10 @@ static bool rest_chaininfo(const std::any& context, HTTPRequest* req, const std:
if (!CheckWarmup(req)) if (!CheckWarmup(req))
return false; return false;
std::string param; std::string param;
const RetFormat rf = ParseDataFormat(param, strURIPart); const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
switch (rf) { switch (rf) {
case RetFormat::JSON: { case RESTResponseFormat::JSON: {
JSONRPCRequest jsonRequest; JSONRPCRequest jsonRequest;
jsonRequest.context = context; jsonRequest.context = context;
jsonRequest.params = UniValue(UniValue::VARR); jsonRequest.params = UniValue(UniValue::VARR);
@ -579,10 +579,10 @@ static bool rest_mempool_info(const std::any& context, HTTPRequest* req, const s
const CTxMemPool* mempool = GetMemPool(context, req); const CTxMemPool* mempool = GetMemPool(context, req);
if (!mempool) return false; if (!mempool) return false;
std::string param; std::string param;
const RetFormat rf = ParseDataFormat(param, strURIPart); const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
switch (rf) { switch (rf) {
case RetFormat::JSON: { case RESTResponseFormat::JSON: {
UniValue mempoolInfoObject = MempoolInfoToJSON(*mempool); UniValue mempoolInfoObject = MempoolInfoToJSON(*mempool);
std::string strJSON = mempoolInfoObject.write() + "\n"; std::string strJSON = mempoolInfoObject.write() + "\n";
@ -602,10 +602,10 @@ static bool rest_mempool_contents(const std::any& context, HTTPRequest* req, con
const CTxMemPool* mempool = GetMemPool(context, req); const CTxMemPool* mempool = GetMemPool(context, req);
if (!mempool) return false; if (!mempool) return false;
std::string param; std::string param;
const RetFormat rf = ParseDataFormat(param, strURIPart); const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
switch (rf) { switch (rf) {
case RetFormat::JSON: { case RESTResponseFormat::JSON: {
UniValue mempoolObject = MempoolToJSON(*mempool, true); UniValue mempoolObject = MempoolToJSON(*mempool, true);
std::string strJSON = mempoolObject.write() + "\n"; std::string strJSON = mempoolObject.write() + "\n";
@ -624,7 +624,7 @@ static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string
if (!CheckWarmup(req)) if (!CheckWarmup(req))
return false; return false;
std::string hashStr; std::string hashStr;
const RetFormat rf = ParseDataFormat(hashStr, strURIPart); const RESTResponseFormat rf = ParseDataFormat(hashStr, strURIPart);
uint256 hash; uint256 hash;
if (!ParseHashStr(hashStr, hash)) if (!ParseHashStr(hashStr, hash))
@ -643,7 +643,7 @@ static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string
} }
switch (rf) { switch (rf) {
case RetFormat::BINARY: { case RESTResponseFormat::BINARY: {
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
ssTx << tx; ssTx << tx;
@ -653,7 +653,7 @@ static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string
return true; return true;
} }
case RetFormat::HEX: { case RESTResponseFormat::HEX: {
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
ssTx << tx; ssTx << tx;
@ -663,7 +663,7 @@ static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string
return true; return true;
} }
case RetFormat::JSON: { case RESTResponseFormat::JSON: {
UniValue objTx(UniValue::VOBJ); UniValue objTx(UniValue::VOBJ);
TxToUniv(*tx, hashBlock, objTx); TxToUniv(*tx, hashBlock, objTx);
std::string strJSON = objTx.write() + "\n"; std::string strJSON = objTx.write() + "\n";
@ -683,7 +683,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
if (!CheckWarmup(req)) if (!CheckWarmup(req))
return false; return false;
std::string param; std::string param;
const RetFormat rf = ParseDataFormat(param, strURIPart); const RESTResponseFormat rf = ParseDataFormat(param, strURIPart);
std::vector<std::string> uriParts; std::vector<std::string> uriParts;
if (param.length() > 1) if (param.length() > 1)
@ -730,14 +730,14 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
} }
switch (rf) { switch (rf) {
case RetFormat::HEX: { case RESTResponseFormat::HEX: {
// convert hex to bin, continue then with bin part // convert hex to bin, continue then with bin part
std::vector<unsigned char> strRequestV = ParseHex(strRequestMutable); std::vector<unsigned char> strRequestV = ParseHex(strRequestMutable);
strRequestMutable.assign(strRequestV.begin(), strRequestV.end()); strRequestMutable.assign(strRequestV.begin(), strRequestV.end());
[[fallthrough]]; [[fallthrough]];
} }
case RetFormat::BINARY: { case RESTResponseFormat::BINARY: {
try { try {
//deserialize only if user sent a request //deserialize only if user sent a request
if (strRequestMutable.size() > 0) if (strRequestMutable.size() > 0)
@ -757,7 +757,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
break; break;
} }
case RetFormat::JSON: { case RESTResponseFormat::JSON: {
if (!fInputParsed) if (!fInputParsed)
return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request"); return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
break; break;
@ -811,7 +811,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
} }
switch (rf) { switch (rf) {
case RetFormat::BINARY: { case RESTResponseFormat::BINARY: {
// serialize data // serialize data
// use exact same output as mentioned in Bip64 // use exact same output as mentioned in Bip64
CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
@ -823,7 +823,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
return true; return true;
} }
case RetFormat::HEX: { case RESTResponseFormat::HEX: {
CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
ssGetUTXOResponse << chainman.ActiveChain().Height() << chainman.ActiveChain().Tip()->GetBlockHash() << bitmap << outs; ssGetUTXOResponse << chainman.ActiveChain().Height() << chainman.ActiveChain().Tip()->GetBlockHash() << bitmap << outs;
std::string strHex = HexStr(ssGetUTXOResponse) + "\n"; std::string strHex = HexStr(ssGetUTXOResponse) + "\n";
@ -833,7 +833,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
return true; return true;
} }
case RetFormat::JSON: { case RESTResponseFormat::JSON: {
UniValue objGetUTXOResponse(UniValue::VOBJ); UniValue objGetUTXOResponse(UniValue::VOBJ);
// pack in some essentials // pack in some essentials
@ -873,7 +873,7 @@ static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
{ {
if (!CheckWarmup(req)) return false; if (!CheckWarmup(req)) return false;
std::string height_str; std::string height_str;
const RetFormat rf = ParseDataFormat(height_str, str_uri_part); const RESTResponseFormat rf = ParseDataFormat(height_str, str_uri_part);
int32_t blockheight = -1; // Initialization done only to prevent valgrind false positive, see https://github.com/bitcoin/bitcoin/pull/18785 int32_t blockheight = -1; // Initialization done only to prevent valgrind false positive, see https://github.com/bitcoin/bitcoin/pull/18785
if (!ParseInt32(height_str, &blockheight) || blockheight < 0) { if (!ParseInt32(height_str, &blockheight) || blockheight < 0) {
@ -893,19 +893,19 @@ static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
pblockindex = active_chain[blockheight]; pblockindex = active_chain[blockheight];
} }
switch (rf) { switch (rf) {
case RetFormat::BINARY: { case RESTResponseFormat::BINARY: {
CDataStream ss_blockhash(SER_NETWORK, PROTOCOL_VERSION); CDataStream ss_blockhash(SER_NETWORK, PROTOCOL_VERSION);
ss_blockhash << pblockindex->GetBlockHash(); ss_blockhash << pblockindex->GetBlockHash();
req->WriteHeader("Content-Type", "application/octet-stream"); req->WriteHeader("Content-Type", "application/octet-stream");
req->WriteReply(HTTP_OK, ss_blockhash.str()); req->WriteReply(HTTP_OK, ss_blockhash.str());
return true; return true;
} }
case RetFormat::HEX: { case RESTResponseFormat::HEX: {
req->WriteHeader("Content-Type", "text/plain"); req->WriteHeader("Content-Type", "text/plain");
req->WriteReply(HTTP_OK, pblockindex->GetBlockHash().GetHex() + "\n"); req->WriteReply(HTTP_OK, pblockindex->GetBlockHash().GetHex() + "\n");
return true; return true;
} }
case RetFormat::JSON: { case RESTResponseFormat::JSON: {
req->WriteHeader("Content-Type", "application/json"); req->WriteHeader("Content-Type", "application/json");
UniValue resp = UniValue(UniValue::VOBJ); UniValue resp = UniValue(UniValue::VOBJ);
resp.pushKV("blockhash", pblockindex->GetBlockHash().GetHex()); resp.pushKV("blockhash", pblockindex->GetBlockHash().GetHex());

View file

@ -7,13 +7,13 @@
#include <string> #include <string>
enum class RetFormat { enum class RESTResponseFormat {
UNDEF, UNDEF,
BINARY, BINARY,
HEX, HEX,
JSON, JSON,
}; };
RetFormat ParseDataFormat(std::string& param, const std::string& strReq); RESTResponseFormat ParseDataFormat(std::string& param, const std::string& strReq);
#endif // BITCOIN_REST_H #endif // BITCOIN_REST_H