1 #include "kernelrecord.h"
7 #pragma warning( disable : 4345)
12 bool KernelRecord::showTransaction(const CWalletTx &wtx)
16 if (wtx.GetDepthInMainChain() < 2)
31 * Decompose CWallet transaction to model kernel records.
33 vector<KernelRecord> KernelRecord::decomposeOutput(const CWallet *wallet, const CWalletTx &wtx)
35 vector<KernelRecord> parts;
36 int64_t nTime = wtx.GetTxTime();
37 uint256 hash = wtx.GetHash();
38 std::map<std::string, std::string> mapValue = wtx.mapValue;
39 int64_t nDayWeight = (min((GetAdjustedTime() - nTime), (int64_t)(nStakeMaxAge+nStakeMinAge)) - nStakeMinAge); // DayWeight * 86400, чтобы был
40 // правильный расчёт CoinAge
41 if (showTransaction(wtx))
43 for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
45 CTxOut txOut = wtx.vout[nOut];
46 if( wallet->IsMine(txOut) ) {
47 CTxDestination address;
50 uint64_t coinAge = max( (txOut.nValue * nDayWeight) / (COIN * nOneDay), (int64_t)0);
52 if (ExtractDestination(txOut.scriptPubKey, address))
54 // Sent to Bitcoin Address
55 addrStr = CBitcoinAddress(address).ToString();
59 // Sent to IP, or other non-address transaction like OP_EVAL
60 addrStr = mapValue["to"];
63 parts.push_back(KernelRecord(hash, nTime, addrStr, txOut.nValue, wtx.IsSpent(nOut), coinAge));
71 std::string KernelRecord::getTxID()
73 return hash.ToString() + strprintf("-%03d", idx);
76 int64_t KernelRecord::getAge() const
78 return (GetAdjustedTime() - nTime) / nOneDay;
81 uint64_t KernelRecord::getCoinDay() const
83 int64_t nWeight = GetAdjustedTime() - nTime - nStakeMinAge;
86 nWeight = min(nWeight, (int64_t)nStakeMaxAge);
87 uint64_t coinAge = (nValue * nWeight ) / (COIN * nOneDay);
91 int64_t KernelRecord::getPoSReward(int nBits, int minutes)
94 int64_t nWeight = GetAdjustedTime() - nTime + minutes * 60;
95 if( nWeight < nStakeMinAge)
97 uint64_t coinAge = (nValue * nWeight ) / (COIN * nOneDay);
98 PoSReward = GetProofOfStakeReward(coinAge, nBits, GetAdjustedTime() + minutes * 60);
102 double KernelRecord::getProbToMintStake(double difficulty, int timeOffset) const
104 //double maxTarget = pow(static_cast<double>(2), 224);
105 //double target = maxTarget / difficulty;
106 //int dayWeight = (min((GetAdjustedTime() - nTime) + timeOffset, (int64_t)(nStakeMinAge+nStakeMaxAge)) - nStakeMinAge) / 86400;
107 //uint64_t coinAge = max(nValue * dayWeight / COIN, (int64_t)0);
108 //return target * coinAge / pow(static_cast<double>(2), 256);
109 int64_t Weight = (min((GetAdjustedTime() - nTime) + timeOffset, (int64_t)(nStakeMinAge+nStakeMaxAge)) - nStakeMinAge);
110 uint64_t coinAge = max(nValue * Weight / (COIN * nOneDay), (int64_t)0);
111 return coinAge / (pow(static_cast<double>(2),32) * difficulty);
114 double KernelRecord::getProbToMintWithinNMinutes(double difficulty, int minutes)
116 if(difficulty != prevDifficulty || minutes != prevMinutes)
120 int d = minutes / (60 * 24); // Number of full days
121 int m = minutes % (60 * 24); // Number of minutes in the last day
124 // Probabilities for the first d days
125 for(i = 0; i < d; i++)
127 timeOffset = i * nOneDay;
128 p = pow(1 - getProbToMintStake(difficulty, timeOffset), nOneDay);
132 // Probability for the m minutes of the last day
133 timeOffset = d * nOneDay;
134 p = pow(1 - getProbToMintStake(difficulty, timeOffset), 60 * m);
138 prevProbability = prob;
139 prevDifficulty = difficulty;
140 prevMinutes = minutes;
142 return prevProbability;