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