]>
Commit | Line | Data |
---|---|---|
fcd36118 | 1 | /****************************************************************************** |
713c2a94 | 2 | * Copyright © 2014-2018 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 | |
5416af1d | 18 | #include "komodo_defs.h" |
fcd36118 | 19 | |
b71b045a | 20 | #ifdef _WIN32 |
21 | #define printf(...) | |
22 | #endif | |
23 | ||
27bf3c5e | 24 | // Todo: |
aa114a60 | 25 | // verify: reorgs |
3ec03ada | 26 | |
26ac06c4 | 27 | #define KOMODO_ASSETCHAINS_WAITNOTARIZE |
fa8d44fc | 28 | #define KOMODO_PAXMAX (10000 * COIN) |
aa114a60 | 29 | |
9499e4de | 30 | #include <stdint.h> |
31 | #include <stdio.h> | |
975b2ddb | 32 | #include <pthread.h> |
88f973c2 | 33 | #include <ctype.h> |
6d5cb6d4 | 34 | #include "uthash.h" |
35 | #include "utlist.h" | |
9499e4de | 36 | |
fc318ffe | 37 | int32_t gettxout_scriptPubKey(uint8_t *scriptPubkey,int32_t maxsize,uint256 txid,int32_t n); |
f3a1de3a | 38 | void komodo_event_rewind(struct komodo_state *sp,char *symbol,int32_t height); |
875cf683 | 39 | void komodo_connectblock(CBlockIndex *pindex,CBlock& block); |
3face669 | 40 | |
fc318ffe | 41 | #include "komodo_structs.h" |
9fb37168 | 42 | #include "komodo_globals.h" |
46beb55e | 43 | #include "komodo_utils.h" |
6b5cfbb4 | 44 | #include "komodo_curve25519.h" |
1bfdde1d | 45 | |
d430a5e8 | 46 | #include "komodo_cJSON.c" |
e4e76989 | 47 | #include "komodo_bitcoind.h" |
48 | #include "komodo_interest.h" | |
49 | #include "komodo_pax.h" | |
50 | #include "komodo_notary.h" | |
ab918767 | 51 | |
52 | int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest); | |
6b5cfbb4 | 53 | #include "komodo_kv.h" |
eeaaf554 | 54 | #include "komodo_jumblr.h" |
e4e76989 | 55 | #include "komodo_gateway.h" |
56 | #include "komodo_events.h" | |
713c2a94 | 57 | #include "komodo_ccdata.h" |
e4e76989 | 58 | |
c75c18fc | 59 | void komodo_currentheight_set(int32_t height) |
60 | { | |
7c130297 | 61 | char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; |
c75c18fc | 62 | if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) |
63 | sp->CURRENT_HEIGHT = height; | |
64 | } | |
65 | ||
66 | int32_t komodo_currentheight() | |
67 | { | |
7c130297 | 68 | char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; |
c75c18fc | 69 | if ( (sp= komodo_stateptr(symbol,dest)) != 0 ) |
70 | return(sp->CURRENT_HEIGHT); | |
71 | else return(0); | |
72 | } | |
73 | ||
74 | int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest) | |
75 | { | |
76 | static int32_t errs; | |
18c6cfce | 77 | int32_t func,ht,notarized_height,num,matched=0,MoMdepth; uint256 MoM,notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33]; |
c75c18fc | 78 | if ( (func= fgetc(fp)) != EOF ) |
79 | { | |
80 | if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 ) | |
81 | matched = 1; | |
82 | else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0); | |
83 | if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) ) | |
84 | errs++; | |
34c68daa | 85 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && func != 'T' ) |
d0d61f8f | 86 | printf("[%s] matched.%d fpos.%ld func.(%d %c) ht.%d\n",ASSETCHAINS_SYMBOL,matched,ftell(fp),func,func,ht); |
c75c18fc | 87 | if ( func == 'P' ) |
88 | { | |
f3a1de3a | 89 | if ( (num= fgetc(fp)) <= 64 ) |
c75c18fc | 90 | { |
91 | if ( fread(pubkeys,33,num,fp) != num ) | |
92 | errs++; | |
93 | else | |
94 | { | |
ec368288 | 95 | //printf("updated %d pubkeys at %s ht.%d\n",num,symbol,ht); |
15724d02 | 96 | if ( (KOMODO_EXTERNAL_NOTARIES != 0 && matched != 0) || (strcmp(symbol,"KMD") == 0 && KOMODO_EXTERNAL_NOTARIES == 0) ) |
c75c18fc | 97 | komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys); |
98 | } | |
99 | } else printf("illegal num.%d\n",num); | |
100 | } | |
18c6cfce | 101 | else if ( func == 'N' || func == 'M' ) |
c75c18fc | 102 | { |
103 | if ( fread(¬arized_height,1,sizeof(notarized_height),fp) != sizeof(notarized_height) ) | |
104 | errs++; | |
105 | if ( fread(¬arized_hash,1,sizeof(notarized_hash),fp) != sizeof(notarized_hash) ) | |
106 | errs++; | |
107 | if ( fread(¬arized_desttxid,1,sizeof(notarized_desttxid),fp) != sizeof(notarized_desttxid) ) | |
108 | errs++; | |
18c6cfce | 109 | if ( func == 'M' ) |
110 | { | |
111 | if ( fread(&MoM,1,sizeof(MoM),fp) != sizeof(MoM) ) | |
112 | errs++; | |
113 | if ( fread(&MoMdepth,1,sizeof(MoMdepth),fp) != sizeof(MoMdepth) ) | |
114 | errs++; | |
115 | if ( 1 && ASSETCHAINS_SYMBOL[0] != 0 && sp != 0 ) | |
116 | printf("%s load[%s.%d -> %s] NOTARIZED %d %s MoM.%s %d\n",ASSETCHAINS_SYMBOL,symbol,sp->NUM_NPOINTS,dest,notarized_height,notarized_hash.ToString().c_str(),MoM.ToString().c_str(),MoMdepth); | |
117 | } | |
118 | else | |
119 | { | |
120 | memset(&MoM,0,sizeof(MoM)); | |
121 | MoMdepth = 0; | |
122 | } | |
1ebb14e7 | 123 | //if ( matched != 0 ) global independent states -> inside *sp |
18c6cfce | 124 | komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height,MoM,MoMdepth); |
c75c18fc | 125 | } |
1ebb14e7 | 126 | else if ( func == 'U' ) // deprecated |
c75c18fc | 127 | { |
128 | uint8_t n,nid; uint256 hash; uint64_t mask; | |
129 | n = fgetc(fp); | |
130 | nid = fgetc(fp); | |
131 | //printf("U %d %d\n",n,nid); | |
132 | if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) ) | |
133 | errs++; | |
134 | if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) ) | |
135 | errs++; | |
6f3cb4d0 | 136 | //if ( matched != 0 ) |
137 | // komodo_eventadd_utxo(sp,symbol,ht,nid,hash,mask,n); | |
c75c18fc | 138 | } |
139 | else if ( func == 'K' ) | |
140 | { | |
141 | int32_t kheight; | |
142 | if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) ) | |
143 | errs++; | |
1ebb14e7 | 144 | //if ( matched != 0 ) global independent states -> inside *sp |
a8f4e85e | 145 | //printf("%s.%d load[%s] ht.%d\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight); |
e155dbe9 | 146 | komodo_eventadd_kmdheight(sp,symbol,ht,kheight,0); |
147 | } | |
148 | else if ( func == 'T' ) | |
149 | { | |
150 | int32_t kheight,ktimestamp; | |
151 | if ( fread(&kheight,1,sizeof(kheight),fp) != sizeof(kheight) ) | |
152 | errs++; | |
153 | if ( fread(&ktimestamp,1,sizeof(ktimestamp),fp) != sizeof(ktimestamp) ) | |
154 | errs++; | |
155 | //if ( matched != 0 ) global independent states -> inside *sp | |
a8f4e85e | 156 | //printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp); |
e155dbe9 | 157 | komodo_eventadd_kmdheight(sp,symbol,ht,kheight,ktimestamp); |
c75c18fc | 158 | } |
159 | else if ( func == 'R' ) | |
160 | { | |
ef1a9636 | 161 | uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[16384]; |
c75c18fc | 162 | if ( fread(&txid,1,sizeof(txid),fp) != sizeof(txid) ) |
163 | errs++; | |
164 | if ( fread(&v,1,sizeof(v),fp) != sizeof(v) ) | |
165 | errs++; | |
166 | if ( fread(&ovalue,1,sizeof(ovalue),fp) != sizeof(ovalue) ) | |
167 | errs++; | |
168 | if ( fread(&olen,1,sizeof(olen),fp) != sizeof(olen) ) | |
169 | errs++; | |
170 | if ( olen < sizeof(opret) ) | |
171 | { | |
172 | if ( fread(opret,1,olen,fp) != olen ) | |
173 | errs++; | |
34c68daa | 174 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && matched != 0 ) |
e2a12abf | 175 | { |
176 | int32_t i; for (i=0; i<olen; i++) | |
177 | printf("%02x",opret[i]); | |
178 | printf(" %s.%d load[%s] opret[%c] len.%d %.8f\n",ASSETCHAINS_SYMBOL,ht,symbol,opret[0],olen,(double)ovalue/COIN); | |
179 | } | |
180 | komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen); // global shared state -> global PAX | |
939a5a45 | 181 | } else |
182 | { | |
66d02e79 | 183 | int32_t i; |
939a5a45 | 184 | for (i=0; i<olen; i++) |
185 | fgetc(fp); | |
dff4ef45 | 186 | //printf("illegal olen.%u\n",olen); |
939a5a45 | 187 | } |
c75c18fc | 188 | } |
189 | else if ( func == 'D' ) | |
190 | { | |
191 | printf("unexpected function D[%d]\n",ht); | |
192 | } | |
193 | else if ( func == 'V' ) | |
194 | { | |
195 | int32_t numpvals; uint32_t pvals[128]; | |
196 | numpvals = fgetc(fp); | |
197 | if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals ) | |
198 | { | |
1ebb14e7 | 199 | //if ( matched != 0 ) global shared state -> global PVALS |
335f2ea7 | 200 | //printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht); |
15724d02 | 201 | komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals); |
c75c18fc | 202 | //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals); |
203 | } else printf("error loading pvals[%d]\n",numpvals); | |
204 | } | |
7ba6b48d | 205 | else printf("[%s] %s illegal func.(%d %c)\n",ASSETCHAINS_SYMBOL,symbol,func,func); |
c75c18fc | 206 | return(func); |
207 | } else return(-1); | |
208 | } | |
17746e45 | 209 | |
210 | int32_t memread(void *dest,int32_t size,uint8_t *filedata,long *fposp,long datalen) | |
211 | { | |
212 | if ( *fposp+size <= datalen ) | |
213 | { | |
214 | memcpy(dest,&filedata[*fposp],size); | |
215 | (*fposp) += size; | |
216 | return(size); | |
217 | } | |
218 | return(-1); | |
219 | } | |
220 | ||
3b40d631 | 221 | int32_t komodo_parsestatefiledata(struct komodo_state *sp,uint8_t *filedata,long *fposp,long datalen,char *symbol,char *dest) |
17746e45 | 222 | { |
223 | static int32_t errs; | |
18c6cfce | 224 | int32_t func= -1,ht,notarized_height,MoMdepth,num,matched=0; uint256 MoM,notarized_hash,notarized_desttxid; uint8_t pubkeys[64][33]; long fpos = *fposp; |
17746e45 | 225 | if ( fpos < datalen ) |
226 | { | |
227 | func = filedata[fpos++]; | |
228 | if ( ASSETCHAINS_SYMBOL[0] == 0 && strcmp(symbol,"KMD") == 0 ) | |
229 | matched = 1; | |
230 | else matched = (strcmp(symbol,ASSETCHAINS_SYMBOL) == 0); | |
231 | if ( memread(&ht,sizeof(ht),filedata,&fpos,datalen) != sizeof(ht) ) | |
232 | errs++; | |
233 | if ( func == 'P' ) | |
234 | { | |
235 | if ( (num= filedata[fpos++]) <= 64 ) | |
236 | { | |
237 | if ( memread(pubkeys,33*num,filedata,&fpos,datalen) != 33*num ) | |
238 | errs++; | |
239 | else | |
240 | { | |
241 | //printf("updated %d pubkeys at %s ht.%d\n",num,symbol,ht); | |
242 | if ( (KOMODO_EXTERNAL_NOTARIES != 0 && matched != 0) || (strcmp(symbol,"KMD") == 0 && KOMODO_EXTERNAL_NOTARIES == 0) ) | |
243 | komodo_eventadd_pubkeys(sp,symbol,ht,num,pubkeys); | |
244 | } | |
245 | } else printf("illegal num.%d\n",num); | |
246 | } | |
18c6cfce | 247 | else if ( func == 'N' || func == 'M' ) |
17746e45 | 248 | { |
249 | if ( memread(¬arized_height,sizeof(notarized_height),filedata,&fpos,datalen) != sizeof(notarized_height) ) | |
250 | errs++; | |
251 | if ( memread(¬arized_hash,sizeof(notarized_hash),filedata,&fpos,datalen) != sizeof(notarized_hash) ) | |
252 | errs++; | |
253 | if ( memread(¬arized_desttxid,sizeof(notarized_desttxid),filedata,&fpos,datalen) != sizeof(notarized_desttxid) ) | |
254 | errs++; | |
18c6cfce | 255 | if ( func == 'M' ) |
256 | { | |
257 | if ( memread(&MoM,sizeof(MoM),filedata,&fpos,datalen) != sizeof(MoM) ) | |
258 | errs++; | |
259 | if ( memread(&MoMdepth,sizeof(MoMdepth),filedata,&fpos,datalen) != sizeof(MoMdepth) ) | |
260 | errs++; | |
261 | if ( 1 && ASSETCHAINS_SYMBOL[0] != 0 && sp != 0 ) | |
262 | printf("%s load[%s.%d -> %s] NOTARIZED %d %s MoM.%s %d\n",ASSETCHAINS_SYMBOL,symbol,sp->NUM_NPOINTS,dest,notarized_height,notarized_hash.ToString().c_str(),MoM.ToString().c_str(),MoMdepth); | |
263 | } | |
264 | else | |
265 | { | |
266 | memset(&MoM,0,sizeof(MoM)); | |
267 | MoMdepth = 0; | |
268 | } | |
269 | komodo_eventadd_notarized(sp,symbol,ht,dest,notarized_hash,notarized_desttxid,notarized_height,MoM,MoMdepth); | |
17746e45 | 270 | } |
271 | else if ( func == 'U' ) // deprecated | |
272 | { | |
273 | uint8_t n,nid; uint256 hash; uint64_t mask; | |
274 | n = filedata[fpos++]; | |
275 | nid = filedata[fpos++]; | |
276 | //printf("U %d %d\n",n,nid); | |
277 | if ( memread(&mask,sizeof(mask),filedata,&fpos,datalen) != sizeof(mask) ) | |
278 | errs++; | |
279 | if ( memread(&hash,sizeof(hash),filedata,&fpos,datalen) != sizeof(hash) ) | |
280 | errs++; | |
281 | } | |
282 | else if ( func == 'K' ) | |
283 | { | |
284 | int32_t kheight; | |
285 | if ( memread(&kheight,sizeof(kheight),filedata,&fpos,datalen) != sizeof(kheight) ) | |
286 | errs++; | |
287 | komodo_eventadd_kmdheight(sp,symbol,ht,kheight,0); | |
288 | } | |
289 | else if ( func == 'T' ) | |
290 | { | |
291 | int32_t kheight,ktimestamp; | |
292 | if ( memread(&kheight,sizeof(kheight),filedata,&fpos,datalen) != sizeof(kheight) ) | |
293 | errs++; | |
294 | if ( memread(&ktimestamp,sizeof(ktimestamp),filedata,&fpos,datalen) != sizeof(ktimestamp) ) | |
295 | errs++; | |
296 | //if ( matched != 0 ) global independent states -> inside *sp | |
297 | //printf("%s.%d load[%s] ht.%d t.%u\n",ASSETCHAINS_SYMBOL,ht,symbol,kheight,ktimestamp); | |
298 | komodo_eventadd_kmdheight(sp,symbol,ht,kheight,ktimestamp); | |
299 | } | |
300 | else if ( func == 'R' ) | |
301 | { | |
302 | uint16_t olen,v; uint64_t ovalue; uint256 txid; uint8_t opret[16384]; | |
303 | if ( memread(&txid,sizeof(txid),filedata,&fpos,datalen) != sizeof(txid) ) | |
304 | errs++; | |
305 | if ( memread(&v,sizeof(v),filedata,&fpos,datalen) != sizeof(v) ) | |
306 | errs++; | |
307 | if ( memread(&ovalue,sizeof(ovalue),filedata,&fpos,datalen) != sizeof(ovalue) ) | |
308 | errs++; | |
309 | if ( memread(&olen,sizeof(olen),filedata,&fpos,datalen) != sizeof(olen) ) | |
310 | errs++; | |
311 | if ( olen < sizeof(opret) ) | |
312 | { | |
313 | if ( memread(opret,olen,filedata,&fpos,datalen) != olen ) | |
314 | errs++; | |
315 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 && matched != 0 ) | |
316 | { | |
317 | int32_t i; for (i=0; i<olen; i++) | |
318 | printf("%02x",opret[i]); | |
319 | printf(" %s.%d load[%s] opret[%c] len.%d %.8f\n",ASSETCHAINS_SYMBOL,ht,symbol,opret[0],olen,(double)ovalue/COIN); | |
320 | } | |
321 | komodo_eventadd_opreturn(sp,symbol,ht,txid,ovalue,v,opret,olen); // global shared state -> global PAX | |
322 | } else | |
323 | { | |
324 | int32_t i; | |
325 | for (i=0; i<olen; i++) | |
326 | filedata[fpos++]; | |
327 | //printf("illegal olen.%u\n",olen); | |
328 | } | |
329 | } | |
330 | else if ( func == 'D' ) | |
331 | { | |
332 | printf("unexpected function D[%d]\n",ht); | |
333 | } | |
334 | else if ( func == 'V' ) | |
335 | { | |
336 | int32_t numpvals; uint32_t pvals[128]; | |
337 | numpvals = filedata[fpos++]; | |
338 | if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && memread(pvals,(int32_t)(sizeof(uint32_t)*numpvals),filedata,&fpos,datalen) == numpvals*sizeof(uint32_t) ) | |
339 | { | |
340 | //if ( matched != 0 ) global shared state -> global PVALS | |
341 | //printf("%s load[%s] prices %d\n",ASSETCHAINS_SYMBOL,symbol,ht); | |
342 | komodo_eventadd_pricefeed(sp,symbol,ht,pvals,numpvals); | |
343 | //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals); | |
344 | } else printf("error loading pvals[%d]\n",numpvals); | |
345 | } | |
346 | else printf("[%s] %s illegal func.(%d %c)\n",ASSETCHAINS_SYMBOL,symbol,func,func); | |
347 | *fposp = fpos; | |
3b40d631 | 348 | return(func); |
349 | } | |
350 | return(-1); | |
17746e45 | 351 | } |
352 | ||
c130fb54 | 353 | 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,uint256 MoM,int32_t MoMdepth) |
5cea7725 | 354 | { |
c88fa588 | 355 | static FILE *fp; static int32_t errs,didinit; static uint256 zero; |
7c130297 | 356 | struct komodo_state *sp; char fname[512],symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; int32_t retval,ht,func; uint8_t num,pubkeys[64][33]; |
15d0fbd4 | 357 | if ( didinit == 0 ) |
358 | { | |
359 | portable_mutex_init(&KOMODO_KV_mutex); | |
fb9c3652 | 360 | portable_mutex_init(&KOMODO_CC_mutex); |
15d0fbd4 | 361 | didinit = 1; |
362 | } | |
c75c18fc | 363 | if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) |
15373725 | 364 | { |
365 | KOMODO_INITDONE = (uint32_t)time(NULL); | |
fac8d1ff | 366 | printf("[%s] no komodo_stateptr\n",ASSETCHAINS_SYMBOL); |
c75c18fc | 367 | return; |
15373725 | 368 | } |
ca74f186 | 369 | //printf("[%s] (%s) -> (%s)\n",ASSETCHAINS_SYMBOL,symbol,dest); |
888cf827 | 370 | if ( fp == 0 ) |
371 | { | |
055db9b6 | 372 | komodo_statefname(fname,ASSETCHAINS_SYMBOL,(char *)"komodostate"); |
bafc61f3 | 373 | if ( (fp= fopen(fname,"rb+")) != 0 ) |
374 | { | |
f7f96087 | 375 | if ( (retval= komodo_faststateinit(sp,fname,symbol,dest)) > 0 ) |
58f293eb | 376 | fseek(fp,0,SEEK_END); |
08775bc1 | 377 | else |
378 | { | |
a3a7ec85 | 379 | fprintf(stderr,"komodo_faststateinit retval.%d\n",retval); |
08775bc1 | 380 | while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 ) |
381 | ; | |
382 | } | |
bafc61f3 | 383 | } else fp = fopen(fname,"wb+"); |
63c30610 | 384 | KOMODO_INITDONE = (uint32_t)time(NULL); |
bafc61f3 | 385 | } |
e98e2aec | 386 | if ( height <= 0 ) |
49df008c | 387 | { |
6458297e | 388 | //printf("early return: stateupdate height.%d\n",height); |
49df008c | 389 | return; |
390 | } | |
3eea72f2 | 391 | if ( fp != 0 ) // write out funcid, height, other fields, call side effect function |
bafc61f3 | 392 | { |
2578b002 | 393 | //printf("fpos.%ld ",ftell(fp)); |
ab918767 | 394 | if ( KMDheight != 0 ) |
2b84e06c | 395 | { |
e155dbe9 | 396 | if ( KMDtimestamp != 0 ) |
397 | { | |
398 | fputc('T',fp); | |
399 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) | |
400 | errs++; | |
401 | if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) ) | |
402 | errs++; | |
403 | if ( fwrite(&KMDtimestamp,1,sizeof(KMDtimestamp),fp) != sizeof(KMDtimestamp) ) | |
404 | errs++; | |
405 | } | |
406 | else | |
407 | { | |
408 | fputc('K',fp); | |
409 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) | |
410 | errs++; | |
411 | if ( fwrite(&KMDheight,1,sizeof(KMDheight),fp) != sizeof(KMDheight) ) | |
412 | errs++; | |
413 | } | |
414 | komodo_eventadd_kmdheight(sp,symbol,height,KMDheight,KMDtimestamp); | |
2b84e06c | 415 | } |
01f9cb3d | 416 | else if ( opretbuf != 0 && opretlen > 0 ) |
64bb0834 | 417 | { |
01f9cb3d | 418 | uint16_t olen = opretlen; |
419 | fputc('R',fp); | |
64bb0834 | 420 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
421 | errs++; | |
429dabb5 | 422 | if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) ) |
423 | errs++; | |
424 | if ( fwrite(&vout,1,sizeof(vout),fp) != sizeof(vout) ) | |
425 | errs++; | |
30df2e00 | 426 | if ( fwrite(&opretvalue,1,sizeof(opretvalue),fp) != sizeof(opretvalue) ) |
33935bd0 | 427 | errs++; |
289dc57b | 428 | if ( fwrite(&olen,1,sizeof(olen),fp) != olen ) |
429 | errs++; | |
01f9cb3d | 430 | if ( fwrite(opretbuf,1,olen,fp) != olen ) |
64bb0834 | 431 | errs++; |
088b1fa8 | 432 | //printf("ht.%d R opret[%d] sp.%p\n",height,olen,sp); |
c75c18fc | 433 | //komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout); |
434 | komodo_eventadd_opreturn(sp,symbol,height,txhash,opretvalue,vout,opretbuf,olen); | |
01f9cb3d | 435 | } |
9997caa0 | 436 | else if ( notarypubs != 0 && numnotaries > 0 ) |
bafc61f3 | 437 | { |
f3a1de3a | 438 | printf("ht.%d func P[%d] errs.%d\n",height,numnotaries,errs); |
bafc61f3 | 439 | fputc('P',fp); |
c50ec0fb | 440 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
441 | errs++; | |
bafc61f3 | 442 | fputc(numnotaries,fp); |
52ed4002 | 443 | if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries ) |
444 | errs++; | |
c75c18fc | 445 | komodo_eventadd_pubkeys(sp,symbol,height,numnotaries,notarypubs); |
bafc61f3 | 446 | } |
671187dd | 447 | else if ( voutmask != 0 && numvouts > 0 ) |
448 | { | |
2578b002 | 449 | //printf("ht.%d func U %d %d errs.%d hashsize.%ld\n",height,numvouts,notaryid,errs,sizeof(txhash)); |
671187dd | 450 | fputc('U',fp); |
c50ec0fb | 451 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
452 | errs++; | |
671187dd | 453 | fputc(numvouts,fp); |
454 | fputc(notaryid,fp); | |
455 | if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) ) | |
456 | errs++; | |
457 | if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) ) | |
458 | errs++; | |
6f3cb4d0 | 459 | //komodo_eventadd_utxo(sp,symbol,height,notaryid,txhash,voutmask,numvouts); |
671187dd | 460 | } |
4a41b0b2 | 461 | else if ( pvals != 0 && numpvals > 0 ) |
462 | { | |
bfa5b4f2 | 463 | int32_t i,nonz = 0; |
1bf82154 | 464 | for (i=0; i<32; i++) |
465 | if ( pvals[i] != 0 ) | |
466 | nonz++; | |
467 | if ( nonz >= 32 ) | |
468 | { | |
469 | fputc('V',fp); | |
470 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) | |
471 | errs++; | |
472 | fputc(numpvals,fp); | |
473 | if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals ) | |
474 | errs++; | |
c75c18fc | 475 | komodo_eventadd_pricefeed(sp,symbol,height,pvals,numpvals); |
2578b002 | 476 | //printf("ht.%d V numpvals[%d]\n",height,numpvals); |
1bf82154 | 477 | } |
ef7a3548 | 478 | //printf("save pvals height.%d numpvals.%d\n",height,numpvals); |
4a41b0b2 | 479 | } |
6dabcca4 | 480 | else if ( height != 0 ) |
671187dd | 481 | { |
2578b002 | 482 | //printf("ht.%d func N ht.%d errs.%d\n",height,NOTARIZED_HEIGHT,errs); |
b148643e | 483 | if ( sp != 0 ) |
807200e4 | 484 | { |
ed7bd562 | 485 | if ( sp->MoMdepth > 0 && sp->MoM != zero ) |
18c6cfce | 486 | fputc('M',fp); |
487 | else fputc('N',fp); | |
807200e4 | 488 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
489 | errs++; | |
490 | if ( fwrite(&sp->NOTARIZED_HEIGHT,1,sizeof(sp->NOTARIZED_HEIGHT),fp) != sizeof(sp->NOTARIZED_HEIGHT) ) | |
491 | errs++; | |
492 | if ( fwrite(&sp->NOTARIZED_HASH,1,sizeof(sp->NOTARIZED_HASH),fp) != sizeof(sp->NOTARIZED_HASH) ) | |
493 | errs++; | |
494 | if ( fwrite(&sp->NOTARIZED_DESTTXID,1,sizeof(sp->NOTARIZED_DESTTXID),fp) != sizeof(sp->NOTARIZED_DESTTXID) ) | |
495 | errs++; | |
ed7bd562 | 496 | if ( sp->MoMdepth > 0 && sp->MoM != zero ) |
18c6cfce | 497 | { |
498 | if ( fwrite(&sp->MoM,1,sizeof(sp->MoM),fp) != sizeof(sp->MoM) ) | |
499 | errs++; | |
500 | if ( fwrite(&sp->MoMdepth,1,sizeof(sp->MoMdepth),fp) != sizeof(sp->MoMdepth) ) | |
501 | errs++; | |
502 | } | |
503 | komodo_eventadd_notarized(sp,symbol,height,dest,sp->NOTARIZED_HASH,sp->NOTARIZED_DESTTXID,sp->NOTARIZED_HEIGHT,sp->MoM,sp->MoMdepth); | |
807200e4 | 504 | } |
671187dd | 505 | } |
47f47733 | 506 | fflush(fp); |
bafc61f3 | 507 | } |
508 | } | |
509 | ||
8683bd8d | 510 | 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,uint32_t timestamp) |
1390456b | 511 | { |
c96e160a | 512 | static uint256 zero; static FILE *signedfp; |
1bbbf487 | 513 | int32_t opretlen,nid,offset,k,MoMdepth,matched,len = 0; uint256 MoM,srchash,desttxid; uint8_t crypto777[33]; struct komodo_state *sp; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; |
2df2f193 | 514 | if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) |
515 | return(-1); | |
9c406099 | 516 | if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) |
1390456b | 517 | { |
0ef85dd0 | 518 | if ( i == 0 && j == 0 && memcmp(NOTARY_PUBKEY33,scriptbuf+1,33) == 0 && NOTARY_PUBKEY33[0] != 0 ) |
9152feb5 | 519 | { |
ba37a6b9 | 520 | printf("%s KOMODO_LASTMINED.%d -> %d\n",ASSETCHAINS_SYMBOL,KOMODO_LASTMINED,height); |
8ee93080 | 521 | prevKOMODO_LASTMINED = KOMODO_LASTMINED; |
9152feb5 | 522 | KOMODO_LASTMINED = height; |
523 | } | |
c23dd601 | 524 | decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR); |
986d3459 | 525 | /*for (k=0; k<33; k++) |
e1be360e | 526 | printf("%02x",crypto777[k]); |
527 | printf(" crypto777 "); | |
528 | for (k=0; k<scriptlen; k++) | |
529 | printf("%02x",scriptbuf[k]); | |
986d3459 | 530 | printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/ |
1390456b | 531 | if ( memcmp(crypto777,scriptbuf+1,33) == 0 ) |
532 | { | |
533 | *specialtxp = 1; | |
b7f942c8 | 534 | //printf(">>>>>>>> "); |
1390456b | 535 | } |
8683bd8d | 536 | else if ( komodo_chosennotary(&nid,height,scriptbuf + 1,timestamp) >= 0 ) |
1390456b | 537 | { |
84e843cd | 538 | //printf("found notary.k%d\n",k); |
539 | if ( notaryid < 64 ) | |
1390456b | 540 | { |
84e843cd | 541 | if ( notaryid < 0 ) |
542 | { | |
a5f315c7 | 543 | notaryid = nid; |
84e843cd | 544 | *voutmaskp |= (1LL << j); |
545 | } | |
a5f315c7 | 546 | else if ( notaryid != nid ) |
84e843cd | 547 | { |
b7f942c8 | 548 | //for (i=0; i<33; i++) |
549 | // printf("%02x",scriptbuf[i+1]); | |
550 | //printf(" %s mismatch notaryid.%d k.%d\n",ASSETCHAINS_SYMBOL,notaryid,nid); | |
84e843cd | 551 | notaryid = 64; |
552 | *voutmaskp = 0; | |
553 | } | |
554 | else *voutmaskp |= (1LL << j); | |
1390456b | 555 | } |
1390456b | 556 | } |
557 | } | |
844a6138 | 558 | if ( scriptbuf[len++] == 0x6a ) |
1390456b | 559 | { |
a730ab12 | 560 | int32_t nameoffset,opoffset = 0; |
1390456b | 561 | if ( (opretlen= scriptbuf[len++]) == 0x4c ) |
562 | opretlen = scriptbuf[len++]; | |
563 | else if ( opretlen == 0x4d ) | |
564 | { | |
565 | opretlen = scriptbuf[len++]; | |
508b0d3c | 566 | opretlen += (scriptbuf[len++] << 8); |
1390456b | 567 | } |
a730ab12 | 568 | opoffset = len; |
a490f905 | 569 | matched = 0; |
570 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) | |
571 | { | |
e621c0a3 | 572 | if ( strcmp("KMD",(char *)&scriptbuf[len+32 * 2 + 4]) == 0 ) |
a490f905 | 573 | matched = 1; |
574 | } | |
575 | else | |
576 | { | |
a490f905 | 577 | if ( strcmp(ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+offset]) == 0 ) |
578 | matched = 1; | |
579 | } | |
1bbbf487 | 580 | offset = 32 * (1 + matched) + 4; |
d24efb03 | 581 | nameoffset = (int32_t)strlen((char *)&scriptbuf[len+offset]); |
582 | if ( nameoffset == 2 ) | |
583 | nameoffset += 2; | |
584 | else nameoffset++; | |
a730ab12 | 585 | if ( j == 1 && opretlen >= len+offset-opoffset ) |
1390456b | 586 | { |
06575b0a | 587 | static int32_t last_rewind; int32_t rewindtarget,validated = 0; CBlockIndex *pindex;// |
d269d79e | 588 | struct komodo_ccdata ccdata; struct komodo_ccdataMoMoM MoMoMdata; |
a490f905 | 589 | memset(&ccdata,0,sizeof(ccdata)); |
d269d79e | 590 | memset(&MoMoMdata,0,sizeof(MoMoMdata)); |
a490f905 | 591 | strncpy(ccdata.symbol,(char *)&scriptbuf[len+offset],sizeof(ccdata.symbol)); |
90993cdb | 592 | if ( matched == 0 && bitweight(signedmask) >= KOMODO_MINRATIFY ) |
593 | notarized = 1; | |
b6c1a2c3 | 594 | if ( strcmp("PIZZA",ccdata.symbol) == 0 ) |
a490f905 | 595 | notarized = 1; |
5c888905 | 596 | if ( 0 && opretlen != 149 ) |
a730ab12 | 597 | printf("[%s].%d (%s) matched.%d i.%d j.%d notarized.%d %llx opretlen.%d len.%d offset.%d opoffset.%d\n",ASSETCHAINS_SYMBOL,height,ccdata.symbol,matched,i,j,notarized,(long long)signedmask,opretlen,len,offset,opoffset); |
6a933d59 | 598 | len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&srchash); |
dbaf1154 | 599 | len += iguana_rwnum(0,&scriptbuf[len],sizeof(*notarizedheightp),(uint8_t *)notarizedheightp); |
1bbbf487 | 600 | if ( matched != 0 ) |
601 | len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid); | |
195c2e16 | 602 | if ( matched != 0 && IsInitialBlockDownload() == 0 && ((pindex= mapBlockIndex[srchash]) == 0 || pindex->nHeight != *notarizedheightp) ) |
6a933d59 | 603 | { |
f4e0076a | 604 | if ( sp->NOTARIZED_HEIGHT > 0 && sp->NOTARIZED_HEIGHT < *notarizedheightp ) |
11ed15a6 | 605 | rewindtarget = sp->NOTARIZED_HEIGHT - 1; |
83de8f95 | 606 | else if ( *notarizedheightp > 101 ) |
11ed15a6 | 607 | rewindtarget = *notarizedheightp - 101; |
608 | else rewindtarget = 0; | |
609 | if ( rewindtarget != 0 && rewindtarget > KOMODO_REWIND && rewindtarget > last_rewind ) | |
610 | { | |
899aa387 | 611 | if ( last_rewind != 0 ) |
612 | { | |
613 | KOMODO_REWIND = rewindtarget; | |
614 | fprintf(stderr,"%s FORK detected. notarized.%d %s not in this chain! last notarization %d -> rewindtarget.%d\n",ASSETCHAINS_SYMBOL,*notarizedheightp,srchash.ToString().c_str(),sp->NOTARIZED_HEIGHT,rewindtarget); | |
615 | } | |
11ed15a6 | 616 | last_rewind = rewindtarget; |
11ed15a6 | 617 | } |
79d54107 | 618 | } else validated = 1; |
ae9fc2a3 | 619 | if ( notarized != 0 && validated != 0 ) |
1390456b | 620 | { |
195c2e16 | 621 | //sp->NOTARIZED_HEIGHT = *notarizedheightp; |
622 | //sp->NOTARIZED_HASH = srchash; | |
623 | //sp->NOTARIZED_DESTTXID = desttxid; | |
624 | memset(&MoM,0,sizeof(MoM)); | |
625 | MoMdepth = 0; | |
713c2a94 | 626 | len += nameoffset; |
d269d79e | 627 | ccdata.MoMdata.notarized_height = *notarizedheightp; |
628 | ccdata.MoMdata.height = height; | |
629 | ccdata.MoMdata.txi = i; | |
5c888905 | 630 | //printf("nameoffset.%d len.%d + 36 %d vs opretlen.%d\n",nameoffset,len,len+36,opretlen); |
a730ab12 | 631 | if ( len+36-opoffset <= opretlen ) |
18c6cfce | 632 | { |
195c2e16 | 633 | len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&MoM); |
634 | len += iguana_rwnum(0,&scriptbuf[len],sizeof(MoMdepth),(uint8_t *)&MoMdepth); | |
d269d79e | 635 | ccdata.MoMdata.MoM = MoM; |
636 | ccdata.MoMdata.MoMdepth = MoMdepth; | |
713c2a94 | 637 | if ( len+sizeof(ccdata.CCid) <= opretlen ) |
638 | { | |
639 | len += iguana_rwnum(0,&scriptbuf[len],sizeof(ccdata.CCid),(uint8_t *)&ccdata.CCid); | |
640 | ccdata.len = sizeof(ccdata.CCid); | |
641 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) | |
642 | { | |
643 | // MoMoM, depth, numpairs, (notarization ht, MoMoM offset) | |
51dab149 | 644 | if ( len+48-opoffset <= opretlen && strcmp(ccdata.symbol,ASSETCHAINS_SYMBOL) == 0 ) |
713c2a94 | 645 | { |
51dab149 | 646 | len += iguana_rwnum(0,&scriptbuf[len],sizeof(uint32_t),(uint8_t *)&MoMoMdata.kmdstarti); |
647 | len += iguana_rwnum(0,&scriptbuf[len],sizeof(uint32_t),(uint8_t *)&MoMoMdata.kmdendi); | |
090b1c9f | 648 | len += iguana_rwbignum(0,&scriptbuf[len],sizeof(MoMoMdata.MoMoM),(uint8_t *)&MoMoMdata.MoMoM); |
51dab149 | 649 | len += iguana_rwnum(0,&scriptbuf[len],sizeof(uint32_t),(uint8_t *)&MoMoMdata.MoMoMdepth); |
d269d79e | 650 | len += iguana_rwnum(0,&scriptbuf[len],sizeof(uint32_t),(uint8_t *)&MoMoMdata.numpairs); |
51dab149 | 651 | MoMoMdata.len += sizeof(MoMoMdata.MoMoM) + sizeof(uint32_t)*4; |
d269d79e | 652 | if ( len+MoMoMdata.numpairs*8-opoffset == opretlen ) |
713c2a94 | 653 | { |
d269d79e | 654 | MoMoMdata.pairs = (struct komodo_ccdatapair *)calloc(MoMoMdata.numpairs,sizeof(*MoMoMdata.pairs)); |
655 | for (k=0; k<MoMoMdata.numpairs; k++) | |
713c2a94 | 656 | { |
d269d79e | 657 | len += iguana_rwnum(0,&scriptbuf[len],sizeof(int32_t),(uint8_t *)&MoMoMdata.pairs[k].notarization_height); |
658 | len += iguana_rwnum(0,&scriptbuf[len],sizeof(uint32_t),(uint8_t *)&MoMoMdata.pairs[k].MoMoMoffset); | |
659 | MoMoMdata.len += sizeof(uint32_t) * 2; | |
713c2a94 | 660 | } |
d269d79e | 661 | } else ccdata.len = MoMoMdata.len = 0; |
662 | } else ccdata.len = MoMoMdata.len = 0; | |
713c2a94 | 663 | } |
664 | } | |
195c2e16 | 665 | if ( MoM == zero || MoMdepth > 1440 || MoMdepth < 0 ) |
18c6cfce | 666 | { |
195c2e16 | 667 | memset(&MoM,0,sizeof(MoM)); |
668 | MoMdepth = 0; | |
18c6cfce | 669 | } |
670 | else | |
671 | { | |
d269d79e | 672 | komodo_rwccdata(ASSETCHAINS_SYMBOL,1,&ccdata,&MoMoMdata); |
5c888905 | 673 | //printf("[%s] matched.%d VALID (%s) MoM.%s [%d]\n",ASSETCHAINS_SYMBOL,matched,ccdata.symbol,MoM.ToString().c_str(),MoMdepth); |
18c6cfce | 674 | } |
c0d01b7d | 675 | if ( MoMoMdata.pairs != 0 ) |
676 | free(MoMoMdata.pairs); | |
713c2a94 | 677 | memset(&ccdata,0,sizeof(ccdata)); |
c0d01b7d | 678 | memset(&MoMoMdata,0,sizeof(MoMoMdata)); |
18c6cfce | 679 | } |
90b5b4fd | 680 | else if ( ASSETCHAINS_SYMBOL[0] == 0 && matched != 0 && notarized != 0 && validated != 0 ) |
d269d79e | 681 | komodo_rwccdata((char *)"KMD",1,&ccdata,0); |
ae9fc2a3 | 682 | if ( matched != 0 && *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height ) |
06d1a655 | 683 | { |
195c2e16 | 684 | sp->NOTARIZED_HEIGHT = *notarizedheightp; |
685 | sp->NOTARIZED_HASH = srchash; | |
686 | sp->NOTARIZED_DESTTXID = desttxid; | |
687 | sp->MoM = MoM; | |
688 | sp->MoMdepth = MoMdepth; | |
689 | komodo_stateupdate(height,0,0,0,zero,0,0,0,0,0,0,0,0,0,0,sp->MoM,sp->MoMdepth); | |
5c888905 | 690 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) |
195c2e16 | 691 | printf("[%s] ht.%d NOTARIZED.%d %s.%s %sTXID.%s lens.(%d %d) MoM.%s %d\n",ASSETCHAINS_SYMBOL,height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,srchash.ToString().c_str(),ASSETCHAINS_SYMBOL[0]==0?"BTC":"KMD",desttxid.ToString().c_str(),opretlen,len,sp->MoM.ToString().c_str(),sp->MoMdepth); |
692 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) | |
dbaf1154 | 693 | { |
195c2e16 | 694 | if ( signedfp == 0 ) |
695 | { | |
696 | char fname[512]; | |
697 | komodo_statefname(fname,ASSETCHAINS_SYMBOL,(char *)"signedmasks"); | |
698 | if ( (signedfp= fopen(fname,"rb+")) == 0 ) | |
699 | signedfp = fopen(fname,"wb"); | |
700 | else fseek(signedfp,0,SEEK_END); | |
701 | } | |
702 | if ( signedfp != 0 ) | |
703 | { | |
704 | fwrite(&height,1,sizeof(height),signedfp); | |
705 | fwrite(&signedmask,1,sizeof(signedmask),signedfp); | |
706 | fflush(signedfp); | |
707 | } | |
708 | if ( opretlen > len && scriptbuf[len] == 'A' ) | |
709 | { | |
710 | //for (i=0; i<opretlen-len; i++) | |
711 | // printf("%02x",scriptbuf[len+i]); | |
712 | //printf(" Found extradata.[%d] %d - %d\n",opretlen-len,opretlen,len); | |
713 | komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen-len+4+3+(scriptbuf[1] == 0x4d),j,zero,0); | |
714 | } | |
dbaf1154 | 715 | } |
06d1a655 | 716 | } |
5c888905 | 717 | } else //if ( matched != 0 && *notarizedheightp != sp->NOTARIZED_HEIGHT ) |
0d826729 | 718 | printf("validated.%d notarized.%d %llx reject ht.%d NOTARIZED.%d prev.%d %s.%s DESTTXID.%s len.%d opretlen.%d\n",validated,notarized,(long long)signedmask,height,*notarizedheightp,sp->NOTARIZED_HEIGHT,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,srchash.ToString().c_str(),desttxid.ToString().c_str(),len,opretlen); |
1390456b | 719 | } |
1bbbf487 | 720 | else if ( matched != 0 && i == 0 && j == 1 && opretlen == 149 ) |
d42d3348 | 721 | { |
76b9b8b8 | 722 | if ( notaryid >= 0 && notaryid < 64 ) |
d42d3348 | 723 | komodo_paxpricefeed(height,&scriptbuf[len],opretlen); |
724 | } | |
1bbbf487 | 725 | else if ( matched != 0 ) |
eb406ebd | 726 | { |
cd586654 | 727 | //int32_t k; for (k=0; k<scriptlen; k++) |
728 | // printf("%02x",scriptbuf[k]); | |
729 | //printf(" <- script ht.%d i.%d j.%d value %.8f %s\n",height,i,j,dstr(value),ASSETCHAINS_SYMBOL); | |
6dba48ec | 730 | if ( opretlen >= 32*2+4 && strcmp(ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,(char *)&scriptbuf[len+32*2+4]) == 0 ) |
731 | { | |
5a286a6f | 732 | for (k=0; k<32; k++) |
733 | if ( scriptbuf[len+k] != 0 ) | |
734 | break; | |
735 | if ( k == 32 ) | |
6dba48ec | 736 | { |
737 | *isratificationp = 1; | |
738 | printf("ISRATIFICATION (%s)\n",(char *)&scriptbuf[len+32*2+4]); | |
739 | } | |
740 | } | |
8f3aa743 | 741 | |
c9e4d982 | 742 | if ( *isratificationp == 0 && (signedmask != 0 || (scriptbuf[len] != 'X' && scriptbuf[len] != 'A')) ) // && scriptbuf[len] != 'I') |
18c6cfce | 743 | komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen,j,zero,0); |
eb406ebd | 744 | } |
1390456b | 745 | } |
746 | return(notaryid); | |
747 | } | |
748 | ||
58033467 | 749 | /*int32_t komodo_isratify(int32_t isspecial,int32_t numvalid) |
f38345e9 | 750 | { |
755ead98 | 751 | if ( isspecial != 0 && numvalid >= KOMODO_MINRATIFY ) |
f38345e9 | 752 | return(1); |
753 | else return(0); | |
58033467 | 754 | }*/ |
f38345e9 | 755 | |
756 | // Special tx have vout[0] -> CRYPTO777 | |
755ead98 | 757 | // with more than KOMODO_MINRATIFY pay2pubkey outputs -> ratify |
f38345e9 | 758 | // if all outputs to notary -> notary utxo |
759 | // if txi == 0 && 2 outputs and 2nd OP_RETURN, len == 32*2+4 -> notarized, 1st byte 'P' -> pricefeed | |
420d7311 | 760 | // OP_RETURN: 'D' -> deposit, 'W' -> withdraw |
ab918767 | 761 | |
6f3cb4d0 | 762 | int32_t gettxout_scriptPubKey(uint8_t *scriptPubKey,int32_t maxsize,uint256 txid,int32_t n); |
763 | ||
764 | int32_t komodo_notarycmp(uint8_t *scriptPubKey,int32_t scriptlen,uint8_t pubkeys[64][33],int32_t numnotaries,uint8_t rmd160[20]) | |
765 | { | |
766 | int32_t i; | |
767 | if ( scriptlen == 25 && memcmp(&scriptPubKey[3],rmd160,20) == 0 ) | |
768 | return(0); | |
769 | else if ( scriptlen == 35 ) | |
770 | { | |
771 | for (i=0; i<numnotaries; i++) | |
772 | if ( memcmp(&scriptPubKey[1],pubkeys[i],33) == 0 ) | |
773 | return(i); | |
774 | } | |
775 | return(-1); | |
776 | } | |
f38345e9 | 777 | |
651989c7 | 778 | void komodo_connectblock(CBlockIndex *pindex,CBlock& block) |
50027f06 | 779 | { |
a7f0d3e4 | 780 | static int32_t hwmheight; |
7c130297 | 781 | uint64_t signedmask,voutmask; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; struct komodo_state *sp; |
6a933d59 | 782 | uint8_t scriptbuf[10001],pubkeys[64][33],rmd160[20],scriptPubKey[35]; uint256 zero,btctxid,txhash; |
ea365a71 | 783 | int32_t i,j,k,numnotaries,notarized,scriptlen,isratification,nid,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count; |
878d3961 | 784 | memset(&zero,0,sizeof(zero)); |
c93dc546 | 785 | komodo_init(pindex->nHeight); |
d7227bf6 | 786 | KOMODO_INITDONE = (uint32_t)time(NULL); |
ab918767 | 787 | if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) |
3df53390 | 788 | { |
789 | fprintf(stderr,"unexpected null komodostateptr.[%s]\n",ASSETCHAINS_SYMBOL); | |
ab918767 | 790 | return; |
3df53390 | 791 | } |
e0440cc3 | 792 | //fprintf(stderr,"%s connect.%d\n",ASSETCHAINS_SYMBOL,pindex->nHeight); |
85ef725a | 793 | numnotaries = komodo_notaries(pubkeys,pindex->nHeight,pindex->GetBlockTime()); |
6f3cb4d0 | 794 | calc_rmd160_sha256(rmd160,pubkeys[0],33); |
a7f0d3e4 | 795 | if ( pindex->nHeight > hwmheight ) |
796 | hwmheight = pindex->nHeight; | |
797 | else | |
798 | { | |
174cae41 | 799 | if ( pindex->nHeight != hwmheight ) |
800 | 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 | 801 | komodo_event_rewind(sp,symbol,pindex->nHeight); |
18c6cfce | 802 | komodo_stateupdate(pindex->nHeight,0,0,0,zero,0,0,0,0,-pindex->nHeight,pindex->nTime,0,0,0,0,zero,0); |
a7f0d3e4 | 803 | } |
c75c18fc | 804 | komodo_currentheight_set(chainActive.Tip()->nHeight); |
68916cc6 | 805 | if ( pindex != 0 ) |
b501ded2 | 806 | { |
656eddcd | 807 | height = pindex->nHeight; |
01cc012f | 808 | txn_count = block.vtx.size(); |
809 | for (i=0; i<txn_count; i++) | |
dc64de68 | 810 | { |
352f8081 | 811 | txhash = block.vtx[i].GetHash(); |
01cc012f | 812 | numvouts = block.vtx[i].vout.size(); |
352f8081 | 813 | notaryid = -1; |
ea365a71 | 814 | voutmask = specialtx = notarizedheight = isratification = notarized = 0; |
517b1691 | 815 | signedmask = (height < 91400) ? 1 : 0; |
ea365a71 | 816 | numvins = block.vtx[i].vin.size(); |
817 | for (j=0; j<numvins; j++) | |
818 | { | |
69383c9f | 819 | if ( i == 0 && j == 0 ) |
820 | continue; | |
ea365a71 | 821 | if ( (scriptlen= gettxout_scriptPubKey(scriptPubKey,sizeof(scriptPubKey),block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) > 0 ) |
822 | { | |
823 | if ( (k= komodo_notarycmp(scriptPubKey,scriptlen,pubkeys,numnotaries,rmd160)) >= 0 ) | |
824 | signedmask |= (1LL << k); | |
c7918614 | 825 | else if ( 0 && numvins >= 17 ) |
bb1963ca | 826 | { |
827 | int32_t k; | |
828 | for (k=0; k<scriptlen; k++) | |
829 | printf("%02x",scriptPubKey[k]); | |
57d9cfcc | 830 | printf(" scriptPubKey doesnt match any notary vini.%d of %d\n",j,numvins); |
bb1963ca | 831 | } |
832 | } else printf("cant get scriptPubKey for ht.%d txi.%d vin.%d\n",height,i,j); | |
ea365a71 | 833 | } |
e6d7eca6 | 834 | numvalid = bitweight(signedmask); |
ee5e8026 | 835 | if ( (((height < 90000 || (signedmask & 1) != 0) && numvalid >= KOMODO_MINRATIFY) || |
836 | (numvalid >= KOMODO_MINRATIFY && ASSETCHAINS_SYMBOL[0] != 0) || | |
837 | numvalid > (numnotaries/5)) ) | |
ea365a71 | 838 | { |
f1d6fb9b | 839 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) |
840 | { | |
841 | static FILE *signedfp; | |
842 | if ( signedfp == 0 ) | |
843 | { | |
844 | char fname[512]; | |
845 | komodo_statefname(fname,ASSETCHAINS_SYMBOL,(char *)"signedmasks"); | |
846 | if ( (signedfp= fopen(fname,"rb+")) == 0 ) | |
847 | signedfp = fopen(fname,"wb"); | |
848 | else fseek(signedfp,0,SEEK_END); | |
849 | } | |
850 | if ( signedfp != 0 ) | |
851 | { | |
852 | fwrite(&height,1,sizeof(height),signedfp); | |
853 | fwrite(&signedmask,1,sizeof(signedmask),signedfp); | |
854 | fflush(signedfp); | |
855 | } | |
1132e845 | 856 | printf("[%s] ht.%d txi.%d signedmask.%llx numvins.%d numvouts.%d <<<<<<<<<<< notarized\n",ASSETCHAINS_SYMBOL,height,i,(long long)signedmask,numvins,numvouts); |
f1d6fb9b | 857 | } |
ea365a71 | 858 | notarized = 1; |
859 | } | |
e8b7db1a | 860 | if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 ) |
9e5fc6d7 | 861 | printf("(tx.%d: ",i); |
01cc012f | 862 | for (j=0; j<numvouts; j++) |
dc64de68 | 863 | { |
081ab21c | 864 | /*if ( i == 0 && j == 0 ) |
6615cec9 | 865 | { |
866 | uint8_t *script = (uint8_t *)block.vtx[0].vout[numvouts-1].scriptPubKey.data(); | |
867 | if ( numvouts <= 2 || script[0] != 0x6a ) | |
868 | { | |
869 | if ( numvouts == 2 && block.vtx[0].vout[1].nValue != 0 ) | |
870 | { | |
871 | fprintf(stderr,"ht.%d numvouts.%d value %.8f\n",height,numvouts,dstr(block.vtx[0].vout[1].nValue)); | |
49508d3c | 872 | if ( height >= 235300 && block.vtx[0].vout[1].nValue >= 100000*COIN ) |
6615cec9 | 873 | block.vtx[0].vout[1].nValue = 0; |
874 | break; | |
875 | } | |
876 | } | |
081ab21c | 877 | }*/ |
e8b7db1a | 878 | if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 ) |
9e5fc6d7 | 879 | printf("%.8f ",dstr(block.vtx[i].vout[j].nValue)); |
1390456b | 880 | len = block.vtx[i].vout[j].scriptPubKey.size(); |
97561034 | 881 | if ( len >= sizeof(uint32_t) && len <= sizeof(scriptbuf) ) |
01cc012f | 882 | { |
189d9dee | 883 | #ifdef KOMODO_ZCASH |
484f8777 | 884 | memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len); |
189d9dee | 885 | #else |
886 | memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len); | |
887 | #endif | |
8683bd8d | 888 | notaryid = komodo_voutupdate(&isratification,notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,¬arizedheight,(uint64_t)block.vtx[i].vout[j].nValue,notarized,signedmask,(uint32_t)chainActive.Tip()->GetBlockTime()); |
a01acef4 | 889 | if ( 0 && i > 0 ) |
e69a0833 | 890 | { |
891 | for (k=0; k<len; k++) | |
892 | printf("%02x",scriptbuf[k]); | |
a8832194 | 893 | 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 | 894 | } |
01cc012f | 895 | } |
352f8081 | 896 | } |
e8b7db1a | 897 | if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 ) |
9e5fc6d7 | 898 | printf(") "); |
05c1e522 | 899 | if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 ) |
0b27cefa | 900 | 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 | 901 | if ( notarized != 0 && (notarizedheight != 0 || specialtx != 0) ) |
1390456b | 902 | { |
b338ea90 | 903 | if ( isratification != 0 ) |
904 | { | |
905 | 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); | |
906 | printf("ht.%d specialtx.%d isratification.%d numvouts.%d signed.%llx numnotaries.%d\n",height,specialtx,isratification,numvouts,(long long)signedmask,numnotaries); | |
907 | } | |
d2a115d8 | 908 | if ( specialtx != 0 && isratification != 0 && numvouts > 2 ) |
84e843cd | 909 | { |
29e69b85 | 910 | numvalid = 0; |
dd7b22ad | 911 | memset(pubkeys,0,sizeof(pubkeys)); |
6dba48ec | 912 | for (j=1; j<numvouts-1; j++) |
84e843cd | 913 | { |
914 | len = block.vtx[i].vout[j].scriptPubKey.size(); | |
0554de3c | 915 | if ( len >= sizeof(uint32_t) && len <= sizeof(scriptbuf) ) |
84e843cd | 916 | { |
189d9dee | 917 | #ifdef KOMODO_ZCASH |
484f8777 | 918 | memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len); |
189d9dee | 919 | #else |
920 | memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len); | |
921 | #endif | |
84e843cd | 922 | if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) |
923 | { | |
29e69b85 | 924 | memcpy(pubkeys[numvalid++],scriptbuf+1,33); |
e7f99312 | 925 | for (k=0; k<33; k++) |
84e843cd | 926 | printf("%02x",scriptbuf[k+1]); |
927 | printf(" <- new notary.[%d]\n",j-1); | |
928 | } | |
929 | } | |
930 | } | |
aa114a60 | 931 | if ( ((signedmask & 1) != 0 && numvalid >= KOMODO_MINRATIFY) || bitweight(signedmask) > (numnotaries/3) ) |
d8ce705a | 932 | { |
933 | memset(&txhash,0,sizeof(txhash)); | |
18c6cfce | 934 | komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0,0,0,0,0,0,0,zero,0); |
f9d034d4 | 935 | 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 | 936 | } else printf("signedmask.%llx numvalid.%d wt.%d numnotaries.%d\n",(long long)signedmask,numvalid,bitweight(signedmask),numnotaries); |
84e843cd | 937 | } |
a96439f5 | 938 | } |
656eddcd | 939 | } |
e8b7db1a | 940 | if ( NOTARY_PUBKEY33[0] != 0 && ASSETCHAINS_SYMBOL[0] == 0 ) |
ac93bb3e | 941 | printf("%s ht.%d\n",ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL,height); |
e155dbe9 | 942 | if ( pindex->nHeight == hwmheight ) |
18c6cfce | 943 | komodo_stateupdate(height,0,0,0,zero,0,0,0,0,height,(uint32_t)pindex->nTime,0,0,0,0,zero,0); |
3df53390 | 944 | } else fprintf(stderr,"komodo_connectblock: unexpected null pindex\n"); |
17899df4 | 945 | //KOMODO_INITDONE = (uint32_t)time(NULL); |
dd42fd9b | 946 | //fprintf(stderr,"%s end connect.%d\n",ASSETCHAINS_SYMBOL,pindex->nHeight); |
3d35aa5b | 947 | } |
948 | ||
0f24f245 | 949 | |
fcd36118 | 950 | #endif |