Skip to content

Commit fa1d97b

Browse files
author
MarcoFalke
committed
validation: Make ProcessNewBlock*() members of ChainstateManager
1 parent fa24d49 commit fa1d97b

11 files changed

+81
-69
lines changed

src/net_processing.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ inline void static SendBlockTransactions(const CBlock& block, const BlockTransac
17361736
connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCKTXN, resp));
17371737
}
17381738

1739-
bool static ProcessHeadersMessage(CNode* pfrom, CConnman* connman, CTxMemPool& mempool, const std::vector<CBlockHeader>& headers, const CChainParams& chainparams, bool via_compact_block)
1739+
bool static ProcessHeadersMessage(CNode* pfrom, CConnman* connman, ChainstateManager& chainman, CTxMemPool& mempool, const std::vector<CBlockHeader>& headers, const CChainParams& chainparams, bool via_compact_block)
17401740
{
17411741
const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
17421742
size_t nCount = headers.size();
@@ -1796,7 +1796,7 @@ bool static ProcessHeadersMessage(CNode* pfrom, CConnman* connman, CTxMemPool& m
17961796
}
17971797

17981798
BlockValidationState state;
1799-
if (!ProcessNewBlockHeaders(headers, state, chainparams, &pindexLast)) {
1799+
if (!chainman.ProcessNewBlockHeaders(headers, state, chainparams, &pindexLast)) {
18001800
if (state.IsInvalid()) {
18011801
MaybePunishNodeForBlock(pfrom->GetId(), state, via_compact_block, "invalid header received");
18021802
return false;
@@ -2846,7 +2846,7 @@ bool ProcessMessage(CNode* pfrom, const std::string& msg_type, CDataStream& vRec
28462846

28472847
const CBlockIndex *pindex = nullptr;
28482848
BlockValidationState state;
2849-
if (!ProcessNewBlockHeaders({cmpctblock.header}, state, chainparams, &pindex)) {
2849+
if (!chainman.ProcessNewBlockHeaders({cmpctblock.header}, state, chainparams, &pindex)) {
28502850
if (state.IsInvalid()) {
28512851
MaybePunishNodeForBlock(pfrom->GetId(), state, /*via_compact_block*/ true, "invalid header via cmpctblock");
28522852
return true;
@@ -2998,7 +2998,7 @@ bool ProcessMessage(CNode* pfrom, const std::string& msg_type, CDataStream& vRec
29982998
// the peer if the header turns out to be for an invalid block.
29992999
// Note that if a peer tries to build on an invalid chain, that
30003000
// will be detected and the peer will be banned.
3001-
return ProcessHeadersMessage(pfrom, connman, mempool, {cmpctblock.header}, chainparams, /*via_compact_block=*/true);
3001+
return ProcessHeadersMessage(pfrom, connman, chainman, mempool, {cmpctblock.header}, chainparams, /*via_compact_block=*/true);
30023002
}
30033003

30043004
if (fBlockReconstructed) {
@@ -3018,7 +3018,7 @@ bool ProcessMessage(CNode* pfrom, const std::string& msg_type, CDataStream& vRec
30183018
// we have a chain with at least nMinimumChainWork), and we ignore
30193019
// compact blocks with less work than our tip, it is safe to treat
30203020
// reconstructed compact blocks as having been requested.
3021-
ProcessNewBlock(chainparams, pblock, /*fForceProcessing=*/true, &fNewBlock);
3021+
chainman.ProcessNewBlock(chainparams, pblock, /*fForceProcessing=*/true, &fNewBlock);
30223022
if (fNewBlock) {
30233023
pfrom->nLastBlockTime = GetTime();
30243024
} else {
@@ -3108,7 +3108,7 @@ bool ProcessMessage(CNode* pfrom, const std::string& msg_type, CDataStream& vRec
31083108
// disk-space attacks), but this should be safe due to the
31093109
// protections in the compact block handler -- see related comment
31103110
// in compact block optimistic reconstruction handling.
3111-
ProcessNewBlock(chainparams, pblock, /*fForceProcessing=*/true, &fNewBlock);
3111+
chainman.ProcessNewBlock(chainparams, pblock, /*fForceProcessing=*/true, &fNewBlock);
31123112
if (fNewBlock) {
31133113
pfrom->nLastBlockTime = GetTime();
31143114
} else {
@@ -3142,7 +3142,7 @@ bool ProcessMessage(CNode* pfrom, const std::string& msg_type, CDataStream& vRec
31423142
ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
31433143
}
31443144

3145-
return ProcessHeadersMessage(pfrom, connman, mempool, headers, chainparams, /*via_compact_block=*/false);
3145+
return ProcessHeadersMessage(pfrom, connman, chainman, mempool, headers, chainparams, /*via_compact_block=*/false);
31463146
}
31473147

31483148
if (msg_type == NetMsgType::BLOCK)
@@ -3171,7 +3171,7 @@ bool ProcessMessage(CNode* pfrom, const std::string& msg_type, CDataStream& vRec
31713171
mapBlockSource.emplace(hash, std::make_pair(pfrom->GetId(), true));
31723172
}
31733173
bool fNewBlock = false;
3174-
ProcessNewBlock(chainparams, pblock, forceProcessing, &fNewBlock);
3174+
chainman.ProcessNewBlock(chainparams, pblock, forceProcessing, &fNewBlock);
31753175
if (fNewBlock) {
31763176
pfrom->nLastBlockTime = GetTime();
31773177
} else {

src/rpc/blockchain.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ CTxMemPool& EnsureMemPool(const util::Ref& context)
7171
return *node.mempool;
7272
}
7373

74+
ChainstateManager& EnsureChainman(const util::Ref& context)
75+
{
76+
NodeContext& node = EnsureNodeContext(context);
77+
return EnsureChainman(node);
78+
}
79+
7480
/* Calculate the difficulty for a given block index.
7581
*/
7682
double GetDifficulty(const CBlockIndex* blockindex)

src/rpc/blockchain.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern RecursiveMutex cs_main;
1616
class CBlock;
1717
class CBlockIndex;
1818
class CTxMemPool;
19+
class ChainstateManager;
1920
class UniValue;
2021
struct NodeContext;
2122
namespace util {
@@ -52,5 +53,6 @@ void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES],
5253

5354
NodeContext& EnsureNodeContext(const util::Ref& context);
5455
CTxMemPool& EnsureMemPool(const util::Ref& context);
56+
ChainstateManager& EnsureChainman(const util::Ref& context);
5557

5658
#endif

src/rpc/mining.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static UniValue getnetworkhashps(const JSONRPCRequest& request)
101101
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1);
102102
}
103103

104-
static bool GenerateBlock(CBlock& block, uint64_t& max_tries, unsigned int& extra_nonce, uint256& block_hash)
104+
static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, unsigned int& extra_nonce, uint256& block_hash)
105105
{
106106
block_hash.SetNull();
107107

@@ -124,14 +124,15 @@ static bool GenerateBlock(CBlock& block, uint64_t& max_tries, unsigned int& extr
124124
}
125125

126126
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
127-
if (!ProcessNewBlock(chainparams, shared_pblock, true, nullptr))
127+
if (!chainman.ProcessNewBlock(chainparams, shared_pblock, true, nullptr)) {
128128
throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
129+
}
129130

130131
block_hash = block.GetHash();
131132
return true;
132133
}
133134

134-
static UniValue generateBlocks(const CTxMemPool& mempool, const CScript& coinbase_script, int nGenerate, uint64_t nMaxTries)
135+
static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& mempool, const CScript& coinbase_script, int nGenerate, uint64_t nMaxTries)
135136
{
136137
int nHeightEnd = 0;
137138
int nHeight = 0;
@@ -151,7 +152,7 @@ static UniValue generateBlocks(const CTxMemPool& mempool, const CScript& coinbas
151152
CBlock *pblock = &pblocktemplate->block;
152153

153154
uint256 block_hash;
154-
if (!GenerateBlock(*pblock, nMaxTries, nExtraNonce, block_hash)) {
155+
if (!GenerateBlock(chainman, *pblock, nMaxTries, nExtraNonce, block_hash)) {
155156
break;
156157
}
157158

@@ -228,8 +229,9 @@ static UniValue generatetodescriptor(const JSONRPCRequest& request)
228229
}
229230

230231
const CTxMemPool& mempool = EnsureMemPool(request.context);
232+
ChainstateManager& chainman = EnsureChainman(request.context);
231233

232-
return generateBlocks(mempool, coinbase_script, num_blocks, max_tries);
234+
return generateBlocks(chainman, mempool, coinbase_script, num_blocks, max_tries);
233235
}
234236

235237
static UniValue generatetoaddress(const JSONRPCRequest& request)
@@ -266,10 +268,11 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
266268
}
267269

268270
const CTxMemPool& mempool = EnsureMemPool(request.context);
271+
ChainstateManager& chainman = EnsureChainman(request.context);
269272

270273
CScript coinbase_script = GetScriptForDestination(destination);
271274

272-
return generateBlocks(mempool, coinbase_script, nGenerate, nMaxTries);
275+
return generateBlocks(chainman, mempool, coinbase_script, nGenerate, nMaxTries);
273276
}
274277

275278
static UniValue generateblock(const JSONRPCRequest& request)
@@ -370,7 +373,7 @@ static UniValue generateblock(const JSONRPCRequest& request)
370373
uint64_t max_tries{1000000};
371374
unsigned int extra_nonce{0};
372375

373-
if (!GenerateBlock(block, max_tries, extra_nonce, block_hash) || block_hash.IsNull()) {
376+
if (!GenerateBlock(EnsureChainman(request.context), block, max_tries, extra_nonce, block_hash) || block_hash.IsNull()) {
374377
throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block.");
375378
}
376379

@@ -947,7 +950,7 @@ static UniValue submitblock(const JSONRPCRequest& request)
947950
bool new_block;
948951
auto sc = std::make_shared<submitblock_StateCatcher>(block.GetHash());
949952
RegisterSharedValidationInterface(sc);
950-
bool accepted = ProcessNewBlock(Params(), blockptr, /* fForceProcessing */ true, /* fNewBlock */ &new_block);
953+
bool accepted = EnsureChainman(request.context).ProcessNewBlock(Params(), blockptr, /* fForceProcessing */ true, /* fNewBlock */ &new_block);
951954
UnregisterSharedValidationInterface(sc);
952955
if (!new_block && accepted) {
953956
return "duplicate";
@@ -986,7 +989,7 @@ static UniValue submitheader(const JSONRPCRequest& request)
986989
}
987990

988991
BlockValidationState state;
989-
ProcessNewBlockHeaders({h}, state, Params());
992+
EnsureChainman(request.context).ProcessNewBlockHeaders({h}, state, Params());
990993
if (state.IsValid()) return NullUniValue;
991994
if (state.IsError()) {
992995
throw JSONRPCError(RPC_VERIFY_ERROR, state.ToString());

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 (!ProcessNewBlockHeaders({header}, state, Params(), &pindex)) {
97+
if (!EnsureChainman(m_node).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(ProcessNewBlock(Params(), block, true, nullptr));
174+
BOOST_REQUIRE(EnsureChainman(m_node).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(ProcessNewBlock(Params(), block, true, nullptr));
192+
BOOST_REQUIRE(EnsureChainman(m_node).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(ProcessNewBlock(Params(), block, true, nullptr));
223+
BOOST_REQUIRE(EnsureChainman(m_node).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(ProcessNewBlock(chainparams, shared_pblock, true, nullptr));
256+
BOOST_CHECK(EnsureChainman(m_node).ProcessNewBlock(chainparams, shared_pblock, true, nullptr));
257257
pblock->hashPrevBlock = pblock->GetHash();
258258
}
259259

src/test/util/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
3131
assert(block->nNonce);
3232
}
3333

34-
bool processed{ProcessNewBlock(Params(), block, true, nullptr)};
34+
bool processed{EnsureChainman(node).ProcessNewBlock(Params(), block, true, nullptr)};
3535
assert(processed);
3636

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

src/test/util/setup_common.cpp

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

230230
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
231-
ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
231+
EnsureChainman(m_node).ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
232232

233233
CBlock result = block;
234234
return result;

src/test/validation_block_tests.cpp

Lines changed: 7 additions & 7 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(ProcessNewBlockHeaders(headers, state, Params()));
166+
BOOST_CHECK(EnsureChainman(m_node).ProcessNewBlockHeaders(headers, state, Params()));
167167

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

172172
// subscribe to events (this subscriber will validate event ordering)
@@ -183,18 +183,18 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
183183
// will subscribe to events generated during block validation and assert on ordering invariance
184184
std::vector<std::thread> threads;
185185
for (int i = 0; i < 10; i++) {
186-
threads.emplace_back([&blocks]() {
186+
threads.emplace_back([&]() {
187187
bool ignored;
188188
FastRandomContext insecure;
189189
for (int i = 0; i < 1000; i++) {
190190
auto block = blocks[insecure.randrange(blocks.size() - 1)];
191-
ProcessNewBlock(Params(), block, true, &ignored);
191+
EnsureChainman(m_node).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 = ProcessNewBlock(Params(), block, true, &ignored);
197+
bool processed = EnsureChainman(m_node).ProcessNewBlock(Params(), block, true, &ignored);
198198
assert(processed);
199199
}
200200
}
@@ -232,8 +232,8 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
232232
BOOST_AUTO_TEST_CASE(mempool_locks_reorg)
233233
{
234234
bool ignored;
235-
auto ProcessBlock = [&ignored](std::shared_ptr<const CBlock> block) -> bool {
236-
return ProcessNewBlock(Params(), block, /* fForceProcessing */ true, /* fNewBlock */ &ignored);
235+
auto ProcessBlock = [&](std::shared_ptr<const CBlock> block) -> bool {
236+
return EnsureChainman(m_node).ProcessNewBlock(Params(), block, /* fForceProcessing */ true, /* fNewBlock */ &ignored);
237237
};
238238

239239
// Process all mined blocks

src/validation.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3691,13 +3691,14 @@ bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationS
36913691
}
36923692

36933693
// Exposed wrapper for AcceptBlockHeader
3694-
bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, BlockValidationState& state, const CChainParams& chainparams, const CBlockIndex** ppindex)
3694+
bool ChainstateManager::ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, BlockValidationState& state, const CChainParams& chainparams, const CBlockIndex** ppindex)
36953695
{
3696+
AssertLockNotHeld(cs_main);
36963697
{
36973698
LOCK(cs_main);
36983699
for (const CBlockHeader& header : headers) {
36993700
CBlockIndex *pindex = nullptr; // Use a temp pindex instead of ppindex to avoid a const_cast
3700-
bool accepted = g_chainman.m_blockman.AcceptBlockHeader(
3701+
bool accepted = m_blockman.AcceptBlockHeader(
37013702
header, state, chainparams, &pindex);
37023703
::ChainstateActive().CheckBlockIndex(chainparams.GetConsensus());
37033704

@@ -3819,7 +3820,7 @@ bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, Block
38193820
return true;
38203821
}
38213822

3822-
bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<const CBlock> pblock, bool fForceProcessing, bool *fNewBlock)
3823+
bool ChainstateManager::ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<const CBlock> pblock, bool fForceProcessing, bool* fNewBlock)
38233824
{
38243825
AssertLockNotHeld(cs_main);
38253826

0 commit comments

Comments
 (0)