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