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