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