Remove unused includes.
[novacoin.git] / src / kernelrecord.cpp
1 #include "kernelrecord.h"
2
3 #include "wallet.h"
4 #include "base58.h"
5
6 using namespace std;
7
8 bool KernelRecord::showTransaction(const CWalletTx &wtx)
9 {
10     if (wtx.IsCoinBase())
11     {
12         if (wtx.GetDepthInMainChain() < 2)
13         {
14             return false;
15         }
16     }
17
18     if(!wtx.IsTrusted())
19     {
20         return false;
21     }
22
23     return true;
24 }
25
26 /*
27  * Decompose CWallet transaction to model kernel records.
28  */
29 vector<KernelRecord> KernelRecord::decomposeOutput(const CWallet *wallet, const CWalletTx &wtx)
30 {
31     vector<KernelRecord> parts;
32     int64_t nTime = wtx.GetTxTime();
33     uint256 hash = wtx.GetHash();
34     std::map<std::string, std::string> mapValue = wtx.mapValue;
35     int64_t nDayWeight = (min((GetAdjustedTime() - nTime), (int64_t)(nStakeMaxAge+nStakeMinAge)) - nStakeMinAge); // DayWeight * 86400, чтобы был
36                                                                                                               // правильный расчёт CoinAge
37     if (showTransaction(wtx))
38     {
39         for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
40         {
41             CTxOut txOut = wtx.vout[nOut];
42             if( wallet->IsMine(txOut) ) {
43                 CTxDestination address;
44                 std::string addrStr;
45
46                 uint64_t coinAge = max( (txOut.nValue * nDayWeight) / (COIN * nOneDay), (int64_t)0);
47
48                 if (ExtractDestination(txOut.scriptPubKey, address))
49                 {
50                     // Sent to Bitcoin Address
51                     addrStr = CBitcoinAddress(address).ToString();
52                 }
53                 else
54                 {
55                     // Sent to IP, or other non-address transaction like OP_EVAL
56                     addrStr = mapValue["to"];
57                 }
58
59                 parts.push_back(KernelRecord(hash, nTime, addrStr, txOut.nValue, wtx.IsSpent(nOut), coinAge));
60             }
61         }
62     }
63
64     return parts;
65 }
66
67 std::string KernelRecord::getTxID()
68 {
69     return hash.ToString() + strprintf("-%03d", idx);
70 }
71
72 int64_t KernelRecord::getAge() const
73 {
74     return (GetAdjustedTime() - nTime) / nOneDay;
75 }
76
77 uint64_t KernelRecord::getCoinDay() const
78 {
79     int64_t nWeight = GetAdjustedTime() - nTime - nStakeMinAge;
80     if( nWeight <  0)
81         return 0;
82     nWeight = min(nWeight, (int64_t)nStakeMaxAge);
83     uint64_t coinAge = (nValue * nWeight ) / (COIN * nOneDay);
84     return coinAge;
85 }
86
87 int64_t KernelRecord::getPoSReward(int nBits, int minutes)
88 {
89     int64_t PoSReward;
90     int64_t nWeight = GetAdjustedTime() - nTime + minutes * 60;
91     if( nWeight <  nStakeMinAge)
92         return 0;
93     uint64_t coinAge = (nValue * nWeight ) / (COIN * nOneDay);
94     PoSReward = GetProofOfStakeReward(coinAge, nBits, GetAdjustedTime() + minutes * 60);
95     return PoSReward;
96 }
97
98 double KernelRecord::getProbToMintStake(double difficulty, int timeOffset) const
99 {
100     //double maxTarget = pow(static_cast<double>(2), 224);
101     //double target = maxTarget / difficulty;
102     //int dayWeight = (min((GetAdjustedTime() - nTime) + timeOffset, (int64_t)(nStakeMinAge+nStakeMaxAge)) - nStakeMinAge) / 86400;
103     //uint64_t coinAge = max(nValue * dayWeight / COIN, (int64_t)0);
104     //return target * coinAge / pow(static_cast<double>(2), 256);
105     int64_t Weight = (min((GetAdjustedTime() - nTime) + timeOffset, (int64_t)(nStakeMinAge+nStakeMaxAge)) - nStakeMinAge);
106     uint64_t coinAge = max(nValue * Weight / (COIN * nOneDay), (int64_t)0);
107     return coinAge / (pow(static_cast<double>(2),32) * difficulty);
108 }
109
110 double KernelRecord::getProbToMintWithinNMinutes(double difficulty, int minutes)
111 {
112     if(difficulty != prevDifficulty || minutes != prevMinutes)
113     {
114         double prob = 1;
115         double p;
116         int d = minutes / (60 * 24); // Number of full days
117         int m = minutes % (60 * 24); // Number of minutes in the last day
118         int i, timeOffset;
119
120         // Probabilities for the first d days
121         for(i = 0; i < d; i++)
122         {
123             timeOffset = i * nOneDay;
124             p = pow(1 - getProbToMintStake(difficulty, timeOffset), nOneDay);
125             prob *= p;
126         }
127
128         // Probability for the m minutes of the last day
129         timeOffset = d * nOneDay;
130         p = pow(1 - getProbToMintStake(difficulty, timeOffset), 60 * m);
131         prob *= p;
132
133         prob = 1 - prob;
134         prevProbability = prob;
135         prevDifficulty = difficulty;
136         prevMinutes = minutes;
137     }
138     return prevProbability;
139 }