diff --git a/src/main.cpp b/src/main.cpp index aee43dec2c..8618ee9f5f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2521,8 +2521,17 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck, boo if (IsProofOfStake() && pindex->nHeight > nGrandfather) { // ppcoin: coin stake tx earns reward instead of paying fee - if (!vtx[1].GetCoinAge(txdb, nCoinAge)) - return error("ConnectBlock[] : %s unable to get coin age for coinstake", vtx[1].GetHash().ToString().substr(0,10).c_str()); + // + // With block version 10, Gridcoin switched to constant block rewards + // that do not depend on coin age, so we can avoid reading the blocks + // and transactions from the disk. The CheckProofOfStake*() functions + // of the kernel verify the transaction timestamp and that the staked + // inputs exist in the main chain. + // + if (pindex->nVersion <= 9 && !vtx[1].GetCoinAge(txdb, nCoinAge)) { + return error("ConnectBlock[] : %s unable to get coin age for coinstake", + vtx[1].GetHash().ToString().substr(0,10)); + } //9-3-2015 int64_t nMaxResearchAgeReward = NN::ResearchAgeComputer::MaxReward(nTime); @@ -3152,28 +3161,6 @@ bool CTransaction::GetCoinAge(CTxDB& txdb, uint64_t& nCoinAge) const return true; } -// ppcoin: total coin age spent in block, in the unit of coin-days. -bool CBlock::GetCoinAge(uint64_t& nCoinAge) const -{ - nCoinAge = 0; - - CTxDB txdb("r"); - for (auto const& tx : vtx) - { - uint64_t nTxCoinAge; - if (tx.GetCoinAge(txdb, nTxCoinAge)) - nCoinAge += nTxCoinAge; - else - return false; - } - - if (nCoinAge == 0) // block coin age minimum 1 coin-day - nCoinAge = 1; - if (fDebug && GetBoolArg("-printcoinage")) - LogPrintf("block coin age total nCoinDays=%" PRIu64, nCoinAge); - return true; -} - bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const uint256& hashProof) { // Check for duplicate diff --git a/src/main.h b/src/main.h index b6a99f237a..5fe4c8819c 100644 --- a/src/main.h +++ b/src/main.h @@ -1311,7 +1311,6 @@ class CBlock bool AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const uint256& hashProof); bool CheckBlock(std::string sCaller, int height1, int64_t mint, bool fCheckPOW=true, bool fCheckMerkleRoot=true, bool fCheckSig=true, bool fLoadingIndex=false) const; bool AcceptBlock(bool generated_by_me); - bool GetCoinAge(uint64_t& nCoinAge) const; // ppcoin: calculate total coin age spent in block bool CheckBlockSignature() const; private: diff --git a/src/miner.cpp b/src/miner.cpp index f6a01aee78..aca9dcdee3 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -140,7 +140,6 @@ void CMinerStatus::Clear() { WeightSum= ValueSum= WeightMin= WeightMax= 0; Version= 0; - CoinAgeSum= 0; KernelDiffSum = 0; nLastCoinStakeSearchInterval = 0; } @@ -457,7 +456,7 @@ bool CreateRestOfTheBlock(CBlock &block, CBlockIndex* pindexPrev) bool CreateCoinStake( CBlock &blocknew, CKey &key, - vector &StakeInputs, uint64_t &CoinAge, + vector &StakeInputs, CWallet &wallet, CBlockIndex* pindexPrev ) { int64_t CoinWeight; @@ -467,7 +466,6 @@ bool CreateCoinStake( CBlock &blocknew, CKey &key, double StakeValueSum = 0; int64_t StakeWeightMin=MAX_MONEY; int64_t StakeWeightMax=0; - uint64_t StakeCoinAgeSum=0; double StakeDiffSum = 0; double StakeDiffMax = 0; CTransaction &txnew = blocknew.vtx[1]; // second tx is coinstake @@ -510,16 +508,7 @@ bool CreateCoinStake( CBlock &blocknew, CKey &key, continue; } - { - int64_t nStakeValue= CoinTx.vout[CoinTxN].nValue; - StakeValueSum += nStakeValue /(double)COIN; - //crazy formula... - // todo: clean this - // todo reuse calculated value for interst - CBigNum bn = CBigNum(nStakeValue) * (blocknew.nTime-CoinTx.nTime) / CENT; - bn = bn * CENT / COIN / (24 * 60 * 60); - StakeCoinAgeSum += bn.getuint64(); - } + StakeValueSum += CoinTx.vout[CoinTxN].nValue / (double)COIN; uint64_t StakeModifier = 0; if(!FindStakeModifierRev(StakeModifier,pindexPrev)) @@ -596,8 +585,7 @@ bool CreateCoinStake( CBlock &blocknew, CKey &key, txnew.vin.push_back(CTxIn(CoinTx.GetHash(), CoinTxN)); StakeInputs.push_back(pcoin.first); - if (!txnew.GetCoinAge(txdb, CoinAge)) - return error("CreateCoinStake: failed to calculate coin age"); + int64_t nCredit = CoinTx.vout[CoinTxN].nValue; txnew.vout.push_back(CTxOut(0, CScript())); // First Must be empty @@ -618,7 +606,6 @@ bool CreateCoinStake( CBlock &blocknew, CKey &key, MinerStatus.ValueSum = StakeValueSum; MinerStatus.WeightMin=StakeWeightMin; MinerStatus.WeightMax=StakeWeightMax; - MinerStatus.CoinAgeSum=StakeCoinAgeSum; MinerStatus.KernelDiffMax = std::max(MinerStatus.KernelDiffMax,StakeDiffMax); MinerStatus.KernelDiffSum = StakeDiffSum; MinerStatus.nLastCoinStakeSearchInterval= txnew.nTime; @@ -985,7 +972,7 @@ void AddNeuralContractOrVote(CBlock& blocknew) GetSerializeSize(blocknew.m_claim.m_superblock, SER_NETWORK, 1)); } -bool CreateGridcoinReward(CBlock &blocknew, uint64_t &nCoinAge, CBlockIndex* pindexPrev, int64_t &nReward) +bool CreateGridcoinReward(CBlock &blocknew, CBlockIndex* pindexPrev, int64_t &nReward) { // Remove fees from coinbase: int64_t nFees = blocknew.vtx[0].vout[0].nValue; @@ -1013,7 +1000,7 @@ bool CreateGridcoinReward(CBlock &blocknew, uint64_t &nCoinAge, CBlockIndex* pin // Note: Since research age must be exact, we need to transmit the block // nTime here so it matches AcceptBlock(): nReward = GetProofOfStakeReward( - nCoinAge, + 0, // coin age - unused since CBR (block version 10) nFees, claim.m_mining_id, pindexPrev->nTime, @@ -1275,8 +1262,8 @@ void StakeMiner(CWallet *pwallet) // * Try to create a CoinStake transaction CKey BlockKey; vector StakeInputs; - uint64_t StakeCoinAge; - if( !CreateCoinStake( StakeBlock, BlockKey, StakeInputs, StakeCoinAge, *pwallet, pindexPrev ) ) + + if( !CreateCoinStake( StakeBlock, BlockKey, StakeInputs, *pwallet, pindexPrev ) ) continue; StakeBlock.nTime= StakeTX.nTime; @@ -1288,7 +1275,7 @@ void StakeMiner(CWallet *pwallet) // * add gridcoin reward to coinstake, fill-in nReward int64_t nReward = 0; - if(!CreateGridcoinReward(StakeBlock, StakeCoinAge, pindexPrev, nReward)) { + if(!CreateGridcoinReward(StakeBlock, pindexPrev, nReward)) { continue; } diff --git a/src/miner.h b/src/miner.h index 3b02e10340..dca46363aa 100644 --- a/src/miner.h +++ b/src/miner.h @@ -15,7 +15,6 @@ struct CMinerStatus std::string ReasonNotStaking; uint64_t WeightSum,WeightMin,WeightMax; double ValueSum; - double CoinAgeSum; int Version; uint64_t CreatedCnt; uint64_t AcceptedCnt;