1 #include "kernelrecord.h"
8 bool KernelRecord::showTransaction(const CWalletTx &wtx)
12 if (wtx.GetDepthInMainChain() < 2)
27 * Decompose CWallet transaction to model kernel records.
29 vector<KernelRecord> KernelRecord::decomposeOutput(const CWallet *wallet, const CWalletTx &wtx)
31 vector<KernelRecord> parts;
32 auto nTime = wtx.GetTxTime();
33 auto hash = wtx.GetHash();
34 auto mapValue = wtx.mapValue;
35 auto nDayWeight = (min((GetAdjustedTime() - nTime), (int64_t)(nStakeMaxAge+nStakeMinAge)) - nStakeMinAge); // DayWeight * 86400, чтобы был
36 // правильный расчёт CoinAge
37 if (showTransaction(wtx))
39 for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
41 CTxOut txOut = wtx.vout[nOut];
42 if( wallet->IsMine(txOut) ) {
43 CTxDestination address;
46 auto coinAge = max( (txOut.nValue * nDayWeight) / (COIN * nOneDay), (int64_t)0);
48 if (ExtractDestination(txOut.scriptPubKey, address))
50 // Sent to Bitcoin Address
51 addrStr = CBitcoinAddress(address).ToString();
55 // Sent to IP, or other non-address transaction like OP_EVAL
56 addrStr = mapValue["to"];
59 parts.push_back(KernelRecord(hash, nTime, addrStr, txOut.nValue, wtx.IsSpent(nOut), coinAge));
67 string KernelRecord::getTxID()
69 return hash.ToString() + strprintf("-%03d", idx);
72 int64_t KernelRecord::getAge() const
74 return (GetAdjustedTime() - nTime) / nOneDay;
77 uint64_t KernelRecord::getCoinDay() const
79 auto nWeight = GetAdjustedTime() - nTime - nStakeMinAge;
82 nWeight = min(nWeight, (int64_t)nStakeMaxAge);
83 return (nValue * nWeight ) / (COIN * nOneDay);
86 int64_t KernelRecord::getPoSReward(int nBits, int minutes)
88 auto nWeight = GetAdjustedTime() - nTime + minutes * 60;
89 if( nWeight < nStakeMinAge)
91 auto coinAge = (nValue * nWeight ) / (COIN * nOneDay);
92 return GetProofOfStakeReward(coinAge, nBits, GetAdjustedTime() + minutes * 60);
95 double KernelRecord::getProbToMintStake(double difficulty, int timeOffset) const
97 //double maxTarget = pow(static_cast<double>(2), 224);
98 //double target = maxTarget / difficulty;
99 //int dayWeight = (min((GetAdjustedTime() - nTime) + timeOffset, (int64_t)(nStakeMinAge+nStakeMaxAge)) - nStakeMinAge) / 86400;
100 //uint64_t coinAge = max(nValue * dayWeight / COIN, (int64_t)0);
101 //return target * coinAge / pow(static_cast<double>(2), 256);
102 auto Weight = (min((GetAdjustedTime() - nTime) + timeOffset, (int64_t)(nStakeMinAge+nStakeMaxAge)) - nStakeMinAge);
103 auto coinAge = max(nValue * Weight / (COIN * nOneDay), (int64_t)0);
104 return coinAge / (pow(static_cast<double>(2),32) * difficulty);
107 double KernelRecord::getProbToMintWithinNMinutes(double difficulty, int minutes)
109 if(difficulty != prevDifficulty || minutes != prevMinutes)
113 int d = minutes / (60 * 24); // Number of full days
114 int m = minutes % (60 * 24); // Number of minutes in the last day
117 // Probabilities for the first d days
118 for(i = 0; i < d; i++)
120 timeOffset = i * nOneDay;
121 p = pow(1 - getProbToMintStake(difficulty, timeOffset), nOneDay);
125 // Probability for the m minutes of the last day
126 timeOffset = d * nOneDay;
127 p = pow(1 - getProbToMintStake(difficulty, timeOffset), 60 * m);
131 prevProbability = prob;
132 prevDifficulty = difficulty;
133 prevMinutes = minutes;
135 return prevProbability;