]> Git Repo - VerusCoin.git/blob - src/komodo.h
Test
[VerusCoin.git] / src / komodo.h
1 /******************************************************************************
2  * Copyright © 2014-2018 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 #include "komodo_defs.h"
19
20 #ifdef _WIN32
21 #define printf(...)
22 #endif
23
24 // Todo:
25 // verify: reorgs
26
27 #define KOMODO_ASSETCHAINS_WAITNOTARIZE
28 #define KOMODO_PAXMAX (10000 * COIN)
29
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <pthread.h>
33 #include <ctype.h>
34 #include "uthash.h"
35 #include "utlist.h"
36
37 int32_t gettxout_scriptPubKey(uint8_t *scriptPubkey,int32_t maxsize,uint256 txid,int32_t n);
38 void komodo_event_rewind(struct komodo_state *sp,char *symbol,int32_t height);
39 void komodo_connectblock(CBlockIndex *pindex,CBlock& block);
40
41 #include "komodo_structs.h"
42 #include "komodo_globals.h"
43 #include "komodo_utils.h"
44 #include "komodo_curve25519.h"
45
46 #include "komodo_cJSON.c"
47 #include "komodo_bitcoind.h"
48 #include "komodo_interest.h"
49 #include "komodo_pax.h"
50 #include "komodo_notary.h"
51
52 int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest);
53 #include "komodo_kv.h"
54 #include "komodo_jumblr.h"
55 #include "komodo_gateway.h"
56 #include "komodo_events.h"
57 #include "komodo_ccdata.h"
58
59 void komodo_currentheight_set(int32_t height)
60 {
61     char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp;
62     if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
63         sp->CURRENT_HEIGHT = height;
64 }
65
66 int32_t komodo_currentheight()
67 {
68     char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp;
69     if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
70         return(sp->CURRENT_HEIGHT);
71     else return(0);
72 }
73
74 int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest)
75 {
76     static int32_t errs;
77     int32_t func,ht,notarized_height,num,matched=0,MoMdepth; uint256 MoM,notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33];
78     if ( (func= fgetc(fp)) != EOF )
79     {
80         if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 )
81             matched = 1;
82         else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0);
83         if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) )
84             errs++;
85         if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && func != 'T' )
86             printf("[%s] matched.%d fpos.%ld func.(%d %c) ht.%d\n",ASSETCHAINS_SYMBOL,matched,ftell(fp),func,func,ht);
87         if ( func == 'P' )
88         {
89             if ( (num= fgetc(fp)) <= 64 )
90             {
91                 if ( fread(pubkeys,33,num,fp) != num )
92                     errs++;
93                 else
94                 {
95                     //printf("updated %d pubkeys at %s ht.%d\n",num,symbol,ht);
96                     if ( (KOMODO_EXTERNAL_NOTARIES != 0 && matched != 0) || (strcmp(symbol,"KMD") == 0 && KOMODO_EXTERNAL_NOTARIES == 0) )
97                         komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys);
98                 }
99             } else printf("illegal num.%d\n",num);
100         }
101         else if ( func == 'N' || func == 'M' )
102         {
103             if ( fread(&notarized_height,1,sizeof(notarized_height),fp) != sizeof(notarized_height) )
104                 errs++;
105             if ( fread(&notarized_hash,1,sizeof(notarized_hash),fp) != sizeof(notarized_hash) )
106                 errs++;
107             if ( fread(&notarized_desttxid,1,sizeof(notarized_desttxid),fp) != sizeof(notarized_desttxid) )
108                 errs++;
109             if ( func == 'M' )
110             {
111                 if ( fread(&MoM,1,sizeof(MoM),fp) != sizeof(MoM) )
112                     errs++;
113                 if ( fread(&MoMdepth,1,sizeof(MoMdepth),fp) != sizeof(MoMdepth) )
114                     errs++;
115                 if ( 1 && ASSETCHAINS_SYMBOL[0] != 0 && sp != 0 )
116                     printf("%s load[%s.%d -> %s] NOTARIZED %d %s MoM.%s %d\n",ASSETCHAINS_SYMBOL,symbol,sp->NUM_NPOINTS,dest,notarized_height,notarized_hash.ToString().c_str(),MoM.ToString().c_str(),MoMdepth);
117             }
118             else
119             {
120                 memset(&MoM,0,sizeof(MoM));
121                 MoMdepth = 0;
122             }
123             //if ( matched != 0 ) global independent states -> inside *sp
124             komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height,MoM,MoMdepth);
125         }
126         else if ( func == 'U' ) // deprecated
127         {
128             uint8_t n,nid; uint256 hash; uint64_t mask;
129             n = fgetc(fp);
130             nid = fgetc(fp);
131             //printf("U %d %d\n",n,nid);
132             if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) )
133                 errs++;
134             if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) )
135                 errs++;
136             //if ( matched != 0 )
137             //    komodo_eventadd_utxo(sp,symbol,ht,nid,hash,mask,n);
138         }
139         else if ( func == 'K' )
140         {
141             int32_t kheight;
142             if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
143                 errs++;
144             //if ( matched != 0 ) global independent states -> inside *sp
145             //printf("%s.%d load[%s] ht.%d\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight);
146             komodo_eventadd_kmdheight(sp,symbol,ht,kheight,0);
147         }
148         else if ( func == 'T' )
149         {
150             int32_t kheight,ktimestamp;
151             if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
152                 errs++;
153             if ( fread(&ktimestamp,1,sizeof(ktimestamp),fp) != sizeof(ktimestamp) )
154                 errs++;
155             //if ( matched != 0 ) global independent states -> inside *sp
156             //printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp);
157             komodo_eventadd_kmdheight(sp,symbol,ht,kheight,ktimestamp);
158         }
159         else if ( func == 'R' )
160         {
161             uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[16384];
162             if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) )
163                 errs++;
164             if ( fread(&v,1,sizeof(v),fp) != sizeof(v) )
165                 errs++;
166             if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) )
167                 errs++;
168             if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) )
169                 errs++;
170             if ( olen < sizeof(opret) )
171             {
172                 if ( fread(opret,1,olen,fp) != olen )
173                     errs++;
174                 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && matched != 0 )
175                 {
176                     int32_t i;  for (i=0; i<olen; i++)
177                         printf("%02x",opret[i]);
178                     printf(" %s.%d load[%s] opret[%c] len.%d %.8f\n",ASSETCHAINS_SYMBOL,ht,symbol,opret[0],olen,(double)ovalue/COIN);
179                 }
180                 komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen); // global shared state -> global PAX
181             } else
182             {
183                 int32_t i;
184                 for (i=0; i<olen; i++)
185                     fgetc(fp);
186                 //printf("illegal olen.%u\n",olen);
187             }
188         }
189         else if ( func == 'D' )
190         {
191             printf("unexpected function D[%d]\n",ht);
192         }
193         else if ( func == 'V' )
194         {
195             int32_t numpvals; uint32_t pvals[128];
196             numpvals = fgetc(fp);
197             if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals )
198             {
199                 //if ( matched != 0 ) global shared state -> global PVALS
200                 //printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht);
201                 komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals);
202                 //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
203             } else printf("error loading pvals[%d]\n",numpvals);
204         }
205         else printf("[%s] %s illegal func.(%d %c)\n",ASSETCHAINS_SYMBOL,symbol,func,func);
206         return(func);
207     } else return(-1);
208 }
209
210 int32_t memread(void *dest,int32_t size,uint8_t *filedata,long *fposp,long datalen)
211 {
212     if ( *fposp+size <= datalen )
213     {
214         memcpy(dest,&filedata[*fposp],size);
215         (*fposp) += size;
216         return(size);
217     }
218     return(-1);
219 }
220
221 int32_t komodo_parsestatefiledata(struct komodo_state *sp,uint8_t *filedata,long *fposp,long datalen,char *symbol,char *dest)
222 {
223     static int32_t errs;
224     int32_t func= -1,ht,notarized_height,MoMdepth,num,matched=0; uint256 MoM,notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33]; long fpos = *fposp;
225     if ( fpos < datalen )
226     {
227         func = filedata[fpos++];
228         if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 )
229             matched = 1;
230         else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0);
231         if ( memread(&ht,sizeof(ht),filedata,&fpos,datalen) != sizeof(ht) )
232             errs++;
233         if ( func == 'P' )
234         {
235             if ( (num= filedata[fpos++]) <= 64 )
236             {
237                 if ( memread(pubkeys,33*num,filedata,&fpos,datalen) != 33*num )
238                     errs++;
239                 else
240                 {
241                     //printf("updated %d pubkeys at %s ht.%d\n",num,symbol,ht);
242                     if ( (KOMODO_EXTERNAL_NOTARIES != 0 && matched != 0) || (strcmp(symbol,"KMD") == 0 && KOMODO_EXTERNAL_NOTARIES == 0) )
243                         komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys);
244                 }
245             } else printf("illegal num.%d\n",num);
246         }
247         else if ( func == 'N' || func == 'M' )
248         {
249             if ( memread(&notarized_height,sizeof(notarized_height),filedata,&fpos,datalen) != sizeof(notarized_height) )
250                 errs++;
251             if ( memread(&notarized_hash,sizeof(notarized_hash),filedata,&fpos,datalen) != sizeof(notarized_hash) )
252                 errs++;
253             if ( memread(&notarized_desttxid,sizeof(notarized_desttxid),filedata,&fpos,datalen) != sizeof(notarized_desttxid) )
254                 errs++;
255             if ( func == 'M' )
256             {
257                 if ( memread(&MoM,sizeof(MoM),filedata,&fpos,datalen) != sizeof(MoM) )
258                     errs++;
259                 if ( memread(&MoMdepth,sizeof(MoMdepth),filedata,&fpos,datalen) != sizeof(MoMdepth) )
260                     errs++;
261                 if ( 1 && ASSETCHAINS_SYMBOL[0] != 0 && sp != 0 )
262                     printf("%s load[%s.%d -> %s] NOTARIZED %d %s MoM.%s %d\n",ASSETCHAINS_SYMBOL,symbol,sp->NUM_NPOINTS,dest,notarized_height,notarized_hash.ToString().c_str(),MoM.ToString().c_str(),MoMdepth);
263             }
264             else
265             {
266                 memset(&MoM,0,sizeof(MoM));
267                 MoMdepth = 0;
268             }
269             komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height,MoM,MoMdepth);
270         }
271         else if ( func == 'U' ) // deprecated
272         {
273             uint8_t n,nid; uint256 hash; uint64_t mask;
274             n = filedata[fpos++];
275             nid = filedata[fpos++];
276             //printf("U %d %d\n",n,nid);
277             if ( memread(&mask,sizeof(mask),filedata,&fpos,datalen) != sizeof(mask) )
278                 errs++;
279             if ( memread(&hash,sizeof(hash),filedata,&fpos,datalen) != sizeof(hash) )
280                 errs++;
281         }
282         else if ( func == 'K' )
283         {
284             int32_t kheight;
285             if ( memread(&kheight,sizeof(kheight),filedata,&fpos,datalen) != sizeof(kheight) )
286                 errs++;
287              komodo_eventadd_kmdheight(sp,symbol,ht,kheight,0);
288         }
289         else if ( func == 'T' )
290         {
291             int32_t kheight,ktimestamp;
292             if ( memread(&kheight,sizeof(kheight),filedata,&fpos,datalen) != sizeof(kheight) )
293                 errs++;
294             if ( memread(&ktimestamp,sizeof(ktimestamp),filedata,&fpos,datalen) != sizeof(ktimestamp) )
295                 errs++;
296             //if ( matched != 0 ) global independent states -> inside *sp
297             //printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp);
298             komodo_eventadd_kmdheight(sp,symbol,ht,kheight,ktimestamp);
299         }
300         else if ( func == 'R' )
301         {
302             uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[16384];
303             if ( memread(&txid,sizeof(txid),filedata,&fpos,datalen) != sizeof(txid) )
304                 errs++;
305             if ( memread(&v,sizeof(v),filedata,&fpos,datalen) != sizeof(v) )
306                 errs++;
307             if ( memread(&ovalue,sizeof(ovalue),filedata,&fpos,datalen) != sizeof(ovalue) )
308                 errs++;
309             if ( memread(&olen,sizeof(olen),filedata,&fpos,datalen) != sizeof(olen) )
310                 errs++;
311             if ( olen < sizeof(opret) )
312             {
313                 if ( memread(opret,olen,filedata,&fpos,datalen) != olen )
314                     errs++;
315                 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && matched != 0 )
316                 {
317                     int32_t i;  for (i=0; i<olen; i++)
318                         printf("%02x",opret[i]);
319                     printf(" %s.%d load[%s] opret[%c] len.%d %.8f\n",ASSETCHAINS_SYMBOL,ht,symbol,opret[0],olen,(double)ovalue/COIN);
320                 }
321                 komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen); // global shared state -> global PAX
322             } else
323             {
324                 int32_t i;
325                 for (i=0; i<olen; i++)
326                     filedata[fpos++];
327                 //printf("illegal olen.%u\n",olen);
328             }
329         }
330         else if ( func == 'D' )
331         {
332             printf("unexpected function D[%d]\n",ht);
333         }
334         else if ( func == 'V' )
335         {
336             int32_t numpvals; uint32_t pvals[128];
337             numpvals = filedata[fpos++];
338             if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && memread(pvals,(int32_t)(sizeof(uint32_t)*numpvals),filedata,&fpos,datalen) == numpvals*sizeof(uint32_t) )
339             {
340                 //if ( matched != 0 ) global shared state -> global PVALS
341                 //printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht);
342                 komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals);
343                 //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
344             } else printf("error loading pvals[%d]\n",numpvals);
345         }
346         else printf("[%s] %s illegal func.(%d %c)\n",ASSETCHAINS_SYMBOL,symbol,func,func);
347         *fposp = fpos;
348         return(func);
349     }
350     return(-1);
351 }
352
353 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,uint256 MoM,int32_t MoMdepth)
354 {
355     static FILE *fp; static int32_t errs,didinit; static uint256 zero;
356     struct komodo_state *sp; char fname[512],symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; int32_t retval,ht,func; uint8_t num,pubkeys[64][33];
357     if ( didinit == 0 )
358     {
359         portable_mutex_init(&KOMODO_KV_mutex);
360         didinit = 1;
361     }
362     if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
363     {
364         KOMODO_INITDONE = (uint32_t)time(NULL);
365         printf("[%s] no komodo_stateptr\n",ASSETCHAINS_SYMBOL);
366         return;
367     }
368     //printf("[%s] (%s) -> (%s)\n",ASSETCHAINS_SYMBOL,symbol,dest);
369     if ( fp == 0 )
370     {
371         komodo_statefname(fname,ASSETCHAINS_SYMBOL,(char *)"komodostate");
372         if ( (fp= fopen(fname,"rb+")) != 0 )
373         {
374             if ( (retval= komodo_faststateinit(sp,fname,symbol,dest)) > 0 )
375                 fseek(fp,0,SEEK_END);
376             else
377             {
378                 fprintf(stderr,"komodo_faststateinit retval.%d\n",retval);
379                 while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
380                     ;
381             }
382         } else fp = fopen(fname,"wb+");
383         KOMODO_INITDONE = (uint32_t)time(NULL);
384     }
385     if ( height <= 0 )
386     {
387         //printf("early return: stateupdate height.%d\n",height);
388         return;
389     }
390     if ( fp != 0 ) // write out funcid, height, other fields, call side effect function
391     {
392         //printf("fpos.%ld ",ftell(fp));
393         if ( KMDheight != 0 )
394         {
395             if ( KMDtimestamp != 0 )
396             {
397                 fputc('T',fp);
398                 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
399                     errs++;
400                 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
401                     errs++;
402                 if ( fwrite(&KMDtimestamp,1,sizeof(KMDtimestamp),fp) != sizeof(KMDtimestamp) )
403                     errs++;
404             }
405             else
406             {
407                 fputc('K',fp);
408                 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
409                     errs++;
410                 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
411                     errs++;
412             }
413             komodo_eventadd_kmdheight(sp,symbol,height,KMDheight,KMDtimestamp);
414         }
415         else if ( opretbuf != 0 && opretlen > 0 )
416         {
417             uint16_t olen = opretlen;
418             fputc('R',fp);
419             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
420                 errs++;
421             if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
422                 errs++;
423             if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) )
424                 errs++;
425             if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) )
426                 errs++;
427             if ( fwrite(&olen,1,sizeof(olen),fp) != olen )
428                 errs++;
429             if ( fwrite(opretbuf,1,olen,fp) != olen )
430                 errs++;
431 //printf("ht.%d R opret[%d] sp.%p\n",height,olen,sp);
432             //komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout);
433             komodo_eventadd_opreturn(sp,symbol,height,txhash,opretvalue,vout,opretbuf,olen);
434         }
435         else if ( notarypubs != 0 && numnotaries > 0 )
436         {
437             printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs);
438             fputc('P',fp);
439             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
440                 errs++;
441             fputc(numnotaries,fp);
442             if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries )
443                 errs++;
444             komodo_eventadd_pubkeys(sp,symbol,height,numnotaries,notarypubs);
445         }
446         else if ( voutmask != 0 && numvouts > 0 )
447         {
448             //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash));
449             fputc('U',fp);
450             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
451                 errs++;
452             fputc(numvouts,fp);
453             fputc(notaryid,fp);
454             if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) )
455                 errs++;
456             if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
457                 errs++;
458             //komodo_eventadd_utxo(sp,symbol,height,notaryid,txhash,voutmask,numvouts);
459         }
460         else if ( pvals != 0 && numpvals > 0 )
461         {
462             int32_t i,nonz = 0;
463             for (i=0; i<32; i++)
464                 if ( pvals[i] != 0 )
465                     nonz++;
466             if ( nonz >= 32 )
467             {
468                 fputc('V',fp);
469                 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
470                     errs++;
471                 fputc(numpvals,fp);
472                 if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals )
473                     errs++;
474                 komodo_eventadd_pricefeed(sp,symbol,height,pvals,numpvals);
475                 //printf("ht.%d V numpvals[%d]\n",height,numpvals);
476             }
477             //printf("save pvals height.%d numpvals.%d\n",height,numpvals);
478         }
479         else if ( height != 0 )
480         {
481             //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs);
482             if ( sp != 0 )
483             {
484                 if ( sp->MoMdepth > 0 && sp->MoM != zero )
485                     fputc('M',fp);
486                 else fputc('N',fp);
487                 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
488                     errs++;
489                 if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) )
490                     errs++;
491                 if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) )
492                     errs++;
493                 if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) )
494                     errs++;
495                 if ( sp->MoMdepth > 0 && sp->MoM != zero )
496                 {
497                     if ( fwrite(&sp->MoM,1,sizeof(sp->MoM),fp) != sizeof(sp->MoM) )
498                         errs++;
499                     if ( fwrite(&sp->MoMdepth,1,sizeof(sp->MoMdepth),fp) != sizeof(sp->MoMdepth) )
500                         errs++;
501                 }
502                 komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT,sp->MoM,sp->MoMdepth);
503             }
504         }
505         fflush(fp);
506     }
507 }
508
509 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,int32_t notarized,uint64_t signedmask,uint32_t timestamp)
510 {
511     static uint256 zero; static FILE *signedfp;
512     int32_t opretlen,nid,offset,k,MoMdepth,matched,len = 0; uint256 MoM,srchash,desttxid; uint8_t crypto777[33]; struct komodo_state *sp; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN];
513     if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
514         return(-1);
515     if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
516     {
517         if ( i == 0 && j == 0 && memcmp(NOTARY_PUBKEY33,scriptbuf+1,33) == 0 && NOTARY_PUBKEY33[0] != 0 )
518         {
519             printf("%s KOMODO_LASTMINED.%d -> %d\n",ASSETCHAINS_SYMBOL,KOMODO_LASTMINED,height);
520             prevKOMODO_LASTMINED = KOMODO_LASTMINED;
521             KOMODO_LASTMINED = height;
522         }
523         decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR);
524         /*for (k=0; k<33; k++)
525             printf("%02x",crypto777[k]);
526         printf(" crypto777 ");
527         for (k=0; k<scriptlen; k++)
528             printf("%02x",scriptbuf[k]);
529         printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/
530         if ( memcmp(crypto777,scriptbuf+1,33) == 0 )
531         {
532             *specialtxp = 1;
533             //printf(">>>>>>>> ");
534         }
535         else if ( komodo_chosennotary(&nid,height,scriptbuf + 1,timestamp) >= 0 )
536         {
537             //printf("found notary.k%d\n",k);
538             if ( notaryid < 64 )
539             {
540                 if ( notaryid < 0 )
541                 {
542                     notaryid = nid;
543                     *voutmaskp |= (1LL << j);
544                 }
545                 else if ( notaryid != nid )
546                 {
547                     //for (i=0; i<33; i++)
548                     //    printf("%02x",scriptbuf[i+1]);
549                     //printf(" %s mismatch notaryid.%d k.%d\n",ASSETCHAINS_SYMBOL,notaryid,nid);
550                     notaryid = 64;
551                     *voutmaskp = 0;
552                 }
553                 else *voutmaskp |= (1LL << j);
554             }
555         }
556     }
557     if ( scriptbuf[len++] == 0x6a )
558     {
559         int32_t nameoffset,opoffset = 0;
560         if ( (opretlen= scriptbuf[len++]) == 0x4c )
561             opretlen = scriptbuf[len++];
562         else if ( opretlen == 0x4d )
563         {
564             opretlen = scriptbuf[len++];
565             opretlen += (scriptbuf[len++] << 8);
566         }
567         opoffset = len;
568         matched = 0;
569         if ( ASSETCHAINS_SYMBOL[0] == 0 )
570         {
571             if ( strcmp("KMD",(char *)&scriptbuf[len+32 * 2 + 4]) == 0 )
572                 matched = 1;
573         }
574         else
575         {
576             if ( strcmp(ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+offset]) == 0 )
577                 matched = 1;
578         }
579         offset = 32 * (1 + matched) + 4;
580         nameoffset = (int32_t)strlen((char *)&scriptbuf[len+offset]);
581         if ( nameoffset == 2 )
582             nameoffset += 2;
583         else nameoffset++;
584         if ( j == 1 && opretlen >= len+offset-opoffset )
585         {
586             static int32_t last_rewind; int32_t rewindtarget,validated = 0; CBlockIndex *pindex;//
587             struct komodo_ccdata ccdata; struct komodo_ccdataMoMoM MoMoMdata;
588             memset(&ccdata,0,sizeof(ccdata));
589             memset(&MoMoMdata,0,sizeof(MoMoMdata));
590             strncpy(ccdata.symbol,(char *)&scriptbuf[len+offset],sizeof(ccdata.symbol));
591             if ( matched == 0 && bitweight(signedmask) >= KOMODO_MINRATIFY )
592                 notarized = 1;
593             if ( strcmp("PIZZA",ccdata.symbol) == 0 )
594                 notarized = 1;
595             if ( 0 && opretlen != 149 )
596                 printf("[%s].%d (%s) matched.%d i.%d j.%d notarized.%d %llx opretlen.%d len.%d offset.%d opoffset.%d\n",ASSETCHAINS_SYMBOL,height,ccdata.symbol,matched,i,j,notarized,(long long)signedmask,opretlen,len,offset,opoffset);
597             len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&srchash);
598             len += iguana_rwnum(0,&scriptbuf[len],sizeof(*notarizedheightp),(uint8_t *)notarizedheightp);
599             if ( matched != 0 )
600                 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid);
601             if ( matched != 0 && IsInitialBlockDownload() == 0 && ((pindex= mapBlockIndex[srchash]) == 0 || pindex->nHeight != *notarizedheightp) )
602             {
603                 if ( sp->NOTARIZED_HEIGHT > 0 && sp->NOTARIZED_HEIGHT < *notarizedheightp )
604                     rewindtarget = sp->NOTARIZED_HEIGHT - 1;
605                 else if ( *notarizedheightp > 101 )
606                     rewindtarget = *notarizedheightp - 101;
607                 else rewindtarget = 0;
608                 if ( rewindtarget != 0 && rewindtarget > KOMODO_REWIND && rewindtarget > last_rewind )
609                 {
610                     if ( last_rewind != 0 )
611                     {
612                         KOMODO_REWIND = rewindtarget;
613                         fprintf(stderr,"%s FORK detected. notarized.%d %s not in this chain! last notarization %d -> rewindtarget.%d\n",ASSETCHAINS_SYMBOL,*notarizedheightp,srchash.ToString().c_str(),sp->NOTARIZED_HEIGHT,rewindtarget);
614                     }
615                     last_rewind = rewindtarget;
616                 }
617             } else validated = 1;
618             if ( notarized != 0 && validated != 0 )
619             {
620                 //sp->NOTARIZED_HEIGHT = *notarizedheightp;
621                 //sp->NOTARIZED_HASH = srchash;
622                 //sp->NOTARIZED_DESTTXID = desttxid;
623                 memset(&MoM,0,sizeof(MoM));
624                 MoMdepth = 0;
625                 len += nameoffset;
626                 ccdata.MoMdata.notarized_height = *notarizedheightp;
627                 ccdata.MoMdata.height = height;
628                 ccdata.MoMdata.txi = i;
629                 //printf("nameoffset.%d len.%d + 36 %d vs opretlen.%d\n",nameoffset,len,len+36,opretlen);
630                 if ( len+36-opoffset <= opretlen )
631                 {
632                     len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&MoM);
633                     len += iguana_rwnum(0,&scriptbuf[len],sizeof(MoMdepth),(uint8_t *)&MoMdepth);
634                     ccdata.MoMdata.MoM = MoM;
635                     ccdata.MoMdata.MoMdepth = MoMdepth;
636                     if ( len+sizeof(ccdata.CCid) <= opretlen )
637                     {
638                         len += iguana_rwnum(0,&scriptbuf[len],sizeof(ccdata.CCid),(uint8_t *)&ccdata.CCid);
639                         ccdata.len = sizeof(ccdata.CCid);
640                         if ( ASSETCHAINS_SYMBOL[0] != 0 )
641                         {
642                             // MoMoM, depth, numpairs, (notarization ht, MoMoM offset)
643                             if ( len+48-opoffset <= opretlen && strcmp(ccdata.symbol,ASSETCHAINS_SYMBOL) == 0 )
644                             {
645                                 len += iguana_rwnum(0,&scriptbuf[len],sizeof(uint32_t),(uint8_t *)&MoMoMdata.MoMoMstart);
646                                 len += iguana_rwnum(0,&scriptbuf[len],sizeof(uint32_t),(uint8_t *)&MoMoMdata.MoMoMend);
647                                 len += iguana_rwbignum(0,&scriptbuf[len],sizeof(MoMoMdata.MoMoM),(uint8_t *)&MoMoMdata.MoMoM);
648                                 len += iguana_rwnum(0,&scriptbuf[len],sizeof(uint32_t),(uint8_t *)&MoMoMdata.MoMoMdepth);
649                                 len += iguana_rwnum(0,&scriptbuf[len],sizeof(uint32_t),(uint8_t *)&MoMoMdata.numpairs);
650                                 MoMoMdata.len += sizeof(MoMoMdata.MoMoM) + sizeof(uint32_t)*4;
651                                 if ( len+MoMoMdata.numpairs*8-opoffset == opretlen )
652                                 {
653                                     MoMoMdata.pairs = (struct komodo_ccdatapair *)calloc(MoMoMdata.numpairs,sizeof(*MoMoMdata.pairs));
654                                     for (k=0; k<MoMoMdata.numpairs; k++)
655                                     {
656                                         len += iguana_rwnum(0,&scriptbuf[len],sizeof(int32_t),(uint8_t *)&MoMoMdata.pairs[k].notarization_height);
657                                         len += iguana_rwnum(0,&scriptbuf[len],sizeof(uint32_t),(uint8_t *)&MoMoMdata.pairs[k].MoMoMoffset);
658                                         MoMoMdata.len += sizeof(uint32_t) * 2;
659                                     }
660                                 } else ccdata.len = MoMoMdata.len = 0;
661                             } else ccdata.len = MoMoMdata.len = 0;
662                         }
663                     }
664                     if ( MoM == zero || MoMdepth > 1440 || MoMdepth < 0 )
665                     {
666                         memset(&MoM,0,sizeof(MoM));
667                         MoMdepth = 0;
668                     }
669                     else
670                     {
671                         komodo_rwccdata(ASSETCHAINS_SYMBOL,1,&ccdata,&MoMoMdata);
672                         //printf("[%s] matched.%d VALID (%s) MoM.%s [%d]\n",ASSETCHAINS_SYMBOL,matched,ccdata.symbol,MoM.ToString().c_str(),MoMdepth);
673                     }
674                     if ( MoMoMdata.pairs != 0 )
675                         free(MoMoMdata.pairs);
676                     memset(&ccdata,0,sizeof(ccdata));
677                     memset(&MoMoMdata,0,sizeof(MoMoMdata));
678                 }
679                 else if ( ASSETCHAINS_SYMBOL[0] == 0 && matched != 0 && notarized != 0 && validated != 0 )
680                     komodo_rwccdata((char *)"KMD",1,&ccdata,0);
681                 if ( matched != 0 && *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height )
682                 {
683                     sp->NOTARIZED_HEIGHT = *notarizedheightp;
684                     sp->NOTARIZED_HASH = srchash;
685                     sp->NOTARIZED_DESTTXID = desttxid;
686                     sp->MoM = MoM;
687                     sp->MoMdepth = MoMdepth;
688                     komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0,0,sp->MoM,sp->MoMdepth);
689                     if ( ASSETCHAINS_SYMBOL[0] != 0 )
690                         printf("[%s] ht.%d NOTARIZED.%d %s.%s %sTXID.%s lens.(%d %d) MoM.%s %d\n",ASSETCHAINS_SYMBOL,height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,srchash.ToString().c_str(),ASSETCHAINS_SYMBOL[0]==0?"BTC":"KMD",desttxid.ToString().c_str(),opretlen,len,sp->MoM.ToString().c_str(),sp->MoMdepth);
691                     if ( ASSETCHAINS_SYMBOL[0] == 0 )
692                     {
693                         if ( signedfp == 0 )
694                         {
695                             char fname[512];
696                             komodo_statefname(fname,ASSETCHAINS_SYMBOL,(char *)"signedmasks");
697                             if ( (signedfp= fopen(fname,"rb+")) == 0 )
698                                 signedfp = fopen(fname,"wb");
699                             else fseek(signedfp,0,SEEK_END);
700                         }
701                         if ( signedfp != 0 )
702                         {
703                             fwrite(&height,1,sizeof(height),signedfp);
704                             fwrite(&signedmask,1,sizeof(signedmask),signedfp);
705                             fflush(signedfp);
706                         }
707                         if ( opretlen > len && scriptbuf[len] == 'A' )
708                         {
709                             //for (i=0; i<opretlen-len; i++)
710                             //    printf("%02x",scriptbuf[len+i]);
711                             //printf(" Found extradata.[%d] %d - %d\n",opretlen-len,opretlen,len);
712                             komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen-len+4+3+(scriptbuf[1] == 0x4d),j,zero,0);
713                         }
714                     }
715                 }
716             } else //if ( matched != 0 && *notarizedheightp != sp->NOTARIZED_HEIGHT )
717                 printf("validated.%d notarized.%d %llx reject ht.%d NOTARIZED.%d prev.%d %s.%s DESTTXID.%s len.%d opretlen.%d\n",validated,notarized,(long long)signedmask,height,*notarizedheightp,sp->NOTARIZED_HEIGHT,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,srchash.ToString().c_str(),desttxid.ToString().c_str(),len,opretlen);
718         }
719         else if ( matched != 0 && i == 0 && j == 1 && opretlen == 149 )
720         {
721             if ( notaryid >= 0 && notaryid < 64 )
722                 komodo_paxpricefeed(height,&scriptbuf[len],opretlen);
723         }
724         else if ( matched != 0 )
725         {
726             //int32_t k; for (k=0; k<scriptlen; k++)
727             //    printf("%02x",scriptbuf[k]);
728             //printf(" <- script ht.%d i.%d j.%d value %.8f %s\n",height,i,j,dstr(value),ASSETCHAINS_SYMBOL);
729             if ( opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
730             {
731                 for (k=0; k<32; k++)
732                     if ( scriptbuf[len+k] != 0 )
733                         break;
734                 if ( k == 32 )
735                 {
736                     *isratificationp = 1;
737                     printf("ISRATIFICATION (%s)\n",(char *)&scriptbuf[len+32*2+4]);
738                 }
739             }
740             
741             if ( *isratificationp == 0 && (signedmask != 0 || (scriptbuf[len] != 'X' && scriptbuf[len] != 'A')) ) // && scriptbuf[len] != 'I')
742                 komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen,j,zero,0);
743         }
744     }
745     return(notaryid);
746 }
747
748 /*int32_t komodo_isratify(int32_t isspecial,int32_t numvalid)
749 {
750     if ( isspecial != 0 && numvalid >= KOMODO_MINRATIFY )
751         return(1);
752     else return(0);
753 }*/
754
755 // Special tx have vout[0] -> CRYPTO777
756 // with more than KOMODO_MINRATIFY pay2pubkey outputs -> ratify
757 // if all outputs to notary -> notary utxo
758 // if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed
759 // OP_RETURN: 'D' -> deposit, 'W' -> withdraw
760
761 int32_t gettxout_scriptPubKey(uint8_t *scriptPubKey,int32_t maxsize,uint256 txid,int32_t n);
762
763 int32_t komodo_notarycmp(uint8_t *scriptPubKey,int32_t scriptlen,uint8_t pubkeys[64][33],int32_t numnotaries,uint8_t rmd160[20])
764 {
765     int32_t i;
766     if ( scriptlen == 25 && memcmp(&scriptPubKey[3],rmd160,20) == 0 )
767         return(0);
768     else if ( scriptlen == 35 )
769     {
770         for (i=0; i<numnotaries; i++)
771             if ( memcmp(&scriptPubKey[1],pubkeys[i],33) == 0 )
772                 return(i);
773     }
774     return(-1);
775 }
776
777 void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
778 {
779     static int32_t hwmheight;
780     uint64_t signedmask,voutmask; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp;
781     uint8_t scriptbuf[10001],pubkeys[64][33],rmd160[20],scriptPubKey[35]; uint256 zero,btctxid,txhash;
782     int32_t i,j,k,numnotaries,notarized,scriptlen,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
783     memset(&zero,0,sizeof(zero));
784     komodo_init(pindex->nHeight);
785     KOMODO_INITDONE = (uint32_t)time(NULL);
786     if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
787     {
788         fprintf(stderr,"unexpected null komodostateptr.[%s]\n",ASSETCHAINS_SYMBOL);
789         return;
790     }
791     //fprintf(stderr,"%s connect.%d\n",ASSETCHAINS_SYMBOL,pindex->nHeight);
792     numnotaries = komodo_notaries(pubkeys,pindex->nHeight,pindex->GetBlockTime());
793     calc_rmd160_sha256(rmd160,pubkeys[0],33);
794     if ( pindex->nHeight > hwmheight )
795         hwmheight = pindex->nHeight;
796     else
797     {
798         if ( pindex->nHeight != hwmheight )
799             printf("%s hwmheight.%d vs pindex->nHeight.%d t.%u reorg.%d\n",ASSETCHAINS_SYMBOL,hwmheight,pindex->nHeight,(uint32_t)pindex->nTime,hwmheight-pindex->nHeight);
800         komodo_event_rewind(sp,symbol,pindex->nHeight);
801         komodo_stateupdate(pindex->nHeight,0,0,0,zero,0,0,0,0,-pindex->nHeight,pindex->nTime,0,0,0,0,zero,0);
802     }
803     komodo_currentheight_set(chainActive.Tip()->nHeight);
804     if ( pindex != 0 )
805     {
806         height = pindex->nHeight;
807         txn_count = block.vtx.size();
808         for (i=0; i<txn_count; i++)
809         {
810             txhash = block.vtx[i].GetHash();
811             numvouts = block.vtx[i].vout.size();
812             notaryid = -1;
813             voutmask = specialtx = notarizedheight = isratification = notarized = 0;
814             signedmask = (height < 91400) ? 1 : 0;
815             numvins = block.vtx[i].vin.size();
816             for (j=0; j<numvins; j++)
817             {
818                 if ( i == 0 && j == 0 )
819                     continue;
820                 if ( (scriptlen= gettxout_scriptPubKey(scriptPubKey,sizeof(scriptPubKey),block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) > 0 )
821                 {
822                     if ( (k= komodo_notarycmp(scriptPubKey,scriptlen,pubkeys,numnotaries,rmd160)) >= 0 )
823                         signedmask |= (1LL << k);
824                     else if ( 0 && numvins >= 17 )
825                     {
826                         int32_t k;
827                         for (k=0; k<scriptlen; k++)
828                             printf("%02x",scriptPubKey[k]);
829                         printf(" scriptPubKey doesnt match any notary vini.%d of %d\n",j,numvins);
830                     }
831                 } else printf("cant get scriptPubKey for ht.%d txi.%d vin.%d\n",height,i,j);
832             }
833             numvalid = bitweight(signedmask);
834             if ( (((height < 90000 || (signedmask & 1) != 0) && numvalid >= KOMODO_MINRATIFY) ||
835                   (numvalid >= KOMODO_MINRATIFY && ASSETCHAINS_SYMBOL[0] != 0) ||
836                   numvalid > (numnotaries/5)) )
837             {
838                 if ( ASSETCHAINS_SYMBOL[0] != 0 )
839                 {
840                     static FILE *signedfp;
841                     if ( signedfp == 0 )
842                     {
843                         char fname[512];
844                         komodo_statefname(fname,ASSETCHAINS_SYMBOL,(char *)"signedmasks");
845                         if ( (signedfp= fopen(fname,"rb+")) == 0 )
846                             signedfp = fopen(fname,"wb");
847                         else fseek(signedfp,0,SEEK_END);
848                     }
849                     if ( signedfp != 0 )
850                     {
851                         fwrite(&height,1,sizeof(height),signedfp);
852                         fwrite(&signedmask,1,sizeof(signedmask),signedfp);
853                         fflush(signedfp);
854                     }
855                      printf("[%s] ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d <<<<<<<<<<<  notarized\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts);
856                 }
857                 notarized = 1;
858             }
859             if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 )
860                 printf("(tx.%d: ",i);
861             for (j=0; j<numvouts; j++)
862             {
863                 /*if ( i == 0 && j == 0 )
864                 {
865                     uint8_t *script = (uint8_t *)block.vtx[0].vout[numvouts-1].scriptPubKey.data();
866                     if ( numvouts <= 2 || script[0] != 0x6a )
867                     {
868                         if ( numvouts == 2 && block.vtx[0].vout[1].nValue != 0 )
869                         {
870                             fprintf(stderr,"ht.%d numvouts.%d value %.8f\n",height,numvouts,dstr(block.vtx[0].vout[1].nValue));
871                             if ( height >= 235300 && block.vtx[0].vout[1].nValue >= 100000*COIN )
872                                 block.vtx[0].vout[1].nValue = 0;
873                             break;
874                         }
875                     }
876                 }*/
877                 if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 )
878                     printf("%.8f ",dstr(block.vtx[i].vout[j].nValue));
879                 len = block.vtx[i].vout[j].scriptPubKey.size();
880                 if ( len >= sizeof(uint32_t) && len <= sizeof(scriptbuf) )
881                 {
882 #ifdef KOMODO_ZCASH
883                     memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
884 #else
885                     memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
886 #endif
887                     notaryid = komodo_voutupdate(&isratification,notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,&notarizedheight,(uint64_t)block.vtx[i].vout[j].nValue,notarized,signedmask,(uint32_t)chainActive.Tip()->GetBlockTime());
888                     if ( 0 && i > 0 )
889                     {
890                         for (k=0; k<len; k++)
891                             printf("%02x",scriptbuf[k]);
892                         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());
893                     }
894                 }
895             }
896             if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 )
897                 printf(") ");
898             if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 )
899                 printf("[%s] ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d notarized.%d special.%d isratification.%d\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts,notarized,specialtx,isratification);
900             if ( notarized != 0 && (notarizedheight != 0 || specialtx != 0) )
901             {
902                 if ( isratification != 0 )
903                 {
904                     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);
905                     printf("ht.%d specialtx.%d isratification.%d numvouts.%d signed.%llx numnotaries.%d\n",height,specialtx,isratification,numvouts,(long long)signedmask,numnotaries);
906                 }
907                 if ( specialtx != 0 && isratification != 0 && numvouts > 2 )
908                 {
909                     numvalid = 0;
910                     memset(pubkeys,0,sizeof(pubkeys));
911                     for (j=1; j<numvouts-1; j++)
912                     {
913                         len = block.vtx[i].vout[j].scriptPubKey.size();
914                         if ( len >= sizeof(uint32_t) && len <= sizeof(scriptbuf) )
915                         {
916 #ifdef KOMODO_ZCASH
917                             memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
918 #else
919                             memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
920 #endif
921                             if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
922                             {
923                                 memcpy(pubkeys[numvalid++],scriptbuf+1,33);
924                                 for (k=0; k<33; k++)
925                                     printf("%02x",scriptbuf[k+1]);
926                                 printf(" <- new notary.[%d]\n",j-1);
927                             }
928                         }
929                     }
930                     if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries/3) )
931                     {
932                         memset(&txhash,0,sizeof(txhash));
933                         komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0,0,zero,0);
934                         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);
935                     } else printf("signedmask.%llx numvalid.%d wt.%d numnotaries.%d\n",(long long)signedmask,numvalid,bitweight(signedmask),numnotaries);
936                 }
937             }
938         }
939         if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 )
940             printf("%s ht.%d\n",ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL,height);
941         if ( pindex->nHeight == hwmheight )
942             komodo_stateupdate(height,0,0,0,zero,0,0,0,0,height,(uint32_t)pindex->nTime,0,0,0,0,zero,0);
943     } else fprintf(stderr,"komodo_connectblock: unexpected null pindex\n");
944     //KOMODO_INITDONE = (uint32_t)time(NULL);
945     //fprintf(stderr,"%s end connect.%d\n",ASSETCHAINS_SYMBOL,pindex->nHeight);
946 }
947
948
949 #endif
This page took 0.075291 seconds and 4 git commands to generate.