Skip to content

Commit

Permalink
wallet: refactor GetDepthInMainChain to not return the block index.
Browse files Browse the repository at this point in the history
It's only used for coin stake inputs selection and nowhere else.
  • Loading branch information
furszy committed Mar 10, 2021
1 parent e7c8ca6 commit 33f4788
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
22 changes: 5 additions & 17 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2247,14 +2247,14 @@ void CWallet::GetAvailableP2CSCoins(std::vector<COutput>& vCoins) const {
/**
* Test if the transaction is spendable.
*/
bool CheckTXAvailability(const CWalletTx* pcoin, bool fOnlyConfirmed, int& nDepth, const CBlockIndex*& pindexRet)
bool CheckTXAvailability(const CWalletTx* pcoin, bool fOnlyConfirmed, int& nDepth)
{
AssertLockHeld(cs_main);
if (!CheckFinalTx(pcoin->tx)) return false;
if (fOnlyConfirmed && !pcoin->IsTrusted()) return false;
if (pcoin->GetBlocksToMaturity() > 0) return false;

nDepth = pcoin->GetDepthInMainChain(pindexRet);
nDepth = pcoin->GetDepthInMainChain();

// We should not consider coins which aren't at least in our mempool
// It's possible for these to be conflicted via ancestors which we may never be able to detect
Expand All @@ -2263,12 +2263,6 @@ bool CheckTXAvailability(const CWalletTx* pcoin, bool fOnlyConfirmed, int& nDept
return true;
}

bool CheckTXAvailability(const CWalletTx* pcoin, bool fOnlyConfirmed, int& nDepth)
{
const CBlockIndex* pindexRet = nullptr;
return CheckTXAvailability(pcoin, fOnlyConfirmed, nDepth, pindexRet);
}

bool CWallet::GetMasternodeVinAndKeys(CTxIn& txinRet, CPubKey& pubKeyRet, CKey& keyRet, std::string strTxHash, std::string strOutputIndex, std::string& strError)
{
// wait for reindex and/or import to finish
Expand Down Expand Up @@ -2547,13 +2541,13 @@ bool CWallet::StakeableCoins(std::vector<CStakeableOutput>* pCoins)

// Check if the tx is selectable
int nDepth;
const CBlockIndex* pindex = nullptr;
if (!CheckTXAvailability(pcoin, true, nDepth, pindex))
if (!CheckTXAvailability(pcoin, true, nDepth))
continue;

// Check min depth requirement for stake inputs
if (nDepth < Params().GetConsensus().nStakeMinDepth) continue;

const CBlockIndex* pindex = nullptr;
for (unsigned int index = 0; index < pcoin->tx->vout.size(); index++) {

auto res = CheckOutputAvailability(
Expand All @@ -2571,6 +2565,7 @@ bool CWallet::StakeableCoins(std::vector<CStakeableOutput>* pCoins)

// found valid coin
if (!pCoins) return true;
if (!pindex) pindex = mapBlockIndex.at(pcoin->m_confirm.hashBlock);
pCoins->emplace_back(CStakeableOutput(pcoin, (int) index, nDepth, res.spendable, res.solvable, pindex));
}
}
Expand Down Expand Up @@ -4301,12 +4296,6 @@ void CWalletTx::SetConf(Status status, const uint256& blockHash, int posInBlock)
}

int CWalletTx::GetDepthInMainChain() const
{
const CBlockIndex* pindexRet = nullptr;
return GetDepthInMainChain(pindexRet);
}

int CWalletTx::GetDepthInMainChain(const CBlockIndex*& pindexRet) const
{
if (isUnconfirmed() || isAbandoned()) return 0;
AssertLockHeld(cs_main);
Expand All @@ -4321,7 +4310,6 @@ int CWalletTx::GetDepthInMainChain(const CBlockIndex*& pindexRet) const
if (!pindex || !chainActive.Contains(pindex)) {
nResult = 0;
} else {
pindexRet = pindex;
nResult = (isConflicted() ? (-1) : 1) * (chainActive.Height() - pindex->nHeight + 1);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,6 @@ class CWalletTx
* 0 : in memory pool, waiting to be included in a block
* >=1 : this many blocks deep in the main chain
*/
int GetDepthInMainChain(const CBlockIndex*& pindexRet) const;
int GetDepthInMainChain() const;
bool IsInMainChainImmature() const;
int GetBlocksToMaturity() const;
Expand Down

0 comments on commit 33f4788

Please sign in to comment.