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