-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
228 lines (188 loc) · 6.87 KB
/
main.cpp
File metadata and controls
228 lines (188 loc) · 6.87 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <stdio.h>
#include <string>
#include "bignum.h"
#include <array>
#include <vector>
static CBigNum bnProofOfWorkLimit(~uint256(0) >> 20);
static CBigNum bnProofOfStakeLimit(~uint256(0) >> 24);
static CBigNum bnProofOfStakeHardLimit(~uint256(0) >> 30);
using namespace::std;
struct Block {
int array[4];
};
string padString(string in) {
int targetLen = 64;
int strLen = in.length();
int padNum = targetLen - strLen;
string outStr = "0x";
for (int i = 0; i < padNum; i++) {
outStr += "0";
}
outStr += in;
return outStr;
}
Block getBlock(int index, int blocks[][4]) {
for (int i = 0; i < sizeof(blocks); i++) {
if (blocks[i][0] == index) {
Block b;
b.array[0] = blocks[i][0]; // Block index
b.array[1] = blocks[i][1]; // Timestamp
b.array[2] = blocks[i][2]; // Block 'bits' value
b.array[3] = blocks[i][3]; // pos/pow flag (1 == pos)
return b;
}
}
// Null block
Block nb;
return nb;
}
bool isProofOfStake(int index, int blocks[][4]) {
try {
Block b = getBlock(index, blocks);
return (b.array[3] == 1);
} catch (int e) {
return false;
}
}
const int GetLastBlockIndex(int index, int blocks[][4], bool fProofOfStake) {
int pindex = index;
Block b = getBlock(pindex, blocks);
// isProofOfStake(pindex - 1) as the flag is on the prev element
while (pindex > 0 && (pindex - 1) > 0 && isProofOfStake(pindex - 1, blocks) != fProofOfStake) {
pindex--;
b = getBlock(pindex, blocks);
}
return pindex;
}
bool isNullBlock(Block block) {
if (block.array[0] == 0 && block.array[1] == 0 && block.array[2] == 0 && block.array[3] == 0) {
return true;
} else {
return false;
}
}
void getTarget(int index, int blocks[][4]) {
// Get the block for the current index
Block block = getBlock(index, blocks);
if (isNullBlock(block)) {
// Ignore null blocks and first block
return;
}
// Set pos/pow and target based on type
bool fProofOfStake = false;
CBigNum maxTarget = bnProofOfWorkLimit;
if (block.array[3] == 1) {
fProofOfStake = true;
if(index + 1 > 15000)
maxTarget = bnProofOfStakeLimit;
else if(index + 1 > 14060)
maxTarget = bnProofOfStakeHardLimit;
}
// Get indexes with GetLastBlockIndex()
int pBlockHeight = GetLastBlockIndex(index, blocks, fProofOfStake); // Point to previous block with -1
int ppBlockHeight = GetLastBlockIndex(pBlockHeight - 1, blocks, fProofOfStake);
// Get blocks for prev and prev-prev indexes
Block pblock = getBlock(pBlockHeight, blocks);
Block ppblock = getBlock(ppBlockHeight, blocks);
// Previous bits from pBlockHeight
unsigned int prevBits = pblock.array[2];
// Set timestamps from above blocks
int64 pBlockTime = pblock.array[1];
int64 ppBlockTime = ppblock.array[1];
// Reverse the algorithm
unsigned int nStakeMinAge = 60 * 60 * 24; // minimum age for coin age, changed to 8 hours (24 hr realistic min) network will auto balance
unsigned int nStakeMaxAge = 60 * 60 * 24 * 90; // stake age of full weight
unsigned int nStakeTargetSpacing = 1 * 30; // 1-minute block spacing --- actually is 30 second
static const int64 nTargetTimespan = 0.16 * 24 * 60 * 60; // 0.16 of a day
static const int64 nTargetSpacingWorkMax = 12 * nStakeTargetSpacing; // 12 minutes
string type;
if (fProofOfStake) {
type = "pos";
} else {
type = "pow";
}
cout << "Get target algo (" << type << ") constants\n";
cout << "nStakeMinAge: " << nStakeMinAge
<< "\nnStakeMaxAge: " << nStakeMaxAge
<< "\nnStakeTargetSpacing: " << nStakeTargetSpacing
<< "\nnTargetTimespan: " << nTargetTimespan
<< "\nnTargetSpacingWorkMax: " << nTargetSpacingWorkMax << "\n";
cout << "Inputs:\n";
cout << "pBlockHeight: " << pBlockHeight
<< "\nppBlockHeight: " << ppBlockHeight
<< "\nprevBits: " << prevBits
<< "\npBlockTime: " << pBlockTime
<< "\nppBlockTime: " << ppBlockTime << "\n";
cout << "Calculations:\n";
int64 actualSpacing = pBlockTime - ppBlockTime;
cout << "actualSpacing: " << actualSpacing << "\n";
int64 targetSpacing = fProofOfStake ? nStakeTargetSpacing : min(nTargetSpacingWorkMax, (int64) nStakeTargetSpacing * (1 + index - pBlockHeight));
cout << "targetSpacing: " << targetSpacing << "\n";
int64 interval = nTargetTimespan / targetSpacing;
cout << "interval: " << interval << "\n";
// Do the work to get the new target
CBigNum bnNew;
cout << "Setting bits in bignum\n";
bnNew.SetCompact(prevBits);
// Right op 1
int64 rOp = ((interval - 1) * targetSpacing + actualSpacing + actualSpacing);
bnNew *= rOp;
cout << "Val after first op: " << bnNew.GetCompact() << " (" << padString(bnNew.GetHex()) << "). Right operand: " << rOp << "\n";
// Right op 2
rOp = ((interval + 1) * targetSpacing);
bnNew /= rOp;
// New target
if (bnNew > maxTarget) {
cout << "New target (" << bnNew.GetCompact() << ") greater than max target! Setting to max target: " << maxTarget.GetCompact() << "\n";
bnNew = maxTarget;
}
cout << "New target for block " << index + 1 << " is: " << bnNew.GetCompact() << " (" << padString(bnNew.GetHex()) << "). Right operand: " << rOp << "\n";
}
void getTargets(int startIndex, int arrLen, int blocks[][4]) {
for (int i = 0, j = startIndex; i < arrLen; i++, j++) {
if (i == 0) {
continue;
}
cout << blocks[i][0] << " :: " << blocks[i][1] << " :: " << blocks[i][2] << " :: " << blocks[i][3] << "\n";
getTarget(j, blocks);
cout << endl;
}
}
int main(int argc, const char* argv[]) {
cout << "Max targets:\n";
cout << padString(bnProofOfWorkLimit.GetHex()) << "\n";
cout << padString(bnProofOfStakeLimit.GetHex()) << "\n";
cout << padString(bnProofOfStakeHardLimit.GetHex()) << "\n";
CBigNum maxTarget = bnProofOfWorkLimit; // Only changes if: (nHeight + 1 > 14060) or (nHeight + 1 > 15000)
cout << "Compacts:\n";
cout << "Max target (" << padString(maxTarget.GetHex()) << ") as compact: " << maxTarget.GetCompact() << "\n";
CBigNum newNum;
newNum.SetCompact(maxTarget.GetCompact());
cout << "GetCompact(" << newNum.GetCompact() << "): " << newNum.GetCompact() << "\n";
string cmpHex = newNum.GetHex();
cout << "GetCompact(" << newNum.GetCompact() << ") as hex: " << padString(cmpHex) << "\n";
CBigNum hexNum;
hexNum.SetHex(cmpHex);
cout << "GetCompact() from hex: " << padString(cmpHex) << ": " << hexNum.GetCompact() << "\n";
int blocks1[9][4] = {
{1, 1390009623, 504365055, 0},
{2, 1390009701, 504365055, 0},
{3, 1390009747, 504365055, 0},
{4, 1390009868, 504365055, 0},
{5, 1390009984, 504365055, 0},
{6, 1390010147, 504365055, 0},
{7, 1390010322, 504365055, 0},
{8, 1390010337, 504365055, 0},
{9, 1390010424, 504362780, 0},
};
int blocks2[5][4] = {
{88532, 1392635955, 486651584, 0},
{88533, 1392635968, 486651519, 0},
{88534, 1392635990, 486651243, 1}, // Flag previous block for pos
{88535, 1392636015, 503382015, 0},
{88536, 1392636021, 486650628, 0}
};
/* Get multiple block targets from a multi-dim array of block data */
getTargets(1, 9, blocks1); // 2 - 10
getTargets(88532, 5, blocks2); // 88533 - 88537
}