]> 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++;
ea3aa592 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
5633157a 126 printf("%s.%d load[%s] ht.%d\n",ASSETCHAINS_SYMBOL,ht,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
5633157a 137 printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp);
e155dbe9 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
a732fc25 156 int32_t i; for (i=0; i<olen; i++)
9f33e176 157 printf("%02x",opret[i]);
158 printf(" %s load[%s] opret[%c] len.%d %.8f\n",ASSETCHAINS_SYMBOL,symbol,opret[0],olen,(double)ovalue/COIN);
15724d02 159 komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen);
c75c18fc 160 } else printf("illegal olen.%u\n",olen);
161 }
162 else if ( func == 'D' )
163 {
164 printf("unexpected function D[%d]\n",ht);
165 }
166 else if ( func == 'V' )
167 {
168 int32_t numpvals; uint32_t pvals[128];
169 numpvals = fgetc(fp);
170 if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals )
171 {
1ebb14e7 172 //if ( matched != 0 ) global shared state -> global PVALS
335f2ea7 173 //printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht);
15724d02 174 komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals);
c75c18fc 175 //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
176 } else printf("error loading pvals[%d]\n",numpvals);
177 }
178 else printf("illegal func.(%d %c)\n",func,func);
179 return(func);
180 } else return(-1);
181}
182
e155dbe9 183void 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 184{
c75c18fc 185 static FILE *fp; static int32_t errs;
e21ffa1d 186 struct komodo_state *sp; char fname[512],symbol[16],dest[16]; int32_t ht,func; uint8_t num,pubkeys[64][33];
c75c18fc 187 if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
188 return;
888cf827 189 if ( fp == 0 )
190 {
ab918767 191 komodo_statefname(fname,ASSETCHAINS_SYMBOL);
bafc61f3 192 if ( (fp= fopen(fname,"rb+")) != 0 )
193 {
c75c18fc 194 while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
195 ;
bafc61f3 196 } else fp = fopen(fname,"wb+");
197 printf("fname.(%s) fpos.%ld\n",fname,ftell(fp));
63c30610 198 KOMODO_INITDONE = (uint32_t)time(NULL);
bafc61f3 199 }
e98e2aec 200 if ( height <= 0 )
49df008c 201 {
6458297e 202 //printf("early return: stateupdate height.%d\n",height);
49df008c 203 return;
204 }
3eea72f2 205 if ( fp != 0 ) // write out funcid, height, other fields, call side effect function
bafc61f3 206 {
2578b002 207 //printf("fpos.%ld ",ftell(fp));
ab918767 208 if ( KMDheight != 0 )
2b84e06c 209 {
e155dbe9 210 if ( KMDtimestamp != 0 )
211 {
212 fputc('T',fp);
213 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
214 errs++;
215 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
216 errs++;
217 if ( fwrite(&KMDtimestamp,1,sizeof(KMDtimestamp),fp) != sizeof(KMDtimestamp) )
218 errs++;
219 }
220 else
221 {
222 fputc('K',fp);
223 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
224 errs++;
225 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
226 errs++;
227 }
228 komodo_eventadd_kmdheight(sp,symbol,height,KMDheight,KMDtimestamp);
2b84e06c 229 }
01f9cb3d 230 else if ( opretbuf != 0 && opretlen > 0 )
64bb0834 231 {
01f9cb3d 232 uint16_t olen = opretlen;
233 fputc('R',fp);
64bb0834 234 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
235 errs++;
429dabb5 236 if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
237 errs++;
238 if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) )
239 errs++;
30df2e00 240 if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) )
33935bd0 241 errs++;
289dc57b 242 if ( fwrite(&olen,1,sizeof(olen),fp) != olen )
243 errs++;
01f9cb3d 244 if ( fwrite(opretbuf,1,olen,fp) != olen )
64bb0834 245 errs++;
1938746c 246 //printf("ht.%d R opret[%d]\n",height,olen);
c75c18fc 247 //komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout);
248 komodo_eventadd_opreturn(sp,symbol,height,txhash,opretvalue,vout,opretbuf,olen);
01f9cb3d 249 }
9997caa0 250 else if ( notarypubs != 0 && numnotaries > 0 )
bafc61f3 251 {
f3a1de3a 252 printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs);
bafc61f3 253 fputc('P',fp);
c50ec0fb 254 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
255 errs++;
bafc61f3 256 fputc(numnotaries,fp);
52ed4002 257 if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries )
258 errs++;
c75c18fc 259 komodo_eventadd_pubkeys(sp,symbol,height,numnotaries,notarypubs);
bafc61f3 260 }
671187dd 261 else if ( voutmask != 0 && numvouts > 0 )
262 {
2578b002 263 //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash));
671187dd 264 fputc('U',fp);
c50ec0fb 265 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
266 errs++;
671187dd 267 fputc(numvouts,fp);
268 fputc(notaryid,fp);
269 if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) )
270 errs++;
271 if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
272 errs++;
6f3cb4d0 273 //komodo_eventadd_utxo(sp,symbol,height,notaryid,txhash,voutmask,numvouts);
671187dd 274 }
b673d264 275//#ifdef KOMODO_PAX
4a41b0b2 276 else if ( pvals != 0 && numpvals > 0 )
277 {
bfa5b4f2 278 int32_t i,nonz = 0;
1bf82154 279 for (i=0; i<32; i++)
280 if ( pvals[i] != 0 )
281 nonz++;
282 if ( nonz >= 32 )
283 {
284 fputc('V',fp);
285 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
286 errs++;
287 fputc(numpvals,fp);
288 if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals )
289 errs++;
c75c18fc 290 komodo_eventadd_pricefeed(sp,symbol,height,pvals,numpvals);
2578b002 291 //printf("ht.%d V numpvals[%d]\n",height,numpvals);
1bf82154 292 }
ef7a3548 293 //printf("save pvals height.%d numpvals.%d\n",height,numpvals);
4a41b0b2 294 }
b673d264 295//#endif
6dabcca4 296 else if ( height != 0 )
671187dd 297 {
2578b002 298 //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs);
671187dd 299 fputc('N',fp);
c50ec0fb 300 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
301 errs++;
c75c18fc 302 if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) )
671187dd 303 errs++;
c75c18fc 304 if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) )
671187dd 305 errs++;
c75c18fc 306 if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) )
671187dd 307 errs++;
c75c18fc 308 komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT);
309 //komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID);
671187dd 310 }
47f47733 311 fflush(fp);
bafc61f3 312 }
313}
314
6dba48ec 315int32_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 316{
2df2f193 317 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];
318 if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
319 return(-1);
9c406099 320 if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
1390456b 321 {
c23dd601 322 decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR);
986d3459 323 /*for (k=0; k<33; k++)
e1be360e 324 printf("%02x",crypto777[k]);
325 printf(" crypto777 ");
326 for (k=0; k<scriptlen; k++)
327 printf("%02x",scriptbuf[k]);
986d3459 328 printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/
1390456b 329 if ( memcmp(crypto777,scriptbuf+1,33) == 0 )
330 {
331 *specialtxp = 1;
b7f942c8 332 //printf(">>>>>>>> ");
1390456b 333 }
a5f315c7 334 else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 )
1390456b 335 {
84e843cd 336 //printf("found notary.k%d\n",k);
337 if ( notaryid < 64 )
1390456b 338 {
84e843cd 339 if ( notaryid < 0 )
340 {
a5f315c7 341 notaryid = nid;
84e843cd 342 *voutmaskp |= (1LL << j);
343 }
a5f315c7 344 else if ( notaryid != nid )
84e843cd 345 {
b7f942c8 346 //for (i=0; i<33; i++)
347 // printf("%02x",scriptbuf[i+1]);
348 //printf(" %s mismatch notaryid.%d k.%d\n",ASSETCHAINS_SYMBOL,notaryid,nid);
84e843cd 349 notaryid = 64;
350 *voutmaskp = 0;
351 }
352 else *voutmaskp |= (1LL << j);
1390456b 353 }
1390456b 354 }
355 }
844a6138 356 if ( scriptbuf[len++] == 0x6a )
1390456b 357 {
358 if ( (opretlen= scriptbuf[len++]) == 0x4c )
359 opretlen = scriptbuf[len++];
360 else if ( opretlen == 0x4d )
361 {
362 opretlen = scriptbuf[len++];
363 opretlen = (opretlen << 8) + scriptbuf[len++];
364 }
a6400079 365 if ( j == 1 && opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
1390456b 366 {
4a41b0b2 367 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
368 len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp);
3eea72f2 369 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid);
c75c18fc 370 if ( *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height )
1390456b 371 {
1ebb14e7 372 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 373 sp->NOTARIZED_HEIGHT = *notarizedheightp;
374 sp->NOTARIZED_HASH = kmdtxid;
375 sp->NOTARIZED_DESTTXID = desttxid;
e155dbe9 376 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0,0);
ab918767 377 // extract X opreturns here
a6400079 378 } 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 379 }
babdc001 380 else if ( i == 0 && j == 1 && opretlen == 149 )
0201d1a6 381 komodo_paxpricefeed(height,&scriptbuf[len],opretlen);
eb406ebd 382 else
383 {
6806478b 384 int32_t k; for (k=0; k<scriptlen; k++)
385 printf("%02x",scriptbuf[k]);
9a3fc044 386 printf(" <- script ht.%d i.%d j.%d value %.8f %s\n",height,i,j,dstr(value),ASSETCHAINS_SYMBOL);
6dba48ec 387 if ( opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
388 {
5a286a6f 389 for (k=0; k<32; k++)
390 if ( scriptbuf[len+k] != 0 )
391 break;
392 if ( k == 32 )
6dba48ec 393 {
394 *isratificationp = 1;
395 printf("ISRATIFICATION (%s)\n",(char *)&scriptbuf[len+32*2+4]);
396 }
397 }
398 if ( *isratificationp == 0 )
e155dbe9 399 komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen,j);
eb406ebd 400 }
1390456b 401 }
402 return(notaryid);
403}
404
f38345e9 405int32_t komodo_isratify(int32_t isspecial,int32_t numvalid)
406{
755ead98 407 if ( isspecial != 0 && numvalid >= KOMODO_MINRATIFY )
f38345e9 408 return(1);
409 else return(0);
410}
411
412// Special tx have vout[0] -> CRYPTO777
755ead98 413// with more than KOMODO_MINRATIFY pay2pubkey outputs -> ratify
f38345e9 414// if all outputs to notary -> notary utxo
415// if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed
420d7311 416// OP_RETURN: 'D' -> deposit, 'W' -> withdraw
ab918767 417
6f3cb4d0 418int32_t gettxout_scriptPubKey(uint8_t *scriptPubKey,int32_t maxsize,uint256 txid,int32_t n);
419
420int32_t komodo_notarycmp(uint8_t *scriptPubKey,int32_t scriptlen,uint8_t pubkeys[64][33],int32_t numnotaries,uint8_t rmd160[20])
421{
422 int32_t i;
423 if ( scriptlen == 25 && memcmp(&scriptPubKey[3],rmd160,20) == 0 )
424 return(0);
425 else if ( scriptlen == 35 )
426 {
427 for (i=0; i<numnotaries; i++)
428 if ( memcmp(&scriptPubKey[1],pubkeys[i],33) == 0 )
429 return(i);
430 }
431 return(-1);
432}
f38345e9 433
651989c7 434void komodo_connectblock(CBlockIndex *pindex,CBlock& block)
50027f06 435{
a7f0d3e4 436 static int32_t hwmheight;
9273e756 437 uint64_t signedmask,voutmask; char symbol[16],dest[16]; struct komodo_state *sp;
ab918767 438 uint8_t scriptbuf[4096],pubkeys[64][33],rmd160[20],scriptPubKey[35]; uint256 kmdtxid,zero,btctxid,txhash;
6f3cb4d0 439 int32_t i,j,k,numnotaries,scriptlen,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count;
878d3961 440 memset(&zero,0,sizeof(zero));
c93dc546 441 komodo_init(pindex->nHeight);
ab918767 442 if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
443 return;
7589f03a 444 numnotaries = komodo_notaries(pubkeys,pindex->nHeight);
6f3cb4d0 445 calc_rmd160_sha256(rmd160,pubkeys[0],33);
a7f0d3e4 446 if ( pindex->nHeight > hwmheight )
447 hwmheight = pindex->nHeight;
448 else
449 {
8afdc8e7 450 printf("hwmheight.%d vs pindex->nHeight.%d t.%u reorg.%d\n",hwmheight,pindex->nHeight,(uint32_t)pindex->nTime,hwmheight-pindex->nHeight);
ab918767 451 komodo_event_rewind(sp,symbol,pindex->nHeight);
e155dbe9 452 komodo_stateupdate(pindex->nHeight,0,0,0,zero,0,0,0,0,-pindex->nHeight,pindex->nTime,0,0,0,0);
a7f0d3e4 453 }
c75c18fc 454 komodo_currentheight_set(chainActive.Tip()->nHeight);
ab918767 455 /*if ( komodo_is_issuer() != 0 )
2d657c5b 456 {
f67800d0 457 while ( KOMODO_REALTIME == 0 || time(NULL) <= KOMODO_REALTIME )
2d657c5b 458 {
1fdfe03c 459 fprintf(stderr,"komodo_connect.(%s) waiting for realtime RT.%u now.%u\n",ASSETCHAINS_SYMBOL,KOMODO_REALTIME,(uint32_t)time(NULL));
cc66cf72 460 sleep(30);
2d657c5b 461 }
ab918767 462 }*/
463 sp->KOMODO_REALTIME = KOMODO_INITDONE = (uint32_t)time(NULL);
68916cc6 464 if ( pindex != 0 )
b501ded2 465 {
656eddcd 466 height = pindex->nHeight;
01cc012f 467 txn_count = block.vtx.size();
468 for (i=0; i<txn_count; i++)
dc64de68 469 {
352f8081 470 txhash = block.vtx[i].GetHash();
01cc012f 471 numvouts = block.vtx[i].vout.size();
352f8081 472 notaryid = -1;
6dba48ec 473 voutmask = specialtx = notarizedheight = isratification = 0;
01cc012f 474 for (j=0; j<numvouts; j++)
dc64de68 475 {
1390456b 476 len = block.vtx[i].vout[j].scriptPubKey.size();
477 if ( len <= sizeof(scriptbuf) )
01cc012f 478 {
189d9dee 479#ifdef KOMODO_ZCASH
484f8777 480 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
189d9dee 481#else
482 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
483#endif
6dba48ec 484 notaryid = komodo_voutupdate(&isratification,notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,&notarizedheight,(uint64_t)block.vtx[i].vout[j].nValue);
a01acef4 485 if ( 0 && i > 0 )
e69a0833 486 {
487 for (k=0; k<len; k++)
488 printf("%02x",scriptbuf[k]);
a8832194 489 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 490 }
01cc012f 491 }
352f8081 492 }
1390456b 493 signedmask = 0;
494 numvins = block.vtx[i].vin.size();
5bdebffe 495 for (j=0; j<numvins; j++)
352f8081 496 {
6f3cb4d0 497 if ( (scriptlen= gettxout_scriptPubKey(scriptPubKey,sizeof(scriptPubKey),block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) > 0 )
498 {
499 if ( (k= komodo_notarycmp(scriptPubKey,scriptlen,pubkeys,numnotaries,rmd160)) >= 0 )
500 signedmask |= (1LL << k);
501 }
1390456b 502 }
be7e8999 503 if ( 0 && signedmask != 0 )
ce169ae3 504 printf("%s ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts);
06f0ed43 505 if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) )
1390456b 506 {
ce169ae3 507 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);
7d51d973 508 printf("ht.%d specialtx.%d isratification.%d numvouts.%d signed.%llx numnotaries.%d\n",height,specialtx,isratification,numvouts,(long long)signedmask,numnotaries);
d2a115d8 509 if ( specialtx != 0 && isratification != 0 && numvouts > 2 )
84e843cd 510 {
29e69b85 511 numvalid = 0;
dd7b22ad 512 memset(pubkeys,0,sizeof(pubkeys));
6dba48ec 513 for (j=1; j<numvouts-1; j++)
84e843cd 514 {
515 len = block.vtx[i].vout[j].scriptPubKey.size();
516 if ( len <= sizeof(scriptbuf) )
517 {
189d9dee 518#ifdef KOMODO_ZCASH
484f8777 519 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
189d9dee 520#else
521 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
522#endif
84e843cd 523 if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
524 {
29e69b85 525 memcpy(pubkeys[numvalid++],scriptbuf+1,33);
e7f99312 526 for (k=0; k<33; k++)
84e843cd 527 printf("%02x",scriptbuf[k+1]);
528 printf(" <- new notary.[%d]\n",j-1);
529 }
530 }
531 }
7589f03a 532 if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries>>1) )
d8ce705a 533 {
534 memset(&txhash,0,sizeof(txhash));
e155dbe9 535 komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0,0);
f9d034d4 536 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 537 } else printf("signedmask.%llx numvalid.%d wt.%d numnotaries.%d\n",(long long)signedmask,numvalid,bitweight(signedmask),numnotaries);
84e843cd 538 }
a96439f5 539 }
656eddcd 540 }
e155dbe9 541 if ( pindex->nHeight == hwmheight )
8afdc8e7 542 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,height,(uint32_t)pindex->nTime,0,0,0,0);
44c4fbbd 543 } else printf("komodo_connectblock: unexpected null pindex\n");
63c30610 544 KOMODO_INITDONE = (uint32_t)time(NULL);
3d35aa5b 545}
546
0f24f245 547
fcd36118 548#endif
This page took 0.22965 seconds and 4 git commands to generate.