-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBlockChainAddresses.cpp
285 lines (257 loc) · 6.96 KB
/
BlockChainAddresses.cpp
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "BlockChainAddresses.h"
#include "BitcoinAddress.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#ifdef _MSC_VER
#pragma warning(disable:4100 4505 4996)
#endif
#define CURRENT_VERSION 1
#define MAXNUMERIC 32 // JWR support up to 16 32 character long numeric formated strings
#define MAXFNUM 16
static char gFormat[MAXNUMERIC*MAXFNUM];
static int32_t gIndex=0;
static const char * formatNumber(int32_t number) // JWR format this integer into a fancy comma delimited string
{
char * dest = &gFormat[gIndex*MAXNUMERIC];
gIndex++;
if ( gIndex == MAXFNUM ) gIndex = 0;
char scratch[512];
#ifdef _MSC_VER
itoa(number,scratch,10);
#else
snprintf(scratch, 10, "%d", number);
#endif
char *source = scratch;
char *str = dest;
uint32_t len = (uint32_t)strlen(scratch);
if ( scratch[0] == '-' )
{
*str++ = '-';
source++;
len--;
}
for (uint32_t i=0; i<len; i++)
{
int32_t place = (len-1)-i;
*str++ = source[i];
if ( place && (place%3) == 0 ) *str++ = ',';
}
*str = 0;
return dest;
}
static const char *getTimeString(uint32_t timeStamp)
{
static char scratch[1024];
time_t t(timeStamp);
struct tm *gtm = gmtime(&t);
strftime(scratch, 1024, "%m/%d/%Y %H:%M:%S", gtm);
return scratch;
}
class BlockChainAddressesImpl : public BlockChainAddresses
{
public:
class Address
{
public:
uint8_t mAddress[20];
};
class Row
{
public:
Row(void)
{
mStartTime = 0;
mAddressCount = 0;
mChangeAddressCount = 0;
mDeleteAddressCount = 0;
mNewAddresses = NULL;
mChangedAddresses = NULL;
mDeletedAddresses = NULL;
}
~Row(void)
{
delete []mNewAddresses;
delete []mChangedAddresses;
delete []mDeletedAddresses;
}
uint32_t mStartTime;
uint32_t mAddressCount;
uint32_t mChangeAddressCount;
uint32_t mDeleteAddressCount;
BlockChainAddresses::StatAddress *mNewAddresses;
BlockChainAddresses::StatAddress *mChangedAddresses;
uint32_t *mDeletedAddresses;
};
BlockChainAddressesImpl(const char *fname)
{
mRowCount = 0;
mAddressCount = 0;
mAddresses = 0;
mRows = 0;
mCurrentRow = 0;
mCurrentRowIndex = 0;
FILE *fph = fopen(fname,"rb");
if ( fph )
{
char headerBlock[22];
size_t r = fread(headerBlock,sizeof(headerBlock),1,fph); // read the block header
if ( r == 1 )
{
const char *headerString = "BLOCK_CHAIN_ADDRESSES";
if ( headerBlock[21] == 0 && strcmp(headerBlock,headerString) == 0 ) // confirm this is a valid block-header for the data file we are expecting
{
uint32_t version=0;
fread(&version,sizeof(version),1,fph);
if ( version == CURRENT_VERSION ) // Confirm this is for the same version number of this file we are one.
{
r = fread(&mAddressCount,sizeof(mAddressCount),1,fph); // Read the number of unique public key addresses recorded
if ( r == 1 )
{
mAddresses = new Address[mAddressCount];
r = fread(mAddresses,sizeof(Address)*mAddressCount,1,fph); // Read all of the addresses into memory.
if ( r == 1 )
{
fread(&mRowCount,sizeof(mRowCount),1,fph); // Read the number of rows (days)
if ( mRowCount )
{
mRows = new Row[mRowCount];
// For each row read the start time, the number of new addresses, the number of changed addresses, and the number of removed addresses
// Also allocate memory to hold the change data
for (uint32_t i=0; i<mRowCount; i++)
{
Row &row = mRows[i];
fread(&row.mStartTime,sizeof(row.mStartTime),1,fph);
fread(&row.mAddressCount,sizeof(row.mAddressCount),1,fph);
fread(&row.mChangeAddressCount,sizeof(row.mChangeAddressCount),1,fph);
fread(&row.mDeleteAddressCount,sizeof(row.mDeleteAddressCount),1,fph);
if ( row.mAddressCount )
{
row.mNewAddresses = new BlockChainAddresses::StatAddress[row.mAddressCount];
}
if ( row.mChangeAddressCount )
{
row.mChangedAddresses = new BlockChainAddresses::StatAddress[row.mChangeAddressCount];
}
if ( row.mDeleteAddressCount )
{
row.mDeletedAddresses = new uint32_t[row.mDeleteAddressCount];
}
}
// Now for each row, read the new address data, the change address data, and the deleted address data
for (uint32_t i=0; i<mRowCount; i++)
{
Row &row = mRows[i];
if ( row.mNewAddresses )
{
fread(row.mNewAddresses,sizeof(BlockChainAddresses::StatAddress)*row.mAddressCount,1,fph);
}
if ( row.mChangedAddresses )
{
fread(row.mChangedAddresses,sizeof(BlockChainAddresses::StatAddress)*row.mChangeAddressCount,1,fph);
}
if ( row.mDeletedAddresses )
{
fread(row.mDeletedAddresses,sizeof(uint32_t)*row.mDeleteAddressCount,1,fph);
}
}
}
else
{
printf("Failed to read the row counter.\r\n");
}
}
else
{
printf("Failed to read public-key addresses.\r\n");
}
}
else
{
printf("Failed to read address count.\r\n");
}
}
}
else
{
printf("Invalid header block.\r\n");
}
}
else
{
printf("Failed to read header block!\r\n");
}
fclose(fph);
}
else
{
printf("Failed to open BitcoinAddress file: %s\r\n", fname );
}
}
virtual ~BlockChainAddressesImpl(void)
{
delete []mAddresses;
delete []mRows;
delete []mCurrentRow;
}
const char *getKey(uint32_t a) const
{
static char scratch[256];
const char *ret = "UNKNOWN ADDRESS";
// assert(a);
// assert( (a-1) < mAddressCount );
if ( a && (a-1) < mAddressCount )
{
Address *ba = &mAddresses[a-1];
uint8_t address1[25];
bitcoinRIPEMD160ToAddress((const uint8_t *)ba,address1);
bitcoinAddressToAscii(address1,scratch,256);
ret = scratch;
}
return ret;
}
virtual void printRow(void) // Print out for debugging purposes the data in this current row
{
if ( !mCurrentRow ) return;
}
virtual bool seekRow(uint32_t index) // Seek the file to this row location.
{
bool ret = false;
#if 0
assert( index < mRowCount );
if ( index < mRowCount )
{
Row &r = mRows[index];
delete []mCurrentRow;
mCurrentRow = NULL;
}
#endif
return ret;
}
virtual uint32_t getRowCount(void) const // return the number of rows found in the file.
{
return mRowCount;
}
virtual void release(void) // release the interface
{
delete this;
};
virtual const StatAddress *getRow(uint32_t &scount)
{
scount = mRows[mCurrentRowIndex].mAddressCount;
return mCurrentRow;
}
uint32_t mAddressCount;
Address *mAddresses;
uint32_t mRowCount;
Row *mRows;
uint32_t mCurrentRowIndex;
StatAddress *mCurrentRow;
};
BlockChainAddresses *createBlockChainAddresses(const char *fname)
{
BlockChainAddressesImpl *b = new BlockChainAddressesImpl(fname);
return static_cast< BlockChainAddresses *>(b);
}