-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPublicKeyDatabase.cpp
2432 lines (2158 loc) · 68.6 KB
/
PublicKeyDatabase.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "PublicKeyDatabase.h"
#include "BitcoinAddress.h"
#include "FileInterface.h"
#include "logging.h"
#include "HeapSort.h"
#include "CRC32.h"
#include <stdio.h>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <assert.h>
#include <time.h>
#define ONE_BTC 100000000
#define ONE_MBTC (ONE_BTC/1000)
#define SECONDS_PER_DAY (60*60*24)
#define DUST_VALUE ONE_MBTC
// how old, in seconds, before an input is considered a 'zombie'
#define ZOMBIE_TIME (365*4)
#ifdef _MSC_VER
#pragma warning(disable:4100 4996 4189 4456)
#endif
namespace PUBLIC_KEY_DATABASE
{
enum ValueType
{
VT_MICRO_BIT, // 0.0001
VT_MILI_BIT, // 0.001
VT_CENTI_BIT, // 0.01
VT_TENTH, // 0.1
VT_QUARTER, // 0.25
VT_BTC, // 1
VT_TEN_BTC, // 10
VT_HUNDRED_BTC, // 100
VT_THOUSAND_BTC, // 1000
VT_TEN_THOUSAND_BTC, // 10,000
VT_ONE_HUNDRED_THOUSAND_BTC, // 100,000
VT_ONE_MILLION_BTC, // 1,000,000
VT_LAST_ENTRY
};
double getValueTypeValue(ValueType t)
{
double ret = 0;
switch ( t )
{
case VT_MICRO_BIT: // 0.0001
ret = 0.0001;
break;
case VT_MILI_BIT: // 0.001
ret = 0.001;
break;
case VT_CENTI_BIT: // 0.01
ret = 0.01;
break;
case VT_TENTH: // 0.1
ret = 0.1;
break;
case VT_QUARTER: // 0.25
ret = 0.25;
break;
case VT_BTC: // 1
ret = 1;
break;
case VT_TEN_BTC: // 10
ret = 10;
break;
case VT_HUNDRED_BTC: // 100
ret = 100;
break;
case VT_THOUSAND_BTC: // 1000
ret = 1000;
break;
case VT_TEN_THOUSAND_BTC:
ret = 10000;
break;
case VT_ONE_HUNDRED_THOUSAND_BTC:
ret = 100000;
break;
case VT_ONE_MILLION_BTC:
ret = 1000000;
break;
default:
assert(0);
break;
}
return ret;
}
const char * getValueTypeLabel(ValueType type)
{
const char *ret = "UNKNOWN";
switch (type)
{
case VT_MICRO_BIT: // 0.0001
ret = "0.0001";
break;
case VT_MILI_BIT: // 0.001
ret = "0.001";
break;
case VT_CENTI_BIT: // 0.01
ret = "0.01";
break;
case VT_TENTH: // 0.1
ret = "0.1";
break;
case VT_QUARTER: // 0.25
ret = "0.25";
break;
case VT_BTC: // 1
ret = "1btc";
break;
case VT_TEN_BTC: // 10
ret = "10btc";
break;
case VT_HUNDRED_BTC: // 100
ret = "100btc";
break;
case VT_THOUSAND_BTC: // 1000
ret = "1,000btc";
break;
case VT_TEN_THOUSAND_BTC:
ret = "10,000btc";
break;
case VT_ONE_HUNDRED_THOUSAND_BTC:
ret = "100,000btc";
break;
case VT_ONE_MILLION_BTC:
ret = "1,000,000btc";
break;
default:
assert(0);
break;
}
return ret;
}
struct ValueEntry
{
void init(ValueType t)
{
mType = t;
mCount = 0;
mTotalValue = 0;
mValue = getValueTypeValue(mType);
mLabel = getValueTypeLabel(mType);
}
void addValue(uint64_t v)
{
double dv = double(v) / ONE_BTC;
addValue(v);
}
void addValue(double v)
{
mCount++;
mTotalValue += v;
}
ValueType mType; // type of value
double mValue; // amount of value
uint32_t mCount; // number of transactions within this value range
double mTotalValue; // total value of transactions within this range
const char *mLabel;
};
struct ValueEntryTable
{
ValueEntryTable(void)
{
init();
}
void init(void)
{
mTable[VT_MICRO_BIT].init(VT_MICRO_BIT);
mTable[VT_MILI_BIT].init(VT_MILI_BIT);
mTable[VT_CENTI_BIT].init(VT_CENTI_BIT);
mTable[VT_TENTH].init(VT_TENTH);
mTable[VT_QUARTER].init(VT_QUARTER);
mTable[VT_BTC].init(VT_BTC);
mTable[VT_TEN_BTC].init(VT_TEN_BTC);
mTable[VT_HUNDRED_BTC].init(VT_HUNDRED_BTC);
mTable[VT_THOUSAND_BTC].init(VT_THOUSAND_BTC);
mTable[VT_TEN_THOUSAND_BTC].init(VT_TEN_THOUSAND_BTC);
mTable[VT_ONE_HUNDRED_THOUSAND_BTC].init(VT_ONE_HUNDRED_THOUSAND_BTC);
mTable[VT_ONE_MILLION_BTC].init(VT_ONE_MILLION_BTC);
}
void addValue(uint64_t v)
{
double dv = double(v) / ONE_BTC;
addValue(dv);
}
void addValue(double v)
{
bool found = false;
double prev = 0;
for (uint32_t i = 0; i < (VT_LAST_ENTRY-1); i++)
{
if (v >= prev && v < mTable[i].mValue)
{
found = true;
mTable[i].addValue(v);
break;
}
prev = mTable[i].mValue;
}
if (!found)
{
assert(v >= 1000000);
mTable[VT_ONE_MILLION_BTC].addValue(v);
}
}
void outputHeader(FILE_INTERFACE *fph)
{
for (uint32_t i = 0; i < VT_LAST_ENTRY; i++)
{
fi_fprintf(fph, "\"%s count\",", mTable[i].mLabel);
}
for (uint32_t i = 0; i < VT_LAST_ENTRY; i++)
{
fi_fprintf(fph, "\"%s value\",", mTable[i].mLabel);
}
}
void outputValue(FILE_INTERFACE *fph)
{
for (uint32_t i = 0; i < VT_LAST_ENTRY; i++)
{
fi_fprintf(fph, "%d,", mTable[i].mCount);
}
for (uint32_t i = 0; i < VT_LAST_ENTRY; i++)
{
fi_fprintf(fph, "%f,", mTable[i].mTotalValue);
}
}
ValueEntry mTable[VT_LAST_ENTRY];
};
#define MAXIMUM_DAYS (365*10) // Leave room for 10 years of days
enum AgeRank
{
AR_ONE_DAY,
AR_ONE_WEEK, // 2- 7 days
AR_ONE_MONTH, // 2 to 4 weeks
AR_THREE_MONTHS, // one month to three months
AR_SIX_MONTHS, // six months to one year
AR_ONE_YEAR, // one-to-two years
AR_TWO_YEARS, // two to three years
AR_THREE_YEARS,
AR_FOUR_YEARS,
AR_ZOMBIE, // over 3 years
AR_LAST
};
class AgeStat
{
public:
void init(AgeRank r)
{
switch (r)
{
case AR_ONE_DAY:
mDays = 1;
mLabel = "One Day";
mCount = 0;
mValue = 0;
break;
case AR_ONE_WEEK:
mDays = 7;
mLabel = "Past Week";
mCount = 0;
mValue = 0;
break;
case AR_ONE_MONTH:
mDays = 30;
mLabel = "Past Month";
mCount = 0;
mValue = 0;
break;
case AR_THREE_MONTHS:
mDays = 365 / 4;
mLabel = "One to Three Months";
mCount = 0;
mValue = 0;
break;
case AR_SIX_MONTHS:
mDays = 365 / 2;
mLabel = "Four to Six Months";
mCount = 0;
mValue = 0;
break;
case AR_ONE_YEAR:
mDays = 365;
mLabel = "Six Months to One Year";
mCount = 0;
mValue = 0;
break;
case AR_TWO_YEARS:
mDays = 365 * 2;
mLabel = "One to Two Years";
mCount = 0;
mValue = 0;
break;
case AR_THREE_YEARS:
mDays = 365 * 3;
mLabel = "Two to Three Years";
mCount = 0;
mValue = 0;
break;
case AR_FOUR_YEARS:
mDays = 365 * 4;
mLabel = "Three to Four Years";
mCount = 0;
mValue = 0;
break;
case AR_ZOMBIE:
mDays = 365 * 1000;
mLabel = "Over Four Years";
mCount = 0;
mValue = 0;
break;
}
}
AgeRank mRank;
uint32_t mDays;
const char *mLabel;
uint32_t mCount;
double mValue;
};
class DailyStatistics
{
public:
DailyStatistics(void)
{
memset(this, sizeof(DailyStatistics), 0);
for (uint32_t i = 0; i < AR_LAST; i++)
{
mAgeStats[i].init((AgeRank)i);
}
mValueEntryTable.init();
}
uint32_t getMeanInputCount(void) const
{
return mTransactionCount ? mInputCount / mTransactionCount : 0;
}
uint32_t getMeanOutputCount(void) const
{
return mTransactionCount ? mOutputCount / mTransactionCount : 0;
}
uint32_t getMeanTransactionSize(void) const
{
return mTransactionCount ? mTransactionSize / mTransactionCount : 0;
}
uint32_t mTimeStamp; // The time stamp for this day
uint32_t mTransactionCount; // How many transactions happened on this day
uint32_t mInputCount; // Total number of inputs in all transactions
uint32_t mOutputCount; // Total number of outputs in all transactions
uint32_t mMaxInputCount; // Largest number of transaction inputs on this day
uint32_t mMaxOutputCount; // Largest number of transaction outputs on this day
uint32_t mTransactionSize; // Size of all transactions on this day
uint32_t mMaxTransactionSize; // Maximum transaction size on this day
uint32_t mKeyTypeCounts[BlockChain::KT_LAST]; // Counts of various types of keys
double mTotalInputValue; // Total value of all inputs for this day
uint64_t mMaxInputValue; // Maximum input value for this day
uint32_t mTotalInputScriptLength; // Total size of all input scripts for this day
uint32_t mMaxInputScriptLength; // Maximum input script length on this day
double mTotalOutputValue; // Total value of all outputs for this day
uint64_t mMaxOutputValue; // Maximum output value for this day
uint32_t mTotalOutputScriptLength; // Total size of all output scripts for this day
uint32_t mMaxOutputScriptLength; // Maximum output script length on this day
uint32_t mCurrentBlock; // The current block for this day
uint32_t mTransactionBlockCount; // How many transactions are in the current block
uint32_t mBlockCount; // Number of blocks on this day
uint32_t mMaxTransactionBlockCount; // Maximum number of transactions in a block
uint32_t mMaxInputAge; // old input for this day.
uint32_t mDustCount; // number of dust transactions on this day
uint32_t mZombieInputCount; // Number of times an input more than 3 years old was encountered
double mZombieInputValue; // Total value in BTC that was more than 3 years old
double mZombieScore;
uint32_t mUTXOCount; // total number of unspent transaction outputs
double mUTXOValue; // total value of all unspent transaction outputs
uint32_t mEarlyCount; // Number of unspent transaction outputs from 2009-2010
double mEarlyValue; // Total vaule of unspent transaction outputs from 2009-2010
AgeStat mAgeStats[AR_LAST]; // UTXO by age stats
ValueEntryTable mValueEntryTable;
};
const uint32_t BITCOIN_START_DATE = 1231001362;
uint32_t getAgeInDays(uint32_t timestamp)
{
uint32_t diff = timestamp - BITCOIN_START_DATE;
uint32_t days = diff / SECONDS_PER_DAY;
return days;
}
uint32_t getAgeInDays(uint32_t timestamp,uint32_t referenceDate)
{
uint32_t diff = 0;
if (referenceDate > timestamp)
{
diff = referenceDate - timestamp;
}
uint32_t days = diff / SECONDS_PER_DAY;
return days;
}
uint32_t getAgeInDaysCurrent(uint32_t timestamp)
{
time_t t;
time(&t);
uint32_t diff = uint32_t(t) - timestamp;
uint32_t days = diff / SECONDS_PER_DAY;
return days;
}
bool isEarly(uint32_t timeStamp)
{
bool ret = false;
static char scratch[1024];
time_t t(timeStamp);
struct tm *gtm = gmtime(&t);
uint32_t year = gtm->tm_year + 1900;
if (year == 2009 || year == 2010 )
{
ret = true;
}
return ret;
}
typedef std::vector< uint64_t > TransactionVector;
// A 256 bit hash
class Hash256
{
public:
Hash256(void)
{
mWord0 = 0;
mWord1 = 0;
mWord2 = 0;
mWord3 = 0;
}
Hash256(const Hash256 &h)
{
mWord0 = h.mWord0;
mWord1 = h.mWord1;
mWord2 = h.mWord2;
mWord3 = h.mWord3;
}
inline Hash256(const uint8_t *src)
{
mWord0 = *(const uint64_t *)(src);
mWord1 = *(const uint64_t *)(src + 8);
mWord2 = *(const uint64_t *)(src + 16);
mWord3 = *(const uint64_t *)(src + 24);
}
inline uint32_t getHash(void) const
{
const uint32_t *h = (const uint32_t *)&mWord0;
return h[0] ^ h[1] ^ h[2] ^ h[3] ^ h[4] ^ h[5] ^ h[6] ^ h[7];
}
inline bool operator==(const Hash256 &h) const
{
return mWord0 == h.mWord0 && mWord1 == h.mWord1 && mWord2 == h.mWord2 && mWord3 == h.mWord3;
}
uint64_t mWord0;
uint64_t mWord1;
uint64_t mWord2;
uint64_t mWord3;
};
class PublicKeyData
{
public:
bool operator==(const PublicKeyData &other) const
{
return memcmp(address, other.address, sizeof(address)) == 0;
}
uint8_t address[25];
};
class PublicKey : public PublicKeyData
{
public:
PublicKey(void)
{
}
PublicKey(const PublicKeyData &h) : PublicKeyData(h)
{
mCRC = CRC32(address, sizeof(address), sizeof(address));
}
// Here the == operator is used to see if the hash values match
bool operator==(const PublicKey &other) const
{
if (mCRC == other.mCRC) // if they have the same CRC value...
{
const PublicKeyData &a = *this;
const PublicKeyData &b = other;
return a == b;
}
return false;
}
uint32_t getHash(void) const
{
return mCRC;
}
uint32_t mIndex;
uint32_t mCRC;
};
// Data we would like to accumulate
// First, is it a spend or a receive transaction
// How much value is involved
// What is the timestamp
// Must be exact multiple of 16 bytes
class PublicKeyTransaction
{
public:
PublicKeyTransaction(void) : mTransactionOffset(0)
, mValue(0)
, mTimeStamp(0)
, mSpend(false)
, mCoinbase(false)
, mChange(false)
{
}
uint64_t mTransactionOffset; // 8 : The file offset location to the full transaction details
uint64_t mValue; // 16 : How much value is in this spend/receive transaction
uint32_t mTimeStamp; // 20 : Time stamp for this transaction
bool mSpend : 1; // 24 : is it a spend transaction?
bool mCoinbase : 1; // is it a coinbase transaction
bool mChange : 1; // Whether or not this receive was change (came from ourselves)
uint64_t mBalance; // 8 :
};
typedef std::vector< PublicKeyTransaction > PublicKeyTransactionVector;
// This class represents the collection of all transactions associated with a particular public key
class PublicKeyRecord
{
public:
void save(FILE_INTERFACE *fph)
{
fi_fwrite(&mKeyType, sizeof(mKeyType), 1, fph);
fi_fwrite(&mIndex, sizeof(mIndex), 1, fph);
uint32_t count = uint32_t(mTransactions.size());
fi_fwrite(&count, sizeof(count), 1, fph);
uint32_t padding = 0;
uint64_t padding64 = 0;
fi_fwrite(&padding, sizeof(padding), 1, fph);
fi_fwrite(&padding64, sizeof(padding64), 1, fph); // Will be the balance field in PublicKeyRecordFile
fi_fwrite(&padding, sizeof(padding), 1, fph); // Will be the LastSendTime field in the PublicKeyRecordFile
fi_fwrite(&padding, sizeof(padding), 1, fph); // Will be the LastReceiveTime field in the PublicKeyRecordFile
if (count)
{
PublicKeyTransaction *p = &mTransactions[0];
fi_fwrite(p, sizeof(PublicKeyTransaction)*count, 1, fph);
}
}
BlockChain::KeyType mKeyType; // What type of bitcoin key is this? Standard, MultiSig, Pay2Hash, Stealth?
uint32_t mIndex; // The array index for this public key (needed after pointer sorting)
PublicKeyTransactionVector mTransactions; // all transactions in chronological order relative to this public key
};
// For performance and memory reasons we access the PublicKeyRecords from a memory mapped file; which refers to the copy
// saved to disk rather than the one used during the build phase
// Must match exactly the layout produced by the 'save' method in PublicKeyRecord
class PublicKeyRecordFile
{
public:
uint64_t getBalance(uint32_t endTime=0xFFFFFFFF) const
{
uint64_t ret = 0;
for (uint32_t i = 0; i < mCount; i++)
{
const PublicKeyTransaction &t = mTransactions[i];
if (t.mTimeStamp > endTime)
{
break;
}
if (t.mSpend)
{
ret -= t.mValue;
}
else
{
ret += t.mValue;
}
}
return ret;
}
uint64_t getTotalSend(uint32_t endTime=0xFFFFFFFF) const
{
uint64_t ret = 0;
for (uint32_t i = 0; i < mCount; i++)
{
const PublicKeyTransaction &t = mTransactions[i];
if (t.mTimeStamp > endTime)
{
break;
}
if (t.mSpend)
{
ret += t.mValue;
}
}
return ret;
}
uint64_t getTotalReceive(uint32_t endTime=0xFFFFFFFF) const
{
uint64_t ret = 0;
for (uint32_t i = 0; i < mCount; i++)
{
const PublicKeyTransaction &t = mTransactions[i];
if (t.mTimeStamp > endTime)
{
break;
}
if (!t.mSpend)
{
ret += t.mValue;
}
}
return ret;
}
uint32_t getLastSendTime(uint32_t endTime=0xFFFFFFFF) const
{
uint32_t ret = 0;
uint32_t count = mCount;
if (count)
{
uint32_t index = count - 1;
for (uint32_t i = 0; i < count; i++, index--)
{
const PublicKeyTransaction &t = mTransactions[index];
if (t.mTimeStamp > endTime)
{
continue;
}
if (t.mSpend)
{
ret = t.mTimeStamp;
break;
}
}
}
return ret;
}
uint32_t getLastReceiveTime(uint32_t endTime=0xFFFFFFFF) const
{
uint32_t ret = 0;
uint32_t count = mCount;
if (count)
{
uint32_t index = count - 1;
for (uint32_t i = 0; i < count; i++, index--)
{
const PublicKeyTransaction &t = mTransactions[index];
if (t.mTimeStamp > endTime)
{
continue;
}
if (!t.mSpend)
{
ret = t.mTimeStamp;
break;
}
}
}
return ret;
}
uint32_t getAge(void)
{
uint32_t lastTime;
if ( mLastSendTime)
{
lastTime = mLastSendTime;
}
else
{
lastTime = mTransactions[0].mTimeStamp;
}
uint32_t daysOld = getAgeInDaysCurrent(lastTime);
return daysOld;
}
void computeBalance(uint32_t endTime=0xFFFFFFFF) // compute the balance, up to this time stamp
{
mBalance = getBalance(endTime);
mLastSendTime = getLastSendTime(endTime);
mLastReceiveTime = getLastReceiveTime(endTime);
mDaysOld = getAge();
}
BlockChain::KeyType mKeyType; // What type of bitcoin key is this? Standard, MultiSig, Pay2Hash, Stealth?
uint32_t mIndex; // Array index for public key
uint32_t mCount; // Number of transactions associated with this public key
uint32_t mDaysOld; // Must be here because the PublicKeyTransaction is going to be 16 byte aligned!
uint64_t mBalance; // 8 bytes Balance.
uint32_t mLastSendTime; // compute the time of last sent transaction
uint32_t mLastReceiveTime; // compute the time of the last receive transaction
PublicKeyTransaction mTransactions[1]; // This is a bit of a fake; we are accessing this via a memory mapped file so there will be 'mCount' number of actual transactions
};
class TransactionHash : public Hash256
{
public:
TransactionHash(void) : mFileOffset(0), mTimeStamp(0)
{
}
TransactionHash(const Hash256 &h) : Hash256(h), mFileOffset(0), mTimeStamp(0)
{
}
// Here the == operator is used to see if the hash values match
bool operator==(const TransactionHash &other) const
{
const Hash256 &a = *this;
const Hash256 &b = other;
return a == b;
}
void setFileOffset(uint64_t fileOffset)
{
mFileOffset = fileOffset;
}
uint64_t getFileOffset(void) const
{
return mFileOffset;
}
void setTimeStamp(uint32_t t)
{
mTimeStamp = t;
}
uint32_t getTimeStamp(void) const
{
return mTimeStamp;
}
private:
uint64_t mFileOffset; // The location in the file for this transaction
uint32_t mTimeStamp;
};
class TransactionOutput
{
public:
TransactionOutput(void)
{
}
TransactionOutput(const BlockChain::BlockOutput &bo,uint32_t addressIndex)
{
mValue = bo.value;
mIndex = addressIndex;
mKeyType = bo.keyType;
mScriptLength = bo.challengeScriptLength;
}
TransactionOutput(FILE_INTERFACE *fph)
{
fi_fread(&mValue, sizeof(mValue), 1, fph);
fi_fread(&mIndex, sizeof(mIndex), 1, fph);
fi_fread(&mKeyType, sizeof(mKeyType), 1, fph);
fi_fread(&mScriptLength, sizeof(mScriptLength), 1, fph);
}
void save(FILE_INTERFACE *fph)
{
fi_fwrite(&mValue, sizeof(mValue), 1, fph);
fi_fwrite(&mIndex, sizeof(mIndex), 1, fph);
fi_fwrite(&mKeyType, sizeof(mKeyType), 1, fph);
fi_fwrite(&mScriptLength, sizeof(mScriptLength), 1, fph );
}
void echo(void)
{
}
uint64_t mValue; // The value of the output
uint32_t mIndex; // The array index for this public key (stored in a separate table)
BlockChain::KeyType mKeyType; // type of key
uint32_t mScriptLength;
};
class TransactionInput
{
public:
TransactionInput(void)
{
}
TransactionInput(const BlockChain::BlockInput &bi, uint64_t fileOffset,uint32_t timeStamp,uint64_t inputValue)
{
mTransactionFileOffset = fileOffset;
mTransactionIndex = bi.transactionIndex;
mInputValue = inputValue;
mResponseScriptLength = bi.responseScriptLength;
mTimeStamp = timeStamp;
}
TransactionInput(FILE_INTERFACE *fph)
{
fi_fread(&mTransactionFileOffset, sizeof(mTransactionFileOffset), 1, fph);
fi_fread(&mTransactionIndex, sizeof(mTransactionIndex), 1, fph);
fi_fread(&mInputValue, sizeof(mInputValue), 1, fph);
fi_fread(&mResponseScriptLength, sizeof(mResponseScriptLength), 1, fph);
fi_fread(&mTimeStamp, sizeof(mTimeStamp), 1, fph);
}
void save(FILE_INTERFACE *fph)
{
fi_fwrite(&mTransactionFileOffset, sizeof(mTransactionFileOffset), 1, fph);
fi_fwrite(&mTransactionIndex, sizeof(mTransactionIndex), 1, fph);
fi_fwrite(&mInputValue, sizeof(mInputValue), 1, fph);
fi_fwrite(&mResponseScriptLength, sizeof(mResponseScriptLength), 1, fph);
fi_fwrite(&mTimeStamp, sizeof(mTimeStamp), 1, fph);
}
void echo(void)
{
}
uint64_t mTransactionFileOffset; // Which transaction this input refers to (0 means coinbase)
uint32_t mTransactionIndex; // Which output forms this input
uint32_t mResponseScriptLength; // The length of the response script
uint64_t mInputValue; // The input value
uint32_t mTimeStamp;
};
typedef std::vector< TransactionInput > TransactionInputVector;
typedef std::vector< TransactionOutput > TransactionOutputVector;
// Temporarily holds the data representing a transaction
class Transaction
{
public:
Transaction(void)
{
}
Transaction(const BlockChain::BlockTransaction &t,uint32_t transactionTime,uint32_t blockNumber)
{
memcpy(mTransactionHash, t.transactionHash, sizeof(mTransactionHash));
mTransactionVersionNumber = t.transactionVersionNumber;
mLockTime = t.lockTime;
mTransactionTime = transactionTime;
mBlockNumber = blockNumber;
mTransactionSize = t.transactionLength;
}
bool read(FILE_INTERFACE *fph)
{
bool ret = true;
mInputs.clear();
mOutputs.clear();
size_t r = fi_fread(mTransactionHash, sizeof(mTransactionHash), 1, fph); // Write out the transaction hash
if (r != 1)
{
ret = false;
}
else
{
fi_fread(&mBlockNumber, sizeof(mBlockNumber), 1, fph); // Write out the transaction hash
fi_fread(&mTransactionVersionNumber, sizeof(mTransactionVersionNumber), 1, fph); // Write out the transaction version number
fi_fread(&mTransactionTime, sizeof(mTransactionTime), 1, fph); // Write out the block-time of this transaction.
fi_fread(&mLockTime, sizeof(mLockTime), 1, fph); // Write out the lock-time of this transaction.
fi_fread(&mTransactionSize, sizeof(mTransactionSize), 1, fph);
uint32_t count;
fi_fread(&count, sizeof(count), 1, fph);
mInputs.reserve(count);
for (uint32_t i = 0; i < count; i++)
{
TransactionInput ti(fph);
mInputs.push_back(ti);
}
fi_fread(&count, sizeof(count), 1, fph);
mOutputs.reserve(count);
for (uint32_t i = 0; i < count; i++)
{
TransactionOutput to(fph);
mOutputs.push_back(to);
}
}
return ret;
}
void save(FILE_INTERFACE *fph)
{
fi_fwrite(mTransactionHash, sizeof(mTransactionHash), 1, fph); // Write out the transaction hash
fi_fwrite(&mBlockNumber, sizeof(mBlockNumber), 1, fph); // Write out the transaction hash
fi_fwrite(&mTransactionVersionNumber, sizeof(mTransactionVersionNumber), 1, fph); // Write out the transaction version number
fi_fwrite(&mTransactionTime, sizeof(mTransactionTime), 1, fph); // Write out the block-time of this transaction.
fi_fwrite(&mLockTime, sizeof(mLockTime), 1, fph); // Write out the lock-time of this transaction.
fi_fwrite(&mTransactionSize, sizeof(mTransactionSize), 1, fph);
uint32_t count = uint32_t(mInputs.size()); // Write out the number of transaction inputs
fi_fwrite(&count, sizeof(count), 1, fph);
for (uint32_t i = 0; i < count; i++)
{
mInputs[i].save(fph); // Save each input
}
count = uint32_t(mOutputs.size()); // Write out the number of transaction outputs
fi_fwrite(&count, sizeof(count), 1, fph);
for (uint32_t i = 0; i < count; i++)
{
mOutputs[i].save(fph); // Write out each output
}
}
void addInput(const BlockChain::BlockInput &bi, uint64_t fileOffset,uint32_t timeStamp,uint64_t inputValue)
{
TransactionInput ti(bi, fileOffset,timeStamp,inputValue);
mInputs.push_back(ti);
}
void addOutput(const BlockChain::BlockOutput &bo,uint32_t addressIndex)
{
TransactionOutput to(bo,addressIndex);
mOutputs.push_back(to);
}
void echo(void)
{
logMessage("===============================================================================================================================\n");