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