Skip to content

Commit 5ec19df

Browse files
author
MarcoFalke
committed
Merge #19277: util: Add Assert identity function
fab80fe refactor: Remove unused EnsureChainman (MarcoFalke) fa34587 scripted-diff: Replace EnsureChainman with Assert in unit tests (MarcoFalke) fa6ef70 util: Add Assert identity function (MarcoFalke) fa457fb move-only: Move NDEBUG compile time check to util/check (MarcoFalke) Pull request description: The utility function is primarily useful to dereference pointer types, which are known to be not null at that time. For example, the ArgsManager is known to exist when the wallets are started: https://github.com/bitcoin/bitcoin/pull/18923/files#diff-fdb2a1a1d8bc790fcddeb6cf5a42ac55R503 . Instead of silently relying on that assumption, `Assert` can be used to abort the program and avoid UB should the assumption ever be violated. ACKs for top commit: promag: Tested ACK fab80fe. ryanofsky: Code review ACK fab80fe Tree-SHA512: 830fba10152ba17d47c4dd42809c7e26f9fe6d38e17a2d5b3f054fd644a5c4c9841286ac421ec9bb28cea9f5faeb659740fcf00de6cc589d423fee7694c42d16
2 parents 3276c14 + fab80fe commit 5ec19df

14 files changed

+46
-39
lines changed

src/init.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include <txdb.h>
5151
#include <txmempool.h>
5252
#include <util/asmap.h>
53+
#include <util/check.h>
5354
#include <util/moneystr.h>
5455
#include <util/string.h>
5556
#include <util/system.h>
@@ -1378,9 +1379,9 @@ bool AppInitMain(const util::Ref& context, NodeContext& node)
13781379
node.mempool = &::mempool;
13791380
assert(!node.chainman);
13801381
node.chainman = &g_chainman;
1381-
ChainstateManager& chainman = EnsureChainman(node);
1382+
ChainstateManager& chainman = *Assert(node.chainman);
13821383

1383-
node.peer_logic.reset(new PeerLogicValidation(node.connman.get(), node.banman.get(), *node.scheduler, *node.chainman, *node.mempool));
1384+
node.peer_logic.reset(new PeerLogicValidation(node.connman.get(), node.banman.get(), *node.scheduler, chainman, *node.mempool));
13841385
RegisterValidationInterface(node.peer_logic.get());
13851386

13861387
// sanitize comments per BIP-0014, format user agent and check total size

src/net_processing.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
#include <consensus/validation.h>
1414
#include <hash.h>
1515
#include <index/blockfilterindex.h>
16-
#include <validation.h>
1716
#include <merkleblock.h>
18-
#include <netmessagemaker.h>
1917
#include <netbase.h>
18+
#include <netmessagemaker.h>
2019
#include <policy/fees.h>
2120
#include <policy/policy.h>
2221
#include <primitives/block.h>
@@ -26,16 +25,14 @@
2625
#include <scheduler.h>
2726
#include <tinyformat.h>
2827
#include <txmempool.h>
29-
#include <util/system.h>
28+
#include <util/check.h> // For NDEBUG compile time check
3029
#include <util/strencodings.h>
30+
#include <util/system.h>
31+
#include <validation.h>
3132

3233
#include <memory>
3334
#include <typeinfo>
3435

35-
#if defined(NDEBUG)
36-
# error "Bitcoin cannot be compiled without assertions."
37-
#endif
38-
3936
/** Expiration time for orphan transactions in seconds */
4037
static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
4138
/** Minimum time between orphan transactions expire time checks in seconds */

src/node/context.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,4 @@ struct NodeContext {
4949
~NodeContext();
5050
};
5151

52-
inline ChainstateManager& EnsureChainman(const NodeContext& node)
53-
{
54-
assert(node.chainman);
55-
return *node.chainman;
56-
}
57-
5852
#endif // BITCOIN_NODE_CONTEXT_H

src/rpc/blockchain.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ CTxMemPool& EnsureMemPool(const util::Ref& context)
7575
ChainstateManager& EnsureChainman(const util::Ref& context)
7676
{
7777
NodeContext& node = EnsureNodeContext(context);
78-
return EnsureChainman(node);
78+
if (!node.chainman) {
79+
throw JSONRPCError(RPC_INTERNAL_ERROR, "Node chainman not found");
80+
}
81+
return *node.chainman;
7982
}
8083

