diff --git a/src/random.cpp b/src/random.cpp index 99d895c09..42438ad2a 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -1,676 +1,677 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #ifdef WIN32 #include // for Windows API #include #endif #include #include // for LogPrint() +#include #include #include // for WAIT_LOCK #include // for GetTime() #include #include #include #include #include #include #include #include -#include - #ifndef WIN32 #include #include #endif #ifdef HAVE_SYS_GETRANDOM #include #include #endif #if defined(HAVE_GETENTROPY) || \ (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)) #include #endif #if defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX) #include #endif #ifdef HAVE_SYSCTL_ARND #include #include // for ARRAYLEN #endif #if defined(__x86_64__) || defined(__amd64__) || defined(__i386__) #include #endif [[noreturn]] static void RandFailure() { LogPrintf("Failed to read randomness, aborting\n"); std::abort(); } static inline int64_t GetPerformanceCounter() noexcept { // Read the hardware time stamp counter when available. // See https://en.wikipedia.org/wiki/Time_Stamp_Counter for more information. #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) return __rdtsc(); #elif !defined(_MSC_VER) && defined(__i386__) uint64_t r = 0; // Constrain the r variable to the eax:edx pair. __asm__ volatile("rdtsc" : "=A"(r)); return r; #elif !defined(_MSC_VER) && (defined(__x86_64__) || defined(__amd64__)) uint64_t r1 = 0, r2 = 0; // Constrain r1 to rax and r2 to rdx. __asm__ volatile("rdtsc" : "=a"(r1), "=d"(r2)); return (r2 << 32) | r1; #else // Fall back to using C++11 clock (usually microsecond or nanosecond // precision) return std::chrono::high_resolution_clock::now().time_since_epoch().count(); #endif } #if defined(__x86_64__) || defined(__amd64__) || defined(__i386__) static bool rdrand_supported = false; static constexpr uint32_t CPUID_F1_ECX_RDRAND = 0x40000000; static void InitHardwareRand() { uint32_t eax, ebx, ecx, edx; if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) && (ecx & CPUID_F1_ECX_RDRAND)) { rdrand_supported = true; } } static void ReportHardwareRand() { if (rdrand_supported) { // This must be done in a separate function, as HWRandInit() may be // indirectly called from global constructors, before logging is // initialized. LogPrintf("Using RdRand as an additional entropy source\n"); } } #else /** * Access to other hardware random number generators could be added here later, * assuming it is sufficiently fast (in the order of a few hundred CPU cycles). * Slower sources should probably be invoked separately, and/or only from * RandAddSeedSleep (which is called during idle background operation). */ static void InitHardwareRand() {} static void ReportHardwareRand() {} #endif static bool GetHardwareRand(uint8_t *ent32) noexcept { #if defined(__x86_64__) || defined(__amd64__) || defined(__i386__) if (rdrand_supported) { uint8_t ok; // Not all assemblers support the rdrand instruction, write it in hex. #ifdef __i386__ for (int iter = 0; iter < 4; ++iter) { uint32_t r1, r2; __asm__ volatile(".byte 0x0f, 0xc7, 0xf0;" // rdrand %eax ".byte 0x0f, 0xc7, 0xf2;" // rdrand %edx "setc %2" : "=a"(r1), "=d"(r2), "=q"(ok)::"cc"); if (!ok) { return false; } WriteLE32(ent32 + 8 * iter, r1); WriteLE32(ent32 + 8 * iter + 4, r2); } #else uint64_t r1, r2, r3, r4; __asm__ volatile(".byte 0x48, 0x0f, 0xc7, 0xf0, " // rdrand %rax "0x48, 0x0f, 0xc7, 0xf3, " // rdrand %rbx "0x48, 0x0f, 0xc7, 0xf1, " // rdrand %rcx "0x48, 0x0f, 0xc7, 0xf2; " // rdrand %rdx "setc %4" : "=a"(r1), "=b"(r2), "=c"(r3), "=d"(r4), "=q"(ok)::"cc"); if (!ok) { return false; } WriteLE64(ent32, r1); WriteLE64(ent32 + 8, r2); WriteLE64(ent32 + 16, r3); WriteLE64(ent32 + 24, r4); #endif return true; } #endif return false; } static void RandAddSeedPerfmon(CSHA512 &hasher) { #ifdef WIN32 // Don't need this on Linux, OpenSSL automatically uses /dev/urandom // Seed with the entire set of perfmon data // This can take up to 2 seconds, so only do it every 10 minutes static int64_t nLastPerfmon; if (GetTime() < nLastPerfmon + 10 * 60) { return; } nLastPerfmon = GetTime(); std::vector vData(250000, 0); long ret = 0; unsigned long nSize = 0; // Bail out at more than 10MB of performance data const size_t nMaxSize = 10000000; while (true) { nSize = vData.size(); ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", nullptr, nullptr, vData.data(), &nSize); if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize) { break; } // Grow size of buffer exponentially vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); } RegCloseKey(HKEY_PERFORMANCE_DATA); if (ret == ERROR_SUCCESS) { hasher.Write(vData.data(), nSize); memory_cleanse(vData.data(), nSize); } else { // Performance data is only a best-effort attempt at improving the // situation when the OS randomness (and other sources) aren't // adequate. As a result, failure to read it is isn't considered // critical, so we don't call RandFailure(). // TODO: Add logging when the logger is made functional before global // constructors have been invoked. } #endif } #ifndef WIN32 /** * Fallback: get 32 bytes of system entropy from /dev/urandom. The most * compatible way to get cryptographic randomness on UNIX-ish platforms. */ static void GetDevURandom(uint8_t *ent32) { int f = open("/dev/urandom", O_RDONLY); if (f == -1) { RandFailure(); } int have = 0; do { ssize_t n = read(f, ent32 + have, NUM_OS_RANDOM_BYTES - have); if (n <= 0 || n + have > NUM_OS_RANDOM_BYTES) { close(f); RandFailure(); } have += n; } while (have < NUM_OS_RANDOM_BYTES); close(f); } #endif /** Get 32 bytes of system entropy. */ void GetOSRand(uint8_t *ent32) { #if defined(WIN32) HCRYPTPROV hProvider; int ret = CryptAcquireContextW(&hProvider, nullptr, nullptr, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); if (!ret) { RandFailure(); } ret = CryptGenRandom(hProvider, NUM_OS_RANDOM_BYTES, ent32); if (!ret) { RandFailure(); } CryptReleaseContext(hProvider, 0); #elif defined(HAVE_SYS_GETRANDOM) /** * Linux. From the getrandom(2) man page: * "If the urandom source has been initialized, reads of up to 256 bytes * will always return as many bytes as requested and will not be interrupted * by signals." */ int rv = syscall(SYS_getrandom, ent32, NUM_OS_RANDOM_BYTES, 0); if (rv != NUM_OS_RANDOM_BYTES) { if (rv < 0 && errno == ENOSYS) { /* Fallback for kernel <3.17: the return value will be -1 and errno * ENOSYS if the syscall is not available, in that case fall back * to /dev/urandom. */ GetDevURandom(ent32); } else { RandFailure(); } } #elif defined(HAVE_GETENTROPY) && defined(__OpenBSD__) /** * On OpenBSD this can return up to 256 bytes of entropy, will return an * error if more are requested. * The call cannot return less than the requested number of bytes. * getentropy is explicitly limited to openbsd here, as a similar (but not * the same) function may exist on other platforms via glibc. */ if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) { RandFailure(); } #elif defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX) // We need a fallback for OSX < 10.12 if (&getentropy != nullptr) { if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) { RandFailure(); } } else { GetDevURandom(ent32); } #elif defined(HAVE_SYSCTL_ARND) /** * FreeBSD and similar. It is possible for the call to return less bytes * than requested, so need to read in a loop. */ static const int name[2] = {CTL_KERN, KERN_ARND}; int have = 0; do { size_t len = NUM_OS_RANDOM_BYTES - have; if (sysctl(name, ARRAYLEN(name), ent32 + have, &len, nullptr, 0) != 0) { RandFailure(); } have += len; } while (have < NUM_OS_RANDOM_BYTES); #else /** * Fall back to /dev/urandom if there is no specific method implemented to * get system entropy for this OS. */ GetDevURandom(ent32); #endif } void LockingCallbackOpenSSL(int mode, int i, const char *file, int line); namespace { class RNGState { Mutex m_mutex; /** * The RNG state consists of 256 bits of entropy, taken from the output of * one operation's SHA512 output, and fed as input to the next one. * Carrying 256 bits of entropy should be sufficient to guarantee * unpredictability as long as any entropy source was ever unpredictable * to an attacker. To protect against situations where an attacker might * observe the RNG's state, fresh entropy is always mixed when * GetStrongRandBytes is called. */ uint8_t m_state[32] GUARDED_BY(m_mutex) = {0}; uint64_t m_counter GUARDED_BY(m_mutex) = 0; bool m_strongly_seeded GUARDED_BY(m_mutex) = false; std::unique_ptr m_mutex_openssl; public: RNGState() noexcept { InitHardwareRand(); // Init OpenSSL library multithreading support m_mutex_openssl.reset(new Mutex[CRYPTO_num_locks()]); CRYPTO_set_locking_callback(LockingCallbackOpenSSL); // OpenSSL can optionally load a config file which lists optional // loadable modules and engines. We don't use them so we don't require // the config. However some of our libs may call functions which attempt // to load the config file, possibly resulting in an exit() or crash if // it is missing or corrupt. Explicitly tell OpenSSL not to try to load // the file. The result for our libs will be that the config appears to // have been loaded and there are no modules/engines available. OPENSSL_no_config(); } ~RNGState() { // Securely erase the memory used by the OpenSSL PRNG RAND_cleanup(); // Shutdown OpenSSL library multithreading support CRYPTO_set_locking_callback(nullptr); } /** * Extract up to 32 bytes of entropy from the RNG state, mixing in new * entropy from hasher. * * If this function has never been called with strong_seed = true, false is * returned. */ bool MixExtract(uint8_t *out, size_t num, CSHA512 &&hasher, bool strong_seed) noexcept { assert(num <= 32); uint8_t buf[64]; static_assert(sizeof(buf) == CSHA512::OUTPUT_SIZE, "Buffer needs to have hasher's output size"); bool ret; { LOCK(m_mutex); ret = (m_strongly_seeded |= strong_seed); // Write the current state of the RNG into the hasher hasher.Write(m_state, 32); // Write a new counter number into the state hasher.Write((const uint8_t *)&m_counter, sizeof(m_counter)); ++m_counter; // Finalize the hasher hasher.Finalize(buf); // Store the last 32 bytes of the hash output as new RNG state. memcpy(m_state, buf + 32, 32); } // If desired, copy (up to) the first 32 bytes of the hash output as // output. if (num) { assert(out != nullptr); memcpy(out, buf, num); } // Best effort cleanup of internal state hasher.Reset(); memory_cleanse(buf, 64); return ret; } Mutex &GetOpenSSLMutex(int i) { return m_mutex_openssl[i]; } }; RNGState &GetRNGState() noexcept { // This C++11 idiom relies on the guarantee that static variable are // initialized on first call, even when multiple parallel calls are // permitted. static std::vector> g_rng(1); return g_rng[0]; } } // namespace void LockingCallbackOpenSSL(int mode, int i, const char *file, int line) NO_THREAD_SAFETY_ANALYSIS { RNGState &rng = GetRNGState(); if (mode & CRYPTO_LOCK) { rng.GetOpenSSLMutex(i).lock(); } else { rng.GetOpenSSLMutex(i).unlock(); } } /** * A note on the use of noexcept in the seeding functions below: * * None of the RNG code should ever throw any exception, with the sole exception * of MilliSleep in SeedSleep, which can (and does) support interruptions which * cause a boost::thread_interrupted to be thrown. * * This means that SeedSleep, and all functions that invoke it are throwing. * However, we know that GetRandBytes() and GetStrongRandBytes() never trigger * this sleeping logic, so they are noexcept. The same is true for all the * GetRand*() functions that use GetRandBytes() indirectly. * * TODO: After moving away from interruptible boost-based thread management, * everything can become noexcept here. */ static void SeedTimestamp(CSHA512 &hasher) noexcept { int64_t perfcounter = GetPerformanceCounter(); hasher.Write((const uint8_t *)&perfcounter, sizeof(perfcounter)); } static void SeedFast(CSHA512 &hasher) noexcept { uint8_t buffer[32]; // Stack pointer to indirectly commit to thread/callstack const uint8_t *ptr = buffer; hasher.Write((const uint8_t *)&ptr, sizeof(ptr)); // Hardware randomness is very fast when available; use it always. bool have_hw_rand = GetHardwareRand(buffer); if (have_hw_rand) { hasher.Write(buffer, sizeof(buffer)); } // High-precision timestamp SeedTimestamp(hasher); } static void SeedSlow(CSHA512 &hasher) noexcept { uint8_t buffer[32]; // Everything that the 'fast' seeder includes SeedFast(hasher); // OS randomness GetOSRand(buffer); hasher.Write(buffer, sizeof(buffer)); // OpenSSL RNG (for now) RAND_bytes(buffer, sizeof(buffer)); hasher.Write(buffer, sizeof(buffer)); // High-precision timestamp. // // Note that we also commit to a timestamp in the Fast seeder, so we // indirectly commit to a benchmark of all the entropy gathering sources in // this function). SeedTimestamp(hasher); } static void SeedSleep(CSHA512 &hasher) { // Everything that the 'fast' seeder includes SeedFast(hasher); // High-precision timestamp SeedTimestamp(hasher); // Sleep for 1ms MilliSleep(1); // High-precision timestamp after sleeping (as we commit to both the time // before and after, this measures the delay) SeedTimestamp(hasher); // Windows performance monitor data (once every 10 minutes) RandAddSeedPerfmon(hasher); } static void SeedStartup(CSHA512 &hasher) noexcept { #ifdef WIN32 RAND_screen(); #endif // Everything that the 'slow' seeder includes. SeedSlow(hasher); // Windows performance monitor data. RandAddSeedPerfmon(hasher); } enum class RNGLevel { FAST, //!< Automatically called by GetRandBytes SLOW, //!< Automatically called by GetStrongRandBytes SLEEP, //!< Called by RandAddSeedSleep() }; static void ProcRand(uint8_t *out, int num, RNGLevel level) { // Make sure the RNG is initialized first (as all Seed* function possibly // need hwrand to be available). RNGState &rng = GetRNGState(); assert(num <= 32); CSHA512 hasher; switch (level) { case RNGLevel::FAST: SeedFast(hasher); break; case RNGLevel::SLOW: SeedSlow(hasher); break; case RNGLevel::SLEEP: SeedSleep(hasher); break; } // Combine with and update state if (!rng.MixExtract(out, num, std::move(hasher), false)) { // On the first invocation, also seed with SeedStartup(). CSHA512 startup_hasher; SeedStartup(startup_hasher); rng.MixExtract(out, num, std::move(startup_hasher), true); } // For anything but the 'fast' level, feed the resulting RNG output (after // an additional hashing step) back into OpenSSL. if (level != RNGLevel::FAST) { uint8_t buf[64]; CSHA512().Write(out, num).Finalize(buf); RAND_add(buf, sizeof(buf), num); memory_cleanse(buf, 64); } } void GetRandBytes(uint8_t *buf, int num) noexcept { ProcRand(buf, num, RNGLevel::FAST); } void GetStrongRandBytes(uint8_t *buf, int num) noexcept { ProcRand(buf, num, RNGLevel::SLOW); } void RandAddSeedSleep() { ProcRand(nullptr, 0, RNGLevel::SLEEP); } +bool g_mock_deterministic_tests{false}; + uint64_t GetRand(uint64_t nMax) noexcept { - return FastRandomContext().randrange(nMax); + return FastRandomContext(g_mock_deterministic_tests).randrange(nMax); } int GetRandInt(int nMax) noexcept { return GetRand(nMax); } uint256 GetRandHash() noexcept { uint256 hash; GetRandBytes((uint8_t *)&hash, sizeof(hash)); return hash; } void FastRandomContext::RandomSeed() { uint256 seed = GetRandHash(); rng.SetKey(seed.begin(), 32); requires_seed = false; } uint256 FastRandomContext::rand256() noexcept { if (bytebuf_size < 32) { FillByteBuffer(); } uint256 ret; memcpy(ret.begin(), bytebuf + 64 - bytebuf_size, 32); bytebuf_size -= 32; return ret; } std::vector FastRandomContext::randbytes(size_t len) { if (requires_seed) { RandomSeed(); } std::vector ret(len); if (len > 0) { rng.Output(&ret[0], len); } return ret; } FastRandomContext::FastRandomContext(const uint256 &seed) noexcept : requires_seed(false), bytebuf_size(0), bitbuf_size(0) { rng.SetKey(seed.begin(), 32); } bool Random_SanityCheck() { uint64_t start = GetPerformanceCounter(); /** * This does not measure the quality of randomness, but it does test that * OSRandom() overwrites all 32 bytes of the output given a maximum number * of tries. */ static const ssize_t MAX_TRIES = 1024; uint8_t data[NUM_OS_RANDOM_BYTES]; /* Tracks which bytes have been overwritten at least once */ bool overwritten[NUM_OS_RANDOM_BYTES] = {}; int num_overwritten; int tries = 0; /** * Loop until all bytes have been overwritten at least once, or max number * tries reached. */ do { memset(data, 0, NUM_OS_RANDOM_BYTES); GetOSRand(data); for (int x = 0; x < NUM_OS_RANDOM_BYTES; ++x) { overwritten[x] |= (data[x] != 0); } num_overwritten = 0; for (int x = 0; x < NUM_OS_RANDOM_BYTES; ++x) { if (overwritten[x]) { num_overwritten += 1; } } tries += 1; } while (num_overwritten < NUM_OS_RANDOM_BYTES && tries < MAX_TRIES); /* If this failed, bailed out after too many tries */ if (num_overwritten != NUM_OS_RANDOM_BYTES) { return false; } // Check that GetPerformanceCounter increases at least during a GetOSRand() // call + 1ms sleep. std::this_thread::sleep_for(std::chrono::milliseconds(1)); uint64_t stop = GetPerformanceCounter(); if (stop == start) { return false; } // We called GetPerformanceCounter. Use it as entropy. CSHA512 to_add; to_add.Write((const uint8_t *)&start, sizeof(start)); to_add.Write((const uint8_t *)&stop, sizeof(stop)); GetRNGState().MixExtract(nullptr, 0, std::move(to_add), false); return true; } FastRandomContext::FastRandomContext(bool fDeterministic) noexcept : requires_seed(!fDeterministic), bytebuf_size(0), bitbuf_size(0) { if (!fDeterministic) { return; } uint256 seed; rng.SetKey(seed.begin(), 32); } FastRandomContext &FastRandomContext:: operator=(FastRandomContext &&from) noexcept { requires_seed = from.requires_seed; rng = from.rng; std::copy(std::begin(from.bytebuf), std::end(from.bytebuf), std::begin(bytebuf)); bytebuf_size = from.bytebuf_size; bitbuf = from.bitbuf; bitbuf_size = from.bitbuf_size; from.requires_seed = true; from.bytebuf_size = 0; from.bitbuf_size = 0; return *this; } void RandomInit() { // Invoke RNG code to trigger initialization (if not already performed) ProcRand(nullptr, 0, RNGLevel::FAST); ReportHardwareRand(); } diff --git a/src/test/bloom_tests.cpp b/src/test/bloom_tests.cpp index a1d4b0206..57190a582 100644 --- a/src/test/bloom_tests.cpp +++ b/src/test/bloom_tests.cpp @@ -1,1269 +1,1265 @@ // Copyright (c) 2012-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include BOOST_FIXTURE_TEST_SUITE(bloom_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize) { CBloomFilter filter(3, 0.01, 0, BLOOM_UPDATE_ALL); filter.insert(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")); BOOST_CHECK_MESSAGE( filter.contains(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")), "Bloom filter doesn't contain just-inserted object!"); // One bit different in first byte BOOST_CHECK_MESSAGE( !filter.contains(ParseHex("19108ad8ed9bb6274d3980bab5a85c048f0950c8")), "Bloom filter contains something it shouldn't!"); filter.insert(ParseHex("b5a2c786d9ef4658287ced5914b37a1b4aa32eee")); BOOST_CHECK_MESSAGE( filter.contains(ParseHex("b5a2c786d9ef4658287ced5914b37a1b4aa32eee")), "Bloom filter doesn't contain just-inserted object (2)!"); filter.insert(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")); BOOST_CHECK_MESSAGE( filter.contains(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")), "Bloom filter doesn't contain just-inserted object (3)!"); CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); stream << filter; std::vector vch = ParseHex("03614e9b050000000000000001"); std::vector expected(vch.size()); 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()); BOOST_CHECK_MESSAGE( filter.contains(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")), "Bloom filter doesn't contain just-inserted object!"); filter.clear(); BOOST_CHECK_MESSAGE( !filter.contains(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")), "Bloom filter should be empty!"); } BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize_with_tweak) { // Same test as bloom_create_insert_serialize, but we add a nTweak of 100 CBloomFilter filter(3, 0.01, 2147483649UL, BLOOM_UPDATE_ALL); filter.insert(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")); BOOST_CHECK_MESSAGE( filter.contains(ParseHex("99108ad8ed9bb6274d3980bab5a85c048f0950c8")), "Bloom filter doesn't contain just-inserted object!"); // One bit different in first byte BOOST_CHECK_MESSAGE( !filter.contains(ParseHex("19108ad8ed9bb6274d3980bab5a85c048f0950c8")), "Bloom filter contains something it shouldn't!"); filter.insert(ParseHex("b5a2c786d9ef4658287ced5914b37a1b4aa32eee")); BOOST_CHECK_MESSAGE( filter.contains(ParseHex("b5a2c786d9ef4658287ced5914b37a1b4aa32eee")), "Bloom filter doesn't contain just-inserted object (2)!"); filter.insert(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")); BOOST_CHECK_MESSAGE( filter.contains(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")), "Bloom filter doesn't contain just-inserted object (3)!"); CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); stream << filter; std::vector vch = ParseHex("03ce4299050000000100008001"); std::vector expected(vch.size()); 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()); } BOOST_AUTO_TEST_CASE(bloom_create_insert_key) { std::string strSecret = std::string("5Kg1gnAjaLfKiwhhPpGS3QfRg2m6awQvaj98JCZBZQ5SuS2F15C"); CKey key = DecodeSecret(strSecret); CPubKey pubkey = key.GetPubKey(); std::vector vchPubKey(pubkey.begin(), pubkey.end()); CBloomFilter filter(2, 0.001, 0, BLOOM_UPDATE_ALL); filter.insert(vchPubKey); uint160 hash = pubkey.GetID(); filter.insert(std::vector(hash.begin(), hash.end())); CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); stream << filter; std::vector vch = ParseHex("038fc16b080000000000000001"); std::vector expected(vch.size()); 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()); } BOOST_AUTO_TEST_CASE(bloom_match) { // Random real transaction // (b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b) CDataStream stream( ParseHex("01000000010b26e9b7735eb6aabdf358bab62f9816a21ba9ebdb719d5299e" "88607d722c190000000008b4830450220070aca44506c5cef3a16ed519d7c" "3c39f8aab192c4e1c90d065f37b8a4af6141022100a8e160b856c2d43d27d" "8fba71e5aef6405b8643ac4cb7cb3c462aced7f14711a0141046d11fee51b" "0e60666d5049a9101a72741df480b96ee26488a4d3466b95c9a40ac5eeef8" "7e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe76036c339ffff" "ffff021bff3d11000000001976a91404943fdd508053c75000106d3bc6e27" "54dbcff1988ac2f15de00000000001976a914a266436d2965547608b9e15d" "9032a7b9d64fa43188ac00000000"), SER_DISK, CLIENT_VERSION); CTransaction tx(deserialize, stream); // and one which spends it // (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436) uint8_t ch[] = { 0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00}; std::vector vch(ch, ch + sizeof(ch) - 1); CDataStream spendStream(vch, SER_DISK, CLIENT_VERSION); CTransaction spendingTx(deserialize, spendStream); CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); filter.insert(uint256S( "0xb4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b")); BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match tx hash"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // byte-reversed tx hash filter.insert(ParseHex( "6bff7fcd4f8565ef406dd5d63d4ff94f318fe82027fd4dc451b04474019f74b4")); BOOST_CHECK_MESSAGE( filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match manually serialized tx hash"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); filter.insert(ParseHex("30450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e" "1c90d065f37b8a4af6141022100a8e160b856c2d43d27d8fba7" "1e5aef6405b8643ac4cb7cb3c462aced7f14711a01")); BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match input signature"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); filter.insert(ParseHex("046d11fee51b0e60666d5049a9101a72741df480b96ee26488a" "4d3466b95c9a40ac5eeef87e10a5cd336c19a84565f80fa6c54" "7957b7700ff4dfbdefe76036c339")); BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match input pub key"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); filter.insert(ParseHex("04943fdd508053c75000106d3bc6e2754dbcff19")); BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match output address"); BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(spendingTx), "Simple Bloom filter didn't add output"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); filter.insert(ParseHex("04943fdd508053c75000106d3bc6e2754dbcff19")); BOOST_CHECK_MESSAGE(filter.MatchAndInsertOutputs(tx), "Simple Bloom filter didn't match output address"); BOOST_CHECK_MESSAGE(!filter.MatchAndInsertOutputs(spendingTx), "Simple Bloom filter matched unrelated output"); BOOST_CHECK_MESSAGE(filter.MatchInputs(spendingTx), "Simple Bloom filter didn't add output"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); filter.insert(ParseHex("a266436d2965547608b9e15d9032a7b9d64fa431")); BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match output address"); const TxId txid(uint256S( "0x90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b")); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); filter.insert(COutPoint(txid, 0)); BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match COutPoint"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); COutPoint prevOutPoint(txid, 0); { std::vector data(32 + sizeof(uint32_t)); memcpy(data.data(), prevOutPoint.GetTxId().begin(), 32); uint32_t n = prevOutPoint.GetN(); memcpy(data.data() + 32, &n, sizeof(uint32_t)); filter.insert(data); } BOOST_CHECK_MESSAGE( filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match manually serialized COutPoint"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); filter.insert(uint256S( "00000009e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436")); BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched random tx hash"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); filter.insert(ParseHex("0000006d2965547608b9e15d9032a7b9d64fa431")); BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched random address"); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); filter.insert(COutPoint(txid, 1)); BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched COutPoint for an output " "we didn't care about"); const TxId lowtxid(uint256S( "0x000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b")); filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL); filter.insert(COutPoint(lowtxid, 0)); BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched COutPoint for an output " "we didn't care about"); } BOOST_AUTO_TEST_CASE(merkle_block_1) { // Random real block // (0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af) // With 9 txes CBlock block; CDataStream stream( ParseHex( "0100000090f0a9f110702f808219ebea1173056042a714bad51b916cb680000000" "0000005275289558f51c9966699404ae2294730c3c9f9bda53523ce50e9b95e558" "da2fdb261b4d4c86041b1ab1bf9309010000000100000000000000000000000000" "00000000000000000000000000000000000000ffffffff07044c86041b0146ffff" "ffff0100f2052a01000000434104e18f7afbe4721580e81e8414fc8c24d7cfacf2" "54bb5c7b949450c3e997c2dc1242487a8169507b631eb3771f2b425483fb13102c" "4eb5d858eef260fe70fbfae0ac00000000010000000196608ccbafa16abada9027" "80da4dc35dafd7af05fa0da08cf833575f8cf9e836000000004a493046022100da" "b24889213caf43ae6adc41cf1c9396c08240c199f5225acf45416330fd7dbd0221" "00fe37900e0644bf574493a07fc5edba06dbc07c311b947520c2d514bc5725dcb4" "01ffffffff0100f2052a010000001976a914f15d1921f52e4007b146dfa60f369e" "d2fc393ce288ac000000000100000001fb766c1288458c2bafcfec81e48b24d98e" "c706de6b8af7c4e3c29419bfacb56d000000008c493046022100f268ba165ce0ad" "2e6d93f089cfcd3785de5c963bb5ea6b8c1b23f1ce3e517b9f022100da7c0f21ad" "c6c401887f2bfd1922f11d76159cbc597fbd756a23dcbb00f4d7290141042b4e86" "25a96127826915a5b109852636ad0da753c9e1d5606a50480cd0c40f1f8b8d8982" "35e571fe9357d9ec842bc4bba1827daaf4de06d71844d0057707966affffffff02" "80969800000000001976a9146963907531db72d0ed1a0cfb471ccb63923446f388" "ac80d6e34c000000001976a914f0688ba1c0d1ce182c7af6741e02658c7d4dfcd3" "88ac000000000100000002c40297f730dd7b5a99567eb8d27b78758f607507c522" "92d02d4031895b52f2ff010000008b483045022100f7edfd4b0aac404e5bab4fd3" "889e0c6c41aa8d0e6fa122316f68eddd0a65013902205b09cc8b2d56e1cd1f7f2f" "afd60a129ed94504c4ac7bdc67b56fe67512658b3e014104732012cb962afa90d3" "1b25d8fb0e32c94e513ab7a17805c14ca4c3423e18b4fb5d0e676841733cb83aba" "f975845c9f6f2a8097b7d04f4908b18368d6fc2d68ecffffffffca5065ff9617cb" "cba45eb23726df6498a9b9cafed4f54cbab9d227b0035ddefb000000008a473044" "022068010362a13c7f9919fa832b2dee4e788f61f6f5d344a7c2a0da6ae7406056" "58022006d1af525b9a14a35c003b78b72bd59738cd676f845d1ff3fc25049e0100" "3614014104732012cb962afa90d31b25d8fb0e32c94e513ab7a17805c14ca4c342" "3e18b4fb5d0e676841733cb83abaf975845c9f6f2a8097b7d04f4908b18368d6fc" "2d68ecffffffff01001ec4110200000043410469ab4181eceb28985b9b4e895c13" "fa5e68d85761b7eee311db5addef76fa8621865134a221bd01f28ec9999ee3e021" "e60766e9d1f3458c115fb28650605f11c9ac000000000100000001cdaf2f758e91" "c514655e2dc50633d1e4c84989f8aa90a0dbc883f0d23ed5c2fa010000008b4830" "4502207ab51be6f12a1962ba0aaaf24a20e0b69b27a94fac5adf45aa7d2d18ffd9" "236102210086ae728b370e5329eead9accd880d0cb070aea0c96255fae6c4f1ddc" "ce1fd56e014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d78990" "4f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8" "ebbb12dcd4ffffffff02404b4c00000000001976a9142b6ba7c9d796b75eef7942" "fc9288edd37c32f5c388ac002d3101000000001976a9141befba0cdc1ad5652937" "1864d9f6cb042faa06b588ac000000000100000001b4a47603e71b61bc3326efd9" "0111bf02d2f549b067f4c4a8fa183b57a0f800cb010000008a4730440220177c37" "f9a505c3f1a1f0ce2da777c339bd8339ffa02c7cb41f0a5804f473c9230220585b" "25a2ee80eb59292e52b987dad92acb0c64eced92ed9ee105ad153cdb12d0014104" "43bd44f683467e549dae7d20d1d79cbdb6df985c6e9c029c8d0c6cb46cc1a4d3cf" "7923c5021b27f7a0b562ada113bc85d5fda5a1b41e87fe6e8802817cf69996ffff" "ffff0280651406000000001976a9145505614859643ab7b547cd7f1f5e7e2a1232" "2d3788ac00aa0271000000001976a914ea4720a7a52fc166c55ff2298e07baf70a" "e67e1b88ac00000000010000000586c62cd602d219bb60edb14a3e204de0705176" "f9022fe49a538054fb14abb49e010000008c493046022100f2bc2aba2534becbdf" "062eb993853a42bbbc282083d0daf9b4b585bd401aa8c9022100b1d7fd7ee0b956" "00db8535bbf331b19eed8d961f7a8e54159c53675d5f69df8c014104462e76fd40" "67b3a0aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c6" "9b3dc31895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff03ad0e" "58ccdac3df9dc28a218bcf6f1997b0a93306faaa4b3a28ae83447b217901000000" "8b483045022100be12b2937179da88599e27bb31c3525097a07cdb52422d165b3c" "a2f2020ffcf702200971b51f853a53d644ebae9ec8f3512e442b1bcb6c315a5b49" "1d119d10624c83014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33" "d789904f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312e" "f1c0e8ebbb12dcd4ffffffff2acfcab629bbc8685792603762c921580030ba144a" "f553d271716a95089e107b010000008b483045022100fa579a840ac258871365dd" "48cd7552f96c8eea69bd00d84f05b283a0dab311e102207e3c0ee9234814cfbb1b" "659b83671618f45abc1326b9edcc77d552a4f2a805c0014104462e76fd4067b3a0" "aa42070082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc3" "1895d0c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffffdcdc6023bbc9" "944a658ddc588e61eacb737ddf0a3cd24f113b5a8634c517fcd2000000008b4830" "450221008d6df731df5d32267954bd7d2dda2302b74c6c2a6aa5c0ca64ecbabc1a" "f03c75022010e55c571d65da7701ae2da1956c442df81bbf076cdbac25133f99d9" "8a9ed34c014104462e76fd4067b3a0aa42070082dcb0bf2f388b6495cf33d78990" "4f07d0f55c40fbd4b82963c69b3dc31895d0c772c812b1d5fbcade15312ef1c0e8" "ebbb12dcd4ffffffffe15557cd5ce258f479dfd6dc6514edf6d7ed5b21fcfa4a03" "8fd69f06b83ac76e010000008b483045022023b3e0ab071eb11de2eb1cc3a67261" "b866f86bf6867d4558165f7c8c8aca2d86022100dc6e1f53a91de3efe8f6351285" "0811f26284b62f850c70ca73ed5de8771fb451014104462e76fd4067b3a0aa4207" "0082dcb0bf2f388b6495cf33d789904f07d0f55c40fbd4b82963c69b3dc31895d0" "c772c812b1d5fbcade15312ef1c0e8ebbb12dcd4ffffffff01404b4c0000000000" "1976a9142b6ba7c9d796b75eef7942fc9288edd37c32f5c388ac00000000010000" "000166d7577163c932b4f9690ca6a80b6e4eb001f0a2fa9023df5595602aae96ed" "8d000000008a4730440220262b42546302dfb654a229cefc86432b89628ff259dc" "87edd1154535b16a67e102207b4634c020a97c3e7bbd0d4d19da6aa2269ad9dded" "4026e896b213d73ca4b63f014104979b82d02226b3a4597523845754d44f13639e" "3bf2df5e82c6aab2bdc79687368b01b1ab8b19875ae3c90d661a3d0a33161dab29" "934edeb36aa01976be3baf8affffffff02404b4c00000000001976a9144854e695" "a02af0aeacb823ccbc272134561e0a1688ac40420f00000000001976a914abee93" "376d6b37b5c2940655a6fcaf1c8e74237988ac0000000001000000014e3f8ef2e9" "1349a9059cb4f01e54ab2597c1387161d3da89919f7ea6acdbb371010000008c49" "304602210081f3183471a5ca22307c0800226f3ef9c353069e0773ac76bb580654" "d56aa523022100d4c56465bdc069060846f4fbf2f6b20520b2a80b08b168b31e66" "ddb9c694e240014104976c79848e18251612f8940875b2b08d06e6dc73b9840e88" "60c066b7e87432c477e9a59a453e71e6d76d5fe34058b800a098fc1740ce3012e8" "fc8a00c96af966ffffffff02c0e1e400000000001976a9144134e75a6fcb604203" "4aab5e18570cf1f844f54788ac404b4c00000000001976a9142b6ba7c9d796b75e" "ef7942fc9288edd37c32f5c388ac00000000"), SER_NETWORK, PROTOCOL_VERSION); stream >> block; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // Match the last transaction filter.insert(uint256S( "0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); std::pair pair = merkleBlock.vMatchedTxn[0]; BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b" "586ec87451f20")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 8); std::vector vMatched; std::vector vIndex; BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); 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( "0xdd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f168809cdfae1053")); merkleBlock = CMerkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 2); BOOST_CHECK(merkleBlock.vMatchedTxn[1] == pair); BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0xdd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f16" "8809cdfae1053")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 7); BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); for (size_t i = 0; i < vMatched.size(); i++) { BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); } } BOOST_AUTO_TEST_CASE(merkle_block_2) { // Random real block // (000000005a4ded781e667e06ceefafb71410b511fe0d5adc3e5a27ecbec34ae6) // With 4 txes CBlock block; CDataStream stream( ParseHex( "0100000075616236cc2126035fadb38deb65b9102cc2c41c09cdf29fc051906800" "000000fe7d5e12ef0ff901f6050211249919b1c0653771832b3a80c66cea42847f" "0ae1d4d26e49ffff001d00f0a44104010000000100000000000000000000000000" "00000000000000000000000000000000000000ffffffff0804ffff001d029105ff" "ffffff0100f2052a010000004341046d8709a041d34357697dfcb30a9d05900a62" "94078012bf3bb09c6f9b525f1d16d5503d7905db1ada9501446ea00728668fc571" "9aa80be2fdfc8a858a4dbdd4fbac00000000010000000255605dc6f5c3dc148b6d" "a58442b0b2cd422be385eab2ebea4119ee9c268d28350000000049483045022100" "aa46504baa86df8a33b1192b1b9367b4d729dc41e389f2c04f3e5c7f0559aae702" "205e82253a54bf5c4f65b7428551554b2045167d6d206dfe6a2e198127d3f7df15" "01ffffffff55605dc6f5c3dc148b6da58442b0b2cd422be385eab2ebea4119ee9c" "268d2835010000004847304402202329484c35fa9d6bb32a55a70c0982f606ce0e" "3634b69006138683bcd12cbb6602200c28feb1e2555c3210f1dddb299738b4ff8b" "be9667b68cb8764b5ac17b7adf0001ffffffff0200e1f505000000004341046a07" "65b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a" "68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d" "8f000000004341044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad7" "69f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cf" "c617c0ea45afac0000000001000000025f9a06d3acdceb56be1bfeaa3e8a25e62d" "182fa24fefe899d1c17f1dad4c2028000000004847304402205d6058484157235b" "06028c30736c15613a28bdb768ee628094ca8b0030d4d6eb0220328789c9a2ec27" "ddaec0ad5ef58efded42e6ea17c2e1ce838f3d6913f5e95db601ffffffff5f9a06" "d3acdceb56be1bfeaa3e8a25e62d182fa24fefe899d1c17f1dad4c202801000000" "4a493046022100c45af050d3cea806cedd0ab22520c53ebe63b987b8954146cdca" "42487b84bdd6022100b9b027716a6b59e640da50a864d6dd8a0ef24c76ce62391f" "a3eabaf4d2886d2d01ffffffff0200e1f505000000004341046a0765b5865641ce" "08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d484" "8b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d8f0000000043" "41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef1" "70e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0c" "ac000000000100000002e2274e5fea1bf29d963914bd301aa63b64daaf8a3e88f1" "19b5046ca5738a0f6b0000000048473044022016e7a727a061ea2254a6c358376a" "aa617ac537eb836c77d646ebda4c748aac8b0220192ce28bf9f2c06a6467e6531e" "27648d2b3e2e2bae85159c9242939840295ba501ffffffffe2274e5fea1bf29d96" "3914bd301aa63b64daaf8a3e88f119b5046ca5738a0f6b010000004a4930460221" "00b7a1a755588d4190118936e15cd217d133b0e4a53c3c15924010d5648d8925c9" "022100aaef031874db2114f2d869ac2de4ae53908fbfea5b2b1862e181626bb900" "5c9f01ffffffff0200e1f505000000004341044a656f065871a353f216ca26cef8" "dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8d" "d2c875a390f67c1f6c94cfc617c0ea45afac00180d8f000000004341046a0765b5" "865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68ae" "e3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00000000"), SER_NETWORK, PROTOCOL_VERSION); stream >> block; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // Match the first transaction filter.insert(uint256S( "0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); std::pair pair = merkleBlock.vMatchedTxn[0]; BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df" "5b47aecb93b70")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 0); std::vector vMatched; std::vector vIndex; BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); 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) // This should match the third transaction because it spends the output // matched // It also matches the fourth transaction, which spends to the pubkey again filter.insert(ParseHex("044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad" "769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875" "a390f67c1f6c94cfc617c0ea45af")); merkleBlock = CMerkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 4); BOOST_CHECK(pair == merkleBlock.vMatchedTxn[0]); BOOST_CHECK(merkleBlock.vMatchedTxn[1].second == uint256S("0x28204cad1d7fc1d199e8ef4fa22f182de6258a3eaafe1bbe56e" "bdcacd3069a5f")); BOOST_CHECK(merkleBlock.vMatchedTxn[1].first == 1); BOOST_CHECK(merkleBlock.vMatchedTxn[2].second == uint256S("0x6b0f8a73a56c04b519f1883e8aafda643ba61a30bd1439969df" "21bea5f4e27e2")); BOOST_CHECK(merkleBlock.vMatchedTxn[2].first == 2); BOOST_CHECK(merkleBlock.vMatchedTxn[3].second == uint256S("0x3c1d7e82342158e4109df2e0b6348b6e84e403d8b4046d70076" "63ace63cddb23")); BOOST_CHECK(merkleBlock.vMatchedTxn[3].first == 3); BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); for (size_t i = 0; i < vMatched.size(); i++) { BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); } } BOOST_AUTO_TEST_CASE(merkle_block_2_reversed) { // Like merkle_block_2 except this block gets its transactions reversed in // order to check non-topological processing. // Random real block // (000000005a4ded781e667e06ceefafb71410b511fe0d5adc3e5a27ecbec34ae6) // With 4 txes CBlock block; CDataStream stream( ParseHex( "0100000075616236cc2126035fadb38deb65b9102cc2c41c09cdf29fc051906800" "000000fe7d5e12ef0ff901f6050211249919b1c0653771832b3a80c66cea42847f" "0ae1d4d26e49ffff001d00f0a44104010000000100000000000000000000000000" "00000000000000000000000000000000000000ffffffff0804ffff001d029105ff" "ffffff0100f2052a010000004341046d8709a041d34357697dfcb30a9d05900a62" "94078012bf3bb09c6f9b525f1d16d5503d7905db1ada9501446ea00728668fc571" "9aa80be2fdfc8a858a4dbdd4fbac00000000010000000255605dc6f5c3dc148b6d" "a58442b0b2cd422be385eab2ebea4119ee9c268d28350000000049483045022100" "aa46504baa86df8a33b1192b1b9367b4d729dc41e389f2c04f3e5c7f0559aae702" "205e82253a54bf5c4f65b7428551554b2045167d6d206dfe6a2e198127d3f7df15" "01ffffffff55605dc6f5c3dc148b6da58442b0b2cd422be385eab2ebea4119ee9c" "268d2835010000004847304402202329484c35fa9d6bb32a55a70c0982f606ce0e" "3634b69006138683bcd12cbb6602200c28feb1e2555c3210f1dddb299738b4ff8b" "be9667b68cb8764b5ac17b7adf0001ffffffff0200e1f505000000004341046a07" "65b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a" "68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d" "8f000000004341044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad7" "69f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cf" "c617c0ea45afac0000000001000000025f9a06d3acdceb56be1bfeaa3e8a25e62d" "182fa24fefe899d1c17f1dad4c2028000000004847304402205d6058484157235b" "06028c30736c15613a28bdb768ee628094ca8b0030d4d6eb0220328789c9a2ec27" "ddaec0ad5ef58efded42e6ea17c2e1ce838f3d6913f5e95db601ffffffff5f9a06" "d3acdceb56be1bfeaa3e8a25e62d182fa24fefe899d1c17f1dad4c202801000000" "4a493046022100c45af050d3cea806cedd0ab22520c53ebe63b987b8954146cdca" "42487b84bdd6022100b9b027716a6b59e640da50a864d6dd8a0ef24c76ce62391f" "a3eabaf4d2886d2d01ffffffff0200e1f505000000004341046a0765b5865641ce" "08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d484" "8b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d8f0000000043" "41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef1" "70e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0c" "ac000000000100000002e2274e5fea1bf29d963914bd301aa63b64daaf8a3e88f1" "19b5046ca5738a0f6b0000000048473044022016e7a727a061ea2254a6c358376a" "aa617ac537eb836c77d646ebda4c748aac8b0220192ce28bf9f2c06a6467e6531e" "27648d2b3e2e2bae85159c9242939840295ba501ffffffffe2274e5fea1bf29d96" "3914bd301aa63b64daaf8a3e88f119b5046ca5738a0f6b010000004a4930460221" "00b7a1a755588d4190118936e15cd217d133b0e4a53c3c15924010d5648d8925c9" "022100aaef031874db2114f2d869ac2de4ae53908fbfea5b2b1862e181626bb900" "5c9f01ffffffff0200e1f505000000004341044a656f065871a353f216ca26cef8" "dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8d" "d2c875a390f67c1f6c94cfc617c0ea45afac00180d8f000000004341046a0765b5" "865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68ae" "e3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00000000"), SER_NETWORK, PROTOCOL_VERSION); stream >> block; // Reverse the transactions and recalculate merkle root. The remainder of // this test is the same as merkle_block_2 above except the transaction // indices get reversed too. std::reverse(block.vtx.begin(), block.vtx.end()); block.hashMerkleRoot = BlockMerkleRoot(block); CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // Match the fourth (was first) transaction filter.insert(uint256S( "0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); std::pair pair = merkleBlock.vMatchedTxn[0]; BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df" "5b47aecb93b70")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 3); std::vector vMatched; std::vector vIndex; BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); for (size_t i = 0; i < vMatched.size(); i++) { BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second); } // Match an output from the third (was second) transaction (the pubkey for // address 1DZTzaBHUDM7T3QvUKBz4qXMRpkg8jsfB5) This should match the second // (was third) transaction because it spends the output matched // It also matches the first (was fourth) transaction, which spends to the // pubkey again filter.insert(ParseHex("044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad" "769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875" "a390f67c1f6c94cfc617c0ea45af")); merkleBlock = CMerkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 4); BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x3c1d7e82342158e4109df2e0b6348b6e84e403d8b4046d70076" "63ace63cddb23")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 0); BOOST_CHECK(merkleBlock.vMatchedTxn[1].second == uint256S("0x6b0f8a73a56c04b519f1883e8aafda643ba61a30bd1439969df" "21bea5f4e27e2")); BOOST_CHECK(merkleBlock.vMatchedTxn[1].first == 1); BOOST_CHECK(merkleBlock.vMatchedTxn[2].second == uint256S("0x28204cad1d7fc1d199e8ef4fa22f182de6258a3eaafe1bbe56e" "bdcacd3069a5f")); BOOST_CHECK(merkleBlock.vMatchedTxn[2].first == 2); BOOST_CHECK(pair == merkleBlock.vMatchedTxn[3]); BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); 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) { // Random real block // (000000005a4ded781e667e06ceefafb71410b511fe0d5adc3e5a27ecbec34ae6) // With 4 txes CBlock block; CDataStream stream( ParseHex( "0100000075616236cc2126035fadb38deb65b9102cc2c41c09cdf29fc051906800" "000000fe7d5e12ef0ff901f6050211249919b1c0653771832b3a80c66cea42847f" "0ae1d4d26e49ffff001d00f0a44104010000000100000000000000000000000000" "00000000000000000000000000000000000000ffffffff0804ffff001d029105ff" "ffffff0100f2052a010000004341046d8709a041d34357697dfcb30a9d05900a62" "94078012bf3bb09c6f9b525f1d16d5503d7905db1ada9501446ea00728668fc571" "9aa80be2fdfc8a858a4dbdd4fbac00000000010000000255605dc6f5c3dc148b6d" "a58442b0b2cd422be385eab2ebea4119ee9c268d28350000000049483045022100" "aa46504baa86df8a33b1192b1b9367b4d729dc41e389f2c04f3e5c7f0559aae702" "205e82253a54bf5c4f65b7428551554b2045167d6d206dfe6a2e198127d3f7df15" "01ffffffff55605dc6f5c3dc148b6da58442b0b2cd422be385eab2ebea4119ee9c" "268d2835010000004847304402202329484c35fa9d6bb32a55a70c0982f606ce0e" "3634b69006138683bcd12cbb6602200c28feb1e2555c3210f1dddb299738b4ff8b" "be9667b68cb8764b5ac17b7adf0001ffffffff0200e1f505000000004341046a07" "65b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a" "68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d" "8f000000004341044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad7" "69f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cf" "c617c0ea45afac0000000001000000025f9a06d3acdceb56be1bfeaa3e8a25e62d" "182fa24fefe899d1c17f1dad4c2028000000004847304402205d6058484157235b" "06028c30736c15613a28bdb768ee628094ca8b0030d4d6eb0220328789c9a2ec27" "ddaec0ad5ef58efded42e6ea17c2e1ce838f3d6913f5e95db601ffffffff5f9a06" "d3acdceb56be1bfeaa3e8a25e62d182fa24fefe899d1c17f1dad4c202801000000" "4a493046022100c45af050d3cea806cedd0ab22520c53ebe63b987b8954146cdca" "42487b84bdd6022100b9b027716a6b59e640da50a864d6dd8a0ef24c76ce62391f" "a3eabaf4d2886d2d01ffffffff0200e1f505000000004341046a0765b5865641ce" "08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d484" "8b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d8f0000000043" "41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef1" "70e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0c" "ac000000000100000002e2274e5fea1bf29d963914bd301aa63b64daaf8a3e88f1" "19b5046ca5738a0f6b0000000048473044022016e7a727a061ea2254a6c358376a" "aa617ac537eb836c77d646ebda4c748aac8b0220192ce28bf9f2c06a6467e6531e" "27648d2b3e2e2bae85159c9242939840295ba501ffffffffe2274e5fea1bf29d96" "3914bd301aa63b64daaf8a3e88f119b5046ca5738a0f6b010000004a4930460221" "00b7a1a755588d4190118936e15cd217d133b0e4a53c3c15924010d5648d8925c9" "022100aaef031874db2114f2d869ac2de4ae53908fbfea5b2b1862e181626bb900" "5c9f01ffffffff0200e1f505000000004341044a656f065871a353f216ca26cef8" "dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8d" "d2c875a390f67c1f6c94cfc617c0ea45afac00180d8f000000004341046a0765b5" "865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68ae" "e3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00000000"), SER_NETWORK, PROTOCOL_VERSION); stream >> block; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_NONE); // Match the first transaction filter.insert(uint256S( "0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); std::pair pair = merkleBlock.vMatchedTxn[0]; BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0xe980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df" "5b47aecb93b70")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 0); std::vector vMatched; std::vector vIndex; BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); 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) // This should not match the third transaction though it spends the output // matched // It will match the fourth transaction, which has another pay-to-pubkey // output to the same address filter.insert(ParseHex("044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad" "769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875" "a390f67c1f6c94cfc617c0ea45af")); merkleBlock = CMerkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 3); BOOST_CHECK(pair == merkleBlock.vMatchedTxn[0]); BOOST_CHECK(merkleBlock.vMatchedTxn[1].second == uint256S("0x28204cad1d7fc1d199e8ef4fa22f182de6258a3eaafe1bbe56e" "bdcacd3069a5f")); BOOST_CHECK(merkleBlock.vMatchedTxn[1].first == 1); BOOST_CHECK(merkleBlock.vMatchedTxn[2].second == uint256S("0x3c1d7e82342158e4109df2e0b6348b6e84e403d8b4046d70076" "63ace63cddb23")); BOOST_CHECK(merkleBlock.vMatchedTxn[2].first == 3); BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); 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) { // Random real block // (000000000000dab0130bbcc991d3d7ae6b81aa6f50a798888dfe62337458dc45) // With one tx CBlock block; CDataStream stream( ParseHex("0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b00" "00000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3f" "f60abe184f196367291b4d4c86041b8fa45d6301010000000100000000000" "00000000000000000000000000000000000000000000000000000ffffffff" "08044c86041b020a02ffffffff0100f2052a01000000434104ecd3229b057" "1c3be876feaac0442a9f13c5a572742927af1dc623353ecf8c202225f6486" "8137a18cdd85cbbb4c74fbccfd4f49639cf1bdc94a5672bb15ad5d4cac000" "00000"), SER_NETWORK, PROTOCOL_VERSION); stream >> block; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // Match the only transaction filter.insert(uint256S( "0x63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee" "3a3d669c00cb5")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 0); std::vector vMatched; std::vector vIndex; BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); 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; std::vector vch = ParseHex("0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b00" "00000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3f" "f60abe184f196367291b4d4c86041b8fa45d630100000001b50cc069d6a3e" "33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f19630101"); std::vector expected(vch.size()); 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()); } BOOST_AUTO_TEST_CASE(merkle_block_4) { // Random real block // (000000000000b731f2eef9e8c63173adfb07e41bd53eb0ef0a6b720d6cb6dea4) // With 7 txes CBlock block; CDataStream stream( ParseHex( "0100000082bb869cf3a793432a66e826e05a6fc37469f8efb7421dc88067010000" "0000007f16c5962e8bd963659c793ce370d95f093bc7e367117b3c30c1f8fdd0d9" "728776381b4d4c86041b554b852907010000000100000000000000000000000000" "00000000000000000000000000000000000000ffffffff07044c86041b0136ffff" "ffff0100f2052a01000000434104eaafc2314def4ca98ac970241bcab022b9c1e1" "f4ea423a20f134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad" "1357231a2252247d97a46a91ac000000000100000001bcad20a6a29827d1424f08" "989255120bf7f3e9e3cdaaa6bb31b0737fe048724300000000494830450220356e" "834b046cadc0f8ebb5a8a017b02de59c86305403dad52cd77b55af062ea1022100" "9253cd6c119d4729b77c978e1e2aa19f5ea6e0e52b3f16e32fa608cd5bab753901" "ffffffff02008d380c010000001976a9142b4b8072ecbba129b6453c63e129e643" "207249ca88ac0065cd1d000000001976a9141b8dd13b994bcfc787b32aeadf58cc" "b3615cbd5488ac000000000100000003fdacf9b3eb077412e7a968d2e4f11b9a9d" "ee312d666187ed77ee7d26af16cb0b000000008c493046022100ea1608e70911ca" "0de5af51ba57ad23b9a51db8d28f82c53563c56a05c20f5a87022100a8bdc8b4a8" "acc8634c6b420410150775eb7f2474f5615f7fccd65af30f310fbf01410465fdf4" "9e29b06b9a1582287b6279014f834edc317695d125ef623c1cc3aaece245bd69fc" "ad7508666e9c74a49dc9056d5fc14338ef38118dc4afae5fe2c585caffffffff30" "9e1913634ecb50f3c4f83e96e70b2df071b497b8973a3e75429df397b5af830000" "00004948304502202bdb79c596a9ffc24e96f4386199aba386e9bc7b6071516e2b" "51dda942b3a1ed022100c53a857e76b724fc14d45311eac5019650d415c3abb542" "8f3aae16d8e69bec2301ffffffff2089e33491695080c9edc18a428f7d834db5b6" "d372df13ce2b1b0e0cbcb1e6c10000000049483045022100d4ce67c5896ee251c8" "10ac1ff9ceccd328b497c8f553ab6e08431e7d40bad6b5022033119c0c2b7d792d" "31f1187779c7bd95aefd93d90a715586d73801d9b47471c601ffffffff01007144" "60030000001976a914c7b55141d097ea5df7a0ed330cf794376e53ec8d88ac0000" "000001000000045bf0e214aa4069a3e792ecee1e1bf0c1d397cde8dd08138f4b72" "a00681743447000000008b48304502200c45de8c4f3e2c1821f2fc878cba97b1e6" "f8807d94930713aa1c86a67b9bf1e40221008581abfef2e30f957815fc89978423" "746b2086375ca8ecf359c85c2a5b7c88ad01410462bb73f76ca0994fcb8b4271e6" "fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd62" "38f4d87270efb1d3ae37079b794a92d7ec95ffffffffd669f7d7958d40fc59d225" "3d88e0f248e29b599c80bbcec344a83dda5f9aa72c000000008a47304402207812" "4c8beeaa825f9e0b30bff96e564dd859432f2d0cb3b72d3d5d93d38d7e93022069" "1d233b6c0f995be5acb03d70a7f7a65b6bc9bdd426260f38a1346669507a360141" "0462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab8" "44c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ff" "fffffff878af0d93f5229a68166cf051fd372bb7a537232946e0a46f53636b4daf" "daa4000000008c493046022100c717d1714551663f69c3c5759bdbb3a0fcd3fab0" "23abc0e522fe6440de35d8290221008d9cbe25bffc44af2b18e81c58eb37293fd7" "fe1c2e7b46fc37ee8c96c50ab1e201410462bb73f76ca0994fcb8b4271e6fb7561" "f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d8" "7270efb1d3ae37079b794a92d7ec95ffffffff27f2b668859cd7f2f894aa0fd2d9" "e60963bcd07c88973f425f999b8cbfd7a1e2000000008c493046022100e0084714" "7cbf517bcc2f502f3ddc6d284358d102ed20d47a8aa788a62f0db780022100d17b" "2d6fa84dcaf1c95d88d7e7c30385aecf415588d749afd3ec81f6022cecd7014104" "62bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844" "c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffff" "ffff0100c817a8040000001976a914b6efd80d99179f4f4ff6f4dd0a007d018c38" "5d2188ac000000000100000001834537b2f1ce8ef9373a258e10545ce5a50b758d" "f616cd4356e0032554ebd3c4000000008b483045022100e68f422dd7c34fdce11e" "eb4509ddae38201773dd62f284e8aa9d96f85099d0b002202243bd399ff96b649a" "0fad05fa759d6a882f0af8c90cf7632c2840c29070aec20141045e58067e815c2f" "464c6a2a15f987758374203895710c2d452442e28496ff38ba8f5fd901dc20e29e" "88477167fe4fc299bf818fd0d9e1632d467b2a3d9503b1aaffffffff0280d7e636" "030000001976a914f34c3e10eb387efe872acb614c89e78bfca7815d88ac404b4c" "00000000001976a914a84e272933aaf87e1715d7786c51dfaeb5b65a6f88ac0000" "0000010000000143ac81c8e6f6ef307dfe17f3d906d999e23e0189fda838c5510d" "850927e03ae7000000008c4930460221009c87c344760a64cb8ae6685a3eec2c1a" "c1bed5b88c87de51acd0e124f266c16602210082d07c037359c3a257b5c63ebd90" "f5a5edf97b2ac1c434b08ca998839f346dd40141040ba7e521fa7946d12edbb1d1" "e95a15c34bd4398195e86433c92b431cd315f455fe30032ede69cad9d1e1ed6c3c" "4ec0dbfced53438c625462afb792dcb098544bffffffff0240420f000000000019" "76a9144676d1b820d63ec272f1900d59d43bc6463d96f888ac40420f0000000000" "1976a914648d04341d00d7968b3405c034adc38d4d8fb9bd88ac00000000010000" "000248cc917501ea5c55f4a8d2009c0567c40cfe037c2e71af017d0a452ff705e3" "f1000000008b483045022100bf5fdc86dc5f08a5d5c8e43a8c9d5b1ed8c65562e2" "80007b52b133021acd9acc02205e325d613e555f772802bf413d36ba807892ed1a" "690a77811d3033b3de226e0a01410429fa713b124484cb2bd7b5557b2c0b9df7b2" "b1fee61825eadc5ae6c37a9920d38bfccdc7dc3cb0c47d7b173dbc9db8d37db0a3" "3ae487982c59c6f8606e9d1791ffffffff41ed70551dd7e841883ab8f0b16bf041" "76b7d1480e4f0af9f3d4c3595768d068000000008b4830450221008513ad65187b" "903aed1102d1d0c47688127658c51106753fed0151ce9c16b80902201432b9ebcb" "87bd04ceb2de66035fbbaf4bf8b00d1cfe41f1a1f7338f9ad79d210141049d4cf8" "0125bf50be1709f718c07ad15d0fc612b7da1f5570dddc35f2a352f0f27c978b06" "820edca9ef982c35fda2d255afba340068c5035552368bc7200c1488ffffffff01" "00093d00000000001976a9148edb68822f1ad580b043c7b3df2e400f8699eb4888" "ac00000000"), SER_NETWORK, PROTOCOL_VERSION); stream >> block; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); // Match the last transaction filter.insert(uint256S( "0x0a2a92f0bda4727d0a13eaddf4dd9ac6b5c61a1429e6b2b818f19b15df0ac154")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1); std::pair pair = merkleBlock.vMatchedTxn[0]; BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x0a2a92f0bda4727d0a13eaddf4dd9ac6b5c61a1429e6b2b818f" "19b15df0ac154")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 6); std::vector vMatched; std::vector vIndex; BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); 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( "0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041")); merkleBlock = CMerkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 2); BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == uint256S("0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de673" "26471df5bc041")); BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 3); BOOST_CHECK(merkleBlock.vMatchedTxn[1] == pair); BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot); BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size()); 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) { // Random real block // (000000000000b731f2eef9e8c63173adfb07e41bd53eb0ef0a6b720d6cb6dea4) // With 7 txes CBlock block; CDataStream stream( ParseHex( "0100000082bb869cf3a793432a66e826e05a6fc37469f8efb7421dc88067010000" "0000007f16c5962e8bd963659c793ce370d95f093bc7e367117b3c30c1f8fdd0d9" "728776381b4d4c86041b554b852907010000000100000000000000000000000000" "00000000000000000000000000000000000000ffffffff07044c86041b0136ffff" "ffff0100f2052a01000000434104eaafc2314def4ca98ac970241bcab022b9c1e1" "f4ea423a20f134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad" "1357231a2252247d97a46a91ac000000000100000001bcad20a6a29827d1424f08" "989255120bf7f3e9e3cdaaa6bb31b0737fe048724300000000494830450220356e" "834b046cadc0f8ebb5a8a017b02de59c86305403dad52cd77b55af062ea1022100" "9253cd6c119d4729b77c978e1e2aa19f5ea6e0e52b3f16e32fa608cd5bab753901" "ffffffff02008d380c010000001976a9142b4b8072ecbba129b6453c63e129e643" "207249ca88ac0065cd1d000000001976a9141b8dd13b994bcfc787b32aeadf58cc" "b3615cbd5488ac000000000100000003fdacf9b3eb077412e7a968d2e4f11b9a9d" "ee312d666187ed77ee7d26af16cb0b000000008c493046022100ea1608e70911ca" "0de5af51ba57ad23b9a51db8d28f82c53563c56a05c20f5a87022100a8bdc8b4a8" "acc8634c6b420410150775eb7f2474f5615f7fccd65af30f310fbf01410465fdf4" "9e29b06b9a1582287b6279014f834edc317695d125ef623c1cc3aaece245bd69fc" "ad7508666e9c74a49dc9056d5fc14338ef38118dc4afae5fe2c585caffffffff30" "9e1913634ecb50f3c4f83e96e70b2df071b497b8973a3e75429df397b5af830000" "00004948304502202bdb79c596a9ffc24e96f4386199aba386e9bc7b6071516e2b" "51dda942b3a1ed022100c53a857e76b724fc14d45311eac5019650d415c3abb542" "8f3aae16d8e69bec2301ffffffff2089e33491695080c9edc18a428f7d834db5b6" "d372df13ce2b1b0e0cbcb1e6c10000000049483045022100d4ce67c5896ee251c8" "10ac1ff9ceccd328b497c8f553ab6e08431e7d40bad6b5022033119c0c2b7d792d" "31f1187779c7bd95aefd93d90a715586d73801d9b47471c601ffffffff01007144" "60030000001976a914c7b55141d097ea5df7a0ed330cf794376e53ec8d88ac0000" "000001000000045bf0e214aa4069a3e792ecee1e1bf0c1d397cde8dd08138f4b72" "a00681743447000000008b48304502200c45de8c4f3e2c1821f2fc878cba97b1e6" "f8807d94930713aa1c86a67b9bf1e40221008581abfef2e30f957815fc89978423" "746b2086375ca8ecf359c85c2a5b7c88ad01410462bb73f76ca0994fcb8b4271e6" "fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd62" "38f4d87270efb1d3ae37079b794a92d7ec95ffffffffd669f7d7958d40fc59d225" "3d88e0f248e29b599c80bbcec344a83dda5f9aa72c000000008a47304402207812" "4c8beeaa825f9e0b30bff96e564dd859432f2d0cb3b72d3d5d93d38d7e93022069" "1d233b6c0f995be5acb03d70a7f7a65b6bc9bdd426260f38a1346669507a360141" "0462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab8" "44c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ff" "fffffff878af0d93f5229a68166cf051fd372bb7a537232946e0a46f53636b4daf" "daa4000000008c493046022100c717d1714551663f69c3c5759bdbb3a0fcd3fab0" "23abc0e522fe6440de35d8290221008d9cbe25bffc44af2b18e81c58eb37293fd7" "fe1c2e7b46fc37ee8c96c50ab1e201410462bb73f76ca0994fcb8b4271e6fb7561" "f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d8" "7270efb1d3ae37079b794a92d7ec95ffffffff27f2b668859cd7f2f894aa0fd2d9" "e60963bcd07c88973f425f999b8cbfd7a1e2000000008c493046022100e0084714" "7cbf517bcc2f502f3ddc6d284358d102ed20d47a8aa788a62f0db780022100d17b" "2d6fa84dcaf1c95d88d7e7c30385aecf415588d749afd3ec81f6022cecd7014104" "62bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844" "c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffff" "ffff0100c817a8040000001976a914b6efd80d99179f4f4ff6f4dd0a007d018c38" "5d2188ac000000000100000001834537b2f1ce8ef9373a258e10545ce5a50b758d" "f616cd4356e0032554ebd3c4000000008b483045022100e68f422dd7c34fdce11e" "eb4509ddae38201773dd62f284e8aa9d96f85099d0b002202243bd399ff96b649a" "0fad05fa759d6a882f0af8c90cf7632c2840c29070aec20141045e58067e815c2f" "464c6a2a15f987758374203895710c2d452442e28496ff38ba8f5fd901dc20e29e" "88477167fe4fc299bf818fd0d9e1632d467b2a3d9503b1aaffffffff0280d7e636" "030000001976a914f34c3e10eb387efe872acb614c89e78bfca7815d88ac404b4c" "00000000001976a914a84e272933aaf87e1715d7786c51dfaeb5b65a6f88ac0000" "0000010000000143ac81c8e6f6ef307dfe17f3d906d999e23e0189fda838c5510d" "850927e03ae7000000008c4930460221009c87c344760a64cb8ae6685a3eec2c1a" "c1bed5b88c87de51acd0e124f266c16602210082d07c037359c3a257b5c63ebd90" "f5a5edf97b2ac1c434b08ca998839f346dd40141040ba7e521fa7946d12edbb1d1" "e95a15c34bd4398195e86433c92b431cd315f455fe30032ede69cad9d1e1ed6c3c" "4ec0dbfced53438c625462afb792dcb098544bffffffff0240420f000000000019" "76a9144676d1b820d63ec272f1900d59d43bc6463d96f888ac40420f0000000000" "1976a914648d04341d00d7968b3405c034adc38d4d8fb9bd88ac00000000010000" "000248cc917501ea5c55f4a8d2009c0567c40cfe037c2e71af017d0a452ff705e3" "f1000000008b483045022100bf5fdc86dc5f08a5d5c8e43a8c9d5b1ed8c65562e2" "80007b52b133021acd9acc02205e325d613e555f772802bf413d36ba807892ed1a" "690a77811d3033b3de226e0a01410429fa713b124484cb2bd7b5557b2c0b9df7b2" "b1fee61825eadc5ae6c37a9920d38bfccdc7dc3cb0c47d7b173dbc9db8d37db0a3" "3ae487982c59c6f8606e9d1791ffffffff41ed70551dd7e841883ab8f0b16bf041" "76b7d1480e4f0af9f3d4c3595768d068000000008b4830450221008513ad65187b" "903aed1102d1d0c47688127658c51106753fed0151ce9c16b80902201432b9ebcb" "87bd04ceb2de66035fbbaf4bf8b00d1cfe41f1a1f7338f9ad79d210141049d4cf8" "0125bf50be1709f718c07ad15d0fc612b7da1f5570dddc35f2a352f0f27c978b06" "820edca9ef982c35fda2d255afba340068c5035552368bc7200c1488ffffffff01" "00093d00000000001976a9148edb68822f1ad580b043c7b3df2e400f8699eb4888" "ac00000000"), SER_NETWORK, PROTOCOL_VERSION); stream >> block; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_P2PUBKEY_ONLY); // Match the generation pubkey filter.insert(ParseHex("04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f" "134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce" "13ad1357231a2252247d97a46a91")); // ...and the output address of the 4th transaction filter.insert(ParseHex("b6efd80d99179f4f4ff6f4dd0a007d018c385d21")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); // We should match the generation outpoint const TxId txid1(uint256S( "0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b")); BOOST_CHECK(filter.contains(COutPoint(txid1, 0))); // ... but not the 4th transaction's output (its not pay-2-pubkey) const TxId txid2(uint256S( "0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041")); BOOST_CHECK(!filter.contains(COutPoint(txid2, 0))); } BOOST_AUTO_TEST_CASE(merkle_block_4_test_update_none) { // Random real block // (000000000000b731f2eef9e8c63173adfb07e41bd53eb0ef0a6b720d6cb6dea4) // With 7 txes CBlock block; CDataStream stream( ParseHex( "0100000082bb869cf3a793432a66e826e05a6fc37469f8efb7421dc88067010000" "0000007f16c5962e8bd963659c793ce370d95f093bc7e367117b3c30c1f8fdd0d9" "728776381b4d4c86041b554b852907010000000100000000000000000000000000" "00000000000000000000000000000000000000ffffffff07044c86041b0136ffff" "ffff0100f2052a01000000434104eaafc2314def4ca98ac970241bcab022b9c1e1" "f4ea423a20f134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad" "1357231a2252247d97a46a91ac000000000100000001bcad20a6a29827d1424f08" "989255120bf7f3e9e3cdaaa6bb31b0737fe048724300000000494830450220356e" "834b046cadc0f8ebb5a8a017b02de59c86305403dad52cd77b55af062ea1022100" "9253cd6c119d4729b77c978e1e2aa19f5ea6e0e52b3f16e32fa608cd5bab753901" "ffffffff02008d380c010000001976a9142b4b8072ecbba129b6453c63e129e643" "207249ca88ac0065cd1d000000001976a9141b8dd13b994bcfc787b32aeadf58cc" "b3615cbd5488ac000000000100000003fdacf9b3eb077412e7a968d2e4f11b9a9d" "ee312d666187ed77ee7d26af16cb0b000000008c493046022100ea1608e70911ca" "0de5af51ba57ad23b9a51db8d28f82c53563c56a05c20f5a87022100a8bdc8b4a8" "acc8634c6b420410150775eb7f2474f5615f7fccd65af30f310fbf01410465fdf4" "9e29b06b9a1582287b6279014f834edc317695d125ef623c1cc3aaece245bd69fc" "ad7508666e9c74a49dc9056d5fc14338ef38118dc4afae5fe2c585caffffffff30" "9e1913634ecb50f3c4f83e96e70b2df071b497b8973a3e75429df397b5af830000" "00004948304502202bdb79c596a9ffc24e96f4386199aba386e9bc7b6071516e2b" "51dda942b3a1ed022100c53a857e76b724fc14d45311eac5019650d415c3abb542" "8f3aae16d8e69bec2301ffffffff2089e33491695080c9edc18a428f7d834db5b6" "d372df13ce2b1b0e0cbcb1e6c10000000049483045022100d4ce67c5896ee251c8" "10ac1ff9ceccd328b497c8f553ab6e08431e7d40bad6b5022033119c0c2b7d792d" "31f1187779c7bd95aefd93d90a715586d73801d9b47471c601ffffffff01007144" "60030000001976a914c7b55141d097ea5df7a0ed330cf794376e53ec8d88ac0000" "000001000000045bf0e214aa4069a3e792ecee1e1bf0c1d397cde8dd08138f4b72" "a00681743447000000008b48304502200c45de8c4f3e2c1821f2fc878cba97b1e6" "f8807d94930713aa1c86a67b9bf1e40221008581abfef2e30f957815fc89978423" "746b2086375ca8ecf359c85c2a5b7c88ad01410462bb73f76ca0994fcb8b4271e6" "fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd62" "38f4d87270efb1d3ae37079b794a92d7ec95ffffffffd669f7d7958d40fc59d225" "3d88e0f248e29b599c80bbcec344a83dda5f9aa72c000000008a47304402207812" "4c8beeaa825f9e0b30bff96e564dd859432f2d0cb3b72d3d5d93d38d7e93022069" "1d233b6c0f995be5acb03d70a7f7a65b6bc9bdd426260f38a1346669507a360141" "0462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab8" "44c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ff" "fffffff878af0d93f5229a68166cf051fd372bb7a537232946e0a46f53636b4daf" "daa4000000008c493046022100c717d1714551663f69c3c5759bdbb3a0fcd3fab0" "23abc0e522fe6440de35d8290221008d9cbe25bffc44af2b18e81c58eb37293fd7" "fe1c2e7b46fc37ee8c96c50ab1e201410462bb73f76ca0994fcb8b4271e6fb7561" "f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d8" "7270efb1d3ae37079b794a92d7ec95ffffffff27f2b668859cd7f2f894aa0fd2d9" "e60963bcd07c88973f425f999b8cbfd7a1e2000000008c493046022100e0084714" "7cbf517bcc2f502f3ddc6d284358d102ed20d47a8aa788a62f0db780022100d17b" "2d6fa84dcaf1c95d88d7e7c30385aecf415588d749afd3ec81f6022cecd7014104" "62bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6485261c4a0dc894f4ab844" "c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffff" "ffff0100c817a8040000001976a914b6efd80d99179f4f4ff6f4dd0a007d018c38" "5d2188ac000000000100000001834537b2f1ce8ef9373a258e10545ce5a50b758d" "f616cd4356e0032554ebd3c4000000008b483045022100e68f422dd7c34fdce11e" "eb4509ddae38201773dd62f284e8aa9d96f85099d0b002202243bd399ff96b649a" "0fad05fa759d6a882f0af8c90cf7632c2840c29070aec20141045e58067e815c2f" "464c6a2a15f987758374203895710c2d452442e28496ff38ba8f5fd901dc20e29e" "88477167fe4fc299bf818fd0d9e1632d467b2a3d9503b1aaffffffff0280d7e636" "030000001976a914f34c3e10eb387efe872acb614c89e78bfca7815d88ac404b4c" "00000000001976a914a84e272933aaf87e1715d7786c51dfaeb5b65a6f88ac0000" "0000010000000143ac81c8e6f6ef307dfe17f3d906d999e23e0189fda838c5510d" "850927e03ae7000000008c4930460221009c87c344760a64cb8ae6685a3eec2c1a" "c1bed5b88c87de51acd0e124f266c16602210082d07c037359c3a257b5c63ebd90" "f5a5edf97b2ac1c434b08ca998839f346dd40141040ba7e521fa7946d12edbb1d1" "e95a15c34bd4398195e86433c92b431cd315f455fe30032ede69cad9d1e1ed6c3c" "4ec0dbfced53438c625462afb792dcb098544bffffffff0240420f000000000019" "76a9144676d1b820d63ec272f1900d59d43bc6463d96f888ac40420f0000000000" "1976a914648d04341d00d7968b3405c034adc38d4d8fb9bd88ac00000000010000" "000248cc917501ea5c55f4a8d2009c0567c40cfe037c2e71af017d0a452ff705e3" "f1000000008b483045022100bf5fdc86dc5f08a5d5c8e43a8c9d5b1ed8c65562e2" "80007b52b133021acd9acc02205e325d613e555f772802bf413d36ba807892ed1a" "690a77811d3033b3de226e0a01410429fa713b124484cb2bd7b5557b2c0b9df7b2" "b1fee61825eadc5ae6c37a9920d38bfccdc7dc3cb0c47d7b173dbc9db8d37db0a3" "3ae487982c59c6f8606e9d1791ffffffff41ed70551dd7e841883ab8f0b16bf041" "76b7d1480e4f0af9f3d4c3595768d068000000008b4830450221008513ad65187b" "903aed1102d1d0c47688127658c51106753fed0151ce9c16b80902201432b9ebcb" "87bd04ceb2de66035fbbaf4bf8b00d1cfe41f1a1f7338f9ad79d210141049d4cf8" "0125bf50be1709f718c07ad15d0fc612b7da1f5570dddc35f2a352f0f27c978b06" "820edca9ef982c35fda2d255afba340068c5035552368bc7200c1488ffffffff01" "00093d00000000001976a9148edb68822f1ad580b043c7b3df2e400f8699eb4888" "ac00000000"), SER_NETWORK, PROTOCOL_VERSION); stream >> block; CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_NONE); // Match the generation pubkey filter.insert(ParseHex("04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f" "134c876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce" "13ad1357231a2252247d97a46a91")); // ...and the output address of the 4th transaction filter.insert(ParseHex("b6efd80d99179f4f4ff6f4dd0a007d018c385d21")); CMerkleBlock merkleBlock(block, filter); BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash()); // We shouldn't match any outpoints (UPDATE_NONE) const TxId txid1(uint256S( "0x147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b")); BOOST_CHECK(!filter.contains(COutPoint(txid1, 0))); const TxId txid2(uint256S( "0x02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041")); BOOST_CHECK(!filter.contains(COutPoint(txid2, 0))); } static std::vector RandomData() { uint256 r = InsecureRand256(); return std::vector(r.begin(), r.end()); } BOOST_AUTO_TEST_CASE(rolling_bloom) { + SeedInsecureRand(/* deterministic */ true); + g_mock_deterministic_tests = true; + // last-100-entry, 1% false positive: CRollingBloomFilter rb1(100, 0.01); // Overfill: static const int DATASIZE = 399; std::vector data[DATASIZE]; for (int i = 0; i < DATASIZE; i++) { data[i] = RandomData(); rb1.insert(data[i]); } // Last 100 guaranteed to be remembered: for (int i = 299; i < DATASIZE; i++) { BOOST_CHECK(rb1.contains(data[i])); } // false positive rate is 1%, so we should get about 100 hits if // testing 10,000 random keys. We get worst-case false positive // behavior when the filter is as full as possible, which is // when we've inserted one minus an integer multiple of nElement*2. unsigned int nHits = 0; for (int i = 0; i < 10000; i++) { if (rb1.contains(RandomData())) ++nHits; } - // Run test_bitcoin with --log_level=message to see BOOST_TEST_MESSAGEs: - BOOST_TEST_MESSAGE("RollingBloomFilter got " - << nHits << " false positives (~100 expected)"); - - // Insanely unlikely to get a fp count outside this range: - BOOST_CHECK(nHits > 25); - BOOST_CHECK(nHits < 175); + // Expect about 100 hits + BOOST_CHECK_EQUAL(nHits, 75); BOOST_CHECK(rb1.contains(data[DATASIZE - 1])); rb1.reset(); BOOST_CHECK(!rb1.contains(data[DATASIZE - 1])); // Now roll through data, make sure last 100 entries // are always remembered: for (int i = 0; i < DATASIZE; i++) { if (i >= 100) BOOST_CHECK(rb1.contains(data[i - 100])); rb1.insert(data[i]); BOOST_CHECK(rb1.contains(data[i])); } // Insert 999 more random entries: for (int i = 0; i < 999; i++) { std::vector d = RandomData(); rb1.insert(d); BOOST_CHECK(rb1.contains(d)); } // Sanity check to make sure the filter isn't just filling up: nHits = 0; for (int i = 0; i < DATASIZE; i++) { if (rb1.contains(data[i])) ++nHits; } - // Expect about 5 false positives, more than 100 means - // something is definitely broken. - BOOST_TEST_MESSAGE("RollingBloomFilter got " - << nHits << " false positives (~5 expected)"); - BOOST_CHECK(nHits < 100); + // Expect about 5 false positives + BOOST_CHECK_EQUAL(nHits, 6); // last-1000-entry, 0.01% false positive: CRollingBloomFilter rb2(1000, 0.001); for (int i = 0; i < DATASIZE; i++) { rb2.insert(data[i]); } // ... room for all of them: for (int i = 0; i < DATASIZE; i++) { BOOST_CHECK(rb2.contains(data[i])); } + g_mock_deterministic_tests = false; } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/random_tests.cpp b/src/test/random_tests.cpp index 9d7c1a871..5316718c3 100644 --- a/src/test/random_tests.cpp +++ b/src/test/random_tests.cpp @@ -1,121 +1,135 @@ // Copyright (c) 2017 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include BOOST_FIXTURE_TEST_SUITE(random_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(osrandom_tests) { BOOST_CHECK(Random_SanityCheck()); } BOOST_AUTO_TEST_CASE(fastrandom_tests) { // Check that deterministic FastRandomContexts are deterministic + g_mock_deterministic_tests = true; FastRandomContext ctx1(true); FastRandomContext ctx2(true); + for (int i = 10; i > 0; --i) { + BOOST_CHECK_EQUAL(GetRand(std::numeric_limits::max()), + uint64_t{10393729187455219830U}); + BOOST_CHECK_EQUAL(GetRandInt(std::numeric_limits::max()), + int{769702006}); + } BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32()); BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32()); BOOST_CHECK_EQUAL(ctx1.rand64(), ctx2.rand64()); BOOST_CHECK_EQUAL(ctx1.randbits(3), ctx2.randbits(3)); BOOST_CHECK(ctx1.randbytes(17) == ctx2.randbytes(17)); BOOST_CHECK(ctx1.rand256() == ctx2.rand256()); BOOST_CHECK_EQUAL(ctx1.randbits(7), ctx2.randbits(7)); BOOST_CHECK(ctx1.randbytes(128) == ctx2.randbytes(128)); BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32()); BOOST_CHECK_EQUAL(ctx1.randbits(3), ctx2.randbits(3)); BOOST_CHECK(ctx1.rand256() == ctx2.rand256()); BOOST_CHECK(ctx1.randbytes(50) == ctx2.randbytes(50)); // Check that a nondeterministic ones are not + g_mock_deterministic_tests = false; + for (int i = 10; i > 0; --i) { + BOOST_CHECK(GetRand(std::numeric_limits::max()) != + uint64_t{10393729187455219830U}); + BOOST_CHECK(GetRandInt(std::numeric_limits::max()) != + int{769702006}); + } { FastRandomContext ctx3, ctx4; // extremely unlikely to be equal BOOST_CHECK(ctx3.rand64() != ctx4.rand64()); } { FastRandomContext ctx3, ctx4; BOOST_CHECK(ctx3.rand256() != ctx4.rand256()); } { FastRandomContext ctx3, ctx4; BOOST_CHECK(ctx3.randbytes(7) != ctx4.randbytes(7)); } } BOOST_AUTO_TEST_CASE(fastrandom_randbits) { FastRandomContext ctx1; FastRandomContext ctx2; for (int bits = 0; bits < 63; ++bits) { for (int j = 0; j < 1000; ++j) { uint64_t rangebits = ctx1.randbits(bits); BOOST_CHECK_EQUAL(rangebits >> bits, 0U); uint64_t range = uint64_t(1) << bits | rangebits; uint64_t rand = ctx2.randrange(range); BOOST_CHECK(rand < range); } } } /** Does-it-compile test for compatibility with standard C++11 RNG interface. */ BOOST_AUTO_TEST_CASE(stdrandom_test) { FastRandomContext ctx; std::uniform_int_distribution distribution(3, 9); for (int i = 0; i < 100; ++i) { int x = distribution(ctx); BOOST_CHECK(x >= 3); BOOST_CHECK(x <= 9); std::vector test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; std::shuffle(test.begin(), test.end(), ctx); for (int j = 1; j <= 10; ++j) { BOOST_CHECK(std::find(test.begin(), test.end(), j) != test.end()); } Shuffle(test.begin(), test.end(), ctx); for (int j = 1; j <= 10; ++j) { BOOST_CHECK(std::find(test.begin(), test.end(), j) != test.end()); } } } /** Test that Shuffle reaches every permutation with equal probability. */ BOOST_AUTO_TEST_CASE(shuffle_stat_test) { FastRandomContext ctx(true); uint32_t counts[5 * 5 * 5 * 5 * 5] = {0}; for (int i = 0; i < 12000; ++i) { int data[5] = {0, 1, 2, 3, 4}; Shuffle(std::begin(data), std::end(data), ctx); int pos = data[0] + data[1] * 5 + data[2] * 25 + data[3] * 125 + data[4] * 625; ++counts[pos]; } unsigned int sum = 0; double chi_score = 0.0; for (int i = 0; i < 5 * 5 * 5 * 5 * 5; ++i) { int i1 = i % 5, i2 = (i / 5) % 5, i3 = (i / 25) % 5, i4 = (i / 125) % 5, i5 = i / 625; uint32_t count = counts[i]; if (i1 == i2 || i1 == i3 || i1 == i4 || i1 == i5 || i2 == i3 || i2 == i4 || i2 == i5 || i3 == i4 || i3 == i5 || i4 == i5) { BOOST_CHECK(count == 0); } else { chi_score += ((count - 100.0) * (count - 100.0)) / 100.0; BOOST_CHECK(count > 50); BOOST_CHECK(count < 150); sum += count; } } BOOST_CHECK(chi_score > 58.1411); // 99.9999% confidence interval BOOST_CHECK(chi_score < 210.275); BOOST_CHECK_EQUAL(sum, 12000); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/test_bitcoin.h b/src/test/test_bitcoin.h index 2155df6d2..a1a5212a6 100644 --- a/src/test/test_bitcoin.h +++ b/src/test/test_bitcoin.h @@ -1,165 +1,170 @@ // Copyright (c) 2015-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_TEST_TEST_BITCOIN_H #define BITCOIN_TEST_TEST_BITCOIN_H #include #include #include #include #include #include #include /** * Version of Boost::test prior to 1.64 have issues when dealing with nullptr_t. * In order to work around this, we ensure that the null pointers are typed in a * way that Boost will like better. * * TODO: Use nullptr directly once the minimum version of boost is 1.64 or more. */ #define NULLPTR(T) static_cast(nullptr) /** * This global and the helpers that use it are not thread-safe. * * If thread-safety is needed, the global could be made thread_local (given * that thread_local is supported on all architectures we support) or a * per-thread instance could be used in the multi-threaded test. */ extern FastRandomContext g_insecure_rand_ctx; +/** + * Flag to make GetRand in random.h return the same number + */ +extern bool g_mock_deterministic_tests; + static inline void SeedInsecureRand(bool deterministic = false) { g_insecure_rand_ctx = FastRandomContext(deterministic); } static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); } static inline uint256 InsecureRand256() { return g_insecure_rand_ctx.rand256(); } static inline uint64_t InsecureRandBits(int bits) { return g_insecure_rand_ctx.randbits(bits); } static inline uint64_t InsecureRandRange(uint64_t range) { return g_insecure_rand_ctx.randrange(range); } static inline bool InsecureRandBool() { return g_insecure_rand_ctx.randbool(); } /** * Basic testing setup. * This just configures logging and chain parameters. */ struct BasicTestingSetup { ECCVerifyHandle globalVerifyHandle; explicit BasicTestingSetup( const std::string &chainName = CBaseChainParams::MAIN); ~BasicTestingSetup(); fs::path SetDataDir(const std::string &name); private: const fs::path m_path_root; }; /** * Testing setup that configures a complete environment. * Included are data directory, coins database, script check threads setup. */ class CConnman; class CNode; class PeerLogicValidation; struct TestingSetup : public BasicTestingSetup { boost::thread_group threadGroup; CScheduler scheduler; explicit TestingSetup( const std::string &chainName = CBaseChainParams::MAIN); ~TestingSetup(); }; class CBlock; class CMutableTransaction; class CScript; // // Testing fixture that pre-creates a // 100-block REGTEST-mode block chain // struct TestChain100Setup : public TestingSetup { TestChain100Setup(); // Create a new block with just given transactions, coinbase paying to // scriptPubKey, and try to add it to the current chain. CBlock CreateAndProcessBlock(const std::vector &txns, const CScript &scriptPubKey); ~TestChain100Setup(); // For convenience, coinbase transactions. std::vector m_coinbase_txns; // private/public key needed to spend coinbase transactions. CKey coinbaseKey; }; class CTxMemPoolEntry; class CTxMemPool; struct TestMemPoolEntryHelper { // Default values Amount nFee; int64_t nTime; double dPriority; unsigned int nHeight; bool spendsCoinbase; unsigned int sigOpCost; LockPoints lp; TestMemPoolEntryHelper() : nFee(), nTime(0), dPriority(0.0), nHeight(1), spendsCoinbase(false), sigOpCost(4) {} CTxMemPoolEntry FromTx(const CMutableTransaction &tx, CTxMemPool *pool = nullptr); CTxMemPoolEntry FromTx(const CTransactionRef &tx, CTxMemPool *pool = nullptr); // Change the default value TestMemPoolEntryHelper &Fee(Amount _fee) { nFee = _fee; return *this; } TestMemPoolEntryHelper &Time(int64_t _time) { nTime = _time; return *this; } TestMemPoolEntryHelper &Priority(double _priority) { dPriority = _priority; return *this; } TestMemPoolEntryHelper &Height(unsigned int _height) { nHeight = _height; return *this; } TestMemPoolEntryHelper &SpendsCoinbase(bool _flag) { spendsCoinbase = _flag; return *this; } TestMemPoolEntryHelper &SigOpsCost(unsigned int _sigopsCost) { sigOpCost = _sigopsCost; return *this; } }; // define an implicit conversion here so that uint256 may be used directly in // BOOST_CHECK_* std::ostream &operator<<(std::ostream &os, const uint256 &num); #endif // BITCOIN_TEST_TEST_BITCOIN_H