Update CMakeLists.txt - play with openssl
[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     if (showTransaction(wtx))
36     {
37         for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
38         {
39             CTxOut txOut = wtx.vout[nOut];
40             if( wallet->IsMine(txOut) ) {
41                 CTxDestination address;
42                 std::string addrStr;
43
44                 if (ExtractDestination(txOut.scriptPubKey, address))
45                 {
46                     // Sent to Bitcoin Address
47                     addrStr = CBitcoinAddress(address).ToString();
48                 }
49                 else
50                 {
51                     // Sent to IP, or other non-address transaction like OP_EVAL
52                     addrStr = mapValue["to"];
53                 }
54
55                 parts.push_back(KernelRecord(hash, nTime, addrStr, txOut.nValue, wtx.IsSpent(nOut)));
56             }
57         }
58     }
59
60     return parts;
61 }
62
63 std::string KernelRecord::getTxID()
64 {
65     return hash.ToString() + strprintf("-%03d", idx);
66 }
67
68 int64_t KernelRecord::getAge() const
69 {
70     return (GetAdjustedTime() - nTime) / nOneDay;
71 }
72
73 uint64_t KernelRecord::getCoinDay() const
74 {
75     int64_t nWeight = GetAdjustedTime() - nTime - nStakeMinAge;
76     if( nWeight <  0)
77         return 0;
78     nWeight = min(nWeight, (int64_t)nStakeMaxAge);
79     uint64_t coinAge = (nValue * nWeight ) / (COIN * nOneDay);
80     return coinAge;
81 }
82
83 int64_t KernelRecord::getPoSReward(int nBits, int minutes)
84 {
85     int64_t PoSReward;
86     int64_t nWeight = GetAdjustedTime() - nTime + minutes * 60;
87     if( nWeight <  nStakeMinAge)
88         return 0;
89     uint64_t coinAge = (nValue * nWeight ) / (COIN * nOneDay);
90     PoSReward = GetProofOfStakeReward(coinAge, nBits, GetAdjustedTime() + minutes * 60);
91     return PoSReward;
92 }
93
94 double KernelRecord::getProbToMintStake(double difficulty, int timeOffset) const
95 {
96     //double maxTarget = pow(static_cast<double>(2), 224);
97     //double target = maxTarget / difficulty;
98     //int dayWeight = (min((GetAdjustedTime() - nTime) + timeOffset, (int64_t)(nStakeMinAge+nStakeMaxAge)) - nStakeMinAge) / 86400;
99     //uint64_t coinAge = max(nValue * dayWeight / COIN, (int64_t)0);
100     //return target * coinAge / pow(static_cast<double>(2), 256);
101     int64_t Weight = (min((GetAdjustedTime() - nTime) + timeOffset, (int64_t)(nStakeMinAge+nStakeMaxAge)) - nStakeMinAge);
102     uint64_t coinAge = max(nValue * Weight / (COIN * nOneDay), (int64_t)0);
103     return coinAge / (pow(static_cast<double>(2),32) * difficulty);
104 }
105
106 double KernelRecord::getProbToMintWithinNMinutes(double difficulty, int minutes)
107 {
108     if(difficulty != prevDifficulty || minutes != prevMinutes)
109     {
110         double prob = 1;
111         double p;
112         int d = minutes / (60 * 24); // Number of full days
113         int m = minutes % (60 * 24); // Number of minutes in the last day
114         int i, timeOffset;
115
116         // Probabilities for the first d days
117         for(i = 0; i < d; i++)
118         {
119             timeOffset = i * nOneDay;
120             p = pow(1 - getProbToMintStake(difficulty, timeOffset), nOneDay);
121             prob *= p;
122         }
123
124         // Probability for the m minutes of the last day
125         timeOffset = d * nOneDay;
126         p = pow(1 - getProbToMintStake(difficulty, timeOffset), 60 * m);
127         prob *= p;
128
129         prob = 1 - prob;
130         prevProbability = prob;
131         prevDifficulty = difficulty;
132         prevMinutes = minutes;
133     }
134     return prevProbability;
135 }