]> 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 int32_t gettxout_scriptPubKey(uint8_t *scriptPubkey,int32_t maxsize,uint256 txid,int32_t n);
37 void komodo_event_rewind(struct komodo_state *sp,char *symbol,int32_t height);
38 void komodo_connectblock(CBlockIndex *pindex,CBlock& block);
39
40 #include "komodo_structs.h"
41 #include "komodo_globals.h"
42 #include "komodo_utils.h"
43
44 #include "cJSON.c"
45 #include "komodo_bitcoind.h"
46 #include "komodo_interest.h"
47 #include "komodo_pax.h"
48 #include "komodo_notary.h"
49
50 int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest);
51 #include "komodo_gateway.h"
52 #include "komodo_events.h"
53
54 void komodo_currentheight_set(int32_t height)
55 {
56     char symbol[16],dest[16]; struct komodo_state *sp;
57     if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
58         sp->CURRENT_HEIGHT = height;
59 }
60
61 int32_t komodo_currentheight()
62 {
63     char symbol[16],dest[16]; struct komodo_state *sp;
64     if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
65         return(sp->CURRENT_HEIGHT);
66     else return(0);
67 }
68
69 int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest)
70 {
71     static int32_t errs;
72     int32_t func,ht,notarized_height,num,matched=0; uint256 notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33];
73     if ( (func= fgetc(fp)) != EOF )
74     {
75         if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 )
76             matched = 1;
77         else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0);
78         if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) )
79             errs++;
80         //printf("fpos.%ld func.(%d %c) ht.%d ",ftell(fp),func,func,ht);
81         if ( func == 'P' )
82         {
83             if ( (num= fgetc(fp)) <= 64 )
84             {
85                 if ( fread(pubkeys,33,num,fp) != num )
86                     errs++;
87                 else
88                 {
89                     printf("updated %d pubkeys at %s ht.%d\n",num,symbol,ht);
90                     if ( (KOMODO_EXTERNAL_NOTARIES != 0 && matched != 0) || (strcmp(symbol,"KMD") == 0 && KOMODO_EXTERNAL_NOTARIES == 0) )
91                         komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys);
92                 }
93             } else printf("illegal num.%d\n",num);
94         }
95         else if ( func == 'N' )
96         {
97             if ( fread(&notarized_height,1,sizeof(notarized_height),fp) != sizeof(notarized_height) )
98                 errs++;
99             if ( fread(&notarized_hash,1,sizeof(notarized_hash),fp) != sizeof(notarized_hash) )
100                 errs++;
101             if ( fread(&notarized_desttxid,1,sizeof(notarized_desttxid),fp) != sizeof(notarized_desttxid) )
102                 errs++;
103             //printf("%s load[%s] NOTARIZED %d %s\n",ASSETCHAINS_SYMBOL,symbol,notarized_height,notarized_hash.ToString().c_str());
104             //if ( matched != 0 ) global independent states -> inside *sp
105             komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height);
106         }
107         else if ( func == 'U' ) // deprecated
108         {
109             uint8_t n,nid; uint256 hash; uint64_t mask;
110             n = fgetc(fp);
111             nid = fgetc(fp);
112             //printf("U %d %d\n",n,nid);
113             if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) )
114                 errs++;
115             if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) )
116                 errs++;
117             //if ( matched != 0 )
118             //    komodo_eventadd_utxo(sp,symbol,ht,nid,hash,mask,n);
119         }
120         else if ( func == 'K' )
121         {
122             int32_t kheight;
123             if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
124                 errs++;
125             //if ( matched != 0 ) global independent states -> inside *sp
126             //printf("%s.%d load[%s] ht.%d\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight);
127             komodo_eventadd_kmdheight(sp,symbol,ht,kheight,0);
128         }
129         else if ( func == 'T' )
130         {
131             int32_t kheight,ktimestamp;
132             if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
133                 errs++;
134             if ( fread(&ktimestamp,1,sizeof(ktimestamp),fp) != sizeof(ktimestamp) )
135                 errs++;
136             //if ( matched != 0 ) global independent states -> inside *sp
137             //printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp);
138             komodo_eventadd_kmdheight(sp,symbol,ht,kheight,ktimestamp);
139         }
140         else if ( func == 'R' )
141         {
142             uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[10000];
143             if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) )
144                 errs++;
145             if ( fread(&v,1,sizeof(v),fp) != sizeof(v) )
146                 errs++;
147             if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) )
148                 errs++;
149             if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) )
150                 errs++;
151             if ( olen < sizeof(opret) )
152             {
153                 if ( fread(opret,1,olen,fp) != olen )
154                     errs++;
155                 //if ( matched != 0 ) global shared state -> global PAX
156                 int32_t i;  for (i=0; i<olen; i++)
157                     printf("%02x",opret[i]);
158                 printf(" %s load[%s] opret[%c] len.%d %.8f\n",ASSETCHAINS_SYMBOL,symbol,opret[0],olen,(double)ovalue/COIN);
159                 komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen);
160             } else printf("illegal olen.%u\n",olen);
161         }
162         else if ( func == 'D' )
163         {
164             printf("unexpected function D[%d]\n",ht);
165         }
166         else if ( func == 'V' )
167         {
168             int32_t numpvals; uint32_t pvals[128];
169             numpvals = fgetc(fp);
170             if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals )
171             {
172                 //if ( matched != 0 ) global shared state -> global PVALS
173                 //printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht);
174                 komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals);
175                 //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
176             } else printf("error loading pvals[%d]\n",numpvals);
177         }
178         else printf("illegal func.(%d %c)\n",func,func);
179         return(func);
180     } else return(-1);
181 }
182                     
183 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,uint32_t KMDtimestamp,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout)
184 {
185     static FILE *fp; static int32_t errs;
186     struct komodo_state *sp; char fname[512],symbol[16],dest[16]; int32_t ht,func; uint8_t num,pubkeys[64][33];
187     if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
188         return;
189     if ( fp == 0 )
190     {
191         komodo_statefname(fname,ASSETCHAINS_SYMBOL);
192         if ( (fp= fopen(fname,"rb+")) != 0 )
193         {
194             while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
195                 ;
196         } else fp = fopen(fname,"wb+");
197         printf("fname.(%s) fpos.%ld\n",fname,ftell(fp));
198         KOMODO_INITDONE = (uint32_t)time(NULL);
199     }
200     if ( height <= 0 )
201     {
202         //printf("early return: stateupdate height.%d\n",height);
203         return;
204     }
205     if ( fp != 0 ) // write out funcid, height, other fields, call side effect function
206     {
207         //printf("fpos.%ld ",ftell(fp));
208         if ( KMDheight != 0 )
209         {
210             if ( KMDtimestamp != 0 )
211             {
212                 fputc('T',fp);
213                 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
214                     errs++;
215                 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
216                     errs++;
217                 if ( fwrite(&KMDtimestamp,1,sizeof(KMDtimestamp),fp) != sizeof(KMDtimestamp) )
218                     errs++;
219             }
220             else
221             {
222                 fputc('K',fp);
223                 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
224                     errs++;
225                 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
226                     errs++;
227             }
228             komodo_eventadd_kmdheight(sp,symbol,height,KMDheight,KMDtimestamp);
229         }
230         else if ( opretbuf != 0 && opretlen > 0 )
231         {
232             uint16_t olen = opretlen;
233             fputc('R',fp);
234             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
235                 errs++;
236             if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
237                 errs++;
238             if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) )
239                 errs++;
240             if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) )
241                 errs++;
242             if ( fwrite(&olen,1,sizeof(olen),fp) != olen )
243                 errs++;
244             if ( fwrite(opretbuf,1,olen,fp) != olen )
245                 errs++;
246             //printf("ht.%d R opret[%d]\n",height,olen);
247             //komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout);
248             komodo_eventadd_opreturn(sp,symbol,height,txhash,opretvalue,vout,opretbuf,olen);
249         }
250         else if ( notarypubs != 0 && numnotaries > 0 )
251         {
252             printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs);
253             fputc('P',fp);
254             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
255                 errs++;
256             fputc(numnotaries,fp);
257             if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries )
258                 errs++;
259             komodo_eventadd_pubkeys(sp,symbol,height,numnotaries,notarypubs);
260         }
261         else if ( voutmask != 0 && numvouts > 0 )
262         {
263             //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash));
264             fputc('U',fp);
265             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
266                 errs++;
267             fputc(numvouts,fp);
268             fputc(notaryid,fp);
269             if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) )
270                 errs++;
271             if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
272                 errs++;
273             //komodo_eventadd_utxo(sp,symbol,height,notaryid,txhash,voutmask,numvouts);
274         }
275 //#ifdef KOMODO_PAX
276         else if ( pvals != 0 && numpvals > 0 )
277         {
278             int32_t i,nonz = 0;
279             for (i=0; i<32; i++)
280                 if ( pvals[i] != 0 )
281                     nonz++;
282             if ( nonz >= 32 )
283             {
284                 fputc('V',fp);
285                 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
286                     errs++;
287                 fputc(numpvals,fp);
288                 if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals )
289                     errs++;
290                 komodo_eventadd_pricefeed(sp,symbol,height,pvals,numpvals);
291                 //printf("ht.%d V numpvals[%d]\n",height,numpvals);
292             }
293             //printf("save pvals height.%d numpvals.%d\n",height,numpvals);
294         }
295 //#endif
296         else if ( height != 0 )
297         {
298             //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs);
299             fputc('N',fp);
300             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
301                 errs++;
302             if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) )
303                 errs++;
304             if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) )
305                 errs++;
306             if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) )
307                 errs++;
308             komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT);
309             //komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID);
310         }
311         fflush(fp);
312     }
313 }
314
315 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)
316 {
317     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];
318     if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
319         return(-1);
320     if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
321     {
322         decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR);
323         /*for (k=0; k<33; k++)
324             printf("%02x",crypto777[k]);
325         printf(" crypto777 ");
326         for (k=0; k<scriptlen; k++)
327             printf("%02x",scriptbuf[k]);
328         printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/
329         if ( memcmp(crypto777,scriptbuf+1,33) == 0 )
330         {
331             *specialtxp = 1;
332             //printf(">>>>>>>> ");
333         }
334         else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 )
335         {
336             //printf("found notary.k%d\n",k);
337             if ( notaryid < 64 )
338             {
339                 if ( notaryid < 0 )
340                 {
341                     notaryid = nid;
342                     *voutmaskp |= (1LL << j);
343                 }
344                 else if ( notaryid != nid )
345                 {
346                     //for (i=0; i<33; i++)
347                     //    printf("%02x",scriptbuf[i+1]);
348                     //printf(" %s mismatch notaryid.%d k.%d\n",ASSETCHAINS_SYMBOL,notaryid,nid);
349                     notaryid = 64;
350                     *voutmaskp = 0;
351                 }
352                 else *voutmaskp |= (1LL << j);
353             }
354         }
355     }
356     if ( scriptbuf[len++] == 0x6a )
357     {
358         if ( (opretlen= scriptbuf[len++]) == 0x4c )
359             opretlen = scriptbuf[len++];
360         else if ( opretlen == 0x4d )
361         {
362             opretlen = scriptbuf[len++];
363             opretlen = (opretlen << 8) + scriptbuf[len++];
364         }
365         if ( j == 1 && opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
366         {
367             len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
368             len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp);
369             len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid);
370             if ( *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height )
371             {
372                 printf("%s ht.%d NOTARIZED.%d %s.%s %sTXID.%s (%s)\n",ASSETCHAINS_SYMBOL,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]);
373                 sp->NOTARIZED_HEIGHT = *notarizedheightp;
374                 sp->NOTARIZED_HASH = kmdtxid;
375                 sp->NOTARIZED_DESTTXID = desttxid;
376                 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0,0);
377                 // extract X opreturns here
378             } 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]);
379         }
380         else if ( i == 0 && j == 1 && opretlen == 149 )
381             komodo_paxpricefeed(height,&scriptbuf[len],opretlen);
382         else
383         {
384             int32_t k; for (k=0; k<scriptlen; k++)
385                 printf("%02x",scriptbuf[k]);
386             printf(" <- script ht.%d i.%d j.%d value %.8f %s\n",height,i,j,dstr(value),ASSETCHAINS_SYMBOL);
387             if ( opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
388             {
389                 for (k=0; k<32; k++)
390                     if ( scriptbuf[len+k] != 0 )
391                         break;
392                 if ( k == 32 )
393                 {
394                     *isratificationp = 1;
395                     printf("ISRATIFICATION (%s)\n",(char *)&scriptbuf[len+32*2+4]);
396                 }
397             }
398             if ( *isratificationp == 0 )
399                 komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen,j);
400         }
401     }
402     return(notaryid);
403 }
404
405 int32_t komodo_isratify(int32_t isspecial,int32_t numvalid)
406 {
407     if ( isspecial != 0 && numvalid >= KOMODO_MINRATIFY )
408         return(1);
409     else return(0);
410 }
411
412 // Special tx have vout[0] -> CRYPTO777
413 // with more than KOMODO_MINRATIFY pay2pubkey outputs -> ratify
414 // if all outputs to notary -> notary utxo
415 // if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed
416 // OP_RETURN: 'D' -> deposit, 'W' -> withdraw
417
418 int32_t gettxout_scriptPubKey(uint8_t *scriptPubKey,int32_t maxsize,uint256 txid,int32_t n);
419
420 int32_t komodo_notarycmp(uint8_t *scriptPubKey,int32_t scriptlen,uint8_t pubkeys[64][33],int32_t numnotaries,uint8_t rmd160[20])
421 {
422     int32_t i;
423     if ( scriptlen == 25 && memcmp(&scriptPubKey[3],rmd160,20) == 0 )
424         return(0);
425     else if ( scriptlen == 35 )
426     {
427         for (i=0; i<numnotaries; i++)
428             if ( memcmp(&scriptPubKey[1],pubkeys[i],33) == 0 )
429                 return(i);
430     }
431     return(-1);
432 }
433
434 void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
435 {
436     static int32_t hwmheight;
437     uint64_t signedmask,voutmask; char symbol[16],dest[16]; struct komodo_state *sp;
438     uint8_t scriptbuf[4096],pubkeys[64][33],rmd160[20],scriptPubKey[35]; uint256 kmdtxid,zero,btctxid,txhash;
439     int32_t i,j,k,numnotaries,scriptlen,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
440     memset(&zero,0,sizeof(zero));
441     komodo_init(pindex->nHeight);
442     if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
443         return;
444     numnotaries = komodo_notaries(pubkeys,pindex->nHeight);
445     calc_rmd160_sha256(rmd160,pubkeys[0],33);
446     if ( pindex->nHeight > hwmheight )
447         hwmheight = pindex->nHeight;
448     else
449     {
450         printf("hwmheight.%d vs pindex->nHeight.%d t.%u reorg.%d\n",hwmheight,pindex->nHeight,(uint32_t)pindex->nTime,hwmheight-pindex->nHeight);
451         komodo_event_rewind(sp,symbol,pindex->nHeight);
452         komodo_stateupdate(pindex->nHeight,0,0,0,zero,0,0,0,0,-pindex->nHeight,pindex->nTime,0,0,0,0);
453     }
454     komodo_currentheight_set(chainActive.Tip()->nHeight);
455     /*if ( komodo_is_issuer() != 0 )
456     {
457         while ( KOMODO_REALTIME == 0 || time(NULL) <= KOMODO_REALTIME )
458         {
459             fprintf(stderr,"komodo_connect.(%s) waiting for realtime RT.%u now.%u\n",ASSETCHAINS_SYMBOL,KOMODO_REALTIME,(uint32_t)time(NULL));
460             sleep(30);
461         }
462     }*/
463     sp->KOMODO_REALTIME = KOMODO_INITDONE = (uint32_t)time(NULL);
464     if ( pindex != 0 )
465     {
466         height = pindex->nHeight;
467         txn_count = block.vtx.size();
468         for (i=0; i<txn_count; i++)
469         {
470             txhash = block.vtx[i].GetHash();
471             numvouts = block.vtx[i].vout.size();
472             notaryid = -1;
473             voutmask = specialtx = notarizedheight = isratification = 0;
474             for (j=0; j<numvouts; j++)
475             {
476                 len = block.vtx[i].vout[j].scriptPubKey.size();
477                 if ( len <= sizeof(scriptbuf) )
478                 {
479 #ifdef KOMODO_ZCASH
480                     memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
481 #else
482                     memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
483 #endif
484                     notaryid = komodo_voutupdate(&isratification,notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,&notarizedheight,(uint64_t)block.vtx[i].vout[j].nValue);
485                     if ( 0 && i > 0 )
486                     {
487                         for (k=0; k<len; k++)
488                             printf("%02x",scriptbuf[k]);
489                         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());
490                     }
491                 }
492             }
493             signedmask = 0;
494             numvins = block.vtx[i].vin.size();
495             for (j=0; j<numvins; j++)
496             {
497                 if ( (scriptlen= gettxout_scriptPubKey(scriptPubKey,sizeof(scriptPubKey),block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) > 0 )
498                 {
499                     if ( (k= komodo_notarycmp(scriptPubKey,scriptlen,pubkeys,numnotaries,rmd160)) >= 0 )
500                         signedmask |= (1LL << k);
501                 }
502             }
503             if ( 0 && signedmask != 0 )
504                 printf("%s ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts);
505             if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) )
506             {
507                 printf("%s NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",ASSETCHAINS_SYMBOL,(long long)signedmask,numvins,height,i,notarizedheight,specialtx);
508                 printf("ht.%d specialtx.%d isratification.%d numvouts.%d signed.%llx numnotaries.%d\n",height,specialtx,isratification,numvouts,(long long)signedmask,numnotaries);
509                 if ( specialtx != 0 && isratification != 0 && numvouts > 2 )
510                 {
511                     numvalid = 0;
512                     memset(pubkeys,0,sizeof(pubkeys));
513                     for (j=1; j<numvouts-1; j++)
514                     {
515                         len = block.vtx[i].vout[j].scriptPubKey.size();
516                         if ( len <= sizeof(scriptbuf) )
517                         {
518 #ifdef KOMODO_ZCASH
519                             memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
520 #else
521                             memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
522 #endif
523                             if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
524                             {
525                                 memcpy(pubkeys[numvalid++],scriptbuf+1,33);
526                                 for (k=0; k<33; k++)
527                                     printf("%02x",scriptbuf[k+1]);
528                                 printf(" <- new notary.[%d]\n",j-1);
529                             }
530                         }
531                     }
532                     if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries>>1) )
533                     {
534                         memset(&txhash,0,sizeof(txhash));
535                         komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0,0);
536                         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);
537                     } else printf("signedmask.%llx numvalid.%d wt.%d numnotaries.%d\n",(long long)signedmask,numvalid,bitweight(signedmask),numnotaries);
538                 }
539             }
540         }
541         if ( pindex->nHeight == hwmheight )
542             komodo_stateupdate(height,0,0,0,zero,0,0,0,0,height,(uint32_t)pindex->nTime,0,0,0,0);
543     } else printf("komodo_connectblock: unexpected null pindex\n");
544     KOMODO_INITDONE = (uint32_t)time(NULL);
545 }
546
547
548 #endif
This page took 0.057915 seconds and 4 git commands to generate.