]> Git Repo - VerusCoin.git/blame - src/komodo.h
test
[VerusCoin.git] / src / komodo.h
CommitLineData
fcd36118 1/******************************************************************************
37fed603 2 * Copyright © 2014-2017 The SuperNET Developers. *
fcd36118 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
27bf3c5e 19// Todo:
aa114a60 20// verify: reorgs
3ec03ada 21
26ac06c4 22#define KOMODO_ASSETCHAINS_WAITNOTARIZE
fa8d44fc 23#define KOMODO_PAXMAX (10000 * COIN)
aa114a60 24
9499e4de 25#include <stdint.h>
26#include <stdio.h>
975b2ddb 27#include <pthread.h>
88f973c2 28#include <ctype.h>
6d5cb6d4 29#include "uthash.h"
30#include "utlist.h"
9499e4de 31
fc318ffe 32int32_t gettxout_scriptPubKey(uint8_t *scriptPubkey,int32_t maxsize,uint256 txid,int32_t n);
f3a1de3a 33void komodo_event_rewind(struct komodo_state *sp,char *symbol,int32_t height);
875cf683 34void komodo_connectblock(CBlockIndex *pindex,CBlock& block);
3face669 35
fc318ffe 36#include "komodo_structs.h"
9fb37168 37#include "komodo_globals.h"
46beb55e 38#include "komodo_utils.h"
1bfdde1d 39
e4e76989 40#include "cJSON.c"
41#include "komodo_bitcoind.h"
42#include "komodo_interest.h"
43#include "komodo_pax.h"
44#include "komodo_notary.h"
ab918767 45
46int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest);
e4e76989 47#include "komodo_gateway.h"
48#include "komodo_events.h"
49
c75c18fc 50void komodo_currentheight_set(int32_t height)
51{
52 char symbol[16],dest[16]; struct komodo_state *sp;
53 if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
54 sp->CURRENT_HEIGHT = height;
55}
56
57int32_t komodo_currentheight()
58{
59 char symbol[16],dest[16]; struct komodo_state *sp;
60 if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
61 return(sp->CURRENT_HEIGHT);
62 else return(0);
63}
64
65int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest)
66{
67 static int32_t errs;
2df2f193 68 int32_t func,ht,notarized_height,num,matched=0; uint256 notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33];
c75c18fc 69 if ( (func= fgetc(fp)) != EOF )
70 {
71 if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 )
72 matched = 1;
73 else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0);
74 if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) )
75 errs++;
76 //printf("fpos.%ld func.(%d %c) ht.%d ",ftell(fp),func,func,ht);
77 if ( func == 'P' )
78 {
f3a1de3a 79 if ( (num= fgetc(fp)) <= 64 )
c75c18fc 80 {
81 if ( fread(pubkeys,33,num,fp) != num )
82 errs++;
83 else
84 {
ec368288 85 //printf("updated %d pubkeys at %s ht.%d\n",num,symbol,ht);
15724d02 86 if ( (KOMODO_EXTERNAL_NOTARIES != 0 && matched != 0) || (strcmp(symbol,"KMD") == 0 && KOMODO_EXTERNAL_NOTARIES == 0) )
c75c18fc 87 komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys);
88 }
89 } else printf("illegal num.%d\n",num);
90 }
91 else if ( func == 'N' )
92 {
93 if ( fread(&notarized_height,1,sizeof(notarized_height),fp) != sizeof(notarized_height) )
94 errs++;
95 if ( fread(&notarized_hash,1,sizeof(notarized_hash),fp) != sizeof(notarized_hash) )
96 errs++;
97 if ( fread(&notarized_desttxid,1,sizeof(notarized_desttxid),fp) != sizeof(notarized_desttxid) )
98 errs++;
e8ce1079 99 if ( 0 && sp != 0 )
f2db1a97 100 printf("%s load[%s.%d] NOTARIZED %d %s\n",ASSETCHAINS_SYMBOL,symbol,sp->NUM_NPOINTS,notarized_height,notarized_hash.ToString().c_str());
1ebb14e7 101 //if ( matched != 0 ) global independent states -> inside *sp
102 komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height);
c75c18fc 103 }
1ebb14e7 104 else if ( func == 'U' ) // deprecated
c75c18fc 105 {
106 uint8_t n,nid; uint256 hash; uint64_t mask;
107 n = fgetc(fp);
108 nid = fgetc(fp);
109 //printf("U %d %d\n",n,nid);
110 if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) )
111 errs++;
112 if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) )
113 errs++;
6f3cb4d0 114 //if ( matched != 0 )
115 // komodo_eventadd_utxo(sp,symbol,ht,nid,hash,mask,n);
c75c18fc 116 }
117 else if ( func == 'K' )
118 {
119 int32_t kheight;
120 if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
121 errs++;
1ebb14e7 122 //if ( matched != 0 ) global independent states -> inside *sp
a8f4e85e 123 //printf("%s.%d load[%s] ht.%d\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight);
e155dbe9 124 komodo_eventadd_kmdheight(sp,symbol,ht,kheight,0);
125 }
126 else if ( func == 'T' )
127 {
128 int32_t kheight,ktimestamp;
129 if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
130 errs++;
131 if ( fread(&ktimestamp,1,sizeof(ktimestamp),fp) != sizeof(ktimestamp) )
132 errs++;
133 //if ( matched != 0 ) global independent states -> inside *sp
a8f4e85e 134 //printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp);
e155dbe9 135 komodo_eventadd_kmdheight(sp,symbol,ht,kheight,ktimestamp);
c75c18fc 136 }
137 else if ( func == 'R' )
138 {
ef1a9636 139 uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[16384];
c75c18fc 140 if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) )
141 errs++;
142 if ( fread(&v,1,sizeof(v),fp) != sizeof(v) )
143 errs++;
144 if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) )
145 errs++;
146 if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) )
147 errs++;
148 if ( olen < sizeof(opret) )
149 {
150 if ( fread(opret,1,olen,fp) != olen )
151 errs++;
b27b2877 152 if ( 0 && matched != 0 )
e2a12abf 153 {
154 int32_t i; for (i=0; i<olen; i++)
155 printf("%02x",opret[i]);
156 printf(" %s.%d load[%s] opret[%c] len.%d %.8f\n",ASSETCHAINS_SYMBOL,ht,symbol,opret[0],olen,(double)ovalue/COIN);
157 }
158 komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen); // global shared state -> global PAX
939a5a45 159 } else
160 {
66d02e79 161 int32_t i;
939a5a45 162 for (i=0; i<olen; i++)
163 fgetc(fp);
dff4ef45 164 //printf("illegal olen.%u\n",olen);
939a5a45 165 }
c75c18fc 166 }
167 else if ( func == 'D' )
168 {
169 printf("unexpected function D[%d]\n",ht);
170 }
171 else if ( func == 'V' )
172 {
173 int32_t numpvals; uint32_t pvals[128];
174 numpvals = fgetc(fp);
175 if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals )
176 {
1ebb14e7 177 //if ( matched != 0 ) global shared state -> global PVALS
335f2ea7 178 //printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht);
15724d02 179 komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals);
c75c18fc 180 //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
181 } else printf("error loading pvals[%d]\n",numpvals);
182 }
7ba6b48d 183 else printf("[%s] %s illegal func.(%d %c)\n",ASSETCHAINS_SYMBOL,symbol,func,func);
c75c18fc 184 return(func);
185 } else return(-1);
186}
187
e155dbe9 188void 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)
5cea7725 189{
c75c18fc 190 static FILE *fp; static int32_t errs;
e21ffa1d 191 struct komodo_state *sp; char fname[512],symbol[16],dest[16]; int32_t ht,func; uint8_t num,pubkeys[64][33];
c75c18fc 192 if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
15373725 193 {
194 KOMODO_INITDONE = (uint32_t)time(NULL);
c75c18fc 195 return;
15373725 196 }
888cf827 197 if ( fp == 0 )
198 {
055db9b6 199 komodo_statefname(fname,ASSETCHAINS_SYMBOL,(char *)"komodostate");
bafc61f3 200 if ( (fp= fopen(fname,"rb+")) != 0 )
201 {
368c61bc 202 while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
203 ;
bafc61f3 204 } else fp = fopen(fname,"wb+");
205 printf("fname.(%s) fpos.%ld\n",fname,ftell(fp));
63c30610 206 KOMODO_INITDONE = (uint32_t)time(NULL);
bafc61f3 207 }
e98e2aec 208 if ( height <= 0 )
49df008c 209 {
6458297e 210 //printf("early return: stateupdate height.%d\n",height);
49df008c 211 return;
212 }
3eea72f2 213 if ( fp != 0 ) // write out funcid, height, other fields, call side effect function
bafc61f3 214 {
2578b002 215 //printf("fpos.%ld ",ftell(fp));
ab918767 216 if ( KMDheight != 0 )
2b84e06c 217 {
e155dbe9 218 if ( KMDtimestamp != 0 )
219 {
220 fputc('T',fp);
221 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
222 errs++;
223 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
224 errs++;
225 if ( fwrite(&KMDtimestamp,1,sizeof(KMDtimestamp),fp) != sizeof(KMDtimestamp) )
226 errs++;
227 }
228 else
229 {
230 fputc('K',fp);
231 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
232 errs++;
233 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
234 errs++;
235 }
236 komodo_eventadd_kmdheight(sp,symbol,height,KMDheight,KMDtimestamp);
2b84e06c 237 }
01f9cb3d 238 else if ( opretbuf != 0 && opretlen > 0 )
64bb0834 239 {
01f9cb3d 240 uint16_t olen = opretlen;
241 fputc('R',fp);
64bb0834 242 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
243 errs++;
429dabb5 244 if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
245 errs++;
246 if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) )
247 errs++;
30df2e00 248 if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) )
33935bd0 249 errs++;
289dc57b 250 if ( fwrite(&olen,1,sizeof(olen),fp) != olen )
251 errs++;
01f9cb3d 252 if ( fwrite(opretbuf,1,olen,fp) != olen )
64bb0834 253 errs++;
1938746c 254 //printf("ht.%d R opret[%d]\n",height,olen);
c75c18fc 255 //komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout);
256 komodo_eventadd_opreturn(sp,symbol,height,txhash,opretvalue,vout,opretbuf,olen);
01f9cb3d 257 }
9997caa0 258 else if ( notarypubs != 0 && numnotaries > 0 )
bafc61f3 259 {
f3a1de3a 260 printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs);
bafc61f3 261 fputc('P',fp);
c50ec0fb 262 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
263 errs++;
bafc61f3 264 fputc(numnotaries,fp);
52ed4002 265 if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries )
266 errs++;
c75c18fc 267 komodo_eventadd_pubkeys(sp,symbol,height,numnotaries,notarypubs);
bafc61f3 268 }
671187dd 269 else if ( voutmask != 0 && numvouts > 0 )
270 {
2578b002 271 //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash));
671187dd 272 fputc('U',fp);
c50ec0fb 273 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
274 errs++;
671187dd 275 fputc(numvouts,fp);
276 fputc(notaryid,fp);
277 if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) )
278 errs++;
279 if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
280 errs++;
6f3cb4d0 281 //komodo_eventadd_utxo(sp,symbol,height,notaryid,txhash,voutmask,numvouts);
671187dd 282 }
4a41b0b2 283 else if ( pvals != 0 && numpvals > 0 )
284 {
bfa5b4f2 285 int32_t i,nonz = 0;
1bf82154 286 for (i=0; i<32; i++)
287 if ( pvals[i] != 0 )
288 nonz++;
289 if ( nonz >= 32 )
290 {
291 fputc('V',fp);
292 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
293 errs++;
294 fputc(numpvals,fp);
295 if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals )
296 errs++;
c75c18fc 297 komodo_eventadd_pricefeed(sp,symbol,height,pvals,numpvals);
2578b002 298 //printf("ht.%d V numpvals[%d]\n",height,numpvals);
1bf82154 299 }
ef7a3548 300 //printf("save pvals height.%d numpvals.%d\n",height,numpvals);
4a41b0b2 301 }
6dabcca4 302 else if ( height != 0 )
671187dd 303 {
2578b002 304 //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs);
b148643e 305 if ( sp != 0 )
807200e4 306 {
307 fputc('N',fp);
308 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
309 errs++;
310 if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) )
311 errs++;
312 if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) )
313 errs++;
314 if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) )
315 errs++;
316 komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT);
317 }
671187dd 318 }
47f47733 319 fflush(fp);
bafc61f3 320 }
321}
322
8f40a7a2 323int32_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)
1390456b 324{
c96e160a 325 static uint256 zero; static FILE *signedfp;
dbaf1154 326 int32_t opretlen,nid,k,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33]; struct komodo_state *sp; char symbol[16],dest[16];
2df2f193 327 if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
328 return(-1);
9c406099 329 if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
1390456b 330 {
c23dd601 331 decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR);
986d3459 332 /*for (k=0; k<33; k++)
e1be360e 333 printf("%02x",crypto777[k]);
334 printf(" crypto777 ");
335 for (k=0; k<scriptlen; k++)
336 printf("%02x",scriptbuf[k]);
986d3459 337 printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/
1390456b 338 if ( memcmp(crypto777,scriptbuf+1,33) == 0 )
339 {
340 *specialtxp = 1;
b7f942c8 341 //printf(">>>>>>>> ");
1390456b 342 }
a5f315c7 343 else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 )
1390456b 344 {
84e843cd 345 //printf("found notary.k%d\n",k);
346 if ( notaryid < 64 )
1390456b 347 {
84e843cd 348 if ( notaryid < 0 )
349 {
a5f315c7 350 notaryid = nid;
84e843cd 351 *voutmaskp |= (1LL << j);
352 }
a5f315c7 353 else if ( notaryid != nid )
84e843cd 354 {
b7f942c8 355 //for (i=0; i<33; i++)
356 // printf("%02x",scriptbuf[i+1]);
357 //printf(" %s mismatch notaryid.%d k.%d\n",ASSETCHAINS_SYMBOL,notaryid,nid);
84e843cd 358 notaryid = 64;
359 *voutmaskp = 0;
360 }
361 else *voutmaskp |= (1LL << j);
1390456b 362 }
1390456b 363 }
364 }
844a6138 365 if ( scriptbuf[len++] == 0x6a )
1390456b 366 {
367 if ( (opretlen= scriptbuf[len++]) == 0x4c )
368 opretlen = scriptbuf[len++];
369 else if ( opretlen == 0x4d )
370 {
371 opretlen = scriptbuf[len++];
508b0d3c 372 opretlen += (scriptbuf[len++] << 8);
1390456b 373 }
a6400079 374 if ( j == 1 && opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
1390456b 375 {
4a41b0b2 376 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
dbaf1154 377 len += iguana_rwnum(0,&scriptbuf[len],sizeof(*notarizedheightp),(uint8_t *)notarizedheightp);
3eea72f2 378 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid);
447744dc 379 if ( notarized != 0 && *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height && (height < sp->CURRENT_HEIGHT-1000 || komodo_verifynotarization(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,ASSETCHAINS_SYMBOL[0] == 0 ? "BTC" : "KMD",height,*notarizedheightp,kmdtxid,desttxid) == 0) )
1390456b 380 {
c75c18fc 381 sp->NOTARIZED_HEIGHT = *notarizedheightp;
382 sp->NOTARIZED_HASH = kmdtxid;
383 sp->NOTARIZED_DESTTXID = desttxid;
e155dbe9 384 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0,0);
3237fdd4 385 len += 4;
056447a6 386 if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 )
e8ce1079 387 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);
dbaf1154 388 if ( ASSETCHAINS_SYMBOL[0] == 0 )
06d1a655 389 {
dbaf1154 390 if ( signedfp == 0 )
391 {
392 char fname[512];
403b5de8 393 komodo_statefname(fname,(char *)"",(char *)"signedmasks");
dbaf1154 394 if ( (signedfp= fopen(fname,"rb+")) == 0 )
395 signedfp = fopen(fname,"wb");
396 else fseek(signedfp,0,SEEK_END);
397 }
398 if ( signedfp != 0 )
399 {
400 fwrite(&height,1,sizeof(height),signedfp);
401 fwrite(&signedmask,1,sizeof(signedmask),signedfp);
402 fflush(signedfp);
403 }
404 if ( opretlen > len && scriptbuf[len] == 'A' )
405 {
8b249d56 406 for (i=0; i<opretlen-len; i++)
407 printf("%02x",scriptbuf[len+i]);
408 printf(" Found extradata.[%d] %d - %d\n",opretlen-len,opretlen,len);
c83633f8 409 komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen-len+4+3+(scriptbuf[1] == 0x4d),j);
dbaf1154 410 }
06d1a655 411 }
48d0e5df 412 } else 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]);
1390456b 413 }
babdc001 414 else if ( i == 0 && j == 1 && opretlen == 149 )
d42d3348 415 {
2b654a4d 416 if ( notaryid >= 0 && notaryid < 64 )
d42d3348 417 komodo_paxpricefeed(height,&scriptbuf[len],opretlen);
418 }
eb406ebd 419 else
420 {
6806478b 421 int32_t k; for (k=0; k<scriptlen; k++)
422 printf("%02x",scriptbuf[k]);
9a3fc044 423 printf(" <- script ht.%d i.%d j.%d value %.8f %s\n",height,i,j,dstr(value),ASSETCHAINS_SYMBOL);
6dba48ec 424 if ( opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
425 {
5a286a6f 426 for (k=0; k<32; k++)
427 if ( scriptbuf[len+k] != 0 )
428 break;
429 if ( k == 32 )
6dba48ec 430 {
431 *isratificationp = 1;
432 printf("ISRATIFICATION (%s)\n",(char *)&scriptbuf[len+32*2+4]);
433 }
434 }
435 if ( *isratificationp == 0 )
e155dbe9 436 komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen,j);
eb406ebd 437 }
1390456b 438 }
439 return(notaryid);
440}
441
58033467 442/*int32_t komodo_isratify(int32_t isspecial,int32_t numvalid)
f38345e9 443{
755ead98 444 if ( isspecial != 0 && numvalid >= KOMODO_MINRATIFY )
f38345e9 445 return(1);
446 else return(0);
58033467 447}*/
f38345e9 448
449// Special tx have vout[0] -> CRYPTO777
755ead98 450// with more than KOMODO_MINRATIFY pay2pubkey outputs -> ratify
f38345e9 451// if all outputs to notary -> notary utxo
452// if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed
420d7311 453// OP_RETURN: 'D' -> deposit, 'W' -> withdraw
ab918767 454
6f3cb4d0 455int32_t gettxout_scriptPubKey(uint8_t *scriptPubKey,int32_t maxsize,uint256 txid,int32_t n);
456
457int32_t komodo_notarycmp(uint8_t *scriptPubKey,int32_t scriptlen,uint8_t pubkeys[64][33],int32_t numnotaries,uint8_t rmd160[20])
458{
459 int32_t i;
460 if ( scriptlen == 25 && memcmp(&scriptPubKey[3],rmd160,20) == 0 )
461 return(0);
462 else if ( scriptlen == 35 )
463 {
464 for (i=0; i<numnotaries; i++)
465 if ( memcmp(&scriptPubKey[1],pubkeys[i],33) == 0 )
466 return(i);
467 }
468 return(-1);
469}
f38345e9 470
651989c7 471void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
50027f06 472{
a7f0d3e4 473 static int32_t hwmheight;
9273e756 474 uint64_t signedmask,voutmask; char symbol[16],dest[16]; struct komodo_state *sp;
ab918767 475 uint8_t scriptbuf[4096],pubkeys[64][33],rmd160[20],scriptPubKey[35]; uint256 kmdtxid,zero,btctxid,txhash;
ea365a71 476 int32_t i,j,k,numnotaries,notarized,scriptlen,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
878d3961 477 memset(&zero,0,sizeof(zero));
c93dc546 478 komodo_init(pindex->nHeight);
d7227bf6 479 KOMODO_INITDONE = (uint32_t)time(NULL);
ab918767 480 if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
3df53390 481 {
482 fprintf(stderr,"unexpected null komodostateptr.[%s]\n",ASSETCHAINS_SYMBOL);
ab918767 483 return;
3df53390 484 }
7589f03a 485 numnotaries = komodo_notaries(pubkeys,pindex->nHeight);
6f3cb4d0 486 calc_rmd160_sha256(rmd160,pubkeys[0],33);
a7f0d3e4 487 if ( pindex->nHeight > hwmheight )
488 hwmheight = pindex->nHeight;
489 else
490 {
5aab0392 491 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);
ab918767 492 komodo_event_rewind(sp,symbol,pindex->nHeight);
e155dbe9 493 komodo_stateupdate(pindex->nHeight,0,0,0,zero,0,0,0,0,-pindex->nHeight,pindex->nTime,0,0,0,0);
a7f0d3e4 494 }
c75c18fc 495 komodo_currentheight_set(chainActive.Tip()->nHeight);
68916cc6 496 if ( pindex != 0 )
b501ded2 497 {
656eddcd 498 height = pindex->nHeight;
01cc012f 499 txn_count = block.vtx.size();
500 for (i=0; i<txn_count; i++)
dc64de68 501 {
352f8081 502 txhash = block.vtx[i].GetHash();
01cc012f 503 numvouts = block.vtx[i].vout.size();
352f8081 504 notaryid = -1;
ea365a71 505 voutmask = specialtx = notarizedheight = isratification = notarized = 0;
517b1691 506 signedmask = (height < 91400) ? 1 : 0;
ea365a71 507 numvins = block.vtx[i].vin.size();
508 for (j=0; j<numvins; j++)
509 {
69383c9f 510 if ( i == 0 && j == 0 )
511 continue;
ea365a71 512 if ( (scriptlen= gettxout_scriptPubKey(scriptPubKey,sizeof(scriptPubKey),block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) > 0 )
513 {
514 if ( (k= komodo_notarycmp(scriptPubKey,scriptlen,pubkeys,numnotaries,rmd160)) >= 0 )
515 signedmask |= (1LL << k);
c7918614 516 else if ( 0 && numvins >= 17 )
bb1963ca 517 {
518 int32_t k;
519 for (k=0; k<scriptlen; k++)
520 printf("%02x",scriptPubKey[k]);
57d9cfcc 521 printf(" scriptPubKey doesnt match any notary vini.%d of %d\n",j,numvins);
bb1963ca 522 }
523 } else printf("cant get scriptPubKey for ht.%d txi.%d vin.%d\n",height,i,j);
ea365a71 524 }
e6d7eca6 525 numvalid = bitweight(signedmask);
13aadf01 526 if ( (((height < 90000 || (signedmask & 1) != 0) && numvalid >= KOMODO_MINRATIFY) || numvalid > (numnotaries/4)) )
ea365a71 527 {
41f5997a 528 printf("%s ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d <<<<<<<<<<< notarized\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts);
ea365a71 529 notarized = 1;
530 }
01cc012f 531 for (j=0; j<numvouts; j++)
dc64de68 532 {
1390456b 533 len = block.vtx[i].vout[j].scriptPubKey.size();
534 if ( len <= sizeof(scriptbuf) )
01cc012f 535 {
189d9dee 536#ifdef KOMODO_ZCASH
484f8777 537 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
189d9dee 538#else
539 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
540#endif
8f40a7a2 541 notaryid = komodo_voutupdate(&isratification,notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,&notarizedheight,(uint64_t)block.vtx[i].vout[j].nValue,notarized,signedmask);
a01acef4 542 if ( 0 && i > 0 )
e69a0833 543 {
544 for (k=0; k<len; k++)
545 printf("%02x",scriptbuf[k]);
a8832194 546 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 547 }
01cc012f 548 }
352f8081 549 }
8985164d 550 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);
ea365a71 551 if ( notarized != 0 && (notarizedheight != 0 || specialtx != 0) )
1390456b 552 {
b338ea90 553 if ( isratification != 0 )
554 {
555 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);
556 printf("ht.%d specialtx.%d isratification.%d numvouts.%d signed.%llx numnotaries.%d\n",height,specialtx,isratification,numvouts,(long long)signedmask,numnotaries);
557 }
d2a115d8 558 if ( specialtx != 0 && isratification != 0 && numvouts > 2 )
84e843cd 559 {
29e69b85 560 numvalid = 0;
dd7b22ad 561 memset(pubkeys,0,sizeof(pubkeys));
6dba48ec 562 for (j=1; j<numvouts-1; j++)
84e843cd 563 {
564 len = block.vtx[i].vout[j].scriptPubKey.size();
565 if ( len <= sizeof(scriptbuf) )
566 {
189d9dee 567#ifdef KOMODO_ZCASH
484f8777 568 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
189d9dee 569#else
570 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
571#endif
84e843cd 572 if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
573 {
29e69b85 574 memcpy(pubkeys[numvalid++],scriptbuf+1,33);
e7f99312 575 for (k=0; k<33; k++)
84e843cd 576 printf("%02x",scriptbuf[k+1]);
577 printf(" <- new notary.[%d]\n",j-1);
578 }
579 }
580 }
aa114a60 581 if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries/3) )
d8ce705a 582 {
583 memset(&txhash,0,sizeof(txhash));
e155dbe9 584 komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0,0);
f9d034d4 585 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);
6423c8a9 586 } else printf("signedmask.%llx numvalid.%d wt.%d numnotaries.%d\n",(long long)signedmask,numvalid,bitweight(signedmask),numnotaries);
84e843cd 587 }
a96439f5 588 }
656eddcd 589 }
e155dbe9 590 if ( pindex->nHeight == hwmheight )
8afdc8e7 591 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,height,(uint32_t)pindex->nTime,0,0,0,0);
3df53390 592 } else fprintf(stderr,"komodo_connectblock: unexpected null pindex\n");
17899df4 593 //KOMODO_INITDONE = (uint32_t)time(NULL);
3d35aa5b 594}
595
0f24f245 596
fcd36118 597#endif
This page took 0.27843 seconds and 4 git commands to generate.