8184
/* Calculate the difficulty for a given block index.

src/test/blockfilter_index_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ bool BuildChainTestingSetup::BuildChain(const CBlockIndex* pindex,
9494
CBlockHeader header = block->GetBlockHeader();
9595

9696
BlockValidationState state;
97-
if (!EnsureChainman(m_node).ProcessNewBlockHeaders({header}, state, Params(), &pindex)) {
97+
if (!Assert(m_node.chainman)->ProcessNewBlockHeaders({header}, state, Params(), &pindex)) {
9898
return false;
9999
}
100100
}
@@ -171,7 +171,7 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
171171
uint256 chainA_last_header = last_header;
172172
for (size_t i = 0; i < 2; i++) {
173173
const auto& block = chainA[i];
174-
BOOST_REQUIRE(EnsureChainman(m_node).ProcessNewBlock(Params(), block, true, nullptr));
174+
BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(Params(), block, true, nullptr));
175175
}
176176
for (size_t i = 0; i < 2; i++) {
177177
const auto& block = chainA[i];
@@ -189,7 +189,7 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
189189
uint256 chainB_last_header = last_header;
190190
for (size_t i = 0; i < 3; i++) {
191191
const auto& block = chainB[i];
192-
BOOST_REQUIRE(EnsureChainman(m_node).ProcessNewBlock(Params(), block, true, nullptr));
192+
BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(Params(), block, true, nullptr));
193193
}
194194
for (size_t i = 0; i < 3; i++) {
195195
const auto& block = chainB[i];
@@ -220,7 +220,7 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
220220
// Reorg back to chain A.
221221
for (size_t i = 2; i < 4; i++) {
222222
const auto& block = chainA[i];
223-
BOOST_REQUIRE(EnsureChainman(m_node).ProcessNewBlock(Params(), block, true, nullptr));
223+
BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(Params(), block, true, nullptr));
224224
}
225225

226226
// Check that chain A and B blocks can be retrieved.

src/test/miner_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
253253
pblock->nNonce = blockinfo[i].nonce;
254254
}
255255
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock);
256-
BOOST_CHECK(EnsureChainman(m_node).ProcessNewBlock(chainparams, shared_pblock, true, nullptr));
256+
BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr));
257257
pblock->hashPrevBlock = pblock->GetHash();
258258
}
259259

src/test/util/mining.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <node/context.h>
1212
#include <pow.h>
1313
#include <script/standard.h>
14+
#include <util/check.h>
1415
#include <validation.h>
1516

1617
CTxIn generatetoaddress(const NodeContext& node, const std::string& address)
@@ -31,17 +32,16 @@ CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
3132
assert(block->nNonce);
3233
}
3334

34-
bool processed{EnsureChainman(node).ProcessNewBlock(Params(), block, true, nullptr)};
35+
bool processed{Assert(node.chainman)->ProcessNewBlock(Params(), block, true, nullptr)};
3536
assert(processed);
3637

3738
return CTxIn{block->vtx[0]->GetHash(), 0};
3839
}
3940

4041
std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
4142
{
42-
assert(node.mempool);
4343
auto block = std::make_shared<CBlock>(
44-
BlockAssembler{*node.mempool, Params()}
44+
BlockAssembler{*Assert(node.mempool), Params()}
4545
.CreateNewBlock(coinbase_scriptPubKey)
4646
->block);
4747

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa
231231
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
232232

233233
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
234-
EnsureChainman(m_node).ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
234+
Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
235235

236236
CBlock result = block;
237237
return result;

src/test/util/setup_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <pubkey.h>
1313
#include <random.h>
1414
#include <txmempool.h>
15+
#include <util/check.h>
1516
#include <util/string.h>
1617

1718
#include <type_traits>

src/test/validation_block_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
163163
std::transform(blocks.begin(), blocks.end(), std::back_inserter(headers), [](std::shared_ptr<const CBlock> b) { return b->GetBlockHeader(); });
164164

165165
// Process all the headers so we understand the toplogy of the chain
166-
BOOST_CHECK(EnsureChainman(m_node).ProcessNewBlockHeaders(headers, state, Params()));
166+
BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlockHeaders(headers, state, Params()));
167167

168168
// Connect the genesis block and drain any outstanding events
169-
BOOST_CHECK(EnsureChainman(m_node).ProcessNewBlock(Params(), std::make_shared<CBlock>(Params().GenesisBlock()), true, &ignored));
169+
BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(Params(), std::make_shared<CBlock>(Params().GenesisBlock()), true, &ignored));
170170
SyncWithValidationInterfaceQueue();
171171

172172
// subscribe to events (this subscriber will validate event ordering)
@@ -188,13 +188,13 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
188188
FastRandomContext insecure;
189189
for (int i = 0; i < 1000; i++) {
190190
auto block = blocks[insecure.randrange(blocks.size() - 1)];
191-
EnsureChainman(m_node).ProcessNewBlock(Params(), block, true, &ignored);
191+
Assert(m_node.chainman)->ProcessNewBlock(Params(), block, true, &ignored);
192192
}
193193

194194
// to make sure that eventually we process the full chain - do it here
195195
for (auto block : blocks) {
196196
if (block->vtx.size() == 1) {
197-
bool processed = EnsureChainman(m_node).ProcessNewBlock(Params(), block, true, &ignored);
197+
bool processed = Assert(m_node.chainman)->ProcessNewBlock(Params(), block, true, &ignored);
198198
assert(processed);
199199
}
200200
}
@@ -233,7 +233,7 @@ BOOST_AUTO_TEST_CASE(mempool_locks_reorg)
233233
{
234234
bool ignored;
235235
auto ProcessBlock = [&](std::shared_ptr<const CBlock> block) -> bool {
236-
return EnsureChainman(m_node).ProcessNewBlock(Params(), block, /* fForceProcessing */ true, /* fNewBlock */ &ignored);
236+
return Assert(m_node.chainman)->ProcessNewBlock(Params(), block, /* fForceProcessing */ true, /* fNewBlock */ &ignored);
237237
};
238238

239239
// Process all mined blocks

0 commit comments

Comments
 (0)