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