]> 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
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <pthread.h>
24 #include <ctype.h>
25
26 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);
27 void komodo_init();
28 int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *notarized_desttxidp);
29 char *komodo_issuemethod(char *method,char *params,uint16_t port);
30
31 int32_t ASSETCHAINS_SHORTFLAG,NOTARIZED_HEIGHT,Num_nutxos,KMDHEIGHT = 43000;
32 uint256 NOTARIZED_HASH,NOTARIZED_DESTTXID;
33 pthread_mutex_t komodo_mutex;
34 char KMDUSERPASS[1024]; uint16_t BITCOIND_PORT = 7771;
35 uint64_t KOMODO_DEPOSIT,PENDING_KOMODO_TX;
36
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     }
152     if ( height <= 0 )
153     {
154         printf("early return: stateupdate height.%d\n",height);
155         return;
156     }
157     if ( fp != 0 ) // write out funcid, height, other fields, call side effect function
158     {
159         //printf("fpos.%ld ",ftell(fp));
160         if ( height < 0 )
161         {
162             height = -height;
163             //printf("func D[%d] errs.%d\n",height,errs);
164             fputc('D',fp);
165             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
166                 errs++;
167         }
168         else if ( KMDheight > 0 )
169         {
170             fputc('K',fp);
171             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
172                 errs++;
173             if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
174                 errs++;
175             //printf("ht.%d K %d\n",height,KMDheight);
176         }
177         else if ( opretbuf != 0 && opretlen > 0 )
178         {
179             uint16_t olen = opretlen;
180             fputc('R',fp);
181             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
182                 errs++;
183             if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
184                 errs++;
185             if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) )
186                 errs++;
187             if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) )
188                 errs++;
189             if ( fwrite(&olen,1,sizeof(olen),fp) != olen )
190                 errs++;
191             if ( fwrite(opretbuf,1,olen,fp) != olen )
192                 errs++;
193             //printf("ht.%d R opret[%d]\n",height,olen);
194             komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout);
195         }
196         else if ( notarypubs != 0 && numnotaries > 0 )
197         {
198             //printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs);
199             fputc('P',fp);
200             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
201                 errs++;
202             fputc(numnotaries,fp);
203             if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries )
204                 errs++;
205             komodo_notarysinit(height,notarypubs,numnotaries);
206         }
207         else if ( voutmask != 0 && numvouts > 0 )
208         {
209             //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash));
210             fputc('U',fp);
211             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
212                 errs++;
213             fputc(numvouts,fp);
214             fputc(notaryid,fp);
215             if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) )
216                 errs++;
217             if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
218                 errs++;
219             komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts);
220         }
221 //#ifdef KOMODO_PAX
222         else if ( pvals != 0 && numpvals > 0 )
223         {
224             int32_t i,nonz = 0;
225             for (i=0; i<32; i++)
226                 if ( pvals[i] != 0 )
227                     nonz++;
228             if ( nonz >= 32 )
229             {
230                 fputc('V',fp);
231                 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
232                     errs++;
233                 fputc(numpvals,fp);
234                 if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals )
235                     errs++;
236                 komodo_pvals(height,pvals,numpvals);
237                 //printf("ht.%d V numpvals[%d]\n",height,numpvals);
238             }
239             //printf("save pvals height.%d numpvals.%d\n",height,numpvals);
240         }
241 //#endif
242         else if ( height != 0 )
243         {
244             //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs);
245             fputc('N',fp);
246             if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
247                 errs++;
248             if ( fwrite(&NOTARIZED_HEIGHT,1,sizeof(NOTARIZED_HEIGHT),fp) != sizeof(NOTARIZED_HEIGHT) )
249                 errs++;
250             if ( fwrite(&NOTARIZED_HASH,1,sizeof(NOTARIZED_HASH),fp) != sizeof(NOTARIZED_HASH) )
251                 errs++;
252             if ( fwrite(&NOTARIZED_DESTTXID,1,sizeof(NOTARIZED_DESTTXID),fp) != sizeof(NOTARIZED_DESTTXID) )
253                 errs++;
254             komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID);
255         }
256         fflush(fp);
257     }
258 }
259
260 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)
261 {
262     static uint256 zero; int32_t opretlen,nid,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33];
263     if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
264     {
265         decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR);
266         /*for (k=0; k<33; k++)
267             printf("%02x",crypto777[k]);
268         printf(" crypto777 ");
269         for (k=0; k<scriptlen; k++)
270             printf("%02x",scriptbuf[k]);
271         printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/
272         if ( memcmp(crypto777,scriptbuf+1,33) == 0 )
273         {
274             *specialtxp = 1;
275             printf(">>>>>>>> ");
276         }
277         else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 )
278         {
279             //printf("found notary.k%d\n",k);
280             if ( notaryid < 64 )
281             {
282                 if ( notaryid < 0 )
283                 {
284                     notaryid = nid;
285                     *voutmaskp |= (1LL << j);
286                 }
287                 else if ( notaryid != nid )
288                 {
289                     printf("mismatch notaryid.%d k.%d\n",notaryid,nid);
290                     notaryid = 64;
291                     *voutmaskp = 0;
292                 }
293                 else *voutmaskp |= (1LL << j);
294             }
295         }
296     }
297     if ( scriptbuf[len++] == 0x6a )
298     {
299         if ( (opretlen= scriptbuf[len++]) == 0x4c )
300             opretlen = scriptbuf[len++];
301         else if ( opretlen == 0x4d )
302         {
303             opretlen = scriptbuf[len++];
304             opretlen = (opretlen << 8) + scriptbuf[len++];
305         }
306         //for (k=0; k<scriptlen; k++)
307         //    printf("%02x",scriptbuf[k]);
308         //printf(" <- script ht.%d i.%d j.%d value %.8f\n",height,i,j,dstr(value));
309         if ( j == 1 && opretlen >= 32*2+4 && strcmp(KOMODO_SOURCE,(char *)&scriptbuf[len+32*2+4]) == 0 )
310         {
311             len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
312             len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp);
313             len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid);
314             if ( *notarizedheightp > NOTARIZED_HEIGHT && *notarizedheightp < height )
315             {
316                 printf("ht.%d NOTARIZED.%d KMD.%s BTCTXID.%s (%s)\n",height,*notarizedheightp,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
317                 NOTARIZED_HEIGHT = *notarizedheightp;
318                 NOTARIZED_HASH = kmdtxid;
319                 NOTARIZED_DESTTXID = desttxid;
320                 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0);
321             } else printf("reject ht.%d NOTARIZED.%d %s.%s DESTTXID.%s (%s)\n",height,*notarizedheightp,KOMODO_SOURCE,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
322         }
323         else if ( i == 0 && j == 1 && opretlen == 149 )
324             komodo_paxpricefeed(height,&scriptbuf[len],opretlen);
325         else komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,value,&scriptbuf[len],opretlen,j);
326     }
327     return(notaryid);
328 }
329
330 int32_t komodo_isratify(int32_t isspecial,int32_t numvalid)
331 {
332     if ( isspecial != 0 && numvalid > 13 )
333         return(1);
334     else return(0);
335 }
336
337 // Special tx have vout[0] -> CRYPTO777
338 // with more than 13 pay2pubkey outputs -> ratify
339 // if all outputs to notary -> notary utxo
340 // if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed
341 // OP_RETURN: 'D' -> deposit, 'W' -> withdraw
342
343 void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
344 {
345     uint64_t signedmask,voutmask;
346     uint8_t scriptbuf[4096],pubkeys[64][33]; uint256 kmdtxid,btctxid,txhash;
347     int32_t i,j,k,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
348     komodo_init();
349 #ifdef KOMODO_ISSUER
350     komodo_gateway_issuer();
351 #else
352     komodo_gateway_redeemer();
353 #endif
354     if ( pindex != 0 )
355     {
356         height = pindex->nHeight;
357         txn_count = block.vtx.size();
358         for (i=0; i<txn_count; i++)
359         {
360             txhash = block.vtx[i].GetHash();
361             numvouts = block.vtx[i].vout.size();
362             notaryid = -1;
363             voutmask = specialtx = notarizedheight = 0;
364             for (j=0; j<numvouts; j++)
365             {
366                 len = block.vtx[i].vout[j].scriptPubKey.size();
367                 if ( len <= sizeof(scriptbuf) )
368                 {
369 #ifdef KOMODO_ZCASH
370                     memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
371 #else
372                     memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
373 #endif
374                     // signedmask is needed here!
375                     notaryid = komodo_voutupdate(notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,&notarizedheight,(uint64_t)block.vtx[i].vout[j].nValue);
376                     if ( 0 && i > 0 )
377                     {
378                         for (k=0; k<len; k++)
379                             printf("%02x",scriptbuf[k]);
380                         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());
381                     }
382                 }
383             }
384             if ( i != 0 && notaryid >= 0 && notaryid < 64 && voutmask != 0 )
385             {
386                 komodo_stateupdate(height,0,0,notaryid,txhash,voutmask,numvouts,0,0,0,0,0,0,0);
387                 //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts);
388             }
389             signedmask = 0;
390             numvins = block.vtx[i].vin.size();
391             for (j=0; j<numvins; j++)
392             {
393                 if ( (k= komodo_nutxofind(height,block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) >= 0 )
394                     signedmask |= (1LL << k);
395                 else if ( 0 && signedmask != 0 )
396                     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);
397             }
398             if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) )
399             {
400                 printf("NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",(long long)signedmask,numvins,height,i,notarizedheight,specialtx);
401                 if ( specialtx != 0 && numvouts > 2 && komodo_ratify_threshold(height,signedmask) > 0 )
402                 {
403                     numvalid = 0;
404                     memset(pubkeys,0,sizeof(pubkeys));
405                     for (j=1; j<numvouts; j++)
406                     {
407                         len = block.vtx[i].vout[j].scriptPubKey.size();
408                         if ( len <= sizeof(scriptbuf) )
409                         {
410 #ifdef KOMODO_ZCASH
411                             memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
412 #else
413                             memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
414 #endif
415                             if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
416                             {
417                                 memcpy(pubkeys[numvalid++],scriptbuf+1,33);
418                                 for (k=0; k<33; k++)
419                                     printf("%02x",scriptbuf[k+1]);
420                                 printf(" <- new notary.[%d]\n",j-1);
421                             }
422                         }
423                     }
424                     if ( komodo_isratify(1,numvalid) > 13 )
425                     {
426                         memset(&txhash,0,sizeof(txhash));
427                         komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0);
428                     }
429                     printf("new notaries.%d newheight.%d from height.%d\n",numvouts-1,KOMODO_PUBKEYS_HEIGHT(height),height);
430                 }
431             }
432         }
433     } else printf("komodo_connectblock: unexpected null pindex\n");
434 }
435
436
437 #endif
This page took 0.049072 seconds and 4 git commands to generate.