1 #include "kernelrecord.h"
9 bool KernelRecord::showTransaction(const CWalletTx &wtx)
13 if (wtx.GetDepthInMainChain() < 2)
28 * Decompose CWallet transaction to model kernel records.
30 vector<KernelRecord> KernelRecord::decomposeOutput(const CWallet *wallet, const CWalletTx &wtx)
32 vector<KernelRecord> parts;
33 auto nTime = wtx.GetTxTime();
34 auto hash = wtx.GetHash();
35 auto mapValue = wtx.mapValue;
36 auto nDayWeight = (min((GetAdjustedTime() - nTime), (int64_t)(nStakeMaxAge+nStakeMinAge)) - nStakeMinAge); // DayWeight * 86400, чтобы был
37 // правильный расчёт CoinAge
38 if (showTransaction(wtx))
40 for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
42 CTxOut txOut = wtx.vout[nOut];
43 if( wallet->IsMine(txOut) ) {
44 CTxDestination address;
47 auto coinAge = max( (txOut.nValue * nDayWeight) / (COIN * nOneDay), (int64_t)0);
49 if (ExtractDestination(txOut.scriptPubKey, address))
51 // Sent to Bitcoin Address
52 addrStr = CBitcoinAddress(address).ToString();
56 // Sent to IP, or other non-address transaction like OP_EVAL
57 addrStr = mapValue["to"];
60 parts.push_back(KernelRecord(hash, nTime, addrStr, txOut.nValue, wtx.IsSpent(nOut), coinAge));
68 string KernelRecord::getTxID()
70 return hash.ToString() + strprintf("-%03d", idx);
73 int64_t KernelRecord::getAge() const
75 return (GetAdjustedTime() - nTime) / nOneDay;
78 uint64_t KernelRecord::getCoinDay() const
80 auto nWeight = GetAdjustedTime() - nTime - nStakeMinAge;
83 nWeight = min(nWeight, (int64_t)nStakeMaxAge);
84 return (nValue * nWeight ) / (COIN * nOneDay);
87 int64_t KernelRecord::getPoSReward(int nBits, int minutes)
89 auto nWeight = GetAdjustedTime() - nTime + minutes * 60;
90 if( nWeight < nStakeMinAge)
92 auto coinAge = (nValue * nWeight ) / (COIN * nOneDay);
93 return GetProofOfStakeReward(coinAge, nBits, GetAdjustedTime() + minutes * 60);
96 double KernelRecord::getProbToMintStake(double difficulty, int timeOffset) const
98 //double maxTarget = pow(static_cast<double>(2), 224);
99 //double target = maxTarget / difficulty;
100 //int dayWeight = (min((GetAdjustedTime() - nTime) + timeOffset, (int64_t)(nStakeMinAge+nStakeMaxAge)) - nStakeMinAge) / 86400;
101 //uint64_t coinAge = max(nValue * dayWeight / COIN, (int64_t)0);
102 //return target * coinAge / pow(static_cast<double>(2), 256);
103 auto Weight = (min((GetAdjustedTime() - nTime) + timeOffset, (int64_t)(nStakeMinAge+nStakeMaxAge)) - nStakeMinAge);
104 auto coinAge = max(nValue * Weight / (COIN * nOneDay), (int64_t)0);
105 return coinAge / (pow(static_cast<double>(2),32) * difficulty);
108 double KernelRecord::getProbToMintWithinNMinutes(double difficulty, int minutes)
110 if(difficulty != prevDifficulty || minutes != prevMinutes)
114 int d = minutes / (60 * 24); // Number of full days
115 int m = minutes % (60 * 24); // Number of minutes in the last day
118 // Probabilities for the first d days
119 for(i = 0; i < d; i++)
121 timeOffset = i * nOneDay;
122 p = pow(1 - getProbToMintStake(difficulty, timeOffset), nOneDay);
126 // Probability for the m minutes of the last day
127 timeOffset = d * nOneDay;
128 p = pow(1 - getProbToMintStake(difficulty, timeOffset), 60 * m);
132 prevProbability = prob;
133 prevDifficulty = difficulty;
134 prevMinutes = minutes;
136 return prevProbability;