]> Git Repo - VerusCoin.git/blame - src/komodo_gateway.h
Test
[VerusCoin.git] / src / komodo_gateway.h
CommitLineData
9b0e1808 1/******************************************************************************
37fed603 2 * Copyright © 2014-2017 The SuperNET Developers. *
9b0e1808 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
f38345e9 16// paxdeposit equivalent in reverse makes opreturn and KMD does the same in reverse
f38345e9 17
aa114a60 18int32_t pax_fiatstatus(uint64_t *available,uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,uint64_t *approved,uint64_t *redeemed,char *base)
3cbfd2b4 19{
4445eeae 20 int32_t baseid; struct komodo_state *sp; int64_t netliability,maxallowed,maxval;
aa114a60 21 *available = *deposited = *issued = *withdrawn = *approved = *redeemed = 0;
3cbfd2b4 22 if ( (baseid= komodo_baseid(base)) >= 0 )
23 {
24 if ( (sp= komodo_stateptrget(base)) != 0 )
25 {
26 *deposited = sp->deposited;
27 *issued = sp->issued;
28 *withdrawn = sp->withdrawn;
29 *approved = sp->approved;
30 *redeemed = sp->redeemed;
4502c41e 31 //netliability = (sp->deposited - sp->withdrawn) - sp->shorted;
4445eeae 32 maxval = sp->approved;
33 if ( sp->withdrawn > maxval )
34 maxval = sp->withdrawn;
35 netliability = (sp->issued - maxval) - sp->shorted;
aa114a60 36 maxallowed = komodo_maxallowed(baseid);
37 if ( netliability < maxallowed )
38 *available = (maxallowed - netliability);
39 //printf("%p %s %.8f %.8f %.8f %.8f %.8f\n",sp,base,dstr(*deposited),dstr(*issued),dstr(*withdrawn),dstr(*approved),dstr(*redeemed));
3cbfd2b4 40 return(0);
ee5e5744 41 } else printf("pax_fiatstatus cant get basesp.%s\n",base);
106a1d04 42 } // else printf("pax_fiatstatus illegal base.%s\n",base);
3cbfd2b4 43 return(-1);
44}
45
51e5d952 46void pax_keyset(uint8_t *buf,uint256 txid,uint16_t vout,uint8_t type)
6210f328 47{
51e5d952 48 memcpy(buf,&txid,32);
49 memcpy(&buf[32],&vout,2);
50 buf[34] = type;
51}
52
53struct pax_transaction *komodo_paxfind(uint256 txid,uint16_t vout,uint8_t type)
54{
55 struct pax_transaction *pax; uint8_t buf[35];
6210f328 56 pthread_mutex_lock(&komodo_mutex);
51e5d952 57 pax_keyset(buf,txid,vout,type);
58 HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
6210f328 59 pthread_mutex_unlock(&komodo_mutex);
60 return(pax);
61}
62
c5c0e527 63struct pax_transaction *komodo_paxfinds(uint256 txid,uint16_t vout)
64{
2aba5e49 65 struct pax_transaction *pax; int32_t i; uint8_t types[] = { 'I', 'D', 'X', 'A', 'W' };
095dce58 66 for (i=0; i<sizeof(types)/sizeof(*types); i++)
c5c0e527 67 if ( (pax= komodo_paxfind(txid,vout,types[i])) != 0 )
68 return(pax);
69 return(0);
70}
71
51e5d952 72struct pax_transaction *komodo_paxmark(int32_t height,uint256 txid,uint16_t vout,uint8_t type,int32_t mark)
6210f328 73{
51e5d952 74 struct pax_transaction *pax; uint8_t buf[35];
6210f328 75 pthread_mutex_lock(&komodo_mutex);
51e5d952 76 pax_keyset(buf,txid,vout,type);
77 HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
2ab43fd2 78 if ( pax == 0 )
79 {
80 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
81 pax->txid = txid;
82 pax->vout = vout;
51e5d952 83 pax->type = type;
84 memcpy(pax->buf,buf,sizeof(pax->buf));
85 HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
32d525eb 86 //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark);
2ab43fd2 87 }
6210f328 88 if ( pax != 0 )
649a9ebf 89 {
6210f328 90 pax->marked = mark;
cfd94f88 91 //if ( height > 214700 || pax->height > 214700 )
92 // printf("mark ht.%d %.8f %.8f\n",pax->height,dstr(pax->komodoshis),dstr(pax->fiatoshis));
649a9ebf 93
94 }
6210f328 95 pthread_mutex_unlock(&komodo_mutex);
96 return(pax);
97}
429dabb5 98
57f349cb 99void komodo_paxdelete(struct pax_transaction *pax)
100{
5720de88 101 return; // breaks when out of order
57f349cb 102 pthread_mutex_lock(&komodo_mutex);
103 HASH_DELETE(hh,PAX,pax);
104 pthread_mutex_unlock(&komodo_mutex);
105}
106
51e5d952 107void komodo_gateway_deposit(char *coinaddr,uint64_t value,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,uint8_t type,int32_t height,int32_t otherheight,char *source,int32_t approved) // assetchain context
d43c7a93 108{
51e5d952 109 struct pax_transaction *pax; uint8_t buf[35]; int32_t addflag = 0; struct komodo_state *sp; char str[16],dest[16],*s;
6cd01056 110 if ( KOMODO_PAX == 0 )
111 return;
bffa91b2 112 sp = komodo_stateptr(str,dest);
d43c7a93 113 pthread_mutex_lock(&komodo_mutex);
51e5d952 114 pax_keyset(buf,txid,vout,type);
115 HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
d43c7a93 116 if ( pax == 0 )
b2d64a0f 117 {
d43c7a93 118 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
8d52488f 119 pax->txid = txid;
120 pax->vout = vout;
51e5d952 121 pax->type = type;
122 memcpy(pax->buf,buf,sizeof(pax->buf));
123 HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
7961668b 124 addflag = 1;
bff51d67 125 if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 )
1fc75a27 126 {
127 int32_t i; for (i=0; i<32; i++)
128 printf("%02x",((uint8_t *)&txid)[i]);
5ce8bf22 129 printf(" v.%d [%s] kht.%d ht.%d create pax.%p symbol.%s source.%s\n",vout,ASSETCHAINS_SYMBOL,height,otherheight,pax,symbol,source);
1fc75a27 130 }
b2d64a0f 131 }
8d52488f 132 pthread_mutex_unlock(&komodo_mutex);
d43c7a93 133 if ( coinaddr != 0 )
134 {
135 strcpy(pax->coinaddr,coinaddr);
9ccd3f33 136 if ( value != 0 )
137 pax->komodoshis = value;
138 if ( symbol != 0 )
139 strcpy(pax->symbol,symbol);
cd26c1f3 140 if ( source != 0 )
141 strcpy(pax->source,source);
9ccd3f33 142 if ( fiatoshis != 0 )
143 pax->fiatoshis = fiatoshis;
144 if ( rmd160 != 0 )
145 memcpy(pax->rmd160,rmd160,20);
146 if ( height != 0 )
147 pax->height = height;
148 if ( otherheight != 0 )
149 pax->otherheight = otherheight;
d43c7a93 150 }
151 else
152 {
153 pax->marked = height;
bff51d67 154 //printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight);
d43c7a93 155 }
d43c7a93 156}
157
7cc6844b 158int32_t komodo_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transaction *pax)
159{
160 int32_t i,len = 0;
ddf827b2 161 if ( rwflag == 1 )
162 {
163 for (i=0; i<32; i++)
164 opretbuf[len++] = ((uint8_t *)&pax->txid)[i];
165 opretbuf[len++] = pax->vout & 0xff;
166 opretbuf[len++] = (pax->vout >> 8) & 0xff;
167 }
168 else
169 {
170 for (i=0; i<32; i++)
a6972662 171 ((uint8_t *)&pax->txid)[i] = opretbuf[len++];
bff51d67 172 //for (i=0; i<32; i++)
173 // printf("%02x",((uint8_t *)&pax->txid)[31-i]);
ddf827b2 174 pax->vout = opretbuf[len++];
175 pax->vout += ((uint32_t)opretbuf[len++] << 8);
bff51d67 176 //printf(" txid v.%d\n",pax->vout);
ddf827b2 177 }
ad66994d 178 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->komodoshis),&pax->komodoshis);
ddf827b2 179 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->fiatoshis),&pax->fiatoshis);
180 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->height),&pax->height);
181 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->otherheight),&pax->otherheight);
182 if ( rwflag != 0 )
183 {
184 memcpy(&opretbuf[len],pax->rmd160,20), len += 20;
185 for (i=0; i<4; i++)
186 opretbuf[len++] = pax->source[i];
187 }
188 else
189 {
190 memcpy(pax->rmd160,&opretbuf[len],20), len += 20;
191 for (i=0; i<4; i++)
192 pax->source[i] = opretbuf[len++];
193 }
7cc6844b 194 return(len);
195}
196
b89ab77c 197int32_t komodo_issued_opreturn(char *base,uint256 *txids,uint16_t *vouts,int64_t *values,int64_t *srcvalues,int32_t *kmdheights,int32_t *otherheights,int8_t *baseids,uint8_t *rmd160s,uint8_t *opretbuf,int32_t opretlen,int32_t iskomodo)
63b289ad 198{
54661780 199 struct pax_transaction p,*pax; int32_t i,n=0,j,len=0,incr,height,otherheight; uint8_t type,rmd160[20]; uint64_t fiatoshis; char symbol[16];
6cd01056 200 if ( KOMODO_PAX == 0 )
201 return(0);
c83633f8 202 incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4));
085c4ee4 203 //41e77b91cb68dc2aa02fa88550eae6b6d44db676a7e935337b6d1392d9718f03cb0200305c90660400000000fbcbeb1f000000bde801006201000058e7945ad08ddba1eac9c9b6c8e1e97e8016a2d152
8b4d7f27 204
205 // 41e94d736ec69d88c08b5d238abeeca609c02357a8317e0d56c328bcb1c259be5d0200485bc80200000000404b4c000000000059470200b80b000061f22ba7d19fe29ac3baebd839af8b7127d1f9075553440046bb4cc7a3b5cd39dffe7206507a3482a00780e617f68b273cce9817ed69298d02001069ca1b0000000080f0fa02000000005b470200b90b000061f22ba7d19fe29ac3baebd839af8b7127d1f90755
206
40f3d9cc 207 //for (i=0; i<opretlen; i++)
208 // printf("%02x",opretbuf[i]);
b3183e3e 209 //printf(" opretlen.%d (%s)\n",opretlen,base);
40f3d9cc 210 //printf(" opretlen.%d vs %d incr.%d (%d)\n",opretlen,(int32_t)(2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 2),incr,opretlen/incr);
c3152c23 211 //if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
63b289ad 212 {
947d956f 213 type = opretbuf[0];
63b289ad 214 opretbuf++, opretlen--;
e8433f7a 215 for (n=0; n<opretlen/incr; n++)
63b289ad 216 {
05d91eb2 217 if ( iskomodo != 0 )
218 {
e8433f7a 219 memset(&p,0,sizeof(p));
ffec138e 220 len += komodo_rwapproval(0,&opretbuf[len],&p);
b89ab77c 221 if ( values != 0 && srcvalues != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 && rmd160s != 0 )
1ed46112 222 {
63342b19 223 txids[n] = p.txid;
224 vouts[n] = p.vout;
c65d0868 225 values[n] = (strcmp("KMD",base) == 0) ? p.komodoshis : p.fiatoshis;
226 srcvalues[n] = (strcmp("KMD",base) == 0) ? p.fiatoshis : p.komodoshis;
a3f1fb69 227 kmdheights[n] = p.height;
228 otherheights[n] = p.otherheight;
7cc6844b 229 memcpy(&rmd160s[n * 20],p.rmd160,20);
1dbea872 230 baseids[n] = komodo_baseid(p.source);
109afe64 231 if ( 0 )
71c9af7c 232 {
233 char coinaddr[64];
234 bitcoin_address(coinaddr,60,&rmd160s[n * 20],20);
947d956f 235 printf(">>>>>>> %s: (%s) fiat %.8f kmdheight.%d other.%d -> %s %.8f\n",type=='A'?"approvedA":"issuedX",baseids[n]>=0?CURRENCIES[baseids[n]]:"???",dstr(p.fiatoshis),kmdheights[n],otherheights[n],coinaddr,dstr(values[n]));
71c9af7c 236 }
52e87248 237 }
05d91eb2 238 }
7cc6844b 239 else
240 {
5cb9312f 241 for (i=0; i<4; i++)
242 base[i] = opretbuf[opretlen-4+i];
7cc6844b 243 for (j=0; j<32; j++)
244 {
245 ((uint8_t *)&txids[n])[j] = opretbuf[len++];
246 //printf("%02x",((uint8_t *)&txids[n])[j]);
247 }
248 vouts[n] = opretbuf[len++];
249 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
cb3bf8df 250 baseids[n] = komodo_baseid(base);
c5c0e527 251 if ( (pax= komodo_paxfinds(txids[n],vouts[n])) != 0 )
54661780 252 {
c3152c23 253 values[n] = (strcmp("KMD",base) == 0) ? pax->komodoshis : pax->fiatoshis;
254 srcvalues[n] = (strcmp("KMD",base) == 0) ? pax->fiatoshis : pax->komodoshis;
54661780 255 kmdheights[n] = pax->height;
256 otherheights[n] = pax->otherheight;
257 memcpy(&rmd160s[n * 20],pax->rmd160,20);
258 }
7cc6844b 259 }
6d898f42 260 //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
63b289ad 261 }
262 }
263 return(n);
264}
265
865bd972 266int32_t komodo_paxcmp(char *symbol,int32_t kmdheight,uint64_t value,uint64_t checkvalue,uint64_t seed)
267{
268 int32_t ratio;
269 if ( seed == 0 && checkvalue != 0 )
270 {
271 ratio = ((value << 6) / checkvalue);
272 if ( ratio >= 63 && ratio <= 65 )
273 return(0);
274 else
275 {
7d935b7e 276 if ( kmdheight >= 238000 )
865bd972 277 printf("ht.%d ignore mismatched %s value %lld vs checkvalue %lld -> ratio.%d\n",kmdheight,symbol,(long long)value,(long long)checkvalue,ratio);
278 return(-1);
279 }
280 }
281 else if ( checkvalue != 0 )
282 {
283 ratio = ((value << 10) / checkvalue);
284 if ( ratio >= 1023 && ratio <= 1025 )
285 return(0);
286 }
287 return(value != checkvalue);
288}
289
f1db88ea 290uint64_t komodo_paxtotal()
291{
a73709a3 292 struct pax_transaction *pax,*pax2,*tmp,*tmp2; char symbol[16],dest[16],*str; int32_t i,ht; int64_t checktoshis; uint64_t seed,total = 0; struct komodo_state *basesp;
9aefc81b 293 if ( KOMODO_PAX == 0 || KOMODO_PASSPORT_INITDONE == 0 )
6cd01056 294 return(0);
48a3cd18 295 if ( komodo_isrealtime(&ht) == 0 )
f1db88ea 296 return(0);
ca75995d 297 else
298 {
299 HASH_ITER(hh,PAX,pax,tmp)
300 {
5ccb222a 301 if ( pax->marked != 0 )
302 continue;
85779087 303 if ( pax->type == 'A' || pax->type == 'D' || pax->type == 'X' )
304 str = pax->symbol;
305 else str = pax->source;
306 basesp = komodo_stateptrget(str);
115e6a5c 307 if ( basesp != 0 && pax->didstats == 0 )
ca75995d 308 {
6e458bf3 309 if ( pax->type == 'I' && (pax2= komodo_paxfind(pax->txid,pax->vout,'D')) != 0 )
ca75995d 310 {
3cfd4837 311 if ( pax2->fiatoshis != 0 )
312 {
313 pax->komodoshis = pax2->komodoshis;
314 pax->fiatoshis = pax2->fiatoshis;
315 basesp->issued += pax->fiatoshis;
316 pax->didstats = 1;
54f87e10 317 if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 )
318 printf("########### %p issued %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,str,dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight);
c3111e7b 319 pax2->marked = pax->height;
54f87e10 320 pax->marked = pax->height;
3cfd4837 321 }
fa81b5ae 322 }
115e6a5c 323 else if ( pax->type == 'W' )
324 {
431af13e 325 //bitcoin_address(coinaddr,addrtype,rmd160,20);
1bc5fdf8 326 if ( (checktoshis= komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis)) != 0 )
327 {
db7becdc 328 if ( komodo_paxcmp(pax->source,pax->height,pax->komodoshis,checktoshis,seed) != 0 )
772c13de 329 {
1bc5fdf8 330 pax->marked = pax->height;
60cd57df 331 //printf("WITHDRAW.%s mark <- %d %.8f != %.8f\n",pax->source,pax->height,dstr(checktoshis),dstr(pax->komodoshis));
772c13de 332 }
1bc5fdf8 333 else if ( pax->validated == 0 )
334 {
d4003105 335 pax->validated = pax->komodoshis = checktoshis;
109afe64 336 //int32_t j; for (j=0; j<32; j++)
337 // printf("%02x",((uint8_t *)&pax->txid)[j]);
2405bc66 338 //if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 )
109afe64 339 // printf(" v%d %p got WITHDRAW.%s kmd.%d ht.%d %.8f -> %.8f/%.8f\n",pax->vout,pax,pax->source,pax->height,pax->otherheight,dstr(pax->fiatoshis),dstr(pax->komodoshis),dstr(checktoshis));
1bc5fdf8 340 }
341 }
115e6a5c 342 }
ca75995d 343 }
344 }
345 }
ede04b12 346 komodo_stateptr(symbol,dest);
f1db88ea 347 HASH_ITER(hh,PAX,pax,tmp)
348 {
115e6a5c 349 pax->ready = 0;
d9229150 350 char str[65],coinaddr[64]; bits256 tmp;
351 bitcoin_address(coinaddr,60,pax->rmd160,20);
7658f5bd 352 memcpy(&tmp,&pax->txid,sizeof(tmp));
d9229150 353 if ( 0 && pax->fiatoshis > 9000*SATOSHIDEN )//pax->type == 'A' )
354 printf("(%s).%c pax.%s <- %s marked.%d %.8f -> %.8f validated.%d approved.%d %s\n",coinaddr,pax->type,pax->symbol,pax->source,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->validated != 0,pax->approved != 0,bits256_str(str,tmp));
5ccb222a 355 if ( pax->marked != 0 )
356 continue;
cfa1fdda 357 if ( strcmp(symbol,pax->symbol) == 0 || pax->type == 'A' )
f1db88ea 358 {
ede04b12 359 if ( pax->marked == 0 )
360 {
361 if ( komodo_is_issuer() != 0 )
1bc5fdf8 362 {
0449e3f0 363 if ( pax->validated != 0 && pax->type == 'D' )
1bc5fdf8 364 {
365 total += pax->fiatoshis;
366 pax->ready = 1;
367 }
368 }
371a9c87 369 else if ( pax->approved != 0 && pax->type == 'A' )
992c3be5 370 {
371 if ( pax->validated != 0 )
02c36c15 372 {
992c3be5 373 total += pax->komodoshis;
02c36c15 374 pax->ready = 1;
375 }
992c3be5 376 else
377 {
378 seed = 0;
f186b8a1 379 checktoshis = komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis);
9ae3d21c 380 //printf("paxtotal PAX_fiatdest ht.%d price %s %.8f -> KMD %.8f vs %.8f\n",pax->height,pax->symbol,(double)pax->fiatoshis/COIN,(double)pax->komodoshis/COIN,(double)checktoshis/COIN);
381 //printf(" v%d %.8f k.%d ht.%d\n",pax->vout,dstr(pax->komodoshis),pax->height,pax->otherheight);
6e458bf3 382 if ( seed != 0 && checktoshis != 0 )
992c3be5 383 {
115e6a5c 384 if ( checktoshis == pax->komodoshis )
992c3be5 385 {
386 total += pax->komodoshis;
387 pax->validated = pax->komodoshis;
115e6a5c 388 pax->ready = 1;
992c3be5 389 } else pax->marked = pax->height;
390 }
391 }
392 }
a8ffc9b9 393 if ( 0 && pax->ready != 0 )
649a9ebf 394 printf("%p (%c) pax.%s marked.%d %.8f -> %.8f validated.%d approved.%d ready.%d ht.%d\n",pax,pax->type,pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->validated != 0,pax->approved != 0,pax->ready,pax->height);
ede04b12 395 }
f1db88ea 396 }
397 }
5481c677 398 //printf("paxtotal %.8f\n",dstr(total));
f1db88ea 399 return(total);
400}
401
fa1a55cd 402static int _paxorder(const void *a,const void *b)
f360ea0a 403{
fa1a55cd 404#define pax_a (*(struct pax_transaction **)a)
405#define pax_b (*(struct pax_transaction **)b)
406 uint64_t aval,bval;
d936b97f 407 aval = pax_a->fiatoshis + pax_a->komodoshis + pax_a->height;
408 bval = pax_b->fiatoshis + pax_b->komodoshis + pax_b->height;
fa1a55cd 409 if ( bval > aval )
410 return(-1);
411 else if ( bval < aval )
412 return(1);
413 return(0);
414#undef pax_a
415#undef pax_b
416}
417
418int32_t komodo_pending_withdraws(char *opretstr) // todo: enforce deterministic order
419{
6b3eb2c4 420 struct pax_transaction *pax,*pax2,*tmp,*paxes[64]; uint8_t opretbuf[16384]; int32_t i,n,ht,len=0; uint64_t total = 0;
9aefc81b 421 if ( KOMODO_PAX == 0 || KOMODO_PASSPORT_INITDONE == 0 )
6cd01056 422 return(0);
f360ea0a 423 if ( komodo_isrealtime(&ht) == 0 || ASSETCHAINS_SYMBOL[0] != 0 )
424 return(0);
fa1a55cd 425 n = 0;
f360ea0a 426 HASH_ITER(hh,PAX,pax,tmp)
427 {
f1e6a937 428 if ( pax->type == 'W' )
f360ea0a 429 {
6b3eb2c4 430 if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'A')) != 0 )
431 {
432 if ( pax2->approved != 0 )
433 pax->approved = pax2->approved;
434 }
435 else if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'X')) != 0 )
0b262b65 436 pax->approved = pax->height;
01d3b36a 437 //printf("pending_withdraw: pax %s marked.%u approved.%u validated.%llu\n",pax->symbol,pax->marked,pax->approved,(long long)pax->validated);
7d121b40 438 if ( pax->marked == 0 && pax->approved == 0 && pax->validated != 0 ) //strcmp((char *)"KMD",pax->symbol) == 0 &&
f1e6a937 439 {
fa1a55cd 440 if ( n < sizeof(paxes)/sizeof(*paxes) )
ac4eb454 441 {
fa1a55cd 442 paxes[n++] = pax;
2e575216 443 //int32_t j; for (j=0; j<32; j++)
444 // printf("%02x",((uint8_t *)&pax->txid)[j]);
445 //printf(" %s.(kmdht.%d ht.%d marked.%u approved.%d validated %.8f) %.8f\n",pax->source,pax->height,pax->otherheight,pax->marked,pax->approved,dstr(pax->validated),dstr(pax->komodoshis));
ac4eb454 446 }
f1e6a937 447 }
f360ea0a 448 }
449 }
fa1a55cd 450 opretstr[0] = 0;
451 if ( n > 0 )
452 {
453 opretbuf[len++] = 'A';
454 qsort(paxes,n,sizeof(*paxes),_paxorder);
455 for (i=0; i<n; i++)
456 {
457 if ( len < (sizeof(opretbuf)>>3)*7 )
458 len += komodo_rwapproval(1,&opretbuf[len],paxes[i]);
459 }
460 if ( len > 0 )
461 init_hexbytes_noT(opretstr,opretbuf,len);
462 }
132aea8c 463 //fprintf(stderr,"komodo_pending_withdraws len.%d PAXTOTAL %.8f\n",len,dstr(komodo_paxtotal()));
f360ea0a 464 return(len);
465}
a4c67285 466
635dd34d 467int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo)
429dabb5 468{
1c05f0b5 469 struct pax_transaction *pax,*tmp; char symbol[16],dest[16]; uint8_t *script,opcode,opret[16384],data[16384]; int32_t i,baseid,ht,len=0,opretlen=0,numvouts=1; struct komodo_state *sp; uint64_t available,deposited,issued,withdrawn,approved,redeemed,mask;
9aefc81b 470 if ( KOMODO_PAX == 0 || KOMODO_PASSPORT_INITDONE == 0 )
6cd01056 471 return(0);
76d59c1d 472 struct komodo_state *kmdsp = komodo_stateptrget((char *)"KMD");
635dd34d 473 sp = komodo_stateptr(symbol,dest);
27bf3c5e 474 strcpy(symbol,base);
b27b2877 475 if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 )
716fb382 476 return(0);
429dabb5 477 PENDING_KOMODO_TX = 0;
b8cceac5 478 for (i=0; i<3; i++)
479 {
480 if ( komodo_isrealtime(&ht) != 0 )
481 break;
482 sleep(1);
483 }
484 if ( i == 3 )
485 {
486 printf("%s not realtime ht.%d\n",ASSETCHAINS_SYMBOL,ht);
487 return(0);
488 }
a3f23ee2 489 if ( tokomodo == 0 )
48e96ba1 490 {
77be6cd9 491 opcode = 'I';
57f349cb 492 }
493 else
494 {
495 opcode = 'X';
57f349cb 496 if ( komodo_paxtotal() == 0 )
497 return(0);
498 }
c169f64b 499 HASH_ITER(hh,PAX,pax,tmp)
429dabb5 500 {
8b4d7f27 501 if ( pax->type != 'D' && pax->type != 'A' )
502 continue;
aa114a60 503 {
504#ifdef KOMODO_ASSETCHAINS_WAITNOTARIZE
1d474f88 505 if ( pax->height > 236000 )
89ede5f3 506 {
507 if ( kmdsp != 0 && kmdsp->NOTARIZED_HEIGHT >= pax->height )
508 pax->validated = pax->komodoshis;
509 else if ( kmdsp->CURRENT_HEIGHT > pax->height+30 )
510 pax->validated = pax->ready = 0;
511 }
512 else
513 {
514 if ( kmdsp != 0 && (kmdsp->NOTARIZED_HEIGHT >= pax->height || kmdsp->CURRENT_HEIGHT > pax->height+30) ) // assumes same chain as notarize
515 pax->validated = pax->komodoshis;
516 else pax->validated = pax->ready = 0;
517 }
13c01d29 518#else
519 pax->validated = pax->komodoshis;
aa114a60 520#endif
521 }
25d878f4 522 if ( ASSETCHAINS_SYMBOL[0] != 0 && (pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis) )
1c05f0b5 523 {
68cfaa13 524 if ( pax->height > 214700 || strcmp(ASSETCHAINS_SYMBOL,symbol) == 0 )
056bd9be 525 printf("miner.[%s]: skip %s %.8f when avail %.8f deposited %.8f, issued %.8f withdrawn %.8f approved %.8f redeemed %.8f\n",ASSETCHAINS_SYMBOL,symbol,dstr(pax->fiatoshis),dstr(available),dstr(deposited),dstr(issued),dstr(withdrawn),dstr(approved),dstr(redeemed));
4502c41e 526 pax->marked = pax->fiatoshis;
527 continue;
1c05f0b5 528 }
274dd7a5 529 /*printf("pax.%s marked.%d %.8f -> %.8f ready.%d validated.%d\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->ready!=0,pax->validated!=0);
02c36c15 530 if ( pax->marked != 0 || (pax->type != 'D' && pax->type != 'A') || pax->ready == 0 )
a06196fe 531 {
532 printf("reject 2\n");
e5430f52 533 continue;
274dd7a5 534 }*/
a822a070 535 if ( ASSETCHAINS_SYMBOL[0] != 0 && (strcmp(pax->symbol,symbol) != 0 || pax->validated == 0 || pax->ready == 0) )
8bd946ce 536 {
a8ffc9b9 537 if ( strcmp(pax->symbol,ASSETCHAINS_SYMBOL) == 0 )
a822a070 538 printf("pax->symbol.%s != %s or null pax->validated %.8f ready.%d ht.(%d %d)\n",pax->symbol,symbol,dstr(pax->validated),pax->ready,kmdsp->CURRENT_HEIGHT,pax->height);
8bd946ce 539 continue;
540 }
fc40c343 541 if ( pax->ready == 0 )
542 continue;
582c6afe 543 if ( pax->type == 'A' && ASSETCHAINS_SYMBOL[0] == 0 )
544 {
76d59c1d 545 if ( kmdsp != 0 )
582c6afe 546 {
547 if ( (baseid= komodo_baseid(pax->symbol)) < 0 || ((1LL << baseid) & sp->RTmask) == 0 )
548 {
549 printf("not RT for (%s) %llx baseid.%d %llx\n",pax->symbol,(long long)sp->RTmask,baseid,(long long)(1LL<<baseid));
550 continue;
551 }
76d59c1d 552 } else continue;
582c6afe 553 }
554
5481c677 555 //printf("redeem.%d? (%c) %p pax.%s marked.%d %.8f -> %.8f ready.%d validated.%d approved.%d\n",tokomodo,pax->type,pax,pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->ready!=0,pax->validated!=0,pax->approved!=0);
49058e97 556 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
635dd34d 557 printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
e5430f52 558 txNew->vout.resize(numvouts+1);
1fc75a27 559 txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis;
e5430f52 560 txNew->vout[numvouts].scriptPubKey.resize(25);
561 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
562 *script++ = 0x76;
563 *script++ = 0xa9;
564 *script++ = 20;
565 memcpy(script,pax->rmd160,20), script += 20;
566 *script++ = 0x88;
567 *script++ = 0xac;
a3f23ee2 568 if ( tokomodo == 0 )
16bed6ac 569 {
570 for (i=0; i<32; i++)
16bed6ac 571 data[len++] = ((uint8_t *)&pax->txid)[i];
16bed6ac 572 data[len++] = pax->vout & 0xff;
573 data[len++] = (pax->vout >> 8) & 0xff;
e5430f52 574 PENDING_KOMODO_TX += pax->fiatoshis;
16bed6ac 575 }
fbc4209b 576 else
577 {
e0357628 578 len += komodo_rwapproval(1,&data[len],pax);
fbc4209b 579 PENDING_KOMODO_TX += pax->komodoshis;
1eaffd74 580 printf(" len.%d vout.%u DEPOSIT %.8f <- pax.%s pending ht %d %d %.8f | ",len,pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,pax->height,pax->otherheight,dstr(PENDING_KOMODO_TX));
fbc4209b 581 }
d4ab9b56 582 if ( numvouts++ >= 1 )
e5430f52 583 break;
429dabb5 584 }
585 if ( numvouts > 1 )
586 {
0e3472c1 587 if ( tokomodo != 0 )
27bf3c5e 588 strcpy(symbol,(char *)"KMD");
9ef47e29 589 for (i=0; symbol[i]!=0; i++)
590 data[len++] = symbol[i];
a5ad8f02 591 data[len++] = 0;
8b4d7f27 592 for (i=0; i<len; i++)
593 printf("%02x",data[i]);
594 printf(" <- data[%d]\n",len);
77be6cd9 595 opretlen = komodo_opreturnscript(opret,opcode,data,len);
50824530 596 txNew->vout.resize(numvouts+1);
597 txNew->vout[numvouts].nValue = 0;
598 txNew->vout[numvouts].scriptPubKey.resize(opretlen);
599 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
429dabb5 600 memcpy(script,opret,opretlen);
8b4d7f27 601 for (i=0; i<8; i++)
602 printf("%02x",opret[i]);
e03bcdd6 603 printf(" <- opret, MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen);
d9a9d562 604 return(1);
9ef47e29 605 }
d9a9d562 606 return(0);
429dabb5 607}
608
5b6fb570 609const char *banned_txids[] =
557d9a23 610{
6fe17943 611 "78cb4e21245c26b015b888b14c4f5096e18137d2741a6de9734d62b07014dfca", //233559
612 "00697be658e05561febdee1aafe368b821ca33fbb89b7027365e3d77b5dfede5", //234172
613 "e909465788b32047c472d73e882d79a92b0d550f90be008f76e1edaee6d742ea", //234187
614 "f56c6873748a327d0b92b8108f8ec8505a2843a541b1926022883678fb24f9dc", //234188
615 "abf08be07d8f5b3a433ddcca7ef539e79a3571632efd6d0294ec0492442a0204", //234213
616 "3b854b996cc982fba8c06e76cf507ae7eed52ab92663f4c0d7d10b3ed879c3b0", //234367
617 "fa9e474c2cda3cb4127881a40eb3f682feaba3f3328307d518589024a6032cc4", //234635
618 "ca746fa13e0113c4c0969937ea2c66de036d20274efad4ce114f6b699f1bc0f3", //234662
619 "43ce88438de4973f21b1388ffe66e68fda592da38c6ef939be10bb1b86387041", //234697
620 "0aeb748de82f209cd5ff7d3a06f65543904c4c17387c9d87c65fd44b14ad8f8c", //234899
621 "bbd3a3d9b14730991e1066bd7c626ca270acac4127131afe25f877a5a886eb25", //235252
622 "fa9943525f2e6c32cbc243294b08187e314d83a2870830180380c3c12a9fd33c", //235253
623 "a01671c8775328a41304e31a6693bbd35e9acbab28ab117f729eaba9cb769461", //235265
624 "2ef49d2d27946ad7c5d5e4ab5c089696762ff04e855f8ab48e83bdf0cc68726d", //235295
625 "c85dcffb16d5a45bd239021ad33443414d60224760f11d535ae2063e5709efee", //235296
c7a3aca6 626};
627
628void komodo_bannedset(uint256 *array,int32_t max)
629{
630 int32_t i;
081ab21c 631 for (i=0; i<sizeof(banned_txids)/sizeof(*banned_txids); i++)
632 array[i] = uint256S(banned_txids[i]);
c7a3aca6 633 if ( i != max )
634 printf("banned txid array error i.%d != max.%d\n",i,max);
36f1b84b 635 //else printf("set %d banned txids\n",max);
c7a3aca6 636}
637
638int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
639{
640 static uint256 array[15];
6c8cc7ee 641 int32_t i,j,k,n,ht,txn_count,num,opretlen,offset=1,errs=0,matched=0,kmdheights[64],otherheights[64]; uint256 hash,txids[64]; char symbol[16],base[16]; uint16_t vouts[64]; int8_t baseids[64]; uint8_t *script,opcode,rmd160s[64*20]; uint64_t total,available,deposited,issued,withdrawn,approved,redeemed; int64_t values[64],srcvalues[64]; struct pax_transaction *pax; struct komodo_state *sp;
c7a3aca6 642 if ( *(int32_t *)&array[0] == 0 )
643 komodo_bannedset(array,(int32_t)(sizeof(array)/sizeof(*array)));
1ed46112 644 memset(baseids,0xff,sizeof(baseids));
645 memset(values,0,sizeof(values));
b89ab77c 646 memset(srcvalues,0,sizeof(srcvalues));
f360ea0a 647 memset(rmd160s,0,sizeof(rmd160s));
1ed46112 648 memset(kmdheights,0,sizeof(kmdheights));
649 memset(otherheights,0,sizeof(otherheights));
c7a3aca6 650 txn_count = block.vtx.size();
651 for (i=0; i<txn_count; i++)
652 {
653 n = block.vtx[i].vin.size();
654 for (j=0; j<n; j++)
655 {
081ab21c 656 for (k=0; k<sizeof(array)/sizeof(*array); k++)
c7a3aca6 657 {
081ab21c 658 if ( block.vtx[i].vin[j].prevout.hash == array[k] && block.vtx[i].vin[j].prevout.n == 1 )
659 {
e699e13d 660 printf("banned tx.%d being used at ht.%d txi.%d vini.%d\n",k,height,i,j);
081ab21c 661 return(-1);
662 }
c7a3aca6 663 }
664 }
665 }
666 n = block.vtx[0].vout.size();
667 script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
12d47153 668 if ( n <= 2 || script[0] != 0x6a )
d083ecd5 669 {
6c8cc7ee 670 total = 0;
671 for (i=1; i<n; i++)
fa6b77c6 672 total += block.vtx[0].vout[i].nValue;
6c8cc7ee 673 if ( total > COIN/10 )
d083ecd5 674 {
4fb9b706 675 //fprintf(stderr,">>>>>>>> <<<<<<<<<< ht.%d illegal nonz output %.8f n.%d\n",height,dstr(block.vtx[0].vout[1].nValue),n);
49508d3c 676 if ( height >= 235300 )
d083ecd5 677 return(-1);
678 }
12d47153 679 return(0);
d083ecd5 680 }
c7a3aca6 681 //fprintf(stderr,"ht.%d n.%d nValue %.8f (%d %d %d)\n",height,n,dstr(block.vtx[0].vout[1].nValue),KOMODO_PAX,komodo_isrealtime(&ht),KOMODO_PASSPORT_INITDONE);
e57971f7 682 if ( komodo_isrealtime(&ht) == 0 || KOMODO_PASSPORT_INITDONE == 0 ) //KOMODO_PAX == 0 ||
7e6f6616 683 return(0);
12d47153 684 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
77be6cd9 685 if ( ASSETCHAINS_SYMBOL[0] == 0 )
686 {
85de2861 687 //for (i=0; i<opretlen; i++)
688 // printf("%02x",script[i]);
689 //printf(" height.%d checkdeposit n.%d [%02x] [%c] %d vs %d\n",height,n,script[0],script[offset],script[offset],'X');
77be6cd9 690 opcode = 'X';
09a235e1 691 strcpy(symbol,(char *)"KMD");
77be6cd9 692 }
693 else
694 {
695 strcpy(symbol,ASSETCHAINS_SYMBOL);
696 opcode = 'I';
716fb382 697 if ( komodo_baseid(symbol) < 0 )
698 {
c7a3aca6 699 if ( block.vtx[0].vout.size() != 1 )
716fb382 700 {
701 printf("%s has more than one coinbase?\n",symbol);
702 return(-1);
703 }
704 return(0);
705 }
77be6cd9 706 }
c7a3aca6 707 if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
557d9a23 708 {
b89ab77c 709 if ( (num= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,&script[offset],opretlen,opcode == 'X')) > 0 )
557d9a23 710 {
711 for (i=1; i<n-1; i++)
712 {
fe348242 713 if ( (sp= komodo_stateptrget(CURRENCIES[baseids[i-1]])) != 0 && (sp->RTmask & (1LL << baseids[i-1])) == 0 )
b8cceac5 714 {
4fb9b706 715 /*printf("skip checkdeposit.%s not RT %llx\n",CURRENCIES[baseids[i-1]],(long long)sp->RTmask);
716 matched++;
717 continue;*/
b8cceac5 718 }
1bc5fdf8 719 if ( (pax= komodo_paxfinds(txids[i-1],vouts[i-1])) != 0 ) // finds... make sure right one
5a2c5b6f 720 {
ca75995d 721 pax->type = opcode;
d5894ea8 722 if ( opcode == 'I' && (pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis) )
1c05f0b5 723 {
507fbc47 724 printf("checkdeposit.[%s]: skip %s %.8f when avail %.8f deposited %.8f, issued %.8f withdrawn %.8f approved %.8f redeemed %.8f\n",ASSETCHAINS_SYMBOL,symbol,dstr(pax->fiatoshis),dstr(available),dstr(deposited),dstr(issued),dstr(withdrawn),dstr(approved),dstr(redeemed));
1c05f0b5 725 continue;
726 }
c7a3aca6 727 if ( ((opcode == 'I' && (pax->fiatoshis == 0 || pax->fiatoshis == block.vtx[0].vout[i].nValue)) || (opcode == 'X' && (pax->komodoshis == 0 || pax->komodoshis == block.vtx[0].vout[i].nValue))) )
f10b8e2e 728 {
02c36c15 729 if ( pax->marked != 0 && height >= 80820 )
73585700 730 {
c7a3aca6 731 printf(">>>>>>>>>>> %c errs.%d i.%d match %.8f vs %.8f paxmarked.%d kht.%d ht.%d\n",opcode,errs,i,dstr(opcode == 'I' ? pax->fiatoshis : pax->komodoshis),dstr(block.vtx[0].vout[i].nValue),pax->marked,pax->height,pax->otherheight);
8b4d7f27 732 if ( pax->komodoshis != 0 || pax->fiatoshis != 0 )
733 errs++;
e03bcdd6 734 else matched++; // onetime init bypass
4eb7c730 735 }
736 else
737 {
4fb9b706 738 if ( opcode == 'X' && strcmp(ASSETCHAINS_SYMBOL,CURRENCIES[baseids[i]]) == 0 )
4eb7c730 739 printf("check deposit validates %s %.8f -> %.8f\n",CURRENCIES[baseids[i]],dstr(srcvalues[i]),dstr(values[i]));
740 matched++;
741 }
f10b8e2e 742 }
743 else
744 {
1c05f0b5 745 for (j=0; j<32; j++)
746 printf("%02x",((uint8_t *)&txids[i-1])[j]);
747 printf(" cant paxfind %c txid\n",opcode);
c7a3aca6 748 printf(">>>>>>>>>>> %c errs.%d i.%d match %.8f vs %.8f pax.%p\n",opcode,errs,i,dstr(opcode == 'I' ? pax->fiatoshis : pax->komodoshis),dstr(block.vtx[0].vout[i].nValue),pax);
f10b8e2e 749 }
5a2c5b6f 750 }
6813c8d5 751 else if ( kmdheights[i-1] > 0 && otherheights[i-1] > 0 )
cb040aa1 752 {
c7a3aca6 753 hash = block.GetHash();
1c05f0b5 754 for (j=0; j<32; j++)
755 printf("%02x",((uint8_t *)&hash)[j]);
7ee875e8 756 printf(" kht.%d ht.%d %.8f %.8f blockhash couldnt find vout.[%d]\n",kmdheights[i-1],otherheights[i-1],dstr(values[i-1]),dstr(srcvalues[i]),i);
cb040aa1 757 }
557d9a23 758 }
283168fd 759 if ( ASSETCHAINS_SYMBOL[0] == 0 )
81041ab2 760 {
283168fd 761 if ( height > 0 && (height < chainActive.Tip()->nHeight || (height >= chainActive.Tip()->nHeight && komodo_isrealtime(&ht) != 0)) && matched != num )
52b2e68d 762 {
5641c3d2 763 printf("WOULD REJECT %s: ht.%d (%c) matched.%d vs num.%d tip.%d isRT.%d\n",symbol,height,opcode,matched,num,(int32_t)chainActive.Tip()->nHeight,komodo_isrealtime(&ht));
764 // can easily happen depending on order of loading
765 if ( height > 200000 )
766 {
767 printf("REJECT: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num);
768 return(-1);
769 }
4fb9b706 770 }
5641c3d2 771 }
772 else
773 {
4df419cd 774 if ( height > 0 && (height < chainActive.Tip()->nHeight || (height >= chainActive.Tip()->nHeight && komodo_isrealtime(&ht) != 0)) && matched != num )
5641c3d2 775 {
776 printf("REJECT %s: ht.%d (%c) matched.%d vs num.%d tip.%d isRT.%d\n",symbol,height,opcode,matched,num,(int32_t)chainActive.Tip()->nHeight,komodo_isrealtime(&ht));
52b2e68d 777 return(-1);
778 }
81041ab2 779 }
557d9a23 780 }
22c10675 781 else
782 {
783 printf("no opreturn entries to check\n");
784 return(-1);
785 }
b98053e2 786 //printf("opretlen.%d num.%d\n",opretlen,num);
22c10675 787 }
788 else
789 {
790 printf("not proper vout with opreturn format\n");
791 return(-1);
792 }
557d9a23 793 return(0);
794}
795
cd26c1f3 796const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source)
429dabb5 797{
4c9da8cf 798 uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33]; int32_t didstats,i,j,n,kvheight,len,tokomodo,kmdheight,otherheights[64],kmdheights[64]; int8_t baseids[64]; char base[4],coinaddr[64],destaddr[64]; uint256 txids[64]; uint16_t vouts[64]; uint64_t convtoshis,seed; int64_t fee,fiatoshis,komodoshis,checktoshis,values[64],srcvalues[64]; struct pax_transaction *pax,*pax2; struct komodo_state *basesp; double diff;
1ed46112 799 const char *typestr = "unknown";
088b1fa8 800 if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 && opretbuf[0] != 'K' )
894c570b 801 {
b27b2877 802 //printf("komodo_opreturn skip %s\n",ASSETCHAINS_SYMBOL);
716fb382 803 return("assetchain");
894c570b 804 }
46222318 805 //else if ( KOMODO_PAX == 0 )
806 // return("nopax");
1ed46112 807 memset(baseids,0xff,sizeof(baseids));
808 memset(values,0,sizeof(values));
06cfcf31 809 memset(srcvalues,0,sizeof(srcvalues));
f360ea0a 810 memset(rmd160s,0,sizeof(rmd160s));
1ed46112 811 memset(kmdheights,0,sizeof(kmdheights));
812 memset(otherheights,0,sizeof(otherheights));
9596e284 813 tokomodo = (komodo_is_issuer() == 0);
ab6e81ec 814 if ( opretbuf[0] == 'K' && opretlen != 40 )
15d0fbd4 815 {
6b5cfbb4 816 komodo_kvupdate(opretbuf,opretlen,value);
15d0fbd4 817 }
818 else if ( opretbuf[0] == 'D' )
64bb0834 819 {
9596e284 820 tokomodo = 0;
05a37d87 821 if ( opretlen == 38 ) // any KMD tx
64bb0834 822 {
05a37d87 823 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
abdd8672 824 memset(base,0,sizeof(base));
825 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
abdd8672 826 bitcoin_address(coinaddr,addrtype,rmd160,20);
81a991e4 827 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
abdd8672 828 typestr = "deposit";
bd041a51 829 if ( kmdheight > 195000 || kmdheight <= height )
6e576848 830 {
f5e3cdfa 831 didstats = 0;
134c49d2 832 if ( 0 && strcmp("USD",ASSETCHAINS_SYMBOL) == 0 && kmdheight > 214700 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
8f261c33 833 {
8f68a6a7 834 printf("(%s) (%s) kmdheight.%d vs height.%d check %.8f vs %.8f tokomodo.%d %d seed.%llx paxcmp.%d\n",ASSETCHAINS_SYMBOL,base,kmdheight,height,dstr(checktoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed,komodo_paxcmp(base,kmdheight,value,checktoshis,0));
8f261c33 835 for (i=0; i<32; i++)
836 printf("%02x",((uint8_t *)&txid)[i]);
837 printf(" <- txid.v%u ",vout);
838 for (i=0; i<33; i++)
839 printf("%02x",pubkey33[i]);
840 printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
841 }
41b528bb 842 if ( komodo_paxcmp(base,kmdheight,value,checktoshis,kmdheight < 225000 ? seed : 0) == 0 )
05a37d87 843 {
b915be35 844 if ( (pax= komodo_paxfind(txid,vout,'D')) == 0 )
05a37d87 845 {
c98b3a08 846 if ( (basesp= komodo_stateptrget(base)) != 0 )
05a37d87 847 {
c98b3a08 848 basesp->deposited += fiatoshis;
22a31d52 849 didstats = 1;
134c49d2 850 if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
0398360f 851 printf("########### %p deposited %s += %.8f kmdheight.%d %.8f\n",basesp,base,dstr(fiatoshis),kmdheight,dstr(value));
c72ee808 852 } else printf("cant get stateptr.(%s)\n",base);
4d4194d3 853 komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,'D',kmdheight,height,(char *)"KMD",0);
3f9cad6d 854 }
51e5d952 855 if ( (pax= komodo_paxfind(txid,vout,'D')) != 0 )
3f9cad6d 856 {
8bd946ce 857 pax->height = kmdheight;
858 pax->validated = value;
88dff0d3 859 pax->komodoshis = value;
860 pax->fiatoshis = fiatoshis;
22a31d52 861 if ( didstats == 0 && pax->didstats == 0 )
3f9cad6d 862 {
ca75995d 863 if ( (basesp= komodo_stateptrget(base)) != 0 )
864 {
865 basesp->deposited += fiatoshis;
22a31d52 866 didstats = 1;
134c49d2 867 if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
c0d7dda6 868 printf("########### %p depositedB %s += %.8f/%.8f kmdheight.%d/%d %.8f/%.8f\n",basesp,base,dstr(fiatoshis),dstr(pax->fiatoshis),kmdheight,pax->height,dstr(value),dstr(pax->komodoshis));
ca75995d 869 }
3f9cad6d 870 }
51e5d952 871 if ( didstats != 0 )
872 pax->didstats = 1;
7ddc4ef7 873 if ( (pax2= komodo_paxfind(txid,vout,'I')) != 0 )
874 {
875 pax2->fiatoshis = pax->fiatoshis;
876 pax2->komodoshis = pax->komodoshis;
7ddc4ef7 877 pax->marked = pax2->marked = pax->height;
da02b3be 878 pax2->height = pax->height = height;
25623352 879 if ( pax2->didstats == 0 )
880 {
bde7e5ef 881 if ( (basesp= komodo_stateptrget(base)) != 0 )
882 {
883 basesp->issued += pax2->fiatoshis;
884 pax2->didstats = 1;
134c49d2 885 if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
4c9da8cf 886 {
887 char str[65]; bits256 tmp;
888 memcpy(&tmp,&pax2->txid,sizeof(tmp));
05d80480 889 printf("########### %p issueda %s += %.8f kmdheight.%d %.8f other.%d (%s)\n",basesp,base,dstr(pax2->fiatoshis),pax2->height,dstr(pax2->komodoshis),pax2->otherheight,bits256_str(str,tmp));
4c9da8cf 890 }
bde7e5ef 891 }
25623352 892 }
7ddc4ef7 893 }
3f9cad6d 894 }
eb928486 895 }
7d935b7e 896 else if ( kmdheight > 238000 && (kmdheight > 214700 || strcmp(base,ASSETCHAINS_SYMBOL) == 0) ) //seed != 0 &&
0a31d015 897 printf("pax %s deposit %.8f rejected kmdheight.%d %.8f KMD check %.8f seed.%llu\n",base,dstr(fiatoshis),kmdheight,dstr(value),dstr(checktoshis),(long long)seed);
182dcdec 898 } //else printf("paxdeposit height.%d vs kmdheight.%d\n",height,kmdheight);
64bb0834 899 }
900 }
36b8ed99 901 else if ( opretbuf[0] == 'I' )
902 {
903 tokomodo = 0;
904 if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
905 {
906 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 )
907 {
908 for (i=0; i<n; i++)
909 {
910 if ( baseids[i] < 0 )
911 {
912 printf("%d of %d illegal baseid.%d\n",i,n,baseids[i]);
913 continue;
914 }
915 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
916 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'I',height,0,CURRENCIES[baseids[i]],0);
917 komodo_paxmark(height,txids[i],vouts[i],'I',height);
918 if ( (pax= komodo_paxfind(txids[i],vouts[i],'I')) != 0 )
919 {
920 pax->type = opretbuf[0];
921 strcpy(pax->source,(char *)&opretbuf[opretlen-4]);
a6a33512 922 if ( (pax2= komodo_paxfind(txids[i],vouts[i],'D')) != 0 && pax2->fiatoshis != 0 && pax2->komodoshis != 0 )
36b8ed99 923 {
924 // realtime path?
925 pax->fiatoshis = pax2->fiatoshis;
926 pax->komodoshis = pax2->komodoshis;
927 pax->marked = pax2->marked = pax2->height;
928 if ( pax->didstats == 0 )
929 {
930 if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
931 {
932 basesp->issued += pax->fiatoshis;
933 pax->didstats = 1;
39863e30 934 pax->height = pax2->height;
935 pax->otherheight = height;
b61ed439 936 if ( strcmp("USD",ASSETCHAINS_SYMBOL) == 0 && strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
36b8ed99 937 printf("########### %p issuedb %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,CURRENCIES[baseids[i]],dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight);
938 }
939 }
940 }
941 }
57f349cb 942 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'I',height)) != 0 )
943 komodo_paxdelete(pax);
944 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'D',height)) != 0 )
945 komodo_paxdelete(pax);
36b8ed99 946 }
174cae41 947 } //else printf("opreturn none issued?\n");
36b8ed99 948 }
36b8ed99 949 }
a3608c89 950 else if ( opretbuf[0] == 'W' )//&& opretlen >= 38 )
01d9ed2f 951 {
bc653eee 952 if ( komodo_baseid((char *)&opretbuf[opretlen-4]) >= 0 && strcmp(ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4]) == 0 )
8b4d7f27 953 {
954 for (i=0; i<opretlen; i++)
955 printf("%02x",opretbuf[i]);
d594eaa4 956 printf(" [%s] reject obsolete withdraw request.%s\n",ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4]);
217eb34f 957 return(typestr);
8b4d7f27 958 }
9596e284 959 tokomodo = 1;
01d9ed2f 960 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
961 memset(base,0,sizeof(base));
c3d94aec 962 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&komodoshis);
01d9ed2f 963 bitcoin_address(coinaddr,addrtype,rmd160,20);
c3d94aec 964 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,value);
01d9ed2f 965 typestr = "withdraw";
8b4d7f27 966 //printf(" [%s] WITHDRAW %s.height.%d vs height.%d check %.8f/%.8f vs %.8f tokomodo.%d %d seed.%llx -> (%s) len.%d\n",ASSETCHAINS_SYMBOL,base,kmdheight,height,dstr(checktoshis),dstr(komodoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed,coinaddr,opretlen);
f5e3cdfa 967 didstats = 0;
a39d7b21 968 //if ( komodo_paxcmp(base,kmdheight,komodoshis,checktoshis,seed) == 0 )
01d9ed2f 969 {
51e5d952 970 if ( value != 0 && ((pax= komodo_paxfind(txid,vout,'W')) == 0 || pax->didstats == 0) )
0b556992 971 {
0126d891 972 if ( (basesp= komodo_stateptrget(base)) != 0 )
973 {
974 basesp->withdrawn += value;
975 didstats = 1;
2405bc66 976 if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
5b489b55 977 printf("########### %p withdrawn %s += %.8f check %.8f\n",basesp,base,dstr(value),dstr(checktoshis));
0126d891 978 }
57f349cb 979 if ( 0 && strcmp(base,"RUB") == 0 && (pax == 0 || pax->approved == 0) )
4718923d 980 printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height);
ea365a71 981 }
115e6a5c 982 komodo_gateway_deposit(coinaddr,0,(char *)"KMD",value,rmd160,txid,vout,'W',kmdheight,height,source,0);
51e5d952 983 if ( (pax= komodo_paxfind(txid,vout,'W')) != 0 )
daeecf95 984 {
985 pax->type = opretbuf[0];
1ac3f9ef 986 strcpy(pax->source,base);
987 strcpy(pax->symbol,"KMD");
daeecf95 988 pax->height = kmdheight;
989 pax->otherheight = height;
1e22f865 990 pax->komodoshis = komodoshis;
daeecf95 991 }
a39d7b21 992 } // else printf("withdraw %s paxcmp ht.%d %d error value %.8f -> %.8f vs %.8f\n",base,kmdheight,height,dstr(value),dstr(komodoshis),dstr(checktoshis));
993 // need to allocate pax
a4c67285 994 }
995 else if ( tokomodo != 0 && opretbuf[0] == 'A' )
996 {
9596e284 997 tokomodo = 1;
109afe64 998 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
826bfe51 999 {
1000 for (i=0; i<opretlen; i++)
1001 printf("%02x",opretbuf[i]);
40f3d9cc 1002 printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f opretlen.%d\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()),opretlen);
826bfe51 1003 }
b89ab77c 1004 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
a4c67285 1005 {
1006 for (i=0; i<n; i++)
1007 {
885643be 1008 //for (j=0; j<32; j++)
1009 // printf("%02x",((uint8_t *)&txids[i])[j]);
1010 //printf(" v%d %.8f %.8f k.%d ht.%d base.%d\n",vouts[i],dstr(values[i]),dstr(srcvalues[i]),kmdheights[i],otherheights[i],baseids[i]);
b89ab77c 1011 if ( baseids[i] < 0 )
8e7b40d9 1012 {
afdb28f3 1013 for (i=0; i<opretlen; i++)
1014 printf("%02x",opretbuf[i]);
1015 printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f opretlen.%d\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()),opretlen);
8952ee9d 1016 //printf("baseids[%d] %d\n",i,baseids[i]);
afdb28f3 1017 if ( (pax= komodo_paxfind(txids[i],vouts[i],'W')) != 0 || (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 )
51752631 1018 {
1019 baseids[i] = komodo_baseid(pax->symbol);
1020 printf("override neg1 with (%s)\n",pax->symbol);
1021 }
1022 if ( baseids[i] < 0 )
1023 continue;
8e7b40d9 1024 }
f5e3cdfa 1025 didstats = 0;
0cc6ad81 1026 seed = 0;
d515094d 1027 checktoshis = komodo_paxprice(&seed,kmdheights[i],CURRENCIES[baseids[i]],(char *)"KMD",(uint64_t)values[i]);
8b4d7f27 1028 //printf("PAX_fiatdest ht.%d price %s %.8f -> KMD %.8f vs %.8f\n",kmdheights[i],CURRENCIES[baseids[i]],(double)values[i]/COIN,(double)srcvalues[i]/COIN,(double)checktoshis/COIN);
371a9c87 1029 if ( srcvalues[i] == checktoshis )
a4c67285 1030 {
36a4e92c 1031 if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 )
0fa85cc3 1032 {
371a9c87 1033 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
5481c677 1034 komodo_gateway_deposit(coinaddr,srcvalues[i],CURRENCIES[baseids[i]],values[i],&rmd160s[i*20],txids[i],vouts[i],'A',kmdheights[i],otherheights[i],CURRENCIES[baseids[i]],kmdheights[i]);
371a9c87 1035 if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 )
1036 printf("unexpected null pax for approve\n");
1037 else pax->validated = checktoshis;
1038 if ( (pax2= komodo_paxfind(txids[i],vouts[i],'W')) != 0 )
1039 pax2->approved = kmdheights[i];
7aadf9a9 1040 komodo_paxmark(height,txids[i],vouts[i],'W',height);
371a9c87 1041 //komodo_paxmark(height,txids[i],vouts[i],'A',height);
bca09689 1042 if ( values[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
371a9c87 1043 {
5481c677 1044 basesp->approved += values[i];
371a9c87 1045 didstats = 1;
5a544e6a 1046 printf("pax.%p ########### %p approved %s += %.8f -> %.8f/%.8f kht.%d %d\n",pax,basesp,CURRENCIES[baseids[i]],dstr(values[i]),dstr(srcvalues[i]),dstr(checktoshis),kmdheights[i],otherheights[i]);
371a9c87 1047 }
1048 //printf(" i.%d (%s) <- %.8f ADDFLAG APPROVED\n",i,coinaddr,dstr(values[i]));
0fa85cc3 1049 }
371a9c87 1050 else if ( pax->didstats == 0 && srcvalues[i] != 0 )
3f9cad6d 1051 {
371a9c87 1052 if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
1053 {
5481c677 1054 basesp->approved += values[i];
371a9c87 1055 didstats = 1;
5a544e6a 1056 printf("pax.%p ########### %p approved %s += %.8f -> %.8f/%.8f kht.%d %d\n",pax,basesp,CURRENCIES[baseids[i]],dstr(values[i]),dstr(srcvalues[i]),dstr(checktoshis),kmdheights[i],otherheights[i]);
371a9c87 1057 }
1058 } //else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]);
1059 if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) != 0 )
1060 {
1061 pax->type = opretbuf[0];
1062 pax->approved = kmdheights[i];
1063 pax->validated = checktoshis;
1064 if ( didstats != 0 )
1065 pax->didstats = 1;
7ad9ed4d 1066 //if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
5481c677 1067 //printf(" i.%d approved.%d <<<<<<<<<<<<< APPROVED %p\n",i,kmdheights[i],pax);
371a9c87 1068 }
06d1a655 1069 }
ea365a71 1070 }
dd278be2 1071 } //else printf("n.%d from opreturns\n",n);
bff51d67 1072 //printf("extra.[%d] after %.8f\n",n,dstr(komodo_paxtotal()));
01d9ed2f 1073 }
ca3c6549 1074 else if ( opretbuf[0] == 'X' )
1075 {
9596e284 1076 tokomodo = 1;
c65d0868 1077 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
4c58b338 1078 {
1079 for (i=0; i<n; i++)
1080 {
3f9cad6d 1081 if ( baseids[i] < 0 )
1082 continue;
4c58b338 1083 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
51e5d952 1084 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'X',height,0,(char *)"KMD",0);
aa114a60 1085 komodo_paxmark(height,txids[i],vouts[i],'W',height);
1086 komodo_paxmark(height,txids[i],vouts[i],'A',height);
1087 komodo_paxmark(height,txids[i],vouts[i],'X',height);
51e5d952 1088 if ( (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 )
ca75995d 1089 {
1090 pax->type = opretbuf[0];
6276f755 1091 if ( height < 121842 ) // fields got switched around due to legacy issues and approves
2ee39fae 1092 value = srcvalues[i];
1093 else value = values[i];
1094 if ( baseids[i] >= 0 && value != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
c6eca157 1095 {
2ee39fae 1096 basesp->redeemed += value;
546672f5 1097 pax->didstats = 1;
dff9ec91 1098 if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
e62f7114 1099 printf("ht.%d %.8f ########### %p redeemed %s += %.8f %.8f kht.%d ht.%d\n",height,dstr(value),basesp,CURRENCIES[baseids[i]],dstr(value),dstr(srcvalues[i]),kmdheights[i],otherheights[i]);
c6eca157 1100 }
ca75995d 1101 }
57f349cb 1102 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'W',height)) != 0 )
1103 komodo_paxdelete(pax);
1104 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'A',height)) != 0 )
1105 komodo_paxdelete(pax);
1106 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'X',height)) != 0 )
1107 komodo_paxdelete(pax);
4c58b338 1108 }
dff4ef45 1109 } //else printf("komodo_issued_opreturn returned %d\n",n);
ca3c6549 1110 }
64bb0834 1111 return(typestr);
1112}
1113
30d79208 1114void komodo_passport_iteration()
ab918767 1115{
017d8211 1116 static long lastpos[34]; static char userpass[33][1024]; int32_t maxseconds = 17;
430d3c77 1117 FILE *fp; int32_t baseid,n,ht,isrealtime,expired,refid,blocks,longest; struct komodo_state *sp,*refsp; char *retstr,fname[512],*base,symbol[16],dest[16]; uint32_t buf[3],starttime; cJSON *infoobj,*result; uint64_t RTmask = 0;
2a15e8ea 1118 //printf("PASSPORT.(%s)\n",ASSETCHAINS_SYMBOL);
430d3c77 1119 expired = 0;
17899df4 1120 while ( KOMODO_INITDONE == 0 )
1121 {
e40060c2 1122 fprintf(stderr,"[%s] PASSPORT iteration waiting for KOMODO_INITDONE\n",ASSETCHAINS_SYMBOL);
17899df4 1123 sleep(3);
1124 }
635dd34d 1125 refsp = komodo_stateptr(symbol,dest);
ab918767 1126 if ( ASSETCHAINS_SYMBOL[0] == 0 )
67cd296f 1127 refid = 33;
b307682f 1128 else
716fb382 1129 {
b307682f 1130 refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0
1131 if ( refid == 0 )
1132 {
1133 KOMODO_PASSPORT_INITDONE = 1;
1134 return;
1135 }
716fb382 1136 }
6cd01056 1137 if ( KOMODO_PAX == 0 )
1138 {
1139 KOMODO_PASSPORT_INITDONE = 1;
1140 return;
1141 }
ba37a6b9 1142 starttime = (uint32_t)time(NULL);
49058e97 1143 //printf("PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
959782f9 1144 for (baseid=32; baseid>=0; baseid--)
508b0d3c 1145 {
e22dcc1e 1146 if ( time(NULL) >= starttime+maxseconds )
588bc897 1147 break;
ecf6256c 1148 sp = 0;
1149 isrealtime = 0;
f789edde 1150 base = (char *)CURRENCIES[baseid];
95a6016d 1151 if ( baseid+1 != refid )
ab918767 1152 {
055db9b6 1153 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate");
52f1d788 1154 komodo_nameset(symbol,dest,base);
22b09834 1155 sp = komodo_stateptrget(symbol);
ba37a6b9 1156 n = 0;
22b09834 1157 if ( (fp= fopen(fname,"rb")) != 0 && sp != 0 )
ab918767 1158 {
52f1d788 1159 fseek(fp,0,SEEK_END);
1160 if ( ftell(fp) > lastpos[baseid] )
ab918767 1161 {
894c570b 1162 if ( 0 && lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 )
9a6744f2 1163 printf("passport refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base);
52f1d788 1164 fseek(fp,lastpos[baseid],SEEK_SET);
017d8211 1165 while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 && n < 10000 )
ba37a6b9 1166 {
1167 if ( n == 999 )
1168 {
588bc897 1169 if ( time(NULL) < starttime+maxseconds )
ba37a6b9 1170 n = 0;
430d3c77 1171 else
1172 {
a822a070 1173 //printf("expire passport loop %s -> %s at %ld\n",ASSETCHAINS_SYMBOL,base,lastpos[baseid]);
430d3c77 1174 expired++;
1175 }
ba37a6b9 1176 }
1177 n++;
1178 }
52f1d788 1179 lastpos[baseid] = ftell(fp);
588bc897 1180 if ( lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 )
8d4fe133 1181 printf("from.(%s) lastpos[%s] %ld isrt.%d\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid],komodo_isrealtime(&ht));
49058e97 1182 } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp));
52f1d788 1183 fclose(fp);
14e3fb45 1184 } else printf("error.(%s) %p\n",fname,sp);
a7ef556c 1185 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
1186 if ( (fp= fopen(fname,"rb")) != 0 )
1187 {
1188 if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) )
1189 {
f1db88ea 1190 sp->CURRENT_HEIGHT = buf[0];
43d45898 1191 if ( buf[0] != 0 && buf[0] >= buf[1] && buf[2] > time(NULL)-300 )
6eb41a2f 1192 {
a7ef556c 1193 isrealtime = 1;
6eb41a2f 1194 RTmask |= (1LL << baseid);
635dd34d 1195 memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
017d8211 1196 } else if ( (time(NULL)-buf[2]) > 1800 )
8e484443 1197 fprintf(stderr,"[%s]: %s not RT %u %u %d\n",ASSETCHAINS_SYMBOL,base,buf[0],buf[1],(int32_t)(time(NULL)-buf[2]));
bffa91b2 1198 } //else fprintf(stderr,"%s size error RT\n",base);
a7ef556c 1199 fclose(fp);
bff51d67 1200 } //else fprintf(stderr,"%s open error RT\n",base);
a7ef556c 1201 }
6238a204 1202 else
a7ef556c 1203 {
1204 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
1205 if ( (fp= fopen(fname,"wb")) != 0 )
1206 {
30376a70 1207 buf[0] = (uint32_t)chainActive.Tip()->nHeight;
a7ef556c 1208 buf[1] = (uint32_t)komodo_longestchain();
1209 if ( buf[0] != 0 && buf[0] == buf[1] )
6eb41a2f 1210 {
a7ef556c 1211 buf[2] = (uint32_t)time(NULL);
582c6afe 1212 RTmask |= (1LL << baseid);
635dd34d 1213 memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
1214 if ( refid != 0 )
1215 memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0]));
6eb41a2f 1216 }
a7ef556c 1217 if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) )
1218 fprintf(stderr,"[%s] %s error writing realtime\n",ASSETCHAINS_SYMBOL,base);
1219 fclose(fp);
f789edde 1220 } else fprintf(stderr,"%s create error RT\n",base);
ecf6256c 1221 }
f1db88ea 1222 if ( sp != 0 && isrealtime == 0 )
1223 refsp->RTbufs[0][2] = 0;
52f1d788 1224 }
6238a204 1225 komodo_paxtotal();
635dd34d 1226 refsp->RTmask = RTmask;
430d3c77 1227 if ( expired == 0 && KOMODO_PASSPORT_INITDONE == 0 )
1228 {
1229 KOMODO_PASSPORT_INITDONE = 1;
1230 printf("done PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
1231 }
ab918767 1232}
ab918767 1233
This page took 0.448803 seconds and 4 git commands to generate.