From bfcfb70d8c60d5b5a186f923a1b7c503257e2a98 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Thu, 13 Jun 2019 12:04:07 +0300 Subject: [PATCH] Ignore blocks that do not match the filter in getmerkleblocks rpc (#2973) Also clarify method description a bit --- src/rpc/blockchain.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 1ded789e01..7df8fb3f5a 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -838,7 +838,7 @@ UniValue getmerkleblocks(const JSONRPCRequest& request) if (request.fHelp || request.params.size() < 1 || request.params.size() > 3) throw std::runtime_error( "getmerkleblocks \"filter\" \"hash\" ( count )\n" - "\nReturns an array of items with information about merkleblocks starting from which match .\n" + "\nReturns an array of hex-encoded merkleblocks for blocks starting from which match .\n" "\nArguments:\n" "1. \"filter\" (string, required) The hex encoded bloom filter\n" "2. \"hash\" (string, required) The block hash\n" @@ -899,16 +899,25 @@ UniValue getmerkleblocks(const JSONRPCRequest& request) for (; pblockindex; pblockindex = chainActive.Next(pblockindex)) { + if (--nCount < 0) { + break; + } + if (!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus())) { // this shouldn't happen, we already checked pruning case earlier throw JSONRPCError(RPC_MISC_ERROR, "Block not found on disk"); } + + CMerkleBlock merkleblock(block, filter); + if (merkleblock.vMatchedTxn.empty()) { + // ignore blocks that do not match the filter + continue; + } + CDataStream ssMerkleBlock(SER_NETWORK, PROTOCOL_VERSION); - ssMerkleBlock << CMerkleBlock(block, filter); + ssMerkleBlock << merkleblock; std::string strHex = HexStr(ssMerkleBlock); arrMerkleBlocks.push_back(strHex); - if (--nCount <= 0) - break; } return arrMerkleBlocks; }