Skip to content

Commit ae22357

Browse files
committed
Replace CValidationState param in ProcessNewBlock with BlockChecked
1 parent 7c98ce5 commit ae22357

File tree

5 files changed

+28
-48
lines changed

5 files changed

+28
-48
lines changed

src/main.cpp

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,22 +3787,26 @@ static bool AcceptBlock(const CBlock& block, CValidationState& state, const CCha
37873787
return true;
37883788
}
37893789

3790-
bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, const CBlock* pblock, bool fForceProcessing, const CDiskBlockPos* dbp, bool *fNewBlock)
3790+
bool ProcessNewBlock(const CChainParams& chainparams, const CBlock* pblock, bool fForceProcessing, const CDiskBlockPos* dbp, bool *fNewBlock)
37913791
{
37923792
{
37933793
LOCK(cs_main);
37943794

37953795
// Store to disk
37963796
CBlockIndex *pindex = NULL;
37973797
if (fNewBlock) *fNewBlock = false;
3798+
CValidationState state;
37983799
bool ret = AcceptBlock(*pblock, state, chainparams, &pindex, fForceProcessing, dbp, fNewBlock);
37993800
CheckBlockIndex(chainparams.GetConsensus());
3800-
if (!ret)
3801+
if (!ret) {
3802+
GetMainSignals().BlockChecked(*pblock, state);
38013803
return error("%s: AcceptBlock FAILED", __func__);
3804+
}
38023805
}
38033806

38043807
NotifyHeaderTip();
38053808

3809+
CValidationState state; // Only used to report errors, not invalidity - ignore it
38063810
if (!ActivateBestChain(state, chainparams, pblock))
38073811
return error("%s: ActivateBestChain failed", __func__);
38083812

@@ -5912,26 +5916,18 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
59125916
fBlockRead = true;
59135917
// mapBlockSource is only used for sending reject messages and DoS scores,
59145918
// so the race between here and cs_main in ProcessNewBlock is fine.
5919+
// BIP 152 permits peers to relay compact blocks after validating
5920+
// the header only; we should not punish peers if the block turns
5921+
// out to be invalid.
59155922
mapBlockSource.emplace(resp.blockhash, std::make_pair(pfrom->GetId(), false));
59165923
}
59175924
} // Don't hold cs_main when we call into ProcessNewBlock
59185925
if (fBlockRead) {
5919-
CValidationState state;
59205926
bool fNewBlock = false;
59215927
// Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
59225928
// even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
5923-
// BIP 152 permits peers to relay compact blocks after validating
5924-
// the header only; we should not punish peers if the block turns
5925-
// out to be invalid.
5926-
ProcessNewBlock(state, chainparams, &block, true, NULL, &fNewBlock);
5927-
int nDoS;
5928-
if (state.IsInvalid(nDoS)) {
5929-
assert (state.GetRejectCode() < REJECT_INTERNAL); // Blocks are never rejected with internal reject codes
5930-
connman.PushMessage(pfrom, NetMsgType::REJECT, strCommand, (unsigned char)state.GetRejectCode(),
5931-
state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), block.GetHash());
5932-
LOCK(cs_main);
5933-
mapBlockSource.erase(resp.blockhash);
5934-
} else if (fNewBlock)
5929+
ProcessNewBlock(chainparams, &block, true, NULL, &fNewBlock);
5930+
if (fNewBlock)
59355931
pfrom->nLastBlockTime = GetTime();
59365932
}
59375933
}
@@ -6090,7 +6086,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
60906086

60916087
LogPrint("net", "received block %s peer=%d\n", block.GetHash().ToString(), pfrom->id);
60926088

6093-
CValidationState state;
60946089
// Process all blocks from whitelisted peers, even if not requested,
60956090
// unless we're still syncing with the network.
60966091
// Such an unrequested block may still be processed, subject to the
@@ -6107,21 +6102,9 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
61076102
mapBlockSource.emplace(hash, std::make_pair(pfrom->GetId(), true));
61086103
}
61096104
bool fNewBlock = false;
6110-
ProcessNewBlock(state, chainparams, &block, forceProcessing, NULL, &fNewBlock);
6111-
int nDoS;
6112-
if (state.IsInvalid(nDoS)) {
6113-
assert (state.GetRejectCode() < REJECT_INTERNAL); // Blocks are never rejected with internal reject codes
6114-
connman.PushMessage(pfrom, NetMsgType::REJECT, strCommand, (unsigned char)state.GetRejectCode(),
6115-
state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), block.GetHash());
6116-
if (nDoS > 0) {
6117-
LOCK(cs_main);
6118-
Misbehaving(pfrom->GetId(), nDoS);
6119-
}
6120-
LOCK(cs_main);
6121-
mapBlockSource.erase(hash);
6122-
} else if (fNewBlock)
6105+
ProcessNewBlock(chainparams, &block, forceProcessing, NULL, &fNewBlock);
6106+
if (fNewBlock)
61236107
pfrom->nLastBlockTime = GetTime();
6124-
61256108
}
61266109

