Skip to content

Commit

Permalink
pos: Perform some simplifications in CreateCoinStake()
Browse files Browse the repository at this point in the history
  • Loading branch information
lateminer committed Jan 22, 2024
1 parent 0207160 commit 5f77e59
Showing 1 changed file with 38 additions and 54 deletions.
92 changes: 38 additions & 54 deletions src/wallet/staking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ bool CreateCoinStake(CWallet& wallet, unsigned int nBits, int64_t nSearchInterva
if (nCredit + pcoin.first->tx->vout[pcoin.second].nValue > nBalance - wallet.m_reserve_balance)
break;
// Do not add additional significant input
if (pcoin.first->tx->vout[pcoin.second].nValue > GetStakeCombineThreshold())
if (pcoin.first->tx->vout[pcoin.second].nValue >= GetStakeCombineThreshold())
continue;

txNew.vin.push_back(CTxIn(pcoin.first->GetHash(), pcoin.second));
Expand Down Expand Up @@ -470,67 +470,51 @@ bool CreateCoinStake(CWallet& wallet, unsigned int nBits, int64_t nSearchInterva
if (isDevFundEnabled)
txNew.vout.push_back(CTxOut(0, Params().GetDevRewardScript()));

while (true)
// Set output amount
if (txNew.vout.size() == (isDevFundEnabled ? 4u : 3u) + bMinterKey) {
txNew.vout[1 + bMinterKey].nValue = (nCredit / 2 / CENT) * CENT;
txNew.vout[2 + bMinterKey].nValue = nCredit - txNew.vout[1 + bMinterKey].nValue;
if (isDevFundEnabled)
txNew.vout[3 + bMinterKey].nValue = nDevCredit;
}
else
{
// Set output amount
txNew.vout[1 + bMinterKey].nValue = nCredit;
if (isDevFundEnabled)
{
if (txNew.vout.size() == 4u + bMinterKey)
{
txNew.vout[1 + bMinterKey].nValue = (nCredit / 2 / CENT) * CENT;
txNew.vout[2 + bMinterKey].nValue = nCredit - txNew.vout[1 + bMinterKey].nValue;
txNew.vout[3 + bMinterKey].nValue = nDevCredit;
}
else
{
txNew.vout[1 + bMinterKey].nValue = nCredit;
txNew.vout[2 + bMinterKey].nValue = nDevCredit;
}
}
else
{
if (txNew.vout.size() == 3u + bMinterKey)
{
txNew.vout[1 + bMinterKey].nValue = (nCredit / 2 / CENT) * CENT;
txNew.vout[2 + bMinterKey].nValue = nCredit - txNew.vout[1 + bMinterKey].nValue;
}
else
txNew.vout[1 + bMinterKey].nValue = nCredit;
}
txNew.vout[2 + bMinterKey].nValue = nDevCredit;
}

// Sign
int nIn = 0;
SignatureData empty;
// Sign
int nIn = 0;
SignatureData empty;

if (wallet.IsLegacy()) {
for (const std::pair<const CWalletTx*, unsigned int> &pcoin : vwtxPrev)
{
if (!SignSignature(*wallet.GetLegacyScriptPubKeyMan(), *pcoin.first->tx, txNew, nIn++, SIGHASH_ALL, empty))
return error("CreateCoinStake : failed to sign coinstake");
}
}
else
if (wallet.IsLegacy()) {
for (const std::pair<const CWalletTx*, unsigned int> &pcoin : vwtxPrev)
{
// Fetch previous transactions (inputs):
std::map<COutPoint, Coin> coins;
for (const CTxIn& txin : txNew.vin) {
coins[txin.prevout]; // Create empty map entry keyed by prevout.
}
wallet.chain().findCoins(coins);
// Script verification errors
std::map<int, bilingual_str> input_errors;
int nTime = txNew.nTime;
wallet.SignTransaction(txNew, coins, SIGHASH_ALL, input_errors);
txNew.nTime = nTime;
if (!SignSignature(*wallet.GetLegacyScriptPubKeyMan(), *pcoin.first->tx, txNew, nIn++, SIGHASH_ALL, empty))
return error("CreateCoinStake : failed to sign coinstake");
}
}
else
{
// Fetch previous transactions (inputs):
std::map<COutPoint, Coin> coins;
for (const CTxIn& txin : txNew.vin) {
coins[txin.prevout]; // Create empty map entry keyed by prevout.
}
wallet.chain().findCoins(coins);
// Script verification errors
std::map<int, bilingual_str> input_errors;
int nTime = txNew.nTime;
wallet.SignTransaction(txNew, coins, SIGHASH_ALL, input_errors);
txNew.nTime = nTime;
}

// Limit size
unsigned int nBytes = ::GetSerializeSize(txNew, PROTOCOL_VERSION);
if (nBytes >= 1000000/5)
return error("CreateCoinStake : exceeded coinstake size limit");
// Limit size
unsigned int nBytes = ::GetSerializeSize(txNew, PROTOCOL_VERSION);
if (nBytes >= 1000000/5)
return error("CreateCoinStake : exceeded coinstake size limit");

break;
}
// Successfully generated coinstake
return true;
}
Expand Down

0 comments on commit 5f77e59

Please sign in to comment.