| 29 | BOOST_FIXTURE_TEST_SUITE(pmt_tests, BasicTestingSetup) |
| 30 | |
| 31 | BOOST_AUTO_TEST_CASE(pmt_test1) |
| 32 | { |
| 33 | static const unsigned int nTxCounts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095}; |
| 34 | |
| 35 | for (int i = 0; i < 12; i++) { |
| 36 | unsigned int nTx = nTxCounts[i]; |
| 37 | |
| 38 | // build a block with some dummy transactions |
| 39 | CBlock block; |
| 40 | for (unsigned int j=0; j<nTx; j++) { |
| 41 | CMutableTransaction tx; |
| 42 | tx.nLockTime = j; // actual transaction data doesn't matter; just make the nLockTime's unique |
| 43 | block.vtx.push_back(MakeTransactionRef(std::move(tx))); |
| 44 | } |
| 45 | |
| 46 | // calculate actual merkle root and height |
| 47 | uint256 merkleRoot1 = BlockMerkleRoot(block); |
| 48 | std::vector<uint256> vTxid(nTx, uint256()); |
| 49 | for (unsigned int j=0; j<nTx; j++) |
| 50 | vTxid[j] = block.vtx[j]->GetHash(); |
| 51 | int nHeight = 1, nTx_ = nTx; |
| 52 | while (nTx_ > 1) { |
| 53 | nTx_ = (nTx_+1)/2; |
| 54 | nHeight++; |
| 55 | } |
| 56 | |
| 57 | // check with random subsets with inclusion chances 1, 1/2, 1/4, ..., 1/128 |
| 58 | for (int att = 1; att < 15; att++) { |
| 59 | // build random subset of txid's |
| 60 | std::vector<bool> vMatch(nTx, false); |
| 61 | std::vector<uint256> vMatchTxid1; |
| 62 | for (unsigned int j=0; j<nTx; j++) { |
| 63 | bool fInclude = InsecureRandBits(att / 2) == 0; |
| 64 | vMatch[j] = fInclude; |
| 65 | if (fInclude) |
| 66 | vMatchTxid1.push_back(vTxid[j]); |
| 67 | } |
| 68 | |
| 69 | // build the partial merkle tree |
| 70 | CPartialMerkleTree pmt1(vTxid, vMatch); |
| 71 | |
| 72 | // serialize |
| 73 | CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); |
| 74 | ss << pmt1; |
| 75 | |
| 76 | // verify CPartialMerkleTree's size guarantees |
| 77 | unsigned int n = std::min<unsigned int>(nTx, 1 + vMatchTxid1.size()*nHeight); |
| 78 | BOOST_CHECK(ss.size() <= 10 + (258*n+7)/8); |
| 79 | |
| 80 | // deserialize into a tester copy |
| 81 | CPartialMerkleTreeTester pmt2; |
| 82 | ss >> pmt2; |
| 83 | |
| 84 | // extract merkle root and matched txids from copy |
| 85 | std::vector<uint256> vMatchTxid2; |
| 86 | std::vector<unsigned int> vIndex; |
| 87 | uint256 merkleRoot2 = pmt2.ExtractMatches(vMatchTxid2, vIndex); |
| 88 |
nothing calls this directly
no test coverage detected