diff options
Diffstat (limited to 'src/test/util')
-rw-r--r-- | src/test/util/blockfilter.cpp | 1 | ||||
-rw-r--r-- | src/test/util/coins.cpp | 27 | ||||
-rw-r--r-- | src/test/util/coins.h | 19 | ||||
-rw-r--r-- | src/test/util/json.cpp | 17 | ||||
-rw-r--r-- | src/test/util/json.h | 14 | ||||
-rw-r--r-- | src/test/util/net.cpp | 7 | ||||
-rw-r--r-- | src/test/util/net.h | 4 | ||||
-rw-r--r-- | src/test/util/random.h | 45 | ||||
-rw-r--r-- | src/test/util/setup_common.cpp | 16 | ||||
-rw-r--r-- | src/test/util/setup_common.h | 6 | ||||
-rw-r--r-- | src/test/util/xoroshiro128plusplus.h | 71 |
11 files changed, 209 insertions, 18 deletions
diff --git a/src/test/util/blockfilter.cpp b/src/test/util/blockfilter.cpp index 3ae22921b9..ec703c6a7b 100644 --- a/src/test/util/blockfilter.cpp +++ b/src/test/util/blockfilter.cpp @@ -28,4 +28,3 @@ bool ComputeFilter(BlockFilterType filter_type, const CBlockIndex* block_index, filter = BlockFilter(filter_type, block, block_undo); return true; } - diff --git a/src/test/util/coins.cpp b/src/test/util/coins.cpp new file mode 100644 index 0000000000..9b6c5535c5 --- /dev/null +++ b/src/test/util/coins.cpp @@ -0,0 +1,27 @@ +// Copyright (c) 2023 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 <test/util/coins.h> + +#include <coins.h> +#include <primitives/transaction.h> +#include <script/script.h> +#include <test/util/random.h> +#include <uint256.h> + +#include <stdint.h> +#include <utility> + +COutPoint AddTestCoin(CCoinsViewCache& coins_view) +{ + Coin new_coin; + const uint256 txid{InsecureRand256()}; + COutPoint outpoint{txid, /*nIn=*/0}; + new_coin.nHeight = 1; + new_coin.out.nValue = InsecureRandMoneyAmount(); + new_coin.out.scriptPubKey.assign(uint32_t{56}, 1); + coins_view.AddCoin(outpoint, std::move(new_coin), /*possible_overwrite=*/false); + + return outpoint; +}; diff --git a/src/test/util/coins.h b/src/test/util/coins.h new file mode 100644 index 0000000000..5e6f4293ae --- /dev/null +++ b/src/test/util/coins.h @@ -0,0 +1,19 @@ +// Copyright (c) 2023 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_UTIL_COINS_H +#define BITCOIN_TEST_UTIL_COINS_H + +#include <primitives/transaction.h> + +class CCoinsViewCache; + +/** + * Create a Coin with DynamicMemoryUsage of 80 bytes and add it to the given view. + * @param[in,out] coins_view The coins view cache to add the new coin to. + * @returns the COutPoint of the created coin. + */ +COutPoint AddTestCoin(CCoinsViewCache& coins_view); + +#endif // BITCOIN_TEST_UTIL_COINS_H diff --git a/src/test/util/json.cpp b/src/test/util/json.cpp new file mode 100644 index 0000000000..ad3c346c84 --- /dev/null +++ b/src/test/util/json.cpp @@ -0,0 +1,17 @@ +// Copyright (c) 2023 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 <test/util/json.h> + +#include <string> +#include <util/check.h> + +#include <univalue.h> + +UniValue read_json(const std::string& jsondata) +{ + UniValue v; + Assert(v.read(jsondata) && v.isArray()); + return v.get_array(); +} diff --git a/src/test/util/json.h b/src/test/util/json.h new file mode 100644 index 0000000000..5b1026762e --- /dev/null +++ b/src/test/util/json.h @@ -0,0 +1,14 @@ +// Copyright (c) 2023 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_UTIL_JSON_H +#define BITCOIN_TEST_UTIL_JSON_H + +#include <string> + +#include <univalue.h> + +UniValue read_json(const std::string& jsondata); + +#endif // BITCOIN_TEST_UTIL_JSON_H diff --git a/src/test/util/net.cpp b/src/test/util/net.cpp index 975aff13c0..ac5dfe9e73 100644 --- a/src/test/util/net.cpp +++ b/src/test/util/net.cpp @@ -67,15 +67,14 @@ void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_by assert(node.ReceiveMsgBytes(msg_bytes, complete)); if (complete) { size_t nSizeAdded = 0; - auto it(node.vRecvMsg.begin()); - for (; it != node.vRecvMsg.end(); ++it) { + for (const auto& msg : node.vRecvMsg) { // vRecvMsg contains only completed CNetMessage // the single possible partially deserialized message are held by TransportDeserializer - nSizeAdded += it->m_raw_message_size; + nSizeAdded += msg.m_raw_message_size; } { LOCK(node.cs_vProcessMsg); - node.vProcessMsg.splice(node.vProcessMsg.end(), node.vRecvMsg, node.vRecvMsg.begin(), it); + node.vProcessMsg.splice(node.vProcessMsg.end(), node.vRecvMsg); node.nProcessQueueSize += nSizeAdded; node.fPauseRecv = node.nProcessQueueSize > nReceiveFloodSize; } diff --git a/src/test/util/net.h b/src/test/util/net.h index 90c606306f..e6506b0d08 100644 --- a/src/test/util/net.h +++ b/src/test/util/net.h @@ -103,7 +103,7 @@ constexpr auto ALL_NETWORKS = std::array{ class StaticContentsSock : public Sock { public: - explicit StaticContentsSock(const std::string& contents) : m_contents{contents}, m_consumed{0} + explicit StaticContentsSock(const std::string& contents) : m_contents{contents} { // Just a dummy number that is not INVALID_SOCKET. m_socket = INVALID_SOCKET - 1; @@ -191,7 +191,7 @@ public: private: const std::string m_contents; - mutable size_t m_consumed; + mutable size_t m_consumed{0}; }; std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context); diff --git a/src/test/util/random.h b/src/test/util/random.h new file mode 100644 index 0000000000..7997e8a346 --- /dev/null +++ b/src/test/util/random.h @@ -0,0 +1,45 @@ +// Copyright (c) 2023 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_UTIL_RANDOM_H +#define BITCOIN_TEST_UTIL_RANDOM_H + +#include <consensus/amount.h> +#include <random.h> +#include <test/util/setup_common.h> +#include <uint256.h> + +#include <cstdint> + +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(); +} + +static inline CAmount InsecureRandMoneyAmount() +{ + return static_cast<CAmount>(InsecureRandRange(MAX_MONEY + 1)); +} + +#endif // BITCOIN_TEST_UTIL_RANDOM_H diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 1b28e5f2c0..4e0000cb3d 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -180,11 +180,15 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve const ChainstateManager::Options chainman_opts{ .chainparams = chainparams, + .datadir = m_args.GetDataDirNet(), .adjusted_time_callback = GetAdjustedTime, .check_block_index = true, }; m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts); - m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(m_cache_sizes.block_tree_db, true); + m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(DBParams{ + .path = m_args.GetDataDirNet() / "blocks" / "index", + .cache_bytes = static_cast<size_t>(m_cache_sizes.block_tree_db), + .memory_only = true}); constexpr int script_check_threads = 2; StartScriptCheckWorkerThreads(script_check_threads); @@ -208,23 +212,25 @@ ChainTestingSetup::~ChainTestingSetup() void TestingSetup::LoadVerifyActivateChainstate() { + auto& chainman{*Assert(m_node.chainman)}; node::ChainstateLoadOptions options; options.mempool = Assert(m_node.mempool.get()); options.block_tree_db_in_memory = m_block_tree_db_in_memory; options.coins_db_in_memory = m_coins_db_in_memory; options.reindex = node::fReindex; options.reindex_chainstate = m_args.GetBoolArg("-reindex-chainstate", false); - options.prune = node::fPruneMode; + options.prune = chainman.m_blockman.IsPruneMode(); options.check_blocks = m_args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS); options.check_level = m_args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL); - auto [status, error] = LoadChainstate(*Assert(m_node.chainman), m_cache_sizes, options); + options.require_full_verification = m_args.IsArgSet("-checkblocks") || m_args.IsArgSet("-checklevel"); + auto [status, error] = LoadChainstate(chainman, m_cache_sizes, options); assert(status == node::ChainstateLoadStatus::SUCCESS); - std::tie(status, error) = VerifyLoadedChainstate(*Assert(m_node.chainman), options); + std::tie(status, error) = VerifyLoadedChainstate(chainman, options); assert(status == node::ChainstateLoadStatus::SUCCESS); BlockValidationState state; - if (!m_node.chainman->ActiveChainstate().ActivateBestChain(state)) { + if (!chainman.ActiveChainstate().ActivateBestChain(state)) { throw std::runtime_error(strprintf("ActivateBestChain failed. (%s)", state.ToString())); } } diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h index 5f653d83ae..8874db7e75 100644 --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -71,12 +71,6 @@ static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED) } } -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(); } - static constexpr CAmount CENT{1000000}; /** Basic testing setup. diff --git a/src/test/util/xoroshiro128plusplus.h b/src/test/util/xoroshiro128plusplus.h new file mode 100644 index 0000000000..ac9f59b3f5 --- /dev/null +++ b/src/test/util/xoroshiro128plusplus.h @@ -0,0 +1,71 @@ +// Copyright (c) 2022 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_UTIL_XOROSHIRO128PLUSPLUS_H +#define BITCOIN_TEST_UTIL_XOROSHIRO128PLUSPLUS_H + +#include <cstdint> +#include <limits> + +/** xoroshiro128++ PRNG. Extremely fast, not appropriate for cryptographic purposes. + * + * Memory footprint is 128bit, period is 2^128 - 1. + * This class is not thread-safe. + * + * Reference implementation available at https://prng.di.unimi.it/xoroshiro128plusplus.c + * See https://prng.di.unimi.it/ + */ +class XoRoShiRo128PlusPlus +{ + uint64_t m_s0; + uint64_t m_s1; + + [[nodiscard]] constexpr static uint64_t rotl(uint64_t x, int n) + { + return (x << n) | (x >> (64 - n)); + } + + [[nodiscard]] constexpr static uint64_t SplitMix64(uint64_t& seedval) noexcept + { + uint64_t z = (seedval += UINT64_C(0x9e3779b97f4a7c15)); + z = (z ^ (z >> 30U)) * UINT64_C(0xbf58476d1ce4e5b9); + z = (z ^ (z >> 27U)) * UINT64_C(0x94d049bb133111eb); + return z ^ (z >> 31U); + } + +public: + using result_type = uint64_t; + + constexpr explicit XoRoShiRo128PlusPlus(uint64_t seedval) noexcept + : m_s0(SplitMix64(seedval)), m_s1(SplitMix64(seedval)) + { + } + + // no copy - that is dangerous, we don't want accidentally copy the RNG and then have two streams + // with exactly the same results. If you need a copy, call copy(). + XoRoShiRo128PlusPlus(const XoRoShiRo128PlusPlus&) = delete; + XoRoShiRo128PlusPlus& operator=(const XoRoShiRo128PlusPlus&) = delete; + + // allow moves + XoRoShiRo128PlusPlus(XoRoShiRo128PlusPlus&&) = default; + XoRoShiRo128PlusPlus& operator=(XoRoShiRo128PlusPlus&&) = default; + + ~XoRoShiRo128PlusPlus() = default; + + constexpr result_type operator()() noexcept + { + uint64_t s0 = m_s0, s1 = m_s1; + const uint64_t result = rotl(s0 + s1, 17) + s0; + s1 ^= s0; + m_s0 = rotl(s0, 49) ^ s1 ^ (s1 << 21); + m_s1 = rotl(s1, 28); + return result; + } + + static constexpr result_type min() noexcept { return std::numeric_limits<result_type>::min(); } + static constexpr result_type max() noexcept { return std::numeric_limits<result_type>::max(); } + static constexpr double entropy() noexcept { return 0.0; } +}; + +#endif // BITCOIN_TEST_UTIL_XOROSHIRO128PLUSPLUS_H |