]> Git Repo - VerusCoin.git/blob - src/komodo.h
test
[VerusCoin.git] / src / komodo.h
1 /******************************************************************************
2  * Copyright © 2014-2016 The SuperNET Developers.                             *
3  *                                                                            *
4  * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at                  *
5  * the top-level directory of this distribution for the individual copyright  *
6  * holder information and the developer policies on copyright and licensing.  *
7  *                                                                            *
8  * Unless otherwise agreed in a custom licensing agreement, no part of the    *
9  * SuperNET software, including this file may be copied, modified, propagated *
10  * or distributed except according to the terms contained in the LICENSE file *
11  *                                                                            *
12  * Removal or modification of this copyright notice is prohibited.            *
13  *                                                                            *
14  ******************************************************************************/
15
16 #ifndef H_KOMODO_H
17 #define H_KOMODO_H
18
19 // Todo: handle reorg: clear all entries above reorged height
20 // smooth consensus price
21 //
22
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <pthread.h>
26 #include <ctype.h>
27
28 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 kheight,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout);
29 void komodo_init();
30 int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *notarized_desttxidp);
31 char *komodo_issuemethod(char *method,char *params,uint16_t port);
32
33 #define GENESIS_NBITS 0x1f00ffff
34 #define KOMODO_MINRATIFY 7
35
36 #include "komodo_globals.h"
37 #include "komodo_utils.h"
38 //queue_t DepositsQ,PendingsQ;
39
40 #include "cJSON.c"
41 #include "komodo_bitcoind.h"
42 #include "komodo_interest.h"
43 #include "komodo_pax.h"
44 #include "komodo_notary.h"
45 #include "komodo_gateway.h"
46
47
48 void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t KMDheight,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout)
49 {
50     static FILE *fp; static int32_t errs; char fname[512]; int32_t ht,func; uint8_t num,pubkeys[64][33];
51 #ifdef WIN32
52     sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"komodostate");
53 #else
54     sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),(char *)"komodostate");
55 #endif
56     if ( fp == 0 )
57     {
58         if ( (fp= fopen(fname,"rb+")) != 0 )
59         {
60             while ( (func= fgetc(fp)) != EOF )
61             {
62                 if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) )
63                     errs++;
64                 //printf("fpos.%ld func.(%d %c) ht.%d ",ftell(fp),func,func,ht);
65                 if ( func == 'P' )
66                 {
67                     if ( (num= fgetc(fp)) < 64 )
68                     {
69                         if ( fread(pubkeys,33,num,fp) != num )
70                             errs++;
71                         else
72                         {
73                             printf("updated %d pubkeys at ht.%d\n",num,ht);
74                             komodo_notarysinit(ht,pubkeys,num);
75                         }
76                     } else printf("illegal num.%d\n",num);
77                     //printf("P[%d]\n",num);
78                 }
79                 else if ( func == 'N' )
80                 {
81                     if ( fread(&NOTARIZED_HEIGHT,1,sizeof(NOTARIZED_HEIGHT),fp) != sizeof(NOTARIZED_HEIGHT) )
82                         errs++;
83                     if ( fread(&NOTARIZED_HASH,1,sizeof(NOTARIZED_HASH),fp) != sizeof(NOTARIZED_HASH) )
84                         errs++;
85                     if ( fread(&NOTARIZED_DESTTXID,1,sizeof(NOTARIZED_DESTTXID),fp) != sizeof(NOTARIZED_DESTTXID) )
86                         errs++;
87                     printf("load NOTARIZED %d %s\n",NOTARIZED_HEIGHT,NOTARIZED_HASH.ToString().c_str());
88                     komodo_notarized_update(ht,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID);
89                 }
90                 else if ( func == 'U' )
91                 {
92                     uint8_t n,nid; uint256 hash; uint64_t mask;
93                     n = fgetc(fp);
94                     nid = fgetc(fp);
95                     //printf("U %d %d\n",n,nid);
96                     if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) )
97                         errs++;
98                     if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) )
99                         errs++;
100                     komodo_nutxoadd(ht,nid,hash,mask,n);
101                 }
102                 else if ( func == 'K' )
103                 {
104                     int32_t kheight;
105                     if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
106                         errs++;
107                     if ( kheight > KMDHEIGHT )
108                     {
109                         KMDHEIGHT = kheight;
110                     }
111                     //printf("ht.%d KMDHEIGHT <- %d\n",ht,kheight);
112                 }
113                 else if ( func == 'R' )
114                 {
115                     uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[10000];
116                     if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) )
117                         errs++;
118                     if ( fread(&v,1,sizeof(v),fp) != sizeof(v) )
119                         errs++;
120                     if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) )
121                         errs++;
122                     if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) )
123                         errs++;
124                     if ( olen < sizeof(opret) )
125                     {
126                         if ( fread(opret,1,olen,fp) != olen )
127                             errs++;
128                         komodo_opreturn(ht,ovalue,opret,olen,txid,v);
129                     } else printf("illegal olen.%u\n",olen);
130                 }
131                 else if ( func == 'D' )
132                 {
133                     //printf("D[%d]\n",ht);
134                 }
135 //#ifdef KOMODO_PAX
136                 else if ( func == 'V' )
137                 {
138                     int32_t numpvals; uint32_t pvals[128];
139                     numpvals = fgetc(fp);
140                     if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals )
141                     {
142                         komodo_pvals(ht,pvals,numpvals);
143                         //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
144                     } else printf("error loading pvals[%d]\n",numpvals);
145                 }
146 //#endif
147                 else printf("illegal func.(%d %c)\n",func,func);
148             }
149         } else fp = fopen(fname,"wb+");
150         printf("fname.(%s) fpos.%ld\n",fname,ftell(fp));
151         KOMODO_INITDONE = (uint32_t)time(NULL);
152     }
153     if ( height <= 0 )
154     {
155         printf("early return: stateupdate height.%d\n",height);
156         return;
157     }
158     if ( fp != 0 ) // write out funcid, height, other fields, call side effect function
159     {
160         //printf("fpos.%ld ",ftell(fp));
161         if ( height < 0 )
162         {
163             height = -height;
164             //printf("func D[%d] errs.%d\n",height,errs);
165             fputc('D',fp);
166             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
167                 errs++;
168         }
169         else if ( KMDheight > 0 )
170         {
171             fputc('K',fp);
172             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
173                 errs++;
174             if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
175                 errs++;
176             //printf("ht.%d K %d\n",height,KMDheight);
177         }
178         else if ( opretbuf != 0 && opretlen > 0 )
179         {
180             uint16_t olen = opretlen;
181             fputc('R',fp);
182             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
183                 errs++;
184             if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
185                 errs++;
186             if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) )
187                 errs++;
188             if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) )
189                 errs++;
190             if ( fwrite(&olen,1,sizeof(olen),fp) != olen )
191                 errs++;
192             if ( fwrite(opretbuf,1,olen,fp) != olen )
193                 errs++;
194             //printf("ht.%d R opret[%d]\n",height,olen);
195             komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout);
196         }
197         else if ( notarypubs != 0 && numnotaries > 0 )
198         {
199             //printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs);
200             fputc('P',fp);
201             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
202                 errs++;
203             fputc(numnotaries,fp);
204             if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries )
205                 errs++;
206             komodo_notarysinit(height,notarypubs,numnotaries);
207         }
208         else if ( voutmask != 0 && numvouts > 0 )
209         {
210             //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash));
211             fputc('U',fp);
212             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
213                 errs++;
214             fputc(numvouts,fp);
215             fputc(notaryid,fp);
216             if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) )
217                 errs++;
218             if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
219                 errs++;
220             komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts);
221         }
222 //#ifdef KOMODO_PAX
223         else if ( pvals != 0 && numpvals > 0 )
224         {
225             int32_t i,nonz = 0;
226             for (i=0; i<32; i++)
227                 if ( pvals[i] != 0 )
228                     nonz++;
229             if ( nonz >= 32 )
230             {
231                 fputc('V',fp);
232                 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
233                     errs++;
234                 fputc(numpvals,fp);
235                 if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals )
236                     errs++;
237                 komodo_pvals(height,pvals,numpvals);
238                 //printf("ht.%d V numpvals[%d]\n",height,numpvals);
239             }
240             //printf("save pvals height.%d numpvals.%d\n",height,numpvals);
241         }
242 //#endif
243         else if ( height != 0 )
244         {
245             //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs);
246             fputc('N',fp);
247             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
248                 errs++;
249             if ( fwrite(&NOTARIZED_HEIGHT,1,sizeof(NOTARIZED_HEIGHT),fp) != sizeof(NOTARIZED_HEIGHT) )
250                 errs++;
251             if ( fwrite(&NOTARIZED_HASH,1,sizeof(NOTARIZED_HASH),fp) != sizeof(NOTARIZED_HASH) )
252                 errs++;
253             if ( fwrite(&NOTARIZED_DESTTXID,1,sizeof(NOTARIZED_DESTTXID),fp) != sizeof(NOTARIZED_DESTTXID) )
254                 errs++;
255             komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID);
256         }
257         fflush(fp);
258     }
259 }
260
261 int32_t komodo_voutupdate(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)
262 {
263     static uint256 zero; int32_t opretlen,nid,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33];
264     if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
265     {
266         decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR);
267         /*for (k=0; k<33; k++)
268             printf("%02x",crypto777[k]);
269         printf(" crypto777 ");
270         for (k=0; k<scriptlen; k++)
271             printf("%02x",scriptbuf[k]);
272         printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/
273         if ( memcmp(crypto777,scriptbuf+1,33) == 0 )
274         {
275             *specialtxp = 1;
276             printf(">>>>>>>> ");
277         }
278         else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 )
279         {
280             //printf("found notary.k%d\n",k);
281             if ( notaryid < 64 )
282             {
283                 if ( notaryid < 0 )
284                 {
285                     notaryid = nid;
286                     *voutmaskp |= (1LL << j);
287                 }
288                 else if ( notaryid != nid )
289                 {
290                     printf("mismatch notaryid.%d k.%d\n",notaryid,nid);
291                     notaryid = 64;
292                     *voutmaskp = 0;
293                 }
294                 else *voutmaskp |= (1LL << j);
295             }
296         }
297     }
298     if ( scriptbuf[len++] == 0x6a )
299     {
300         if ( (opretlen= scriptbuf[len++]) == 0x4c )
301             opretlen = scriptbuf[len++];
302         else if ( opretlen == 0x4d )
303         {
304             opretlen = scriptbuf[len++];
305             opretlen = (opretlen << 8) + scriptbuf[len++];
306         }
307         //for (k=0; k<scriptlen; k++)
308         //    printf("%02x",scriptbuf[k]);
309         //printf(" <- script ht.%d i.%d j.%d value %.8f\n",height,i,j,dstr(value));
310         if ( j == 1 && opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
311         {
312             len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
313             len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp);
314             len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid);
315             if ( *notarizedheightp > NOTARIZED_HEIGHT && *notarizedheightp < height )
316             {
317                 printf("ht.%d NOTARIZED.%d %s.%s %sTXID.%s (%s)\n",height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),ASSETCHAINS_SYMBOL[0]==0?"BTC":"KMD",desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
318                 NOTARIZED_HEIGHT = *notarizedheightp;
319                 NOTARIZED_HASH = kmdtxid;
320                 NOTARIZED_DESTTXID = desttxid;
321                 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0);
322             } else printf("reject ht.%d NOTARIZED.%d %s.%s DESTTXID.%s (%s)\n",height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
323         }
324         else if ( i == 0 && j == 1 && opretlen == 149 )
325             komodo_paxpricefeed(height,&scriptbuf[len],opretlen);
326         else komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,value,&scriptbuf[len],opretlen,j);
327     }
328     return(notaryid);
329 }
330
331 int32_t komodo_isratify(int32_t isspecial,int32_t numvalid)
332 {
333     if ( isspecial != 0 && numvalid >= KOMODO_MINRATIFY )
334         return(1);
335     else return(0);
336 }
337
338 // Special tx have vout[0] -> CRYPTO777
339 // with more than KOMODO_MINRATIFY pay2pubkey outputs -> ratify
340 // if all outputs to notary -> notary utxo
341 // if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed
342 // OP_RETURN: 'D' -> deposit, 'W' -> withdraw
343
344 void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
345 {
346     static int32_t hwmheight;
347     uint64_t signedmask,voutmask;
348     uint8_t scriptbuf[4096],pubkeys[64][33]; uint256 kmdtxid,btctxid,txhash;
349     int32_t i,j,k,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
350     komodo_init();
351     if ( pindex->nHeight > hwmheight )
352         hwmheight = pindex->nHeight;
353     else
354     {
355         printf("hwmheight.%d vs pindex->nHeight.%d reorg.%d\n",hwmheight,pindex->nHeight,hwmheight-pindex->nHeight);
356         // reset komodostate
357     }
358     if ( ASSETCHAINS_SYMBOL[0] != 0 )
359     {
360         while ( KOMODO_REALTIME == 0 || time(NULL) <= KOMODO_REALTIME )
361         {
362             fprintf(stderr,"komodo_connect.(%s) waiting for realtime RT.%u now.%u\n",ASSETCHAINS_SYMBOL,KOMODO_REALTIME,(uint32_t)time(NULL));
363             sleep(3);
364         }
365     }
366     KOMODO_INITDONE = (uint32_t)time(NULL);
367     if ( pindex != 0 )
368     {
369         height = pindex->nHeight;
370         txn_count = block.vtx.size();
371         for (i=0; i<txn_count; i++)
372         {
373             txhash = block.vtx[i].GetHash();
374             numvouts = block.vtx[i].vout.size();
375             notaryid = -1;
376             voutmask = specialtx = notarizedheight = 0;
377             for (j=0; j<numvouts; j++)
378             {
379                 len = block.vtx[i].vout[j].scriptPubKey.size();
380                 if ( len <= sizeof(scriptbuf) )
381                 {
382 #ifdef KOMODO_ZCASH
383                     memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
384 #else
385                     memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
386 #endif
387                     // signedmask is needed here!
388                     notaryid = komodo_voutupdate(notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,&notarizedheight,(uint64_t)block.vtx[i].vout[j].nValue);
389                     if ( 0 && i > 0 )
390                     {
391                         for (k=0; k<len; k++)
392                             printf("%02x",scriptbuf[k]);
393                         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());
394                     }
395                 }
396             }
397             if ( i != 0 && notaryid >= 0 && notaryid < 64 && voutmask != 0 )
398             {
399                 komodo_stateupdate(height,0,0,notaryid,txhash,voutmask,numvouts,0,0,0,0,0,0,0);
400                 //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts);
401             }
402             signedmask = 0;
403             numvins = block.vtx[i].vin.size();
404             for (j=0; j<numvins; j++)
405             {
406                 if ( (k= komodo_nutxofind(height,block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) >= 0 )
407                     signedmask |= (1LL << k);
408                 else if ( 0 && signedmask != 0 )
409                     printf("signedmask.%llx but ht.%d i.%d j.%d not found (%s %d)\n",(long long)signedmask,height,i,j,block.vtx[i].vin[j].prevout.hash.ToString().c_str(),block.vtx[i].vin[j].prevout.n);
410             }
411             if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) )
412             {
413                 printf("NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",(long long)signedmask,numvins,height,i,notarizedheight,specialtx);
414                 if ( specialtx != 0 && numvouts > 2 && komodo_ratify_threshold(height,signedmask) > 0 )
415                 {
416                     numvalid = 0;
417                     memset(pubkeys,0,sizeof(pubkeys));
418                     for (j=1; j<numvouts; j++)
419                     {
420                         len = block.vtx[i].vout[j].scriptPubKey.size();
421                         if ( len <= sizeof(scriptbuf) )
422                         {
423 #ifdef KOMODO_ZCASH
424                             memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
425 #else
426                             memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
427 #endif
428                             if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
429                             {
430                                 memcpy(pubkeys[numvalid++],scriptbuf+1,33);
431                                 for (k=0; k<33; k++)
432                                     printf("%02x",scriptbuf[k+1]);
433                                 printf(" <- new notary.[%d]\n",j-1);
434                             }
435                         }
436                     }
437                     if ( komodo_isratify(1,numvalid) >= KOMODO_MINRATIFY && numvouts > 13 )
438                     {
439                         memset(&txhash,0,sizeof(txhash));
440                         komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0);
441                     }
442                     printf("new notaries.%d newheight.%d from height.%d\n",numvouts-1,KOMODO_PUBKEYS_HEIGHT(height),height);
443                 }
444             }
445         }
446     } else printf("komodo_connectblock: unexpected null pindex\n");
447     KOMODO_INITDONE = (uint32_t)time(NULL);
448 }
449
450
451 #endif
This page took 0.050925 seconds and 4 git commands to generate.