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