-
Notifications
You must be signed in to change notification settings - Fork 8
/
qarchiver.c
1280 lines (1229 loc) · 42.8 KB
/
qarchiver.c
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
#define QARCHIVER
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "uthash.h"
#include "utlist.h"
#include "qshared/K12AndKeyUtil.h"
#include "qshared/qdefines.h"
#include "qshared/qstructs.h"
#include "qshared/qexterns.h"
struct qubictx
{
UT_hash_handle hh;
uint8_t txid[32];
int32_t txlen;
uint8_t txdata[];
} *TXIDS;
pthread_mutex_t txid_mutex;
struct voteq_entry *VOTEQ[MAXTICKS];
int32_t Voteallocs,Votedup,Totalsandwiches;
struct election
{
Tick ref;
struct voteq_entry *voters[NUMBER_OF_COMPUTORS];
int32_t numvotes;
} Candidates[6];
struct RAM_Quorum
{
uint8_t qchain[32],spectrum[32];
int32_t validated,count,pending,needtx;
} RAMQ[MAXTICKS];
#include "qshared/qkeys.c"
#include "qshared/qhelpers.c"
#include "qshared/qtime.c"
#include "qconn.c"
#define QUORUM_FETCHWT 100
#define TX_FETCHWT 300
void queue_vote(int32_t peerid,int32_t offset,Tick *qdata)
{
struct voteq_entry *vp;
if ( qdata->tick >= VALIDATED_TICK )
{
vp = (struct voteq_entry *)calloc(1,sizeof(*vp));
vp->vote = *qdata;
pthread_mutex_lock(&Peertickinfo[peerid].peermutex);
Voteallocs++;
DL_APPEND(Peertickinfo[peerid].voteQ,vp);
pthread_mutex_unlock(&Peertickinfo[peerid].peermutex);
}
}
void dequeue_votes()
{
int32_t peerid,offset,numdel = 0;
struct voteq_entry *vp;
for (offset=0; offset<VALIDATED_TICK-INITIAL_TICK-10; offset++)
{
while ( (vp= VOTEQ[offset]) != 0 )
{
DL_DELETE(VOTEQ[offset],vp);
free(vp);
Voteallocs--;
numdel++;
}
}
//printf("dequeue_votes num deleted %d\n",numdel);
for (peerid=0; peerid<MAXPEERS; peerid++)
{
pthread_mutex_lock(&Peertickinfo[peerid].peermutex);
while ( (vp= Peertickinfo[peerid].voteQ) != 0 )
{
DL_DELETE(Peertickinfo[peerid].voteQ,vp);
if ( vp != 0 )
{
offset = (vp->vote.tick - INITIAL_TICK);
if ( offset >= 0 && offset < MAXTICKS )
{
DL_APPEND(VOTEQ[offset],vp);
}
else
{
free(vp);
Voteallocs--;
}
}
}
pthread_mutex_unlock(&Peertickinfo[peerid].peermutex);
}
}
int32_t cmptick(Tick *a,Tick *b)
{
if ( a->epoch != b->epoch )
return(-1);
if ( a->tick != b->tick )
return(-2);
if ( a->millisecond != b->millisecond )
return(-3);
if ( a->second != b->second )
return(-4);
if ( a->minute != b->minute )
return(-5);
if ( a->hour != b->hour )
return(-6);
if ( a->day != b->day )
return(-7);
if ( a->month != b->month )
return(-8);
if ( a->year != b->year )
return(-9);
if ( a->prevResourceTestingDigest != b->prevResourceTestingDigest )
return(-10);
//if ( a->saltedResourceTestingDigest != b->saltedResourceTestingDigest )
// return(-11);
if ( memcmp(a->prevSpectrumDigest,b->prevSpectrumDigest,sizeof(a->prevSpectrumDigest)) != 0 )
return(-12);
if ( memcmp(a->prevUniverseDigest,b->prevUniverseDigest,sizeof(a->prevUniverseDigest)) != 0 )
return(-13);
if ( memcmp(a->prevComputerDigest,b->prevComputerDigest,sizeof(a->prevComputerDigest)) != 0 )
return(-14);
//if ( memcmp(a->saltedSpectrumDigest,b->saltedSpectrumDigest,sizeof(a->saltedSpectrumDigest)) != 0 )
// return(-15);
//if ( memcmp(a->saltedUniverseDigest,b->saltedUniverseDigest,sizeof(a->saltedUniverseDigest)) != 0 )
// return(-16);
//if ( memcmp(a->saltedComputerDigest,b->saltedComputerDigest,sizeof(a->saltedComputerDigest)) != 0 )
// return(-17);
if ( memcmp(a->transactionDigest,b->transactionDigest,sizeof(a->transactionDigest)) != 0 )
return(-18);
//if ( memcmp(a->expectedNextTickTransactionDigest,b->expectedNextTickTransactionDigest,sizeof(a->expectedNextTickTransactionDigest)) != 0 )
// return(-19);
return(0);
}
int32_t have_quorum(int32_t tick)
{
struct voteq_entry *vp,*tmp;
struct election *votes[NUMBER_OF_COMPUTORS];
char fname[512];
FILE *fp;
int32_t err,offset,i,numcand,numvoters,winner,audit;
offset = (tick - INITIAL_TICK);
if ( offset >= 0 && offset < MAXTICKS )
{
numcand = 0;
numvoters = 0;
winner = -1;
memset(votes,0,sizeof(votes));
memset(Candidates,0,sizeof(Candidates));
//printf("check quorum %d offset.%d\n",tick,offset);
DL_FOREACH_SAFE(VOTEQ[offset],vp,tmp)
{
if ( vp->vote.tick != tick || votes[vp->vote.computorIndex] != 0 )
{
DL_DELETE(VOTEQ[offset],vp);
free(vp);
Votedup++;
Voteallocs--;
continue;
}
for (i=0; i<numcand; i++)
{
if ( (err= cmptick(&vp->vote,&Candidates[i].ref)) == 0 )
{
votes[vp->vote.computorIndex] = &Candidates[i];
Candidates[i].voters[Candidates[i].numvotes++] = vp;
numvoters++;
//printf("computor.%d matches candidate.%d votes.%d\n",vp->vote.computorIndex,i,Candidates[i].numvotes);
if ( Candidates[i].numvotes >= TWOTHIRDS )
winner = i;
break;
}
}
if ( i == numcand )
{
if ( i+1 < sizeof(Candidates)/sizeof(*Candidates) )
{
Candidates[i].ref = vp->vote;
//printf("tick.%d create new candidate.%d n.%d from computor.%d\n",tick,i,n,vp->vote.computorIndex);
votes[vp->vote.computorIndex] = &Candidates[i];
Candidates[i].voters[Candidates[i].numvotes++] = vp;
numvoters++;
numcand++;
} else printf("out of candidate slots.%d\n",i);
}
}
if ( winner >= 0 && Candidates[winner].numvotes >= TWOTHIRDS )
{
audit = 0;
for (i=0; i<NUMBER_OF_COMPUTORS; i++)
if ( votes[i] == &Candidates[winner] )
audit++;
printf("tick.%d candidate.%d wins with %d votes out of %d. audit.%d Voteallocs.%d dups.%d\n",tick,winner,Candidates[winner].numvotes,numvoters,audit,Voteallocs,Votedup);
if ( audit == Candidates[winner].numvotes )
{
RAMQ[offset].validated = Candidates[winner].numvotes;
epochfname(fname,EPOCH,tick,".Q");
if ( (fp= fopen(fname,"wb")) != 0 )
{
for (i=0; i<Candidates[winner].numvotes; i++)
{
if ( cmptick(&Candidates[winner].voters[i]->vote,&Candidates[winner].ref) == 0 )
fwrite(&Candidates[winner].voters[i]->vote,1,sizeof(Candidates[winner].voters[i]->vote),fp);
else printf("err.%d voter.%d for winner.%d with numvotes.%d\n",err,i,winner,Candidates[winner].numvotes);
}
fclose(fp);
}
DL_FOREACH_SAFE(VOTEQ[offset],vp,tmp)
{
DL_DELETE(VOTEQ[offset],vp);
free(vp);
Voteallocs--;
}
return(Candidates[winner].numvotes);
}
}
}
return(-1);
}
void _qubictxadd(uint8_t txid[32],uint8_t *txdata,int32_t txlen)
{
struct qubictx *qtx;
qtx = (struct qubictx *)calloc(1,sizeof(*qtx) + txlen);
memcpy(qtx->txid,txid,sizeof(qtx->txid));
qtx->txlen = txlen;
memcpy(qtx->txdata,txdata,txlen);
HASH_ADD_KEYPTR(hh,TXIDS,qtx->txid,sizeof(qtx->txid),qtx);
}
struct qubictx *qubictxhash(int32_t *addedflagp,uint8_t txid[32],uint8_t *txdata,int32_t txlen)
{
int32_t flag = 0;
struct qubictx *qtx;
pthread_mutex_lock(&txid_mutex);
HASH_FIND(hh,TXIDS,txid,sizeof(qtx->txid),qtx);
if ( qtx == 0 && txdata != 0 && txlen >= sizeof(Transaction) )
{
flag = 1;
_qubictxadd(txid,txdata,txlen);
HASH_FIND(hh,TXIDS,txid,sizeof(qtx->txid),qtx);
if ( qtx == 0 )
printf("FATAL HASH TABLE ERROR\n");
}
pthread_mutex_unlock(&txid_mutex);
if ( addedflagp != 0 )
*addedflagp = flag;
return(qtx);
}
void qpurge(int32_t tick)
{
char fname[512];
epochfname(fname,EPOCH,tick,".T");
deletefile(fname);
epochfname(fname,EPOCH,tick,".Q");
deletefile(fname);
epochfname(fname,EPOCH,tick,"");
deletefile(fname);
if ( VALIDATED_TICK >= tick )
VALIDATED_TICK = tick - 1;
if ( HAVE_TXTICK >= tick )
HAVE_TXTICK = tick - 1;
}
void qchaincalc(uint8_t prevhash[32],uint8_t qchain[32],Tick T)
{
uint8_t digest[32];
char hexstr[65];
Qchain Q;
memset(&Q,0,sizeof(Q));
Q.epoch = T.epoch;
Q.tick = T.tick;
Q.millisecond = T.millisecond;
Q.second = T.second;
Q.minute = T.minute;
Q.hour = T.hour;
Q.day = T.day;
Q.month = T.month;
Q.year = T.year;
Q.prevResourceTestingDigest = T.prevResourceTestingDigest;
memcpy(Q.prevSpectrumDigest,T.prevSpectrumDigest,sizeof(Q.prevSpectrumDigest));
memcpy(Q.prevUniverseDigest,T.prevUniverseDigest,sizeof(Q.prevUniverseDigest));
memcpy(Q.prevComputerDigest,T.prevComputerDigest,sizeof(Q.prevComputerDigest));
memcpy(Q.transactionDigest,T.transactionDigest,sizeof(Q.transactionDigest));
memcpy(Q.prevqchain,prevhash,sizeof(Q.prevqchain));
KangarooTwelve((uint8_t *)&Q,sizeof(Q),digest,32);
byteToHex(digest,hexstr,sizeof(digest));
if ( (T.tick % 1000) == 0 )
printf("qchain.%-6d %s\n",T.tick,hexstr);
memcpy(qchain,digest,sizeof(digest));
}
void incr_VALIDATE_TICK(int32_t tick,Tick T)
{
FILE *fp;
char fname[512],spectrumstr[65],msg[512];
int32_t i;
long offset;
uint8_t prevhash[32],buf[64];
offset = (tick - INITIAL_TICK);
if ( offset >= 0 && offset < MAXTICKS )
{
if ( offset == 0 )
memset(prevhash,0,sizeof(prevhash));
else
{
memcpy(RAMQ[offset].spectrum,T.prevSpectrumDigest,sizeof(prevhash));
memcpy(prevhash,RAMQ[offset-1].qchain,sizeof(prevhash));
}
RAMQ[offset].validated = NUMBER_OF_COMPUTORS;
qchaincalc(prevhash,RAMQ[offset].qchain,T);
sprintf(fname,"%s%cqchain.%d",DATADIR,dir_delim(),EPOCH);
if ( (fp= fopen(fname,"rb+")) == 0 )
{
if ( (fp= fopen(fname,"wb")) != 0 )
{
memset(buf,0,sizeof(buf));
for (i=0; i<MAXTICKS; i++)
fwrite(buf,1,sizeof(buf),fp);
fclose(fp);
}
fp = fopen(fname,"rb+");
}
if ( fp != 0 )
{
byteToHex(RAMQ[offset].spectrum,spectrumstr,32);
sprintf(msg,"merkle %d %s",tick,spectrumstr);
Qserver_msg(msg);
fseek(fp,offset * 64,SEEK_SET);
memcpy(buf,RAMQ[offset].spectrum,32);
memcpy(&buf[32],RAMQ[offset].qchain,32);
fwrite(buf,1,sizeof(buf),fp);
fclose(fp);
}
}
VALIDATED_TICK = tick;
PROGRESSTIME = LATEST_UTIME;
EXITFLAG = 0;
}
int32_t update_validated(void)
{
Tick T;
TickData TD;
uint8_t zeros[32],digest[32];
FILE *fp;
int32_t tick,n,haveQ=0,deleteflag,missed = 0;
char fname[512];
n = 0;
memset(zeros,0,sizeof(zeros));
if ( INITIAL_TICK != 0 )
{
for (tick=VALIDATED_TICK!=0?VALIDATED_TICK:INITIAL_TICK; tick<LATEST_TICK && missed<100; tick++)
{
epochfname(fname,EPOCH,tick,".Q");
if ( (fp= fopen(fname,"rb")) != 0 )
{
fclose(fp);
//printf("skip tick.%d as Q file already there\n",tick);
continue;
}
if ( have_quorum(tick) < TWOTHIRDS )
missed++;
}
}
missed = 0;
for (tick=VALIDATED_TICK!=0?VALIDATED_TICK:INITIAL_TICK; tick<LATEST_TICK && missed<100; tick++)
{
n++;
haveQ = 0;
//printf("validate %d missed.%d\n",tick,missed);
/*
epochfname(fname,EPOCH,tick,".T");
if ( (fp= fopen(fname,"rb")) != 0 )
{
fclose(fp);
epochfname(fname,EPOCH,tick,".Q");
if ( (fp= fopen(fname,"rb")) != 0 )
{
while ( fread(&T,1,sizeof(T),fp) == sizeof(T) )
{
if ( T.tick == tick )
{
haveQ = 1;
break;
}
}
fclose(fp);
if ( haveQ != 0 && missed == 0 && tick > VALIDATED_TICK )
{
//printf("skip VALIDATED_TICK.%d\n",tick);
incr_VALIDATE_TICK(tick,T);
continue;
}
}
//printf("haveQ.%d tick.%d even with Qfile\n",haveQ,tick);
}*/
epochfname(fname,EPOCH,tick,".Q");
if ( (fp= fopen(fname,"rb")) != 0 )
{
if ( fread(&T,1,sizeof(T),fp) == sizeof(T) && T.tick == tick )
haveQ = 1;
fclose(fp);
if ( haveQ == 0 )
{
deletefile(fname);
missed++;
printf("delete strange Q file %s\n",fname);
continue;
}
deleteflag = 0;
epochfname(fname,EPOCH,tick,"");
if ( (fp= fopen(fname,"rb")) != 0 )
{
fseek(fp,0,SEEK_END);
if ( ftell(fp) == 32 )
{
memset(digest,0,sizeof(digest));
//printf("empty tick.%d\n",tick);
}
else
{
rewind(fp);
if ( fread(&TD,1,sizeof(TD),fp) == sizeof(TD) )
KangarooTwelve((uint8_t *)&TD,sizeof(TD),digest,32);
else
{
deleteflag = 1;
printf("TD read error %s\n",fname);
}
}
fclose(fp);
//printf("tick.%d haveQ.%d deleteflag.%d missed.%d %s\n",tick,haveQ,deleteflag,missed,fname);
if ( deleteflag == 0 )
{
if ( memcmp(digest,T.transactionDigest,sizeof(digest)) == 0 )
{
if ( missed == 0 && tick > VALIDATED_TICK )
{
//printf("VALIDATED_TICK.%d T.tick %d latest.%d\n",tick,T.tick,LATEST_TICK);
incr_VALIDATE_TICK(tick,T);
continue;
}
}
else
{
deleteflag = 1;
char hex1[65],hex2[65];
byteToHex(digest,hex1,32);
byteToHex(T.transactionDigest,hex2,32);
if ( memcmp(T.transactionDigest,zeros,32) != 0 )
printf("transactiondigest mismatch tick.%d %s vs %s\n",T.tick,hex1,hex2);
}
}
else
{
//printf("validate deletes %s\n",fname);
deletefile(fname);
}
}
else
{
//printf("%s missing haveQ.%d iszero.%d\n",fname,haveQ,memcmp(zeros,T.transactionDigest,sizeof(zeros)));
if ( memcmp(zeros,T.transactionDigest,sizeof(zeros)) == 0 )
{
if ( (fp= fopen(fname,"wb")) != 0 )
{
fwrite(zeros,1,sizeof(zeros),fp);
fclose(fp);
}
}
missed++;
}
}
else
{
missed++;
//printf("%d missing\n",tick);
}
}
if ( (rand() % 100) == 0 )
printf("end VALIDATED_TICK.%d missed.%d of %d\n",VALIDATED_TICK,missed,n);
return(VALIDATED_TICK);
}
int32_t quorumsigverify(Computors *computors,Tick *T)
{
int computorIndex = T->computorIndex;
uint8_t digest[32],computorOfThisTick[32];
T->computorIndex ^= BROADCAST_TICK;
KangarooTwelve((uint8_t *)T,sizeof(*T) - SIGNATURE_SIZE,digest,32);
memcpy(computorOfThisTick,computors->publicKeys[computorIndex],32);
if ( verify(computorOfThisTick,digest,T->signature) != 0 )
{
T->computorIndex ^= BROADCAST_TICK;
return(1);
}
T->computorIndex ^= BROADCAST_TICK;
return(0);
}
void process_quorumdata(int32_t peerid,char *ipaddr,Tick *qdata,Computors *computors)
{
int32_t offset;
if ( INITIAL_TICK == 0 || qdata->tick < INITIAL_TICK || qdata->tick >= INITIAL_TICK+MAXTICKS )
return;
if ( qdata->epoch == EPOCH && qdata->tick != 0 && qdata->computorIndex >= 0 && qdata->computorIndex < 676 && quorumsigverify(computors,qdata) == 1 )
{
offset = qdata->tick - INITIAL_TICK;
queue_vote(peerid,offset,qdata);
}
}
int32_t updateticktx(int32_t tick,char *ipaddr,int32_t sock)
{
RequestedTickTransactions TX;
struct quheader H;
uint8_t reqbuf[sizeof(H) + sizeof(TX)];
memset(&TX,0,sizeof(TX));
memset(reqbuf,0,sizeof(reqbuf));
H = quheaderset(REQUEST_TICK_TRANSACTIONS,sizeof(H) + sizeof(TX));
memcpy(reqbuf,&H,sizeof(H));
TX.tick = tick;
memcpy(&reqbuf[sizeof(H)],&TX,sizeof(TX));
//printf(">>>>>>>>>>> %s updateticktx %d\n",ipaddr,tick);
return(socksend(ipaddr,sock,reqbuf,sizeof(H) + sizeof(TX)));
}
int32_t updatetick(int32_t *nump,int32_t tick,char *ipaddr,int32_t sock)
{
FILE *fp;
char fname[512];
struct quheader H;
RequestedQuorumTick R;
int32_t offset;
uint8_t reqbuf[sizeof(H) + sizeof(R) ];
epochfname(fname,EPOCH,tick,"");
if ( (fp= fopen(fname,"rb")) == 0 )
{
sock = updateticktx(tick,ipaddr,sock);
H = quheaderset(REQUEST_TICK_DATA,sizeof(H) + sizeof(tick));
memcpy(reqbuf,&H,sizeof(H));
memcpy(&reqbuf[sizeof(H)],&tick,sizeof(tick));
sock = socksend(ipaddr,sock,reqbuf,sizeof(H) + sizeof(tick));
//printf("request %d\n",tick);
(*nump)++;
} else fclose(fp);
epochfname(fname,EPOCH,tick,".Q");
offset = (tick - INITIAL_TICK);
if ( offset >= 0 && offset < MAXTICKS )//&& RAMQ[offset].pending < LATEST_TICK-1 )
{
if ( (fp= fopen(fname,"rb")) == 0 )
{
//printf("request %d.Q\n",tick);
memset(&R,0,sizeof(R));
memset(reqbuf,0,sizeof(reqbuf));
H = quheaderset(REQUEST_QUORUMTICK,sizeof(H) + sizeof(R));
memcpy(reqbuf,&H,sizeof(H));
R.tick = tick;
memcpy(&reqbuf[sizeof(H)],&R,sizeof(R));
sock = socksend(ipaddr,sock,reqbuf,sizeof(H) + sizeof(R));
RAMQ[offset].pending = LATEST_TICK;
(*nump) += 2;
} else fclose(fp);
}
return(sock);
}
void process_entity(int32_t peerid,char *ipaddr,RespondedEntity *E)
{
printf("unexpected entity data in qarchiver\n");
}
int32_t process_transaction(int32_t *savedtxp,FILE *fp,int32_t peerid,char *ipaddr,Transaction *tx,int32_t txlen)
{
static uint8_t txdata[MAXPEERS+1][4096],sigs[MAXPEERS+1][64]; // needs to be aligned or crashes in verify
uint8_t digest[32],txid[32],zero[32],pubkey[32];
char addr[64],dest[64],txidstr[64],qmsg[512],smany[2][64];
int32_t i,addedflag,v = -1;
if ( txlen < sizeof(txdata[peerid]) )
{
memcpy(txdata[peerid],tx,txlen);
KangarooTwelve(txdata[peerid],txlen,txid,32);
getTxHashFromDigest(txid,txidstr);
txidstr[60] = 0;
KangarooTwelve(txdata[peerid],txlen-64,digest,32);
memcpy(sigs[peerid],&txdata[peerid][txlen-64],64);
v = verify(txdata[peerid],digest,sigs[peerid]);
pubkey2addr(tx->sourcePublicKey,addr);
if ( v > 0 && fp != 0 )
{
fwrite(&txlen,1,sizeof(txlen),fp);
fwrite(txid,1,sizeof(txid),fp);
fwrite(tx,1,txlen,fp);
qubictxhash(&addedflag,txid,(uint8_t *)tx,txlen);
(*savedtxp)++;
if ( addedflag != 0 && tx->amount != 0 && tx->tick > LATEST_TICK )
{
if ( tx->inputType == SENDTOMANYV1 )
{
if ( ((uint64_t *)tx->destinationPublicKey)[0] == QUTIL_CONTRACT_ID )
{
memset(zero,0,sizeof(zero));
for (i=0; i<25; i++)
{
memcpy(pubkey,&txdata[peerid][sizeof(*tx) + i*32],32);
if ( memcmp(pubkey,zero,32) != 0 )
{
pubkey2addr(pubkey,dest);
strcpy(smany[i%2],dest);
if ( (i&1) != 0 )
{
sprintf(qmsg,"future %d %s %s",tx->tick,smany[0],smany[1]);
Qserver_msg(qmsg);
Totalsandwiches++;
}
//printf("%s %s, ",dest,amountstr(*(uint64_t *)&txdata[peerid][sizeof(*tx) + 25*32 + i*8]));
}
}
printf("detected sendmany from %s extrasize.%d, might do redundant Qmsg for last dest\n",addr,tx->inputSize);
}
}
else pubkey2addr(tx->destinationPublicKey,dest);
sprintf(qmsg,"future %d %s %s",tx->tick,addr,dest);
//printf("QMSG.(%s)\n",qmsg);
Totalsandwiches++;
Qserver_msg(qmsg);
printf("Total.%d lag.%d %s %s amount %s -> %s\n",Totalsandwiches,tx->tick - LATEST_TICK,ipaddr,addr,amountstr(tx->amount),dest);
}
}
//printf("process tx from tick.%d %s %s v.%d txlen.%d %p\n",tx->tick,txidstr,addr,v,txlen,qubictxhash(txid,(uint8_t *)tx,txlen));
}
return(v);
}
int32_t validate_computors(Computors *computors)
{
uint8_t digest[32],arbPubkey[32];
addr2pubkey(ARBITRATOR,arbPubkey);
KangarooTwelve((uint8_t *)computors,sizeof(*computors) - SIGNATURE_SIZE,digest,32);
return(verify(arbPubkey,digest,computors->signature));
}
int32_t has_computors(Computors *computors,char *ipaddr)
{
FILE *fp;
char fname[512];
sprintf(fname,"%scomputors%c%s",epochdirname(EPOCH),dir_delim(),ipaddr);
if ( (fp= fopen(fname,"rb")) != 0 )
{
fread(computors,1,sizeof(*computors),fp);
fclose(fp);
return(validate_computors(computors));
}
return(-1);
}
void process_computors(int32_t peerid,char *ipaddr,Computors *computors)
{
int32_t v;
FILE *fp;
char fname[512];
v = validate_computors(computors);
if ( v != 0 )
{
sprintf(fname,"%scomputors%c%s",epochdirname(EPOCH),dir_delim(),ipaddr);
if ( (fp= fopen(fname,"rb")) == 0 )
{
if ( (fp= fopen(fname,"wb")) != 0 )
{
fwrite(computors,1,sizeof(*computors),fp);
fclose(fp);
}
} else fclose(fp);
}
printf("peerid.%d computors.%d v.%d\n",peerid,EPOCH,v);
}
void process_tickdata(int32_t peerid,char *ipaddr,TickData *tickdata,Computors *computors)
{
FILE *fp;
char fname[512];
uint8_t sigcheck[32],sig[64],pubkey[32];
int32_t computorIndex = tickdata->computorIndex;
tickdata->computorIndex ^= BROADCAST_FUTURE_TICK_DATA;
KangarooTwelve((uint8_t *)tickdata,sizeof(*tickdata) - SIGNATURE_SIZE,sigcheck,32);
tickdata->computorIndex ^= BROADCAST_FUTURE_TICK_DATA;
memcpy(sig,tickdata->signature,sizeof(tickdata->signature));
memcpy(pubkey,computors->publicKeys[computorIndex],sizeof(pubkey));
if ( verify(pubkey,sigcheck,sig) != 0 )
{
//printf("process tickdata %d GOOD SIG! computor.%d\n",tickdata->tick,computorIndex);
epochfname(fname,EPOCH,tickdata->tick,"");
if ( (fp= fopen(fname,"wb")) != 0 )
{
fwrite(tickdata,1,sizeof(*tickdata),fp);
fclose(fp);
}
}
//else printf("process tickdata %d sig error computor.%d\n",tickdata->tick,computorIndex);
}
void process_issued(int32_t peerid,char *ipaddr,RespondIssuedAssets *issued)
{
}
void process_owned(int32_t peerid,char *ipaddr,RespondOwnedAssets *owned)
{
}
void process_possessed(int32_t peerid,char *ipaddr,RespondPossessedAssets *possessed)
{
}
int32_t process_response(int32_t *savedtxp,FILE *fp,Computors *computors,int32_t peerid,char *ipaddr,struct quheader *H,void *data,int32_t datasize)
{
switch ( H->_type )
{
case EXCHANGE_PUBLIC_PEERS: if ( datasize != sizeof(ExchangePublicPeers) ) return(-1);
process_publicpeers(peerid,ipaddr,(ExchangePublicPeers *)data);
break;
case BROADCAST_COMPUTORS: if ( datasize != sizeof(Computors) ) return(-1);
process_computors(peerid,ipaddr,(Computors *)data);
break;
case BROADCAST_TICK: if ( datasize != sizeof(Tick) ) return(-1);
process_quorumdata(peerid,ipaddr,(Tick *)data,computors);
break;
case BROADCAST_FUTURE_TICK_DATA: if ( datasize > sizeof(TickData) ) return(-1);
process_tickdata(peerid,ipaddr,(TickData *)data,computors);
break;
case BROADCAST_TRANSACTION: if ( datasize < sizeof(Transaction) ) return(-1);
process_transaction(savedtxp,fp,peerid,ipaddr,(Transaction *)data,datasize);
break;
case RESPOND_CURRENT_TICK_INFO: if ( datasize != sizeof(CurrentTickInfo) ) return(-1);
process_tickinfo(peerid,ipaddr,(CurrentTickInfo *)data);
break;
case RESPOND_ENTITY: if ( datasize != sizeof(RespondedEntity) ) return(-1);
process_entity(peerid,ipaddr,(RespondedEntity *)data);
break;
case RESPOND_ISSUED_ASSETS: if ( datasize != sizeof(RespondIssuedAssets) ) return(-1);
process_issued(peerid,ipaddr,(RespondIssuedAssets *)data);
break;
case RESPOND_OWNED_ASSETS: if ( datasize != sizeof(RespondOwnedAssets) ) return(-1);
process_owned(peerid,ipaddr,(RespondOwnedAssets *)data);
break;
case RESPOND_POSSESSED_ASSETS: if ( datasize != sizeof(RespondPossessedAssets) ) return(-1);
process_possessed(peerid,ipaddr,(RespondPossessedAssets *)data);
break;
case REQUEST_SYSTEM_INFO: // we are not server
break;
case REQUEST_TICK_DATA:
break;
case REQUEST_COMPUTORS:
break;
case REQUEST_QUORUMTICK:
break;
case REQUEST_TICK_TRANSACTIONS:
break;
case BROADCAST_MESSAGE:
break;
//case PROCESS_SPECIAL_COMMAND: if ( datasize != sizeof() ) return(-1);
default: printf("%s unknown type.%d sz.%d\n",ipaddr,H->_type,datasize);
break;
}
//printf("peerid.%d got %d cmd.%d from %s\n",peerid,datasize,H->_type,ipaddr);
return(0);
}
int32_t updateticks(char *ipaddr,int32_t sock)
{
int32_t i,tick,n,numsent,firsttick,numticks,offset,ind;
if ( Numpeers == 0 )
return(sock);
ind = (rand() % Numpeers);
firsttick = (VALIDATED_TICK > INITIAL_TICK) ? VALIDATED_TICK : INITIAL_TICK-1;
numticks = (LATEST_TICK - firsttick);
n = 10 * numticks / Numpeers;
if ( n < 676 )
n = 676;
numsent = 0;
if ( VALIDATED_TICK == 0 )
sock = updatetick(&numsent,INITIAL_TICK,ipaddr,sock);
else sock = updatetick(&numsent,VALIDATED_TICK+1,ipaddr,sock);
if ( HAVE_TXTICK == 0 )
sock = updateticktx(INITIAL_TICK,ipaddr,sock);
else
sock = updateticktx(HAVE_TXTICK+1,ipaddr,sock);
/*if ( LATEST_TICK - VALIDATED_TICK < 5 )
{
for (tick=VALIDATED_TICK+2; tick<=LATEST_TICK+2; tick++)
sock = updatetick(&numsent,tick,ipaddr,sock);
}
else*/
{
for (i=0; i<n; i++)
{
tick = firsttick + i*Numpeers + ind + 1;
if ( tick > LATEST_TICK+2 )
break;
sock = updatetick(&numsent,tick,ipaddr,sock);
if ( numsent > QUORUM_FETCHWT )
break;
}
}
if ( numsent < TX_FETCHWT )
{
firsttick = (HAVE_TXTICK > INITIAL_TICK) ? HAVE_TXTICK : INITIAL_TICK-1;
for (i=0; i<n && numsent<TX_FETCHWT; i++)
{
tick = firsttick + i*Numpeers + ind + 1;
if ( tick > LATEST_TICK+2 )
break;
offset = (tick - INITIAL_TICK);
if ( offset >= 0 && offset < MAXTICKS && RAMQ[offset].needtx != 0 )
{
sock = updateticktx(tick,ipaddr,sock);
numsent++;
}
}
}
return(sock);
}
void *peerthread(void *_ipaddr)
{
char *ipaddr = _ipaddr;
struct EntityRequest E;
Computors computors;
FILE *fp;
char fname[512],addr[64];
int32_t peerid,sock=-1,i,savedtx,ptr,sz,recvbyte,prevutime,prevtick,iter,bufsize,hascomputors;
struct quheader H;
uint8_t *buf;
signal(SIGPIPE, SIG_IGN);
bufsize = 1024 * 1024 * 64;
buf = calloc(1,bufsize);
peerid = prevutime = prevtick = iter = hascomputors = 0;
while ( iter++ < 10 )
{
if ( (sock= myconnect(ipaddr,DEFAULT_NODE_PORT)) < 0 )
{
//printf("iter.%d peerthread error connecting to %s\n",iter,ipaddr);
}
else break;
}
if ( iter >= 10 )
return(0);
pthread_mutex_lock(&addpeer_mutex);
if ( Numpeers < MAXPEERS-1 ) // nonzero peerid wastes a slot but no big loss
{
Numpeers++;
peerid = Numpeers;
}
else
{
printf("maxpeers %d reached\n",Numpeers);
pthread_mutex_unlock(&addpeer_mutex);
return(0);
}
pthread_mutex_unlock(&addpeer_mutex);
strcpy(Peertickinfo[peerid].ipaddr,ipaddr);
peertxfname(fname,ipaddr);
if ( (fp= fopen(fname,"rb+")) != 0 )
fseek(fp,0,SEEK_END);
else fp = fopen(fname,"wb");
printf("connected.%d peerthread %s\n",Numpeers,ipaddr);
H = quheaderset(REQUEST_CURRENT_TICK_INFO,sizeof(H));
sock = socksend(ipaddr,sock,(uint8_t *)&H,sizeof(H));
while ( 1 )
{
if ( EXITFLAG != 0 && LATEST_UTIME > EXITFLAG )
break;
if ( hascomputors == 0 && has_computors(&computors,ipaddr) <= 0 )
{
H = quheaderset(REQUEST_COMPUTORS,sizeof(H));
sock = socksend(ipaddr,sock,(uint8_t *)&H,sizeof(H));
} else hascomputors = 1;
savedtx = 0;
while ( (recvbyte= receiveall(sock,buf,bufsize)) > 0 )
{
ptr = 0;
sz = 1;
while ( ptr < recvbyte && sz != 0 && sz < recvbyte )
{
memcpy(&H,&buf[ptr],sizeof(H));
sz = ((H._size[2] << 16) + (H._size[1] << 8) + H._size[0]);
//for (int j=0; j<sz; j++)
// printf("%02x",buf[ptr+j]);
//printf(" %s received %d H.(%d %d bytes)\n",ipaddr,recvbyte,H._type,sz);
if ( sz < 1 || sz > recvbyte-ptr )
{
//printf("illegal sz.%d vs recv.%d ptr.%d type.%d\n",sz,recvbyte,ptr,H._type);
break;
}
if ( H._type == 35 && sz == sizeof(H) )
{
}
else
{
if ( process_response(&savedtx,fp,&computors,peerid,ipaddr,&H,&buf[ptr + sizeof(H)],sz - sizeof(H)) < 0 )
{
//printf("peerid.%d Error processing H.type %d size.%ld\n",peerid,H._type,sz - sizeof(H));
}
else if ( H._type == EXCHANGE_PUBLIC_PEERS )
sock = socksend(ipaddr,sock,(uint8_t *)&H,sz);
}
ptr += sz;
}
}
if ( fp != 0 && savedtx != 0 )
fflush(fp);
if ( Peertickinfo[peerid].packetlen != 0 )
{
printf("%s sends packet[%d]\n",ipaddr,Peertickinfo[peerid].packetlen);
sock = socksend(ipaddr,sock,Peertickinfo[peerid].packet,Peertickinfo[peerid].packetlen);
Peertickinfo[peerid].packetlen = 0;
}
if ( EXITFLAG == 0 && LATEST_UTIME > prevutime )
{
if ( EXITFLAG != 0 && LATEST_UTIME > EXITFLAG )
break;
prevutime = LATEST_UTIME;
H = quheaderset(REQUEST_CURRENT_TICK_INFO,sizeof(H));
sock = socksend(ipaddr,sock,(uint8_t *)&H,sizeof(H));
if ( hascomputors != 0 && (LATEST_UTIME % 7) == (peerid % 7) )
sock = updateticks(ipaddr,sock);
}
if ( LATEST_TICK > prevtick )
{
prevtick = LATEST_TICK;
} else usleep(100000);
}
if ( sock >= 0 )
close(sock);
if ( fp != 0 )
fclose(fp);
printf("%s exits thread\n",ipaddr);
return(0);
}
int32_t loadqtx(FILE *fp)
{
int32_t txlen,retval = -1;
char txidstr[64];
struct qubictx *qtx;
uint8_t txid[32],cmptxid[32],txdata[MAX_TX_SIZE];
if ( fread(&txlen,1,sizeof(txlen),fp) != sizeof(txlen) )
{
//printf("error reading txlen at pos %ld\n",*fposp);
}
else if ( txlen < sizeof(txdata) )
{
if ( fread(txid,1,sizeof(txid),fp) != sizeof(txid) )
{
//printf("error reading txid at pos %ld\n",*fposp);
}
else if ( fread(txdata,1,txlen,fp) != txlen )
{
//printf("error reading txdata at pos %ld\n",*fposp);
}
else
{
KangarooTwelve(txdata,txlen,cmptxid,32);
getTxHashFromDigest(txid,txidstr);
txidstr[60] = 0;
if ( memcmp(txid,cmptxid,sizeof(txid)) == 0 )
{
HASH_FIND(hh,TXIDS,txid,sizeof(qtx->txid),qtx);
if ( qtx == 0 )
{
_qubictxadd(txid,txdata,txlen);
retval = 1;
} else retval = 0;
}