]> Git Repo - VerusCoin.git/blame - src/komodo_gateway.h
test
[VerusCoin.git] / src / komodo_gateway.h
CommitLineData
9b0e1808 1/******************************************************************************
2 * Copyright © 2014-2016 The SuperNET Developers. *
3 * *
4 * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
5 * the top-level directory of this distribution for the individual copyright *
6 * holder information and the developer policies on copyright and licensing. *
7 * *
8 * Unless otherwise agreed in a custom licensing agreement, no part of the *
9 * SuperNET software, including this file may be copied, modified, propagated *
10 * or distributed except according to the terms contained in the LICENSE file *
11 * *
12 * Removal or modification of this copyright notice is prohibited. *
13 * *
14 ******************************************************************************/
15
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{
aa114a60 20 int32_t baseid; struct komodo_state *sp; int64_t netliability,maxallowed;
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;
aa114a60 31 netliability = (sp->deposited - sp->withdrawn) - sp->shorted;
32 maxallowed = komodo_maxallowed(baseid);
33 if ( netliability < maxallowed )
34 *available = (maxallowed - netliability);
35 //printf("%p %s %.8f %.8f %.8f %.8f %.8f\n",sp,base,dstr(*deposited),dstr(*issued),dstr(*withdrawn),dstr(*approved),dstr(*redeemed));
3cbfd2b4 36 return(0);
ee5e5744 37 } else printf("pax_fiatstatus cant get basesp.%s\n",base);
38 } else printf("pax_fiatstatus illegal base.%s\n",base);
3cbfd2b4 39 return(-1);
40}
41
51e5d952 42void pax_keyset(uint8_t *buf,uint256 txid,uint16_t vout,uint8_t type)
6210f328 43{
51e5d952 44 memcpy(buf,&txid,32);
45 memcpy(&buf[32],&vout,2);
46 buf[34] = type;
47}
48
49struct pax_transaction *komodo_paxfind(uint256 txid,uint16_t vout,uint8_t type)
50{
51 struct pax_transaction *pax; uint8_t buf[35];
6210f328 52 pthread_mutex_lock(&komodo_mutex);
51e5d952 53 pax_keyset(buf,txid,vout,type);
54 HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
6210f328 55 pthread_mutex_unlock(&komodo_mutex);
56 return(pax);
57}
58
c5c0e527 59struct pax_transaction *komodo_paxfinds(uint256 txid,uint16_t vout)
60{
64120a71 61 struct pax_transaction *pax; int32_t i; uint8_t types[] = { 'D', 'I', 'W', 'A', 'X' };
095dce58 62 for (i=0; i<sizeof(types)/sizeof(*types); i++)
c5c0e527 63 if ( (pax= komodo_paxfind(txid,vout,types[i])) != 0 )
64 return(pax);
65 return(0);
66}
67
51e5d952 68struct pax_transaction *komodo_paxmark(int32_t height,uint256 txid,uint16_t vout,uint8_t type,int32_t mark)
6210f328 69{
51e5d952 70 struct pax_transaction *pax; uint8_t buf[35];
6210f328 71 pthread_mutex_lock(&komodo_mutex);
51e5d952 72 pax_keyset(buf,txid,vout,type);
73 HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
2ab43fd2 74 if ( pax == 0 )
75 {
76 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
77 pax->txid = txid;
78 pax->vout = vout;
51e5d952 79 pax->type = type;
80 memcpy(pax->buf,buf,sizeof(pax->buf));
81 HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
32d525eb 82 //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark);
2ab43fd2 83 }
6210f328 84 if ( pax != 0 )
6210f328 85 pax->marked = mark;
6210f328 86 pthread_mutex_unlock(&komodo_mutex);
87 return(pax);
88}
429dabb5 89
57f349cb 90void komodo_paxdelete(struct pax_transaction *pax)
91{
5720de88 92 return; // breaks when out of order
57f349cb 93 pthread_mutex_lock(&komodo_mutex);
94 HASH_DELETE(hh,PAX,pax);
95 pthread_mutex_unlock(&komodo_mutex);
96}
97
51e5d952 98void 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 99{
51e5d952 100 struct pax_transaction *pax; uint8_t buf[35]; int32_t addflag = 0; struct komodo_state *sp; char str[16],dest[16],*s;
6cd01056 101 if ( KOMODO_PAX == 0 )
102 return;
bffa91b2 103 sp = komodo_stateptr(str,dest);
d43c7a93 104 pthread_mutex_lock(&komodo_mutex);
51e5d952 105 pax_keyset(buf,txid,vout,type);
106 HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
d43c7a93 107 if ( pax == 0 )
b2d64a0f 108 {
d43c7a93 109 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
8d52488f 110 pax->txid = txid;
111 pax->vout = vout;
51e5d952 112 pax->type = type;
113 memcpy(pax->buf,buf,sizeof(pax->buf));
114 HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
7961668b 115 addflag = 1;
bff51d67 116 if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 )
1fc75a27 117 {
118 int32_t i; for (i=0; i<32; i++)
119 printf("%02x",((uint8_t *)&txid)[i]);
5ce8bf22 120 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 121 }
b2d64a0f 122 }
8d52488f 123 pthread_mutex_unlock(&komodo_mutex);
d43c7a93 124 if ( coinaddr != 0 )
125 {
126 strcpy(pax->coinaddr,coinaddr);
9ccd3f33 127 if ( value != 0 )
128 pax->komodoshis = value;
129 if ( symbol != 0 )
130 strcpy(pax->symbol,symbol);
cd26c1f3 131 if ( source != 0 )
132 strcpy(pax->source,source);
9ccd3f33 133 if ( fiatoshis != 0 )
134 pax->fiatoshis = fiatoshis;
135 if ( rmd160 != 0 )
136 memcpy(pax->rmd160,rmd160,20);
137 if ( height != 0 )
138 pax->height = height;
139 if ( otherheight != 0 )
140 pax->otherheight = otherheight;
d43c7a93 141 }
142 else
143 {
144 pax->marked = height;
bff51d67 145 //printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight);
d43c7a93 146 }
d43c7a93 147}
148
7cc6844b 149int32_t komodo_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transaction *pax)
150{
151 int32_t i,len = 0;
ddf827b2 152 if ( rwflag == 1 )
153 {
154 for (i=0; i<32; i++)
155 opretbuf[len++] = ((uint8_t *)&pax->txid)[i];
156 opretbuf[len++] = pax->vout & 0xff;
157 opretbuf[len++] = (pax->vout >> 8) & 0xff;
158 }
159 else
160 {
161 for (i=0; i<32; i++)
a6972662 162 ((uint8_t *)&pax->txid)[i] = opretbuf[len++];
bff51d67 163 //for (i=0; i<32; i++)
164 // printf("%02x",((uint8_t *)&pax->txid)[31-i]);
ddf827b2 165 pax->vout = opretbuf[len++];
166 pax->vout += ((uint32_t)opretbuf[len++] << 8);
bff51d67 167 //printf(" txid v.%d\n",pax->vout);
ddf827b2 168 }
ad66994d 169 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->komodoshis),&pax->komodoshis);
ddf827b2 170 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->fiatoshis),&pax->fiatoshis);
171 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->height),&pax->height);
172 len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->otherheight),&pax->otherheight);
173 if ( rwflag != 0 )
174 {
175 memcpy(&opretbuf[len],pax->rmd160,20), len += 20;
176 for (i=0; i<4; i++)
177 opretbuf[len++] = pax->source[i];
178 }
179 else
180 {
181 memcpy(pax->rmd160,&opretbuf[len],20), len += 20;
182 for (i=0; i<4; i++)
183 pax->source[i] = opretbuf[len++];
184 }
7cc6844b 185 return(len);
186}
187
b89ab77c 188int32_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 189{
54661780 190 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 191 if ( KOMODO_PAX == 0 )
192 return(0);
5cb9312f 193 incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20));
40f3d9cc 194 //for (i=0; i<opretlen; i++)
195 // printf("%02x",opretbuf[i]);
b3183e3e 196 //printf(" opretlen.%d (%s)\n",opretlen,base);
40f3d9cc 197 //printf(" opretlen.%d vs %d incr.%d (%d)\n",opretlen,(int32_t)(2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 2),incr,opretlen/incr);
c3152c23 198 //if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
63b289ad 199 {
947d956f 200 type = opretbuf[0];
63b289ad 201 opretbuf++, opretlen--;
e8433f7a 202 for (n=0; n<opretlen/incr; n++)
63b289ad 203 {
05d91eb2 204 if ( iskomodo != 0 )
205 {
e8433f7a 206 memset(&p,0,sizeof(p));
ffec138e 207 len += komodo_rwapproval(0,&opretbuf[len],&p);
b89ab77c 208 if ( values != 0 && srcvalues != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 && rmd160s != 0 )
1ed46112 209 {
63342b19 210 txids[n] = p.txid;
211 vouts[n] = p.vout;
c3152c23 212 values[n] = (strcmp("KMD",base) == 0) ? p.komodoshis : p.fiatoshis;
213 srcvalues[n] = (strcmp("KMD",base) == 0) ? p.fiatoshis : p.komodoshis;
a3f1fb69 214 kmdheights[n] = p.height;
215 otherheights[n] = p.otherheight;
7cc6844b 216 memcpy(&rmd160s[n * 20],p.rmd160,20);
1dbea872 217 baseids[n] = komodo_baseid(p.source);
5bcde848 218 if ( 1 )
71c9af7c 219 {
220 char coinaddr[64];
221 bitcoin_address(coinaddr,60,&rmd160s[n * 20],20);
947d956f 222 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 223 }
52e87248 224 }
05d91eb2 225 }
7cc6844b 226 else
227 {
5cb9312f 228 for (i=0; i<4; i++)
229 base[i] = opretbuf[opretlen-4+i];
7cc6844b 230 for (j=0; j<32; j++)
231 {
232 ((uint8_t *)&txids[n])[j] = opretbuf[len++];
233 //printf("%02x",((uint8_t *)&txids[n])[j]);
234 }
235 vouts[n] = opretbuf[len++];
236 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
cb3bf8df 237 baseids[n] = komodo_baseid(base);
c5c0e527 238 if ( (pax= komodo_paxfinds(txids[n],vouts[n])) != 0 )
54661780 239 {
c3152c23 240 values[n] = (strcmp("KMD",base) == 0) ? pax->komodoshis : pax->fiatoshis;
241 srcvalues[n] = (strcmp("KMD",base) == 0) ? pax->fiatoshis : pax->komodoshis;
54661780 242 kmdheights[n] = pax->height;
243 otherheights[n] = pax->otherheight;
244 memcpy(&rmd160s[n * 20],pax->rmd160,20);
245 }
7cc6844b 246 }
6d898f42 247 //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
63b289ad 248 }
249 }
250 return(n);
251}
252
865bd972 253int32_t komodo_paxcmp(char *symbol,int32_t kmdheight,uint64_t value,uint64_t checkvalue,uint64_t seed)
254{
255 int32_t ratio;
256 if ( seed == 0 && checkvalue != 0 )
257 {
258 ratio = ((value << 6) / checkvalue);
259 if ( ratio >= 63 && ratio <= 65 )
260 return(0);
261 else
262 {
263 if ( kmdheight >= 86150 )
264 printf("ht.%d ignore mismatched %s value %lld vs checkvalue %lld -> ratio.%d\n",kmdheight,symbol,(long long)value,(long long)checkvalue,ratio);
265 return(-1);
266 }
267 }
268 else if ( checkvalue != 0 )
269 {
270 ratio = ((value << 10) / checkvalue);
271 if ( ratio >= 1023 && ratio <= 1025 )
272 return(0);
273 }
274 return(value != checkvalue);
275}
276
f1db88ea 277uint64_t komodo_paxtotal()
278{
a73709a3 279 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;
6cd01056 280 if ( KOMODO_PAX == 0 )
281 return(0);
48a3cd18 282 if ( komodo_isrealtime(&ht) == 0 )
f1db88ea 283 return(0);
ca75995d 284 else
285 {
286 HASH_ITER(hh,PAX,pax,tmp)
287 {
5ccb222a 288 if ( pax->marked != 0 )
289 continue;
85779087 290 if ( pax->type == 'A' || pax->type == 'D' || pax->type == 'X' )
291 str = pax->symbol;
292 else str = pax->source;
293 basesp = komodo_stateptrget(str);
115e6a5c 294 if ( basesp != 0 && pax->didstats == 0 )
ca75995d 295 {
6e458bf3 296 if ( pax->type == 'I' && (pax2= komodo_paxfind(pax->txid,pax->vout,'D')) != 0 )
ca75995d 297 {
3cfd4837 298 if ( pax2->fiatoshis != 0 )
299 {
300 pax->komodoshis = pax2->komodoshis;
301 pax->fiatoshis = pax2->fiatoshis;
302 basesp->issued += pax->fiatoshis;
303 pax->didstats = 1;
54f87e10 304 if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 )
305 printf("########### %p issued %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,str,dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight);
c3111e7b 306 pax2->marked = pax->height;
54f87e10 307 pax->marked = pax->height;
3cfd4837 308 }
fa81b5ae 309 }
115e6a5c 310 else if ( pax->type == 'W' )
311 {
431af13e 312 //bitcoin_address(coinaddr,addrtype,rmd160,20);
1bc5fdf8 313 if ( (checktoshis= komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis)) != 0 )
314 {
db7becdc 315 if ( komodo_paxcmp(pax->source,pax->height,pax->komodoshis,checktoshis,seed) != 0 )
772c13de 316 {
1bc5fdf8 317 pax->marked = pax->height;
60cd57df 318 //printf("WITHDRAW.%s mark <- %d %.8f != %.8f\n",pax->source,pax->height,dstr(checktoshis),dstr(pax->komodoshis));
772c13de 319 }
1bc5fdf8 320 else if ( pax->validated == 0 )
321 {
d4003105 322 pax->validated = pax->komodoshis = checktoshis;
ea9639b1 323 int32_t j; for (j=0; j<32; j++)
324 printf("%02x",((uint8_t *)&pax->txid)[j]);
2405bc66 325 //if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 )
bbfaec41 326 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 327 }
328 }
115e6a5c 329 }
ca75995d 330 }
331 }
332 }
ede04b12 333 komodo_stateptr(symbol,dest);
f1db88ea 334 HASH_ITER(hh,PAX,pax,tmp)
335 {
115e6a5c 336 pax->ready = 0;
cfa1fdda 337 if ( 0 && pax->type == 'A' )
1eb7220f 338 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 339 if ( pax->marked != 0 )
340 continue;
cfa1fdda 341 if ( strcmp(symbol,pax->symbol) == 0 || pax->type == 'A' )
f1db88ea 342 {
ede04b12 343 if ( pax->marked == 0 )
344 {
345 if ( komodo_is_issuer() != 0 )
1bc5fdf8 346 {
0449e3f0 347 if ( pax->validated != 0 && pax->type == 'D' )
1bc5fdf8 348 {
349 total += pax->fiatoshis;
350 pax->ready = 1;
351 }
352 }
371a9c87 353 else if ( pax->approved != 0 && pax->type == 'A' )
992c3be5 354 {
355 if ( pax->validated != 0 )
02c36c15 356 {
992c3be5 357 total += pax->komodoshis;
02c36c15 358 pax->ready = 1;
359 }
992c3be5 360 else
361 {
362 seed = 0;
f186b8a1 363 checktoshis = komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis);
1eb7220f 364 printf("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);
0fa85cc3 365 //printf(" v%d %.8f k.%d ht.%d\n",pax->vout,dstr(pax->komodoshis),pax->height,pax->otherheight);
6e458bf3 366 if ( seed != 0 && checktoshis != 0 )
992c3be5 367 {
115e6a5c 368 if ( checktoshis == pax->komodoshis )
992c3be5 369 {
370 total += pax->komodoshis;
371 pax->validated = pax->komodoshis;
115e6a5c 372 pax->ready = 1;
992c3be5 373 } else pax->marked = pax->height;
374 }
375 }
376 }
fc40c343 377 if ( 0 && pax->ready != 0 )
cfa1fdda 378 printf("%p (%c) pax.%s marked.%d %.8f -> %.8f validated.%d approved.%d\n",pax,pax->type,pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->validated != 0,pax->approved != 0);
ede04b12 379 }
f1db88ea 380 }
381 }
5481c677 382 //printf("paxtotal %.8f\n",dstr(total));
f1db88ea 383 return(total);
384}
385
fa1a55cd 386static int _paxorder(const void *a,const void *b)
f360ea0a 387{
fa1a55cd 388#define pax_a (*(struct pax_transaction **)a)
389#define pax_b (*(struct pax_transaction **)b)
390 uint64_t aval,bval;
d936b97f 391 aval = pax_a->fiatoshis + pax_a->komodoshis + pax_a->height;
392 bval = pax_b->fiatoshis + pax_b->komodoshis + pax_b->height;
fa1a55cd 393 if ( bval > aval )
394 return(-1);
395 else if ( bval < aval )
396 return(1);
397 return(0);
398#undef pax_a
399#undef pax_b
400}
401
402int32_t komodo_pending_withdraws(char *opretstr) // todo: enforce deterministic order
403{
6b3eb2c4 404 struct pax_transaction *pax,*pax2,*tmp,*paxes[64]; uint8_t opretbuf[16384]; int32_t i,n,ht,len=0; uint64_t total = 0;
6cd01056 405 if ( KOMODO_PAX == 0 )
406 return(0);
f360ea0a 407 if ( komodo_isrealtime(&ht) == 0 || ASSETCHAINS_SYMBOL[0] != 0 )
408 return(0);
fa1a55cd 409 n = 0;
f360ea0a 410 HASH_ITER(hh,PAX,pax,tmp)
411 {
f1e6a937 412 if ( pax->type == 'W' )
f360ea0a 413 {
6b3eb2c4 414 if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'A')) != 0 )
415 {
416 if ( pax2->approved != 0 )
417 pax->approved = pax2->approved;
418 }
419 else if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'X')) != 0 )
0b262b65 420 pax->approved = pax->height;
6b3eb2c4 421 //printf("pax %s marked.%u approved.%u validated.%llu\n",pax->symbol,pax->marked,pax->approved,(long long)pax->validated);
f1e6a937 422 if ( pax->marked == 0 && strcmp((char *)"KMD",pax->symbol) == 0 && pax->approved == 0 && pax->validated != 0 )
423 {
fa1a55cd 424 if ( n < sizeof(paxes)/sizeof(*paxes) )
ac4eb454 425 {
fa1a55cd 426 paxes[n++] = pax;
ac4eb454 427 int32_t j; for (j=0; j<32; j++)
428 printf("%02x",((uint8_t *)&pax->txid)[j]);
2fb657f6 429 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 430 }
f1e6a937 431 }
f360ea0a 432 }
433 }
fa1a55cd 434 opretstr[0] = 0;
435 if ( n > 0 )
436 {
437 opretbuf[len++] = 'A';
438 qsort(paxes,n,sizeof(*paxes),_paxorder);
439 for (i=0; i<n; i++)
440 {
441 if ( len < (sizeof(opretbuf)>>3)*7 )
442 len += komodo_rwapproval(1,&opretbuf[len],paxes[i]);
443 }
444 if ( len > 0 )
445 init_hexbytes_noT(opretstr,opretbuf,len);
446 }
132aea8c 447 //fprintf(stderr,"komodo_pending_withdraws len.%d PAXTOTAL %.8f\n",len,dstr(komodo_paxtotal()));
f360ea0a 448 return(len);
449}
a4c67285 450
635dd34d 451int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo)
429dabb5 452{
1c05f0b5 453 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;
6cd01056 454 if ( KOMODO_PAX == 0 )
455 return(0);
76d59c1d 456 struct komodo_state *kmdsp = komodo_stateptrget((char *)"KMD");
635dd34d 457 sp = komodo_stateptr(symbol,dest);
27bf3c5e 458 strcpy(symbol,base);
b27b2877 459 if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 )
716fb382 460 return(0);
429dabb5 461 PENDING_KOMODO_TX = 0;
a3f23ee2 462 if ( tokomodo == 0 )
48e96ba1 463 {
77be6cd9 464 opcode = 'I';
48a3cd18 465 if ( komodo_isrealtime(&ht) == 0 )
48e96ba1 466 return(0);
57f349cb 467 }
468 else
469 {
470 opcode = 'X';
57f349cb 471 if ( komodo_paxtotal() == 0 )
472 return(0);
473 }
c169f64b 474 HASH_ITER(hh,PAX,pax,tmp)
429dabb5 475 {
aa114a60 476 {
477#ifdef KOMODO_ASSETCHAINS_WAITNOTARIZE
5334d5ad 478 if ( kmdsp != 0 && kmdsp->NOTARIZED_HEIGHT >= pax->height ) // assumes same chain as notarize
8bd946ce 479 pax->validated = pax->komodoshis; //kmdsp->NOTARIZED_HEIGHT;
fc40c343 480 else pax->validated = pax->ready = 0;
aa114a60 481#endif
482 }
25d878f4 483 if ( ASSETCHAINS_SYMBOL[0] != 0 && (pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis) )
1c05f0b5 484 {
7ad9ed4d 485 if ( strcmp(ASSETCHAINS_SYMBOL,symbol) == 0 )
486 printf("miner.[%s]: skip %s %.8f when avail %.8f\n",ASSETCHAINS_SYMBOL,symbol,dstr(pax->fiatoshis),dstr(available));
1c05f0b5 487 continue;
488 }
274dd7a5 489 /*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 490 if ( pax->marked != 0 || (pax->type != 'D' && pax->type != 'A') || pax->ready == 0 )
a06196fe 491 {
492 printf("reject 2\n");
e5430f52 493 continue;
274dd7a5 494 }*/
495 if ( ASSETCHAINS_SYMBOL[0] != 0 && (strcmp(pax->symbol,symbol) != 0 || pax->validated == 0) )
8bd946ce 496 {
54ef0b1a 497 printf("pax->symbol.%s != %s or null pax->validated %.8f\n",pax->symbol,symbol,dstr(pax->validated));
8bd946ce 498 continue;
499 }
fc40c343 500 if ( pax->ready == 0 )
501 continue;
582c6afe 502 if ( pax->type == 'A' && ASSETCHAINS_SYMBOL[0] == 0 )
503 {
76d59c1d 504 if ( kmdsp != 0 )
582c6afe 505 {
506 if ( (baseid= komodo_baseid(pax->symbol)) < 0 || ((1LL << baseid) & sp->RTmask) == 0 )
507 {
508 printf("not RT for (%s) %llx baseid.%d %llx\n",pax->symbol,(long long)sp->RTmask,baseid,(long long)(1LL<<baseid));
509 continue;
510 }
76d59c1d 511 } else continue;
582c6afe 512 }
513
5481c677 514 //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 515 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
635dd34d 516 printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
e5430f52 517 txNew->vout.resize(numvouts+1);
1fc75a27 518 txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis;
e5430f52 519 txNew->vout[numvouts].scriptPubKey.resize(25);
520 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
521 *script++ = 0x76;
522 *script++ = 0xa9;
523 *script++ = 20;
524 memcpy(script,pax->rmd160,20), script += 20;
525 *script++ = 0x88;
526 *script++ = 0xac;
a3f23ee2 527 if ( tokomodo == 0 )
16bed6ac 528 {
529 for (i=0; i<32; i++)
16bed6ac 530 data[len++] = ((uint8_t *)&pax->txid)[i];
16bed6ac 531 data[len++] = pax->vout & 0xff;
532 data[len++] = (pax->vout >> 8) & 0xff;
e5430f52 533 PENDING_KOMODO_TX += pax->fiatoshis;
16bed6ac 534 }
fbc4209b 535 else
536 {
e0357628 537 len += komodo_rwapproval(1,&data[len],pax);
fbc4209b 538 PENDING_KOMODO_TX += pax->komodoshis;
cefb14cd 539 printf(" vout.%u DEPOSIT %.8f <- pax.%s pending %.8f | ",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,dstr(PENDING_KOMODO_TX));
fbc4209b 540 }
e5430f52 541 if ( numvouts++ >= 64 )
542 break;
429dabb5 543 }
544 if ( numvouts > 1 )
545 {
0e3472c1 546 if ( tokomodo != 0 )
27bf3c5e 547 strcpy(symbol,(char *)"KMD");
9ef47e29 548 for (i=0; symbol[i]!=0; i++)
549 data[len++] = symbol[i];
a5ad8f02 550 data[len++] = 0;
77be6cd9 551 opretlen = komodo_opreturnscript(opret,opcode,data,len);
50824530 552 txNew->vout.resize(numvouts+1);
553 txNew->vout[numvouts].nValue = 0;
554 txNew->vout[numvouts].scriptPubKey.resize(opretlen);
555 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
429dabb5 556 memcpy(script,opret,opretlen);
23d955d4 557 printf("MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen);
d9a9d562 558 return(1);
9ef47e29 559 }
d9a9d562 560 return(0);
429dabb5 561}
562
8f64a3a9 563int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
557d9a23 564{
1c05f0b5 565 int32_t i,j,n,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 available,deposited,issued,withdrawn,approved,redeemed; int64_t values[64],srcvalues[64]; struct pax_transaction *pax;
6cd01056 566 if ( KOMODO_PAX == 0 )
567 return(0);
1ed46112 568 memset(baseids,0xff,sizeof(baseids));
569 memset(values,0,sizeof(values));
b89ab77c 570 memset(srcvalues,0,sizeof(srcvalues));
f360ea0a 571 memset(rmd160s,0,sizeof(rmd160s));
1ed46112 572 memset(kmdheights,0,sizeof(kmdheights));
573 memset(otherheights,0,sizeof(otherheights));
557d9a23 574 n = block.vtx[0].vout.size();
575 script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
12d47153 576 if ( n <= 2 || script[0] != 0x6a )
577 return(0);
578 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
77be6cd9 579 if ( ASSETCHAINS_SYMBOL[0] == 0 )
580 {
85de2861 581 //for (i=0; i<opretlen; i++)
582 // printf("%02x",script[i]);
583 //printf(" height.%d checkdeposit n.%d [%02x] [%c] %d vs %d\n",height,n,script[0],script[offset],script[offset],'X');
77be6cd9 584 opcode = 'X';
09a235e1 585 strcpy(symbol,(char *)"KMD");
77be6cd9 586 }
587 else
588 {
589 strcpy(symbol,ASSETCHAINS_SYMBOL);
590 opcode = 'I';
716fb382 591 if ( komodo_baseid(symbol) < 0 )
592 {
593 if ( block.vtx[0].vout.size() != 1 )
594 {
595 printf("%s has more than one coinbase?\n",symbol);
596 return(-1);
597 }
598 return(0);
599 }
77be6cd9 600 }
601 if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
557d9a23 602 {
b89ab77c 603 if ( (num= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,&script[offset],opretlen,opcode == 'X')) > 0 )
557d9a23 604 {
605 for (i=1; i<n-1; i++)
606 {
1bc5fdf8 607 if ( (pax= komodo_paxfinds(txids[i-1],vouts[i-1])) != 0 ) // finds... make sure right one
5a2c5b6f 608 {
ca75995d 609 pax->type = opcode;
1c05f0b5 610 if ( opcode == 'I' && pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis )
611 {
612 printf("checkdeposit: skip %s %.8f when avail %.8f\n",pax->symbol,dstr(pax->fiatoshis),dstr(available));
613 continue;
614 }
784c0112 615 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 616 {
02c36c15 617 if ( pax->marked != 0 && height >= 80820 )
73585700 618 {
02c36c15 619 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);
620 errs++;
621 } else matched++;
f10b8e2e 622 }
623 else
624 {
1c05f0b5 625 for (j=0; j<32; j++)
626 printf("%02x",((uint8_t *)&txids[i-1])[j]);
627 printf(" cant paxfind %c txid\n",opcode);
1bc5fdf8 628 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 629 }
5a2c5b6f 630 }
cb040aa1 631 else
632 {
1c05f0b5 633 hash = block.GetHash();
634 for (j=0; j<32; j++)
635 printf("%02x",((uint8_t *)&hash)[j]);
7ee875e8 636 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 637 }
557d9a23 638 }
81041ab2 639 if ( matched != num )
640 {
9bd14f97 641 printf("WOULD REJECT %s: ht.%d (%c) matched.%d vs num.%d\n",symbol,height,opcode,matched,num);
0dca8d4c 642 // can easily happen depending on order of loading
9b570fb1 643 if ( height > 150000 )
52b2e68d 644 {
645 printf("REJECT: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num);
646 return(-1);
647 }
81041ab2 648 }
557d9a23 649 }
b98053e2 650 //printf("opretlen.%d num.%d\n",opretlen,num);
557d9a23 651 }
652 return(0);
653}
654
cd26c1f3 655const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source)
429dabb5 656{
7ddc4ef7 657 uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33]; int32_t didstats,i,j,n,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 fiatoshis,komodoshis,checktoshis,values[64],srcvalues[64]; struct pax_transaction *pax,*pax2; struct komodo_state *basesp; double diff;
1ed46112 658 const char *typestr = "unknown";
6cd01056 659 if ( KOMODO_PAX == 0 )
660 return("nopax");
b27b2877 661 if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 )
894c570b 662 {
b27b2877 663 //printf("komodo_opreturn skip %s\n",ASSETCHAINS_SYMBOL);
716fb382 664 return("assetchain");
894c570b 665 }
1ed46112 666 memset(baseids,0xff,sizeof(baseids));
667 memset(values,0,sizeof(values));
06cfcf31 668 memset(srcvalues,0,sizeof(srcvalues));
f360ea0a 669 memset(rmd160s,0,sizeof(rmd160s));
1ed46112 670 memset(kmdheights,0,sizeof(kmdheights));
671 memset(otherheights,0,sizeof(otherheights));
9596e284 672 tokomodo = (komodo_is_issuer() == 0);
0a611c1e 673 if ( opretbuf[0] == 'D' )
64bb0834 674 {
9596e284 675 tokomodo = 0;
05a37d87 676 if ( opretlen == 38 ) // any KMD tx
64bb0834 677 {
05a37d87 678 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
abdd8672 679 memset(base,0,sizeof(base));
680 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
abdd8672 681 bitcoin_address(coinaddr,addrtype,rmd160,20);
682 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
683 typestr = "deposit";
05a37d87 684 if ( kmdheight <= height )
6e576848 685 {
f5e3cdfa 686 didstats = 0;
d4003105 687 if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
8f261c33 688 {
689 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);
690 for (i=0; i<32; i++)
691 printf("%02x",((uint8_t *)&txid)[i]);
692 printf(" <- txid.v%u ",vout);
693 for (i=0; i<33; i++)
694 printf("%02x",pubkey33[i]);
695 printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
696 }
331a6188 697 if ( komodo_paxcmp(base,kmdheight,value,checktoshis,seed) == 0 )
05a37d87 698 {
b915be35 699 if ( (pax= komodo_paxfind(txid,vout,'D')) == 0 )
05a37d87 700 {
c98b3a08 701 if ( (basesp= komodo_stateptrget(base)) != 0 )
05a37d87 702 {
c98b3a08 703 basesp->deposited += fiatoshis;
22a31d52 704 didstats = 1;
bff51d67 705 if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
0398360f 706 printf("########### %p deposited %s += %.8f kmdheight.%d %.8f\n",basesp,base,dstr(fiatoshis),kmdheight,dstr(value));
c72ee808 707 } else printf("cant get stateptr.(%s)\n",base);
4d4194d3 708 komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,'D',kmdheight,height,(char *)"KMD",0);
3f9cad6d 709 }
51e5d952 710 if ( (pax= komodo_paxfind(txid,vout,'D')) != 0 )
3f9cad6d 711 {
8bd946ce 712 pax->height = kmdheight;
713 pax->validated = value;
88dff0d3 714 pax->komodoshis = value;
715 pax->fiatoshis = fiatoshis;
22a31d52 716 if ( didstats == 0 && pax->didstats == 0 )
3f9cad6d 717 {
ca75995d 718 if ( (basesp= komodo_stateptrget(base)) != 0 )
719 {
720 basesp->deposited += fiatoshis;
22a31d52 721 didstats = 1;
bff51d67 722 if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
36b8ed99 723 printf("########### %p deposited %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 724 }
3f9cad6d 725 }
51e5d952 726 if ( didstats != 0 )
727 pax->didstats = 1;
7ddc4ef7 728 if ( (pax2= komodo_paxfind(txid,vout,'I')) != 0 )
729 {
730 pax2->fiatoshis = pax->fiatoshis;
731 pax2->komodoshis = pax->komodoshis;
7ddc4ef7 732 pax->marked = pax2->marked = pax->height;
da02b3be 733 pax2->height = pax->height = height;
25623352 734 if ( pax2->didstats == 0 )
735 {
bde7e5ef 736 if ( (basesp= komodo_stateptrget(base)) != 0 )
737 {
738 basesp->issued += pax2->fiatoshis;
739 pax2->didstats = 1;
740 if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
36b8ed99 741 printf("########### %p issueda %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,base,dstr(pax2->fiatoshis),pax2->height,dstr(pax2->komodoshis),pax2->otherheight);
bde7e5ef 742 }
25623352 743 }
7ddc4ef7 744 }
3f9cad6d 745 }
eb928486 746 }
747 else if ( kmdheight > 91800 )
8f261c33 748 printf("pax %s deposit %.8f rejected kmdheight.%d %.8f KMD\n",base,dstr(fiatoshis),kmdheight,dstr(value));
8bc1bcad 749 }
64bb0834 750 }
751 }
36b8ed99 752 else if ( opretbuf[0] == 'I' )
753 {
754 tokomodo = 0;
755 if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
756 {
757 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 )
758 {
759 for (i=0; i<n; i++)
760 {
761 if ( baseids[i] < 0 )
762 {
763 printf("%d of %d illegal baseid.%d\n",i,n,baseids[i]);
764 continue;
765 }
766 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
767 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'I',height,0,CURRENCIES[baseids[i]],0);
768 komodo_paxmark(height,txids[i],vouts[i],'I',height);
769 if ( (pax= komodo_paxfind(txids[i],vouts[i],'I')) != 0 )
770 {
771 pax->type = opretbuf[0];
772 strcpy(pax->source,(char *)&opretbuf[opretlen-4]);
a6a33512 773 if ( (pax2= komodo_paxfind(txids[i],vouts[i],'D')) != 0 && pax2->fiatoshis != 0 && pax2->komodoshis != 0 )
36b8ed99 774 {
775 // realtime path?
776 pax->fiatoshis = pax2->fiatoshis;
777 pax->komodoshis = pax2->komodoshis;
778 pax->marked = pax2->marked = pax2->height;
779 if ( pax->didstats == 0 )
780 {
781 if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
782 {
783 basesp->issued += pax->fiatoshis;
784 pax->didstats = 1;
39863e30 785 pax->height = pax2->height;
786 pax->otherheight = height;
36b8ed99 787 if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
788 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);
789 }
790 }
791 }
792 }
57f349cb 793 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'I',height)) != 0 )
794 komodo_paxdelete(pax);
795 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'D',height)) != 0 )
796 komodo_paxdelete(pax);
36b8ed99 797 }
798 } else printf("opreturn none issued?\n");
799 }
36b8ed99 800 }
a3608c89 801 else if ( opretbuf[0] == 'W' )//&& opretlen >= 38 )
01d9ed2f 802 {
9596e284 803 tokomodo = 1;
01d9ed2f 804 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
805 memset(base,0,sizeof(base));
c3d94aec 806 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&komodoshis);
01d9ed2f 807 bitcoin_address(coinaddr,addrtype,rmd160,20);
c3d94aec 808 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,value);
01d9ed2f 809 typestr = "withdraw";
2405bc66 810 if ( strcmp(base,"RUB") == 0 )
4718923d 811 printf("RUB WITHDRAW %s.height.%d vs height.%d check %.8f/%.8f vs %.8f tokomodo.%d %d seed.%llx -> (%s) len.%d\n",ASSETCHAINS_SYMBOL,kmdheight,height,dstr(checktoshis),dstr(komodoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed,coinaddr,opretlen);
f5e3cdfa 812 didstats = 0;
a39d7b21 813 //if ( komodo_paxcmp(base,kmdheight,komodoshis,checktoshis,seed) == 0 )
01d9ed2f 814 {
51e5d952 815 if ( value != 0 && ((pax= komodo_paxfind(txid,vout,'W')) == 0 || pax->didstats == 0) )
0b556992 816 {
0126d891 817 if ( (basesp= komodo_stateptrget(base)) != 0 )
818 {
819 basesp->withdrawn += value;
820 didstats = 1;
2405bc66 821 if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 )
5b489b55 822 printf("########### %p withdrawn %s += %.8f check %.8f\n",basesp,base,dstr(value),dstr(checktoshis));
0126d891 823 }
57f349cb 824 if ( 0 && strcmp(base,"RUB") == 0 && (pax == 0 || pax->approved == 0) )
4718923d 825 printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height);
ea365a71 826 }
115e6a5c 827 komodo_gateway_deposit(coinaddr,0,(char *)"KMD",value,rmd160,txid,vout,'W',kmdheight,height,source,0);
51e5d952 828 if ( (pax= komodo_paxfind(txid,vout,'W')) != 0 )
daeecf95 829 {
830 pax->type = opretbuf[0];
1ac3f9ef 831 strcpy(pax->source,base);
832 strcpy(pax->symbol,"KMD");
daeecf95 833 pax->height = kmdheight;
834 pax->otherheight = height;
1e22f865 835 pax->komodoshis = komodoshis;
daeecf95 836 }
a39d7b21 837 } // else printf("withdraw %s paxcmp ht.%d %d error value %.8f -> %.8f vs %.8f\n",base,kmdheight,height,dstr(value),dstr(komodoshis),dstr(checktoshis));
838 // need to allocate pax
a4c67285 839 }
840 else if ( tokomodo != 0 && opretbuf[0] == 'A' )
841 {
9596e284 842 tokomodo = 1;
d515094d 843 //if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
826bfe51 844 {
845 for (i=0; i<opretlen; i++)
846 printf("%02x",opretbuf[i]);
40f3d9cc 847 printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f opretlen.%d\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()),opretlen);
826bfe51 848 }
b89ab77c 849 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
a4c67285 850 {
851 for (i=0; i<n; i++)
852 {
40f3d9cc 853 for (j=0; j<32; j++)
854 printf("%02x",((uint8_t *)&txids[i])[j]);
855 printf(" v%d %.8f k.%d ht.%d base.%d\n",vouts[i],dstr(values[i]),kmdheights[i],otherheights[i],baseids[i]);
b89ab77c 856 if ( baseids[i] < 0 )
8e7b40d9 857 {
858 printf("baseids[%d] %d\n",i,baseids[i]);
b89ab77c 859 continue;
8e7b40d9 860 }
f5e3cdfa 861 didstats = 0;
0cc6ad81 862 seed = 0;
d515094d 863 checktoshis = komodo_paxprice(&seed,kmdheights[i],CURRENCIES[baseids[i]],(char *)"KMD",(uint64_t)values[i]);
2fb657f6 864 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 865 if ( srcvalues[i] == checktoshis )
a4c67285 866 {
36a4e92c 867 if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 )
0fa85cc3 868 {
371a9c87 869 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
5481c677 870 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 871 if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 )
872 printf("unexpected null pax for approve\n");
873 else pax->validated = checktoshis;
874 if ( (pax2= komodo_paxfind(txids[i],vouts[i],'W')) != 0 )
875 pax2->approved = kmdheights[i];
7aadf9a9 876 komodo_paxmark(height,txids[i],vouts[i],'W',height);
371a9c87 877 //komodo_paxmark(height,txids[i],vouts[i],'A',height);
878 if ( srcvalues[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
879 {
5481c677 880 basesp->approved += values[i];
371a9c87 881 didstats = 1;
882 //if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
ac4eb454 883 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 884 }
885 //printf(" i.%d (%s) <- %.8f ADDFLAG APPROVED\n",i,coinaddr,dstr(values[i]));
0fa85cc3 886 }
371a9c87 887 else if ( pax->didstats == 0 && srcvalues[i] != 0 )
3f9cad6d 888 {
371a9c87 889 if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
890 {
5481c677 891 basesp->approved += values[i];
371a9c87 892 didstats = 1;
893 //if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
4d5d0574 894 printf("pax.%p ########### %p approved %s += %.8f -> %.8f/%.8f\n",pax,basesp,CURRENCIES[baseids[i]],dstr(values[i]),dstr(srcvalues[i]),dstr(checktoshis));
371a9c87 895 }
896 } //else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]);
897 if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) != 0 )
898 {
899 pax->type = opretbuf[0];
900 pax->approved = kmdheights[i];
901 pax->validated = checktoshis;
902 if ( didstats != 0 )
903 pax->didstats = 1;
7ad9ed4d 904 //if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
5481c677 905 //printf(" i.%d approved.%d <<<<<<<<<<<<< APPROVED %p\n",i,kmdheights[i],pax);
371a9c87 906 }
06d1a655 907 }
ea365a71 908 }
5cb9312f 909 } else printf("n.%d from opreturns\n",n);
bff51d67 910 //printf("extra.[%d] after %.8f\n",n,dstr(komodo_paxtotal()));
01d9ed2f 911 }
ca3c6549 912 else if ( opretbuf[0] == 'X' )
913 {
9596e284 914 tokomodo = 1;
4c58b338 915 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
916 {
917 for (i=0; i<n; i++)
918 {
3f9cad6d 919 if ( baseids[i] < 0 )
920 continue;
4c58b338 921 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
51e5d952 922 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'X',height,0,(char *)"KMD",0);
aa114a60 923 komodo_paxmark(height,txids[i],vouts[i],'W',height);
924 komodo_paxmark(height,txids[i],vouts[i],'A',height);
925 komodo_paxmark(height,txids[i],vouts[i],'X',height);
51e5d952 926 if ( (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 )
ca75995d 927 {
928 pax->type = opretbuf[0];
5481c677 929 if ( baseids[i] >= 0 && values[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
c6eca157 930 {
5481c677 931 basesp->redeemed += values[i];
546672f5 932 pax->didstats = 1;
57f349cb 933 //if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 )
4d5d0574 934 printf("########### %p redeemed %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(values[i]));
c6eca157 935 }
ca75995d 936 }
57f349cb 937 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'W',height)) != 0 )
938 komodo_paxdelete(pax);
939 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'A',height)) != 0 )
940 komodo_paxdelete(pax);
941 if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'X',height)) != 0 )
942 komodo_paxdelete(pax);
4c58b338 943 }
dff4ef45 944 } //else printf("komodo_issued_opreturn returned %d\n",n);
ca3c6549 945 }
64bb0834 946 return(typestr);
947}
948
30d79208 949void komodo_passport_iteration()
ab918767 950{
368c61bc 951 static long lastpos[34]; static char userpass[33][1024];
635dd34d 952 FILE *fp; int32_t baseid,isrealtime,refid,blocks,longest; struct komodo_state *sp,*refsp; char *retstr,fname[512],*base,symbol[16],dest[16]; uint32_t buf[3]; cJSON *infoobj,*result; uint64_t RTmask = 0;
2a15e8ea 953 //printf("PASSPORT.(%s)\n",ASSETCHAINS_SYMBOL);
17899df4 954 while ( KOMODO_INITDONE == 0 )
955 {
e40060c2 956 fprintf(stderr,"[%s] PASSPORT iteration waiting for KOMODO_INITDONE\n",ASSETCHAINS_SYMBOL);
17899df4 957 sleep(3);
958 }
635dd34d 959 refsp = komodo_stateptr(symbol,dest);
ab918767 960 if ( ASSETCHAINS_SYMBOL[0] == 0 )
67cd296f 961 refid = 33;
b307682f 962 else
716fb382 963 {
b307682f 964 refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0
965 if ( refid == 0 )
966 {
967 KOMODO_PASSPORT_INITDONE = 1;
968 return;
969 }
716fb382 970 }
6cd01056 971 if ( KOMODO_PAX == 0 )
972 {
973 KOMODO_PASSPORT_INITDONE = 1;
974 return;
975 }
49058e97 976 //printf("PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
959782f9 977 for (baseid=32; baseid>=0; baseid--)
508b0d3c 978 {
ecf6256c 979 sp = 0;
980 isrealtime = 0;
f789edde 981 base = (char *)CURRENCIES[baseid];
95a6016d 982 if ( baseid+1 != refid )
ab918767 983 {
055db9b6 984 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate");
52f1d788 985 komodo_nameset(symbol,dest,base);
22b09834 986 sp = komodo_stateptrget(symbol);
987 if ( (fp= fopen(fname,"rb")) != 0 && sp != 0 )
ab918767 988 {
52f1d788 989 fseek(fp,0,SEEK_END);
990 if ( ftell(fp) > lastpos[baseid] )
ab918767 991 {
894c570b 992 if ( 0 && lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 )
9a6744f2 993 printf("passport refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base);
52f1d788 994 fseek(fp,lastpos[baseid],SEEK_SET);
9534c87a 995 while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
52f1d788 996 ;
997 lastpos[baseid] = ftell(fp);
894c570b 998 if ( 0 && lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 )
9a6744f2 999 printf("from.(%s) lastpos[%s] %ld\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid]);
49058e97 1000 } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp));
52f1d788 1001 fclose(fp);
14e3fb45 1002 } else printf("error.(%s) %p\n",fname,sp);
a7ef556c 1003 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
1004 if ( (fp= fopen(fname,"rb")) != 0 )
1005 {
1006 if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) )
1007 {
f1db88ea 1008 sp->CURRENT_HEIGHT = buf[0];
fe0210c0 1009 if ( buf[0] != 0 && buf[0] == buf[1] && buf[2] > time(NULL)-60 )
6eb41a2f 1010 {
a7ef556c 1011 isrealtime = 1;
6eb41a2f 1012 RTmask |= (1LL << baseid);
635dd34d 1013 memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
140fccf8 1014 } //else fprintf(stderr,"%s not RT\n",base);
bffa91b2 1015 } //else fprintf(stderr,"%s size error RT\n",base);
a7ef556c 1016 fclose(fp);
bff51d67 1017 } //else fprintf(stderr,"%s open error RT\n",base);
a7ef556c 1018 }
6238a204 1019 else
a7ef556c 1020 {
1021 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
1022 if ( (fp= fopen(fname,"wb")) != 0 )
1023 {
30376a70 1024 buf[0] = (uint32_t)chainActive.Tip()->nHeight;
a7ef556c 1025 buf[1] = (uint32_t)komodo_longestchain();
1026 if ( buf[0] != 0 && buf[0] == buf[1] )
6eb41a2f 1027 {
a7ef556c 1028 buf[2] = (uint32_t)time(NULL);
582c6afe 1029 RTmask |= (1LL << baseid);
635dd34d 1030 memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
1031 if ( refid != 0 )
1032 memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0]));
6eb41a2f 1033 }
a7ef556c 1034 if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) )
1035 fprintf(stderr,"[%s] %s error writing realtime\n",ASSETCHAINS_SYMBOL,base);
1036 fclose(fp);
f789edde 1037 } else fprintf(stderr,"%s create error RT\n",base);
ecf6256c 1038 }
f1db88ea 1039 if ( sp != 0 && isrealtime == 0 )
1040 refsp->RTbufs[0][2] = 0;
52f1d788 1041 }
6238a204 1042 komodo_paxtotal();
635dd34d 1043 refsp->RTmask = RTmask;
4767f1fc 1044 KOMODO_PASSPORT_INITDONE = 1;
2a15e8ea 1045 //printf("done PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
ab918767 1046}
ab918767 1047
This page took 0.345505 seconds and 4 git commands to generate.