Skip to content

Commit 06582cd

Browse files
b201f1f Merge bitcoin#27892: refactor: Avoid copy of bilingual_str when formatting, Fix ADL violation (DCG-Claude) 7286cbc Merge bitcoin#23813: Add test and docs for getblockfrompeer with pruning (DCG-Claude) 98b9304 Merge bitcoin#27425: test: move remaining rand code from util/setup_common to util/random (DCG-Claude) b539c3d Merge bitcoin#27549: fuzz: addrman, add coverage for `network` field in `Select()`, `Size()` and `GetAddr()` (DCG-Claude) Pull request description: <!-- backportsys v1 --> ## Issue being fixed or feature implemented Backports 4 Bitcoin Core v0.26 pull request(s) that the Dash queue selected, including any discovered prerequisites: bitcoin#27549, bitcoin#27425, bitcoin#23813, bitcoin#27892. ## What was done? | upstream | commit | gates | notes | |---|---|---|---| | bitcoin#27549 | `8787b4a5bf` | build:pass mech:warn pick:pass tests:pass tree:pass verify:pass | The commit is a faithful backport of bitcoin#27549: the fuzz target now picks an optional `Network` fr | | bitcoin#27425 | `53d9fa22f9` | build:pass mech:warn pick:pass tests:pass tree:pass verify:pass | Faithful replay of bitcoin#27425: the g_insecure_rand_ctx global, Seed()/GetUintFromEnv() and the Seed | | bitcoin#23813 | `479e797f80` | build:pass mech:warn pick:pass tests:pass tree:pass verify:pass | The RPC help-text change is applied verbatim and the functional test reproduces every upstream hunk, with prun | | bitcoin#27892 | `9561488e92` | build:pass ci_fork:pass mech:warn pick:pass tests:pass tree:pass verify:pass | The commit replaces the two TranslateArg overloads in src/util/translation.h with the generic-lambda `translat | Each commit keeps the upstream subject (`partial Merge …` only where a hunk is deferred to a prerequisite still to be backported, named in the commit body; a hunk Dash intentionally never wants is recorded in the commit body or the reviewer's note above and does not make the backport partial). Conflicts were resolved commit by commit; commits that needed no resolution were cherry-picked unchanged. ## How Has This Been Tested? Recorded per commit, at that commit's own sha, not once for the branch: - built at each commit, reusing the worktree build cache - the test suites touched by each diff run at that commit, where the diff selected one - a mechanical diff-of-diffs against the upstream patch (every upstream hunk present, no added line without an upstream counterpart, no Dash-specific line dropped) - an independent verification pass on 4 commit(s) that needed adaptation - CI green on the fork gate PR thepastaclaw#67 at `9561488e92`, which is this branch's head Gates that did not come back clean — please weigh these: - bitcoin#27549 `mech`: **warn** — 1 invented line(s); 1 partial/prereq/low-risk finding(s) - bitcoin#27425 `mech`: **warn** — 1 partial/prereq/low-risk finding(s) - bitcoin#23813 `mech`: **warn** — 19 invented line(s); 1 partial/prereq/low-risk finding(s) - bitcoin#27892 `mech`: **warn** — 1 partial/prereq/low-risk finding(s) ## Breaking Changes None beyond the upstream changes themselves. ## Checklist: _Left for the reviewer; backportsys does not tick boxes on its own behalf._ - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [ ] I have assigned this pull request to a milestone --- ### Maintainer controls <!-- backportsys controls --> Tick a box and backportsys acts on it within a few minutes, then clears the box. For anything else — a hunk to drop, a resolution to redo, a question — just leave a review comment; nothing here needs a box. - [ ] 🔒 **Hands off** — stop every automated update to this branch <!-- bps:hands_off --> - [ ] 🔄 **Rebase** onto current `develop` <!-- bps:rebase --> - [ ] ❌ **Close** — abandon this batch and release its items <!-- bps:close --> <!-- /backportsys controls --> <!-- human notes below; the bot never edits this section --> Top commit has no ACKs. Tree-SHA512: 228f702a1a7edad7195bb937d76f78b08d562ac32209d754aa105453ac62c224cc8088b70bd372c56d00f916e084a5f8140f4207b56226b2bc69152617a30360
2 parents ce3620b + b201f1f commit 06582cd

