9#include <boost/test/unit_test.hpp>
15 for (std::vector<uint256>::const_iterator it = vMerkleBranch.begin(); it != vMerkleBranch.end(); ++it) {
17 hash =
Hash(*it, hash);
19 hash =
Hash(hash, *it);
30 vMerkleTree.reserve(block.
vtx.size() * 2 + 16);
31 for (std::vector<CTransactionRef>::const_iterator it(block.
vtx.begin()); it != block.
vtx.end(); ++it)
32 vMerkleTree.push_back((*it)->GetHash());
35 for (
int nSize = block.
vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
37 for (
int i = 0; i < nSize; i += 2)
39 int i2 = std::min(i+1, nSize-1);
40 if (i2 == i + 1 && i2 + 1 == nSize && vMerkleTree[j+i] == vMerkleTree[j+i2]) {
44 vMerkleTree.push_back(
Hash(vMerkleTree[j+i], vMerkleTree[j+i2]));
51 return (vMerkleTree.empty() ?
uint256() : vMerkleTree.back());
57 std::vector<uint256> vMerkleBranch;
59 for (
int nSize = block.
vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
61 int i = std::min(nIndex^1, nSize-1);
62 vMerkleBranch.push_back(vMerkleTree[j+i]);
69static inline int ctz(uint32_t i) {
81 for (
int i = 0; i < 32; i++) {
83 int ntx = (i <= 16) ? i : 17 + (m_rng.randrange(4000));
85 for (
int mutate = 0; mutate <= 3; mutate++) {
86 int duplicate1 = mutate >= 1 ? 1 <<
ctz(ntx) : 0;
87 if (duplicate1 >= ntx)
break;
88 int ntx1 = ntx + duplicate1;
89 int duplicate2 = mutate >= 2 ? 1 <<
ctz(ntx1) : 0;
90 if (duplicate2 >= ntx1)
break;
91 int ntx2 = ntx1 + duplicate2;
92 int duplicate3 = mutate >= 3 ? 1 <<
ctz(ntx2) : 0;
93 if (duplicate3 >= ntx2)
break;
94 int ntx3 = ntx2 + duplicate3;
97 block.
vtx.resize(ntx);
98 for (
int j = 0; j < ntx; j++) {
104 bool unmutatedMutated =
false;
108 block.
vtx.resize(ntx3);
109 for (
int j = 0; j < duplicate1; j++) {
110 block.
vtx[ntx + j] = block.
vtx[ntx + j - duplicate1];
112 for (
int j = 0; j < duplicate2; j++) {
113 block.
vtx[ntx1 + j] = block.
vtx[ntx1 + j - duplicate2];
115 for (
int j = 0; j < duplicate3; j++) {
116 block.
vtx[ntx2 + j] = block.
vtx[ntx2 + j - duplicate3];
119 bool oldMutated =
false;
120 std::vector<uint256> merkleTree;
123 bool newMutated =
false;
132 for (
int loop = 0; loop < std::min(ntx, 16); loop++) {
136 mtx = m_rng.randrange(ntx);
151 bool mutated =
false;
161 bool mutated =
false;
176 CBlock block, blockWithRepeatedLastTx;
180 for (std::size_t pos = 0; pos < block.
vtx.size(); pos++) {
186 blockWithRepeatedLastTx = block;
187 blockWithRepeatedLastTx.
vtx.push_back(blockWithRepeatedLastTx.
vtx.back());
199 CBlock block, leftSubtreeBlock, rightSubtreeBlock;
203 for (pos = 0; pos < block.
vtx.size(); pos++) {
209 for (pos = 0; pos < block.
vtx.size() / 2; pos++)
210 leftSubtreeBlock.
vtx.push_back(block.
vtx[pos]);
212 for (pos = block.
vtx.size() / 2; pos < block.
vtx.size(); pos++)
213 rightSubtreeBlock.
vtx.push_back(block.
vtx[pos]);
218 std::vector<uint256> leftRight;
219 leftRight.push_back(rootOfLeftSubtree);
220 leftRight.push_back(rootOfRightSubtree);
231 for (std::size_t pos = 0; pos < block.
vtx.size(); pos++) {
239 std::vector<uint256> hashes;
240 hashes.resize(block.
vtx.size());
242 hashes[1] = block.
vtx[1]->GetHash();
std::vector< CTransactionRef > vtx
constexpr bool IsNull() const
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
uint256 ComputeMerkleRoot(std::vector< uint256 > hashes, bool *mutated)
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
std::vector< uint256 > TransactionMerklePath(const CBlock &block, uint32_t position)
Compute merkle path to the specified transaction.
uint256 BlockWitnessMerkleRoot(const CBlock &block, bool *mutated)
static uint256 BlockBuildMerkleTree(const CBlock &block, bool *fMutated, std::vector< uint256 > &vMerkleTree)
static int ctz(uint32_t i)
static uint256 ComputeMerkleRootFromBranch(const uint256 &leaf, const std::vector< uint256 > &vMerkleBranch, uint32_t nIndex)
static std::vector< uint256 > BlockGetMerkleBranch(const CBlock &block, const std::vector< uint256 > &vMerkleTree, int nIndex)
BOOST_AUTO_TEST_CASE(merkle_test)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
static CTransactionRef MakeTransactionRef(Tx &&txIn)
A mutable version of CTransaction.
Testing setup that configures a complete environment.