Skip to content

Commit

Permalink
refactor: Replace HexStr(o.begin(), o.end()) with HexStr(o)
Browse files Browse the repository at this point in the history
HexStr can be called with anything that bas `begin()` and `end()` functions,
so clean up the redundant calls.
  • Loading branch information
laanwj authored and xanimo committed Jul 13, 2024
1 parent d2f0124 commit 4a57b7b
Show file tree
Hide file tree
Showing 12 changed files with 2,032 additions and 22 deletions.
10 changes: 5 additions & 5 deletions src/core_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ std::string EncodeHexTx(const CTransaction& tx, const int serialFlags)
{
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | serialFlags);
ssTx << tx;
return HexStr(ssTx.begin(), ssTx.end());
return HexStr(ssTx);
}

void ScriptPubKeyToUniv(const CScript& scriptPubKey,
Expand All @@ -129,7 +129,7 @@ void ScriptPubKeyToUniv(const CScript& scriptPubKey,

out.pushKV("asm", ScriptToAsmStr(scriptPubKey));
if (fIncludeHex)
out.pushKV("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
out.pushKV("hex", HexStr(scriptPubKey));

if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
out.pushKV("type", GetTxnOutputType(type));
Expand Down Expand Up @@ -157,18 +157,18 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry)
const CTxIn& txin = tx.vin[i];
UniValue in(UniValue::VOBJ);
if (tx.IsCoinBase())
in.pushKV("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
in.pushKV("coinbase", HexStr(txin.scriptSig));
else {
in.pushKV("txid", txin.prevout.hash.GetHex());
in.pushKV("vout", (int64_t)txin.prevout.n);
UniValue o(UniValue::VOBJ);
o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
o.pushKV("hex", HexStr(txin.scriptSig));
in.pushKV("scriptSig", o);
if (!tx.vin[i].scriptWitness.IsNull()) {
UniValue txinwitness(UniValue::VARR);
for (const auto& item : tx.vin[i].scriptWitness.stack) {
txinwitness.push_back(HexStr(item.begin(), item.end()));
txinwitness.push_back(HexStr(item));
}
in.pushKV("txinwitness", txinwitness);
}
Expand Down
18 changes: 14 additions & 4 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,12 @@ static bool rest_headers(HTTPRequest* req,
}

case RF_HEX: {
std::string strHex = HexStr(ssHeader.begin(), ssHeader.end()) + "\n";
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
for (const CBlockIndex *pindex : headers) {
ssHeader << pindex->GetBlockHeader(Params().GetConsensus(pindex->nHeight));
}

std::string strHex = HexStr(ssHeader) + "\n";
req->WriteHeader("Content-Type", "text/plain");
req->WriteReply(HTTP_OK, strHex);
return true;
Expand Down Expand Up @@ -239,7 +244,9 @@ static bool rest_block(HTTPRequest* req,
}

case RF_HEX: {
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()) + "\n";
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
ssBlock << block;
std::string strHex = HexStr(ssBlock) + "\n";
req->WriteHeader("Content-Type", "text/plain");
req->WriteReply(HTTP_OK, strHex);
return true;
Expand Down Expand Up @@ -379,7 +386,10 @@ static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
}

case RF_HEX: {
std::string strHex = HexStr(ssTx.begin(), ssTx.end()) + "\n";
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
ssTx << tx;

std::string strHex = HexStr(ssTx) + "\n";
req->WriteHeader("Content-Type", "text/plain");
req->WriteReply(HTTP_OK, strHex);
return true;
Expand Down Expand Up @@ -563,7 +573,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
case RF_HEX: {
CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
ssGetUTXOResponse << chainActive.Height() << chainActive.Tip()->GetBlockHash() << bitmap << outs;
std::string strHex = HexStr(ssGetUTXOResponse.begin(), ssGetUTXOResponse.end()) + "\n";
std::string strHex = HexStr(ssGetUTXOResponse) + "\n";

req->WriteHeader("Content-Type", "text/plain");
req->WriteReply(HTTP_OK, strHex);
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ UniValue getblockheader(const JSONRPCRequest& request)
{
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
ssBlock << pblockindex->GetBlockHeader(Params().GetConsensus(pblockindex->nHeight));
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end());
std::string strHex = HexStr(ssBlock);
return strHex;
}

Expand Down Expand Up @@ -850,7 +850,7 @@ UniValue getblock(const JSONRPCRequest& request)
{
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
ssBlock << block;
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end());
std::string strHex = HexStr(ssBlock);
return strHex;
}

Expand Down
2 changes: 1 addition & 1 deletion src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
result.pushKV("height", (int64_t)(pindexPrev->nHeight+1));

if (!pblocktemplate->vchCoinbaseCommitment.empty() && fSupportsSegwit) {
result.pushKV("default_witness_commitment", HexStr(pblocktemplate->vchCoinbaseCommitment.begin(), pblocktemplate->vchCoinbaseCommitment.end()));
result.pushKV("default_witness_commitment", HexStr(pblocktemplate->vchCoinbaseCommitment));
}

return result;
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ UniValue validateaddress(const JSONRPCRequest& request)
ret.pushKV("address", currentAddress);

CScript scriptPubKey = GetScriptForDestination(dest);
ret.pushKV("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
ret.pushKV("scriptPubKey", HexStr(scriptPubKey));

#ifdef ENABLE_WALLET
isminetype mine = pwalletMain ? IsMine(*pwalletMain, dest) : ISMINE_NO;
Expand Down Expand Up @@ -327,7 +327,7 @@ UniValue createmultisig(const JSONRPCRequest& request)

UniValue result(UniValue::VOBJ);
result.pushKV("address", address.ToString());
result.pushKV("redeemScript", HexStr(inner.begin(), inner.end()));
result.pushKV("redeemScript", HexStr(inner));

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ UniValue gettxoutproof(const JSONRPCRequest& request)
CDataStream ssMB(SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
CMerkleBlock mb(block, setTxids);
ssMB << mb;
std::string strHex = HexStr(ssMB.begin(), ssMB.end());
std::string strHex = HexStr(ssMB);
return strHex;
}

Expand Down
Loading

0 comments on commit 4a57b7b

Please sign in to comment.