forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iguana_recv.dev
executable file
·812 lines (788 loc) · 34.9 KB
/
iguana_recv.dev
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
/******************************************************************************
* Copyright © 2014-2015 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
#include "iguana777.h"
// peer context, ie massively multithreaded -> bundlesQ
struct iguana_bundlereq *iguana_bundlereq(struct iguana_info *coin,struct iguana_peer *addr,int32_t type,int32_t datalen)
{
struct iguana_bundlereq *req; int32_t allocsize;
allocsize = (uint32_t)sizeof(*req) + datalen;
req = mycalloc(type,1,allocsize);
req->allocsize = allocsize;
req->datalen = datalen;
req->addr = addr;
req->coin = coin;
req->type = type;
return(req);
}
void iguana_gotblockM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_txblock *origtxdata,struct iguana_msgtx *txarray,uint8_t *data,int32_t recvlen)
{
struct iguana_bundlereq *req; struct iguana_txblock *txdata = 0; int32_t i; char fname[1024];
if ( 0 )
{
for (i=0; i<txdata->space[0]; i++)
if ( txdata->space[i] != 0 )
break;
if ( i != txdata->space[0] )
{
for (i=0; i<txdata->space[0]; i++)
printf("%02x ",txdata->space[i]);
printf("extra\n");
}
}
req = iguana_bundlereq(coin,addr,'B',0);
txdata = origtxdata;
if ( addr != 0 )
{
if ( addr->pendblocks > 0 )
addr->pendblocks--;
addr->lastblockrecv = (uint32_t)time(NULL);
addr->recvblocks += 1.;
addr->recvtotal += recvlen;
origtxdata->block.ipbits = addr->ipbits;
iguana_ramchain_data(coin,addr,origtxdata,txarray,origtxdata->block.txn_count,data,recvlen);
{
txdata->block.ipbits = addr->ipbits;
if ( 0 )
{
struct iguana_txblock *checktxdata; struct iguana_memspace checkmem; int32_t checkbundlei;
memset(&checkmem,0,sizeof(checkmem));
iguana_meminit(&checkmem,"checkmem",0,txdata->datalen + 4096,0);
if ( (checktxdata= iguana_peertxdata(coin,&checkbundlei,fname,&checkmem,addr->ipbits,txdata->block.hash2)) != 0 )
{
printf("check datalen.%d bundlei.%d T.%d U.%d S.%d P.%d X.%d\n",checktxdata->datalen,checkbundlei,checktxdata->numtxids,checktxdata->numunspents,checktxdata->numspends,checktxdata->numpkinds,checktxdata->numexternaltxids);
}
iguana_mempurge(&checkmem);
}
}
req->datalen = txdata->datalen;
}
//printf("datalen.%d\n",req->datalen);
if ( req->datalen == 0 )
req->datalen = recvlen;
req->block = txdata->block;
req->ipbits = txdata->block.ipbits;
req->block.txn_count = req->numtx = txdata->block.txn_count;
coin->recvcount++;
coin->recvtime = (uint32_t)time(NULL);
req->addr = addr;
queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0);
}
void iguana_gottxidsM(struct iguana_info *coin,struct iguana_peer *addr,bits256 *txids,int32_t n)
{
struct iguana_bundlereq *req;
printf("got %d txids from %s\n",n,addr->ipaddr);
req = iguana_bundlereq(coin,addr,'T',0);
req->hashes = txids, req->n = n;
queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0);
}
void iguana_gotunconfirmedM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_msgtx *tx,uint8_t *data,int32_t datalen)
{
struct iguana_bundlereq *req;
char str[65]; bits256_str(str,tx->txid);
printf("%s unconfirmed.%s\n",addr->ipaddr,str);
req = iguana_bundlereq(coin,addr,'U',datalen);
req->datalen = datalen;
memcpy(req->serialized,data,datalen);
//iguana_freetx(tx,1);
queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0);
}
void iguana_gotheadersM(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_block *blocks,int32_t n)
{
struct iguana_bundlereq *req;
if ( addr != 0 )
{
addr->recvhdrs++;
if ( addr->pendhdrs > 0 )
addr->pendhdrs--;
//printf("%s blocks[%d] ht.%d gotheaders pend.%d %.0f\n",addr->ipaddr,n,blocks[0].height,addr->pendhdrs,milliseconds());
}
req = iguana_bundlereq(coin,addr,'H',0);
req->blocks = blocks, req->n = n;
queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0);
}
void iguana_gotblockhashesM(struct iguana_info *coin,struct iguana_peer *addr,bits256 *blockhashes,int32_t n)
{
struct iguana_bundlereq *req;
if ( addr != 0 )
{
addr->recvhdrs++;
if ( addr->pendhdrs > 0 )
addr->pendhdrs--;
}
req = iguana_bundlereq(coin,addr,'S',0);
req->hashes = blockhashes, req->n = n;
//printf("bundlesQ blockhashes.%p[%d]\n",blockhashes,n);
queue_enqueue("bundlesQ",&coin->bundlesQ,&req->DL,0);
}
void iguana_blocklink(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei,struct iguana_block *prev,struct iguana_block *block)
{
if ( block == 0 || prev == 0 )
return;
//printf("%p prev-> %p [%d:%d] %p block %p\n",prev,prev->hh.next,bp!=0?bp->bundleheight:-1,bundlei,block->hh.prev,block);
if ( (prev->hh.next == 0 || prev->hh.next == block) && block->hh.prev == 0 )
block->hh.prev = prev;
else if ( prev->hh.next == 0 && block->hh.prev == prev )
prev->hh.next = block;
else if ( prev->hh.next == block && block->hh.prev == prev )
{
/*if ( bundlei < coin->chain->bundlesize )
{
if ( bp != 0 && iguana_bundlehash2add(coin,0,bp,bundlei,block->hash2) < 0 )
{
if ( prev->mainchain == 0 )
block->hh.prev = prev->hh.next = 0;
printf("clear hashes?\n");
//memset(bp->hashes[i].bytes,0,sizeof(bp->hashes[i]));
}
}
else if ( bp != 0 && bp->bundleheight + coin->chain->bundlesize >= coin->bundlescount*coin->chain->bundlesize )
{
char str[65]; printf("AUTOCREATE.%d new bundle.%s\n",bp->bundleheight + coin->chain->bundlesize,bits256_str(str,block->hash2));
iguana_bundlecreate(coin,&bundlei,bp->bundleheight + coin->chain->bundlesize,block->hash2);
//for (j=2; j<bp->n; j++)
// iguana_blockQ(coin,bp,j,bp->hashes[j],0);
}*/
}
else if ( prev->mainchain == 0 )
block->hh.prev = prev->hh.next = 0;
}
void iguana_recvissue(struct iguana_info *coin,struct iguana_block *origblock)
{
int32_t height,bundlei,i; struct iguana_block *prev,*block; bits256 prevhash2; struct iguana_bundle *bp;
block = iguana_blockhashset(coin,-1,origblock->prev_block,1);
if ( block != origblock )
iguana_blockcopy(coin,block,origblock);
for (i=0; i<coin->chain->bundlesize; i++)
{
if ( block != 0 && bits256_nonz(block->prev_block) > 0 )
{
prev = iguana_blockhashset(coin,-1,block->prev_block,1);
bp = 0, bundlei = -2, iguana_bundlefind(coin,&bp,&bundlei,block->hash2);
if ( prev != 0 && prev->ipbits == 0 && bp != 0 && bp->bundleheight >= 0 )
{
prevhash2 = iguana_blockhash(coin,bp->bundleheight + bundlei - 1);
if ( bits256_nonz(prevhash2) == 0 )
{
height = bp->bundleheight + bundlei - 1;
//char str[65]; printf("[%d] RECVISSUE.%s %d\n",i,bits256_str(str,block->prev_block),height);
iguana_hash2set(coin,"recvadd",coin->bundles[height/coin->chain->bundlesize],height%coin->chain->bundlesize,block->prev_block);
iguana_blockQ(coin,coin->bundles[height/coin->chain->bundlesize],height%coin->chain->bundlesize,block->prev_block,1);
break;
}
}
block = prev;
} else break;
}
}
// main context, ie single threaded
struct iguana_bundlereq *iguana_recvblockhashes(struct iguana_info *coin,struct iguana_bundlereq *req,bits256 *blockhashes,int32_t num)
{
int32_t i,bundlei; struct iguana_bundle *bp; struct iguana_block *block,*prev = 0;
bp = 0, bundlei = -2, iguana_bundlefind(coin,&bp,&bundlei,blockhashes[1]);
if ( (rand() % 100) == 0 )
{
char str[65]; printf("got %d hashes %d:%d %s\n",num,bp==0?-1:bp->bundleheight,bundlei,bits256_str(str,blockhashes[1]));
}
if ( bp == 0 || bundlei != 1 || num <= 2 )
{
if ( num > 2 )
iguana_blockQ(coin,0,-1,blockhashes[1],1);
return(req);
}
bp->hdrtime = (uint32_t)time(NULL);
//if ( num > coin->chain->bundlesize+1 )
// num = coin->chain->bundlesize+1;
for (i=0; i<num; i++)
{
if ( bits256_nonz(blockhashes[i]) > 0 )
{
if ( (block= iguana_blockhashset(coin,-1,blockhashes[i],1)) != 0 && prev != 0 && block->ipbits == 0 )
{
prev->hh.next = block;
if ( prev->mainchain != 0 )
{
//char str[65]; printf("idle issue %s %d\n",bits256_str(str,block->hash2),prev->height+1);
iguana_blockQ(coin,0,-1,block->hash2,0);
}
}
if ( block != 0 && block->ipbits == 0 && i < coin->chain->bundlesize )
iguana_hash2set(coin,"recvhash",bp,i,blockhashes[i]);
if ( (i % coin->chain->bundlesize) == 0 || i == 1 )
iguana_blockQ(coin,0,-1,blockhashes[i],1);
else //if ( bp->hdrsi < 100 )
iguana_blockQ(coin,0,-1,blockhashes[i],0);
}
prev = block;
}
return(req);
}
struct iguana_bundle *iguana_bundleset(struct iguana_info *coin,struct iguana_block **blockp,int32_t *bundleip,struct iguana_block *origblock)
{
struct iguana_block *block,*prev = 0; struct iguana_bundle *bp = 0; int32_t bundlei = -2;
if ( origblock == 0 )
return(0);
block = iguana_blockhashset(coin,-1,origblock->hash2,1);
*blockp = block;
if ( block != 0 )
{
if ( bits256_nonz(origblock->prev_block) > 0 )
{
prev = iguana_blockhashset(coin,-1,origblock->prev_block,1);
//iguana_blocklink(coin,0,-1,prev,block);
if ( prev != 0 )
{
prev->hh.next = block, block->hh.prev = prev;
//printf("link block\n");
}
}
if ( (bp= iguana_bundlefind(coin,&bp,&bundlei,origblock->hash2)) != 0 )
{
if ( bundlei < coin->chain->bundlesize )
{
iguana_blocklink(coin,bp,bundlei,prev,block);
block->bundlei = bundlei;
block->hdrsi = bp->hdrsi;
block->havebundle = 1;
iguana_hash2set(coin,"blockadd",bp,block->bundlei,block->hash2);
}
}
else if ( (bp= iguana_bundlefind(coin,&bp,&bundlei,origblock->prev_block)) != 0 )
{
if ( bundlei < coin->chain->bundlesize-1 )
{
block->bundlei = ++bundlei;
block->hdrsi = bp->hdrsi;
block->havebundle = 1;
iguana_hash2set(coin,"blockadd",bp,block->bundlei,block->hash2);
}
else if ( bundlei == coin->chain->bundlesize-1 )
{
char str[65]; printf("CREATE.%d new bundle.%s\n",bp->bundleheight + coin->chain->bundlesize,bits256_str(str,origblock->hash2));
iguana_blockQ(coin,0,-1,origblock->hash2,1);
iguana_bundlecreate(coin,&bundlei,bp->bundleheight + coin->chain->bundlesize,origblock->hash2);
}
}
else
{
//char str[65]; printf("can find.(%s)\n",bits256_str(str,origblock->hash2));
return(0);
}
if ( block->havebundle != 0 && block->hdrsi < coin->bundlescount )
{
bundlei = block->bundlei;
bp = coin->bundles[block->hdrsi];
}
//char str[65]; printf("iguana_recvblock (%s) %d %d[%d] %p\n",bits256_str(str,block->hash2),block->havebundle,block->hdrsi,bundlei,bp);
}
*bundleip = bundlei;
return(bp);
}
struct iguana_bundlereq *iguana_recvblockhdrs(struct iguana_info *coin,struct iguana_bundlereq *req,struct iguana_block *blocks,int32_t n,int32_t *newhwmp)
{
int32_t i,bundlei; struct iguana_block *block; struct iguana_bundle *bp;
if ( blocks == 0 )
{
printf("iguana_recvblockhdrs null blocks?\n");
return(req);
}
if ( blocks != 0 && n > 0 )
{
for (i=0; i<n; i++)
{
//fprintf(stderr,"i.%d of %d bundleset\n",i,n);
bp = iguana_bundleset(coin,&block,&bundlei,&blocks[i]);
if ( block != 0 && memcmp(block->prev_block.bytes,coin->blocks.hwmchain.hash2.bytes,sizeof(bits256)) == 0 )
_iguana_chainlink(coin,block);
if ( bundlei == 1 )
iguana_blockQ(coin,0,-1,blocks[i].hash2,1);
else if ( bp != 0 && bp->hdrsi < IGUANA_MAXACTIVEBUNDLES )
iguana_blockQ(coin,0,-1,blocks[i].hash2,0);
//fprintf(stderr,"i.%d of %d iguana_chainextend\n",i,n);
//iguana_chainextend(coin,&blocks[i]);
}
}
return(req);
}
struct iguana_bundlereq *iguana_recvblock(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_bundlereq *req,struct iguana_block *origblock,int32_t numtx,int32_t datalen,int32_t *newhwmp)
{
struct iguana_bundle *bp=0; char str[65]; int32_t tmp,bundlei = -2;
bits256 hash2; struct iguana_block *block,*prev,*ptr; double duration;
bp = iguana_bundleset(coin,&block,&bundlei,origblock);
if ( block != origblock )
iguana_blockcopy(coin,block,origblock);
if ( memcmp(block->prev_block.bytes,coin->blocks.hwmchain.hash2.bytes,sizeof(bits256)) == 0 )
{
_iguana_chainlink(coin,block);
iguana_bundleset(coin,&ptr,&tmp,block);
//printf("HWMCHAIN %s height.%d\n",bits256_str(str,block->hash2),coin->blocks.hwmchain.height+1);
}
else if ( (prev= iguana_blockfind(coin,block->prev_block)) != 0 )
{
if ( prev->mainchain != 0 && prev->height >= 0 )
{
block->mainchain = 1;
block->height = prev->height+1;
iguana_bundleset(coin,&ptr,&tmp,block);
hash2 = iguana_blockhash(coin,block->height+1);
if ( bits256_nonz(hash2) == 0 && (ptr= block->hh.next) != 0 )
hash2 = ptr->hash2;
if ( bits256_nonz(hash2) > 0 && (ptr= iguana_blockfind(coin,hash2)) != 0 && ptr->ipbits == 0 )
{
//printf("AUTONEXT.%d\n",block->height+1);
if ( prev->height == coin->blocks.hwmchain.height )
{
coin->backstop = coin->blocks.hwmchain.height+1;
coin->backstophash2 = hash2;
coin->backstopmillis = milliseconds();
}
iguana_blockQ(coin,0,-1,hash2,0);
}
}
}
if ( strcmp(coin->symbol,"BTC") != 0 )
iguana_recvissue(coin,block);
if ( bp != 0 && bundlei >= 0 )
{
if ( bp->bundleheight+bundlei > coin->longestchain )
coin->longestchain = bp->bundleheight+bundlei;
if ( bp->bundleheight+bundlei == coin->blocks.hwmchain.height+1 && bp->requests[bundlei] > 99 )
printf("recv bundlei.%d hdrs.%d reqs.[%d]\n",bundlei,bp->hdrsi,bp->requests[bundlei]);
if ( 0 && bundlei == 1 && bp->numhashes < bp->n )
{
bits256_str(str,block->prev_block);
queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(str),1);
}
if ( datalen > 0 )
{
SETBIT(bp->recv,bundlei);
if ( bp->issued[bundlei] > 0 )
{
duration = 1000. * ((int32_t)time(NULL) - bp->issued[bundlei]);
if ( duration < bp->avetime/10. )
duration = bp->avetime/10.;
else if ( duration > bp->avetime*10. )
duration = bp->avetime * 10.;
dxblend(&bp->avetime,duration,.9);
if ( bp->avetime < 100 )
bp->avetime = 100;
dxblend(&coin->avetime,bp->avetime,.9);
if ( coin->avetime < 100 )
coin->avetime = 100;
}
if ( bundlei >= 0 && bundlei < bp->n )
{
//bp->blocks[bundlei] = block;
//bp->numrecv++;
}
}
if ( strcmp(coin->symbol,"BTC") != 0 && bp->bundleheight+bundlei == coin->blocks.hwmchain.height+1 )
{
_iguana_chainlink(coin,block);
//if ( (rand() % 10) == 0 )
{
hash2 = iguana_blockhash(coin,coin->blocks.hwmchain.height+2);
if ( bits256_nonz(hash2) > 0 )
{
printf("AUTOBLOCK.%d\n",coin->blocks.hwmchain.height+2);
iguana_blockQ(coin,0,-1,hash2,0);
}
}
}
}
if ( block != 0 )
{
block->recvlen = datalen;
if ( bp != 0 )
block->ipbits = req->ipbits;
//printf("datalen.%d ipbits.%x\n",datalen,req->ipbits);
} else printf("cant create block.%llx block.%p bp.%p bundlei.%d\n",(long long)origblock->hash2.txid,block,bp,bundlei);
return(req);
}
struct iguana_bundlereq *iguana_recvtxids(struct iguana_info *coin,struct iguana_bundlereq *req,bits256 *txids,int32_t n)
{
return(req);
}
struct iguana_bundlereq *iguana_recvunconfirmed(struct iguana_info *coin,struct iguana_bundlereq *req,uint8_t *data,int32_t datalen)
{
return(req);
}
int32_t iguana_processbundlesQ(struct iguana_info *coin,int32_t *newhwmp) // single threaded
{
int32_t flag = 0; struct iguana_bundlereq *req;
*newhwmp = 0;
while ( flag < IGUANA_BUNDLELOOP && (req= queue_dequeue(&coin->bundlesQ,0)) != 0 )
{
//printf("%s bundlesQ.%p type.%c n.%d\n",req->addr != 0 ? req->addr->ipaddr : "0",req,req->type,req->n);
if ( req->type == 'B' ) // one block with all txdata
req = iguana_recvblock(coin,req->addr,req,&req->block,req->numtx,req->datalen,newhwmp);
else if ( req->type == 'H' ) // blockhdrs (doesnt have txn_count!)
{
if ( (req= iguana_recvblockhdrs(coin,req,req->blocks,req->n,newhwmp)) != 0 )
{
if ( req->blocks != 0 )
myfree(req->blocks,sizeof(*req->blocks) * req->n), req->blocks = 0;
}
}
else if ( req->type == 'S' ) // blockhashes
{
if ( (req= iguana_recvblockhashes(coin,req,req->hashes,req->n)) != 0 && req->hashes != 0 )
myfree(req->hashes,sizeof(*req->hashes) * req->n), req->hashes = 0;
}
else if ( req->type == 'U' ) // unconfirmed tx
req = iguana_recvunconfirmed(coin,req,req->serialized,req->datalen);
else if ( req->type == 'T' ) // txids from inv
{
if ( (req= iguana_recvtxids(coin,req,req->hashes,req->n)) != 0 )
myfree(req->hashes,(req->n+1) * sizeof(*req->hashes)), req->hashes = 0;
}
else printf("iguana_updatebundles unknown type.%c\n",req->type);
flag++;
//printf("done %s bundlesQ.%p type.%c n.%d\n",req->addr != 0 ? req->addr->ipaddr : "0",req,req->type,req->n);
if ( req != 0 )
myfree(req,req->allocsize), req = 0;
}
return(flag);
}
/*int32_t iguana_needhdrs(struct iguana_info *coin)
{
if ( coin->longestchain == 0 || coin->blocks.hashblocks < coin->longestchain-coin->chain->bundlesize )
return(1);
else return(0);
}*/
int32_t iguana_reqhdrs(struct iguana_info *coin)
{
int32_t i,gap,n = 0; struct iguana_bundle *bp; char hashstr[65];
if ( queue_size(&coin->hdrsQ) == 0 ) //iguana_needhdrs(coin) > 0 &&
{
if ( coin->zcount++ > 100 )
{
for (i=0; i<coin->bundlescount; i++)
{
if ( (bp= coin->bundles[i]) != 0 )
{
if ( i == coin->bundlescount-1 && coin->longestchain/coin->chain->bundlesize != i )
gap = 6;
else gap = 60;
if ( bp->emitfinish != 0 || bp->numhashes >= bp->n || time(NULL) < bp->hdrtime+gap )
continue;
if ( bp->emitfinish == 0 && time(NULL) > bp->issuetime+gap )
{
printf("LAG.%ld hdrsi.%d numhashes.%d:%d qsize.%d zcount.%d\n",time(NULL)-bp->hdrtime,i,bp->numhashes,bp->n,queue_size(&coin->hdrsQ),coin->zcount);
if ( bp->issuetime == 0 )
coin->numpendings++;
char str[65];
bits256_str(str,bp->hashes[0]);
printf("(%s %d).%d ",str,bp->bundleheight,i);
init_hexbytes_noT(hashstr,bp->hashes[0].bytes,sizeof(bits256));
queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(hashstr),1);
n++;
bp->issuetime = (uint32_t)time(NULL);
}
}
}
if ( n > 0 )
printf("REQ HDRS pending.%d\n",coin->numpendings);
coin->zcount = 0;
}
} else coin->zcount = 0;
return(n);
}
struct iguana_blockreq { struct queueitem DL; bits256 hash2,*blockhashes; struct iguana_bundle *bp; int32_t n,height,bundlei; };
int32_t iguana_blockQ(struct iguana_info *coin,struct iguana_bundle *bp,int32_t bundlei,bits256 hash2,int32_t priority)
{
queue_t *Q; char *str; struct iguana_blockreq *req; struct iguana_block *block;
if ( bits256_nonz(hash2) == 0 )
{
printf("cant queue zerohash bundlei.%d\n",bundlei);
return(-1);
}
if ( priority != 0 || bp == 0 || (block= iguana_blockfind(coin,bp->hashes[bundlei])) == 0 || block->ipbits == 0 )
{
if ( priority != 0 )
str = "priorityQ", Q = &coin->priorityQ;
else str = "blocksQ", Q = &coin->blocksQ;
if ( Q != 0 )
{
req = mycalloc('r',1,sizeof(*req));
req->hash2 = hash2;
req->bp = bp;
req->bundlei = bundlei;
if ( bp != 0 && bundlei >= 0 && bundlei < bp->n )
{
bp->issued[bundlei] = milliseconds();
if ( bp->bundleheight >= 0 )
req->height = (bp->bundleheight + bundlei);
}
char str[65];
bits256_str(str,hash2);
if ( 0 && (bundlei % 250) == 0 )
printf("%s %d %s recv.%d numranked.%d qsize.%d\n",str,req->height,str,coin->blocks.recvblocks,coin->peers.numranked,queue_size(Q));
queue_enqueue(str,Q,&req->DL,0);
return(1);
} else printf("null Q\n");
} //else printf("queueblock skip priority.%d bundlei.%d\n",bundlei,priority);
return(0);
}
int32_t iguana_pollQsPT(struct iguana_info *coin,struct iguana_peer *addr)
{
uint8_t serialized[sizeof(struct iguana_msghdr) + sizeof(uint32_t)*32 + sizeof(bits256)];
char *hashstr=0,hexstr[65]; bits256 hash2; uint32_t now; struct iguana_blockreq *req=0;
struct iguana_bundle *bp,*bestbp = 0; int32_t limit,refbundlei,bundlei,height=-1,datalen,flag = 0;
now = (uint32_t)time(NULL);
if ( addr->pendhdrs < IGUANA_MAXPENDHDRS ) //iguana_needhdrs(coin) != 0 &&
{
//printf("%s check hdrsQ\n",addr->ipaddr);
if ( (hashstr= queue_dequeue(&coin->hdrsQ,1)) != 0 )
{
if ( (datalen= iguana_gethdrs(coin,serialized,coin->chain->gethdrsmsg,hashstr)) > 0 )
{
decode_hex(hash2.bytes,sizeof(hash2),hashstr);
bp = 0, bundlei = -2, iguana_bundlefind(coin,&bp,&bundlei,hash2);
if ( bits256_nonz(hash2) > 0 )
{
if ( bp == 0 || bp->emitfinish == 0 )
{
if ( (rand() % 100) == 0 )
printf("%s request hdr.(%s) ht.%d\n",addr!=0?addr->ipaddr:"local",hashstr,bp!=0?bp->bundleheight:-1);
iguana_send(coin,addr,serialized,datalen);
addr->pendhdrs++;
flag++;
}
}
} else printf("datalen.%d from gethdrs\n",datalen);
free_queueitem(hashstr);
hashstr = 0;
if ( flag != 0 )
return(flag);
}
}
if ( (limit= addr->recvblocks) > coin->MAXPENDING )
limit = coin->MAXPENDING;
if ( limit < 1 )
limit = 1;
if ( coin->bundlescount > 0 && (req= queue_dequeue(&coin->priorityQ,0)) == 0 && addr->pendblocks < limit )
{
int32_t i,flag,r,diff,j,k,n; double metric,bestmetric = -1.;
for (i=n=0; i<coin->bundlescount; i++)
if ( coin->bundles[i] != 0 && coin->bundles[i]->emitfinish == 0 )
n++;
if ( n >= coin->bundlescount-(coin->bundlescount>>3) || (addr->ipbits % 10) < 2 )
refbundlei = (addr->ipbits % coin->bundlescount);
else
{
if ( n*2 < coin->bundlescount )
{
for (i=refbundlei=0; i<IGUANA_MAXPEERS; i++)
{
if ( addr->usock == coin->peers.active[i].usock )
break;
if ( coin->peers.active[i].usock >= 0 )
refbundlei++;
}
//printf("half done\n");
} else refbundlei = ((addr->addrind*100) % coin->bundlescount);
}
for (i=0; i<coin->bundlescount; i++)
{
if ( (diff= (i - refbundlei)) < 0 )
diff = -diff;
if ( (bp= coin->bundles[i]) != 0 && bp->emitfinish == 0 )
{
metric = (1 + diff * ((addr->addrind&1) == 0 ? 1 : diff) * (1. + bp->metric)) / (i + 1);
//printf("%f ",bp->metric);
if ( bestmetric < 0. || metric < bestmetric )
bestmetric = metric, bestbp = bp;
}
}
if ( bestbp != 0 && bp->emitfinish == 0 )
{
for (k=0; k<coin->bundlescount; k++)
{
i = (bestbp->hdrsi + k) % coin->bundlescount;
if ( (bp= coin->bundles[i]) == 0 || bp->emitfinish > coin->starttime )
continue;
//printf("%.15f ref.%d addrind.%d bestbp.%d\n",bestmetric,refbundlei,addr->addrind,bp->hdrsi);
for (r=0; r<coin->chain->bundlesize && r<bp->n; r++)
{
j = (addr->addrind*3 + r) % bp->n;
hash2 = bp->hashes[j];
if ( bits256_nonz(hash2) == 0 )
continue;
flag = 0;
if ( (bp->numrecv >= bp->n-5 || bp->requests[j] <= bp->minrequests) && bp->ipbits[j] == 0 && (bp->issued[j] == 0 || now > bp->issued[j]+bp->avetime/1000.) )
flag = 1;
if ( flag != 0 )
{
init_hexbytes_noT(hexstr,hash2.bytes,sizeof(hash2));
if ( (datalen= iguana_getdata(coin,serialized,MSG_BLOCK,hexstr)) > 0 )
{
iguana_send(coin,addr,serialized,datalen);
coin->numreqsent++;
addr->pendblocks++;
addr->pendtime = (uint32_t)time(NULL);
if ( 1 && (rand() % 1000) == 0 )
{
char str[65];
printf(" %s %s issue.%d %d lag.%d\n",addr->ipaddr,bits256_str(str,hash2),bp->hdrsi,j,now-bp->issued[j]);
}
bp->issued[j] = (uint32_t)time(NULL);
if ( bp->requests[j] < 100 )
bp->requests[j]++;
return(1);
} else printf("MSG_BLOCK null datalen.%d\n",datalen);
} //else printf("null hash\n");
}
}
}
}
if ( addr->rank != 1 && req == 0 )
req = queue_dequeue(&coin->blocksQ,0);
if ( req != 0 )
{
hash2 = req->hash2;
height = req->height;
if ( 0 && req->bp != 0 && req->bundlei >= 0 && req->bundlei < req->bp->n && req->bundlei < coin->chain->bundlesize && req->bp->recvlens[req->bundlei] != 0 )
{
//printf("%p[%d] %d\n",req->bp,req->bp!=0?req->bp->bundleheight:-1,req->bundlei);
myfree(req,sizeof(*req));
}
else if ( req->bp == 0 || (req->bp != 0 && req->bundlei >= 0) )//&& GETBIT(req->bp->recv,req->bundlei) == 0) )
{
init_hexbytes_noT(hexstr,hash2.bytes,sizeof(hash2));
if ( (datalen= iguana_getdata(coin,serialized,MSG_BLOCK,hexstr)) > 0 )
{
if ( 0 && queue_size(&coin->priorityQ) > 0 )
printf("%s %s BLOCK.%d:%d bit.%d Q.(%d %d)\n",addr->ipaddr,hexstr,req->bp!=0?req->bp->hdrsi:-1,req->bundlei,req->bp!=0?GETBIT(req->bp->recv,req->bundlei):-1,queue_size(&coin->priorityQ),queue_size(&coin->blocksQ));
iguana_send(coin,addr,serialized,datalen);
coin->numreqsent++;
addr->pendblocks++;
addr->pendtime = (uint32_t)time(NULL);
if ( (bp= req->bp) != 0 && req->bundlei >= 0 && req->bundlei < bp->n )
{
if ( bp->requests[req->bundlei] < 100 )
bp->requests[req->bundlei]++;
bp->issued[req->bundlei] = (uint32_t)time(NULL);
}
flag++;
myfree(req,sizeof(*req));
return(flag);
} else printf("error constructing request %s.%d\n",hexstr,height);
}
}
return(flag);
}
int32_t iguana_processrecv(struct iguana_info *coin) // single threaded
{
int32_t newhwm = 0,h,lflag,i,flag = 0; bits256 hash2; struct iguana_block *next,*block,*prev;
//printf("process bundlesQ\n");
flag += iguana_processbundlesQ(coin,&newhwm);
flag += iguana_reqhdrs(coin);
lflag = 1;
while ( 1 )
{
h = coin->blocks.hwmchain.height;
if ( (next= iguana_blockfind(coin,iguana_blockhash(coin,coin->blocks.hwmchain.height+1))) != 0 )
{
double lag;
_iguana_chainlink(coin,next);
lag = milliseconds() - coin->backstopmillis;
if ( (coin->blocks.hwmchain.height+1 < coin->longestchain && (lag > coin->avetime && coin->backstop != coin->blocks.hwmchain.height+1)) || lag > 10*coin->avetime )
{
coin->backstop = coin->blocks.hwmchain.height+1;
coin->backstophash2 = next->hash2;
coin->backstopmillis = milliseconds();
iguana_blockQ(coin,0,coin->blocks.hwmchain.height+1,next->hash2,0);
//char str[65]; printf("MAINCHAIN.%d %s avetime.%.3f lag %.3f\n",coin->blocks.hwmchain.height+1,bits256_str(str,next->hash2),coin->avetime,lag);
}
}
iguana_chainextend(coin,iguana_blockfind(coin,coin->blocks.hwmchain.hash2));
if ( h == coin->blocks.hwmchain.height )
break;
}
if ( strcmp(coin->symbol,"BTC") != 0 )
for (i=coin->blocks.hwmchain.height+1; i<coin->longestchain; i++)
{
if ( (block= iguana_blockfind(coin,iguana_blockhash(coin,i))) != 0 && block->ipbits == 0 )
{
if ( (prev= iguana_blockfind(coin,block->prev_block)) != 0 && prev->mainchain != 0 )
{
printf("adjacent %d\n",i);
iguana_blockQ(coin,0,-1,block->hash2,1);
}
}
}
while ( 0)//lflag != 0 )
{
lflag = 0;
h = coin->blocks.hwmchain.height / coin->chain->bundlesize;
if ( (next= iguana_blockfind(coin,iguana_blockhash(coin,coin->blocks.hwmchain.height+1))) != 0 )
/*{
if ( (block= iguana_blockfind(coin,coin->blocks.hwmchain.hash2)) != 0 )
next = block->hh.next;//, block->mainchain = 1;
}
if ( next != 0 )*/
{
if ( _iguana_chainlink(coin,next) == 0 )
//printf("have next\n");
/*if ( 0 && memcmp(next->prev_block.bytes,coin->blocks.hwmchain.hash2.bytes,sizeof(bits256)) == 0 )
{
if ( _iguana_chainlink(coin,next) != 0 )
lflag++;
else printf("chainlink error for %d\n",coin->blocks.hwmchain.height+1);//, getchar();
}
else if ( 0 )*/
{
double threshold,lag = milliseconds() - coin->backstopmillis;
threshold = (10 + coin->longestchain - coin->blocksrecv);
if ( threshold < 1 )
threshold = 1.;
threshold = 1000;//coin->avetime * sqrt(threshold) * .000777;
if ( coin->blocks.hwmchain.height+1 < coin->longestchain && (coin->backstop != coin->blocks.hwmchain.height+1 || lag > threshold) )
{
coin->backstop = coin->blocks.hwmchain.height+1;
coin->backstophash2 = next->hash2;
coin->backstopmillis = milliseconds();
iguana_blockQ(coin,0,coin->blocks.hwmchain.height+1,next->hash2,0);
// clear recvlens
//if ( (rand() % 100) == 0 )
{
char str[65]; printf("MAINCHAIN.%d %s avetime %.3f lag %.3f\n",coin->blocks.hwmchain.height+1,bits256_str(str,next->hash2),coin->avetime,lag);
}
}
else if ( 0 && bits256_nonz(next->prev_block) > 0 )
printf("next prev cmp error nonz.%d\n",bits256_nonz(next->prev_block));
}
}
else if ( 0 )
{
for (i=2; i<coin->chain->bundlesize; i++)
{
hash2 = iguana_blockhash(coin,coin->blocks.hwmchain.height+i);
if ( bits256_nonz(hash2) > 0 )
{
if ( (block= iguana_blockfind(coin,hash2)) != 0 && bits256_nonz(block->prev_block) > 0 )
{
printf("REVBACKSTOP.%d\n",coin->blocks.hwmchain.height+i-1);
iguana_blockQ(coin,0,-1,block->prev_block,1);
break;
}
}
}
}
if ( h != coin->blocks.hwmchain.height / coin->chain->bundlesize )
iguana_savehdrs(coin);
}
return(flag);
}