PosTab
[novacoin.git] / src / kernelrecord.cpp
1 #include "kernelrecord.h"
2
3 #include "wallet.h"
4 #include "base58.h"
5 #include "main.h"
6
7 using namespace std;
8
9 bool KernelRecord::showTransaction(const CWalletTx &wtx)
10 {
11     if (wtx.IsCoinBase())
12     {
13         if (wtx.GetDepthInMainChain() < 2)
14         {
15             return false;
16         }
17     }
18
19     if(!wtx.IsTrusted())
20     {
21         return false;
22     }
23
24     return true;
25 }
26
27 /*
28  * Decompose CWallet transaction to model kernel records.
29  */
30 vector<KernelRecord> KernelRecord::decomposeOutput(const CWallet *wallet, const CWalletTx &wtx)
31 {
32     vector<KernelRecord> parts;
33     int64 nTime = wtx.GetTxTime();
34     uint256 hash = wtx.GetHash();
35     std::map<std::string, std::string> mapValue = wtx.mapValue;
36     int64 nDayWeight = (min((GetAdjustedTime() - nTime), (int64)(nStakeMaxAge+nStakeMinAge)) - nStakeMinAge); // DayWeight * 86400, ÷òîáû áûë
37                                                                                                               // ïðàâèëüíûé ðàñ÷¸ò CoinAge                                                         
38     if (showTransaction(wtx))
39     {
40         for (int nOut = 0; nOut < wtx.vout.size(); nOut++)
41         {
42             CTxOut txOut = wtx.vout[nOut];
43             if( wallet->IsMine(txOut) ) {
44                 CTxDestination address;
45                 std::string addrStr;
46
47                 uint64 coinAge = max( (txOut.nValue * nDayWeight) / (COIN * 86400), (int64)0);
48
49                 if (ExtractDestination(txOut.scriptPubKey, address))
50                 {
51                     // Sent to Bitcoin Address
52                     addrStr = CBitcoinAddress(address).ToString();
53                 }
54                 else
55                 {
56                     // Sent to IP, or other non-address transaction like OP_EVAL
57                     addrStr = mapValue["to"];
58                 }
59
60                 parts.push_back(KernelRecord(hash, nTime, addrStr, txOut.nValue, wtx.IsSpent(nOut), coinAge));
61             }
62         }
63     }
64
65     return parts;
66 }
67
68 std::string KernelRecord::getTxID()
69 {
70     return hash.ToString() + strprintf("-%03d", idx);
71 }
72
73 int64 KernelRecord::getAge() const
74 {
75     return (GetAdjustedTime() - nTime) / 86400;
76 }
77
78 double KernelRecord::getProbToMintStake(double difficulty, int timeOffset) const
79 {
80     double maxTarget = pow(static_cast<double>(2), 224);
81     double target = maxTarget / difficulty;
82     int dayWeight = (min((GetAdjustedTime() - nTime) + timeOffset, (int64)(nStakeMinAge+nStakeMaxAge)) - nStakeMinAge) / 86400;
83     uint64 coinAge = max(nValue * dayWeight / COIN, (int64)0);
84     return target * coinAge / pow(static_cast<double>(2), 256);
85 }
86
87 double KernelRecord::getProbToMintWithinNMinutes(double difficulty, int minutes)
88 {
89     if(difficulty != prevDifficulty || minutes != prevMinutes)
90     {
91         double prob = 1;
92         double p;
93         int d = minutes / (60 * 24); // Number of full days
94         int m = minutes % (60 * 24); // Number of minutes in the last day
95         int i, timeOffset;
96
97         // Probabilities for the first d days
98         for(i = 0; i < d; i++)
99         {
100             timeOffset = i * 86400;
101             p = pow(1 - getProbToMintStake(difficulty, timeOffset), 86400);
102             prob *= p;
103         }
104
105         // Probability for the m minutes of the last day
106         timeOffset = d * 86400;
107         p = pow(1 - getProbToMintStake(difficulty, timeOffset), 60 * m);
108         prob *= p;
109
110         prob = 1 - prob;
111         prevProbability = prob;
112         prevDifficulty = difficulty;
113         prevMinutes = minutes;
114     }
115     return prevProbability;
116 }