| 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 | |
| 16 | // paxdeposit equivalent in reverse makes opreturn and KMD does the same in reverse |
| 17 | |
| 18 | int32_t pax_fiatstatus(uint64_t *available,uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,uint64_t *approved,uint64_t *redeemed,char *base) |
| 19 | { |
| 20 | int32_t baseid; struct komodo_state *sp; int64_t netliability,maxallowed; |
| 21 | *available = *deposited = *issued = *withdrawn = *approved = *redeemed = 0; |
| 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; |
| 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)); |
| 36 | return(0); |
| 37 | } else printf("pax_fiatstatus cant get basesp.%s\n",base); |
| 38 | } else printf("pax_fiatstatus illegal base.%s\n",base); |
| 39 | return(-1); |
| 40 | } |
| 41 | |
| 42 | void pax_keyset(uint8_t *buf,uint256 txid,uint16_t vout,uint8_t type) |
| 43 | { |
| 44 | memcpy(buf,&txid,32); |
| 45 | memcpy(&buf[32],&vout,2); |
| 46 | buf[34] = type; |
| 47 | } |
| 48 | |
| 49 | struct pax_transaction *komodo_paxfind(uint256 txid,uint16_t vout,uint8_t type) |
| 50 | { |
| 51 | struct pax_transaction *pax; uint8_t buf[35]; |
| 52 | pthread_mutex_lock(&komodo_mutex); |
| 53 | pax_keyset(buf,txid,vout,type); |
| 54 | HASH_FIND(hh,PAX,buf,sizeof(buf),pax); |
| 55 | pthread_mutex_unlock(&komodo_mutex); |
| 56 | return(pax); |
| 57 | } |
| 58 | |
| 59 | struct pax_transaction *komodo_paxfinds(uint256 txid,uint16_t vout) |
| 60 | { |
| 61 | struct pax_transaction *pax; int32_t i; uint8_t types[] = { 'D', 'I', 'W', 'A', 'X' }; |
| 62 | for (i=0; i<sizeof(types)/sizeof(*types); i++) |
| 63 | if ( (pax= komodo_paxfind(txid,vout,types[i])) != 0 ) |
| 64 | return(pax); |
| 65 | return(0); |
| 66 | } |
| 67 | |
| 68 | struct pax_transaction *komodo_paxmark(int32_t height,uint256 txid,uint16_t vout,uint8_t type,int32_t mark) |
| 69 | { |
| 70 | struct pax_transaction *pax; uint8_t buf[35]; |
| 71 | pthread_mutex_lock(&komodo_mutex); |
| 72 | pax_keyset(buf,txid,vout,type); |
| 73 | HASH_FIND(hh,PAX,buf,sizeof(buf),pax); |
| 74 | if ( pax == 0 ) |
| 75 | { |
| 76 | pax = (struct pax_transaction *)calloc(1,sizeof(*pax)); |
| 77 | pax->txid = txid; |
| 78 | pax->vout = vout; |
| 79 | pax->type = type; |
| 80 | memcpy(pax->buf,buf,sizeof(pax->buf)); |
| 81 | HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax); |
| 82 | //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark); |
| 83 | } |
| 84 | if ( pax != 0 ) |
| 85 | pax->marked = mark; |
| 86 | pthread_mutex_unlock(&komodo_mutex); |
| 87 | return(pax); |
| 88 | } |
| 89 | |
| 90 | void 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 |
| 91 | { |
| 92 | struct pax_transaction *pax; uint8_t buf[35]; int32_t addflag = 0; struct komodo_state *sp; char str[16],dest[16],*s; |
| 93 | sp = komodo_stateptr(str,dest); |
| 94 | pthread_mutex_lock(&komodo_mutex); |
| 95 | pax_keyset(buf,txid,vout,type); |
| 96 | HASH_FIND(hh,PAX,buf,sizeof(buf),pax); |
| 97 | if ( pax == 0 ) |
| 98 | { |
| 99 | pax = (struct pax_transaction *)calloc(1,sizeof(*pax)); |
| 100 | pax->txid = txid; |
| 101 | pax->vout = vout; |
| 102 | pax->type = type; |
| 103 | memcpy(pax->buf,buf,sizeof(pax->buf)); |
| 104 | HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax); |
| 105 | addflag = 1; |
| 106 | if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 ) |
| 107 | { |
| 108 | int32_t i; for (i=0; i<32; i++) |
| 109 | printf("%02x",((uint8_t *)&txid)[i]); |
| 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); |
| 111 | } |
| 112 | } |
| 113 | pthread_mutex_unlock(&komodo_mutex); |
| 114 | if ( coinaddr != 0 ) |
| 115 | { |
| 116 | strcpy(pax->coinaddr,coinaddr); |
| 117 | if ( value != 0 ) |
| 118 | pax->komodoshis = value; |
| 119 | if ( symbol != 0 ) |
| 120 | strcpy(pax->symbol,symbol); |
| 121 | if ( source != 0 ) |
| 122 | strcpy(pax->source,source); |
| 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; |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | pax->marked = height; |
| 135 | //printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | int32_t komodo_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transaction *pax) |
| 140 | { |
| 141 | int32_t i,len = 0; |
| 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++) |
| 152 | ((uint8_t *)&pax->txid)[i] = opretbuf[len++]; |
| 153 | //for (i=0; i<32; i++) |
| 154 | // printf("%02x",((uint8_t *)&pax->txid)[31-i]); |
| 155 | pax->vout = opretbuf[len++]; |
| 156 | pax->vout += ((uint32_t)opretbuf[len++] << 8); |
| 157 | //printf(" txid v.%d\n",pax->vout); |
| 158 | } |
| 159 | len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->komodoshis),&pax->komodoshis); |
| 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 | } |
| 175 | return(len); |
| 176 | } |
| 177 | |
| 178 | int32_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) |
| 179 | { |
| 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]; |
| 181 | incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4)); |
| 182 | for (i=0; i<4; i++) |
| 183 | base[i] = opretbuf[opretlen-4+i]; |
| 184 | //for (i=0; i<opretlen; i++) |
| 185 | // printf("%02x",opretbuf[i]); |
| 186 | //printf(" opretlen.%d (%s)\n",opretlen,base); |
| 187 | //printf(" opretlen.%d vs %d incr.%d\n",opretlen,(int32_t)(2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4),incr); |
| 188 | //if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) |
| 189 | { |
| 190 | type = opretbuf[0]; |
| 191 | opretbuf++, opretlen--; |
| 192 | for (n=0; n<opretlen/incr; n++) |
| 193 | { |
| 194 | if ( iskomodo != 0 ) |
| 195 | { |
| 196 | memset(&p,0,sizeof(p)); |
| 197 | len += komodo_rwapproval(0,&opretbuf[len],&p); |
| 198 | if ( values != 0 && srcvalues != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 && rmd160s != 0 ) |
| 199 | { |
| 200 | txids[n] = p.txid; |
| 201 | vouts[n] = p.vout; |
| 202 | values[n] = (strcmp("KMD",base) == 0) ? p.komodoshis : p.fiatoshis; |
| 203 | srcvalues[n] = (strcmp("KMD",base) == 0) ? p.fiatoshis : p.komodoshis; |
| 204 | kmdheights[n] = p.height; |
| 205 | otherheights[n] = p.otherheight; |
| 206 | memcpy(&rmd160s[n * 20],p.rmd160,20); |
| 207 | baseids[n] = komodo_baseid(p.source); |
| 208 | if ( 0 ) |
| 209 | { |
| 210 | char coinaddr[64]; |
| 211 | bitcoin_address(coinaddr,60,&rmd160s[n * 20],20); |
| 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])); |
| 213 | } |
| 214 | } |
| 215 | } |
| 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]; |
| 225 | baseids[n] = komodo_baseid(base); |
| 226 | if ( (pax= komodo_paxfinds(txids[n],vouts[n])) != 0 ) |
| 227 | { |
| 228 | values[n] = (strcmp("KMD",base) == 0) ? pax->komodoshis : pax->fiatoshis; |
| 229 | srcvalues[n] = (strcmp("KMD",base) == 0) ? pax->fiatoshis : pax->komodoshis; |
| 230 | kmdheights[n] = pax->height; |
| 231 | otherheights[n] = pax->otherheight; |
| 232 | memcpy(&rmd160s[n * 20],pax->rmd160,20); |
| 233 | } |
| 234 | } |
| 235 | //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen); |
| 236 | } |
| 237 | } |
| 238 | return(n); |
| 239 | } |
| 240 | |
| 241 | uint64_t komodo_paxtotal() |
| 242 | { |
| 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; |
| 244 | if ( komodo_isrealtime(&ht) == 0 ) |
| 245 | return(0); |
| 246 | else |
| 247 | { |
| 248 | HASH_ITER(hh,PAX,pax,tmp) |
| 249 | { |
| 250 | if ( pax->marked != 0 ) |
| 251 | continue; |
| 252 | if ( pax->type == 'A' || pax->type == 'D' || pax->type == 'X' ) |
| 253 | str = pax->symbol; |
| 254 | else str = pax->source; |
| 255 | basesp = komodo_stateptrget(str); |
| 256 | if ( basesp != 0 && pax->didstats == 0 ) |
| 257 | { |
| 258 | if ( pax->type == 'I' && (pax2= komodo_paxfind(pax->txid,pax->vout,'D')) != 0 ) |
| 259 | { |
| 260 | if ( pax2->fiatoshis != 0 ) |
| 261 | { |
| 262 | pax->komodoshis = pax2->komodoshis; |
| 263 | pax->fiatoshis = pax2->fiatoshis; |
| 264 | basesp->issued += pax->fiatoshis; |
| 265 | pax->didstats = 1; |
| 266 | if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 ) |
| 267 | printf("########### %p issued %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,str,dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight); |
| 268 | pax2->marked = pax->height; |
| 269 | pax->marked = pax->height; |
| 270 | } |
| 271 | } |
| 272 | else if ( pax->type == 'W' ) |
| 273 | { |
| 274 | //bitcoin_address(coinaddr,addrtype,rmd160,20); |
| 275 | if ( (checktoshis= komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis)) != 0 ) |
| 276 | { |
| 277 | if ( checktoshis != pax->komodoshis ) |
| 278 | pax->marked = pax->height; |
| 279 | else if ( pax->validated == 0 ) |
| 280 | { |
| 281 | pax->validated = pax->komodoshis; |
| 282 | if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 ) |
| 283 | 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)); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | komodo_stateptr(symbol,dest); |
| 291 | HASH_ITER(hh,PAX,pax,tmp) |
| 292 | { |
| 293 | pax->ready = 0; |
| 294 | if ( pax->marked != 0 ) |
| 295 | continue; |
| 296 | //printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis)); |
| 297 | if ( strcmp(symbol,pax->symbol) == 0 ) |
| 298 | { |
| 299 | if ( pax->marked == 0 ) |
| 300 | { |
| 301 | if ( komodo_is_issuer() != 0 ) |
| 302 | { |
| 303 | if ( pax->validated != 0 && pax->type == 'D' ) |
| 304 | { |
| 305 | total += pax->fiatoshis; |
| 306 | pax->ready = 1; |
| 307 | } |
| 308 | } |
| 309 | else if ( pax->approved != 0 ) |
| 310 | { |
| 311 | if ( pax->validated != 0 ) |
| 312 | { |
| 313 | total += pax->komodoshis; |
| 314 | pax->ready = 1; |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | seed = 0; |
| 319 | checktoshis = komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis); |
| 320 | //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); |
| 321 | //printf(" v%d %.8f k.%d ht.%d\n",pax->vout,dstr(pax->komodoshis),pax->height,pax->otherheight); |
| 322 | if ( seed != 0 && checktoshis != 0 ) |
| 323 | { |
| 324 | if ( checktoshis == pax->komodoshis ) |
| 325 | { |
| 326 | total += pax->komodoshis; |
| 327 | pax->validated = pax->komodoshis; |
| 328 | pax->ready = 1; |
| 329 | } else pax->marked = pax->height; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | //printf("paxtotal %.8f\n",dstr(total)); |
| 337 | return(total); |
| 338 | } |
| 339 | |
| 340 | int32_t komodo_pending_withdraws(char *opretstr) |
| 341 | { |
| 342 | struct pax_transaction *pax,*tmp; uint8_t opretbuf[16384]; int32_t ht,len=0; uint64_t total = 0; |
| 343 | if ( komodo_isrealtime(&ht) == 0 || ASSETCHAINS_SYMBOL[0] != 0 ) |
| 344 | return(0); |
| 345 | HASH_ITER(hh,PAX,pax,tmp) |
| 346 | { |
| 347 | //printf("pax %s marked.%u approved.%u\n",pax->symbol,pax->marked,pax->approved); |
| 348 | if ( pax->marked == 0 && strcmp((char *)"KMD",pax->symbol) == 0 && pax->approved == 0 && pax->validated != 0 ) |
| 349 | { |
| 350 | // add 'A' opreturn entry |
| 351 | if ( len == 0 ) |
| 352 | opretbuf[len++] = 'A'; |
| 353 | len += komodo_rwapproval(1,&opretbuf[len],pax); |
| 354 | //printf("%s.(marked.%u approved.%d) %p\n",pax->source,pax->marked,pax->approved,pax); |
| 355 | } |
| 356 | } |
| 357 | if ( len > 0 ) |
| 358 | init_hexbytes_noT(opretstr,opretbuf,len); |
| 359 | else opretstr[0] = 0; |
| 360 | //fprintf(stderr,"komodo_pending_withdraws len.%d PAXTOTAL %.8f\n",len,dstr(komodo_paxtotal())); |
| 361 | return(len); |
| 362 | } |
| 363 | |
| 364 | int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo) |
| 365 | { |
| 366 | 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; |
| 367 | sp = komodo_stateptr(symbol,dest); |
| 368 | strcpy(symbol,base); |
| 369 | if ( komodo_baseid(base) < 0 ) |
| 370 | return(0); |
| 371 | PENDING_KOMODO_TX = 0; |
| 372 | if ( tokomodo == 0 ) |
| 373 | { |
| 374 | opcode = 'I'; |
| 375 | if ( komodo_isrealtime(&ht) == 0 ) |
| 376 | return(0); |
| 377 | } else opcode = 'X'; |
| 378 | HASH_ITER(hh,PAX,pax,tmp) |
| 379 | { |
| 380 | //printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis)); |
| 381 | if ( strcmp(symbol,"KMD") == 0 && pax->approved == 0 ) |
| 382 | continue; |
| 383 | //else if ( strcmp(symbol,"KMD") != 0 ) |
| 384 | { |
| 385 | #ifdef KOMODO_ASSETCHAINS_WAITNOTARIZE |
| 386 | struct komodo_state *kmdsp = komodo_stateptrget((char *)"KMD"); |
| 387 | if ( kmdsp != 0 && kmdsp->NOTARIZED_HEIGHT >= pax->height ) // assumes same chain as notarize |
| 388 | pax->validated = pax->komodoshis; //kmdsp->NOTARIZED_HEIGHT; |
| 389 | #endif |
| 390 | } |
| 391 | if ( pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis ) |
| 392 | { |
| 393 | printf("miner: skip %s %.8f when avail %.8f\n",symbol,dstr(pax->fiatoshis),dstr(available)); |
| 394 | continue; |
| 395 | } |
| 396 | if ( pax->marked != 0 || (pax->type != 'D' && pax->type != 'A') || pax->ready == 0 ) |
| 397 | continue; |
| 398 | if ( strcmp(pax->symbol,symbol) != 0 || pax->validated == 0 ) |
| 399 | { |
| 400 | //printf("pax->symbol.%s != %s or null pax->validated %.8f\n",pax->symbol,symbol,dstr(pax->validated)); |
| 401 | continue; |
| 402 | } |
| 403 | //if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) |
| 404 | printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis)); |
| 405 | txNew->vout.resize(numvouts+1); |
| 406 | txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis; |
| 407 | txNew->vout[numvouts].scriptPubKey.resize(25); |
| 408 | script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0]; |
| 409 | *script++ = 0x76; |
| 410 | *script++ = 0xa9; |
| 411 | *script++ = 20; |
| 412 | memcpy(script,pax->rmd160,20), script += 20; |
| 413 | *script++ = 0x88; |
| 414 | *script++ = 0xac; |
| 415 | if ( tokomodo == 0 ) |
| 416 | { |
| 417 | for (i=0; i<32; i++) |
| 418 | data[len++] = ((uint8_t *)&pax->txid)[i]; |
| 419 | data[len++] = pax->vout & 0xff; |
| 420 | data[len++] = (pax->vout >> 8) & 0xff; |
| 421 | PENDING_KOMODO_TX += pax->fiatoshis; |
| 422 | } |
| 423 | else |
| 424 | { |
| 425 | len += komodo_rwapproval(1,&data[len],pax); |
| 426 | PENDING_KOMODO_TX += pax->komodoshis; |
| 427 | //printf(" vout.%u DEPOSIT %.8f <- pax.%s pending %.8f | ",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,dstr(PENDING_KOMODO_TX)); |
| 428 | } |
| 429 | if ( numvouts++ >= 64 ) |
| 430 | break; |
| 431 | } |
| 432 | if ( numvouts > 1 ) |
| 433 | { |
| 434 | if ( tokomodo != 0 ) |
| 435 | strcpy(symbol,(char *)"KMD"); |
| 436 | for (i=0; symbol[i]!=0; i++) |
| 437 | data[len++] = symbol[i]; |
| 438 | data[len++] = 0; |
| 439 | opretlen = komodo_opreturnscript(opret,opcode,data,len); |
| 440 | txNew->vout.resize(numvouts+1); |
| 441 | txNew->vout[numvouts].nValue = 0; |
| 442 | txNew->vout[numvouts].scriptPubKey.resize(opretlen); |
| 443 | script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0]; |
| 444 | memcpy(script,opret,opretlen); |
| 445 | printf("MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen); |
| 446 | return(1); |
| 447 | } |
| 448 | return(0); |
| 449 | } |
| 450 | |
| 451 | int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing |
| 452 | { |
| 453 | 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; |
| 454 | memset(baseids,0xff,sizeof(baseids)); |
| 455 | memset(values,0,sizeof(values)); |
| 456 | memset(srcvalues,0,sizeof(srcvalues)); |
| 457 | memset(rmd160s,0,sizeof(rmd160s)); |
| 458 | memset(kmdheights,0,sizeof(kmdheights)); |
| 459 | memset(otherheights,0,sizeof(otherheights)); |
| 460 | n = block.vtx[0].vout.size(); |
| 461 | script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data(); |
| 462 | if ( n <= 2 || script[0] != 0x6a ) |
| 463 | return(0); |
| 464 | offset += komodo_scriptitemlen(&opretlen,&script[offset]); |
| 465 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) |
| 466 | { |
| 467 | //for (i=0; i<opretlen; i++) |
| 468 | // printf("%02x",script[i]); |
| 469 | //printf(" height.%d checkdeposit n.%d [%02x] [%c] %d vs %d\n",height,n,script[0],script[offset],script[offset],'X'); |
| 470 | opcode = 'X'; |
| 471 | strcpy(symbol,(char *)"KMD"); |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | strcpy(symbol,ASSETCHAINS_SYMBOL); |
| 476 | opcode = 'I'; |
| 477 | if ( komodo_baseid(symbol) < 0 ) |
| 478 | { |
| 479 | if ( block.vtx[0].vout.size() != 1 ) |
| 480 | { |
| 481 | printf("%s has more than one coinbase?\n",symbol); |
| 482 | return(-1); |
| 483 | } |
| 484 | return(0); |
| 485 | } |
| 486 | } |
| 487 | if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() ) |
| 488 | { |
| 489 | if ( (num= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,&script[offset],opretlen,opcode == 'X')) > 0 ) |
| 490 | { |
| 491 | for (i=1; i<n-1; i++) |
| 492 | { |
| 493 | if ( (pax= komodo_paxfinds(txids[i-1],vouts[i-1])) != 0 ) // finds... make sure right one |
| 494 | { |
| 495 | pax->type = opcode; |
| 496 | if ( opcode == 'I' && pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis ) |
| 497 | { |
| 498 | printf("checkdeposit: skip %s %.8f when avail %.8f\n",pax->symbol,dstr(pax->fiatoshis),dstr(available)); |
| 499 | continue; |
| 500 | } |
| 501 | 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))) ) |
| 502 | { |
| 503 | if ( pax->marked != 0 && height >= 80820 ) |
| 504 | { |
| 505 | 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); |
| 506 | errs++; |
| 507 | } else matched++; |
| 508 | } |
| 509 | else |
| 510 | { |
| 511 | for (j=0; j<32; j++) |
| 512 | printf("%02x",((uint8_t *)&txids[i-1])[j]); |
| 513 | printf(" cant paxfind %c txid\n",opcode); |
| 514 | 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); |
| 515 | } |
| 516 | } |
| 517 | else |
| 518 | { |
| 519 | hash = block.GetHash(); |
| 520 | for (j=0; j<32; j++) |
| 521 | printf("%02x",((uint8_t *)&hash)[j]); |
| 522 | printf(" ht.%d blockhash X couldnt find vout.[%d]\n",height,i); |
| 523 | } |
| 524 | } |
| 525 | if ( matched != num ) |
| 526 | { |
| 527 | printf("WOULD REJECT %s: ht.%d (%c) matched.%d vs num.%d\n",symbol,height,opcode,matched,num); |
| 528 | // can easily happen depending on order of loading |
| 529 | if ( height > 150000 ) |
| 530 | { |
| 531 | printf("REJECT: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num); |
| 532 | return(-1); |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | //printf("opretlen.%d num.%d\n",opretlen,num); |
| 537 | } |
| 538 | return(0); |
| 539 | } |
| 540 | |
| 541 | int32_t komodo_paxcmp(char *symbol,int32_t kmdheight,uint64_t value,uint64_t checkvalue,uint64_t seed) |
| 542 | { |
| 543 | int32_t ratio; |
| 544 | if ( seed == 0 && checkvalue != 0 ) |
| 545 | { |
| 546 | ratio = ((value << 6) / checkvalue); |
| 547 | if ( ratio >= 63 && ratio <= 65 ) |
| 548 | return(0); |
| 549 | else |
| 550 | { |
| 551 | if ( kmdheight >= 86150 ) |
| 552 | printf("ht.%d ignore mismatched %s value %lld vs checkvalue %lld -> ratio.%d\n",kmdheight,symbol,(long long)value,(long long)checkvalue,ratio); |
| 553 | return(-1); |
| 554 | } |
| 555 | } |
| 556 | else if ( checkvalue != 0 ) |
| 557 | { |
| 558 | ratio = ((value << 10) / checkvalue); |
| 559 | if ( ratio >= 1023 && ratio <= 1025 ) |
| 560 | return(0); |
| 561 | } |
| 562 | return(value != checkvalue); |
| 563 | } |
| 564 | |
| 565 | const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source) |
| 566 | { |
| 567 | 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; |
| 568 | const char *typestr = "unknown"; |
| 569 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) |
| 570 | return("komodo"); |
| 571 | else if ( komodo_baseid(ASSETCHAINS_SYMBOL) < 0 ) |
| 572 | return("assetchain"); |
| 573 | memset(baseids,0xff,sizeof(baseids)); |
| 574 | memset(values,0,sizeof(values)); |
| 575 | memset(srcvalues,0,sizeof(srcvalues)); |
| 576 | memset(rmd160s,0,sizeof(rmd160s)); |
| 577 | memset(kmdheights,0,sizeof(kmdheights)); |
| 578 | memset(otherheights,0,sizeof(otherheights)); |
| 579 | tokomodo = (komodo_is_issuer() == 0); |
| 580 | if ( opretbuf[0] == 'D' ) |
| 581 | { |
| 582 | tokomodo = 0; |
| 583 | if ( opretlen == 38 ) // any KMD tx |
| 584 | { |
| 585 | iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight); |
| 586 | memset(base,0,sizeof(base)); |
| 587 | PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis); |
| 588 | bitcoin_address(coinaddr,addrtype,rmd160,20); |
| 589 | checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis); |
| 590 | typestr = "deposit"; |
| 591 | if ( kmdheight <= height ) |
| 592 | { |
| 593 | didstats = 0; |
| 594 | if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 ) |
| 595 | { |
| 596 | 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); |
| 597 | for (i=0; i<32; i++) |
| 598 | printf("%02x",((uint8_t *)&txid)[i]); |
| 599 | printf(" <- txid.v%u ",vout); |
| 600 | for (i=0; i<33; i++) |
| 601 | printf("%02x",pubkey33[i]); |
| 602 | printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height); |
| 603 | } |
| 604 | if ( komodo_paxcmp(base,kmdheight,value,checktoshis,seed) == 0 ) |
| 605 | { |
| 606 | if ( (pax= komodo_paxfind(txid,vout,'D')) == 0 ) |
| 607 | { |
| 608 | if ( (basesp= komodo_stateptrget(base)) != 0 ) |
| 609 | { |
| 610 | basesp->deposited += fiatoshis; |
| 611 | didstats = 1; |
| 612 | if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 ) |
| 613 | printf("########### %p deposited %s += %.8f kmdheight.%d %.8f\n",basesp,base,dstr(fiatoshis),kmdheight,dstr(value)); |
| 614 | } else printf("cant get stateptr.(%s)\n",base); |
| 615 | komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,'D',kmdheight,height,(char *)"KMD",0); |
| 616 | } |
| 617 | if ( (pax= komodo_paxfind(txid,vout,'D')) != 0 ) |
| 618 | { |
| 619 | pax->height = kmdheight; |
| 620 | pax->validated = value; |
| 621 | pax->komodoshis = value; |
| 622 | pax->fiatoshis = fiatoshis; |
| 623 | if ( didstats == 0 && pax->didstats == 0 ) |
| 624 | { |
| 625 | if ( (basesp= komodo_stateptrget(base)) != 0 ) |
| 626 | { |
| 627 | basesp->deposited += fiatoshis; |
| 628 | didstats = 1; |
| 629 | if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 ) |
| 630 | 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)); |
| 631 | } |
| 632 | } |
| 633 | if ( didstats != 0 ) |
| 634 | pax->didstats = 1; |
| 635 | if ( (pax2= komodo_paxfind(txid,vout,'I')) != 0 ) |
| 636 | { |
| 637 | pax2->fiatoshis = pax->fiatoshis; |
| 638 | pax2->komodoshis = pax->komodoshis; |
| 639 | pax->marked = pax2->marked = pax->height; |
| 640 | pax2->height = pax->height = height; |
| 641 | if ( pax2->didstats == 0 ) |
| 642 | { |
| 643 | if ( (basesp= komodo_stateptrget(base)) != 0 ) |
| 644 | { |
| 645 | basesp->issued += pax2->fiatoshis; |
| 646 | pax2->didstats = 1; |
| 647 | if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 ) |
| 648 | printf("########### %p issueda %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,base,dstr(pax2->fiatoshis),pax2->height,dstr(pax2->komodoshis),pax2->otherheight); |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | else if ( kmdheight > 91800 ) |
| 655 | printf("pax %s deposit %.8f rejected kmdheight.%d %.8f KMD\n",base,dstr(fiatoshis),kmdheight,dstr(value)); |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | else if ( opretbuf[0] == 'I' ) |
| 660 | { |
| 661 | tokomodo = 0; |
| 662 | if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 ) |
| 663 | { |
| 664 | if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 ) |
| 665 | { |
| 666 | for (i=0; i<n; i++) |
| 667 | { |
| 668 | if ( baseids[i] < 0 ) |
| 669 | { |
| 670 | printf("%d of %d illegal baseid.%d\n",i,n,baseids[i]); |
| 671 | continue; |
| 672 | } |
| 673 | bitcoin_address(coinaddr,60,&rmd160s[i*20],20); |
| 674 | komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'I',height,0,CURRENCIES[baseids[i]],0); |
| 675 | komodo_paxmark(height,txids[i],vouts[i],'I',height); |
| 676 | if ( (pax= komodo_paxfind(txids[i],vouts[i],'I')) != 0 ) |
| 677 | { |
| 678 | pax->type = opretbuf[0]; |
| 679 | strcpy(pax->source,(char *)&opretbuf[opretlen-4]); |
| 680 | if ( (pax2= komodo_paxfind(txids[i],vouts[i],'D')) != 0 && pax2->fiatoshis != 0 && pax2->komodoshis != 0 ) |
| 681 | { |
| 682 | // realtime path? |
| 683 | pax->fiatoshis = pax2->fiatoshis; |
| 684 | pax->komodoshis = pax2->komodoshis; |
| 685 | pax->marked = pax2->marked = pax2->height; |
| 686 | if ( pax->didstats == 0 ) |
| 687 | { |
| 688 | if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) |
| 689 | { |
| 690 | basesp->issued += pax->fiatoshis; |
| 691 | pax->didstats = 1; |
| 692 | pax->height = pax2->height; |
| 693 | pax->otherheight = height; |
| 694 | if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) |
| 695 | 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); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | komodo_paxmark(pax->height,txids[i],vouts[i],'D',height); |
| 700 | } |
| 701 | } |
| 702 | } else printf("opreturn none issued?\n"); |
| 703 | } |
| 704 | } |
| 705 | else if ( opretbuf[0] == 'W' )//&& opretlen >= 38 ) |
| 706 | { |
| 707 | tokomodo = 1; |
| 708 | iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight); |
| 709 | memset(base,0,sizeof(base)); |
| 710 | PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&komodoshis); |
| 711 | bitcoin_address(coinaddr,addrtype,rmd160,20); |
| 712 | checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,value); |
| 713 | typestr = "withdraw"; |
| 714 | if ( strcmp(base,"RUB") == 0 ) |
| 715 | 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); |
| 716 | didstats = 0; |
| 717 | //if ( komodo_paxcmp(base,kmdheight,komodoshis,checktoshis,seed) == 0 ) |
| 718 | { |
| 719 | if ( value != 0 && ((pax= komodo_paxfind(txid,vout,'W')) == 0 || pax->didstats == 0) ) |
| 720 | { |
| 721 | if ( (basesp= komodo_stateptrget(base)) != 0 ) |
| 722 | { |
| 723 | basesp->withdrawn += value; |
| 724 | didstats = 1; |
| 725 | if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 ) |
| 726 | printf("########### %p withdrawn %s += %.8f\n",basesp,base,dstr(value)); |
| 727 | } |
| 728 | if ( strcmp(base,"RUB") == 0 ) |
| 729 | printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height); |
| 730 | } |
| 731 | komodo_gateway_deposit(coinaddr,0,(char *)"KMD",value,rmd160,txid,vout,'W',kmdheight,height,source,0); |
| 732 | if ( (pax= komodo_paxfind(txid,vout,'W')) != 0 ) |
| 733 | { |
| 734 | pax->type = opretbuf[0]; |
| 735 | strcpy(pax->symbol,base); |
| 736 | pax->height = kmdheight; |
| 737 | pax->otherheight = height; |
| 738 | pax->komodoshis = komodoshis; |
| 739 | } |
| 740 | } // else printf("withdraw %s paxcmp ht.%d %d error value %.8f -> %.8f vs %.8f\n",base,kmdheight,height,dstr(value),dstr(komodoshis),dstr(checktoshis)); |
| 741 | // need to allocate pax |
| 742 | } |
| 743 | else if ( tokomodo != 0 && opretbuf[0] == 'A' ) |
| 744 | { |
| 745 | tokomodo = 1; |
| 746 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) |
| 747 | { |
| 748 | for (i=0; i<opretlen; i++) |
| 749 | printf("%02x",opretbuf[i]); |
| 750 | printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal())); |
| 751 | } |
| 752 | if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 ) |
| 753 | { |
| 754 | for (i=0; i<n; i++) |
| 755 | { |
| 756 | if ( baseids[i] < 0 ) |
| 757 | continue; |
| 758 | didstats = 0; |
| 759 | seed = 0; |
| 760 | checktoshis = komodo_paxprice(&seed,kmdheights[i],CURRENCIES[baseids[i]],(char *)"KMD",(uint64_t)srcvalues[i]); |
| 761 | /*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); |
| 762 | for (j=0; j<32; j++) |
| 763 | printf("%02x",((uint8_t *)&txids[i])[j]); |
| 764 | printf(" v%d %.8f k.%d ht.%d base.%d\n",vouts[i],dstr(values[i]),kmdheights[i],otherheights[i],baseids[i]);*/ |
| 765 | if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 ) |
| 766 | { |
| 767 | bitcoin_address(coinaddr,60,&rmd160s[i*20],20); |
| 768 | 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]); |
| 769 | komodo_paxmark(height,txids[i],vouts[i],'W',height); |
| 770 | komodo_paxmark(height,txids[i],vouts[i],'A',height); |
| 771 | if ( srcvalues[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) |
| 772 | { |
| 773 | basesp->approved += srcvalues[i]; |
| 774 | didstats = 1; |
| 775 | if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) |
| 776 | printf("########### %p approved %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i])); |
| 777 | } |
| 778 | //printf(" i.%d (%s) <- %.8f ADDFLAG APPROVED\n",i,coinaddr,dstr(values[i])); |
| 779 | } |
| 780 | else if ( pax->didstats == 0 && srcvalues[i] != 0 ) |
| 781 | { |
| 782 | if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) |
| 783 | { |
| 784 | basesp->approved += srcvalues[i]; |
| 785 | didstats = 1; |
| 786 | if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) |
| 787 | printf("########### %p approved %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i])); |
| 788 | } |
| 789 | } //else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]); |
| 790 | if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) != 0 ) |
| 791 | { |
| 792 | pax->type = opretbuf[0]; |
| 793 | pax->approved = kmdheights[i]; |
| 794 | if ( didstats != 0 ) |
| 795 | pax->didstats = 1; |
| 796 | if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) |
| 797 | printf(" i.%d approved.%d <<<<<<<<<<<<< APPROVED %p\n",i,kmdheights[i],pax); |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | //printf("extra.[%d] after %.8f\n",n,dstr(komodo_paxtotal())); |
| 802 | } |
| 803 | else if ( opretbuf[0] == 'X' ) |
| 804 | { |
| 805 | tokomodo = 1; |
| 806 | if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 ) |
| 807 | { |
| 808 | for (i=0; i<n; i++) |
| 809 | { |
| 810 | if ( baseids[i] < 0 ) |
| 811 | continue; |
| 812 | bitcoin_address(coinaddr,60,&rmd160s[i*20],20); |
| 813 | komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'X',height,0,(char *)"KMD",0); |
| 814 | komodo_paxmark(height,txids[i],vouts[i],'W',height); |
| 815 | komodo_paxmark(height,txids[i],vouts[i],'A',height); |
| 816 | komodo_paxmark(height,txids[i],vouts[i],'X',height); |
| 817 | if ( (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 ) |
| 818 | { |
| 819 | pax->type = opretbuf[0]; |
| 820 | if ( baseids[i] >= 0 && srcvalues[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) |
| 821 | { |
| 822 | basesp->redeemed += srcvalues[i]; |
| 823 | pax->didstats = 1; |
| 824 | if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) |
| 825 | printf("########### %p redeemed %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i])); |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | } //else printf("komodo_issued_opreturn returned %d\n",n); |
| 830 | } |
| 831 | return(typestr); |
| 832 | } |
| 833 | |
| 834 | void komodo_passport_iteration() |
| 835 | { |
| 836 | static long lastpos[34]; static char userpass[33][1024]; |
| 837 | 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; |
| 838 | printf("PASSPORT.(%s)\n",ASSETCHAINS_SYMBOL); |
| 839 | |
| 840 | while ( KOMODO_INITDONE == 0 ) |
| 841 | { |
| 842 | fprintf(stderr,"PASSPORT iteration waiting for KOMODO_INITDONE\n"); |
| 843 | sleep(3); |
| 844 | } |
| 845 | refsp = komodo_stateptr(symbol,dest); |
| 846 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) |
| 847 | refid = 33; |
| 848 | else |
| 849 | { |
| 850 | refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0 |
| 851 | if ( refid == 0 ) |
| 852 | { |
| 853 | KOMODO_PASSPORT_INITDONE = 1; |
| 854 | return; |
| 855 | } |
| 856 | } |
| 857 | printf("PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid); |
| 858 | for (baseid=32; baseid>=0; baseid--) |
| 859 | { |
| 860 | sp = 0; |
| 861 | isrealtime = 0; |
| 862 | base = (char *)CURRENCIES[baseid]; |
| 863 | if ( baseid+1 != refid ) |
| 864 | { |
| 865 | komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate"); |
| 866 | komodo_nameset(symbol,dest,base); |
| 867 | if ( (fp= fopen(fname,"rb")) != 0 && (sp= komodo_stateptrget(symbol)) != 0 ) |
| 868 | { |
| 869 | fseek(fp,0,SEEK_END); |
| 870 | if ( ftell(fp) > lastpos[baseid] ) |
| 871 | { |
| 872 | if ( 0 && lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 ) |
| 873 | printf("passport refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base); |
| 874 | fseek(fp,lastpos[baseid],SEEK_SET); |
| 875 | while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 ) |
| 876 | ; |
| 877 | lastpos[baseid] = ftell(fp); |
| 878 | if ( 0 && lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 ) |
| 879 | printf("from.(%s) lastpos[%s] %ld\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid]); |
| 880 | } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp)); |
| 881 | fclose(fp); |
| 882 | } |
| 883 | komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime"); |
| 884 | if ( (fp= fopen(fname,"rb")) != 0 ) |
| 885 | { |
| 886 | if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) ) |
| 887 | { |
| 888 | sp->CURRENT_HEIGHT = buf[0]; |
| 889 | if ( buf[0] != 0 && buf[0] == buf[1] && buf[2] > time(NULL)-60 ) |
| 890 | { |
| 891 | isrealtime = 1; |
| 892 | RTmask |= (1LL << baseid); |
| 893 | memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1])); |
| 894 | } //else fprintf(stderr,"%s not RT\n",base); |
| 895 | } //else fprintf(stderr,"%s size error RT\n",base); |
| 896 | fclose(fp); |
| 897 | } //else fprintf(stderr,"%s open error RT\n",base); |
| 898 | } |
| 899 | else |
| 900 | { |
| 901 | komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime"); |
| 902 | if ( (fp= fopen(fname,"wb")) != 0 ) |
| 903 | { |
| 904 | buf[0] = (uint32_t)chainActive.Tip()->nHeight; |
| 905 | buf[1] = (uint32_t)komodo_longestchain(); |
| 906 | if ( buf[0] != 0 && buf[0] == buf[1] ) |
| 907 | { |
| 908 | buf[2] = (uint32_t)time(NULL); |
| 909 | RTmask |= (1LL << baseid) | 1; |
| 910 | memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1])); |
| 911 | if ( refid != 0 ) |
| 912 | memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0])); |
| 913 | } |
| 914 | if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) ) |
| 915 | fprintf(stderr,"[%s] %s error writing realtime\n",ASSETCHAINS_SYMBOL,base); |
| 916 | fclose(fp); |
| 917 | } else fprintf(stderr,"%s create error RT\n",base); |
| 918 | } |
| 919 | if ( sp != 0 && isrealtime == 0 ) |
| 920 | refsp->RTbufs[0][2] = 0; |
| 921 | } |
| 922 | komodo_paxtotal(); |
| 923 | refsp->RTmask = RTmask; |
| 924 | KOMODO_PASSPORT_INITDONE = 1; |
| 925 | printf("done PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid); |
| 926 | } |
| 927 | |