]>
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: |
3ec03ada | 20 | // 0. optimize assetchains |
27bf3c5e | 21 | // 1. error check fiat redeem amounts |
4a4e912b | 22 | // 2. net balance limiter |
09e3cf94 | 23 | // 3. verify: reorgs |
3ec03ada | 24 | |
25 | // non komodod (non-hardfork) todo: | |
26 | // a. automate notarization fee payouts | |
27 | // b. automated distribution of test REVS snapshot | |
3eea72f2 | 28 | |
9499e4de | 29 | #include <stdint.h> |
30 | #include <stdio.h> | |
975b2ddb | 31 | #include <pthread.h> |
88f973c2 | 32 | #include <ctype.h> |
6d5cb6d4 | 33 | #include "uthash.h" |
34 | #include "utlist.h" | |
9499e4de | 35 | |
3face669 | 36 | #include "komodo_structs.h" |
37 | ||
9fb37168 | 38 | #include "komodo_globals.h" |
46beb55e | 39 | #include "komodo_utils.h" |
1bfdde1d | 40 | |
01f9cb3d | 41 | |
40d4047d | 42 | void komodo_setkmdheight(int32_t kmdheight) |
43 | { | |
44 | if ( kmdheight > KMDHEIGHT ) | |
45 | KMDHEIGHT = kmdheight; | |
46 | } | |
e4e76989 | 47 | |
48 | #include "cJSON.c" | |
49 | #include "komodo_bitcoind.h" | |
50 | #include "komodo_interest.h" | |
51 | #include "komodo_pax.h" | |
52 | #include "komodo_notary.h" | |
53 | #include "komodo_gateway.h" | |
54 | #include "komodo_events.h" | |
55 | ||
e4e76989 | 56 | |
c75c18fc | 57 | void komodo_currentheight_set(int32_t height) |
58 | { | |
59 | char symbol[16],dest[16]; struct komodo_state *sp; | |
60 | if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) | |
61 | sp->CURRENT_HEIGHT = height; | |
62 | } | |
63 | ||
64 | int32_t komodo_currentheight() | |
65 | { | |
66 | char symbol[16],dest[16]; struct komodo_state *sp; | |
67 | if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) | |
68 | return(sp->CURRENT_HEIGHT); | |
69 | else return(0); | |
70 | } | |
71 | ||
72 | int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest) | |
73 | { | |
74 | static int32_t errs; | |
2df2f193 | 75 | int32_t func,ht,notarized_height,num,matched=0; uint256 notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33]; |
c75c18fc | 76 | if ( (func= fgetc(fp)) != EOF ) |
77 | { | |
78 | if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 ) | |
79 | matched = 1; | |
80 | else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0); | |
81 | if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) ) | |
82 | errs++; | |
83 | //printf("fpos.%ld func.(%d %c) ht.%d ",ftell(fp),func,func,ht); | |
84 | if ( func == 'P' ) | |
85 | { | |
86 | if ( (num= fgetc(fp)) < 64 ) | |
87 | { | |
88 | if ( fread(pubkeys,33,num,fp) != num ) | |
89 | errs++; | |
90 | else | |
91 | { | |
92 | printf("updated %d pubkeys at ht.%d\n",num,ht); | |
93 | if ( matched != 0 ) | |
94 | komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys); | |
95 | } | |
96 | } else printf("illegal num.%d\n",num); | |
97 | } | |
98 | else if ( func == 'N' ) | |
99 | { | |
100 | if ( fread(¬arized_height,1,sizeof(notarized_height),fp) != sizeof(notarized_height) ) | |
101 | errs++; | |
102 | if ( fread(¬arized_hash,1,sizeof(notarized_hash),fp) != sizeof(notarized_hash) ) | |
103 | errs++; | |
104 | if ( fread(¬arized_desttxid,1,sizeof(notarized_desttxid),fp) != sizeof(notarized_desttxid) ) | |
105 | errs++; | |
106 | printf("load NOTARIZED %d %s\n",notarized_height,notarized_hash.ToString().c_str()); | |
107 | if ( matched != 0 ) | |
108 | komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height); | |
109 | } | |
110 | else if ( func == 'U' ) | |
111 | { | |
112 | uint8_t n,nid; uint256 hash; uint64_t mask; | |
113 | n = fgetc(fp); | |
114 | nid = fgetc(fp); | |
115 | //printf("U %d %d\n",n,nid); | |
116 | if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) ) | |
117 | errs++; | |
118 | if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) ) | |
119 | errs++; | |
120 | if ( matched != 0 ) | |
2df2f193 | 121 | komodo_eventadd_utxo(sp,symbol,ht,nid,hash,mask,n); |
c75c18fc | 122 | } |
123 | else if ( func == 'K' ) | |
124 | { | |
125 | int32_t kheight; | |
126 | if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) ) | |
127 | errs++; | |
128 | if ( matched != 0 ) | |
129 | komodo_eventadd_kmdheight(sp,symbol,ht,kheight); | |
130 | } | |
131 | else if ( func == 'R' ) | |
132 | { | |
133 | uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[10000]; | |
134 | if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) ) | |
135 | errs++; | |
136 | if ( fread(&v,1,sizeof(v),fp) != sizeof(v) ) | |
137 | errs++; | |
138 | if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) ) | |
139 | errs++; | |
140 | if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) ) | |
141 | errs++; | |
142 | if ( olen < sizeof(opret) ) | |
143 | { | |
144 | if ( fread(opret,1,olen,fp) != olen ) | |
145 | errs++; | |
146 | if ( matched != 0 ) | |
2df2f193 | 147 | komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen); |
c75c18fc | 148 | } else printf("illegal olen.%u\n",olen); |
149 | } | |
150 | else if ( func == 'D' ) | |
151 | { | |
152 | printf("unexpected function D[%d]\n",ht); | |
153 | } | |
154 | else if ( func == 'V' ) | |
155 | { | |
156 | int32_t numpvals; uint32_t pvals[128]; | |
157 | numpvals = fgetc(fp); | |
158 | if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals ) | |
159 | { | |
160 | if ( matched != 0 ) | |
2df2f193 | 161 | komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals); |
c75c18fc | 162 | //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals); |
163 | } else printf("error loading pvals[%d]\n",numpvals); | |
164 | } | |
165 | else printf("illegal func.(%d %c)\n",func,func); | |
166 | return(func); | |
167 | } else return(-1); | |
168 | } | |
169 | ||
429dabb5 | 170 | 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,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout) |
5cea7725 | 171 | { |
c75c18fc | 172 | static FILE *fp; static int32_t errs; |
173 | struct komodo_state *sp; char fname[512],fname2[512],symbol[16],dest[16]; int32_t ht,func; uint8_t num,pubkeys[64][33]; | |
174 | if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) | |
175 | return; | |
888cf827 | 176 | if ( fp == 0 ) |
177 | { | |
e402ebee | 178 | #ifdef WIN32 |
888cf827 | 179 | sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"komodostate"); |
180 | sprintf(fname2,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"minerids"); | |
e402ebee | 181 | #else |
888cf827 | 182 | sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),(char *)"komodostate"); |
183 | sprintf(fname2,"%s/%s",GetDataDir(false).string().c_str(),(char *)"minerids"); | |
e402ebee | 184 | #endif |
888cf827 | 185 | memset(Minerids,0xfe,sizeof(Minerids)); |
186 | if ( (Minerfp= fopen(fname2,"rb+")) == 0 ) | |
187 | { | |
188 | if ( (Minerfp= fopen(fname2,"wb")) != 0 ) | |
189 | { | |
190 | fwrite(Minerids,1,sizeof(Minerids),Minerfp); | |
191 | fclose(Minerfp); | |
192 | } | |
193 | Minerfp = fopen(fname2,"rb+"); | |
194 | } | |
3901c5b8 | 195 | if ( Minerfp != 0 && fread(Minerids,1,sizeof(Minerids),Minerfp) != sizeof(Minerids) ) |
196 | printf("read error Minerids\n"); | |
bafc61f3 | 197 | if ( (fp= fopen(fname,"rb+")) != 0 ) |
198 | { | |
c75c18fc | 199 | while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 ) |
200 | ; | |
bafc61f3 | 201 | } else fp = fopen(fname,"wb+"); |
202 | printf("fname.(%s) fpos.%ld\n",fname,ftell(fp)); | |
63c30610 | 203 | KOMODO_INITDONE = (uint32_t)time(NULL); |
bafc61f3 | 204 | } |
e98e2aec | 205 | if ( height <= 0 ) |
49df008c | 206 | { |
6458297e | 207 | //printf("early return: stateupdate height.%d\n",height); |
49df008c | 208 | return; |
209 | } | |
3eea72f2 | 210 | if ( fp != 0 ) // write out funcid, height, other fields, call side effect function |
bafc61f3 | 211 | { |
2578b002 | 212 | //printf("fpos.%ld ",ftell(fp)); |
c75c18fc | 213 | if ( KMDheight > 0 ) |
2b84e06c | 214 | { |
215 | fputc('K',fp); | |
216 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) | |
217 | errs++; | |
218 | if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) ) | |
219 | errs++; | |
c75c18fc | 220 | komodo_eventadd_kmdheight(sp,symbol,height,KMDheight); |
2b84e06c | 221 | } |
01f9cb3d | 222 | else if ( opretbuf != 0 && opretlen > 0 ) |
64bb0834 | 223 | { |
01f9cb3d | 224 | uint16_t olen = opretlen; |
225 | fputc('R',fp); | |
64bb0834 | 226 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
227 | errs++; | |
429dabb5 | 228 | if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) ) |
229 | errs++; | |
230 | if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) ) | |
231 | errs++; | |
30df2e00 | 232 | if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) ) |
33935bd0 | 233 | errs++; |
289dc57b | 234 | if ( fwrite(&olen,1,sizeof(olen),fp) != olen ) |
235 | errs++; | |
01f9cb3d | 236 | if ( fwrite(opretbuf,1,olen,fp) != olen ) |
64bb0834 | 237 | errs++; |
1938746c | 238 | //printf("ht.%d R opret[%d]\n",height,olen); |
c75c18fc | 239 | //komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout); |
240 | komodo_eventadd_opreturn(sp,symbol,height,txhash,opretvalue,vout,opretbuf,olen); | |
01f9cb3d | 241 | } |
9997caa0 | 242 | else if ( notarypubs != 0 && numnotaries > 0 ) |
bafc61f3 | 243 | { |
2578b002 | 244 | //printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs); |
bafc61f3 | 245 | fputc('P',fp); |
c50ec0fb | 246 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
247 | errs++; | |
bafc61f3 | 248 | fputc(numnotaries,fp); |
52ed4002 | 249 | if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries ) |
250 | errs++; | |
c75c18fc | 251 | komodo_eventadd_pubkeys(sp,symbol,height,numnotaries,notarypubs); |
bafc61f3 | 252 | } |
671187dd | 253 | else if ( voutmask != 0 && numvouts > 0 ) |
254 | { | |
2578b002 | 255 | //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash)); |
671187dd | 256 | fputc('U',fp); |
c50ec0fb | 257 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
258 | errs++; | |
671187dd | 259 | fputc(numvouts,fp); |
260 | fputc(notaryid,fp); | |
261 | if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) ) | |
262 | errs++; | |
263 | if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) ) | |
264 | errs++; | |
c75c18fc | 265 | komodo_eventadd_utxo(sp,symbol,height,notaryid,txhash,voutmask,numvouts); |
266 | //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts); | |
671187dd | 267 | } |
b673d264 | 268 | //#ifdef KOMODO_PAX |
4a41b0b2 | 269 | else if ( pvals != 0 && numpvals > 0 ) |
270 | { | |
bfa5b4f2 | 271 | int32_t i,nonz = 0; |
1bf82154 | 272 | for (i=0; i<32; i++) |
273 | if ( pvals[i] != 0 ) | |
274 | nonz++; | |
275 | if ( nonz >= 32 ) | |
276 | { | |
277 | fputc('V',fp); | |
278 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) | |
279 | errs++; | |
280 | fputc(numpvals,fp); | |
281 | if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals ) | |
282 | errs++; | |
c75c18fc | 283 | komodo_eventadd_pricefeed(sp,symbol,height,pvals,numpvals); |
2578b002 | 284 | //printf("ht.%d V numpvals[%d]\n",height,numpvals); |
1bf82154 | 285 | } |
ef7a3548 | 286 | //printf("save pvals height.%d numpvals.%d\n",height,numpvals); |
4a41b0b2 | 287 | } |
b673d264 | 288 | //#endif |
6dabcca4 | 289 | else if ( height != 0 ) |
671187dd | 290 | { |
2578b002 | 291 | //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs); |
671187dd | 292 | fputc('N',fp); |
c50ec0fb | 293 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
294 | errs++; | |
c75c18fc | 295 | if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) ) |
671187dd | 296 | errs++; |
c75c18fc | 297 | if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) ) |
671187dd | 298 | errs++; |
c75c18fc | 299 | if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) ) |
671187dd | 300 | errs++; |
c75c18fc | 301 | komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT); |
302 | //komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID); | |
671187dd | 303 | } |
47f47733 | 304 | fflush(fp); |
bafc61f3 | 305 | } |
306 | } | |
307 | ||
6dba48ec | 308 | 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) |
1390456b | 309 | { |
2df2f193 | 310 | 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]; |
311 | if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) | |
312 | return(-1); | |
9c406099 | 313 | if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) |
1390456b | 314 | { |
c23dd601 | 315 | decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR); |
986d3459 | 316 | /*for (k=0; k<33; k++) |
e1be360e | 317 | printf("%02x",crypto777[k]); |
318 | printf(" crypto777 "); | |
319 | for (k=0; k<scriptlen; k++) | |
320 | printf("%02x",scriptbuf[k]); | |
986d3459 | 321 | printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/ |
1390456b | 322 | if ( memcmp(crypto777,scriptbuf+1,33) == 0 ) |
323 | { | |
324 | *specialtxp = 1; | |
b7f942c8 | 325 | //printf(">>>>>>>> "); |
1390456b | 326 | } |
a5f315c7 | 327 | else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 ) |
1390456b | 328 | { |
84e843cd | 329 | //printf("found notary.k%d\n",k); |
330 | if ( notaryid < 64 ) | |
1390456b | 331 | { |
84e843cd | 332 | if ( notaryid < 0 ) |
333 | { | |
a5f315c7 | 334 | notaryid = nid; |
84e843cd | 335 | *voutmaskp |= (1LL << j); |
336 | } | |
a5f315c7 | 337 | else if ( notaryid != nid ) |
84e843cd | 338 | { |
b7f942c8 | 339 | //for (i=0; i<33; i++) |
340 | // printf("%02x",scriptbuf[i+1]); | |
341 | //printf(" %s mismatch notaryid.%d k.%d\n",ASSETCHAINS_SYMBOL,notaryid,nid); | |
84e843cd | 342 | notaryid = 64; |
343 | *voutmaskp = 0; | |
344 | } | |
345 | else *voutmaskp |= (1LL << j); | |
1390456b | 346 | } |
1390456b | 347 | } |
348 | } | |
844a6138 | 349 | if ( scriptbuf[len++] == 0x6a ) |
1390456b | 350 | { |
351 | if ( (opretlen= scriptbuf[len++]) == 0x4c ) | |
352 | opretlen = scriptbuf[len++]; | |
353 | else if ( opretlen == 0x4d ) | |
354 | { | |
355 | opretlen = scriptbuf[len++]; | |
356 | opretlen = (opretlen << 8) + scriptbuf[len++]; | |
357 | } | |
a6400079 | 358 | if ( j == 1 && opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 ) |
1390456b | 359 | { |
4a41b0b2 | 360 | len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid); |
361 | len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp); | |
3eea72f2 | 362 | len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid); |
c75c18fc | 363 | if ( *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height ) |
1390456b | 364 | { |
eff7174e | 365 | printf("ht.%d NOTARIZED.%d %s.%s %sTXID.%s (%s)\n",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 | 366 | sp->NOTARIZED_HEIGHT = *notarizedheightp; |
367 | sp->NOTARIZED_HASH = kmdtxid; | |
368 | sp->NOTARIZED_DESTTXID = desttxid; | |
429dabb5 | 369 | komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0); |
a6400079 | 370 | } else printf("reject ht.%d NOTARIZED.%d %s.%s DESTTXID.%s (%s)\n",height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]); |
1390456b | 371 | } |
babdc001 | 372 | else if ( i == 0 && j == 1 && opretlen == 149 ) |
0201d1a6 | 373 | komodo_paxpricefeed(height,&scriptbuf[len],opretlen); |
eb406ebd | 374 | else |
375 | { | |
1938746c | 376 | //int32_t k; for (k=0; k<scriptlen; k++) |
377 | // printf("%02x",scriptbuf[k]); | |
378 | //printf(" <- script ht.%d i.%d j.%d value %.8f\n",height,i,j,dstr(value)); | |
6dba48ec | 379 | if ( opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 ) |
380 | { | |
381 | iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid); | |
5a286a6f | 382 | for (k=0; k<32; k++) |
383 | if ( scriptbuf[len+k] != 0 ) | |
384 | break; | |
385 | if ( k == 32 ) | |
6dba48ec | 386 | { |
387 | *isratificationp = 1; | |
388 | printf("ISRATIFICATION (%s)\n",(char *)&scriptbuf[len+32*2+4]); | |
389 | } | |
390 | } | |
391 | if ( *isratificationp == 0 ) | |
392 | komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,value,&scriptbuf[len],opretlen,j); | |
eb406ebd | 393 | } |
1390456b | 394 | } |
395 | return(notaryid); | |
396 | } | |
397 | ||
f38345e9 | 398 | int32_t komodo_isratify(int32_t isspecial,int32_t numvalid) |
399 | { | |
755ead98 | 400 | if ( isspecial != 0 && numvalid >= KOMODO_MINRATIFY ) |
f38345e9 | 401 | return(1); |
402 | else return(0); | |
403 | } | |
404 | ||
405 | // Special tx have vout[0] -> CRYPTO777 | |
755ead98 | 406 | // with more than KOMODO_MINRATIFY pay2pubkey outputs -> ratify |
f38345e9 | 407 | // if all outputs to notary -> notary utxo |
408 | // if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed | |
420d7311 | 409 | // OP_RETURN: 'D' -> deposit, 'W' -> withdraw |
c75c18fc | 410 | void komodo_currentheight_set(int32_t height); |
f38345e9 | 411 | |
651989c7 | 412 | void komodo_connectblock(CBlockIndex *pindex,CBlock& block) |
50027f06 | 413 | { |
a7f0d3e4 | 414 | static int32_t hwmheight; |
e9595d76 | 415 | uint64_t signedmask,voutmask; |
dd7b22ad | 416 | uint8_t scriptbuf[4096],pubkeys[64][33]; uint256 kmdtxid,btctxid,txhash; |
7589f03a | 417 | int32_t i,j,k,numnotaries,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count; |
c93dc546 | 418 | komodo_init(pindex->nHeight); |
7589f03a | 419 | numnotaries = komodo_notaries(pubkeys,pindex->nHeight); |
a7f0d3e4 | 420 | if ( pindex->nHeight > hwmheight ) |
421 | hwmheight = pindex->nHeight; | |
422 | else | |
423 | { | |
424 | printf("hwmheight.%d vs pindex->nHeight.%d reorg.%d\n",hwmheight,pindex->nHeight,hwmheight-pindex->nHeight); | |
425 | // reset komodostate | |
426 | } | |
c75c18fc | 427 | komodo_currentheight_set(chainActive.Tip()->nHeight); |
2b03a458 | 428 | if ( komodo_is_issuer() != 0 ) |
2d657c5b | 429 | { |
f67800d0 | 430 | while ( KOMODO_REALTIME == 0 || time(NULL) <= KOMODO_REALTIME ) |
2d657c5b | 431 | { |
1fdfe03c | 432 | fprintf(stderr,"komodo_connect.(%s) waiting for realtime RT.%u now.%u\n",ASSETCHAINS_SYMBOL,KOMODO_REALTIME,(uint32_t)time(NULL)); |
cc66cf72 | 433 | sleep(30); |
2d657c5b | 434 | } |
2d657c5b | 435 | } |
c021b871 | 436 | KOMODO_REALTIME = KOMODO_INITDONE = (uint32_t)time(NULL); |
68916cc6 | 437 | if ( pindex != 0 ) |
b501ded2 | 438 | { |
656eddcd | 439 | height = pindex->nHeight; |
01cc012f | 440 | txn_count = block.vtx.size(); |
1938746c | 441 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) |
e0bf88b0 | 442 | printf("%s ht.%d connect txn_count.%d\n",ASSETCHAINS_SYMBOL,height,txn_count); |
01cc012f | 443 | for (i=0; i<txn_count; i++) |
dc64de68 | 444 | { |
352f8081 | 445 | txhash = block.vtx[i].GetHash(); |
01cc012f | 446 | numvouts = block.vtx[i].vout.size(); |
352f8081 | 447 | notaryid = -1; |
6dba48ec | 448 | voutmask = specialtx = notarizedheight = isratification = 0; |
01cc012f | 449 | for (j=0; j<numvouts; j++) |
dc64de68 | 450 | { |
1390456b | 451 | len = block.vtx[i].vout[j].scriptPubKey.size(); |
452 | if ( len <= sizeof(scriptbuf) ) | |
01cc012f | 453 | { |
189d9dee | 454 | #ifdef KOMODO_ZCASH |
484f8777 | 455 | memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len); |
189d9dee | 456 | #else |
457 | memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len); | |
458 | #endif | |
6dba48ec | 459 | notaryid = komodo_voutupdate(&isratification,notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,¬arizedheight,(uint64_t)block.vtx[i].vout[j].nValue); |
4a4e912b | 460 | if ( i == 0 && j == 0 && komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 ) |
461 | { | |
462 | if ( height < sizeof(Minerids)/sizeof(*Minerids) ) | |
888cf827 | 463 | { |
464 | if ( (Minerids[height]= nid) >= -1 ) | |
465 | { | |
466 | if ( Minerfp != 0 ) | |
467 | { | |
468 | fseek(Minerfp,height,SEEK_SET); | |
469 | fputc(Minerids[height],Minerfp); | |
470 | fflush(Minerfp); | |
471 | } | |
472 | } | |
473 | } | |
4a4e912b | 474 | } |
a01acef4 | 475 | if ( 0 && i > 0 ) |
e69a0833 | 476 | { |
477 | for (k=0; k<len; k++) | |
478 | printf("%02x",scriptbuf[k]); | |
a8832194 | 479 | 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 | 480 | } |
01cc012f | 481 | } |
352f8081 | 482 | } |
cf9538e0 | 483 | if ( i != 0 && notaryid >= 0 && notaryid < 64 && voutmask != 0 ) |
3eea72f2 | 484 | { |
429dabb5 | 485 | komodo_stateupdate(height,0,0,notaryid,txhash,voutmask,numvouts,0,0,0,0,0,0,0); |
3eea72f2 | 486 | //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts); |
487 | } | |
1390456b | 488 | signedmask = 0; |
489 | numvins = block.vtx[i].vin.size(); | |
5bdebffe | 490 | for (j=0; j<numvins; j++) |
352f8081 | 491 | { |
4355e769 | 492 | if ( (k= komodo_nutxofind(height,block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) >= 0 ) |
1390456b | 493 | signedmask |= (1LL << k); |
6423c8a9 | 494 | else if ( signedmask != 0 ) |
bc6fd011 | 495 | printf("signedmask.%llx but ht.%d i.%d j.%d not found (%s %d)\n",(long long)signedmask,height,i,j,block.vtx[i].vin[j].prevout.hash.ToString().c_str(),block.vtx[i].vin[j].prevout.n); |
1390456b | 496 | } |
06f0ed43 | 497 | if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) ) |
1390456b | 498 | { |
e69a0833 | 499 | printf("NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",(long long)signedmask,numvins,height,i,notarizedheight,specialtx); |
7d51d973 | 500 | printf("ht.%d specialtx.%d isratification.%d numvouts.%d signed.%llx numnotaries.%d\n",height,specialtx,isratification,numvouts,(long long)signedmask,numnotaries); |
d2a115d8 | 501 | if ( specialtx != 0 && isratification != 0 && numvouts > 2 ) |
84e843cd | 502 | { |
29e69b85 | 503 | numvalid = 0; |
dd7b22ad | 504 | memset(pubkeys,0,sizeof(pubkeys)); |
6dba48ec | 505 | for (j=1; j<numvouts-1; j++) |
84e843cd | 506 | { |
507 | len = block.vtx[i].vout[j].scriptPubKey.size(); | |
508 | if ( len <= sizeof(scriptbuf) ) | |
509 | { | |
189d9dee | 510 | #ifdef KOMODO_ZCASH |
484f8777 | 511 | memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len); |
189d9dee | 512 | #else |
513 | memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len); | |
514 | #endif | |
84e843cd | 515 | if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) |
516 | { | |
29e69b85 | 517 | memcpy(pubkeys[numvalid++],scriptbuf+1,33); |
e7f99312 | 518 | for (k=0; k<33; k++) |
84e843cd | 519 | printf("%02x",scriptbuf[k+1]); |
520 | printf(" <- new notary.[%d]\n",j-1); | |
521 | } | |
522 | } | |
523 | } | |
7589f03a | 524 | if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries>>1) ) |
d8ce705a | 525 | { |
526 | memset(&txhash,0,sizeof(txhash)); | |
429dabb5 | 527 | komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0); |
f9d034d4 | 528 | 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 | 529 | } else printf("signedmask.%llx numvalid.%d wt.%d numnotaries.%d\n",(long long)signedmask,numvalid,bitweight(signedmask),numnotaries); |
84e843cd | 530 | } |
a96439f5 | 531 | } |
656eddcd | 532 | } |
44c4fbbd | 533 | } else printf("komodo_connectblock: unexpected null pindex\n"); |
63c30610 | 534 | KOMODO_INITDONE = (uint32_t)time(NULL); |
3d35aa5b | 535 | } |
536 | ||
0f24f245 | 537 | |
fcd36118 | 538 | #endif |