@@ -110,6 +110,28 @@ struct CMainSignals {
110110} g_signals;
111111}
112112
113+ bool IsMNCollateralValid (int64_t value, int nHeight) {
114+ if (nHeight < TIERED_MASTERNODES_START_BLOCK) {
115+ return value == 5000 *COIN;
116+ } else {
117+ // Using BOOST_FOREACH for concistency with the rest of the code, everything should be using a plain for from c++ 11 or 17
118+ BOOST_FOREACH (PAIRTYPE (const int , int )& mntier, masternodeTiers)
119+ {
120+ if (value == (mntier.second )*COIN)
121+ return true ;
122+ }
123+ }
124+ return false ;
125+ }
126+
127+ int64_t GetMNCollateral (int nHeight, int tier) {
128+ if (nHeight < TIERED_MASTERNODES_START_BLOCK) {
129+ return 5000 ;
130+ } else {
131+ return masternodeTiers[tier];
132+ }
133+ }
134+
113135void RegisterWallet (CWalletInterface* pwalletIn) {
114136 g_signals.SyncTransaction .connect (boost::bind (&CWalletInterface::SyncTransaction, pwalletIn, _1, _2, _3));
115137 g_signals.EraseTransaction .connect (boost::bind (&CWalletInterface::EraseFromWallet, pwalletIn, _1));
@@ -1368,6 +1390,33 @@ int64_t GetProofOfWorkReward(int nHeight, int64_t nFees)
13681390 return nSubsidy + nFees;
13691391}
13701392
1393+ // Checks if a given reward present in a block is valid
1394+ bool IsPOSRewardValid (int64_t value, int64_t nFees) {
1395+ int nHeight = pindexBest->nHeight +1 ;
1396+ if (nHeight < 20000 ) {
1397+ return value == (25 *COIN + nFees);
1398+ }
1399+ else {
1400+ if (nHeight < TIERED_MASTERNODES_START_BLOCK) {
1401+ return value == (120 *COIN + nFees);
1402+ }
1403+ else {
1404+ // Using BOOST_FOREACH for concistency with the rest of the code
1405+ BOOST_FOREACH (PAIRTYPE (const int , int )& tier, masternodeTierRewards)
1406+ {
1407+ if (value == (tier.second *COIN + POS_REWARD_TIERED_MN*COIN + nFees))
1408+ return true ;
1409+ }
1410+ // The case of a wallet staking with no mns up
1411+ if (value == POS_REWARD_TIERED_MN*COIN + nFees) {
1412+ return true ;
1413+ }
1414+ }
1415+ }
1416+ return false ;
1417+ }
1418+
1419+
13711420// miner's coin stake reward
13721421int64_t GetProofOfStakeReward (const CBlockIndex* pindexPrev, int64_t nCoinAge, int64_t nFees)
13731422{
@@ -1376,15 +1425,11 @@ int64_t GetProofOfStakeReward(const CBlockIndex* pindexPrev, int64_t nCoinAge, i
13761425 if (pindexBest->nHeight +1 > 1 && pindexBest->nHeight +1 <= 20000 ) {
13771426 nSubsidy = 25 * COIN;
13781427 }
1379- else if (pindexBest->nHeight +1 > 20000 && pindexBest->nHeight +1 <= 540000 )
1428+ else if (pindexBest->nHeight +1 > 20000 && pindexBest->nHeight +1 < TIERED_MASTERNODES_START_BLOCK )
13801429 {
13811430 nSubsidy = 120 * COIN;
13821431 }
1383- else if (pindexBest->nHeight +1 > 540000 )
1384- {
1385- nSubsidy = 20 * COIN;
1386- }
1387-
1432+
13881433 return nSubsidy + nFees;
13891434}
13901435
@@ -1403,9 +1448,9 @@ unsigned int GetNextTargetRequired(const CBlockIndex* pindexLast, bool fProofOfS
14031448 CBigNum bnTargetLimit = fProofOfStake ? GetProofOfStakeLimit (pindexLast->nHeight ) : Params ().ProofOfWorkLimit ();
14041449
14051450 unsigned int nTargetTemp = TARGET_SPACING;
1406- if (pindexLast->nTime > FORK_TIME)
1407- nTargetTemp = TARGET_SPACING_NEW;
1408-
1451+ if (pindexLast->nTime > FORK_TIME)
1452+ nTargetTemp = TARGET_SPACING_NEW;
1453+
14091454 if (pindexLast == NULL )
14101455 return bnTargetLimit.GetCompact (); // genesis block
14111456
@@ -1976,10 +2021,8 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck)
19762021 if (!vtx[1 ].GetCoinAge (txdb, pindex->pprev , nCoinAge))
19772022 return error (" ConnectBlock() : %s unable to get coin age for coinstake" , vtx[1 ].GetHash ().ToString ());
19782023
1979- int64_t nCalculatedStakeReward = GetProofOfStakeReward (pindex->pprev , nCoinAge, nFees);
1980-
1981- if (nStakeReward > nCalculatedStakeReward)
1982- return DoS (100 , error (" ConnectBlock() : coinstake pays too much(actual=%d vs calculated=%d)" , nStakeReward, nCalculatedStakeReward));
2024+ if (!IsPOSRewardValid (nStakeReward, nFees))
2025+ return DoS (100 , error (" ConnectBlock() : coinstake pays too much(actual=%d)" , nStakeReward));
19832026 }
19842027
19852028 // ppcoin: track money supply and mint amount info
@@ -2692,6 +2735,20 @@ bool CBlock::AcceptBlock()
26922735 pnode->PushInventory (CInv (MSG_BLOCK, hash));
26932736 }
26942737
2738+ // Record masternode payment
2739+ if (IsProofOfStake ()) {
2740+ CScript payee;
2741+ for (int i = vtx[1 ].vout .size (); i--> 0 ; ) {
2742+ payee = vtx[1 ].vout [i].scriptPubKey ;
2743+ break ;
2744+ }
2745+ CTxDestination address1;
2746+ ExtractDestination (payee, address1);
2747+ CCropCoincoinAddress address2 (address1);
2748+
2749+ mnodeman.RecordMasternodePayment (payee, nTime, PROTOCOL_VERSION);
2750+ }
2751+
26952752 return true ;
26962753}
26972754
0 commit comments