Skip to content

Commit f935801

Browse files
committed
wallet: Implement a slightly different StakeableCoins, initializing and returning CStakeableOutput.
1 parent bbc7d77 commit f935801

File tree

6 files changed

+48
-21
lines changed

6 files changed

+48
-21
lines changed

src/miner.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ bool CreateCoinbaseTx(CBlock* pblock, const CScript& scriptPubKeyIn, CBlockIndex
168168
return true;
169169
}
170170

171-
bool SolveProofOfStake(CBlock* pblock, CBlockIndex* pindexPrev, CWallet* pwallet, std::vector<COutput>* availableCoins)
171+
bool SolveProofOfStake(CBlock* pblock, CBlockIndex* pindexPrev, CWallet* pwallet, std::vector<CStakeableOutput>* availableCoins)
172172
{
173173
boost::this_thread::interruption_point();
174174
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock);
@@ -194,7 +194,7 @@ bool SolveProofOfStake(CBlock* pblock, CBlockIndex* pindexPrev, CWallet* pwallet
194194
return true;
195195
}
196196

197-
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet, bool fProofOfStake, std::vector<COutput>* availableCoins)
197+
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet, bool fProofOfStake, std::vector<CStakeableOutput>* availableCoins)
198198
{
199199
// Create new block
200200
std::unique_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
@@ -556,7 +556,7 @@ bool fGenerateBitcoins = false;
556556
bool fStakeableCoins = false;
557557
int nMintableLastCheck = 0;
558558

559-
void CheckForCoins(CWallet* pwallet, const int minutes, std::vector<COutput>* availableCoins)
559+
void CheckForCoins(CWallet* pwallet, const int minutes, std::vector<CStakeableOutput>* availableCoins)
560560
{
561561
//control the amount of times the client will check for mintable coins
562562
int nTimeNow = GetTime();
@@ -581,7 +581,7 @@ void BitcoinMiner(CWallet* pwallet, bool fProofOfStake)
581581
}
582582

583583
// Available UTXO set
584-
std::vector<COutput> availableCoins;
584+
std::vector<CStakeableOutput> availableCoins;
585585
unsigned int nExtraNonce = 0;
586586

587587
while (fGenerateBitcoins || fProofOfStake) {

src/miner.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class CBlock;
1515
class CBlockHeader;
1616
class CBlockIndex;
17-
class COutput;
17+
class CStakeableOutput;
1818
class CReserveKey;
1919
class CScript;
2020
class CWallet;
@@ -24,7 +24,7 @@ static const bool DEFAULT_PRINTPRIORITY = false;
2424
struct CBlockTemplate;
2525

2626
/** Generate a new block, without valid proof-of-work */
27-
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet, bool fProofOfStake, std::vector<COutput>* availableCoins = nullptr);
27+
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet, bool fProofOfStake, std::vector<CStakeableOutput>* availableCoins = nullptr);
2828
/** Modify the extranonce in a block */
2929
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
3030
/** Check mined block */

src/rpc/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ UniValue generate(const JSONRPCRequest& request)
160160
while (nHeight < nHeightEnd && !ShutdownRequested()) {
161161

162162
// Get available coins
163-
std::vector<COutput> availableCoins;
163+
std::vector<CStakeableOutput> availableCoins;
164164
if (fPoS && !pwalletMain->StakeableCoins(&availableCoins)) {
165165
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "No available coins to stake");
166166
}

src/rpc/misc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ UniValue getstakingstatus(const JSONRPCRequest& request)
765765
obj.pushKV("haveconnections", (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) > 0));
766766
obj.pushKV("mnsync", !masternodeSync.NotCompleted());
767767
obj.pushKV("walletunlocked", !pwalletMain->IsLocked());
768-
std::vector<COutput> vCoins;
768+
std::vector<CStakeableOutput> vCoins;
769769
pwalletMain->StakeableCoins(&vCoins);
770770
obj.pushKV("stakeablecoins", (int)vCoins.size());
771771
obj.pushKV("stakingbalance", ValueFromAmount(pwalletMain->GetStakingBalance(fColdStaking)));

src/wallet/wallet.cpp

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,17 +2186,45 @@ static void ApproximateBestSubset(std::vector<std::pair<CAmount, std::pair<const
21862186
}
21872187
}
21882188

