diff --git a/src/coldreward/coldrewardtracker.cpp b/src/coldreward/coldrewardtracker.cpp index 62c77e25f0..0c94c5b289 100644 --- a/src/coldreward/coldrewardtracker.cpp +++ b/src/coldreward/coldrewardtracker.cpp @@ -100,11 +100,11 @@ void ColdRewardTracker::endPersistedTransaction() } const std::vector> ColdRewardTracker::getBalances() { - const std::map> ranges = allRangesGetter(); + const std::map> ranges = getAllRanges(); std::vector> result; for(const auto& r: ranges) { - CAmount balance = balanceGetter(r.first); + CAmount balance = getBalance(r.first); auto add = std::string(r.first.begin(), r.first.end()); LogPrintf("%s Tracking addr %s with balance %i \n", __func__, add, balance); result.push_back(std::make_pair(r.first, balance)); @@ -142,22 +142,19 @@ boost::optional ColdRewardTracker::GetLastCheckpoint(const std::map& addressRanges) +unsigned ColdRewardTracker::ExtractRewardMultiplierFromRanges(int currentBlockHeight, const std::vector& ar) { std::vector rewardMultipliers; - const auto& ar = addressRanges; - for(unsigned i = 0; i < ar.size(); i++) { const unsigned idx = ar.size() - i - 1; - // Now we're getting the elig addr every block - // AssertTrue(currentBlockHeight > ar[idx].getStart(), std::string(__func__), "You can't get the reward for the past"); - // AssertTrue(currentBlockHeight > ar[idx].getEnd(), std::string(__func__), "You can't get the reward for the past"); + // AssertTrue(currentBlockHeight >= ar[idx].getStart(), std::string(__func__), "You can't get the reward for the past"); + // AssertTrue(currentBlockHeight >= ar[idx].getEnd(), std::string(__func__), "You can't get the reward for the past"); // collect all reward multipliers that are > 0 over the last periods, to figure out the final reward const int startDistance = currentBlockHeight - ar[idx].getStart(); - const int endDistance = currentBlockHeight - ar[idx].getEnd(); + const int endDistance = currentBlockHeight - ar[idx].getEnd(); if(ar[idx].getRewardMultiplier() > 0) { // collect all changes in balance @@ -196,7 +193,7 @@ unsigned ColdRewardTracker::ExtractRewardMultiplierFromRanges(int currentBlockHe std::vector> ColdRewardTracker::getEligibleAddresses(int currentBlockHeight) { - const std::map> ranges = allRangesGetter(); + const std::map> ranges = getAllRanges(); std::vector> result; for(const auto& r: ranges) { @@ -255,7 +252,7 @@ void ColdRewardTracker::addAddressTransaction(int blockHeight, const AddressType // we add a [blockHeight, blockHeight] range as a marker that the balance has crossed a threshold multiple ranges.push_back(BlockHeightRange(blockHeight, blockHeight, currentMultiplier, ranges.back().getRewardMultiplier())); } else { - LogPrintf("%s Previous range value [%d, %d]\n", __func__, ranges.back().getStart(), ranges.back().getEnd()); + LogPrintf("%s Previous range value for [%s] is [%d, %d]\n", __func__, std::string(address.begin(), address.end()), ranges.back().getStart(), ranges.back().getEnd()); ranges.back().newEnd(blockHeight); } } @@ -356,7 +353,12 @@ void ColdRewardTracker::setAllRangesGetter(const std::function>& ColdRewardTracker::getAllRanges() const +const std::map>& ColdRewardTracker::getAllRanges() { + if (!addressesRanges.empty()) { + return addressesRanges; + } + + addressesRanges = allRangesGetter(); return addressesRanges; } diff --git a/src/coldreward/coldrewardtracker.h b/src/coldreward/coldrewardtracker.h index 13c4763728..9e832cedc2 100644 --- a/src/coldreward/coldrewardtracker.h +++ b/src/coldreward/coldrewardtracker.h @@ -127,7 +127,7 @@ class ColdRewardTracker void setAllRangesGetter(const std::function>()>& func); - const std::map>& getAllRanges() const; + const std::map>& getAllRanges(); const std::vector> getBalances(); void setGvrThreshold(const CAmount& amount) { diff --git a/src/txdb.cpp b/src/txdb.cpp index f4c5ab7c9e..bbae8f99e5 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -679,7 +679,7 @@ bool CBlockTreeDB::WriteRewardTrackerUndo(const ColdRewardUndo& rewardUndo) bool CBlockTreeDB::WriteLastTrackedHeight(std::int64_t lastHeight) { CDBBatch batch(*this); - LogPrintf("%s Writting last tracked height %d\n", __func__, lastHeight); + LogPrintf("%s Writing last tracked height %d\n", __func__, lastHeight); batch.Write(std::make_pair(DB_LAST_TRACKED_HEIGHT, 0), lastHeight); return WriteBatch(batch); diff --git a/src/validation.cpp b/src/validation.cpp index 8f704dcecc..ffe29ff262 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2386,7 +2386,7 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockI !pblocktree->WriteLastTrackedHeight(pindex->pprev->nHeight)) { return DISCONNECT_FAILED; } else { - LogPrintf("%s Writting last tracked height %d\n", __func__, pindex->pprev->nHeight); + LogPrintf("%s Writing last tracked height %d\n", __func__, pindex->pprev->nHeight); } } @@ -3338,6 +3338,16 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state, } else { // The staker was not eligible so the carried forward has to be set // The carried forward is set since there were no gvr output + LogPrintf("THE STAKER OF THE BLOCK IS %s\n", EncodeDestination(stakerAddrDest)); + LogPrintf("THE SIZE OF THE ELIG MAP IS %d\n", eligibleAddresses.size()); + + for (auto& elig: eligibleAddresses) { + LogPrintf("ELIGIBLE ADRR WERE (ADDR=%s, MUL=%d)\n", std::string(elig.first.begin(), elig.first.end()), elig.second); + } + + eligibleAddresses = rewardTracker.getEligibleAddresses(pindex->nHeight); + LogPrintf("THE SIZE OF THE ELIG MAP AFTER RETRY IS %d\n", eligibleAddresses.size()); + if (!txCoinstake->GetGvrFundCfwd(ngvrCfwdCheck)) { LogPrintf("ERROR: %s: Coinstake gvr cfwd must be set.\n", __func__); return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cs-cfwd"); @@ -3390,20 +3400,24 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state, std::int64_t readHeight; - if (pindex->nHeight >= 1 && pindex->nHeight >= consensus.automatedGvrActivationHeight && !pblocktree->ReadLastTrackedHeight(readHeight)) { - if (pindex->nHeight == 1 || pindex->nHeight == consensus.automatedGvrActivationHeight) { - readHeight = 0; - } else { + if (pindex->nHeight >= 1 && pindex->nHeight >= consensus.automatedGvrActivationHeight) { + + if (!pblocktree->ReadLastTrackedHeight(readHeight)) { LogPrintf("%s Impossible to read last tracked height attempted height %s\n", __func__, pindex->nHeight); return false; } + if (pindex->nHeight == 1 || pindex->nHeight == consensus.automatedGvrActivationHeight) { + readHeight = 0; + } } + // Track the inputs/outputs for any balance changes // Track out/in only after verifydb is done if (!fVerifyingDB) { - if (readHeight < pindex->nHeight && pindex->nHeight >= consensus.automatedGvrActivationHeight) { + LogPrintf("TRACKING CURRENT HEIGHT %d AND READ HEIGHT = %d\n", pindex->nHeight, readHeight); + if (pindex->nHeight >= consensus.automatedGvrActivationHeight) { LogPrintf("%s Last tracked Height %d, Current connecting height %d\n", __func__, readHeight, pindex->nHeight); @@ -3788,6 +3802,7 @@ std::map> allRangesGetter() { while (pcursor->Valid()) { if (ShutdownRequested()) { + LogPrintf("SHUTDOWN REQUESTED RETURNING EMPTY BLOCK RANGES"); return std::map>(); } std::pair key; @@ -3801,6 +3816,10 @@ std::map> allRangesGetter() { break; } } + + if (ranges.empty()) { + LogPrintf("RETRIEVED FOR SOME REASONS EMPTY RANGES FROM DB"); + } return ranges; } @@ -6459,6 +6478,43 @@ bool CChainState::LoadChainTip(const CChainParams& chainparams) m_chain.SetTip(pindex); PruneBlockIndexCandidates(); + std::int64_t readHeight; + int currentHeight = m_chain.Height(); + ColdRewardUndo undoData; + ColdRewardTracker& tracker = initColdReward(); + + LogPrintf("REMOVING COLD REWARD DURING STARTUP %d\n", currentHeight); + if (!pblocktree->ReadLastTrackedHeight(readHeight)) { + LogPrintf("Can't read last tracked height from disk"); + } + + LogPrintf("READ HEIGHT AFTER STARTUP IS %d\n", readHeight); + + if (currentHeight >= 1 && currentHeight >= chainparams.GetConsensus().automatedGvrActivationHeight) { + if (currentHeight == 1 || currentHeight == chainparams.GetConsensus().automatedGvrActivationHeight) { + readHeight = 0; + } + + pblocktree->ReadRewardTrackerUndo(undoData, 1); + + while (readHeight > currentHeight) { + for(const auto& output: undoData.outputs.at(readHeight)) { + const auto addr = std::string(output.first.begin(), output.first.end()); + LogPrintf("%s Remove tracked output %d of addr %s \n", __func__, output.second, addr); + rewardTracker.removeAddressTransaction(readHeight, output.first, output.second); + } + + for(const auto& input: undoData.inputs.at(readHeight)) { + const auto addr = std::string(input.first.begin(), input.first.end()); + LogPrintf("%s Remove tracked input %d of addr %s \n", __func__, input.second, addr); + rewardTracker.removeAddressTransaction(readHeight, input.first, - input.second); + } + readHeight--; + } + + pblocktree->WriteLastTrackedHeight(readHeight); + } + tip = m_chain.Tip(); LogPrintf("Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n", tip->GetBlockHash().ToString(), diff --git a/src/wallet/rpchdwallet.cpp b/src/wallet/rpchdwallet.cpp index 6164f6d4ca..e867eeac2c 100644 --- a/src/wallet/rpchdwallet.cpp +++ b/src/wallet/rpchdwallet.cpp @@ -6726,7 +6726,7 @@ static UniValue debugwallet(const JSONRPCRequest &request) bool clear_stakes_seen = false; bool downgrade_wallets = false; bool exit_ibd = false; - CAmount max_frozen_output_spendable = pwallet->GetLastBlockHeight() >= Params().GetConsensus().nBlockRewardCorrectionHeight ? + CAmount max_frozen_output_spendable = ::ChainActive().Height() >= Params().GetConsensus().nBlockRewardCorrectionHeight ? Params().GetConsensus().m_max_tainted_value_out_increased : Params().GetConsensus().m_max_tainted_value_out; int64_t time_now = GetAdjustedTime();