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