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