]> Git Repo - VerusCoin.git/blob - src/komodo.h
test
[VerusCoin.git] / src / komodo.h
1 /******************************************************************************
2  * Copyright © 2014-2016 The SuperNET Developers.                             *
3  *                                                                            *
4  * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at                  *
5  * the top-level directory of this distribution for the individual copyright  *
6  * holder information and the developer policies on copyright and licensing.  *
7  *                                                                            *
8  * Unless otherwise agreed in a custom licensing agreement, no part of the    *
9  * SuperNET software, including this file may be copied, modified, propagated *
10  * or distributed except according to the terms contained in the LICENSE file *
11  *                                                                            *
12  * Removal or modification of this copyright notice is prohibited.            *
13  *                                                                            *
14  ******************************************************************************/
15
16 #ifndef H_KOMODO_H
17 #define H_KOMODO_H
18
19 // Todo:
20 // 0. optimize assetchains
21 // 1. error check fiat redeem amounts
22 // 2. net balance limiter
23 // 3. verify: reorgs
24
25 // non komodod (non-hardfork) todo:
26 // a. automate notarization fee payouts
27 // b. automated distribution of test REVS snapshot
28
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <pthread.h>
32 #include <ctype.h>
33 #include "uthash.h"
34 #include "utlist.h"
35
36 #define GENESIS_NBITS 0x1f00ffff
37 #define KOMODO_MINRATIFY 7
38 #define KOMODO_MAXBLOCKS 5000000
39
40 #include "komodo_structs.h"
41
42
43 struct komodo_state
44 {
45     uint256 NOTARIZED_HASH,NOTARIZED_DESTTXID;
46     int32_t CURRENT_HEIGHT,NOTARIZED_HEIGHT;
47     // gateway state
48 } KOMODO_STATES[33];
49
50 #include "komodo_globals.h"
51 #include "komodo_utils.h"
52 struct komodo_state *komodo_stateptr(char *symbol,char *dest)
53 {
54     int32_t baseid; struct komodo_state *sp;
55     if ( ASSETCHAINS_SYMBOL[0] == 0 )
56     {
57         strcpy(symbol,"KMD");
58         strcpy(dest,"BTC");
59         sp = &KOMODO_STATES[0];
60     }
61     else
62     {
63         strcpy(symbol,ASSETCHAINS_SYMBOL);
64         strcpy(dest,"KMD");
65         if ( (baseid= komodo_baseid(ASSETCHAINS_SYMBOL)) >= 0 )
66             sp = &KOMODO_STATES[baseid];
67         else
68         {
69             fprintf(stderr,"komodo_stateupdate.(%s) not supported\n",ASSETCHAINS_SYMBOL);
70             return(0);
71         }
72     }
73     return(sp);
74 }
75
76 void komodo_setkmdheight(int32_t kmdheight)
77 {
78     if ( kmdheight > KMDHEIGHT )
79         KMDHEIGHT = kmdheight;
80 }
81
82 #include "cJSON.c"
83 #include "komodo_bitcoind.h"
84 #include "komodo_interest.h"
85 #include "komodo_pax.h"
86 #include "komodo_notary.h"
87 #include "komodo_gateway.h"
88 #include "komodo_events.h"
89
90
91 void komodo_currentheight_set(int32_t height)
92 {
93     char symbol[16],dest[16]; struct komodo_state *sp;
94     if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
95         sp->CURRENT_HEIGHT = height;
96 }
97
98 int32_t komodo_currentheight()
99 {
100     char symbol[16],dest[16]; struct komodo_state *sp;
101     if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
102         return(sp->CURRENT_HEIGHT);
103     else return(0);
104 }
105
106 int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest)
107 {
108     static int32_t errs;
109     int32_t func,ht,notarized_height,num,matched=0; uint256 notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33];
110     if ( (func= fgetc(fp)) != EOF )
111     {
112         if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 )
113             matched = 1;
114         else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0);
115         if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) )
116             errs++;
117         //printf("fpos.%ld func.(%d %c) ht.%d ",ftell(fp),func,func,ht);
118         if ( func == 'P' )
119         {
120             if ( (num= fgetc(fp)) < 64 )
121             {
122                 if ( fread(pubkeys,33,num,fp) != num )
123                     errs++;
124                 else
125                 {
126                     printf("updated %d pubkeys at ht.%d\n",num,ht);
127                     if ( matched != 0 )
128                         komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys);
129                 }
130             } else printf("illegal num.%d\n",num);
131         }
132         else if ( func == 'N' )
133         {
134             if ( fread(&notarized_height,1,sizeof(notarized_height),fp) != sizeof(notarized_height) )
135                 errs++;
136             if ( fread(&notarized_hash,1,sizeof(notarized_hash),fp) != sizeof(notarized_hash) )
137                 errs++;
138             if ( fread(&notarized_desttxid,1,sizeof(notarized_desttxid),fp) != sizeof(notarized_desttxid) )
139                 errs++;
140             printf("load NOTARIZED %d %s\n",notarized_height,notarized_hash.ToString().c_str());
141             if ( matched != 0 )
142                 komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height);
143         }
144         else if ( func == 'U' )
145         {
146             uint8_t n,nid; uint256 hash; uint64_t mask;
147             n = fgetc(fp);
148             nid = fgetc(fp);
149             //printf("U %d %d\n",n,nid);
150             if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) )
151                 errs++;
152             if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) )
153                 errs++;
154             if ( matched != 0 )
155                 komodo_eventadd_utxo(sp,symbol,ht,nid,hash,mask,n);
156         }
157         else if ( func == 'K' )
158         {
159             int32_t kheight;
160             if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
161                 errs++;
162             if ( matched != 0 )
163                 komodo_eventadd_kmdheight(sp,symbol,ht,kheight);
164         }
165         else if ( func == 'R' )
166         {
167             uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[10000];
168             if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) )
169                 errs++;
170             if ( fread(&v,1,sizeof(v),fp) != sizeof(v) )
171                 errs++;
172             if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) )
173                 errs++;
174             if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) )
175                 errs++;
176             if ( olen < sizeof(opret) )
177             {
178                 if ( fread(opret,1,olen,fp) != olen )
179                     errs++;
180                 if ( matched != 0 )
181                     komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen);
182             } else printf("illegal olen.%u\n",olen);
183         }
184         else if ( func == 'D' )
185         {
186             printf("unexpected function D[%d]\n",ht);
187         }
188         else if ( func == 'V' )
189         {
190             int32_t numpvals; uint32_t pvals[128];
191             numpvals = fgetc(fp);
192             if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals )
193             {
194                 if ( matched != 0 )
195                     komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals);
196                 //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
197             } else printf("error loading pvals[%d]\n",numpvals);
198         }
199         else printf("illegal func.(%d %c)\n",func,func);
200         return(func);
201     } else return(-1);
202 }
203                     
204 void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t KMDheight,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout)
205 {
206     static FILE *fp; static int32_t errs;
207     struct komodo_state *sp; char fname[512],fname2[512],symbol[16],dest[16]; int32_t ht,func; uint8_t num,pubkeys[64][33];
208     if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
209         return;
210     if ( fp == 0 )
211     {
212 #ifdef WIN32
213         sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"komodostate");
214         sprintf(fname2,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"minerids");
215 #else
216         sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),(char *)"komodostate");
217         sprintf(fname2,"%s/%s",GetDataDir(false).string().c_str(),(char *)"minerids");
218 #endif
219         memset(Minerids,0xfe,sizeof(Minerids));
220         if ( (Minerfp= fopen(fname2,"rb+")) == 0 )
221         {
222             if ( (Minerfp= fopen(fname2,"wb")) != 0 )
223             {
224                 fwrite(Minerids,1,sizeof(Minerids),Minerfp);
225                 fclose(Minerfp);
226             }
227             Minerfp = fopen(fname2,"rb+");
228         }
229         if ( Minerfp != 0 && fread(Minerids,1,sizeof(Minerids),Minerfp) != sizeof(Minerids) )
230             printf("read error Minerids\n");
231         if ( (fp= fopen(fname,"rb+")) != 0 )
232         {
233             while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
234                 ;
235         } else fp = fopen(fname,"wb+");
236         printf("fname.(%s) fpos.%ld\n",fname,ftell(fp));
237         KOMODO_INITDONE = (uint32_t)time(NULL);
238     }
239     if ( height <= 0 )
240     {
241         //printf("early return: stateupdate height.%d\n",height);
242         return;
243     }
244     if ( fp != 0 ) // write out funcid, height, other fields, call side effect function
245     {
246         //printf("fpos.%ld ",ftell(fp));
247         if ( KMDheight > 0 )
248         {
249             fputc('K',fp);
250             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
251                 errs++;
252             if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
253                 errs++;
254             komodo_eventadd_kmdheight(sp,symbol,height,KMDheight);
255         }
256         else if ( opretbuf != 0 && opretlen > 0 )
257         {
258             uint16_t olen = opretlen;
259             fputc('R',fp);
260             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
261                 errs++;
262             if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
263                 errs++;
264             if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) )
265                 errs++;
266             if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) )
267                 errs++;
268             if ( fwrite(&olen,1,sizeof(olen),fp) != olen )
269                 errs++;
270             if ( fwrite(opretbuf,1,olen,fp) != olen )
271                 errs++;
272             //printf("ht.%d R opret[%d]\n",height,olen);
273             //komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout);
274             komodo_eventadd_opreturn(sp,symbol,height,txhash,opretvalue,vout,opretbuf,olen);
275         }
276         else if ( notarypubs != 0 && numnotaries > 0 )
277         {
278             //printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs);
279             fputc('P',fp);
280             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
281                 errs++;
282             fputc(numnotaries,fp);
283             if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries )
284                 errs++;
285             komodo_eventadd_pubkeys(sp,symbol,height,numnotaries,notarypubs);
286         }
287         else if ( voutmask != 0 && numvouts > 0 )
288         {
289             //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash));
290             fputc('U',fp);
291             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
292                 errs++;
293             fputc(numvouts,fp);
294             fputc(notaryid,fp);
295             if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) )
296                 errs++;
297             if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
298                 errs++;
299             komodo_eventadd_utxo(sp,symbol,height,notaryid,txhash,voutmask,numvouts);
300             //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts);
301         }
302 //#ifdef KOMODO_PAX
303         else if ( pvals != 0 && numpvals > 0 )
304         {
305             int32_t i,nonz = 0;
306             for (i=0; i<32; i++)
307                 if ( pvals[i] != 0 )
308                     nonz++;
309             if ( nonz >= 32 )
310             {
311                 fputc('V',fp);
312                 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
313                     errs++;
314                 fputc(numpvals,fp);
315                 if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals )
316                     errs++;
317                 komodo_eventadd_pricefeed(sp,symbol,height,pvals,numpvals);
318                 //printf("ht.%d V numpvals[%d]\n",height,numpvals);
319             }
320             //printf("save pvals height.%d numpvals.%d\n",height,numpvals);
321         }
322 //#endif
323         else if ( height != 0 )
324         {
325             //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs);
326             fputc('N',fp);
327             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
328                 errs++;
329             if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) )
330                 errs++;
331             if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) )
332                 errs++;
333             if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) )
334                 errs++;
335             komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT);
336             //komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID);
337         }
338         fflush(fp);
339     }
340 }
341
342 int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scriptbuf,int32_t scriptlen,int32_t height,uint256 txhash,int32_t i,int32_t j,uint64_t *voutmaskp,int32_t *specialtxp,int32_t *notarizedheightp,uint64_t value)
343 {
344     static uint256 zero; int32_t opretlen,nid,k,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33]; struct komodo_state *sp; char symbol[16],dest[16];
345     if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
346         return(-1);
347     if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
348     {
349         decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR);
350         /*for (k=0; k<33; k++)
351             printf("%02x",crypto777[k]);
352         printf(" crypto777 ");
353         for (k=0; k<scriptlen; k++)
354             printf("%02x",scriptbuf[k]);
355         printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/
356         if ( memcmp(crypto777,scriptbuf+1,33) == 0 )
357         {
358             *specialtxp = 1;
359             //printf(">>>>>>>> ");
360         }
361         else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 )
362         {
363             //printf("found notary.k%d\n",k);
364             if ( notaryid < 64 )
365             {
366                 if ( notaryid < 0 )
367                 {
368                     notaryid = nid;
369                     *voutmaskp |= (1LL << j);
370                 }
371                 else if ( notaryid != nid )
372                 {
373                     //for (i=0; i<33; i++)
374                     //    printf("%02x",scriptbuf[i+1]);
375                     //printf(" %s mismatch notaryid.%d k.%d\n",ASSETCHAINS_SYMBOL,notaryid,nid);
376                     notaryid = 64;
377                     *voutmaskp = 0;
378                 }
379                 else *voutmaskp |= (1LL << j);
380             }
381         }
382     }
383     if ( scriptbuf[len++] == 0x6a )
384     {
385         if ( (opretlen= scriptbuf[len++]) == 0x4c )
386             opretlen = scriptbuf[len++];
387         else if ( opretlen == 0x4d )
388         {
389             opretlen = scriptbuf[len++];
390             opretlen = (opretlen << 8) + scriptbuf[len++];
391         }
392         if ( j == 1 && opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
393         {
394             len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
395             len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp);
396             len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid);
397             if ( *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height )
398             {
399                 printf("ht.%d NOTARIZED.%d %s.%s %sTXID.%s (%s)\n",height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),ASSETCHAINS_SYMBOL[0]==0?"BTC":"KMD",desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
400                 sp->NOTARIZED_HEIGHT = *notarizedheightp;
401                 sp->NOTARIZED_HASH = kmdtxid;
402                 sp->NOTARIZED_DESTTXID = desttxid;
403                 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0);
404             } else printf("reject ht.%d NOTARIZED.%d %s.%s DESTTXID.%s (%s)\n",height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
405         }
406         else if ( i == 0 && j == 1 && opretlen == 149 )
407             komodo_paxpricefeed(height,&scriptbuf[len],opretlen);
408         else
409         {
410             //int32_t k; for (k=0; k<scriptlen; k++)
411             //    printf("%02x",scriptbuf[k]);
412             //printf(" <- script ht.%d i.%d j.%d value %.8f\n",height,i,j,dstr(value));
413             if ( opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
414             {
415                 iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
416                 for (k=0; k<32; k++)
417                     if ( scriptbuf[len+k] != 0 )
418                         break;
419                 if ( k == 32 )
420                 {
421                     *isratificationp = 1;
422                     printf("ISRATIFICATION (%s)\n",(char *)&scriptbuf[len+32*2+4]);
423                 }
424             }
425             if ( *isratificationp == 0 )
426                 komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,value,&scriptbuf[len],opretlen,j);
427         }
428     }
429     return(notaryid);
430 }
431
432 int32_t komodo_isratify(int32_t isspecial,int32_t numvalid)
433 {
434     if ( isspecial != 0 && numvalid >= KOMODO_MINRATIFY )
435         return(1);
436     else return(0);
437 }
438
439 // Special tx have vout[0] -> CRYPTO777
440 // with more than KOMODO_MINRATIFY pay2pubkey outputs -> ratify
441 // if all outputs to notary -> notary utxo
442 // if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed
443 // OP_RETURN: 'D' -> deposit, 'W' -> withdraw
444 void komodo_currentheight_set(int32_t height);
445
446 void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
447 {
448     static int32_t hwmheight;
449     uint64_t signedmask,voutmask;
450     uint8_t scriptbuf[4096],pubkeys[64][33]; uint256 kmdtxid,btctxid,txhash;
451     int32_t i,j,k,numnotaries,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
452     komodo_init(pindex->nHeight);
453     numnotaries = komodo_notaries(pubkeys,pindex->nHeight);
454     if ( pindex->nHeight > hwmheight )
455         hwmheight = pindex->nHeight;
456     else
457     {
458         printf("hwmheight.%d vs pindex->nHeight.%d reorg.%d\n",hwmheight,pindex->nHeight,hwmheight-pindex->nHeight);
459         // reset komodostate
460     }
461     komodo_currentheight_set(chainActive.Tip()->nHeight);
462     if ( komodo_is_issuer() != 0 )
463     {
464         while ( KOMODO_REALTIME == 0 || time(NULL) <= KOMODO_REALTIME )
465         {
466             fprintf(stderr,"komodo_connect.(%s) waiting for realtime RT.%u now.%u\n",ASSETCHAINS_SYMBOL,KOMODO_REALTIME,(uint32_t)time(NULL));
467             sleep(30);
468         }
469     }
470     KOMODO_REALTIME = KOMODO_INITDONE = (uint32_t)time(NULL);
471     if ( pindex != 0 )
472     {
473         height = pindex->nHeight;
474         txn_count = block.vtx.size();
475         if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
476             printf("%s ht.%d connect txn_count.%d\n",ASSETCHAINS_SYMBOL,height,txn_count);
477         for (i=0; i<txn_count; i++)
478         {
479             txhash = block.vtx[i].GetHash();
480             numvouts = block.vtx[i].vout.size();
481             notaryid = -1;
482             voutmask = specialtx = notarizedheight = isratification = 0;
483             for (j=0; j<numvouts; j++)
484             {
485                 len = block.vtx[i].vout[j].scriptPubKey.size();
486                 if ( len <= sizeof(scriptbuf) )
487                 {
488 #ifdef KOMODO_ZCASH
489                     memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
490 #else
491                     memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
492 #endif
493                     notaryid = komodo_voutupdate(&isratification,notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,&notarizedheight,(uint64_t)block.vtx[i].vout[j].nValue);
494                     if ( i == 0 && j == 0 && komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 )
495                     {
496                         if ( height < sizeof(Minerids)/sizeof(*Minerids) )
497                         {
498                             if ( (Minerids[height]= nid) >= -1 )
499                             {
500                                 if ( Minerfp != 0 )
501                                 {
502                                     fseek(Minerfp,height,SEEK_SET);
503                                     fputc(Minerids[height],Minerfp);
504                                     fflush(Minerfp);
505                                 }
506                             }
507                         }
508                     }
509                     if ( 0 && i > 0 )
510                     {
511                         for (k=0; k<len; k++)
512                             printf("%02x",scriptbuf[k]);
513                         printf(" <- notaryid.%d ht.%d i.%d j.%d numvouts.%d numvins.%d voutmask.%llx txid.(%s)\n",notaryid,height,i,j,numvouts,numvins,(long long)voutmask,txhash.ToString().c_str());
514                     }
515                 }
516             }
517             if ( i != 0 && notaryid >= 0 && notaryid < 64 && voutmask != 0 )
518             {
519                 komodo_stateupdate(height,0,0,notaryid,txhash,voutmask,numvouts,0,0,0,0,0,0,0);
520                 //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts);
521             }
522             signedmask = 0;
523             numvins = block.vtx[i].vin.size();
524             for (j=0; j<numvins; j++)
525             {
526                 if ( (k= komodo_nutxofind(height,block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) >= 0 )
527                     signedmask |= (1LL << k);
528                 else if ( 0 && signedmask != 0 )
529                     printf("signedmask.%llx but ht.%d i.%d j.%d not found (%s %d)\n",(long long)signedmask,height,i,j,block.vtx[i].vin[j].prevout.hash.ToString().c_str(),block.vtx[i].vin[j].prevout.n);
530             }
531             if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) )
532             {
533                 printf("NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",(long long)signedmask,numvins,height,i,notarizedheight,specialtx);
534                 printf("ht.%d specialtx.%d isratification.%d numvouts.%d signed.%llx numnotaries.%d\n",height,specialtx,isratification,numvouts,(long long)signedmask,numnotaries);
535                 if ( specialtx != 0 && isratification != 0 && numvouts > 2 )
536                 {
537                     numvalid = 0;
538                     memset(pubkeys,0,sizeof(pubkeys));
539                     for (j=1; j<numvouts-1; j++)
540                     {
541                         len = block.vtx[i].vout[j].scriptPubKey.size();
542                         if ( len <= sizeof(scriptbuf) )
543                         {
544 #ifdef KOMODO_ZCASH
545                             memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
546 #else
547                             memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
548 #endif
549                             if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
550                             {
551                                 memcpy(pubkeys[numvalid++],scriptbuf+1,33);
552                                 for (k=0; k<33; k++)
553                                     printf("%02x",scriptbuf[k+1]);
554                                 printf(" <- new notary.[%d]\n",j-1);
555                             }
556                         }
557                     }
558                     if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries>>1) )
559                     {
560                         memset(&txhash,0,sizeof(txhash));
561                         komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0);
562                         printf("RATIFIED! >>>>>>>>>> new notaries.%d newheight.%d from height.%d\n",numvalid,(((height+KOMODO_ELECTION_GAP/2)/KOMODO_ELECTION_GAP)+1)*KOMODO_ELECTION_GAP,height);
563                     }
564                 }
565             }
566         }
567     } else printf("komodo_connectblock: unexpected null pindex\n");
568     KOMODO_INITDONE = (uint32_t)time(NULL);
569 }
570
571
572 #endif
This page took 0.055416 seconds and 4 git commands to generate.