61276110

src/main.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,21 @@ static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024;
215215
* Process an incoming block. This only returns after the best known valid
216216
* block is made active. Note that it does not, however, guarantee that the
217217
* specific block passed to it has been checked for validity!
218+
*
219+
* If you want to *possibly* get feedback on whether pblock is valid, you must
220+
* install a CValidationInterface (see validationinterface.h) - this will have
221+
* its BlockChecked method called whenever *any* block completes validation.
222+
*
223+
* Note that we guarantee that either the proof-of-work is valid on pblock, or
224+
* (and possibly also) BlockChecked will have been called.
218225
*
219-
* @param[out] state This may be set to an Error state if any error occurred processing it, including during validation/connection/etc of otherwise unrelated blocks during reorganization; or it may be set to an Invalid state if pblock is itself invalid (but this is not guaranteed even when the block is checked). If you want to *possibly* get feedback on whether pblock is valid, you must also install a CValidationInterface (see validationinterface.h) - this will have its BlockChecked method called whenever *any* block completes validation.
220226
* @param[in] pblock The block we want to process.
221227
* @param[in] fForceProcessing Process this block even if unrequested; used for non-network block sources and whitelisted peers.
222228
* @param[out] dbp The already known disk position of pblock, or NULL if not yet stored.
223229
* @param[out] fNewBlock A boolean which is set to indicate if the block was first received via this call
224230
* @return True if state.IsValid()
225231
*/
226-
bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, const CBlock* pblock, bool fForceProcessing, const CDiskBlockPos* dbp, bool* fNewBlock);
232+
bool ProcessNewBlock(const CChainParams& chainparams, const CBlock* pblock, bool fForceProcessing, const CDiskBlockPos* dbp, bool* fNewBlock);
227233
/** Check whether enough disk space is available for an incoming block */
228234
bool CheckDiskSpace(uint64_t nAdditionalBytes = 0);
229235
/** Open a block file (blk?????.dat) */

src/rpc/mining.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ UniValue generateBlocks(boost::shared_ptr<CReserveScript> coinbaseScript, int nG
131131
if (pblock->nNonce == nInnerLoopCount) {
132132
continue;
133133
}
134-
CValidationState state;
135-
if (!ProcessNewBlock(state, Params(), pblock, true, NULL, NULL))
134+
if (!ProcessNewBlock(Params(), pblock, true, NULL, NULL))
136135
throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
137136
++nHeight;
138137
blockHashes.push_back(pblock->GetHash().GetHex());
@@ -754,24 +753,19 @@ UniValue submitblock(const JSONRPCRequest& request)
754753
}
755754
}
756755

757-
CValidationState state;
758756
submitblock_StateCatcher sc(block.GetHash());
759757
RegisterValidationInterface(&sc);
760-
bool fAccepted = ProcessNewBlock(state, Params(), &block, true, NULL, NULL);
758+
bool fAccepted = ProcessNewBlock(Params(), &block, true, NULL, NULL);
761759
UnregisterValidationInterface(&sc);
762760
if (fBlockPresent)
763761
{
764762
if (fAccepted && !sc.found)
765763
return "duplicate-inconclusive";
766764
return "duplicate";
767765
}
768-
if (fAccepted)
769-
{
770-
if (!sc.found)
771-
return "inconclusive";
772-
state = sc.state;
773-
}
774-
return BIP22ValidationResult(state);
766+
if (!sc.found)
767+
return "inconclusive";
768+
return BIP22ValidationResult(sc.state);
775769
}
776770

777771
UniValue estimatefee(const JSONRPCRequest& request)

src/test/miner_tests.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
222222
txFirst.push_back(new CTransaction(pblock->vtx[0]));
223223
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
224224
pblock->nNonce = blockinfo[i].nonce;
225-
CValidationState state;
226-
BOOST_CHECK(ProcessNewBlock(state, chainparams, pblock, true, NULL, NULL));
227-
BOOST_CHECK(state.IsValid());
225+
BOOST_CHECK(ProcessNewBlock(chainparams, pblock, true, NULL, NULL));
228226
pblock->hashPrevBlock = pblock->GetHash();
229227
}
230228

src/test/test_bitcoin.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>&
126126

127127
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
128128

129-
CValidationState state;
130-
ProcessNewBlock(state, chainparams, &block, true, NULL, NULL);
129+
ProcessNewBlock(chainparams, &block, true, NULL, NULL);
131130

132131
CBlock result = block;
133132
return result;

0 commit comments

Comments
 (0)