10#include <boost/test/unit_test.hpp>
16 for (std::vector<uint256>::const_iterator it = vMerkleBranch.begin(); it != vMerkleBranch.end(); ++it) {
18 hash =
Hash(*it, hash);
20 hash =
Hash(hash, *it);
31 vMerkleTree.reserve(block.
vtx.size() * 2 + 16);
32 for (std::vector<CTransactionRef>::const_iterator it(block.
vtx.begin()); it != block.
vtx.end(); ++it)
33 vMerkleTree.push_back((*it)->GetHash().ToUint256());
36 for (
int nSize = block.
vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
38 for (
int i = 0; i < nSize; i += 2)
40 int i2 = std::min(i+1, nSize-1);
41 if (i2 == i + 1 && i2 + 1 == nSize && vMerkleTree[j+i] == vMerkleTree[j+i2]) {
45 vMerkleTree.push_back(
Hash(vMerkleTree[j+i], vMerkleTree[j+i2]));
52 return (vMerkleTree.empty() ?
uint256() : vMerkleTree.back());
58 std::vector<uint256> vMerkleBranch;
60 for (
int nSize = block.
vtx.size(); nSize > 1; nSize = (nSize + 1) / 2)
62 int i = std::min(nIndex^1, nSize-1);
63 vMerkleBranch.push_back(vMerkleTree[j+i]);
70static inline int ctz(uint32_t i) {
82 for (
int i = 0; i < 32; i++) {
84 int ntx = (i <= 16) ? i : 17 + (m_rng.randrange(4000));
86 for (
int mutate = 0; mutate <= 3; mutate++) {
87 int duplicate1 = mutate >= 1 ? 1 <<
ctz(ntx) : 0;
88 if (duplicate1 >= ntx)
break;
89 int ntx1 = ntx + duplicate1;
90 int duplicate2 = mutate >= 2 ? 1 <<
ctz(ntx1) : 0;
91 if (duplicate2 >= ntx1)
break;
92 int ntx2 = ntx1 + duplicate2;
93 int duplicate3 = mutate >= 3 ? 1 <<
ctz(ntx2) : 0;
94 if (duplicate3 >= ntx2)
break;
95 int ntx3 = ntx2 + duplicate3;
98 block.
vtx.resize(ntx);
99 for (
int j = 0; j < ntx; j++) {
105 bool unmutatedMutated =
false;
109 block.
vtx.resize(ntx3);
110 for (
int j = 0; j < duplicate1; j++) {
111 block.
vtx[ntx + j] = block.
vtx[ntx + j - duplicate1];
113 for (
int j = 0; j < duplicate2; j++) {
114 block.
vtx[ntx1 + j] = block.
vtx[ntx1 + j - duplicate2];
116 for (
int j = 0; j < duplicate3; j++) {
117 block.
vtx[ntx2 + j] = block.
vtx[ntx2 + j - duplicate3];
120 bool oldMutated =
false;
121 std::vector<uint256> merkleTree;
124 bool newMutated =
false;
133 for (
int loop = 0; loop < std::min(ntx, 16); loop++) {
137 mtx = m_rng.randrange(ntx);
152 bool mutated =
false;
167 bool mutated =
false;
182 CBlock block, blockWithRepeatedLastTx;
186 for (std::size_t pos = 0; pos < block.
vtx.size(); pos++) {
192 blockWithRepeatedLastTx = block;
193 blockWithRepeatedLastTx.
vtx.push_back(blockWithRepeatedLastTx.
vtx.back());
205 CBlock block, leftSubtreeBlock, rightSubtreeBlock;
209 for (pos = 0; pos < block.
vtx.size(); pos++) {
215 for (pos = 0; pos < block.
vtx.size() / 2; pos++)
216 leftSubtreeBlock.
vtx.push_back(block.
vtx[pos]);
218 for (pos = block.
vtx.size() / 2; pos < block.
vtx.size(); pos++)
219 rightSubtreeBlock.
vtx.push_back(block.
vtx[pos]);
224 std::vector<uint256> leftRight;
225 leftRight.push_back(rootOfLeftSubtree);
226 leftRight.push_back(rootOfRightSubtree);
236 constexpr size_t vtx_count{3};
237 block.
vtx.resize(vtx_count);
238 for (std::size_t pos = 0; pos < vtx_count; pos++) {
246 std::vector<uint256> hashes;
247 hashes.resize(vtx_count);
249 for (
size_t pos{1}; pos < vtx_count; ++pos) {
250 hashes[pos] = block.
vtx[pos]->GetWitnessHash().ToUint256();
std::vector< CTransactionRef > vtx
constexpr bool IsNull() const
static const uint256 ZERO
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)
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.
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.