Skip to content

Commit

Permalink
fix coinstake size fee allowance
Browse files Browse the repository at this point in the history
  • Loading branch information
backpacker69 committed May 7, 2024
1 parent 1d3e82d commit 89823cc
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3787,7 +3787,7 @@ bool CWallet::CreateCoinStake(ChainstateManager& chainman, const CWallet* pwalle
double difficulty = GetDifficulty(GetLastBlockIndex(chainman.ActiveChain().Tip(), true), chainman.ActiveChain().Tip());
CAmount supply = chainman.ActiveChain().Tip()->nMoneySupply;
int maxDayWeight = (params.nStakeMaxAge - params.nStakeMinAge) / (60*60*24);
double securityLevel = (long(2) << 31)*difficulty / maxDayWeight / (supply/COIN) / params.nStakeTargetSpacing;
double securityLevel = (uint64_t(2) << 31)*difficulty / maxDayWeight / (supply/COIN) / params.nStakeTargetSpacing;
bool isTestnet = Params().NetworkIDString() != CBaseChainParams::MAIN;
CAmount nTargetOutputAmount = SecurityToOptimalFraction(securityLevel, isTestnet)*supply;

Expand Down Expand Up @@ -3826,8 +3826,8 @@ bool CWallet::CreateCoinStake(ChainstateManager& chainman, const CWallet* pwalle
if (((pcoin->txout.scriptPubKey == scriptPubKeyKernel || pcoin->txout.scriptPubKey == txNew.vout[1].scriptPubKey))
&& pcoin->outpoint.hash != txNew.vin[0].prevout.hash)
{
// Stop adding more inputs if already too many inputs and we are above target
if ((txNew.vin.size() >= MAX_COINSTAKE_INPUTS) && (nCredit > nTargetOutputAmount))
// Stop adding more inputs if already too many inputs and we are above target or have minter key to add
if ((txNew.vin.size() >= MAX_COINSTAKE_INPUTS) && (bMinterKey || nCredit > nTargetOutputAmount))
break;
// Stop adding more inputs if we are below target but about to exceed 1kb
if ((txNew.vin.size() >= 8) && (nCredit < nTargetOutputAmount))
Expand Down Expand Up @@ -3861,7 +3861,7 @@ bool CWallet::CreateCoinStake(ChainstateManager& chainman, const CWallet* pwalle

CAmount nMinFee = 0;
int maxOutputs = 30;
CAmount nMinFeeBase = (IsProtocolV07(txNew.nTime) ? MIN_TX_FEE : MIN_TX_FEE_PREV7);
CAmount nSubsidizedFee = PERKB_TX_FEE;
while(true)
{
// Clear outputs
Expand Down Expand Up @@ -3891,7 +3891,9 @@ bool CWallet::CreateCoinStake(ChainstateManager& chainman, const CWallet* pwalle
// Add remainder to first output
txNew.vout.push_back(CTxOut(outValue + (i == 0 ? remainder : 0), scriptPubKeyOut));
// make sure transaction below 1kb
if (::GetSerializeSize(txNew, SER_NETWORK, PROTOCOL_VERSION) > 1024) {
if (::GetSerializeSize(txNew, SER_NETWORK, PROTOCOL_VERSION) > 1000) {
if (!i)
return error("CreateCoinStake : failed to create coinstake");
// Remove output that goes over max size and set maxOutputs so that the logic can be re-run
txNew.vout.pop_back();
outputsOk = false;
Expand Down Expand Up @@ -3935,7 +3937,12 @@ bool CWallet::CreateCoinStake(ChainstateManager& chainman, const CWallet* pwalle
return error("CreateCoinStake : exceeded coinstake size limit");

// Check enough fee is paid
CAmount nNeededFee = GetMinFee(CTransaction(txNew), txNew.nTime) - nMinFeeBase;
CAmount nTxnFee = GetMinFee(CTransaction(txNew), txNew.nTime);
CAmount nNeededFee = 0;

if (nTxnFee > nSubsidizedFee)
nNeededFee = nTxnFee - nSubsidizedFee;

// If not enough fee paid or max outputs have changed requiring a new split and fee, run again
if (nMinFee < nNeededFee || !outputsOk) {
nMinFee = nNeededFee;
Expand Down

0 comments on commit 89823cc

Please sign in to comment.