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