15 files changed

Lines changed: 153 additions & 77 deletions

‎src/Makefile.test_util.include‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ libtest_util_a_SOURCES = \
4343
test/util/masternode.cpp \
4444
test/util/mining.cpp \
4545
test/util/net.cpp \
46+
test/util/random.cpp \
4647
test/util/script.cpp \
4748
test/util/setup_common.cpp \
4849
test/util/str.cpp \

‎src/rpc/blockchain.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,8 @@ static RPCHelpMan getblockfrompeer()
488488
"We must have the header for this block, e.g. using submitheader.\n"
489489
"Subsequent calls for the same block may cause the response from the previous peer to be ignored.\n"
490490
"Peers generally ignore requests for a stale block that they never fully verified, or one that is more than a month old.\n"
491-
"When a peer does not respond with a block, we will disconnect.\n\n"
491+
"When a peer does not respond with a block, we will disconnect.\n"
492+
"Note: The block could be re-pruned as soon as it is received.\n\n"
492493
"Returns an empty JSON object if the request was successfully scheduled.",
493494
{
494495
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash to try to fetch"},

‎src/test/denialofservice_tests.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <script/signingprovider.h>
1414
#include <script/standard.h>
1515
#include <test/util/net.h>
16+
#include <test/util/random.h>
1617
#include <test/util/setup_common.h>
1718
#include <timedata.h>
1819
#include <util/string.h>

‎src/test/fuzz/addrman.cpp‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,21 @@ FUZZ_TARGET(addrman, .init = initialize_addrman)
280280
});
281281
}
282282
const AddrMan& const_addr_man{addr_man};
283+
std::optional<Network> network;
284+
if (fuzzed_data_provider.ConsumeBool()) {
285+
network = fuzzed_data_provider.PickValueInArray(ALL_NETWORKS);
286+
}
283287
(void)const_addr_man.GetAddr(
284-
/* max_addresses */ fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096),
285-
/* max_pct */ fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096),
286-
/* network */ std::nullopt,
287-
/* filtered =*/ fuzzed_data_provider.ConsumeBool());
288-
(void)const_addr_man.Select(fuzzed_data_provider.ConsumeBool());
289-
(void)const_addr_man.Size();
288+
/*max_addresses=*/fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096),
289+
/*max_pct=*/fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096),
290+
network,
291+
/*filtered=*/fuzzed_data_provider.ConsumeBool());
292+
(void)const_addr_man.Select(fuzzed_data_provider.ConsumeBool(), network);
293+
std::optional<bool> in_new;
294+
if (fuzzed_data_provider.ConsumeBool()) {
295+
in_new = fuzzed_data_provider.ConsumeBool();
296+
}
297+
(void)const_addr_man.Size(network, in_new);
290298
CDataStream data_stream(SER_NETWORK, PROTOCOL_VERSION);
291299
data_stream << const_addr_man;
292300
}

‎src/test/miniscript_tests.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <string>
77

8+
#include <test/util/random.h>
89
#include <test/util/setup_common.h>
910
#include <boost/test/unit_test.hpp>
1011

‎src/test/net_peer_connection_tests.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <random.h>
1515
#include <test/util/logging.h>
1616
#include <test/util/net.h>
17+
#include <test/util/random.h>
1718
#include <test/util/setup_common.h>
1819
#include <tinyformat.h>
1920
#include <version.h>

‎src/test/random_tests.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <random.h>
66

7+
#include <test/util/random.h>
78
#include <test/util/setup_common.h>
89
#include <util/time.h>
910

