diff --git a/src/masternode-payments.cpp b/src/masternode-payments.cpp index cb6be6bbf84e..226ffe882da5 100644 --- a/src/masternode-payments.cpp +++ b/src/masternode-payments.cpp @@ -327,7 +327,20 @@ void CMasternodePayments::FillBlockPayee(CMutableTransaction& txNew, int64_t nFe //subtract mn payment from the stake reward if (!txNew.vout[1].IsZerocoinMint()) - txNew.vout[i - 1].nValue -= masternodePayment; + if (i == 2) { + // Majority of cases; do it quick and move on + txNew.vout[i - 1].nValue -= masternodePayment; + } else if (i > 2) { + // special case, stake is split between (i-1) outputs + unsigned int outputs = i-1; + CAmount mnPaymentSplit = masternodePayment / outputs; + CAmount mnPaymentRemainder = masternodePayment - (mnPaymentSplit * outputs); + for (unsigned int j=1; j<=outputs; j++) { + txNew.vout[j].nValue -= mnPaymentSplit; + } + // in case it's not an even division, take the last bit of dust from the last one + txNew.vout[outputs].nValue -= mnPaymentRemainder; + } } else { txNew.vout.resize(2); txNew.vout[1].scriptPubKey = payee; diff --git a/src/stakeinput.cpp b/src/stakeinput.cpp index adf37c636dc6..fe19fb7134b7 100644 --- a/src/stakeinput.cpp +++ b/src/stakeinput.cpp @@ -224,8 +224,17 @@ bool CPivStake::CreateTxOuts(CWallet* pwallet, std::vector& vout, CAmoun vout.emplace_back(CTxOut(0, scriptPubKey)); // Calculate if we need to split the output - if (nTotal / 2 > (CAmount)(pwallet->nStakeSplitThreshold * COIN)) - vout.emplace_back(CTxOut(0, scriptPubKey)); + int nSplit = nTotal / (static_cast(pwallet->nStakeSplitThreshold * COIN)); + if (nSplit > 1) { + // if nTotal is twice or more of the threshold; create more outputs + int txSizeMax = MAX_STANDARD_TX_SIZE >> 11; // limit splits to <10% of the max TX size (/2048) + if (nSplit > txSizeMax) + nSplit = txSizeMax; + for (int i = nSplit; i > 1; i--) { + LogPrintf("%s: StakeSplit: nTotal = %d; adding output %d of %d\n", __func__, nTotal, (nSplit-i)+2, nSplit); + vout.emplace_back(CTxOut(0, scriptPubKey)); + } + } return true; } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 256a6aa3873f..fb23ab03cf69 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2425,11 +2425,19 @@ bool CWallet::CreateCoinStake( CAmount nMinFee = 0; if (!stakeInput->IsZPIV()) { // Set output amount - if (txNew.vout.size() == 3) { - txNew.vout[1].nValue = ((nCredit - nMinFee) / 2 / CENT) * CENT; - txNew.vout[2].nValue = nCredit - nMinFee - txNew.vout[1].nValue; - } else - txNew.vout[1].nValue = nCredit - nMinFee; + unsigned int outputs = txNew.vout.size() - 1; + CAmount nRemaining = nCredit - nMinFee; + if (outputs > 1) { + // Split the stake across the outputs + CAmount nShare = nRemaining / outputs; + for (int i = 1; i < outputs; i++) { + // loop through all but the last one. + txNew.vout[i].nValue = nShare; + nRemaining -= nShare; + } + } + // put the remaining on the last output (which all into the first if only one output) + txNew.vout[outputs].nValue += nRemaining; } // Limit size