]> 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
30df2e00 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,uint64_t opretvalue,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
d3b56fcb 31int32_t NOTARIZED_HEIGHT,Num_nutxos,KMDHEIGHT = 34000;
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
30df2e00 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,uint64_t opretvalue,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 {
33935bd0 111 uint16_t olen; uint64_t ovalue; uint8_t opret[10000];
33935bd0 112 if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) )
113 errs++;
289dc57b 114 if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) )
64bb0834 115 errs++;
289dc57b 116 if ( olen < sizeof(opret) )
117 {
118 if ( fread(opret,1,olen,fp) != olen )
119 errs++;
120 komodo_opreturn(ht,ovalue,opret,olen);
121 } else printf("illegal olen.%u\n",olen);
64bb0834 122 }
4355e769 123 else if ( func == 'D' )
124 {
9997caa0 125 //printf("D[%d]\n",ht);
671187dd 126 }
b673d264 127//#ifdef KOMODO_PAX
5cea7725 128 else if ( func == 'V' )
129 {
130 int32_t numpvals; uint32_t pvals[128];
131 numpvals = fgetc(fp);
4a41b0b2 132 if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals )
5cea7725 133 {
134 komodo_pvals(ht,pvals,numpvals);
e4a19e3a 135 printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
4a41b0b2 136 } else printf("error loading pvals[%d]\n",numpvals);
5cea7725 137 }
b673d264 138//#endif
bafc61f3 139 else printf("illegal func.(%d %c)\n",func,func);
140 }
141 } else fp = fopen(fname,"wb+");
142 printf("fname.(%s) fpos.%ld\n",fname,ftell(fp));
143 }
3eea72f2 144 if ( fp != 0 ) // write out funcid, height, other fields, call side effect function
bafc61f3 145 {
4355e769 146 if ( height < 0 )
147 {
9997caa0 148 height = -height;
149 //printf("func D[%d] errs.%d\n",height,errs);
4355e769 150 fputc('D',fp);
151 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
152 errs++;
153 }
2b84e06c 154 else if ( KMDheight > 0 )
155 {
156 fputc('K',fp);
157 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
158 errs++;
159 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
160 errs++;
161 }
64bb0834 162 else if ( opretbuf != 0 && opretlen > 0 )
163 {
289dc57b 164 uint16_t olen;
9dea9602 165 fputc('O',fp);
64bb0834 166 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
167 errs++;
30df2e00 168 if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) )
33935bd0 169 errs++;
289dc57b 170 olen = opretlen;
171 if ( fwrite(&olen,1,sizeof(olen),fp) != olen )
172 errs++;
64bb0834 173 if ( fwrite(opretbuf,1,opretlen,fp) != opretlen )
174 errs++;
b55937de 175 komodo_opreturn(height,opretvalue,opretbuf,opretlen);
64bb0834 176 }
9997caa0 177 else if ( notarypubs != 0 && numnotaries > 0 )
bafc61f3 178 {
9997caa0 179 //printf("func P[%d] errs.%d\n",numnotaries,errs);
bafc61f3 180 fputc('P',fp);
c50ec0fb 181 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
182 errs++;
bafc61f3 183 fputc(numnotaries,fp);
52ed4002 184 if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries )
185 errs++;
3eea72f2 186 komodo_notarysinit(height,notarypubs,numnotaries);
bafc61f3 187 }
671187dd 188 else if ( voutmask != 0 && numvouts > 0 )
189 {
9997caa0 190 //printf("func U %d %d errs.%d hashsize.%ld\n",numvouts,notaryid,errs,sizeof(txhash));
671187dd 191 fputc('U',fp);
c50ec0fb 192 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
193 errs++;
671187dd 194 fputc(numvouts,fp);
195 fputc(notaryid,fp);
196 if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) )
197 errs++;
198 if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
199 errs++;
3eea72f2 200 komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts);
671187dd 201 }
b673d264 202//#ifdef KOMODO_PAX
4a41b0b2 203 else if ( pvals != 0 && numpvals > 0 )
204 {
bfa5b4f2 205 int32_t i,nonz = 0;
1bf82154 206 for (i=0; i<32; i++)
207 if ( pvals[i] != 0 )
208 nonz++;
209 if ( nonz >= 32 )
210 {
211 fputc('V',fp);
212 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
213 errs++;
214 fputc(numpvals,fp);
215 if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals )
216 errs++;
217 komodo_pvals(height,pvals,numpvals);
218 }
ef7a3548 219 //printf("save pvals height.%d numpvals.%d\n",height,numpvals);
4a41b0b2 220 }
b673d264 221//#endif
6dabcca4 222 else if ( height != 0 )
671187dd 223 {
9997caa0 224 //printf("func N ht.%d errs.%d\n",NOTARIZED_HEIGHT,errs);
671187dd 225 fputc('N',fp);
c50ec0fb 226 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
227 errs++;
671187dd 228 if ( fwrite(&NOTARIZED_HEIGHT,1,sizeof(NOTARIZED_HEIGHT),fp) != sizeof(NOTARIZED_HEIGHT) )
229 errs++;
230 if ( fwrite(&NOTARIZED_HASH,1,sizeof(NOTARIZED_HASH),fp) != sizeof(NOTARIZED_HASH) )
231 errs++;
1e81ccb7 232 if ( fwrite(&NOTARIZED_DESTTXID,1,sizeof(NOTARIZED_DESTTXID),fp) != sizeof(NOTARIZED_DESTTXID) )
671187dd 233 errs++;
1e81ccb7 234 komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID);
671187dd 235 }
47f47733 236 fflush(fp);
bafc61f3 237 }
238}
239
844a6138 240int32_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)
1390456b 241{
0ac1f6fc 242 static uint256 zero; int32_t opretlen,nid,k,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33];
9c406099 243 if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
1390456b 244 {
c23dd601 245 decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR);
986d3459 246 /*for (k=0; k<33; k++)
e1be360e 247 printf("%02x",crypto777[k]);
248 printf(" crypto777 ");
249 for (k=0; k<scriptlen; k++)
250 printf("%02x",scriptbuf[k]);
986d3459 251 printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/
1390456b 252 if ( memcmp(crypto777,scriptbuf+1,33) == 0 )
253 {
254 *specialtxp = 1;
255 printf(">>>>>>>> ");
256 }
a5f315c7 257 else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 )
1390456b 258 {
84e843cd 259 //printf("found notary.k%d\n",k);
260 if ( notaryid < 64 )
1390456b 261 {
84e843cd 262 if ( notaryid < 0 )
263 {
a5f315c7 264 notaryid = nid;
84e843cd 265 *voutmaskp |= (1LL << j);
266 }
a5f315c7 267 else if ( notaryid != nid )
84e843cd 268 {
a5f315c7 269 printf("mismatch notaryid.%d k.%d\n",notaryid,nid);
84e843cd 270 notaryid = 64;
271 *voutmaskp = 0;
272 }
273 else *voutmaskp |= (1LL << j);
1390456b 274 }
1390456b 275 }
276 }
844a6138 277 if ( scriptbuf[len++] == 0x6a )
1390456b 278 {
279 if ( (opretlen= scriptbuf[len++]) == 0x4c )
280 opretlen = scriptbuf[len++];
281 else if ( opretlen == 0x4d )
282 {
283 opretlen = scriptbuf[len++];
284 opretlen = (opretlen << 8) + scriptbuf[len++];
285 }
babdc001 286 for (k=0; k<scriptlen; k++)
287 printf("%02x",scriptbuf[k]);
288 printf(" <- script ht.%d i.%d j.%d\n",height,i,j);
844a6138 289 if ( j == 1 && opretlen >= 32*2+4 && strcmp(KOMODO_SOURCE,(char *)&scriptbuf[len+32*2+4]) == 0 )
1390456b 290 {
4a41b0b2 291 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
292 len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp);
3eea72f2 293 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid);
bc1d085a 294 if ( *notarizedheightp > NOTARIZED_HEIGHT && *notarizedheightp < height )
1390456b 295 {
f9d24468 296 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 297 NOTARIZED_HEIGHT = *notarizedheightp;
298 NOTARIZED_HASH = kmdtxid;
1e81ccb7 299 NOTARIZED_DESTTXID = desttxid;
30df2e00 300 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0);
f9d24468 301 } 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 302 }
babdc001 303 else if ( i == 0 && j == 1 && opretlen == 149 )
0129ff5a 304 komodo_paxpricefeed(height,&scriptbuf[len + scriptbuf[len] == 'P'],opretlen);
844a6138 305 else
306 {
ed932000 307 komodo_stateupdate(0,0,0,0,zero,0,0,0,0,0,value,&scriptbuf[len],opretlen);
844a6138 308 }
1390456b 309 }
310 return(notaryid);
311}
312
f38345e9 313int32_t komodo_isratify(int32_t isspecial,int32_t numvalid)
314{
315 if ( isspecial != 0 && numvalid > 13 )
316 return(1);
317 else return(0);
318}
319
320// Special tx have vout[0] -> CRYPTO777
321// with more than 13 pay2pubkey outputs -> ratify
322// if all outputs to notary -> notary utxo
323// if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed
324// OP_RETURN: 'D' -> deposit
325
651989c7 326void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
50027f06 327{
e9595d76 328 uint64_t signedmask,voutmask;
dd7b22ad 329 uint8_t scriptbuf[4096],pubkeys[64][33]; uint256 kmdtxid,btctxid,txhash;
e9595d76 330 int32_t i,j,k,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
77117dbe 331 komodo_init();
9b0e1808 332#ifdef KOMODO_ISSUER
333 komodo_gateway_issuer();
334#else
f38345e9 335 komodo_gateway_redeemer();
9b0e1808 336#endif
68916cc6 337 if ( pindex != 0 )
b501ded2 338 {
656eddcd 339 height = pindex->nHeight;
01cc012f 340 txn_count = block.vtx.size();
341 for (i=0; i<txn_count; i++)
dc64de68 342 {
352f8081 343 txhash = block.vtx[i].GetHash();
01cc012f 344 numvouts = block.vtx[i].vout.size();
352f8081 345 notaryid = -1;
1390456b 346 voutmask = specialtx = notarizedheight = 0;
01cc012f 347 for (j=0; j<numvouts; j++)
dc64de68 348 {
1390456b 349 len = block.vtx[i].vout[j].scriptPubKey.size();
350 if ( len <= sizeof(scriptbuf) )
01cc012f 351 {
189d9dee 352#ifdef KOMODO_ZCASH
484f8777 353 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
189d9dee 354#else
355 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
356#endif
bfa5b4f2 357 // signedmask is needed here!
629e8f37 358 notaryid = komodo_voutupdate(notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,&notarizedheight,(uint64_t)block.vtx[i].vout[j].nValue);
a01acef4 359 if ( 0 && i > 0 )
e69a0833 360 {
361 for (k=0; k<len; k++)
362 printf("%02x",scriptbuf[k]);
a8832194 363 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 364 }
01cc012f 365 }
352f8081 366 }
cf9538e0 367 if ( i != 0 && notaryid >= 0 && notaryid < 64 && voutmask != 0 )
3eea72f2 368 {
30df2e00 369 komodo_stateupdate(height,0,0,notaryid,txhash,voutmask,numvouts,0,0,0,0,0,0);
3eea72f2 370 //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts);
371 }
1390456b 372 signedmask = 0;
373 numvins = block.vtx[i].vin.size();
5bdebffe 374 for (j=0; j<numvins; j++)
352f8081 375 {
4355e769 376 if ( (k= komodo_nutxofind(height,block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) >= 0 )
1390456b 377 signedmask |= (1LL << k);
21cc1d3e 378 else if ( 0 && signedmask != 0 )
bc6fd011 379 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 380 }
06f0ed43 381 if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) )
1390456b 382 {
e69a0833 383 printf("NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",(long long)signedmask,numvins,height,i,notarizedheight,specialtx);
3eea72f2 384 if ( specialtx != 0 && numvouts > 2 && komodo_ratify_threshold(height,signedmask) > 0 )
84e843cd 385 {
29e69b85 386 numvalid = 0;
dd7b22ad 387 memset(pubkeys,0,sizeof(pubkeys));
84e843cd 388 for (j=1; j<numvouts; j++)
389 {
390 len = block.vtx[i].vout[j].scriptPubKey.size();
391 if ( len <= sizeof(scriptbuf) )
392 {
189d9dee 393#ifdef KOMODO_ZCASH
484f8777 394 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
189d9dee 395#else
396 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
397#endif
84e843cd 398 if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
399 {
29e69b85 400 memcpy(pubkeys[numvalid++],scriptbuf+1,33);
e7f99312 401 for (k=0; k<33; k++)
84e843cd 402 printf("%02x",scriptbuf[k+1]);
403 printf(" <- new notary.[%d]\n",j-1);
404 }
405 }
406 }
f38345e9 407 if ( komodo_isratify(1,numvalid) > 13 )
d8ce705a 408 {
409 memset(&txhash,0,sizeof(txhash));
30df2e00 410 komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0);
d8ce705a 411 }
835c617e 412 printf("new notaries.%d newheight.%d from height.%d\n",numvouts-1,KOMODO_PUBKEYS_HEIGHT(height),height);
84e843cd 413 }
a96439f5 414 }
656eddcd 415 }
44c4fbbd 416 } else printf("komodo_connectblock: unexpected null pindex\n");
3d35aa5b 417}
418
0f24f245 419
fcd36118 420#endif
This page took 0.182236 seconds and 4 git commands to generate.