2189-
2190-
bool CWallet::StakeableCoins(std::vector<COutput>* pCoins)
2189+
bool CWallet::StakeableCoins(std::vector<CStakeableOutput>* pCoins)
21912190
{
2192-
const bool fIncludeCold = (sporkManager.IsSporkActive(SPORK_17_COLDSTAKING_ENFORCEMENT) &&
2191+
const bool fIncludeColdStaking = (sporkManager.IsSporkActive(SPORK_17_COLDSTAKING_ENFORCEMENT) &&
21932192
GetBoolArg("-coldstaking", DEFAULT_COLDSTAKING));
21942193

2195-
return AvailableCoins(pCoins,
2196-
nullptr, // coin control
2197-
false, // fIncludeDelegated
2198-
fIncludeCold, // fIncludeColdStaking
2199-
STAKEABLE_COINS); // coin type
2194+
LOCK2(cs_main, cs_wallet);
2195+
for (const auto& it : mapWallet) {
2196+
const uint256& wtxid = it.first;
2197+
const CWalletTx* pcoin = &(it).second;
2198+
2199+
// Check if the tx is selectable
2200+
int nDepth;
2201+
const CBlockIndex* pindex = nullptr;
2202+
if (!CheckTXAvailability(pcoin, true, false, nDepth, pindex))
2203+
continue;
2204+
2205+
// Check min depth requirement for stake inputs
2206+
if (nDepth < Params().GetConsensus().nStakeMinDepth) continue;
2207+
2208+
for (unsigned int index = 0; index < pcoin->vout.size(); index++) {
2209+
2210+
auto res = CheckOutputAvailability(
2211+
pcoin->vout[index],
2212+
index,
2213+
wtxid,
2214+
STAKEABLE_COINS,
2215+
nullptr, // coin control
2216+
false, // fIncludeDelegated
2217+
fIncludeColdStaking,
2218+
false);
2219+
2220+
if (!res.available) continue;
2221+
2222+
// found valid coin
2223+
if (!pCoins) return true;
2224+
pCoins->emplace_back(CStakeableOutput(pcoin, (int) index, nDepth, res.spendable, res.solvable, pindex));
2225+
}
2226+
}
2227+
return (pCoins && !pCoins->empty());
22002228
}
22012229

22022230
bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*, unsigned int> >& setCoinsRet, CAmount& nValueRet) const
@@ -2704,8 +2732,7 @@ bool CWallet::CreateCoinStake(
27042732
unsigned int nBits,
27052733
CMutableTransaction& txNew,
27062734
int64_t& nTxNewTime,
2707-
std::vector<COutput>* availableCoins
2708-
)
2735+
std::vector<CStakeableOutput>* availableCoins)
27092736
{
27102737

27112738
const Consensus::Params& consensus = Params().GetConsensus();
@@ -2727,7 +2754,7 @@ bool CWallet::CreateCoinStake(
27272754
CScript scriptPubKeyKernel;
27282755
bool fKernelFound = false;
27292756
int nAttempts = 0;
2730-
for (const COutput &out : *availableCoins) {
2757+
for (const auto& out : *availableCoins) {
27312758
CPivStake stakeInput;
27322759
stakeInput.SetPrevout(out.tx->vout[out.i], COutPoint(out.tx->GetHash(), out.i));
27332760

src/wallet/wallet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
446446
bool SelectCoinsToSpend(const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, std::set<std::pair<const CWalletTx*, unsigned int> >& setCoinsRet, CAmount& nValueRet, const CCoinControl* coinControl = nullptr) const;
447447
bool SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*, unsigned int> >& setCoinsRet, CAmount& nValueRet) const;
448448
//! >> Available coins (staking)
449-
bool StakeableCoins(std::vector<COutput>* pCoins = nullptr);
449+
bool StakeableCoins(std::vector<CStakeableOutput>* pCoins = nullptr);
450450
//! >> Available coins (P2CS)
451451
void GetAvailableP2CSCoins(std::vector<COutput>& vCoins) const;
452452

@@ -628,7 +628,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
628628
unsigned int nBits,
629629
CMutableTransaction& txNew,
630630
int64_t& nTxNewTime,
631-
std::vector<COutput>* availableCoins);
631+
std::vector<CStakeableOutput>* availableCoins);
632632
bool MultiSend();
633633
void AutoCombineDust(CConnman* connman);
634634

0 commit comments

Comments
 (0)