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
6210f328 18struct pax_transaction *komodo_paxfind(struct pax_transaction *space,uint256 txid,uint16_t vout)
19{
20 struct pax_transaction *pax;
21 pthread_mutex_lock(&komodo_mutex);
22 HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
23 if ( pax != 0 )
24 memcpy(space,pax,sizeof(*pax));
25 pthread_mutex_unlock(&komodo_mutex);
26 return(pax);
27}
28
b5bf65f5 29struct pax_transaction *komodo_paxmark(int32_t height,struct pax_transaction *space,uint256 txid,uint16_t vout,int32_t mark)
6210f328 30{
31 struct pax_transaction *pax;
32 pthread_mutex_lock(&komodo_mutex);
33 HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
2ab43fd2 34 if ( pax == 0 )
35 {
36 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
37 pax->txid = txid;
38 pax->vout = vout;
39 HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
32d525eb 40 //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark);
2ab43fd2 41 }
6210f328 42 if ( pax != 0 )
43 {
44 pax->marked = mark;
f1e6bb01 45 //int32_t i; for (i=0; i<32; i++)
46 // printf("%02x",((uint8_t *)&txid)[i]);
47 //printf(" paxmark.ht %d vout%d\n",mark,vout);
6210f328 48 memcpy(space,pax,sizeof(*pax));
49 }
50 pthread_mutex_unlock(&komodo_mutex);
51 return(pax);
52}
429dabb5 53
cd26c1f3 54void komodo_gateway_deposit(char *coinaddr,uint64_t value,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,int32_t height,int32_t otherheight,char *source) // assetchain context
d43c7a93 55{
bffa91b2 56 struct pax_transaction *pax; int32_t addflag = 0; struct komodo_state *sp; char str[16],dest[16];
57 sp = komodo_stateptr(str,dest);
d43c7a93 58 pthread_mutex_lock(&komodo_mutex);
59 HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
60 if ( pax == 0 )
b2d64a0f 61 {
d43c7a93 62 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
8d52488f 63 pax->txid = txid;
64 pax->vout = vout;
65 HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
7961668b 66 addflag = 1;
1fc75a27 67 if ( ASSETCHAINS_SYMBOL[0] == 0 )
68 {
69 int32_t i; for (i=0; i<32; i++)
70 printf("%02x",((uint8_t *)&txid)[i]);
71 printf(" v.%d [%s] kht.%d ht.%d create pax.%p\n",vout,ASSETCHAINS_SYMBOL,height,otherheight,pax);
72 }
b2d64a0f 73 }
8d52488f 74 pthread_mutex_unlock(&komodo_mutex);
d43c7a93 75 if ( coinaddr != 0 )
76 {
77 strcpy(pax->coinaddr,coinaddr);
9ccd3f33 78 if ( value != 0 )
79 pax->komodoshis = value;
80 if ( symbol != 0 )
81 strcpy(pax->symbol,symbol);
cd26c1f3 82 if ( source != 0 )
83 strcpy(pax->source,source);
9ccd3f33 84 if ( fiatoshis != 0 )
85 pax->fiatoshis = fiatoshis;
86 if ( rmd160 != 0 )
87 memcpy(pax->rmd160,rmd160,20);
88 if ( height != 0 )
89 pax->height = height;
90 if ( otherheight != 0 )
91 pax->otherheight = otherheight;
d43c7a93 92 if ( pax->marked == 0 )
7961668b 93 {
94 if ( addflag != 0 )
cd26c1f3 95 printf("[%s] addflag.%d ADD %s %s %.8f -> %s TO PAX ht.%d otherht.%d total %.8f\n",ASSETCHAINS_SYMBOL,addflag,ASSETCHAINS_SYMBOL[0]==0?"WITHDRAW":"DEPOSIT",symbol,dstr(ASSETCHAINS_SYMBOL[0]==0?pax->komodoshis:pax->fiatoshis),pax->coinaddr,pax->height,pax->otherheight,dstr(komodo_paxtotal()));
7961668b 96 }
0dca8d4c 97 //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 98 }
99 else
100 {
101 pax->marked = height;
1fc75a27 102 printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight);
d43c7a93 103 }
d43c7a93 104}
105
7cc6844b 106int32_t komodo_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transaction *pax)
107{
108 int32_t i,len = 0;
109 for (i=0; i<32; i++)
110 opretbuf[len++] = ((uint8_t *)&pax->txid)[i];
111 opretbuf[len++] = pax->vout & 0xff;
112 opretbuf[len++] = (pax->vout >> 8) & 0xff;
7cc6844b 113 len += iguana_rwnum(1,&opretbuf[len],sizeof(pax->fiatoshis),&pax->komodoshis);
114 len += iguana_rwnum(1,&opretbuf[len],sizeof(pax->fiatoshis),&pax->fiatoshis);
3b024daf 115 len += iguana_rwnum(1,&opretbuf[len],sizeof(pax->height),&pax->height);
7cc6844b 116 len += iguana_rwnum(1,&opretbuf[len],sizeof(pax->otherheight),&pax->otherheight);
117 memcpy(&opretbuf[len],pax->rmd160,20), len += 20;
118 for (i=0; i<4; i++)
cd26c1f3 119 opretbuf[len++] = pax->source[i];
7cc6844b 120 return(len);
121}
122
f360ea0a 123int32_t komodo_issued_opreturn(char *base,uint256 *txids,uint16_t *vouts,int64_t *values,int32_t *kmdheights,int32_t *otherheights,int8_t *baseids,uint8_t *rmd160s,uint8_t *opretbuf,int32_t opretlen,int32_t iskomodo)
63b289ad 124{
e8433f7a 125 struct pax_transaction p; int32_t i,n=0,j,len=0,incr,height,otherheight; uint8_t rmd160[20]; uint64_t fiatoshis; char symbol[16];
63b289ad 126 for (i=0; i<4; i++)
127 base[i] = opretbuf[opretlen-4+i];
986df802 128 if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
63b289ad 129 {
130 opretbuf++, opretlen--;
ffec138e 131 incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4));
e8433f7a 132 for (n=0; n<opretlen/incr; n++)
63b289ad 133 {
c7caa0da 134 //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
05d91eb2 135 if ( iskomodo != 0 )
136 {
e8433f7a 137 memset(&p,0,sizeof(p));
ffec138e 138 len += komodo_rwapproval(0,&opretbuf[len],&p);
1ed46112 139 if ( values != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 )
140 {
a3f1fb69 141 values[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? p.komodoshis : p.fiatoshis;
142 kmdheights[n] = p.height;
143 otherheights[n] = p.otherheight;
7cc6844b 144 memcpy(&rmd160s[n * 20],p.rmd160,20);
a3f1fb69 145 baseids[n] = komodo_baseid(p.symbol);
1ed46112 146 }
ab918767 147 printf(">>>>>>> iskomodo X: (%s) fiat %.8f kmdheight.%d other.%d\n",symbol,dstr(fiatoshis),height,otherheight);
05d91eb2 148 }
7cc6844b 149 else
150 {
151 for (j=0; j<32; j++)
152 {
153 ((uint8_t *)&txids[n])[j] = opretbuf[len++];
154 //printf("%02x",((uint8_t *)&txids[n])[j]);
155 }
156 vouts[n] = opretbuf[len++];
157 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
158 }
63b289ad 159 }
160 }
161 return(n);
162}
163
f1db88ea 164uint64_t komodo_paxtotal()
165{
bffa91b2 166 struct pax_transaction *pax,*tmp; int32_t ht; uint64_t total = 0;
48a3cd18 167 if ( komodo_isrealtime(&ht) == 0 )
f1db88ea 168 return(0);
169 HASH_ITER(hh,PAX,pax,tmp)
170 {
10694486 171 //printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
f1db88ea 172 if ( pax->marked == 0 )
173 {
174 if ( komodo_is_issuer() != 0 )
175 total += pax->fiatoshis;
176 else total += pax->komodoshis;
177 }
178 }
986df802 179 //printf("paxtotal %.8f\n",dstr(total));
f1db88ea 180 return(total);
181}
182
7cc6844b 183int32_t komodo_pending_withdraws(char *opretstr)
f360ea0a 184{
7cc6844b 185 struct pax_transaction *pax,*tmp; uint8_t opretbuf[10000]; int32_t ht,len=0; uint64_t total = 0;
f360ea0a 186 if ( komodo_isrealtime(&ht) == 0 || ASSETCHAINS_SYMBOL[0] != 0 )
187 return(0);
188 HASH_ITER(hh,PAX,pax,tmp)
189 {
23256cca 190 printf("pax %s marked.%u approved.%u\n",pax->symbol,pax->marked,pax->approved);
f360ea0a 191 if ( pax->marked == 0 && strcmp((char *)"KMD",pax->symbol) == 0 && pax->approved == 0 )
192 {
193 // add 'A' opreturn entry
7cc6844b 194 if ( len == 0 )
195 opretbuf[len++] = 'A';
196 len += komodo_rwapproval(1,&opretbuf[len],pax);
f360ea0a 197 }
198 }
7cc6844b 199 if ( len > 0 )
200 init_hexbytes_noT(opretstr,opretbuf,len);
201 else opretstr[0] = 0;
f360ea0a 202 return(len);
203}
a4c67285 204
635dd34d 205int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo)
429dabb5 206{
f1db88ea 207 struct pax_transaction *pax,*tmp; char symbol[16],dest[16]; uint8_t *script,opcode,opret[10000],data[10000]; int32_t i,baseid,ht,len=0,opretlen=0,numvouts=1; struct komodo_state *sp; uint64_t mask;
635dd34d 208 sp = komodo_stateptr(symbol,dest);
27bf3c5e 209 strcpy(symbol,base);
429dabb5 210 PENDING_KOMODO_TX = 0;
a3f23ee2 211 if ( tokomodo == 0 )
48e96ba1 212 {
77be6cd9 213 opcode = 'I';
48a3cd18 214 if ( komodo_isrealtime(&ht) == 0 )
48e96ba1 215 return(0);
48e96ba1 216 }
77be6cd9 217 else opcode = 'X';
c169f64b 218 HASH_ITER(hh,PAX,pax,tmp)
429dabb5 219 {
986df802 220 //printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
43f79eee 221 if ( pax->marked != 0 || strcmp(pax->symbol,symbol) != 0 || (strcmp(symbol,"KMD") == 0 && pax->approved == 0) )
e5430f52 222 continue;
986df802 223 //if ( ASSETCHAINS_SYMBOL[0] != 0 )
635dd34d 224 printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
e5430f52 225 txNew->vout.resize(numvouts+1);
1fc75a27 226 txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis;
e5430f52 227 txNew->vout[numvouts].scriptPubKey.resize(25);
228 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
229 *script++ = 0x76;
230 *script++ = 0xa9;
231 *script++ = 20;
232 memcpy(script,pax->rmd160,20), script += 20;
233 *script++ = 0x88;
234 *script++ = 0xac;
235 for (i=0; i<32; i++)
236 {
1938746c 237 //printf("%02x",((uint8_t *)&pax->txid)[i]);
e5430f52 238 data[len++] = ((uint8_t *)&pax->txid)[i];
429dabb5 239 }
e5430f52 240 data[len++] = pax->vout & 0xff;
241 data[len++] = (pax->vout >> 8) & 0xff;
a3f23ee2 242 if ( tokomodo == 0 )
e5430f52 243 PENDING_KOMODO_TX += pax->fiatoshis;
fbc4209b 244 else
245 {
1fc75a27 246 //[{"prev_hash":"5d5c9a49489b558de9e84f991f996dedaae6b9d0f157f82b2fec64662476d5cf","prev_vout":2,"EUR":0.10000000,"fiat":"EUR","kmdheight":57930,"height":153,"KMD":0.78329000,"address":"RDhEGYScNQYetCyG75Kf8Fg61UWPdwc1C5","rmd160":"306c507eea639e7220b3069ed9f49f3bc97eaca1"}]
247 len += iguana_rwnum(1,&data[len],sizeof(pax->fiatoshis),&pax->fiatoshis);
248 len += iguana_rwnum(1,&data[len],sizeof(pax->height),&pax->height);
249 len += iguana_rwnum(1,&data[len],sizeof(pax->otherheight),&pax->otherheight);
635dd34d 250 for (i=0; pax->symbol[i]!=0&&i<3; i++) // must be 3 letter currency
1fc75a27 251 data[len++] = pax->symbol[i];
252 data[len++] = 0;
fbc4209b 253 PENDING_KOMODO_TX += pax->komodoshis;
05d91eb2 254 printf(" vout.%u DEPOSIT %.8f <- pax.%s pending %.8f | ",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,dstr(PENDING_KOMODO_TX));
fbc4209b 255 }
e5430f52 256 if ( numvouts++ >= 64 )
257 break;
429dabb5 258 }
259 if ( numvouts > 1 )
260 {
0e3472c1 261 if ( tokomodo != 0 )
27bf3c5e 262 strcpy(symbol,(char *)"KMD");
9ef47e29 263 for (i=0; symbol[i]!=0; i++)
264 data[len++] = symbol[i];
a5ad8f02 265 data[len++] = 0;
77be6cd9 266 opretlen = komodo_opreturnscript(opret,opcode,data,len);
50824530 267 txNew->vout.resize(numvouts+1);
268 txNew->vout[numvouts].nValue = 0;
269 txNew->vout[numvouts].scriptPubKey.resize(opretlen);
270 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
429dabb5 271 memcpy(script,opret,opretlen);
23d955d4 272 printf("MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen);
d9a9d562 273 return(1);
9ef47e29 274 }
d9a9d562 275 return(0);
429dabb5 276}
277
8f64a3a9 278int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
557d9a23 279{
f360ea0a 280 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]; struct pax_transaction *pax,space;
1ed46112 281 memset(baseids,0xff,sizeof(baseids));
282 memset(values,0,sizeof(values));
f360ea0a 283 memset(rmd160s,0,sizeof(rmd160s));
1ed46112 284 memset(kmdheights,0,sizeof(kmdheights));
285 memset(otherheights,0,sizeof(otherheights));
557d9a23 286 n = block.vtx[0].vout.size();
287 script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
12d47153 288 if ( n <= 2 || script[0] != 0x6a )
289 return(0);
290 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
77be6cd9 291 if ( ASSETCHAINS_SYMBOL[0] == 0 )
292 {
85de2861 293 //for (i=0; i<opretlen; i++)
294 // printf("%02x",script[i]);
295 //printf(" height.%d checkdeposit n.%d [%02x] [%c] %d vs %d\n",height,n,script[0],script[offset],script[offset],'X');
77be6cd9 296 opcode = 'X';
09a235e1 297 strcpy(symbol,(char *)"KMD");
77be6cd9 298 }
299 else
300 {
301 strcpy(symbol,ASSETCHAINS_SYMBOL);
302 opcode = 'I';
303 }
304 if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
557d9a23 305 {
f360ea0a 306 if ( (num= komodo_issued_opreturn(base,txids,vouts,values,kmdheights,otherheights,baseids,rmd160s,&script[offset],opretlen,opcode == 'X')) > 0 )
557d9a23 307 {
308 for (i=1; i<n-1; i++)
309 {
f10b8e2e 310 if ( (pax= komodo_paxfind(&space,txids[i-1],vouts[i-1])) != 0 )
5a2c5b6f 311 {
f10b8e2e 312 if ( ((opcode == 'I' && pax->fiatoshis == block.vtx[0].vout[i].nValue) || (opcode == 'X' && pax->komodoshis == block.vtx[0].vout[i].nValue)) )
313 {
314 if ( pax->marked != 0 )
315 errs++;
316 else matched++;
98ecae42 317 if ( 0 && opcode == 'X' )
05d91eb2 318 printf("errs.%d i.%d match %.8f == %.8f\n",errs,i,dstr(pax != 0 ? pax->fiatoshis:-1),dstr(block.vtx[0].vout[i].nValue));
43f79eee 319 komodo_paxmark(height,&space,txids[i-1],vouts[i-1],height);
f10b8e2e 320 }
321 else
322 {
323 hash = block.GetHash();
05d91eb2 324 if ( opcode == 'X' )
325 {
326 for (j=0; j<32; j++)
327 printf("%02x",((uint8_t *)&hash)[j]);
1ed46112 328 printf(" ht.%d blockhash X couldnt find vout.[%d]\n",height,i);
c246f72f 329 // validate amount! via fiat chain
05d91eb2 330 }
f10b8e2e 331 }
5a2c5b6f 332 }
cb040aa1 333 else
334 {
05d91eb2 335 if ( opcode == 'X' )
336 {
ce94767e 337 matched++;
05d91eb2 338 for (j=0; j<32; j++)
339 printf("%02x",((uint8_t *)&txids[i-1])[j]);
1ed46112 340 printf(" cant paxfind X txid\n");
c246f72f 341 // validate amount! via fiat chain
ce94767e 342 } else if ( opcode == 'I' )
343 matched++;
cb040aa1 344 }
557d9a23 345 }
81041ab2 346 if ( matched != num )
347 {
0dca8d4c 348 // can easily happen depending on order of loading
ce94767e 349 if ( height > 60000 )
350 printf("WARNING: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num);
81041ab2 351 }
557d9a23 352 }
b98053e2 353 //printf("opretlen.%d num.%d\n",opretlen,num);
557d9a23 354 }
355 return(0);
356}
357
cd26c1f3 358const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source)
429dabb5 359{
f360ea0a 360 uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33]; int32_t i,j,n,len,tokomodo,kmdheight,otherheights[64],kmdheights[64]; int8_t baseids[64]; char base[4],coinaddr[64],destaddr[64]; struct pax_transaction space; uint256 txids[64]; uint16_t vouts[64]; uint64_t convtoshis,seed; int64_t fiatoshis,komodoshis,checktoshis,values[64]; struct pax_transaction *pax;
1ed46112 361 const char *typestr = "unknown";
362 memset(baseids,0xff,sizeof(baseids));
363 memset(values,0,sizeof(values));
f360ea0a 364 memset(rmd160s,0,sizeof(rmd160s));
1ed46112 365 memset(kmdheights,0,sizeof(kmdheights));
366 memset(otherheights,0,sizeof(otherheights));
95db8239 367 tokomodo = (komodo_is_issuer() == 0);
1938746c 368 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
ad7b6271 369 {
370 for (i=0; i<opretlen; i++)
371 printf("%02x",opretbuf[i]);
372 printf(" opret[%c] else path tokomodo.%d ht.%d\n",opretbuf[0],tokomodo,height);
373 }
0a611c1e 374 if ( opretbuf[0] == 'D' )
64bb0834 375 {
05a37d87 376 if ( opretlen == 38 ) // any KMD tx
64bb0834 377 {
05a37d87 378 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
abdd8672 379 memset(base,0,sizeof(base));
380 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
abdd8672 381 bitcoin_address(coinaddr,addrtype,rmd160,20);
382 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
383 typestr = "deposit";
1ed46112 384 printf("%s kmdheight.%d vs height.%d check %.8f vs %.8f tokomodo.%d %d seed.%llx\n",ASSETCHAINS_SYMBOL,kmdheight,height,dstr(checktoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed);
05a37d87 385 if ( kmdheight <= height )
6e576848 386 {
ab918767 387 if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
05a37d87 388 {
ab918767 389 for (i=0; i<32; i++)
390 printf("%02x",((uint8_t *)&txid)[i]);
391 printf(" <- txid.v%u ",vout);
392 for (i=0; i<33; i++)
393 printf("%02x",pubkey33[i]);
394 printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
f1db88ea 395 if ( value == checktoshis )//value >= checktoshis || (seed == 0 && diff < .01) )
05a37d87 396 {
ab918767 397 if ( komodo_paxfind(&space,txid,vout) == 0 )
05a37d87 398 {
cd26c1f3 399 komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,kmdheight,height,(char *)"KMD");
ab918767 400 } else printf("duplicate deposit\n");
755ead98 401 }
8e3430ee 402 }
8bc1bcad 403 }
64bb0834 404 }
405 }
01d9ed2f 406 else if ( opretbuf[0] == 'W' && opretlen >= 38 )
407 {
408 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
409 memset(base,0,sizeof(base));
c3d94aec 410 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&komodoshis);
01d9ed2f 411 bitcoin_address(coinaddr,addrtype,rmd160,20);
c3d94aec 412 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,value);
01d9ed2f 413 typestr = "withdraw";
c3d94aec 414 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);
0b556992 415 if ( checktoshis == komodoshis )
01d9ed2f 416 {
0b556992 417 if ( (pax= komodo_paxfind(&space,txid,vout)) == 0 )
418 {
419 printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height);
ea365a71 420 }
cd26c1f3 421 komodo_gateway_deposit(coinaddr,komodoshis,(char *)"KMD",value,rmd160,txid,vout,kmdheight,height,source);
a4c67285 422 }
423 }
424 else if ( tokomodo != 0 && opretbuf[0] == 'A' )
425 {
cd26c1f3 426 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 )
a4c67285 427 {
428 for (i=0; i<n; i++)
429 {
cd26c1f3 430 if ( (pax= komodo_paxfind(&space,txids[i],vouts[i])) == 0 && baseids[i] >= 0 )
a4c67285 431 {
f360ea0a 432 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
cd26c1f3 433 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],kmdheights[i],otherheights[i],CURRENCIES[baseids[i]]);
a4c67285 434 } else pax->approved = kmdheights[i];
ea365a71 435 }
0b556992 436 }
01d9ed2f 437 }
a4c67285 438 else if ( tokomodo == 0 && opretbuf[0] == 'I' )
429dabb5 439 {
a4c67285 440 if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
429dabb5 441 {
9ccd3f33 442 if ( (n= komodo_issued_opreturn(base,txids,vouts,values,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 )
429dabb5 443 {
63b289ad 444 for (i=0; i<n; i++)
ec35af90 445 {
9ccd3f33 446 bitcoin_address(coinaddr,60,&rmd160s[i*20],20);
cd26c1f3 447 if ( komodo_paxmark(height,&space,txids[i],vouts[i],height) == 0 && baseids[i] >= 0 )
448 komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],height,0,CURRENCIES[baseids[i]]);
ec35af90 449 }
429dabb5 450 }
429dabb5 451 }
452 }
64bb0834 453 return(typestr);
454}
455
30d79208 456void komodo_passport_iteration()
ab918767 457{
635dd34d 458 static long lastpos[34]; static char userpass[33][1024];
459 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 460 while ( KOMODO_INITDONE == 0 )
461 {
462 fprintf(stderr,"PASSPORT iteration waiting for KOMODO_INITDONE\n");
463 sleep(3);
464 }
635dd34d 465 refsp = komodo_stateptr(symbol,dest);
ab918767 466 if ( ASSETCHAINS_SYMBOL[0] == 0 )
67cd296f 467 refid = 33;
635dd34d 468 else refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0
3085016b 469 //printf("PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid);
52f1d788 470 for (baseid=0; baseid<=32; baseid++)
ab918767 471 {
ecf6256c 472 sp = 0;
473 isrealtime = 0;
f789edde 474 base = (char *)CURRENCIES[baseid];
95a6016d 475 if ( baseid+1 != refid )
ab918767 476 {
055db9b6 477 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate");
52f1d788 478 komodo_nameset(symbol,dest,base);
30376a70 479 //port = komodo_port(base,10,&magic) + 1;
52f1d788 480 if ( (fp= fopen(fname,"rb")) != 0 && (sp= komodo_stateptrget(symbol)) != 0 )
ab918767 481 {
52f1d788 482 fseek(fp,0,SEEK_END);
483 if ( ftell(fp) > lastpos[baseid] )
ab918767 484 {
c7caa0da 485 //printf("passport refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base);
52f1d788 486 fseek(fp,lastpos[baseid],SEEK_SET);
9534c87a 487 while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 )
52f1d788 488 ;
489 lastpos[baseid] = ftell(fp);
a9bb99d2 490 //printf("from.(%s) lastpos[%s] %ld\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid]);
52f1d788 491 } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp));
492 fclose(fp);
ecf6256c 493 }
a7ef556c 494 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
495 if ( (fp= fopen(fname,"rb")) != 0 )
496 {
497 if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) )
498 {
f1db88ea 499 sp->CURRENT_HEIGHT = buf[0];
fe0210c0 500 if ( buf[0] != 0 && buf[0] == buf[1] && buf[2] > time(NULL)-60 )
6eb41a2f 501 {
a7ef556c 502 isrealtime = 1;
6eb41a2f 503 RTmask |= (1LL << baseid);
635dd34d 504 memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
140fccf8 505 } //else fprintf(stderr,"%s not RT\n",base);
bffa91b2 506 } //else fprintf(stderr,"%s size error RT\n",base);
a7ef556c 507 fclose(fp);
209dc96c 508 } else fprintf(stderr,"%s open error RT\n",base);
a7ef556c 509 }
635dd34d 510 else
a7ef556c 511 {
512 komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime");
513 if ( (fp= fopen(fname,"wb")) != 0 )
514 {
30376a70 515 buf[0] = (uint32_t)chainActive.Tip()->nHeight;
a7ef556c 516 buf[1] = (uint32_t)komodo_longestchain();
517 if ( buf[0] != 0 && buf[0] == buf[1] )
6eb41a2f 518 {
a7ef556c 519 buf[2] = (uint32_t)time(NULL);
48a3cd18 520 RTmask |= (1LL << baseid) | 1;
635dd34d 521 memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1]));
522 if ( refid != 0 )
523 memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0]));
6eb41a2f 524 }
a7ef556c 525 if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) )
526 fprintf(stderr,"[%s] %s error writing realtime\n",ASSETCHAINS_SYMBOL,base);
527 fclose(fp);
f789edde 528 } else fprintf(stderr,"%s create error RT\n",base);
ecf6256c 529 }
f1db88ea 530 if ( sp != 0 && isrealtime == 0 )
531 refsp->RTbufs[0][2] = 0;
52f1d788 532 }
635dd34d 533 refsp->RTmask = RTmask;
4767f1fc 534 KOMODO_PASSPORT_INITDONE = 1;
ab918767 535}
ab918767 536
This page took 0.16357 seconds and 4 git commands to generate.