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