]>
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 | |
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 | { | |
89 | printf("updated %d pubkeys at ht.%d\n",num,ht); | |
1ebb14e7 | 90 | if ( matched != 0 ) // private state -> global Pubkeys |
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++; | |
1ebb14e7 | 103 | printf("load %s NOTARIZED %d %s\n",ASSETCHAINS_SYMBOL,notarized_height,notarized_hash.ToString().c_str()); |
104 | //if ( matched != 0 ) global independent states -> inside *sp | |
105 | komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height); | |
c75c18fc | 106 | } |
1ebb14e7 | 107 | else if ( func == 'U' ) // deprecated |
c75c18fc | 108 | { |
109 | uint8_t n,nid; uint256 hash; uint64_t mask; | |
110 | n = fgetc(fp); | |
111 | nid = fgetc(fp); | |
112 | //printf("U %d %d\n",n,nid); | |
113 | if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) ) | |
114 | errs++; | |
115 | if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) ) | |
116 | errs++; | |
6f3cb4d0 | 117 | //if ( matched != 0 ) |
118 | // komodo_eventadd_utxo(sp,symbol,ht,nid,hash,mask,n); | |
c75c18fc | 119 | } |
120 | else if ( func == 'K' ) | |
121 | { | |
122 | int32_t kheight; | |
123 | if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) ) | |
124 | errs++; | |
1ebb14e7 | 125 | //if ( matched != 0 ) global independent states -> inside *sp |
c75c18fc | 126 | komodo_eventadd_kmdheight(sp,symbol,ht,kheight); |
127 | } | |
128 | else if ( func == 'R' ) | |
129 | { | |
130 | uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[10000]; | |
131 | if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) ) | |
132 | errs++; | |
133 | if ( fread(&v,1,sizeof(v),fp) != sizeof(v) ) | |
134 | errs++; | |
135 | if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) ) | |
136 | errs++; | |
137 | if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) ) | |
138 | errs++; | |
139 | if ( olen < sizeof(opret) ) | |
140 | { | |
141 | if ( fread(opret,1,olen,fp) != olen ) | |
142 | errs++; | |
1ebb14e7 | 143 | //if ( matched != 0 ) global shared state -> global PAX |
2df2f193 | 144 | komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen); |
c75c18fc | 145 | } else printf("illegal olen.%u\n",olen); |
146 | } | |
147 | else if ( func == 'D' ) | |
148 | { | |
149 | printf("unexpected function D[%d]\n",ht); | |
150 | } | |
151 | else if ( func == 'V' ) | |
152 | { | |
153 | int32_t numpvals; uint32_t pvals[128]; | |
154 | numpvals = fgetc(fp); | |
155 | if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals ) | |
156 | { | |
1ebb14e7 | 157 | //if ( matched != 0 ) global shared state -> global PVALS |
2df2f193 | 158 | komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals); |
c75c18fc | 159 | //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals); |
160 | } else printf("error loading pvals[%d]\n",numpvals); | |
161 | } | |
162 | else printf("illegal func.(%d %c)\n",func,func); | |
163 | return(func); | |
164 | } else return(-1); | |
165 | } | |
166 | ||
429dabb5 | 167 | 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 | 168 | { |
c75c18fc | 169 | static FILE *fp; static int32_t errs; |
e21ffa1d | 170 | struct komodo_state *sp; char fname[512],symbol[16],dest[16]; int32_t ht,func; uint8_t num,pubkeys[64][33]; |
c75c18fc | 171 | if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) |
172 | return; | |
888cf827 | 173 | if ( fp == 0 ) |
174 | { | |
ab918767 | 175 | komodo_statefname(fname,ASSETCHAINS_SYMBOL); |
bafc61f3 | 176 | if ( (fp= fopen(fname,"rb+")) != 0 ) |
177 | { | |
c75c18fc | 178 | while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 ) |
179 | ; | |
bafc61f3 | 180 | } else fp = fopen(fname,"wb+"); |
181 | printf("fname.(%s) fpos.%ld\n",fname,ftell(fp)); | |
63c30610 | 182 | KOMODO_INITDONE = (uint32_t)time(NULL); |
bafc61f3 | 183 | } |
e98e2aec | 184 | if ( height <= 0 ) |
49df008c | 185 | { |
6458297e | 186 | //printf("early return: stateupdate height.%d\n",height); |
49df008c | 187 | return; |
188 | } | |
3eea72f2 | 189 | if ( fp != 0 ) // write out funcid, height, other fields, call side effect function |
bafc61f3 | 190 | { |
2578b002 | 191 | //printf("fpos.%ld ",ftell(fp)); |
ab918767 | 192 | if ( KMDheight != 0 ) |
2b84e06c | 193 | { |
194 | fputc('K',fp); | |
195 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) | |
196 | errs++; | |
197 | if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) ) | |
198 | errs++; | |
c75c18fc | 199 | komodo_eventadd_kmdheight(sp,symbol,height,KMDheight); |
2b84e06c | 200 | } |
01f9cb3d | 201 | else if ( opretbuf != 0 && opretlen > 0 ) |
64bb0834 | 202 | { |
01f9cb3d | 203 | uint16_t olen = opretlen; |
204 | fputc('R',fp); | |
64bb0834 | 205 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
206 | errs++; | |
429dabb5 | 207 | if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) ) |
208 | errs++; | |
209 | if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) ) | |
210 | errs++; | |
30df2e00 | 211 | if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) ) |
33935bd0 | 212 | errs++; |
289dc57b | 213 | if ( fwrite(&olen,1,sizeof(olen),fp) != olen ) |
214 | errs++; | |
01f9cb3d | 215 | if ( fwrite(opretbuf,1,olen,fp) != olen ) |
64bb0834 | 216 | errs++; |
1938746c | 217 | //printf("ht.%d R opret[%d]\n",height,olen); |
c75c18fc | 218 | //komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout); |
219 | komodo_eventadd_opreturn(sp,symbol,height,txhash,opretvalue,vout,opretbuf,olen); | |
01f9cb3d | 220 | } |
9997caa0 | 221 | else if ( notarypubs != 0 && numnotaries > 0 ) |
bafc61f3 | 222 | { |
f3a1de3a | 223 | printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs); |
bafc61f3 | 224 | fputc('P',fp); |
c50ec0fb | 225 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
226 | errs++; | |
bafc61f3 | 227 | fputc(numnotaries,fp); |
52ed4002 | 228 | if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries ) |
229 | errs++; | |
c75c18fc | 230 | komodo_eventadd_pubkeys(sp,symbol,height,numnotaries,notarypubs); |
bafc61f3 | 231 | } |
671187dd | 232 | else if ( voutmask != 0 && numvouts > 0 ) |
233 | { | |
2578b002 | 234 | //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash)); |
671187dd | 235 | fputc('U',fp); |
c50ec0fb | 236 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
237 | errs++; | |
671187dd | 238 | fputc(numvouts,fp); |
239 | fputc(notaryid,fp); | |
240 | if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) ) | |
241 | errs++; | |
242 | if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) ) | |
243 | errs++; | |
6f3cb4d0 | 244 | //komodo_eventadd_utxo(sp,symbol,height,notaryid,txhash,voutmask,numvouts); |
671187dd | 245 | } |
b673d264 | 246 | //#ifdef KOMODO_PAX |
4a41b0b2 | 247 | else if ( pvals != 0 && numpvals > 0 ) |
248 | { | |
bfa5b4f2 | 249 | int32_t i,nonz = 0; |
1bf82154 | 250 | for (i=0; i<32; i++) |
251 | if ( pvals[i] != 0 ) | |
252 | nonz++; | |
253 | if ( nonz >= 32 ) | |
254 | { | |
255 | fputc('V',fp); | |
256 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) | |
257 | errs++; | |
258 | fputc(numpvals,fp); | |
259 | if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals ) | |
260 | errs++; | |
c75c18fc | 261 | komodo_eventadd_pricefeed(sp,symbol,height,pvals,numpvals); |
2578b002 | 262 | //printf("ht.%d V numpvals[%d]\n",height,numpvals); |
1bf82154 | 263 | } |
ef7a3548 | 264 | //printf("save pvals height.%d numpvals.%d\n",height,numpvals); |
4a41b0b2 | 265 | } |
b673d264 | 266 | //#endif |
6dabcca4 | 267 | else if ( height != 0 ) |
671187dd | 268 | { |
2578b002 | 269 | //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs); |
671187dd | 270 | fputc('N',fp); |
c50ec0fb | 271 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
272 | errs++; | |
c75c18fc | 273 | if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) ) |
671187dd | 274 | errs++; |
c75c18fc | 275 | if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) ) |
671187dd | 276 | errs++; |
c75c18fc | 277 | if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) ) |
671187dd | 278 | errs++; |
c75c18fc | 279 | komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT); |
280 | //komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID); | |
671187dd | 281 | } |
47f47733 | 282 | fflush(fp); |
bafc61f3 | 283 | } |
284 | } | |
285 | ||
6dba48ec | 286 | 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 | 287 | { |
2df2f193 | 288 | 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]; |
289 | if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) | |
290 | return(-1); | |
9c406099 | 291 | if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) |
1390456b | 292 | { |
c23dd601 | 293 | decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR); |
986d3459 | 294 | /*for (k=0; k<33; k++) |
e1be360e | 295 | printf("%02x",crypto777[k]); |
296 | printf(" crypto777 "); | |
297 | for (k=0; k<scriptlen; k++) | |
298 | printf("%02x",scriptbuf[k]); | |
986d3459 | 299 | printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/ |
1390456b | 300 | if ( memcmp(crypto777,scriptbuf+1,33) == 0 ) |
301 | { | |
302 | *specialtxp = 1; | |
b7f942c8 | 303 | //printf(">>>>>>>> "); |
1390456b | 304 | } |
a5f315c7 | 305 | else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 ) |
1390456b | 306 | { |
84e843cd | 307 | //printf("found notary.k%d\n",k); |
308 | if ( notaryid < 64 ) | |
1390456b | 309 | { |
84e843cd | 310 | if ( notaryid < 0 ) |
311 | { | |
a5f315c7 | 312 | notaryid = nid; |
84e843cd | 313 | *voutmaskp |= (1LL << j); |
314 | } | |
a5f315c7 | 315 | else if ( notaryid != nid ) |
84e843cd | 316 | { |
b7f942c8 | 317 | //for (i=0; i<33; i++) |
318 | // printf("%02x",scriptbuf[i+1]); | |
319 | //printf(" %s mismatch notaryid.%d k.%d\n",ASSETCHAINS_SYMBOL,notaryid,nid); | |
84e843cd | 320 | notaryid = 64; |
321 | *voutmaskp = 0; | |
322 | } | |
323 | else *voutmaskp |= (1LL << j); | |
1390456b | 324 | } |
1390456b | 325 | } |
326 | } | |
844a6138 | 327 | if ( scriptbuf[len++] == 0x6a ) |
1390456b | 328 | { |
329 | if ( (opretlen= scriptbuf[len++]) == 0x4c ) | |
330 | opretlen = scriptbuf[len++]; | |
331 | else if ( opretlen == 0x4d ) | |
332 | { | |
333 | opretlen = scriptbuf[len++]; | |
334 | opretlen = (opretlen << 8) + scriptbuf[len++]; | |
335 | } | |
a6400079 | 336 | if ( j == 1 && opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 ) |
1390456b | 337 | { |
4a41b0b2 | 338 | len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid); |
339 | len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp); | |
3eea72f2 | 340 | len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid); |
c75c18fc | 341 | if ( *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height ) |
1390456b | 342 | { |
1ebb14e7 | 343 | printf("%s ht.%d NOTARIZED.%d %s.%s %sTXID.%s (%s)\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]); |
c75c18fc | 344 | sp->NOTARIZED_HEIGHT = *notarizedheightp; |
345 | sp->NOTARIZED_HASH = kmdtxid; | |
346 | sp->NOTARIZED_DESTTXID = desttxid; | |
429dabb5 | 347 | komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0); |
ab918767 | 348 | // extract X opreturns here |
a6400079 | 349 | } 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 | 350 | } |
babdc001 | 351 | else if ( i == 0 && j == 1 && opretlen == 149 ) |
0201d1a6 | 352 | komodo_paxpricefeed(height,&scriptbuf[len],opretlen); |
eb406ebd | 353 | else |
354 | { | |
6806478b | 355 | int32_t k; for (k=0; k<scriptlen; k++) |
356 | printf("%02x",scriptbuf[k]); | |
357 | printf(" <- script ht.%d i.%d j.%d value %.8f\n",height,i,j,dstr(value)); | |
6dba48ec | 358 | if ( opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 ) |
359 | { | |
5a286a6f | 360 | for (k=0; k<32; k++) |
361 | if ( scriptbuf[len+k] != 0 ) | |
362 | break; | |
363 | if ( k == 32 ) | |
6dba48ec | 364 | { |
365 | *isratificationp = 1; | |
366 | printf("ISRATIFICATION (%s)\n",(char *)&scriptbuf[len+32*2+4]); | |
367 | } | |
368 | } | |
369 | if ( *isratificationp == 0 ) | |
370 | komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,value,&scriptbuf[len],opretlen,j); | |
eb406ebd | 371 | } |
1390456b | 372 | } |
373 | return(notaryid); | |
374 | } | |
375 | ||
f38345e9 | 376 | int32_t komodo_isratify(int32_t isspecial,int32_t numvalid) |
377 | { | |
755ead98 | 378 | if ( isspecial != 0 && numvalid >= KOMODO_MINRATIFY ) |
f38345e9 | 379 | return(1); |
380 | else return(0); | |
381 | } | |
382 | ||
383 | // Special tx have vout[0] -> CRYPTO777 | |
755ead98 | 384 | // with more than KOMODO_MINRATIFY pay2pubkey outputs -> ratify |
f38345e9 | 385 | // if all outputs to notary -> notary utxo |
386 | // if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed | |
420d7311 | 387 | // OP_RETURN: 'D' -> deposit, 'W' -> withdraw |
ab918767 | 388 | |
6f3cb4d0 | 389 | int32_t gettxout_scriptPubKey(uint8_t *scriptPubKey,int32_t maxsize,uint256 txid,int32_t n); |
390 | ||
391 | int32_t komodo_notarycmp(uint8_t *scriptPubKey,int32_t scriptlen,uint8_t pubkeys[64][33],int32_t numnotaries,uint8_t rmd160[20]) | |
392 | { | |
393 | int32_t i; | |
394 | if ( scriptlen == 25 && memcmp(&scriptPubKey[3],rmd160,20) == 0 ) | |
395 | return(0); | |
396 | else if ( scriptlen == 35 ) | |
397 | { | |
398 | for (i=0; i<numnotaries; i++) | |
399 | if ( memcmp(&scriptPubKey[1],pubkeys[i],33) == 0 ) | |
400 | return(i); | |
401 | } | |
402 | return(-1); | |
403 | } | |
f38345e9 | 404 | |
651989c7 | 405 | void komodo_connectblock(CBlockIndex *pindex,CBlock& block) |
50027f06 | 406 | { |
a7f0d3e4 | 407 | static int32_t hwmheight; |
9273e756 | 408 | uint64_t signedmask,voutmask; char symbol[16],dest[16]; struct komodo_state *sp; |
ab918767 | 409 | uint8_t scriptbuf[4096],pubkeys[64][33],rmd160[20],scriptPubKey[35]; uint256 kmdtxid,zero,btctxid,txhash; |
6f3cb4d0 | 410 | int32_t i,j,k,numnotaries,scriptlen,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count; |
878d3961 | 411 | memset(&zero,0,sizeof(zero)); |
c93dc546 | 412 | komodo_init(pindex->nHeight); |
ab918767 | 413 | if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) |
414 | return; | |
7589f03a | 415 | numnotaries = komodo_notaries(pubkeys,pindex->nHeight); |
6f3cb4d0 | 416 | calc_rmd160_sha256(rmd160,pubkeys[0],33); |
a7f0d3e4 | 417 | if ( pindex->nHeight > hwmheight ) |
418 | hwmheight = pindex->nHeight; | |
419 | else | |
420 | { | |
421 | printf("hwmheight.%d vs pindex->nHeight.%d reorg.%d\n",hwmheight,pindex->nHeight,hwmheight-pindex->nHeight); | |
ab918767 | 422 | komodo_event_rewind(sp,symbol,pindex->nHeight); |
423 | komodo_stateupdate(pindex->nHeight,0,0,0,zero,0,0,0,0,-pindex->nHeight,0,0,0,0); | |
a7f0d3e4 | 424 | } |
c75c18fc | 425 | komodo_currentheight_set(chainActive.Tip()->nHeight); |
ab918767 | 426 | /*if ( komodo_is_issuer() != 0 ) |
2d657c5b | 427 | { |
f67800d0 | 428 | while ( KOMODO_REALTIME == 0 || time(NULL) <= KOMODO_REALTIME ) |
2d657c5b | 429 | { |
1fdfe03c | 430 | fprintf(stderr,"komodo_connect.(%s) waiting for realtime RT.%u now.%u\n",ASSETCHAINS_SYMBOL,KOMODO_REALTIME,(uint32_t)time(NULL)); |
cc66cf72 | 431 | sleep(30); |
2d657c5b | 432 | } |
ab918767 | 433 | }*/ |
434 | sp->KOMODO_REALTIME = KOMODO_INITDONE = (uint32_t)time(NULL); | |
68916cc6 | 435 | if ( pindex != 0 ) |
b501ded2 | 436 | { |
656eddcd | 437 | height = pindex->nHeight; |
01cc012f | 438 | txn_count = block.vtx.size(); |
439 | for (i=0; i<txn_count; i++) | |
dc64de68 | 440 | { |
352f8081 | 441 | txhash = block.vtx[i].GetHash(); |
01cc012f | 442 | numvouts = block.vtx[i].vout.size(); |
352f8081 | 443 | notaryid = -1; |
6dba48ec | 444 | voutmask = specialtx = notarizedheight = isratification = 0; |
01cc012f | 445 | for (j=0; j<numvouts; j++) |
dc64de68 | 446 | { |
1390456b | 447 | len = block.vtx[i].vout[j].scriptPubKey.size(); |
448 | if ( len <= sizeof(scriptbuf) ) | |
01cc012f | 449 | { |
189d9dee | 450 | #ifdef KOMODO_ZCASH |
484f8777 | 451 | memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len); |
189d9dee | 452 | #else |
453 | memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len); | |
454 | #endif | |
6dba48ec | 455 | notaryid = komodo_voutupdate(&isratification,notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,¬arizedheight,(uint64_t)block.vtx[i].vout[j].nValue); |
a01acef4 | 456 | if ( 0 && i > 0 ) |
e69a0833 | 457 | { |
458 | for (k=0; k<len; k++) | |
459 | printf("%02x",scriptbuf[k]); | |
a8832194 | 460 | 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 | 461 | } |
01cc012f | 462 | } |
352f8081 | 463 | } |
1390456b | 464 | signedmask = 0; |
465 | numvins = block.vtx[i].vin.size(); | |
5bdebffe | 466 | for (j=0; j<numvins; j++) |
352f8081 | 467 | { |
6f3cb4d0 | 468 | if ( (scriptlen= gettxout_scriptPubKey(scriptPubKey,sizeof(scriptPubKey),block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) > 0 ) |
469 | { | |
470 | if ( (k= komodo_notarycmp(scriptPubKey,scriptlen,pubkeys,numnotaries,rmd160)) >= 0 ) | |
471 | signedmask |= (1LL << k); | |
472 | } | |
1390456b | 473 | } |
6f3cb4d0 | 474 | if ( signedmask != 0 ) |
475 | printf("ht.%d signedmask.%llx numvins.%d numvouts.%d\n",height,(long long)signedmask,numvins,numvouts); | |
06f0ed43 | 476 | if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) ) |
1390456b | 477 | { |
e69a0833 | 478 | printf("NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",(long long)signedmask,numvins,height,i,notarizedheight,specialtx); |
7d51d973 | 479 | printf("ht.%d specialtx.%d isratification.%d numvouts.%d signed.%llx numnotaries.%d\n",height,specialtx,isratification,numvouts,(long long)signedmask,numnotaries); |
d2a115d8 | 480 | if ( specialtx != 0 && isratification != 0 && numvouts > 2 ) |
84e843cd | 481 | { |
29e69b85 | 482 | numvalid = 0; |
dd7b22ad | 483 | memset(pubkeys,0,sizeof(pubkeys)); |
6dba48ec | 484 | for (j=1; j<numvouts-1; j++) |
84e843cd | 485 | { |
486 | len = block.vtx[i].vout[j].scriptPubKey.size(); | |
487 | if ( len <= sizeof(scriptbuf) ) | |
488 | { | |
189d9dee | 489 | #ifdef KOMODO_ZCASH |
484f8777 | 490 | memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len); |
189d9dee | 491 | #else |
492 | memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len); | |
493 | #endif | |
84e843cd | 494 | if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) |
495 | { | |
29e69b85 | 496 | memcpy(pubkeys[numvalid++],scriptbuf+1,33); |
e7f99312 | 497 | for (k=0; k<33; k++) |
84e843cd | 498 | printf("%02x",scriptbuf[k+1]); |
499 | printf(" <- new notary.[%d]\n",j-1); | |
500 | } | |
501 | } | |
502 | } | |
7589f03a | 503 | if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries>>1) ) |
d8ce705a | 504 | { |
505 | memset(&txhash,0,sizeof(txhash)); | |
429dabb5 | 506 | komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0); |
f9d034d4 | 507 | 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 | 508 | } else printf("signedmask.%llx numvalid.%d wt.%d numnotaries.%d\n",(long long)signedmask,numvalid,bitweight(signedmask),numnotaries); |
84e843cd | 509 | } |
a96439f5 | 510 | } |
656eddcd | 511 | } |
ab918767 | 512 | if ( pindex->nHeight == hwmheight && (hwmheight % 100) == 0 ) |
513 | komodo_stateupdate(height,0,0,0,zero,0,0,0,0,height,0,0,0,0); | |
44c4fbbd | 514 | } else printf("komodo_connectblock: unexpected null pindex\n"); |
63c30610 | 515 | KOMODO_INITDONE = (uint32_t)time(NULL); |
3d35aa5b | 516 | } |
517 | ||
0f24f245 | 518 | |
fcd36118 | 519 | #endif |