Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up and optimize legacy coin age code #1616

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 11 additions & 24 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 8 additions & 21 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ void CMinerStatus::Clear()
{
WeightSum= ValueSum= WeightMin= WeightMax= 0;
Version= 0;
CoinAgeSum= 0;
KernelDiffSum = 0;
nLastCoinStakeSearchInterval = 0;
}
Expand Down Expand Up @@ -457,7 +456,7 @@ bool CreateRestOfTheBlock(CBlock &block, CBlockIndex* pindexPrev)


bool CreateCoinStake( CBlock &blocknew, CKey &key,
vector<const CWalletTx*> &StakeInputs, uint64_t &CoinAge,
vector<const CWalletTx*> &StakeInputs,
CWallet &wallet, CBlockIndex* pindexPrev )
{
int64_t CoinWeight;
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1275,8 +1262,8 @@ void StakeMiner(CWallet *pwallet)
// * Try to create a CoinStake transaction
CKey BlockKey;
vector<const CWalletTx*> StakeInputs;
uint64_t StakeCoinAge;
if( !CreateCoinStake( StakeBlock, BlockKey, StakeInputs, StakeCoinAge, *pwallet, pindexPrev ) )

if( !CreateCoinStake( StakeBlock, BlockKey, StakeInputs, *pwallet, pindexPrev ) )
continue;
StakeBlock.nTime= StakeTX.nTime;

Expand All @@ -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;
}

Expand Down
1 change: 0 additions & 1 deletion src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down