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 | { |
6806478b |
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 | { |
5a286a6f |
381 | for (k=0; k<32; k++) |
382 | if ( scriptbuf[len+k] != 0 ) |
383 | break; |
384 | if ( k == 32 ) |
6dba48ec |
385 | { |
386 | *isratificationp = 1; |
387 | printf("ISRATIFICATION (%s)\n",(char *)&scriptbuf[len+32*2+4]); |
388 | } |
389 | } |
390 | if ( *isratificationp == 0 ) |
391 | komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,value,&scriptbuf[len],opretlen,j); |
eb406ebd |
392 | } |
1390456b |
393 | } |
394 | return(notaryid); |
395 | } |
396 | |
f38345e9 |
397 | int32_t komodo_isratify(int32_t isspecial,int32_t numvalid) |
398 | { |
755ead98 |
399 | if ( isspecial != 0 && numvalid >= KOMODO_MINRATIFY ) |
f38345e9 |
400 | return(1); |
401 | else return(0); |
402 | } |
403 | |
404 | // Special tx have vout[0] -> CRYPTO777 |
755ead98 |
405 | // with more than KOMODO_MINRATIFY pay2pubkey outputs -> ratify |
f38345e9 |
406 | // if all outputs to notary -> notary utxo |
407 | // if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed |
420d7311 |
408 | // OP_RETURN: 'D' -> deposit, 'W' -> withdraw |
c75c18fc |
409 | void komodo_currentheight_set(int32_t height); |
f38345e9 |
410 | |
651989c7 |
411 | void komodo_connectblock(CBlockIndex *pindex,CBlock& block) |
50027f06 |
412 | { |
a7f0d3e4 |
413 | static int32_t hwmheight; |
e9595d76 |
414 | uint64_t signedmask,voutmask; |
dd7b22ad |
415 | uint8_t scriptbuf[4096],pubkeys[64][33]; uint256 kmdtxid,btctxid,txhash; |
7589f03a |
416 | int32_t i,j,k,numnotaries,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count; |
c93dc546 |
417 | komodo_init(pindex->nHeight); |
7589f03a |
418 | numnotaries = komodo_notaries(pubkeys,pindex->nHeight); |
a7f0d3e4 |
419 | if ( pindex->nHeight > hwmheight ) |
420 | hwmheight = pindex->nHeight; |
421 | else |
422 | { |
423 | printf("hwmheight.%d vs pindex->nHeight.%d reorg.%d\n",hwmheight,pindex->nHeight,hwmheight-pindex->nHeight); |
424 | // reset komodostate |
425 | } |
c75c18fc |
426 | komodo_currentheight_set(chainActive.Tip()->nHeight); |
2b03a458 |
427 | if ( komodo_is_issuer() != 0 ) |
2d657c5b |
428 | { |
f67800d0 |
429 | while ( KOMODO_REALTIME == 0 || time(NULL) <= KOMODO_REALTIME ) |
2d657c5b |
430 | { |
1fdfe03c |
431 | fprintf(stderr,"komodo_connect.(%s) waiting for realtime RT.%u now.%u\n",ASSETCHAINS_SYMBOL,KOMODO_REALTIME,(uint32_t)time(NULL)); |
cc66cf72 |
432 | sleep(30); |
2d657c5b |
433 | } |
2d657c5b |
434 | } |
c021b871 |
435 | KOMODO_REALTIME = KOMODO_INITDONE = (uint32_t)time(NULL); |
68916cc6 |
436 | if ( pindex != 0 ) |
b501ded2 |
437 | { |
656eddcd |
438 | height = pindex->nHeight; |
01cc012f |
439 | txn_count = block.vtx.size(); |
1938746c |
440 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) |
e0bf88b0 |
441 | printf("%s ht.%d connect txn_count.%d\n",ASSETCHAINS_SYMBOL,height,txn_count); |
01cc012f |
442 | for (i=0; i<txn_count; i++) |
dc64de68 |
443 | { |
352f8081 |
444 | txhash = block.vtx[i].GetHash(); |
01cc012f |
445 | numvouts = block.vtx[i].vout.size(); |
352f8081 |
446 | notaryid = -1; |
6dba48ec |
447 | voutmask = specialtx = notarizedheight = isratification = 0; |
01cc012f |
448 | for (j=0; j<numvouts; j++) |
dc64de68 |
449 | { |
1390456b |
450 | len = block.vtx[i].vout[j].scriptPubKey.size(); |
451 | if ( len <= sizeof(scriptbuf) ) |
01cc012f |
452 | { |
189d9dee |
453 | #ifdef KOMODO_ZCASH |
484f8777 |
454 | memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len); |
189d9dee |
455 | #else |
456 | memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len); |
457 | #endif |
6dba48ec |
458 | notaryid = komodo_voutupdate(&isratification,notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,¬arizedheight,(uint64_t)block.vtx[i].vout[j].nValue); |
4a4e912b |
459 | if ( i == 0 && j == 0 && komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 ) |
460 | { |
461 | if ( height < sizeof(Minerids)/sizeof(*Minerids) ) |
888cf827 |
462 | { |
463 | if ( (Minerids[height]= nid) >= -1 ) |
464 | { |
465 | if ( Minerfp != 0 ) |
466 | { |
467 | fseek(Minerfp,height,SEEK_SET); |
468 | fputc(Minerids[height],Minerfp); |
469 | fflush(Minerfp); |
470 | } |
471 | } |
472 | } |
4a4e912b |
473 | } |
a01acef4 |
474 | if ( 0 && i > 0 ) |
e69a0833 |
475 | { |
476 | for (k=0; k<len; k++) |
477 | printf("%02x",scriptbuf[k]); |
a8832194 |
478 | 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 |
479 | } |
01cc012f |
480 | } |
352f8081 |
481 | } |
cf9538e0 |
482 | if ( i != 0 && notaryid >= 0 && notaryid < 64 && voutmask != 0 ) |
3eea72f2 |
483 | { |
429dabb5 |
484 | komodo_stateupdate(height,0,0,notaryid,txhash,voutmask,numvouts,0,0,0,0,0,0,0); |
3eea72f2 |
485 | //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts); |
486 | } |
1390456b |
487 | signedmask = 0; |
488 | numvins = block.vtx[i].vin.size(); |
5bdebffe |
489 | for (j=0; j<numvins; j++) |
352f8081 |
490 | { |
4355e769 |
491 | if ( (k= komodo_nutxofind(height,block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) >= 0 ) |
1390456b |
492 | signedmask |= (1LL << k); |
6423c8a9 |
493 | else if ( signedmask != 0 ) |
bc6fd011 |
494 | 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 |
495 | } |
06f0ed43 |
496 | if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) ) |
1390456b |
497 | { |
e69a0833 |
498 | printf("NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",(long long)signedmask,numvins,height,i,notarizedheight,specialtx); |
7d51d973 |
499 | printf("ht.%d specialtx.%d isratification.%d numvouts.%d signed.%llx numnotaries.%d\n",height,specialtx,isratification,numvouts,(long long)signedmask,numnotaries); |
d2a115d8 |
500 | if ( specialtx != 0 && isratification != 0 && numvouts > 2 ) |
84e843cd |
501 | { |
29e69b85 |
502 | numvalid = 0; |
dd7b22ad |
503 | memset(pubkeys,0,sizeof(pubkeys)); |
6dba48ec |
504 | for (j=1; j<numvouts-1; j++) |
84e843cd |
505 | { |
506 | len = block.vtx[i].vout[j].scriptPubKey.size(); |
507 | if ( len <= sizeof(scriptbuf) ) |
508 | { |
189d9dee |
509 | #ifdef KOMODO_ZCASH |
484f8777 |
510 | memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len); |
189d9dee |
511 | #else |
512 | memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len); |
513 | #endif |
84e843cd |
514 | if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) |
515 | { |
29e69b85 |
516 | memcpy(pubkeys[numvalid++],scriptbuf+1,33); |
e7f99312 |
517 | for (k=0; k<33; k++) |
84e843cd |
518 | printf("%02x",scriptbuf[k+1]); |
519 | printf(" <- new notary.[%d]\n",j-1); |
520 | } |
521 | } |
522 | } |
7589f03a |
523 | if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries>>1) ) |
d8ce705a |
524 | { |
525 | memset(&txhash,0,sizeof(txhash)); |
429dabb5 |
526 | komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0); |
f9d034d4 |
527 | 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 |
528 | } else printf("signedmask.%llx numvalid.%d wt.%d numnotaries.%d\n",(long long)signedmask,numvalid,bitweight(signedmask),numnotaries); |
84e843cd |
529 | } |
a96439f5 |
530 | } |
656eddcd |
531 | } |
44c4fbbd |
532 | } else printf("komodo_connectblock: unexpected null pindex\n"); |
63c30610 |
533 | KOMODO_INITDONE = (uint32_t)time(NULL); |
3d35aa5b |
534 | } |
535 | |
0f24f245 |
536 | |
fcd36118 |
537 | #endif |