From 4a57b7bd7fb0e81d64dca970c1e67d7682b52e92 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Wed, 24 Jun 2020 15:48:26 +0200 Subject: [PATCH] refactor: Replace HexStr(o.begin(), o.end()) with HexStr(o) HexStr can be called with anything that bas `begin()` and `end()` functions, so clean up the redundant calls. --- src/core_write.cpp | 10 +- src/rest.cpp | 18 +- src/rpc/blockchain.cpp | 4 +- src/rpc/mining.cpp | 2 +- src/rpc/misc.cpp | 4 +- src/rpc/rawtransaction.cpp | 2 +- src/rpc/util.cpp | 852 ++++++++++++++++++++++ src/script/descriptor.cpp | 1148 ++++++++++++++++++++++++++++++ src/test/coins_tests.cpp | 4 +- src/test/sighash_tests.cpp | 2 +- src/wallet/rpcwallet.cpp | 4 +- src/wallet/test/crypto_tests.cpp | 4 +- 12 files changed, 2032 insertions(+), 22 deletions(-) create mode 100644 src/rpc/util.cpp create mode 100644 src/script/descriptor.cpp diff --git a/src/core_write.cpp b/src/core_write.cpp index 4ce18225da9..05d103185eb 100644 --- a/src/core_write.cpp +++ b/src/core_write.cpp @@ -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, @@ -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)); @@ -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); } diff --git a/src/rest.cpp b/src/rest.cpp index a5f23c2930e..914a9071c6a 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -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; @@ -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; @@ -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; @@ -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); diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 9b4c50cdcfe..ef0476ee487 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -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; } @@ -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; } diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 47ececdb7f1..d80cad959d9 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -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; diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index ca29683e762..c1f9caba276 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -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; @@ -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; } diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 6bb7b9fb22a..d77ab7b4067 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -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; } diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp new file mode 100644 index 00000000000..54ea352a72c --- /dev/null +++ b/src/rpc/util.cpp @@ -0,0 +1,852 @@ +// Copyright (c) 2017-2020 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include +#include +#include +#include