diff --git a/src/merkleblock.h b/src/merkleblock.h --- a/src/merkleblock.h +++ b/src/merkleblock.h @@ -52,7 +52,7 @@ class CPartialMerkleTree { protected: /** the total number of transactions in the block */ - unsigned int nTransactions; + uint32_t nTransactions; /** node-is-parent-of-matched-txid bits */ std::vector vBits; @@ -67,7 +67,7 @@ * Helper function to efficiently calculate the number of nodes at given * height in the merkle tree. */ - unsigned int CalcTreeWidth(int height) const { + size_t CalcTreeWidth(int height) const { return (nTransactions + (1 << height) - 1) >> height; } @@ -75,14 +75,13 @@ * Calculate the hash of a node in the merkle tree (at leaf level: the * txid's themselves) */ - uint256 CalcHash(int height, unsigned int pos, - const std::vector &vTxid); + uint256 CalcHash(int height, size_t pos, const std::vector &vTxid); /** * Recursive function that traverses tree nodes, storing the data as bits * and hashes. */ - void TraverseAndBuild(int height, unsigned int pos, + void TraverseAndBuild(int height, size_t pos, const std::vector &vTxid, const std::vector &vMatch); @@ -91,10 +90,9 @@ * hashes produced by TraverseAndBuild. It returns the hash of the * respective node and its respective index. */ - uint256 TraverseAndExtract(int height, unsigned int pos, - unsigned int &nBitsUsed, unsigned int &nHashUsed, - std::vector &vMatch, - std::vector &vnIndex); + uint256 TraverseAndExtract(int height, size_t pos, size_t &nBitsUsed, + size_t &nHashUsed, std::vector &vMatch, + std::vector &vnIndex); public: /** serialization implementation */ @@ -109,13 +107,13 @@ READWRITE(vBytes); CPartialMerkleTree &us = *(const_cast(this)); us.vBits.resize(vBytes.size() * 8); - for (unsigned int p = 0; p < us.vBits.size(); p++) { + for (size_t p = 0; p < us.vBits.size(); p++) { us.vBits[p] = (vBytes[p / 8] & (1 << (p % 8))) != 0; } us.fBad = false; } else { vBytes.resize((vBits.size() + 7) / 8); - for (unsigned int p = 0; p < vBits.size(); p++) { + for (size_t p = 0; p < vBits.size(); p++) { vBytes[p / 8] |= vBits[p] << (p % 8); } READWRITE(vBytes); @@ -137,7 +135,7 @@ * root, or 0 in case of failure. */ uint256 ExtractMatches(std::vector &vMatch, - std::vector &vnIndex); + std::vector &vnIndex); }; /** @@ -159,7 +157,7 @@ CPartialMerkleTree txn; /** Public only for unit testing and relay testing (not relayed) */ - std::vector> vMatchedTxn; + std::vector> vMatchedTxn; /** * Create a Merkle proof according to a bloom filter. Note diff --git a/src/merkleblock.cpp b/src/merkleblock.cpp --- a/src/merkleblock.cpp +++ b/src/merkleblock.cpp @@ -52,7 +52,7 @@ txn = CPartialMerkleTree(vHashes, vMatch); } -uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, +uint256 CPartialMerkleTree::CalcHash(int height, size_t pos, const std::vector &vTxid) { // we can never have zero txs in a merkle block, we always need the // coinbase tx if we do not have this assert, we can hit a memory @@ -77,13 +77,13 @@ return Hash(BEGIN(left), END(left), BEGIN(right), END(right)); } -void CPartialMerkleTree::TraverseAndBuild(int height, unsigned int pos, +void CPartialMerkleTree::TraverseAndBuild(int height, size_t pos, const std::vector &vTxid, const std::vector &vMatch) { // Determine whether this node is the parent of at least one matched txid. bool fParentOfMatch = false; - for (unsigned int p = pos << height; - p < (pos + 1) << height && p < nTransactions; p++) { + for (size_t p = pos << height; p < (pos + 1) << height && p < nTransactions; + p++) { fParentOfMatch |= vMatch[p]; } @@ -101,10 +101,11 @@ } } -uint256 CPartialMerkleTree::TraverseAndExtract( - int height, unsigned int pos, unsigned int &nBitsUsed, - unsigned int &nHashUsed, std::vector &vMatch, - std::vector &vnIndex) { +uint256 CPartialMerkleTree::TraverseAndExtract(int height, size_t pos, + size_t &nBitsUsed, + size_t &nHashUsed, + std::vector &vMatch, + std::vector &vnIndex) { if (nBitsUsed >= vBits.size()) { // Overflowed the bits array - failure fBad = true; @@ -169,7 +170,7 @@ CPartialMerkleTree::CPartialMerkleTree() : nTransactions(0), fBad(true) {} uint256 CPartialMerkleTree::ExtractMatches(std::vector &vMatch, - std::vector &vnIndex) { + std::vector &vnIndex) { vMatch.clear(); // An empty set will not work @@ -198,7 +199,7 @@ } // traverse the partial tree. - unsigned int nBitsUsed = 0, nHashUsed = 0; + size_t nBitsUsed = 0, nHashUsed = 0; uint256 hashMerkleRoot = TraverseAndExtract(nHeight, 0, nBitsUsed, nHashUsed, vMatch, vnIndex); diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1371,7 +1371,7 @@ // protocol spec specified allows for us to provide duplicate // txn here, however we MUST always provide at least what the // remote peer needs. - typedef std::pair PairType; + typedef std::pair PairType; for (PairType &pair : merkleBlock.vMatchedTxn) { connman->PushMessage( pfrom, msgMaker.Make(NetMsgType::TX, diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -382,7 +382,7 @@ UniValue res(UniValue::VARR); std::vector vMatch; - std::vector vIndex; + std::vector vIndex; if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != merkleBlock.header.hashMerkleRoot) { return res; diff --git a/src/test/bloom_tests.cpp b/src/test/bloom_tests.cpp --- a/src/test/bloom_tests.cpp +++ b/src/test/bloom_tests.cpp @@ -51,8 +51,9 @@ std::vector vch = ParseHex("03614e9b050000000000000001"); std::vector expected(vch.size()); - for (unsigned int i = 0; i < vch.size(); i++) + for (size_t i = 0; i < vch.size(); i++) { expected[i] = (char)vch[i]; + } BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), expected.begin(), expected.end()); @@ -95,8 +96,9 @@ std::vector vch = ParseHex("03ce4299050000000100008001"); std::vector expected(vch.size()); - for (unsigned int i = 0; i < vch.size(); i++) + for (size_t i = 0; i < vch.size(); i++) { expected[i] = (char)vch[i]; + } BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), expected.begin(), expected.end()); @@ -123,8 +125,9 @@ std::vector vch = ParseHex("038fc16b080000000000000001"); std::vector expected(vch.size()); - for (unsigned int i = 0; i < vch.size(); i++) + for (size_t i = 0; i < vch.size(); i++) { expected[i] = (char)vch[i]; + } BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), expected.begin(), expected.end()); @@ -227,7 +230,7 @@ "ba58f3bdaab65e73b7e9260b"), 0); { - std::vector data(32 + sizeof(unsigned int)); + std::vector data(32 + sizeof(uint32_t)); memcpy(&data[0], prevOutPoint.GetTxId().begin(), 32); uint32_t n = prevOutPoint.GetN(); memcpy(&data[32], &n, sizeof(uint32_t)); @@ -377,7 +380,7 @@ BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); - std::pair pair = merkleBlock.vMatchedTxn[0]; + std::pair pair = merkleBlock.vMatchedTxn[0]; BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b" @@ -385,12 +388,13 @@ BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 8); std::vector vMatched; - std::vector vIndex; + std::vector vIndex; BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); - for (unsigned int i = 0; i < vMatched.size(); i++) + for (size_t i = 0; i < vMatched.size(); i++) { BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); + } // Also match the 8th transaction filter.insert(uint256S( @@ -410,8 +414,9 @@ BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); - for (unsigned int i = 0; i < vMatched.size(); i++) + for (size_t i = 0; i < vMatched.size(); i++) { BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); + } } BOOST_AUTO_TEST_CASE(merkle_block_2) { @@ -475,7 +480,7 @@ BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); - std::pair pair = merkleBlock.vMatchedTxn[0]; + std::pair pair = merkleBlock.vMatchedTxn[0]; BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df" @@ -483,12 +488,13 @@ BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 0); std::vector vMatched; - std::vector vIndex; + std::vector vIndex; BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); - for (unsigned int i = 0; i < vMatched.size(); i++) + for (size_t i = 0; i < vMatched.size(); i++) { BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); + } // Match an output from the second transaction (the pubkey for address // 1DZTzaBHUDM7T3QvUKBz4qXMRpkg8jsfB5) @@ -524,8 +530,9 @@ BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); - for (unsigned int i = 0; i < vMatched.size(); i++) + for (size_t i = 0; i < vMatched.size(); i++) { BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); + } } BOOST_AUTO_TEST_CASE(merkle_block_2_with_update_none) { @@ -589,7 +596,7 @@ BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); - std::pair pair = merkleBlock.vMatchedTxn[0]; + std::pair pair = merkleBlock.vMatchedTxn[0]; BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df" @@ -597,12 +604,13 @@ BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 0); std::vector vMatched; - std::vector vIndex; + std::vector vIndex; BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); - for (unsigned int i = 0; i < vMatched.size(); i++) + for (size_t i = 0; i < vMatched.size(); i++) { BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); + } // Match an output from the second transaction (the pubkey for address // 1DZTzaBHUDM7T3QvUKBz4qXMRpkg8jsfB5) @@ -634,8 +642,9 @@ BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); - for (unsigned int i = 0; i < vMatched.size(); i++) + for (size_t i = 0; i < vMatched.size(); i++) { BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); + } } BOOST_AUTO_TEST_CASE(merkle_block_3_and_serialize) { @@ -671,12 +680,13 @@ BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 0); std::vector vMatched; - std::vector vIndex; + std::vector vIndex; BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); - for (unsigned int i = 0; i < vMatched.size(); i++) + for (size_t i = 0; i < vMatched.size(); i++) { BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); + } CDataStream merkleStream(SER_NETWORK, PROTOCOL_VERSION); merkleStream << merkleBlock; @@ -688,8 +698,9 @@ "33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f19630101"); std::vector expected(vch.size()); - for (unsigned int i = 0; i < vch.size(); i++) + for (size_t i = 0; i < vch.size(); i++) { expected[i] = (char)vch[i]; + } BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), merkleStream.begin(), merkleStream.end()); @@ -792,7 +803,7 @@ BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); - std::pair pair = merkleBlock.vMatchedTxn[0]; + std::pair pair = merkleBlock.vMatchedTxn[0]; BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x0a2a92f0bda4727d0a13eaddf4dd9ac6b5c61a1429e6b2b818f" @@ -800,12 +811,13 @@ BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 6); std::vector vMatched; - std::vector vIndex; + std::vector vIndex; BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); - for (unsigned int i = 0; i < vMatched.size(); i++) + for (size_t i = 0; i < vMatched.size(); i++) { BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); + } // Also match the 4th transaction filter.insert(uint256S( @@ -825,8 +837,9 @@ BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); - for (unsigned int i = 0; i < vMatched.size(); i++) + for (size_t i = 0; i < vMatched.size(); i++) { BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); + } } BOOST_AUTO_TEST_CASE(merkle_block_4_test_p2pubkey_only) { diff --git a/src/test/pmt_tests.cpp b/src/test/pmt_tests.cpp --- a/src/test/pmt_tests.cpp +++ b/src/test/pmt_tests.cpp @@ -87,7 +87,7 @@ // extract merkle root and matched txids from copy std::vector vMatchTxid2; - std::vector vIndex; + std::vector vIndex; uint256 merkleRoot2 = pmt2.ExtractMatches(vMatchTxid2, vIndex); // check that it has the same merkle root as the original, and a @@ -121,7 +121,7 @@ false, false, false, true, true, false}; CPartialMerkleTree tree(vTxid, vMatch); - std::vector vIndex; + std::vector vIndex; BOOST_CHECK(tree.ExtractMatches(vTxid, vIndex).IsNull()); } diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -387,8 +387,8 @@ // Search partial merkle tree in proof for our transaction and index in // valid block std::vector vMatch; - std::vector vIndex; - unsigned int txnIndex = 0; + std::vector vIndex; + size_t txnIndex = 0; if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) == merkleBlock.header.hashMerkleRoot) {