]> 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:
20// 1. error check fiat redeem amounts
4a4e912b 21// 2. net balance limiter
09e3cf94 22// 3. verify: reorgs
3ec03ada 23
24// non komodod (non-hardfork) todo:
25// a. automate notarization fee payouts
26// b. automated distribution of test REVS snapshot
3eea72f2 27
9499e4de 28#include <stdint.h>
29#include <stdio.h>
975b2ddb 30#include <pthread.h>
88f973c2 31#include <ctype.h>
6d5cb6d4 32#include "uthash.h"
33#include "utlist.h"
9499e4de 34
fc318ffe 35int32_t gettxout_scriptPubKey(uint8_t *scriptPubkey,int32_t maxsize,uint256 txid,int32_t n);
f3a1de3a 36void komodo_event_rewind(struct komodo_state *sp,char *symbol,int32_t height);
875cf683 37void komodo_connectblock(CBlockIndex *pindex,CBlock& block);
3face669 38
fc318ffe 39#include "komodo_structs.h"
9fb37168 40#include "komodo_globals.h"
46beb55e 41#include "komodo_utils.h"
1bfdde1d 42
e4e76989 43#include "cJSON.c"
44#include "komodo_bitcoind.h"
45#include "komodo_interest.h"
46#include "komodo_pax.h"
47#include "komodo_notary.h"
ab918767 48
49int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest);
e4e76989 50#include "komodo_gateway.h"
51#include "komodo_events.h"
52
c75c18fc 53void komodo_currentheight_set(int32_t height)
54{
55 char symbol[16],dest[16]; struct komodo_state *sp;
56 if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
57 sp->CURRENT_HEIGHT = height;
58}
59
60int32_t komodo_currentheight()
61{
62 char symbol[16],dest[16]; struct komodo_state *sp;
63 if ( (sp= komodo_stateptr(symbol,dest)) != 0 )
64 return(sp->CURRENT_HEIGHT);
65 else return(0);
66}
67
68int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest)
69{
70 static int32_t errs;
2df2f193 71 int32_t func,ht,notarized_height,num,matched=0; uint256 notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33];
c75c18fc 72 if ( (func= fgetc(fp)) != EOF )
73 {
74 if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 )
75 matched = 1;
76 else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0);
77 if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) )
78 errs++;
79 //printf("fpos.%ld func.(%d %c) ht.%d ",ftell(fp),func,func,ht);
80 if ( func == 'P' )
81 {
f3a1de3a 82 if ( (num= fgetc(fp)) <= 64 )
c75c18fc 83 {
84 if ( fread(pubkeys,33,num,fp) != num )
85 errs++;
86 else
87 {
15724d02 88 printf("updated %d pubkeys at %s ht.%d\n",num,symbol,ht);
89 if ( (KOMODO_EXTERNAL_NOTARIES != 0 && matched != 0) || (strcmp(symbol,"KMD") == 0 && KOMODO_EXTERNAL_NOTARIES == 0) )
c75c18fc 90 komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys);
91 }
92 } else printf("illegal num.%d\n",num);
93 }
94 else if ( func == 'N' )
95 {
96 if ( fread(&notarized_height,1,sizeof(notarized_height),fp) != sizeof(notarized_height) )
97 errs++;
98 if ( fread(&notarized_hash,1,sizeof(notarized_hash),fp) != sizeof(notarized_hash) )
99 errs++;
100 if ( fread(&notarized_desttxid,1,sizeof(notarized_desttxid),fp) != sizeof(notarized_desttxid) )
101 errs++;
ea3aa592 102 //printf("%s load[%s] NOTARIZED %d %s\n",ASSETCHAINS_SYMBOL,symbol,notarized_height,notarized_hash.ToString().c_str());
1ebb14e7 103 //if ( matched != 0 ) global independent states -> inside *sp
104 komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height);
c75c18fc 105 }
1ebb14e7 106 else if ( func == 'U' ) // deprecated
c75c18fc 107 {
108 uint8_t n,nid; uint256 hash; uint64_t mask;
109 n = fgetc(fp);
110 nid = fgetc(fp);
111 //printf("U %d %d\n",n,nid);
112 if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) )
113 errs++;
114 if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) )
115 errs++;
6f3cb4d0 116 //if ( matched != 0 )
117 // komodo_eventadd_utxo(sp,symbol,ht,nid,hash,mask,n);
c75c18fc 118 }
119 else if ( func == 'K' )
120 {
121 int32_t kheight;
122 if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
123 errs++;
1ebb14e7 124 //if ( matched != 0 ) global independent states -> inside *sp
a8f4e85e 125 //printf("%s.%d load[%s] ht.%d\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight);
e155dbe9 126 komodo_eventadd_kmdheight(sp,symbol,ht,kheight,0);
127 }
128 else if ( func == 'T' )
129 {
130 int32_t kheight,ktimestamp;
131 if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) )
132 errs++;
133 if ( fread(&ktimestamp,1,sizeof(ktimestamp),fp) != sizeof(ktimestamp) )
134 errs++;
135 //if ( matched != 0 ) global independent states -> inside *sp
a8f4e85e 136 //printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp);
e155dbe9 137 komodo_eventadd_kmdheight(sp,symbol,ht,kheight,ktimestamp);
c75c18fc 138 }
139 else if ( func == 'R' )
140 {
141 uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[10000];
142 if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) )
143 errs++;
144 if ( fread(&v,1,sizeof(v),fp) != sizeof(v) )
145 errs++;
146 if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) )
147 errs++;
148 if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) )
149 errs++;
150 if ( olen < sizeof(opret) )
151 {
152 if ( fread(opret,1,olen,fp) != olen )
153 errs++;
1ebb14e7 154 //if ( matched != 0 ) global shared state -> global PAX
a732fc25 155 int32_t i; for (i=0; i<olen; i++)
9f33e176 156 printf("%02x",opret[i]);
157 printf(" %s load[%s] opret[%c] len.%d %.8f\n",ASSETCHAINS_SYMBOL,symbol,opret[0],olen,(double)ovalue/COIN);
15724d02 158 komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen);
c75c18fc 159 } else printf("illegal olen.%u\n",olen);
160 }
161 else if ( func == 'D' )
162 {
163 printf("unexpected function D[%d]\n",ht);
164 }
165 else if ( func == 'V' )
166 {
167 int32_t numpvals; uint32_t pvals[128];
168 numpvals = fgetc(fp);
169 if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals )
170 {
1ebb14e7 171 //if ( matched != 0 ) global shared state -> global PVALS
335f2ea7 172 //printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht);
15724d02 173 komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals);
c75c18fc 174 //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals);
175 } else printf("error loading pvals[%d]\n",numpvals);
176 }
177 else printf("illegal func.(%d %c)\n",func,func);
178 return(func);
179 } else return(-1);
180}
181
e155dbe9 182void 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 183{
c75c18fc 184 static FILE *fp; static int32_t errs;
e21ffa1d 185 struct komodo_state *sp; char fname[512],symbol[16],dest[16]; int32_t ht,func; uint8_t num,pubkeys[64][33];
c75c18fc 186 if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
187 return;
888cf827 188 if ( fp == 0 )
189 {
055db9b6 190 komodo_statefname(fname,ASSETCHAINS_SYMBOL,(char *)"komodostate");
bafc61f3 191 if ( (fp= fopen(fname,"rb+")) != 0 )
192 {
c75c18fc 193 while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
194 ;
bafc61f3 195 } else fp = fopen(fname,"wb+");
196 printf("fname.(%s) fpos.%ld\n",fname,ftell(fp));
63c30610 197 KOMODO_INITDONE = (uint32_t)time(NULL);
bafc61f3 198 }
e98e2aec 199 if ( height <= 0 )
49df008c 200 {
6458297e 201 //printf("early return: stateupdate height.%d\n",height);
49df008c 202 return;
203 }
3eea72f2 204 if ( fp != 0 ) // write out funcid, height, other fields, call side effect function
bafc61f3 205 {
2578b002 206 //printf("fpos.%ld ",ftell(fp));
ab918767 207 if ( KMDheight != 0 )
2b84e06c 208 {
e155dbe9 209 if ( KMDtimestamp != 0 )
210 {
211 fputc('T',fp);
212 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
213 errs++;
214 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
215 errs++;
216 if ( fwrite(&KMDtimestamp,1,sizeof(KMDtimestamp),fp) != sizeof(KMDtimestamp) )
217 errs++;
218 }
219 else
220 {
221 fputc('K',fp);
222 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
223 errs++;
224 if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) )
225 errs++;
226 }
227 komodo_eventadd_kmdheight(sp,symbol,height,KMDheight,KMDtimestamp);
2b84e06c 228 }
01f9cb3d 229 else if ( opretbuf != 0 && opretlen > 0 )
64bb0834 230 {
01f9cb3d 231 uint16_t olen = opretlen;
232 fputc('R',fp);
64bb0834 233 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
234 errs++;
429dabb5 235 if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
236 errs++;
237 if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) )
238 errs++;
30df2e00 239 if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) )
33935bd0 240 errs++;
289dc57b 241 if ( fwrite(&olen,1,sizeof(olen),fp) != olen )
242 errs++;
01f9cb3d 243 if ( fwrite(opretbuf,1,olen,fp) != olen )
64bb0834 244 errs++;
1938746c 245 //printf("ht.%d R opret[%d]\n",height,olen);
c75c18fc 246 //komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout);
247 komodo_eventadd_opreturn(sp,symbol,height,txhash,opretvalue,vout,opretbuf,olen);
01f9cb3d 248 }
9997caa0 249 else if ( notarypubs != 0 && numnotaries > 0 )
bafc61f3 250 {
f3a1de3a 251 printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs);
bafc61f3 252 fputc('P',fp);
c50ec0fb 253 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
254 errs++;
bafc61f3 255 fputc(numnotaries,fp);
52ed4002 256 if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries )
257 errs++;
c75c18fc 258 komodo_eventadd_pubkeys(sp,symbol,height,numnotaries,notarypubs);
bafc61f3 259 }
671187dd 260 else if ( voutmask != 0 && numvouts > 0 )
261 {
2578b002 262 //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash));
671187dd 263 fputc('U',fp);
c50ec0fb 264 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
265 errs++;
671187dd 266 fputc(numvouts,fp);
267 fputc(notaryid,fp);
268 if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) )
269 errs++;
270 if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) )
271 errs++;
6f3cb4d0 272 //komodo_eventadd_utxo(sp,symbol,height,notaryid,txhash,voutmask,numvouts);
671187dd 273 }
b673d264 274//#ifdef KOMODO_PAX
4a41b0b2 275 else if ( pvals != 0 && numpvals > 0 )
276 {
bfa5b4f2 277 int32_t i,nonz = 0;
1bf82154 278 for (i=0; i<32; i++)
279 if ( pvals[i] != 0 )
280 nonz++;
281 if ( nonz >= 32 )
282 {
283 fputc('V',fp);
284 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
285 errs++;
286 fputc(numpvals,fp);
287 if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals )
288 errs++;
c75c18fc 289 komodo_eventadd_pricefeed(sp,symbol,height,pvals,numpvals);
2578b002 290 //printf("ht.%d V numpvals[%d]\n",height,numpvals);
1bf82154 291 }
ef7a3548 292 //printf("save pvals height.%d numpvals.%d\n",height,numpvals);
4a41b0b2 293 }
b673d264 294//#endif
6dabcca4 295 else if ( height != 0 )
671187dd 296 {
2578b002 297 //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs);
671187dd 298 fputc('N',fp);
c50ec0fb 299 if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) )
300 errs++;
c75c18fc 301 if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) )
671187dd 302 errs++;
c75c18fc 303 if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) )
671187dd 304 errs++;
c75c18fc 305 if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) )
671187dd 306 errs++;
c75c18fc 307 komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT);
308 //komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID);
671187dd 309 }
47f47733 310 fflush(fp);
bafc61f3 311 }
312}
313
8f40a7a2 314int32_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 315{
2df2f193 316 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];
317 if ( (sp= komodo_stateptr(symbol,dest)) == 0 )
318 return(-1);
9c406099 319 if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
1390456b 320 {
c23dd601 321 decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR);
986d3459 322 /*for (k=0; k<33; k++)
e1be360e 323 printf("%02x",crypto777[k]);
324 printf(" crypto777 ");
325 for (k=0; k<scriptlen; k++)
326 printf("%02x",scriptbuf[k]);
986d3459 327 printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/
1390456b 328 if ( memcmp(crypto777,scriptbuf+1,33) == 0 )
329 {
330 *specialtxp = 1;
b7f942c8 331 //printf(">>>>>>>> ");
1390456b 332 }
a5f315c7 333 else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 )
1390456b 334 {
84e843cd 335 //printf("found notary.k%d\n",k);
336 if ( notaryid < 64 )
1390456b 337 {
84e843cd 338 if ( notaryid < 0 )
339 {
a5f315c7 340 notaryid = nid;
84e843cd 341 *voutmaskp |= (1LL << j);
342 }
a5f315c7 343 else if ( notaryid != nid )
84e843cd 344 {
b7f942c8 345 //for (i=0; i<33; i++)
346 // printf("%02x",scriptbuf[i+1]);
347 //printf(" %s mismatch notaryid.%d k.%d\n",ASSETCHAINS_SYMBOL,notaryid,nid);
84e843cd 348 notaryid = 64;
349 *voutmaskp = 0;
350 }
351 else *voutmaskp |= (1LL << j);
1390456b 352 }
1390456b 353 }
354 }
844a6138 355 if ( scriptbuf[len++] == 0x6a )
1390456b 356 {
357 if ( (opretlen= scriptbuf[len++]) == 0x4c )
358 opretlen = scriptbuf[len++];
359 else if ( opretlen == 0x4d )
360 {
361 opretlen = scriptbuf[len++];
362 opretlen = (opretlen << 8) + scriptbuf[len++];
363 }
a6400079 364 if ( j == 1 && opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 )
1390456b 365 {
4a41b0b2 366 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid);
367 len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp);
3eea72f2 368 len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid);
2cc7e8de 369 if ( notarized != 0 && *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height )
1390456b 370 {
1ebb14e7 371 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 372 sp->NOTARIZED_HEIGHT = *notarizedheightp;
373 sp->NOTARIZED_HASH = kmdtxid;
374 sp->NOTARIZED_DESTTXID = desttxid;
e155dbe9 375 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0,0);
a4c67285 376 if ( opretlen > len && scriptbuf[len] == 'A' )
377 komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen-len,j);
8f40a7a2 378 } else printf("notarized.%d %llx reject ht.%d NOTARIZED.%d %s.%s DESTTXID.%s (%s)\n",notarized,(long long)signedmask,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;
ea365a71 439 int32_t i,j,k,numnotaries,notarized,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 }*/
550824a8 463 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;
ea365a71 473 voutmask = specialtx = notarizedheight = isratification = notarized = 0;
474 signedmask = 0;
475 numvins = block.vtx[i].vin.size();
476 for (j=0; j<numvins; j++)
477 {
478 if ( (scriptlen= gettxout_scriptPubKey(scriptPubKey,sizeof(scriptPubKey),block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) > 0 )
479 {
480 if ( (k= komodo_notarycmp(scriptPubKey,scriptlen,pubkeys,numnotaries,rmd160)) >= 0 )
481 signedmask |= (1LL << k);
482 }
483 }
e6d7eca6 484 numvalid = bitweight(signedmask);
30a4262a 485 if ( (((height < 76000 || (signedmask & 1) != 0) && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries>>1)) )
ea365a71 486 {
fd0c033f 487 printf("%s ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d <<<<<<<<<<< notarized\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts);
ea365a71 488 notarized = 1;
489 }
01cc012f 490 for (j=0; j<numvouts; j++)
dc64de68 491 {
1390456b 492 len = block.vtx[i].vout[j].scriptPubKey.size();
493 if ( len <= sizeof(scriptbuf) )
01cc012f 494 {
189d9dee 495#ifdef KOMODO_ZCASH
484f8777 496 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
189d9dee 497#else
498 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
499#endif
8f40a7a2 500 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 501 if ( 0 && i > 0 )
e69a0833 502 {
503 for (k=0; k<len; k++)
504 printf("%02x",scriptbuf[k]);
a8832194 505 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 506 }
01cc012f 507 }
352f8081 508 }
ea365a71 509 if ( notarized != 0 && (notarizedheight != 0 || specialtx != 0) )
1390456b 510 {
ce169ae3 511 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 512 printf("ht.%d specialtx.%d isratification.%d numvouts.%d signed.%llx numnotaries.%d\n",height,specialtx,isratification,numvouts,(long long)signedmask,numnotaries);
d2a115d8 513 if ( specialtx != 0 && isratification != 0 && numvouts > 2 )
84e843cd 514 {
29e69b85 515 numvalid = 0;
dd7b22ad 516 memset(pubkeys,0,sizeof(pubkeys));
6dba48ec 517 for (j=1; j<numvouts-1; j++)
84e843cd 518 {
519 len = block.vtx[i].vout[j].scriptPubKey.size();
520 if ( len <= sizeof(scriptbuf) )
521 {
189d9dee 522#ifdef KOMODO_ZCASH
484f8777 523 memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len);
189d9dee 524#else
525 memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len);
526#endif
84e843cd 527 if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac )
528 {
29e69b85 529 memcpy(pubkeys[numvalid++],scriptbuf+1,33);
e7f99312 530 for (k=0; k<33; k++)
84e843cd 531 printf("%02x",scriptbuf[k+1]);
532 printf(" <- new notary.[%d]\n",j-1);
533 }
534 }
535 }
7589f03a 536 if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries>>1) )
d8ce705a 537 {
538 memset(&txhash,0,sizeof(txhash));
e155dbe9 539 komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0,0);
f9d034d4 540 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 541 } else printf("signedmask.%llx numvalid.%d wt.%d numnotaries.%d\n",(long long)signedmask,numvalid,bitweight(signedmask),numnotaries);
84e843cd 542 }
a96439f5 543 }
656eddcd 544 }
e155dbe9 545 if ( pindex->nHeight == hwmheight )
8afdc8e7 546 komodo_stateupdate(height,0,0,0,zero,0,0,0,0,height,(uint32_t)pindex->nTime,0,0,0,0);
44c4fbbd 547 } else printf("komodo_connectblock: unexpected null pindex\n");
63c30610 548 KOMODO_INITDONE = (uint32_t)time(NULL);
3d35aa5b 549}
550
0f24f245 551
fcd36118 552#endif
This page took 0.187905 seconds and 4 git commands to generate.