]> Git Repo - VerusCoin.git/blame - src/komodo.h
test
[VerusCoin.git] / src / komodo.h
CommitLineData
fcd36118 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
3eea72f2 19// Todo: handle reorg: clear all entries above reorged height
20
9499e4de 21#include <stdint.h>
22#include <stdio.h>
975b2ddb 23#include <pthread.h>
88f973c2 24#include <ctype.h>
9499e4de 25
64bb0834 26void 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,uint8_t *opretbuf,uint16_t opretlen);
58c94994 27void komodo_init();
d9d3a941 28int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *notarized_desttxidp);
3c0f5d94 29char *komodo_issuemethod(char *method,char *params,uint16_t port);
9499e4de 30
f38345e9 31int32_t NOTARIZED_HEIGHT,Num_nutxos,KMDHEIGHT;
3b34b59c 32uint256 NOTARIZED_HASH,NOTARIZED_DESTTXID;
975b2ddb 33pthread_mutex_t komodo_mutex;
f38345e9 34char KMDUSERPASS[1024]; uint16_t BITCOIND_PORT = 7771;
d2471545 35
46beb55e 36#include "komodo_utils.h"
8d6bd385 37#include "cJSON.c"
a40be56e 38#include "komodo_bitcoind.h"
3eea72f2 39#include "komodo_interest.h"
3eea72f2 40#include "komodo_pax.h"
3eea72f2 41#include "komodo_notary.h"
88593976 42#include "komodo_gateway.h"
9499e4de 43
64bb0834 44void 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,uint8_t *opretbuf,uint16_t opretlen)
5cea7725 45{
e9595d76 46 static FILE *fp; static int32_t errs; char fname[512]; int32_t ht,func; uint8_t num,pubkeys[64][33];
e402ebee 47#ifdef WIN32
48 sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"komodostate");
49#else
073594aa 50 sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),(char *)"komodostate");
e402ebee 51#endif
bafc61f3 52 if ( fp == 0 )
53 {
54 if ( (fp= fopen(fname,"rb+")) != 0 )
55 {
56 while ( (func= fgetc(fp)) != EOF )
57 {
c50ec0fb 58 if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) )
59 errs++;
5203fc4b 60 //printf("func.(%d %c) ht.%d\n",func,func,ht);
bafc61f3 61 if ( func == 'P' )
62 {
63 if ( (num= fgetc(fp)) < 64 )
dd7b22ad 64 {
52ed4002 65 if ( fread(pubkeys,33,num,fp) != num )
66 errs++;
732fdf87 67 else
68 {
5203fc4b 69 printf("updated %d pubkeys at ht.%d\n",num,ht);
70 komodo_notarysinit(ht,pubkeys,num);
732fdf87 71 }
72 } else printf("illegal num.%d\n",num);
9997caa0 73 //printf("P[%d]\n",num);
bafc61f3 74 }
75 else if ( func == 'N' )
76 {
52ed4002 77 if ( fread(&NOTARIZED_HEIGHT,1,sizeof(NOTARIZED_HEIGHT),fp) != sizeof(NOTARIZED_HEIGHT) )
78 errs++;
79 if ( fread(&NOTARIZED_HASH,1,sizeof(NOTARIZED_HASH),fp) != sizeof(NOTARIZED_HASH) )
80 errs++;
1e81ccb7 81 if ( fread(&NOTARIZED_DESTTXID,1,sizeof(NOTARIZED_DESTTXID),fp) != sizeof(NOTARIZED_DESTTXID) )
52ed4002 82 errs++;
81bac2f0 83 printf("load NOTARIZED %d %s\n",NOTARIZED_HEIGHT,NOTARIZED_HASH.ToString().c_str());
1e81ccb7 84 komodo_notarized_update(ht,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID);
bafc61f3 85 }
671187dd 86 else if ( func == 'U' )
87 {
88 uint8_t n,nid; uint256 hash; uint64_t mask;
89 n = fgetc(fp);
90 nid = fgetc(fp);
9997caa0 91 //printf("U %d %d\n",n,nid);
671187dd 92 if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) )
93 errs++;
94 if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) )
95 errs++;
3eea72f2 96 komodo_nutxoadd(ht,nid,hash,mask,n);
4355e769 97 }
2b84e06c 98 else if ( func == 'K' )
99 {
100 int32_t kheight;
101 if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
102 errs++;
103 if ( kheight > KMDHEIGHT )
104 {
105 KMDHEIGHT = kheight;
106 printf("KMDHEIGHT <- %d\n",kheight);
107 }
108 }
64bb0834 109 else if ( func == 'O' )
110 {
111 uint16_t olen; uint8_t opret[10000];
112 if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) )
113 errs++;
114 if ( fread(opret,1,olen,fp) != olen )
115 errs++;
116 komodo_opreturn(ht,opret,olen);
117 }
4355e769 118 else if ( func == 'D' )
119 {
9997caa0 120 //printf("D[%d]\n",ht);
671187dd 121 }
1e81ccb7 122#ifdef KOMODO_PAX
5cea7725 123 else if ( func == 'V' )
124 {
125 int32_t numpvals; uint32_t pvals[128];
126 numpvals = fgetc(fp);
4a41b0b2 127 if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals )
5cea7725 128 {
129 komodo_pvals(ht,pvals,numpvals);
7ad554ba 130 //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
4a41b0b2 131 } else printf("error loading pvals[%d]\n",numpvals);
5cea7725 132 }
1e81ccb7 133#endif
bafc61f3 134 else printf("illegal func.(%d %c)\n",func,func);
135 }
136 } else fp = fopen(fname,"wb+");
137 printf("fname.(%s) fpos.%ld\n",fname,ftell(fp));
138 }
3eea72f2 139 if ( fp != 0 ) // write out funcid, height, other fields, call side effect function
bafc61f3 140 {
4355e769 141 if ( height < 0 )
142 {
9997caa0 143 height = -height;
144 //printf("func D[%d] errs.%d\n",height,errs);
4355e769 145 fputc('D',fp);
146 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
147 errs++;
148 }
2b84e06c 149 else if ( KMDheight > 0 )
150 {
151 fputc('K',fp);
152 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
153 errs++;
154 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
155 errs++;
156 }
64bb0834 157 else if ( opretbuf != 0 && opretlen > 0 )
158 {
159 fputc('K',fp);
160 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
161 errs++;
162 if ( fwrite(opretbuf,1,opretlen,fp) != opretlen )
163 errs++;
164 komodo_opreturn(height,opretbuf,opretlen);
165 }
9997caa0 166 else if ( notarypubs != 0 && numnotaries > 0 )
bafc61f3 167 {
9997caa0 168 //printf("func P[%d] errs.%d\n",numnotaries,errs);
bafc61f3 169 fputc('P',fp);
c50ec0fb 170 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
171 errs++;
bafc61f3 172 fputc(numnotaries,fp);
52ed4002 173 if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries )
174 errs++;
3eea72f2 175 komodo_notarysinit(height,notarypubs,numnotaries);
bafc61f3 176 }
671187dd 177 else if ( voutmask != 0 && numvouts > 0 )
178 {
9997caa0 179 //printf("func U %d %d errs.%d hashsize.%ld\n",numvouts,notaryid,errs,sizeof(txhash));
671187dd 180 fputc('U',fp);
c50ec0fb 181 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
182 errs++;
671187dd 183 fputc(numvouts,fp);
184 fputc(notaryid,fp);
185 if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) )
186 errs++;
187 if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
188 errs++;
3eea72f2 189 komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts);
671187dd 190 }
1e81ccb7 191#ifdef KOMODO_PAX
4a41b0b2 192 else if ( pvals != 0 && numpvals > 0 )
193 {
194 fputc('V',fp);
195 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
196 errs++;
197 fputc(numpvals,fp);
198 if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals )
199 errs++;
200 komodo_pvals(height,pvals,numpvals);
ef7a3548 201 //printf("save pvals height.%d numpvals.%d\n",height,numpvals);
4a41b0b2 202 }
1e81ccb7 203#endif
6dabcca4 204 else if ( height != 0 )
671187dd 205 {
9997caa0 206 //printf("func N ht.%d errs.%d\n",NOTARIZED_HEIGHT,errs);
671187dd 207 fputc('N',fp);
c50ec0fb 208 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
209 errs++;
671187dd 210 if ( fwrite(&NOTARIZED_HEIGHT,1,sizeof(NOTARIZED_HEIGHT),fp) != sizeof(NOTARIZED_HEIGHT) )
211 errs++;
212 if ( fwrite(&NOTARIZED_HASH,1,sizeof(NOTARIZED_HASH),fp) != sizeof(NOTARIZED_HASH) )
213 errs++;
1e81ccb7 214 if ( fwrite(&NOTARIZED_DESTTXID,1,sizeof(NOTARIZED_DESTTXID),fp) != sizeof(NOTARIZED_DESTTXID) )
671187dd 215 errs++;
1e81ccb7 216 komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID);
671187dd 217 }
47f47733 218 fflush(fp);
bafc61f3 219 }
220}
221
1390456b 222int32_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)
223{
0ac1f6fc 224 static uint256 zero; int32_t opretlen,nid,k,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33];
9c406099 225 if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
1390456b 226 {
c23dd601 227 decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR);
986d3459 228 /*for (k=0; k<33; k++)
e1be360e 229 printf("%02x",crypto777[k]);
230 printf(" crypto777 ");
231 for (k=0; k<scriptlen; k++)
232 printf("%02x",scriptbuf[k]);
986d3459 233 printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/
1390456b 234 if ( memcmp(crypto777,scriptbuf+1,33) == 0 )
235 {
236 *specialtxp = 1;
237 printf(">>>>>>>> ");
238 }
a5f315c7 239 else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 )
1390456b 240 {
84e843cd 241 //printf("found notary.k%d\n",k);
242 if ( notaryid < 64 )
1390456b 243 {
84e843cd 244 if ( notaryid < 0 )
245 {
a5f315c7 246 notaryid = nid;
84e843cd 247 *voutmaskp |= (1LL << j);
248 }
a5f315c7 249 else if ( notaryid != nid )
84e843cd 250 {
a5f315c7 251 printf("mismatch notaryid.%d k.%d\n",notaryid,nid);
84e843cd 252 notaryid = 64;
253 *voutmaskp = 0;
254 }
255 else *voutmaskp |= (1LL << j);
1390456b 256 }
1390456b 257 }
258 }
259 if ( j == 1 && scriptbuf[len++] == 0x6a )
260 {
261 if ( (opretlen= scriptbuf[len++]) == 0x4c )
262 opretlen = scriptbuf[len++];
263 else if ( opretlen == 0x4d )
264 {
265 opretlen = scriptbuf[len++];
266 opretlen = (opretlen << 8) + scriptbuf[len++];
267 }
3eea72f2 268 if ( opretlen >= 32*2+4 && strcmp(KOMODO_SOURCE,(char *)&scriptbuf[len+32*2+4]) == 0 )
1390456b 269 {
4a41b0b2 270 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
271 len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp);
3eea72f2 272 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid);
484f8777 273 for (k=0; k<scriptlen; k++)
274 printf("%02x",scriptbuf[k]);
275 printf(" <- script ht.%d i.%d j.%d\n",height,i,j);
bc1d085a 276 if ( *notarizedheightp > NOTARIZED_HEIGHT && *notarizedheightp < height )
1390456b 277 {
f9d24468 278 printf("ht.%d NOTARIZED.%d KMD.%s BTCTXID.%s (%s)\n",height,*notarizedheightp,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]);
4a41b0b2 279 NOTARIZED_HEIGHT = *notarizedheightp;
280 NOTARIZED_HASH = kmdtxid;
1e81ccb7 281 NOTARIZED_DESTTXID = desttxid;
64bb0834 282 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0);
f9d24468 283 } 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]);
1390456b 284 }
1e81ccb7 285#ifdef KOMODO_PAX
4a41b0b2 286 else if ( i == 0 && scriptbuf[len] == 'P' )
cd1fcb48 287 komodo_paxpricefeed(height,&scriptbuf[++len],opretlen);
1e81ccb7 288#endif
1390456b 289 }
290 return(notaryid);
291}
292
f38345e9 293int32_t komodo_isratify(int32_t isspecial,int32_t numvalid)
294{
295 if ( isspecial != 0 && numvalid > 13 )
296 return(1);
297 else return(0);
298}
299
300// Special tx have vout[0] -> CRYPTO777
301// with more than 13 pay2pubkey outputs -> ratify
302// if all outputs to notary -> notary utxo
303// if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed
304// OP_RETURN: 'D' -> deposit
305
651989c7 306void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
50027f06 307{
e9595d76 308 uint64_t signedmask,voutmask;
dd7b22ad 309 uint8_t scriptbuf[4096],pubkeys[64][33]; uint256 kmdtxid,btctxid,txhash;
e9595d76 310 int32_t i,j,k,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
77117dbe 311 komodo_init();
9b0e1808 312#ifdef KOMODO_ISSUER
313 komodo_gateway_issuer();
314#else
f38345e9 315 komodo_gateway_redeemer();
9b0e1808 316#endif
68916cc6 317 if ( pindex != 0 )
b501ded2 318 {
656eddcd 319 height = pindex->nHeight;
01cc012f 320 txn_count = block.vtx.size();
321 for (i=0; i<txn_count; i++)
dc64de68 322 {
352f8081 323 txhash = block.vtx[i].GetHash();
01cc012f 324 numvouts = block.vtx[i].vout.size();
352f8081 325 notaryid = -1;
1390456b 326 voutmask = specialtx = notarizedheight = 0;
01cc012f 327 for (j=0; j<numvouts; j++)
dc64de68 328 {
1390456b 329 len = block.vtx[i].vout[j].scriptPubKey.size();
330 if ( len <= sizeof(scriptbuf) )
01cc012f 331 {
189d9dee 332#ifdef KOMODO_ZCASH
484f8777 333 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
189d9dee 334#else
335 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
336#endif
acb3f1d8 337 notaryid = komodo_voutupdate(notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,&notarizedheight);
a01acef4 338 if ( 0 && i > 0 )
e69a0833 339 {
340 for (k=0; k<len; k++)
341 printf("%02x",scriptbuf[k]);
a8832194 342 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());
e69a0833 343 }
01cc012f 344 }
352f8081 345 }
cf9538e0 346 if ( i != 0 && notaryid >= 0 && notaryid < 64 && voutmask != 0 )
3eea72f2 347 {
64bb0834 348 komodo_stateupdate(height,0,0,notaryid,txhash,voutmask,numvouts,0,0,0,0,0);
3eea72f2 349 //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts);
350 }
1390456b 351 signedmask = 0;
352 numvins = block.vtx[i].vin.size();
5bdebffe 353 for (j=0; j<numvins; j++)
352f8081 354 {
4355e769 355 if ( (k= komodo_nutxofind(height,block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) >= 0 )
1390456b 356 signedmask |= (1LL << k);
21cc1d3e 357 else if ( 0 && signedmask != 0 )
bc6fd011 358 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);
1390456b 359 }
06f0ed43 360 if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) )
1390456b 361 {
e69a0833 362 printf("NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",(long long)signedmask,numvins,height,i,notarizedheight,specialtx);
3eea72f2 363 if ( specialtx != 0 && numvouts > 2 && komodo_ratify_threshold(height,signedmask) > 0 )
84e843cd 364 {
29e69b85 365 numvalid = 0;
dd7b22ad 366 memset(pubkeys,0,sizeof(pubkeys));
84e843cd 367 for (j=1; j<numvouts; j++)
368 {
369 len = block.vtx[i].vout[j].scriptPubKey.size();
370 if ( len <= sizeof(scriptbuf) )
371 {
189d9dee 372#ifdef KOMODO_ZCASH
484f8777 373 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
189d9dee 374#else
375 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
376#endif
84e843cd 377 if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
378 {
29e69b85 379 memcpy(pubkeys[numvalid++],scriptbuf+1,33);
e7f99312 380 for (k=0; k<33; k++)
84e843cd 381 printf("%02x",scriptbuf[k+1]);
382 printf(" <- new notary.[%d]\n",j-1);
383 }
384 }
385 }
f38345e9 386 if ( komodo_isratify(1,numvalid) > 13 )
d8ce705a 387 {
388 memset(&txhash,0,sizeof(txhash));
64bb0834 389 komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0);
d8ce705a 390 }
835c617e 391 printf("new notaries.%d newheight.%d from height.%d\n",numvouts-1,KOMODO_PUBKEYS_HEIGHT(height),height);
84e843cd 392 }
a96439f5 393 }
656eddcd 394 }
44c4fbbd 395 } else printf("komodo_connectblock: unexpected null pindex\n");
3d35aa5b 396}
397
0f24f245 398
fcd36118 399#endif
This page took 0.166649 seconds and 4 git commands to generate.