]> 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
3cbfd2b4 18int32_t pax_fiatstatus(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;
21 if ( (baseid= komodo_baseid(base)) >= 0 )
22 {
23 if ( (sp= komodo_stateptrget(base)) != 0 )
24 {
25 *deposited = sp->deposited;
26 *issued = sp->issued;
27 *withdrawn = sp->withdrawn;
28 *approved = sp->approved;
29 *redeemed = sp->redeemed;
1db587b5 30 //printf("%p %s %.8f %.8f %.8f %.8f %.8f\n",sp,base,dstr(*deposited),dstr(*issued),dstr(*withdrawn),dstr(*approved),dstr(*redeemed));
3cbfd2b4 31 return(0);
ee5e5744 32 } else printf("pax_fiatstatus cant get basesp.%s\n",base);
33 } else printf("pax_fiatstatus illegal base.%s\n",base);
3cbfd2b4 34 return(-1);
35}
36
51e5d952 37void pax_keyset(uint8_t *buf,uint256 txid,uint16_t vout,uint8_t type)
6210f328 38{
51e5d952 39 memcpy(buf,&txid,32);
40 memcpy(&buf[32],&vout,2);
41 buf[34] = type;
42}
43
44struct pax_transaction *komodo_paxfind(uint256 txid,uint16_t vout,uint8_t type)
45{
46 struct pax_transaction *pax; uint8_t buf[35];
6210f328 47 pthread_mutex_lock(&komodo_mutex);
51e5d952 48 pax_keyset(buf,txid,vout,type);
49 HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
6210f328 50 pthread_mutex_unlock(&komodo_mutex);
51 return(pax);
52}
53
51e5d952 54struct pax_transaction *komodo_paxmark(int32_t height,uint256 txid,uint16_t vout,uint8_t type,int32_t mark)
6210f328 55{
51e5d952 56 struct pax_transaction *pax; uint8_t buf[35];
6210f328 57 pthread_mutex_lock(&komodo_mutex);
51e5d952 58 pax_keyset(buf,txid,vout,type);
59 HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
2ab43fd2 60 if ( pax == 0 )
61 {
62 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
63 pax->txid = txid;
64 pax->vout = vout;
51e5d952 65 pax->type = type;
66 memcpy(pax->buf,buf,sizeof(pax->buf));
67 HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
32d525eb 68 //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark);
2ab43fd2 69 }
6210f328 70 if ( pax != 0 )
71 {
72 pax->marked = mark;
f1e6bb01 73 //int32_t i; for (i=0; i<32; i++)
74 // printf("%02x",((uint8_t *)&txid)[i]);
75 //printf(" paxmark.ht %d vout%d\n",mark,vout);
6210f328 76 }
77 pthread_mutex_unlock(&komodo_mutex);
78 return(pax);
79}
429dabb5 80
51e5d952 81void 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 82{
51e5d952 83 struct pax_transaction *pax; uint8_t buf[35]; int32_t addflag = 0; struct komodo_state *sp; char str[16],dest[16],*s;
bffa91b2 84 sp = komodo_stateptr(str,dest);
d43c7a93 85 pthread_mutex_lock(&komodo_mutex);
51e5d952 86 pax_keyset(buf,txid,vout,type);
87 HASH_FIND(hh,PAX,buf,sizeof(buf),pax);
d43c7a93 88 if ( pax == 0 )
b2d64a0f 89 {
d43c7a93 90 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
8d52488f 91 pax->txid = txid;
92 pax->vout = vout;
51e5d952 93 pax->type = type;
94 memcpy(pax->buf,buf,sizeof(pax->buf));
95 HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax);
7961668b 96 addflag = 1;
1fc75a27 97 if ( ASSETCHAINS_SYMBOL[0] == 0 )
98 {
99 int32_t i; for (i=0; i<32; i++)
100 printf("%02x",((uint8_t *)&txid)[i]);
5ce8bf22 101 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 102 }
b2d64a0f 103 }
8d52488f 104 pthread_mutex_unlock(&komodo_mutex);
d43c7a93 105 if ( coinaddr != 0 )
106 {
107 strcpy(pax->coinaddr,coinaddr);
9ccd3f33 108 if ( value != 0 )
109 pax->komodoshis = value;
110 if ( symbol != 0 )
111 strcpy(pax->symbol,symbol);
cd26c1f3 112 if ( source != 0 )
113 strcpy(pax->source,source);
9ccd3f33 114 if ( fiatoshis != 0 )
115 pax->fiatoshis = fiatoshis;
116 if ( rmd160 != 0 )
117 memcpy(pax->rmd160,rmd160,20);
118 if ( height != 0 )
119 pax->height = height;
120 if ( otherheight != 0 )
121 pax->otherheight = otherheight;
1db587b5 122 if ( pax->marked == 0 )
123 {
124 if ( addflag != 0 )
125 {
126 if ( (pax->approved= approved) != 0 )
127 s = (char *)"APPROVED";
128 else s = (char *)((ASSETCHAINS_SYMBOL[0] == 0) ? "WITHDRAW" : "DEPOSIT");
3cbfd2b4 129 printf("[%s] addflag.%d ADD %s/%s %s %.8f -> %s TO PAX ht.%d otherht.%d total %.8f\n",ASSETCHAINS_SYMBOL,addflag,s,symbol,source,dstr(ASSETCHAINS_SYMBOL[0]==0?pax->komodoshis:pax->fiatoshis),pax->coinaddr,pax->height,pax->otherheight,dstr(komodo_paxtotal()));
d6be719d 130 }
7961668b 131 }
0dca8d4c 132 //else printf("%p MARKED.%d DEPOSIT %s %.8f -> %s TO PAX ht.%d otherht.%d\n",pax,pax->marked,symbol,dstr(fiatoshis),coinaddr,height,otherheight);
d43c7a93 133 }
134 else
135 {
136 pax->marked = height;
1fc75a27 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++];
fbd1641c 155 for (i=0; i<32; i++)
a6972662 156 printf("%02x",((uint8_t *)&pax->txid)[31-i]);
ddf827b2 157 pax->vout = opretbuf[len++];
158 pax->vout += ((uint32_t)opretbuf[len++] << 8);
ad66994d 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];
16bed6ac 183 incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4));
63b289ad 184 for (i=0; i<4; i++)
185 base[i] = opretbuf[opretlen-4+i];
0e1af8ed 186 for (i=0; i<opretlen; i++)
187 printf("%02x",opretbuf[i]);
188 printf(" opretlen.%d (%s)\n",opretlen,base);
6d898f42 189 //printf(" opretlen.%d vs %d incr.%d\n",opretlen,(int32_t)(2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4),incr);
986df802 190 if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
63b289ad 191 {
947d956f 192 type = opretbuf[0];
63b289ad 193 opretbuf++, opretlen--;
e8433f7a 194 for (n=0; n<opretlen/incr; n++)
63b289ad 195 {
05d91eb2 196 if ( iskomodo != 0 )
197 {
e8433f7a 198 memset(&p,0,sizeof(p));
ffec138e 199 len += komodo_rwapproval(0,&opretbuf[len],&p);
b89ab77c 200 if ( values != 0 && srcvalues != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 && rmd160s != 0 )
1ed46112 201 {
63342b19 202 txids[n] = p.txid;
203 vouts[n] = p.vout;
a3f1fb69 204 values[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? p.komodoshis : p.fiatoshis;
b89ab77c 205 srcvalues[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? p.fiatoshis : p.komodoshis;
a3f1fb69 206 kmdheights[n] = p.height;
207 otherheights[n] = p.otherheight;
7cc6844b 208 memcpy(&rmd160s[n * 20],p.rmd160,20);
1dbea872 209 baseids[n] = komodo_baseid(p.source);
71c9af7c 210 {
211 char coinaddr[64];
212 bitcoin_address(coinaddr,60,&rmd160s[n * 20],20);
947d956f 213 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 214 }
52e87248 215 }
05d91eb2 216 }
7cc6844b 217 else
218 {
219 for (j=0; j<32; j++)
220 {
221 ((uint8_t *)&txids[n])[j] = opretbuf[len++];
222 //printf("%02x",((uint8_t *)&txids[n])[j]);
223 }
224 vouts[n] = opretbuf[len++];
225 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
cb3bf8df 226 baseids[n] = komodo_baseid(base);
54661780 227 if ( (pax= komodo_paxfind(txids[n],vouts[n])) != 0 )
228 {
229 values[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? pax->komodoshis : pax->fiatoshis;
230 srcvalues[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? pax->fiatoshis : pax->komodoshis;
231 kmdheights[n] = pax->height;
232 otherheights[n] = pax->otherheight;
233 memcpy(&rmd160s[n * 20],pax->rmd160,20);
234 }
7cc6844b 235 }
6d898f42 236 //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
63b289ad 237 }
238 }
239 return(n);
240}
241
f1db88ea 242uint64_t komodo_paxtotal()
243{
a73709a3 244 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 245 if ( komodo_isrealtime(&ht) == 0 )
f1db88ea 246 return(0);
ca75995d 247 else
248 {
249 HASH_ITER(hh,PAX,pax,tmp)
250 {
16e7ab0f 251 //if ( pax->didstats == 0 && pax->type == 'I' )
ca75995d 252 {
042e3b9f 253 //for (i=0; i<32; i++)
254 // printf("%02x",((uint8_t *)&pax->txid)[i]);
ceabe3df 255 if ( pax->type == 'X' || pax->type == 'A' || pax->type == 'D' || pax->type == 'I' )
a73709a3 256 str = pax->symbol;
257 else str = pax->source;
1113e57b 258 basesp = komodo_stateptrget(str);
c5f534f4 259 if ( komodo_baseid(str) >= 0 && pax->didstats == 0 && pax->fiatoshis != 0 )
ca75995d 260 {
1113e57b 261 if ( basesp != 0 )
ca75995d 262 {
c5f534f4 263 if ( pax->type == 'I' )
264 {
265 basesp->issued += pax->fiatoshis;
266 pax->didstats = 1;
28231c88 267 printf("Iset dstats %.8f += %.8f\n",dstr(basesp->issued),dstr(pax->fiatoshis));
c5f534f4 268 }
fa81b5ae 269 }
270 }
4adf625a 271 printf(" stats.%d type.%c (%s) k.%d %.8f h.%d %.8f I.%.8f X.%.8f\n",pax->didstats,pax->type,str,pax->height,dstr(pax->komodoshis),pax->otherheight,dstr(pax->fiatoshis),dstr(basesp->issued),dstr(basesp->redeemed));
ca75995d 272 }
273 }
274 }
ede04b12 275 komodo_stateptr(symbol,dest);
f1db88ea 276 HASH_ITER(hh,PAX,pax,tmp)
277 {
10694486 278 //printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
ede04b12 279 if ( strcmp(symbol,pax->symbol) == 0 )
f1db88ea 280 {
ede04b12 281 if ( pax->marked == 0 )
282 {
283 if ( komodo_is_issuer() != 0 )
284 total += pax->fiatoshis;
f2db1a97 285 else if ( pax->approved != 0 )
992c3be5 286 {
287 if ( pax->validated != 0 )
288 total += pax->komodoshis;
289 else
290 {
291 seed = 0;
f186b8a1 292 checktoshis = komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis);
0fa85cc3 293 //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);
294 //printf(" v%d %.8f k.%d ht.%d\n",pax->vout,dstr(pax->komodoshis),pax->height,pax->otherheight);
992c3be5 295 if ( seed != 0 )
296 {
e8cbe5fb 297 if ( checktoshis >= pax->komodoshis )
992c3be5 298 {
299 total += pax->komodoshis;
300 pax->validated = pax->komodoshis;
301 } else pax->marked = pax->height;
302 }
303 }
304 }
ede04b12 305 }
f1db88ea 306 }
307 }
986df802 308 //printf("paxtotal %.8f\n",dstr(total));
f1db88ea 309 return(total);
310}
311
7cc6844b 312int32_t komodo_pending_withdraws(char *opretstr)
f360ea0a 313{
ef1a9636 314 struct pax_transaction *pax,*tmp; uint8_t opretbuf[16384]; int32_t ht,len=0; uint64_t total = 0;
f360ea0a 315 if ( komodo_isrealtime(&ht) == 0 || ASSETCHAINS_SYMBOL[0] != 0 )
316 return(0);
317 HASH_ITER(hh,PAX,pax,tmp)
318 {
f97d3439 319 //printf("pax %s marked.%u approved.%u\n",pax->symbol,pax->marked,pax->approved);
f360ea0a 320 if ( pax->marked == 0 && strcmp((char *)"KMD",pax->symbol) == 0 && pax->approved == 0 )
321 {
322 // add 'A' opreturn entry
7cc6844b 323 if ( len == 0 )
324 opretbuf[len++] = 'A';
325 len += komodo_rwapproval(1,&opretbuf[len],pax);
75e47988 326 printf("%s.(marked.%u approved.%d) %p\n",pax->source,pax->marked,pax->approved,pax);
f360ea0a 327 }
328 }
7cc6844b 329 if ( len > 0 )
330 init_hexbytes_noT(opretstr,opretbuf,len);
331 else opretstr[0] = 0;
2afc2dfd 332 printf("komodo_pending_withdraws len.%d PAXTOTAL %.8f\n",len,dstr(komodo_paxtotal()));
f360ea0a 333 return(len);
334}
a4c67285 335
635dd34d 336int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo)
429dabb5 337{
ef1a9636 338 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 mask;
635dd34d 339 sp = komodo_stateptr(symbol,dest);
27bf3c5e 340 strcpy(symbol,base);
429dabb5 341 PENDING_KOMODO_TX = 0;
a3f23ee2 342 if ( tokomodo == 0 )
48e96ba1 343 {
77be6cd9 344 opcode = 'I';
48a3cd18 345 if ( komodo_isrealtime(&ht) == 0 )
48e96ba1 346 return(0);
54661780 347 } else opcode = 'X';
c169f64b 348 HASH_ITER(hh,PAX,pax,tmp)
429dabb5 349 {
986df802 350 //printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
f186b8a1 351 if ( strcmp(symbol,"KMD") == 0 && (pax->approved == 0 || pax->validated == 0) )
992c3be5 352 continue;
353 if ( pax->marked != 0 || strcmp(pax->symbol,symbol) != 0 )
e5430f52 354 continue;
3cbfd2b4 355 if ( ASSETCHAINS_SYMBOL[0] != 0 )
635dd34d 356 printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
e5430f52 357 txNew->vout.resize(numvouts+1);
1fc75a27 358 txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis;
e5430f52 359 txNew->vout[numvouts].scriptPubKey.resize(25);
360 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
361 *script++ = 0x76;
362 *script++ = 0xa9;
363 *script++ = 20;
364 memcpy(script,pax->rmd160,20), script += 20;
365 *script++ = 0x88;
366 *script++ = 0xac;
a3f23ee2 367 if ( tokomodo == 0 )
16bed6ac 368 {
369 for (i=0; i<32; i++)
16bed6ac 370 data[len++] = ((uint8_t *)&pax->txid)[i];
16bed6ac 371 data[len++] = pax->vout & 0xff;
372 data[len++] = (pax->vout >> 8) & 0xff;
e5430f52 373 PENDING_KOMODO_TX += pax->fiatoshis;
16bed6ac 374 }
fbc4209b 375 else
376 {
e0357628 377 len += komodo_rwapproval(1,&data[len],pax);
fbc4209b 378 PENDING_KOMODO_TX += pax->komodoshis;
05d91eb2 379 printf(" vout.%u DEPOSIT %.8f <- pax.%s pending %.8f | ",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,dstr(PENDING_KOMODO_TX));
fbc4209b 380 }
e5430f52 381 if ( numvouts++ >= 64 )
382 break;
429dabb5 383 }
384 if ( numvouts > 1 )
385 {
0e3472c1 386 if ( tokomodo != 0 )
27bf3c5e 387 strcpy(symbol,(char *)"KMD");
9ef47e29 388 for (i=0; symbol[i]!=0; i++)
389 data[len++] = symbol[i];
a5ad8f02 390 data[len++] = 0;
77be6cd9 391 opretlen = komodo_opreturnscript(opret,opcode,data,len);
50824530 392 txNew->vout.resize(numvouts+1);
393 txNew->vout[numvouts].nValue = 0;
394 txNew->vout[numvouts].scriptPubKey.resize(opretlen);
395 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
429dabb5 396 memcpy(script,opret,opretlen);
23d955d4 397 printf("MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen);
d9a9d562 398 return(1);
9ef47e29 399 }
d9a9d562 400 return(0);
429dabb5 401}
402
8f64a3a9 403int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
557d9a23 404{
b89ab77c 405 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]; int64_t values[64],srcvalues[64]; struct pax_transaction *pax;
1ed46112 406 memset(baseids,0xff,sizeof(baseids));
407 memset(values,0,sizeof(values));
b89ab77c 408 memset(srcvalues,0,sizeof(srcvalues));
f360ea0a 409 memset(rmd160s,0,sizeof(rmd160s));
1ed46112 410 memset(kmdheights,0,sizeof(kmdheights));
411 memset(otherheights,0,sizeof(otherheights));
557d9a23 412 n = block.vtx[0].vout.size();
413 script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
12d47153 414 if ( n <= 2 || script[0] != 0x6a )
415 return(0);
416 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
77be6cd9 417 if ( ASSETCHAINS_SYMBOL[0] == 0 )
418 {
85de2861 419 //for (i=0; i<opretlen; i++)
420 // printf("%02x",script[i]);
421 //printf(" height.%d checkdeposit n.%d [%02x] [%c] %d vs %d\n",height,n,script[0],script[offset],script[offset],'X');
77be6cd9 422 opcode = 'X';
09a235e1 423 strcpy(symbol,(char *)"KMD");
77be6cd9 424 }
425 else
426 {
427 strcpy(symbol,ASSETCHAINS_SYMBOL);
428 opcode = 'I';
429 }
430 if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
557d9a23 431 {
b89ab77c 432 if ( (num= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,&script[offset],opretlen,opcode == 'X')) > 0 )
557d9a23 433 {
434 for (i=1; i<n-1; i++)
435 {
dfe40c43 436 if ( (pax= komodo_paxfind(txids[i-1],vouts[i-1])) != 0 )
5a2c5b6f 437 {
ca75995d 438 pax->type = opcode;
f10b8e2e 439 if ( ((opcode == 'I' && pax->fiatoshis == block.vtx[0].vout[i].nValue) || (opcode == 'X' && pax->komodoshis == block.vtx[0].vout[i].nValue)) )
440 {
c35c0838 441 if ( pax->marked != 0 && height >= 80820 )
f10b8e2e 442 errs++;
443 else matched++;
8c6619bc 444 if ( opcode == 'X' )
1d996b5f 445 printf("errs.%d i.%d match %.8f == %.8f pax.%p\n",errs,i,dstr(pax != 0 ? pax->komodoshis:-1),dstr(block.vtx[0].vout[i].nValue),pax);
f10b8e2e 446 }
447 else
448 {
05d91eb2 449 if ( opcode == 'X' )
450 {
451 for (j=0; j<32; j++)
947d956f 452 printf("%02x",((uint8_t *)&txids[i-1])[j]);
453 printf(" cant paxfind X txid\n");
05d91eb2 454 }
f10b8e2e 455 }
5a2c5b6f 456 }
cb040aa1 457 else
458 {
05d91eb2 459 if ( opcode == 'X' )
460 {
1d996b5f 461 hash = block.GetHash();
05d91eb2 462 for (j=0; j<32; j++)
947d956f 463 printf("%02x",((uint8_t *)&hash)[j]);
464 printf(" ht.%d blockhash X couldnt find vout.[%d]\n",height,i);
ce94767e 465 } else if ( opcode == 'I' )
466 matched++;
cb040aa1 467 }
557d9a23 468 }
81041ab2 469 if ( matched != num )
470 {
0dca8d4c 471 // can easily happen depending on order of loading
7eb21300 472 if ( height > 100000 && opcode == 'X' )
52b2e68d 473 {
474 printf("REJECT: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num);
475 return(-1);
476 }
81041ab2 477 }
557d9a23 478 }
b98053e2 479 //printf("opretlen.%d num.%d\n",opretlen,num);
557d9a23 480 }
481 return(0);
482}
483
e97061cb 484int32_t komodo_paxcmp(uint64_t value,uint64_t checkvalue,uint64_t seed)
485{
e5e64f55 486 int32_t ratio;
e97061cb 487 if ( seed == 0 )
488 {
02862b72 489 ratio = ((value << 10) / checkvalue);
e5e64f55 490 if ( ratio >= 1023 && ratio <= 1025 )
491 return(0);
02862b72 492 else
493 {
494 printf("value %lld vs checkvalue %lld -> ratio.%d\n",(long long)value,(long long)checkvalue,ratio);
495 return(-1);
496 }
e97061cb 497 }
498 return(value != checkvalue);
499}
500
cd26c1f3 501const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source)
429dabb5 502{
e97061cb 503 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; struct komodo_state *basesp; double diff;
1ed46112 504 const char *typestr = "unknown";
505 memset(baseids,0xff,sizeof(baseids));
506 memset(values,0,sizeof(values));
06cfcf31 507 memset(srcvalues,0,sizeof(srcvalues));
f360ea0a 508 memset(rmd160s,0,sizeof(rmd160s));
1ed46112 509 memset(kmdheights,0,sizeof(kmdheights));
510 memset(otherheights,0,sizeof(otherheights));
95db8239 511 tokomodo = (komodo_is_issuer() == 0);
0a611c1e 512 if ( opretbuf[0] == 'D' )
64bb0834 513 {
05a37d87 514 if ( opretlen == 38 ) // any KMD tx
64bb0834 515 {
05a37d87 516 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
abdd8672 517 memset(base,0,sizeof(base));
518 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
abdd8672 519 bitcoin_address(coinaddr,addrtype,rmd160,20);
520 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
521 typestr = "deposit";
1c6e217d 522 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);
05a37d87 523 if ( kmdheight <= height )
6e576848 524 {
c98b3a08 525 for (i=0; i<32; i++)
526 printf("%02x",((uint8_t *)&txid)[i]);
527 printf(" <- txid.v%u ",vout);
528 for (i=0; i<33; i++)
529 printf("%02x",pubkey33[i]);
530 printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
f5e3cdfa 531 didstats = 0;
e69107f8 532 if ( komodo_paxcmp(value,checktoshis,seed) == 0 )
05a37d87 533 {
3f9cad6d 534 if ( (pax= komodo_paxfind(txid,vout)) == 0 )
05a37d87 535 {
c98b3a08 536 if ( (basesp= komodo_stateptrget(base)) != 0 )
05a37d87 537 {
c98b3a08 538 basesp->deposited += fiatoshis;
22a31d52 539 didstats = 1;
c98b3a08 540 printf("########### %p deposited %s += %.8f\n",basesp,base,dstr(fiatoshis));
541 }
645ecdd5 542 //if ( strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) //tokomodo == 0 &&
f5e3cdfa 543 {
51e5d952 544 komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,'D',kmdheight,height,(char *)"KMD",0);
f5e3cdfa 545 }
3f9cad6d 546 }
51e5d952 547 if ( (pax= komodo_paxfind(txid,vout,'D')) != 0 )
3f9cad6d 548 {
22a31d52 549 if ( didstats == 0 && pax->didstats == 0 )
3f9cad6d 550 {
ca75995d 551 if ( (basesp= komodo_stateptrget(base)) != 0 )
552 {
553 basesp->deposited += fiatoshis;
22a31d52 554 didstats = 1;
ca75995d 555 printf("########### %p deposited %s += %.8f\n",basesp,base,dstr(fiatoshis));
556 }
3f9cad6d 557 }
51e5d952 558 if ( didstats != 0 )
559 pax->didstats = 1;
3f9cad6d 560 }
8e3430ee 561 }
8bc1bcad 562 }
64bb0834 563 }
564 }
01d9ed2f 565 else if ( opretbuf[0] == 'W' && opretlen >= 38 )
566 {
567 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
568 memset(base,0,sizeof(base));
c3d94aec 569 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&komodoshis);
01d9ed2f 570 bitcoin_address(coinaddr,addrtype,rmd160,20);
c3d94aec 571 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,value);
01d9ed2f 572 typestr = "withdraw";
c3d94aec 573 printf("%s.height.%d vs height.%d check %.8f/%.8f vs %.8f tokomodo.%d %d seed.%llx -> (%s)\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);
f5e3cdfa 574 didstats = 0;
d506f4ec 575 if ( komodo_paxcmp(komodoshis,checktoshis,seed) == 0 )
01d9ed2f 576 {
51e5d952 577 if ( value != 0 && ((pax= komodo_paxfind(txid,vout,'W')) == 0 || pax->didstats == 0) )
0b556992 578 {
33319f82 579 if ( (basesp= komodo_stateptrget(base)) != 0 )
0fa85cc3 580 {
5ce8bf22 581 basesp->withdrawn += value;
f5e3cdfa 582 didstats = 1;
33319f82 583 printf("########### %p withdrawn %s += %.8f\n",basesp,base,dstr(value));
0fa85cc3 584 }
0b556992 585 printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height);
ea365a71 586 }
51e5d952 587 komodo_gateway_deposit(coinaddr,komodoshis,(char *)"KMD",value,rmd160,txid,vout,'W',kmdheight,height,source,0);
588 if ( (pax= komodo_paxfind(txid,vout,'W')) != 0 )
ca75995d 589 {
14941d4c 590 //if ( didstats != 0 )
591 // pax->didstats = 1;
ca75995d 592 pax->type = opretbuf[0];
593 }
a4c67285 594 }
595 }
596 else if ( tokomodo != 0 && opretbuf[0] == 'A' )
597 {
52b2e68d 598 if ( ASSETCHAINS_SYMBOL[0] != 0 )
826bfe51 599 {
600 for (i=0; i<opretlen; i++)
601 printf("%02x",opretbuf[i]);
74a9722e 602 printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()));
826bfe51 603 }
b89ab77c 604 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
a4c67285 605 {
606 for (i=0; i<n; i++)
607 {
b89ab77c 608 if ( baseids[i] < 0 )
609 continue;
f5e3cdfa 610 didstats = 0;
0cc6ad81 611 seed = 0;
992c3be5 612 checktoshis = komodo_paxprice(&seed,kmdheights[i],CURRENCIES[baseids[i]],(char *)"KMD",(uint64_t)srcvalues[i]);
613 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 614 for (j=0; j<32; j++)
615 printf("%02x",((uint8_t *)&txids[i])[j]);
71c9af7c 616 printf(" v%d %.8f k.%d ht.%d base.%d\n",vouts[i],dstr(values[i]),kmdheights[i],otherheights[i],baseids[i]);
51e5d952 617 if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 )
a4c67285 618 {
f360ea0a 619 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
51e5d952 620 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]);
042e3b9f 621 if ( srcvalues[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
0fa85cc3 622 {
3cbfd2b4 623 basesp->approved += srcvalues[i];
f5e3cdfa 624 didstats = 1;
ad16127c 625 printf("########### %p approved %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i]));
0fa85cc3 626 }
3cbfd2b4 627 printf(" i.%d (%s) <- %.8f ADDFLAG APPROVED\n",i,coinaddr,dstr(values[i]));
3f9cad6d 628 }
042e3b9f 629 else if ( pax->didstats == 0 && srcvalues[i] != 0 )
3f9cad6d 630 {
631 if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
632 {
633 basesp->approved += srcvalues[i];
634 didstats = 1;
635 printf("########### %p approved %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i]));
636 }
637 }
638 else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]);
51e5d952 639 if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) != 0 )
06d1a655 640 {
ca75995d 641 pax->type = opretbuf[0];
f97d3439 642 pax->approved = kmdheights[i];
14941d4c 643 //if ( didstats != 0 )
644 // pax->didstats = 1;
dfe40c43 645 printf(" i.%d approved.%d <<<<<<<<<<<<< APPROVED %p\n",i,kmdheights[i],pax);
06d1a655 646 }
ea365a71 647 }
0b556992 648 }
ecfc4f44 649 printf("extra.[%d] after %.8f\n",n,dstr(komodo_paxtotal()));
01d9ed2f 650 }
20e09846 651 else if ( opretbuf[0] == 'I' )
429dabb5 652 {
f5e3cdfa 653 if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
429dabb5 654 {
b89ab77c 655 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 )
429dabb5 656 {
63b289ad 657 for (i=0; i<n; i++)
ec35af90 658 {
f5e3cdfa 659 if ( baseids[i] < 0 )
b971ca89 660 {
661 printf("%d of %d illegal baseid.%d\n",i,n,baseids[i]);
f5e3cdfa 662 continue;
b971ca89 663 }
9ccd3f33 664 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
c6eca157 665 printf("ISSUE %s %.8f %.8f\n",CURRENCIES[baseids[i]],dstr(values[i]),dstr(srcvalues[i]));
51e5d952 666 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'I',height,0,CURRENCIES[baseids[i]],0);
667 if ( komodo_paxmark(height,txids[i],vouts[i],'I',height) == 0 )
f5e3cdfa 668 {
3cbfd2b4 669 }
51e5d952 670 if ( (pax= komodo_paxfind(txids[i],vouts[i],'I')) != 0 )
ca75995d 671 {
672 pax->type = opretbuf[0];
3f2d5618 673 strcpy(pax->source,(char *)&opretbuf[opretlen-4]);
ca75995d 674 }
ec35af90 675 }
c1bc1804 676 } else printf("opreturn none issued?\n");
b971ca89 677 } else printf("opreturn matches KMD? %s\n",(char *)&opretbuf[opretlen-4]);
429dabb5 678 }
ca3c6549 679 else if ( opretbuf[0] == 'X' )
680 {
4c58b338 681 printf("got X opreturn height.%d\n",height);
682 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
683 {
684 for (i=0; i<n; i++)
685 {
3f9cad6d 686 if ( baseids[i] < 0 )
687 continue;
4c58b338 688 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
19cba879 689 printf("X i.%d of %d: %.8f -> %s\n",i,n,dstr(values[i]),coinaddr);
51e5d952 690 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'X',height,0,(char *)"KMD",0);
691 if ( komodo_paxmark(height,txids[i],vouts[i],'X',height) == 0 )
c6eca157 692 ;
51e5d952 693 if ( (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 )
ca75995d 694 {
695 pax->type = opretbuf[0];
e649b292 696 if ( baseids[i] >= 0 && srcvalues[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 )
c6eca157 697 {
e649b292 698 basesp->redeemed += srcvalues[i];
14941d4c 699 //pax->didstats = 1;
c6eca157 700 }
ca75995d 701 }
4c58b338 702 }
e988a431 703 } else printf("komodo_issued_opreturn returned %d\n",n);
ca3c6549 704 }
64bb0834 705 return(typestr);
706}
707
30d79208 708void komodo_passport_iteration()
ab918767 709{
635dd34d 710 static long lastpos[34]; static char userpass[33][1024];
711 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 712 while ( KOMODO_INITDONE == 0 )
713 {
714 fprintf(stderr,"PASSPORT iteration waiting for KOMODO_INITDONE\n");
715 sleep(3);
716 }
635dd34d 717 refsp = komodo_stateptr(symbol,dest);
ab918767 718 if ( ASSETCHAINS_SYMBOL[0] == 0 )
67cd296f 719 refid = 33;
635dd34d 720 else refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0
3085016b 721 //printf("PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
52f1d788 722 for (baseid=0; baseid<=32; baseid++)
508b0d3c 723 {
ecf6256c 724 sp = 0;
725 isrealtime = 0;
f789edde 726 base = (char *)CURRENCIES[baseid];
c1bc1804 727if ( strcmp("EUR",base) != 0 && baseid < 32 )
728 continue;
95a6016d 729 if ( baseid+1 != refid )
ab918767 730 {
055db9b6 731 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate");
52f1d788 732 komodo_nameset(symbol,dest,base);
30376a70 733 //port = komodo_port(base,10,&magic) + 1;
52f1d788 734 if ( (fp= fopen(fname,"rb")) != 0 && (sp= komodo_stateptrget(symbol)) != 0 )
ab918767 735 {
52f1d788 736 fseek(fp,0,SEEK_END);
737 if ( ftell(fp) > lastpos[baseid] )
ab918767 738 {
c7caa0da 739 //printf("passport refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base);
52f1d788 740 fseek(fp,lastpos[baseid],SEEK_SET);
9534c87a 741 while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
52f1d788 742 ;
743 lastpos[baseid] = ftell(fp);
a9bb99d2 744 //printf("from.(%s) lastpos[%s] %ld\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid]);
52f1d788 745 } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp));
746 fclose(fp);
ecf6256c 747 }
a7ef556c 748 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
749 if ( (fp= fopen(fname,"rb")) != 0 )
750 {
751 if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) )
752 {
f1db88ea 753 sp->CURRENT_HEIGHT = buf[0];
fe0210c0 754 if ( buf[0] != 0 && buf[0] == buf[1] && buf[2] > time(NULL)-60 )
6eb41a2f 755 {
a7ef556c 756 isrealtime = 1;
6eb41a2f 757 RTmask |= (1LL << baseid);
635dd34d 758 memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
140fccf8 759 } //else fprintf(stderr,"%s not RT\n",base);
bffa91b2 760 } //else fprintf(stderr,"%s size error RT\n",base);
a7ef556c 761 fclose(fp);
209dc96c 762 } else fprintf(stderr,"%s open error RT\n",base);
a7ef556c 763 }
635dd34d 764 else
a7ef556c 765 {
766 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
767 if ( (fp= fopen(fname,"wb")) != 0 )
768 {
30376a70 769 buf[0] = (uint32_t)chainActive.Tip()->nHeight;
a7ef556c 770 buf[1] = (uint32_t)komodo_longestchain();
771 if ( buf[0] != 0 && buf[0] == buf[1] )
6eb41a2f 772 {
a7ef556c 773 buf[2] = (uint32_t)time(NULL);
48a3cd18 774 RTmask |= (1LL << baseid) | 1;
635dd34d 775 memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
776 if ( refid != 0 )
777 memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0]));
6eb41a2f 778 }
a7ef556c 779 if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) )
780 fprintf(stderr,"[%s] %s error writing realtime\n",ASSETCHAINS_SYMBOL,base);
781 fclose(fp);
f789edde 782 } else fprintf(stderr,"%s create error RT\n",base);
ecf6256c 783 }
f1db88ea 784 if ( sp != 0 && isrealtime == 0 )
785 refsp->RTbufs[0][2] = 0;
52f1d788 786 }
635dd34d 787 refsp->RTmask = RTmask;
4767f1fc 788 KOMODO_PASSPORT_INITDONE = 1;
ab918767 789}
ab918767 790
This page took 0.247864 seconds and 4 git commands to generate.