]>
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 | |
26ac06c4 | 22 | #define KOMODO_ASSETCHAINS_WAITNOTARIZE |
fa8d44fc | 23 | #define KOMODO_PAXMAX (10000 * COIN) |
aa114a60 | 24 | |
9499e4de | 25 | #include <stdint.h> |
26 | #include <stdio.h> | |
975b2ddb | 27 | #include <pthread.h> |
88f973c2 | 28 | #include <ctype.h> |
6d5cb6d4 | 29 | #include "uthash.h" |
30 | #include "utlist.h" | |
9499e4de | 31 | |
fc318ffe | 32 | int32_t gettxout_scriptPubKey(uint8_t *scriptPubkey,int32_t maxsize,uint256 txid,int32_t n); |
f3a1de3a | 33 | void komodo_event_rewind(struct komodo_state *sp,char *symbol,int32_t height); |
875cf683 | 34 | void komodo_connectblock(CBlockIndex *pindex,CBlock& block); |
3face669 | 35 | |
fc318ffe | 36 | #include "komodo_structs.h" |
9fb37168 | 37 | #include "komodo_globals.h" |
46beb55e | 38 | #include "komodo_utils.h" |
6b5cfbb4 | 39 | #include "komodo_curve25519.h" |
1bfdde1d | 40 | |
e4e76989 | 41 | #include "cJSON.c" |
42 | #include "komodo_bitcoind.h" | |
43 | #include "komodo_interest.h" | |
44 | #include "komodo_pax.h" | |
45 | #include "komodo_notary.h" | |
ab918767 | 46 | |
47 | int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest); | |
6b5cfbb4 | 48 | #include "komodo_kv.h" |
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 | { | |
ec368288 | 87 | //printf("updated %d pubkeys at %s ht.%d\n",num,symbol,ht); |
15724d02 | 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++; | |
b27b2877 | 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); | |
dff4ef45 | 166 | //printf("illegal olen.%u\n",olen); |
939a5a45 | 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 | { |
15d0fbd4 | 192 | static FILE *fp; static int32_t errs,didinit; |
e21ffa1d | 193 | struct komodo_state *sp; char fname[512],symbol[16],dest[16]; int32_t ht,func; uint8_t num,pubkeys[64][33]; |
15d0fbd4 | 194 | if ( didinit == 0 ) |
195 | { | |
196 | portable_mutex_init(&KOMODO_KV_mutex); | |
197 | didinit = 1; | |
198 | } | |
c75c18fc | 199 | if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) |
15373725 | 200 | { |
201 | KOMODO_INITDONE = (uint32_t)time(NULL); | |
c75c18fc | 202 | return; |
15373725 | 203 | } |
888cf827 | 204 | if ( fp == 0 ) |
205 | { | |
055db9b6 | 206 | komodo_statefname(fname,ASSETCHAINS_SYMBOL,(char *)"komodostate"); |
bafc61f3 | 207 | if ( (fp= fopen(fname,"rb+")) != 0 ) |
208 | { | |
368c61bc | 209 | while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 ) |
210 | ; | |
bafc61f3 | 211 | } else fp = fopen(fname,"wb+"); |
212 | printf("fname.(%s) fpos.%ld\n",fname,ftell(fp)); | |
63c30610 | 213 | KOMODO_INITDONE = (uint32_t)time(NULL); |
bafc61f3 | 214 | } |
e98e2aec | 215 | if ( height <= 0 ) |
49df008c | 216 | { |
6458297e | 217 | //printf("early return: stateupdate height.%d\n",height); |
49df008c | 218 | return; |
219 | } | |
3eea72f2 | 220 | if ( fp != 0 ) // write out funcid, height, other fields, call side effect function |
bafc61f3 | 221 | { |
2578b002 | 222 | //printf("fpos.%ld ",ftell(fp)); |
ab918767 | 223 | if ( KMDheight != 0 ) |
2b84e06c | 224 | { |
e155dbe9 | 225 | if ( KMDtimestamp != 0 ) |
226 | { | |
227 | fputc('T',fp); | |
228 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) | |
229 | errs++; | |
230 | if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) ) | |
231 | errs++; | |
232 | if ( fwrite(&KMDtimestamp,1,sizeof(KMDtimestamp),fp) != sizeof(KMDtimestamp) ) | |
233 | errs++; | |
234 | } | |
235 | else | |
236 | { | |
237 | fputc('K',fp); | |
238 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) | |
239 | errs++; | |
240 | if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) ) | |
241 | errs++; | |
242 | } | |
243 | komodo_eventadd_kmdheight(sp,symbol,height,KMDheight,KMDtimestamp); | |
2b84e06c | 244 | } |
01f9cb3d | 245 | else if ( opretbuf != 0 && opretlen > 0 ) |
64bb0834 | 246 | { |
01f9cb3d | 247 | uint16_t olen = opretlen; |
248 | fputc('R',fp); | |
64bb0834 | 249 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
250 | errs++; | |
429dabb5 | 251 | if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) ) |
252 | errs++; | |
253 | if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) ) | |
254 | errs++; | |
30df2e00 | 255 | if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) ) |
33935bd0 | 256 | errs++; |
289dc57b | 257 | if ( fwrite(&olen,1,sizeof(olen),fp) != olen ) |
258 | errs++; | |
01f9cb3d | 259 | if ( fwrite(opretbuf,1,olen,fp) != olen ) |
64bb0834 | 260 | errs++; |
088b1fa8 | 261 | //printf("ht.%d R opret[%d] sp.%p\n",height,olen,sp); |
c75c18fc | 262 | //komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout); |
263 | komodo_eventadd_opreturn(sp,symbol,height,txhash,opretvalue,vout,opretbuf,olen); | |
01f9cb3d | 264 | } |
9997caa0 | 265 | else if ( notarypubs != 0 && numnotaries > 0 ) |
bafc61f3 | 266 | { |
f3a1de3a | 267 | printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs); |
bafc61f3 | 268 | fputc('P',fp); |
c50ec0fb | 269 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
270 | errs++; | |
bafc61f3 | 271 | fputc(numnotaries,fp); |
52ed4002 | 272 | if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries ) |
273 | errs++; | |
c75c18fc | 274 | komodo_eventadd_pubkeys(sp,symbol,height,numnotaries,notarypubs); |
bafc61f3 | 275 | } |
671187dd | 276 | else if ( voutmask != 0 && numvouts > 0 ) |
277 | { | |
2578b002 | 278 | //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash)); |
671187dd | 279 | fputc('U',fp); |
c50ec0fb | 280 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
281 | errs++; | |
671187dd | 282 | fputc(numvouts,fp); |
283 | fputc(notaryid,fp); | |
284 | if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) ) | |
285 | errs++; | |
286 | if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) ) | |
287 | errs++; | |
6f3cb4d0 | 288 | //komodo_eventadd_utxo(sp,symbol,height,notaryid,txhash,voutmask,numvouts); |
671187dd | 289 | } |
4a41b0b2 | 290 | else if ( pvals != 0 && numpvals > 0 ) |
291 | { | |
bfa5b4f2 | 292 | int32_t i,nonz = 0; |
1bf82154 | 293 | for (i=0; i<32; i++) |
294 | if ( pvals[i] != 0 ) | |
295 | nonz++; | |
296 | if ( nonz >= 32 ) | |
297 | { | |
298 | fputc('V',fp); | |
299 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) | |
300 | errs++; | |
301 | fputc(numpvals,fp); | |
302 | if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals ) | |
303 | errs++; | |
c75c18fc | 304 | komodo_eventadd_pricefeed(sp,symbol,height,pvals,numpvals); |
2578b002 | 305 | //printf("ht.%d V numpvals[%d]\n",height,numpvals); |
1bf82154 | 306 | } |
ef7a3548 | 307 | //printf("save pvals height.%d numpvals.%d\n",height,numpvals); |
4a41b0b2 | 308 | } |
6dabcca4 | 309 | else if ( height != 0 ) |
671187dd | 310 | { |
2578b002 | 311 | //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs); |
b148643e | 312 | if ( sp != 0 ) |
807200e4 | 313 | { |
314 | fputc('N',fp); | |
315 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) | |
316 | errs++; | |
317 | if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) ) | |
318 | errs++; | |
319 | if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) ) | |
320 | errs++; | |
321 | if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) ) | |
322 | errs++; | |
323 | komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT); | |
324 | } | |
671187dd | 325 | } |
47f47733 | 326 | fflush(fp); |
bafc61f3 | 327 | } |
328 | } | |
329 | ||
8f40a7a2 | 330 | 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 | 331 | { |
c96e160a | 332 | static uint256 zero; static FILE *signedfp; |
dbaf1154 | 333 | int32_t opretlen,nid,k,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33]; struct komodo_state *sp; char symbol[16],dest[16]; |
2df2f193 | 334 | if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) |
335 | return(-1); | |
9c406099 | 336 | if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) |
1390456b | 337 | { |
0ef85dd0 | 338 | if ( i == 0 && j == 0 && memcmp(NOTARY_PUBKEY33,scriptbuf+1,33) == 0 && NOTARY_PUBKEY33[0] != 0 ) |
9152feb5 | 339 | { |
ba37a6b9 | 340 | printf("%s KOMODO_LASTMINED.%d -> %d\n",ASSETCHAINS_SYMBOL,KOMODO_LASTMINED,height); |
8ee93080 | 341 | prevKOMODO_LASTMINED = KOMODO_LASTMINED; |
9152feb5 | 342 | KOMODO_LASTMINED = height; |
343 | } | |
c23dd601 | 344 | decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR); |
986d3459 | 345 | /*for (k=0; k<33; k++) |
e1be360e | 346 | printf("%02x",crypto777[k]); |
347 | printf(" crypto777 "); | |
348 | for (k=0; k<scriptlen; k++) | |
349 | printf("%02x",scriptbuf[k]); | |
986d3459 | 350 | printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/ |
1390456b | 351 | if ( memcmp(crypto777,scriptbuf+1,33) == 0 ) |
352 | { | |
353 | *specialtxp = 1; | |
b7f942c8 | 354 | //printf(">>>>>>>> "); |
1390456b | 355 | } |
a5f315c7 | 356 | else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 ) |
1390456b | 357 | { |
84e843cd | 358 | //printf("found notary.k%d\n",k); |
359 | if ( notaryid < 64 ) | |
1390456b | 360 | { |
84e843cd | 361 | if ( notaryid < 0 ) |
362 | { | |
a5f315c7 | 363 | notaryid = nid; |
84e843cd | 364 | *voutmaskp |= (1LL << j); |
365 | } | |
a5f315c7 | 366 | else if ( notaryid != nid ) |
84e843cd | 367 | { |
b7f942c8 | 368 | //for (i=0; i<33; i++) |
369 | // printf("%02x",scriptbuf[i+1]); | |
370 | //printf(" %s mismatch notaryid.%d k.%d\n",ASSETCHAINS_SYMBOL,notaryid,nid); | |
84e843cd | 371 | notaryid = 64; |
372 | *voutmaskp = 0; | |
373 | } | |
374 | else *voutmaskp |= (1LL << j); | |
1390456b | 375 | } |
1390456b | 376 | } |
377 | } | |
844a6138 | 378 | if ( scriptbuf[len++] == 0x6a ) |
1390456b | 379 | { |
380 | if ( (opretlen= scriptbuf[len++]) == 0x4c ) | |
381 | opretlen = scriptbuf[len++]; | |
382 | else if ( opretlen == 0x4d ) | |
383 | { | |
384 | opretlen = scriptbuf[len++]; | |
508b0d3c | 385 | opretlen += (scriptbuf[len++] << 8); |
1390456b | 386 | } |
a6400079 | 387 | if ( j == 1 && opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 ) |
1390456b | 388 | { |
4a41b0b2 | 389 | len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid); |
dbaf1154 | 390 | len += iguana_rwnum(0,&scriptbuf[len],sizeof(*notarizedheightp),(uint8_t *)notarizedheightp); |
3eea72f2 | 391 | len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid); |
42af20bb | 392 | if ( notarized != 0 && *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height && (height < sp->CURRENT_HEIGHT-1000 || komodo_verifynotarization(ASSETCHAINS_SYMBOL[0]==0?(char *)"KMD":ASSETCHAINS_SYMBOL,(char *)(ASSETCHAINS_SYMBOL[0] == 0 ? "BTC" : "KMD"),height,*notarizedheightp,kmdtxid,desttxid) == 0) ) |
1390456b | 393 | { |
c75c18fc | 394 | sp->NOTARIZED_HEIGHT = *notarizedheightp; |
395 | sp->NOTARIZED_HASH = kmdtxid; | |
396 | sp->NOTARIZED_DESTTXID = desttxid; | |
e155dbe9 | 397 | komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0,0); |
3237fdd4 | 398 | len += 4; |
056447a6 | 399 | if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 ) |
e8ce1079 | 400 | 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 | 401 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) |
06d1a655 | 402 | { |
dbaf1154 | 403 | if ( signedfp == 0 ) |
404 | { | |
405 | char fname[512]; | |
403b5de8 | 406 | komodo_statefname(fname,(char *)"",(char *)"signedmasks"); |
dbaf1154 | 407 | if ( (signedfp= fopen(fname,"rb+")) == 0 ) |
408 | signedfp = fopen(fname,"wb"); | |
409 | else fseek(signedfp,0,SEEK_END); | |
410 | } | |
411 | if ( signedfp != 0 ) | |
412 | { | |
413 | fwrite(&height,1,sizeof(height),signedfp); | |
414 | fwrite(&signedmask,1,sizeof(signedmask),signedfp); | |
415 | fflush(signedfp); | |
416 | } | |
417 | if ( opretlen > len && scriptbuf[len] == 'A' ) | |
418 | { | |
2962d925 | 419 | //for (i=0; i<opretlen-len; i++) |
420 | // printf("%02x",scriptbuf[len+i]); | |
421 | //printf(" Found extradata.[%d] %d - %d\n",opretlen-len,opretlen,len); | |
c83633f8 | 422 | 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 | 423 | } |
06d1a655 | 424 | } |
cd586654 | 425 | } else if ( height >= KOMODO_MAINNET_START ) |
426 | printf("notarized.%d %llx reject ht.%d NOTARIZED.%d prev.%d %s.%s DESTTXID.%s (%s)\n",notarized,(long long)signedmask,height,*notarizedheightp,sp->NOTARIZED_HEIGHT,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]); | |
1390456b | 427 | } |
babdc001 | 428 | else if ( i == 0 && j == 1 && opretlen == 149 ) |
d42d3348 | 429 | { |
76b9b8b8 | 430 | if ( notaryid >= 0 && notaryid < 64 ) |
d42d3348 | 431 | komodo_paxpricefeed(height,&scriptbuf[len],opretlen); |
432 | } | |
eb406ebd | 433 | else |
434 | { | |
cd586654 | 435 | //int32_t k; for (k=0; k<scriptlen; k++) |
436 | // printf("%02x",scriptbuf[k]); | |
437 | //printf(" <- script ht.%d i.%d j.%d value %.8f %s\n",height,i,j,dstr(value),ASSETCHAINS_SYMBOL); | |
6dba48ec | 438 | if ( opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 ) |
439 | { | |
5a286a6f | 440 | for (k=0; k<32; k++) |
441 | if ( scriptbuf[len+k] != 0 ) | |
442 | break; | |
443 | if ( k == 32 ) | |
6dba48ec | 444 | { |
445 | *isratificationp = 1; | |
446 | printf("ISRATIFICATION (%s)\n",(char *)&scriptbuf[len+32*2+4]); | |
447 | } | |
448 | } | |
8f3aa743 | 449 | |
c9e4d982 | 450 | if ( *isratificationp == 0 && (signedmask != 0 || (scriptbuf[len] != 'X' && scriptbuf[len] != 'A')) ) // && scriptbuf[len] != 'I') |
e155dbe9 | 451 | komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen,j); |
eb406ebd | 452 | } |
1390456b | 453 | } |
454 | return(notaryid); | |
455 | } | |
456 | ||
58033467 | 457 | /*int32_t komodo_isratify(int32_t isspecial,int32_t numvalid) |
f38345e9 | 458 | { |
755ead98 | 459 | if ( isspecial != 0 && numvalid >= KOMODO_MINRATIFY ) |
f38345e9 | 460 | return(1); |
461 | else return(0); | |
58033467 | 462 | }*/ |
f38345e9 | 463 | |
464 | // Special tx have vout[0] -> CRYPTO777 | |
755ead98 | 465 | // with more than KOMODO_MINRATIFY pay2pubkey outputs -> ratify |
f38345e9 | 466 | // if all outputs to notary -> notary utxo |
467 | // if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed | |
420d7311 | 468 | // OP_RETURN: 'D' -> deposit, 'W' -> withdraw |
ab918767 | 469 | |
6f3cb4d0 | 470 | int32_t gettxout_scriptPubKey(uint8_t *scriptPubKey,int32_t maxsize,uint256 txid,int32_t n); |
471 | ||
472 | int32_t komodo_notarycmp(uint8_t *scriptPubKey,int32_t scriptlen,uint8_t pubkeys[64][33],int32_t numnotaries,uint8_t rmd160[20]) | |
473 | { | |
474 | int32_t i; | |
475 | if ( scriptlen == 25 && memcmp(&scriptPubKey[3],rmd160,20) == 0 ) | |
476 | return(0); | |
477 | else if ( scriptlen == 35 ) | |
478 | { | |
479 | for (i=0; i<numnotaries; i++) | |
480 | if ( memcmp(&scriptPubKey[1],pubkeys[i],33) == 0 ) | |
481 | return(i); | |
482 | } | |
483 | return(-1); | |
484 | } | |
f38345e9 | 485 | |
651989c7 | 486 | void komodo_connectblock(CBlockIndex *pindex,CBlock& block) |
50027f06 | 487 | { |
a7f0d3e4 | 488 | static int32_t hwmheight; |
9273e756 | 489 | uint64_t signedmask,voutmask; char symbol[16],dest[16]; struct komodo_state *sp; |
ab918767 | 490 | uint8_t scriptbuf[4096],pubkeys[64][33],rmd160[20],scriptPubKey[35]; uint256 kmdtxid,zero,btctxid,txhash; |
ea365a71 | 491 | int32_t i,j,k,numnotaries,notarized,scriptlen,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count; |
878d3961 | 492 | memset(&zero,0,sizeof(zero)); |
c93dc546 | 493 | komodo_init(pindex->nHeight); |
d7227bf6 | 494 | KOMODO_INITDONE = (uint32_t)time(NULL); |
ab918767 | 495 | if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) |
3df53390 | 496 | { |
497 | fprintf(stderr,"unexpected null komodostateptr.[%s]\n",ASSETCHAINS_SYMBOL); | |
ab918767 | 498 | return; |
3df53390 | 499 | } |
e0440cc3 | 500 | //fprintf(stderr,"%s connect.%d\n",ASSETCHAINS_SYMBOL,pindex->nHeight); |
7589f03a | 501 | numnotaries = komodo_notaries(pubkeys,pindex->nHeight); |
6f3cb4d0 | 502 | calc_rmd160_sha256(rmd160,pubkeys[0],33); |
a7f0d3e4 | 503 | if ( pindex->nHeight > hwmheight ) |
504 | hwmheight = pindex->nHeight; | |
505 | else | |
506 | { | |
174cae41 | 507 | if ( pindex->nHeight != hwmheight ) |
508 | 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 | 509 | komodo_event_rewind(sp,symbol,pindex->nHeight); |
e155dbe9 | 510 | komodo_stateupdate(pindex->nHeight,0,0,0,zero,0,0,0,0,-pindex->nHeight,pindex->nTime,0,0,0,0); |
a7f0d3e4 | 511 | } |
c75c18fc | 512 | komodo_currentheight_set(chainActive.Tip()->nHeight); |
68916cc6 | 513 | if ( pindex != 0 ) |
b501ded2 | 514 | { |
656eddcd | 515 | height = pindex->nHeight; |
01cc012f | 516 | txn_count = block.vtx.size(); |
517 | for (i=0; i<txn_count; i++) | |
dc64de68 | 518 | { |
352f8081 | 519 | txhash = block.vtx[i].GetHash(); |
01cc012f | 520 | numvouts = block.vtx[i].vout.size(); |
352f8081 | 521 | notaryid = -1; |
ea365a71 | 522 | voutmask = specialtx = notarizedheight = isratification = notarized = 0; |
517b1691 | 523 | signedmask = (height < 91400) ? 1 : 0; |
ea365a71 | 524 | numvins = block.vtx[i].vin.size(); |
525 | for (j=0; j<numvins; j++) | |
526 | { | |
69383c9f | 527 | if ( i == 0 && j == 0 ) |
528 | continue; | |
ea365a71 | 529 | if ( (scriptlen= gettxout_scriptPubKey(scriptPubKey,sizeof(scriptPubKey),block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) > 0 ) |
530 | { | |
531 | if ( (k= komodo_notarycmp(scriptPubKey,scriptlen,pubkeys,numnotaries,rmd160)) >= 0 ) | |
532 | signedmask |= (1LL << k); | |
c7918614 | 533 | else if ( 0 && numvins >= 17 ) |
bb1963ca | 534 | { |
535 | int32_t k; | |
536 | for (k=0; k<scriptlen; k++) | |
537 | printf("%02x",scriptPubKey[k]); | |
57d9cfcc | 538 | printf(" scriptPubKey doesnt match any notary vini.%d of %d\n",j,numvins); |
bb1963ca | 539 | } |
540 | } else printf("cant get scriptPubKey for ht.%d txi.%d vin.%d\n",height,i,j); | |
ea365a71 | 541 | } |
e6d7eca6 | 542 | numvalid = bitweight(signedmask); |
96e6053c | 543 | if ( (((height < 90000 || (signedmask & 1) != 0) && numvalid >= KOMODO_MINRATIFY) || numvalid > (numnotaries/5)) ) |
ea365a71 | 544 | { |
41f5997a | 545 | printf("%s ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d <<<<<<<<<<< notarized\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts); |
ea365a71 | 546 | notarized = 1; |
547 | } | |
e8b7db1a | 548 | if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 ) |
9e5fc6d7 | 549 | printf("(tx.%d: ",i); |
01cc012f | 550 | for (j=0; j<numvouts; j++) |
dc64de68 | 551 | { |
081ab21c | 552 | /*if ( i == 0 && j == 0 ) |
6615cec9 | 553 | { |
554 | uint8_t *script = (uint8_t *)block.vtx[0].vout[numvouts-1].scriptPubKey.data(); | |
555 | if ( numvouts <= 2 || script[0] != 0x6a ) | |
556 | { | |
557 | if ( numvouts == 2 && block.vtx[0].vout[1].nValue != 0 ) | |
558 | { | |
559 | fprintf(stderr,"ht.%d numvouts.%d value %.8f\n",height,numvouts,dstr(block.vtx[0].vout[1].nValue)); | |
49508d3c | 560 | if ( height >= 235300 && block.vtx[0].vout[1].nValue >= 100000*COIN ) |
6615cec9 | 561 | block.vtx[0].vout[1].nValue = 0; |
562 | break; | |
563 | } | |
564 | } | |
081ab21c | 565 | }*/ |
e8b7db1a | 566 | if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 ) |
9e5fc6d7 | 567 | printf("%.8f ",dstr(block.vtx[i].vout[j].nValue)); |
1390456b | 568 | len = block.vtx[i].vout[j].scriptPubKey.size(); |
97561034 | 569 | if ( len >= sizeof(uint32_t) && len <= sizeof(scriptbuf) ) |
01cc012f | 570 | { |
189d9dee | 571 | #ifdef KOMODO_ZCASH |
484f8777 | 572 | memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len); |
189d9dee | 573 | #else |
574 | memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len); | |
575 | #endif | |
8f40a7a2 | 576 | 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 | 577 | if ( 0 && i > 0 ) |
e69a0833 | 578 | { |
579 | for (k=0; k<len; k++) | |
580 | printf("%02x",scriptbuf[k]); | |
a8832194 | 581 | 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 | 582 | } |
01cc012f | 583 | } |
352f8081 | 584 | } |
e8b7db1a | 585 | if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 ) |
9e5fc6d7 | 586 | printf(") "); |
bbc4bbc0 | 587 | //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 | 588 | if ( notarized != 0 && (notarizedheight != 0 || specialtx != 0) ) |
1390456b | 589 | { |
b338ea90 | 590 | if ( isratification != 0 ) |
591 | { | |
592 | 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); | |
593 | printf("ht.%d specialtx.%d isratification.%d numvouts.%d signed.%llx numnotaries.%d\n",height,specialtx,isratification,numvouts,(long long)signedmask,numnotaries); | |
594 | } | |
d2a115d8 | 595 | if ( specialtx != 0 && isratification != 0 && numvouts > 2 ) |
84e843cd | 596 | { |
29e69b85 | 597 | numvalid = 0; |
dd7b22ad | 598 | memset(pubkeys,0,sizeof(pubkeys)); |
6dba48ec | 599 | for (j=1; j<numvouts-1; j++) |
84e843cd | 600 | { |
601 | len = block.vtx[i].vout[j].scriptPubKey.size(); | |
0554de3c | 602 | if ( len >= sizeof(uint32_t) && len <= sizeof(scriptbuf) ) |
84e843cd | 603 | { |
189d9dee | 604 | #ifdef KOMODO_ZCASH |
484f8777 | 605 | memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len); |
189d9dee | 606 | #else |
607 | memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len); | |
608 | #endif | |
84e843cd | 609 | if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) |
610 | { | |
29e69b85 | 611 | memcpy(pubkeys[numvalid++],scriptbuf+1,33); |
e7f99312 | 612 | for (k=0; k<33; k++) |
84e843cd | 613 | printf("%02x",scriptbuf[k+1]); |
614 | printf(" <- new notary.[%d]\n",j-1); | |
615 | } | |
616 | } | |
617 | } | |
aa114a60 | 618 | if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries/3) ) |
d8ce705a | 619 | { |
620 | memset(&txhash,0,sizeof(txhash)); | |
e155dbe9 | 621 | komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0,0); |
f9d034d4 | 622 | 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 | 623 | } else printf("signedmask.%llx numvalid.%d wt.%d numnotaries.%d\n",(long long)signedmask,numvalid,bitweight(signedmask),numnotaries); |
84e843cd | 624 | } |
a96439f5 | 625 | } |
656eddcd | 626 | } |
e8b7db1a | 627 | if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 ) |
ac93bb3e | 628 | printf("%s ht.%d\n",ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL,height); |
e155dbe9 | 629 | if ( pindex->nHeight == hwmheight ) |
8afdc8e7 | 630 | komodo_stateupdate(height,0,0,0,zero,0,0,0,0,height,(uint32_t)pindex->nTime,0,0,0,0); |
3df53390 | 631 | } else fprintf(stderr,"komodo_connectblock: unexpected null pindex\n"); |
17899df4 | 632 | //KOMODO_INITDONE = (uint32_t)time(NULL); |
dd42fd9b | 633 | //fprintf(stderr,"%s end connect.%d\n",ASSETCHAINS_SYMBOL,pindex->nHeight); |
3d35aa5b | 634 | } |
635 | ||
0f24f245 | 636 | |
fcd36118 | 637 | #endif |