Skip to content

Commit 5564002

Browse files
committed
[Refactor] set budgetManager height in NewBlock + remove GetHeight()
1 parent 80d4962 commit 5564002

File tree

5 files changed

+18
-28
lines changed

5 files changed

+18
-28
lines changed

src/main.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,6 @@ CNodeState* State(NodeId pnode)
381381
return &it->second;
382382
}
383383

384-
int GetHeight()
385-
{
386-
while (true) {
387-
TRY_LOCK(cs_main, lockMain);
388-
if (!lockMain) {
389-
MilliSleep(50);
390-
continue;
391-
}
392-
return chainActive.Height();
393-
}
394-
}
395-
396384
void UpdatePreferredDownload(CNode* node, CNodeState* state)
397385
{
398386
nPreferredDownload -= state->fPreferredDownload;
@@ -3044,7 +3032,7 @@ static bool ActivateBestChainStep(CValidationState& state, CBlockIndex* pindexMo
30443032
* or an activated best chain. pblock is either NULL or a pointer to a block
30453033
* that is already loaded (to avoid loading it again from disk).
30463034
*/
3047-
bool ActivateBestChain(CValidationState& state, const CBlock* pblock, bool fAlreadyChecked, CConnman* connman, CBudgetManager* pbudget)
3035+
bool ActivateBestChain(CValidationState& state, const CBlock* pblock, bool fAlreadyChecked, CConnman* connman)
30483036
{
30493037
// Note that while we're often called here from ProcessNewBlock, this is
30503038
// far from a guarantee. Things in the P2P/RPC will often end up calling
@@ -3101,8 +3089,6 @@ bool ActivateBestChain(CValidationState& state, const CBlock* pblock, bool fAlre
31013089
// Notifications/callbacks that can run without cs_main
31023090
if(connman)
31033091
connman->SetBestHeight(pindexNewTip->nHeight);
3104-
if(pbudget)
3105-
pbudget->SetBestHeight(pindexNewTip->nHeight);
31063092

31073093
// Always notify the UI if a new block tip was connected
31083094
if (pindexFork != pindexNewTip) {
@@ -4171,8 +4157,8 @@ bool ProcessNewBlock(CValidationState& state, CNode* pfrom, const CBlock* pblock
41714157

41724158
// For now, we need the tip to know whether p2pkh block signatures are accepted or not.
41734159
// After 5.0, this can be removed and replaced by the enforcement block time.
4174-
const CBlockIndex* pindexPrev = chainActive.Tip();
4175-
const bool enableP2PKH = (pindexPrev) ? consensus.NetworkUpgradeActive(pindexPrev->nHeight + 1, Consensus::UPGRADE_V5_DUMMY) : false;
4160+
const int newHeight = chainActive.Height() + 1;
4161+
const bool enableP2PKH = consensus.NetworkUpgradeActive(newHeight, Consensus::UPGRADE_V5_DUMMY);
41764162
if (!CheckBlockSignature(*pblock, enableP2PKH))
41774163
return error("%s : bad proof-of-stake block signature", __func__);
41784164

@@ -4199,7 +4185,7 @@ bool ProcessNewBlock(CValidationState& state, CNode* pfrom, const CBlock* pblock
41994185
if (pindex && pfrom) {
42004186
mapBlockSource[pindex->GetBlockHash ()] = pfrom->GetId ();
42014187
}
4202-
CheckBlockIndex ();
4188+
CheckBlockIndex();
42034189
if (!ret) {
42044190
// Check spamming
42054191
if(pindex && pfrom && GetBoolArg("-blockspamfilter", DEFAULT_BLOCK_SPAM_FILTER)) {
@@ -4223,13 +4209,13 @@ bool ProcessNewBlock(CValidationState& state, CNode* pfrom, const CBlock* pblock
42234209
}
42244210
}
42254211

4226-
if (!ActivateBestChain(state, pblock, checked, connman, &budget))
4212+
if (!ActivateBestChain(state, pblock, checked, connman))
42274213
return error("%s : ActivateBestChain failed", __func__);
42284214

42294215
if (!fLiteMode) {
42304216
if (masternodeSync.RequestedMasternodeAssets > MASTERNODE_SYNC_LIST) {
4231-
masternodePayments.ProcessBlock(GetHeight() + 10);
4232-
budget.NewBlock();
4217+
masternodePayments.ProcessBlock(newHeight + 10);
4218+
budget.NewBlock(newHeight);
42334219
}
42344220
}
42354221

@@ -4245,7 +4231,7 @@ bool ProcessNewBlock(CValidationState& state, CNode* pfrom, const CBlock* pblock
42454231
pwalletMain->AutoCombineDust(connman);
42464232
}
42474233

4248-
LogPrintf("%s : ACCEPTED Block %ld in %ld milliseconds with size=%d\n", __func__, GetHeight(), GetTimeMillis() - nStartTime,
4234+
LogPrintf("%s : ACCEPTED Block %ld in %ld milliseconds with size=%d\n", __func__, newHeight, GetTimeMillis() - nStartTime,
42494235
GetSerializeSize(*pblock, SER_DISK, CLIENT_VERSION));
42504236

42514237
return true;

src/main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ double ConvertBitsToDouble(unsigned int nBits);
245245
int64_t GetMasternodePayment();
246246
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader* pblock, bool fProofOfStake);
247247

248-
bool ActivateBestChain(CValidationState& state, const CBlock* pblock = NULL, bool fAlreadyChecked = false, CConnman* connman = nullptr, CBudgetManager* pbudget = nullptr);
248+
bool ActivateBestChain(CValidationState& state, const CBlock* pblock = NULL, bool fAlreadyChecked = false, CConnman* connman = nullptr);
249249
CAmount GetBlockValue(int nHeight);
250250

251251
/** Create a new block index entry for a given block hash */

src/masternode-budget.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,10 @@ bool CBudgetManager::AddAndRelayProposalVote(const CBudgetVote& vote, std::strin
895895
return false;
896896
}
897897

898-
void CBudgetManager::NewBlock()
898+
void CBudgetManager::NewBlock(int height)
899899
{
900+
SetBestHeight(height);
901+
900902
if (masternodeSync.RequestedMasternodeAssets <= MASTERNODE_SYNC_BUDGET) return;
901903

902904
if (strBudgetMode == "suggest") { //suggest the budget we see

src/masternode-budget.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class CBudgetManager
227227

228228
void SetSynced(bool synced);
229229

230-
// Memory Only. Updated in ActivateBestChain
230+
// Memory Only. Updated in NewBlock (blocks arrive in order)
231231
std::atomic<int> nBestHeight;
232232

233233
public:
@@ -276,7 +276,7 @@ class CBudgetManager
276276
int GetBestHeight() const { return nBestHeight.load(std::memory_order_acquire); }
277277

278278
void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
279-
void NewBlock();
279+
void NewBlock(int height);
280280
CBudgetProposal* FindProposal(const uint256& nHash);
281281
// finds the proposal with the given name, with highest net yes count.
282282
const CBudgetProposal* FindProposalByName(const std::string& strProposalName) const;

src/rpc/blockchain.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,8 @@ UniValue invalidateblock(const JSONRPCRequest& request)
11471147
}
11481148

11491149
if (state.IsValid()) {
1150-
ActivateBestChain(state, nullptr, false, g_connman.get(), &budget);
1150+
ActivateBestChain(state, nullptr, false, g_connman.get());
1151+
budget.SetBestHeight(WITH_LOCK(cs_main, return chainActive.Height(); ));
11511152
}
11521153

11531154
if (!state.IsValid()) {
@@ -1185,7 +1186,8 @@ UniValue reconsiderblock(const JSONRPCRequest& request)
11851186
}
11861187

11871188
if (state.IsValid()) {
1188-
ActivateBestChain(state, nullptr, false, g_connman.get(), &budget);
1189+
ActivateBestChain(state, nullptr, false, g_connman.get());
1190+
budget.SetBestHeight(WITH_LOCK(cs_main, return chainActive.Height(); ));
11891191
}
11901192

11911193
if (!state.IsValid()) {

0 commit comments

Comments
 (0)