‎src/test/util/random.cpp‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2023 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <test/util/random.h>
6+
7+
#include <logging.h>
8+
#include <random.h>
9+
#include <uint256.h>
10+
11+
#include <cstdlib>
12+
#include <string>
13+
14+
FastRandomContext g_insecure_rand_ctx;
15+
16+
/** Return the unsigned from the environment var if available, otherwise 0 */
17+
static uint256 GetUintFromEnv(const std::string& env_name)
18+
{
19+
const char* num = std::getenv(env_name.c_str());
20+
if (!num) return {};
21+
return uint256S(num);
22+
}
23+
24+
void Seed(FastRandomContext& ctx)
25+
{
26+
// Should be enough to get the seed once for the process
27+
static uint256 seed{};
28+
static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
29+
if (seed.IsNull()) seed = GetUintFromEnv(RANDOM_CTX_SEED);
30+
if (seed.IsNull()) seed = GetRandHash();
31+
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
32+
ctx = FastRandomContext(seed);
33+
}

‎src/test/util/random.h‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,41 @@
77

88
#include <consensus/amount.h>
99
#include <random.h>
10-
#include <test/util/setup_common.h>
1110
#include <uint256.h>
1211

1312
#include <cstdint>
1413

14+
/**
15+
* This global and the helpers that use it are not thread-safe.
16+
*
17+
* If thread-safety is needed, the global could be made thread_local (given
18+
* that thread_local is supported on all architectures we support) or a
19+
* per-thread instance could be used in the multi-threaded test.
20+
*/
21+
extern FastRandomContext g_insecure_rand_ctx;
22+
23+
/**
24+
* Flag to make GetRand in random.h return the same number
25+
*/
26+
extern bool g_mock_deterministic_tests;
27+
28+
enum class SeedRand {
29+
ZEROS, //!< Seed with a compile time constant of zeros
30+
SEED, //!< Call the Seed() helper
31+
};
32+
33+
/** Seed the given random ctx or use the seed passed in via an environment var */
34+
void Seed(FastRandomContext& ctx);
35+
36+
static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
37+
{
38+
if (seed == SeedRand::ZEROS) {
39+
g_insecure_rand_ctx = FastRandomContext(/*fDeterministic=*/true);
40+
} else {
41+
Seed(g_insecure_rand_ctx);
42+
}
43+
}
44+
1545
static inline uint32_t InsecureRand32()
1646
{
1747
return g_insecure_rand_ctx.rand32();

‎src/test/util/setup_common.cpp‎

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <policy/fees_args.h>
4141
#include <policy/settings.h>
4242
#include <pow.h>
43+
#include <random.h>
4344
#include <rpc/blockchain.h>
4445
#include <rpc/register.h>
4546
#include <rpc/server.h>
@@ -49,6 +50,7 @@
4950
#include <streams.h>
5051
#include <test/util/index.h>
5152
#include <test/util/net.h>
53+
#include <test/util/random.h>
5254
#include <test/util/txmempool.h>
5355
#include <timedata.h>
5456
#include <txdb.h>
@@ -105,29 +107,9 @@ using node::VerifyLoadedChainstate;
105107

106108
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
107109

108-
FastRandomContext g_insecure_rand_ctx;
109110
/** Random context to get unique temp data dirs. Separate from g_insecure_rand_ctx, which can be seeded from a const env var */
110111
static FastRandomContext g_insecure_rand_ctx_temp_path;
111112

112-
/** Return the unsigned from the environment var if available, otherwise 0 */
113-
static uint256 GetUintFromEnv(const std::string& env_name)
114-
{
115-
const char* num = std::getenv(env_name.c_str());
116-
if (!num) return {};
117-
return uint256S(num);
118-
}
119-
120-
void Seed(FastRandomContext& ctx)
121-
{
122-
// Should be enough to get the seed once for the process
123-
static uint256 seed{};
124-
static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
125-
if (seed.IsNull()) seed = GetUintFromEnv(RANDOM_CTX_SEED);
126-
if (seed.IsNull()) seed = GetRandHash();
127-
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
128-
ctx = FastRandomContext(seed);
129-
}
130-
131113
std::ostream& operator<<(std::ostream& os, const uint256& num)
132114
{
133115
os << num.ToString();

0 commit comments

Comments
 (0)