Skip to content

Commit

Permalink
Merge #2782: Wallet: Fix stake split output count calculation
Browse files Browse the repository at this point in the history
6e51d38 exclude masternode payment from stake split calculation (PeterL73)

Pull request description:

  ## Issue being fixed #1571
  The UTXO value of a stake split is sometimes smaller then the stake split threshold
  The help description for `setstakesplitthreshold` states:
  ```
  This will set the stake-split threshold value.
  Whenever a successful stake is found, the stake amount is split across as many outputs
  (each with a value higher than the threshold) as possible.
  ```
  So you would not expect the value of the UTXOs to be lower then the threshold value
  This happens because the number of stake split outputs is calculated including the masternode payment
  An example (for testnet) with a UTXO with a value of 9992 and the stake split threshold set to 500:
  ```
  9992 + 10 (total block reward: 4 stake reward + 6 masternode payment) = 10002
  10002 / 500 = 20.004
  (int) 20.004 = 20
  ```
  However the staker only receives the stake reward (4) so the total amount after staking will be 9996 which is split across 20 UTXOs with a value of 499.8 each
  ## What was done
  The masternode payment is excluded from the stake split output count calculation
  ## How Has This Been Tested
  On testnet 2 UTXOs were made (9992 each) and the stake split threshold was set to 500
  The expected outcome would be a split across 19 UTXOs with a value of 526.1 each
  ```
  9992 + 4 (stake reward) = 9996
  9996 / 500 = 19.992
  (int) 19.992 = 19
  9996 / 19 = 526.1
  ```
  #### Before
  On stake 20 UTXOs, with a value of 499.8 each, were created
  https://testnet.rockdev.org/tx/b966ff04d35ecae596f0dfde3977862268995d63939af3ab90bb172b38c37f20
  #### After
  On stake 19 UTXOs, with a value of 526.1 each were created
  https://testnet.rockdev.org/tx/e80c79ad1ac41aa096b4613462be348e7a1a4a70755c4943087a83be8865de7a

ACKs for top commit:
  Liquid369:
    tACK 6e51d38
  Fuzzbawls:
    ACK 6e51d38

Tree-SHA512: a875c2074cd93136802b2cfcde9ba252ac12b7759657f8041e03e720216b864866297c3d9f9c0b7c03ab62173af23580ec0c6b950f40726fc232a0499e2629e4
  • Loading branch information
Fuzzbawls committed Feb 12, 2023
2 parents 5c55659 + 6e51d38 commit 0bd96c8
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3332,6 +3332,7 @@ bool CWallet::CreateCoinStake(

// Kernel Search
CAmount nCredit;
CAmount nMasternodePayment;
CScript scriptPubKeyKernel;
bool fKernelFound = false;
int nAttempts = 0;
Expand Down Expand Up @@ -3374,10 +3375,11 @@ bool CWallet::CreateCoinStake(

// Add block reward to the credit
nCredit += GetBlockValue(pindexPrev->nHeight + 1);
nMasternodePayment = GetMasternodePayment(pindexPrev->nHeight + 1);

// Create the output transaction(s)
std::vector<CTxOut> vout;
if (!CreateCoinstakeOuts(stakeInput, vout, nCredit)) {
if (!CreateCoinstakeOuts(stakeInput, vout, nCredit - nMasternodePayment)) {
LogPrintf("%s : failed to create output\n", __func__);
it++;
continue;
Expand Down

0 comments on commit 0bd96c8

Please sign in to comment.