]>
Commit | Line | Data |
---|---|---|
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 | struct pax_transaction | |
19 | { | |
20 | UT_hash_handle hh; | |
21 | uint256 txid; | |
22 | uint64_t komodoshis,fiatoshis; | |
23 | int32_t marked,height; | |
24 | uint16_t vout; | |
25 | char symbol[16],coinaddr[64]; uint8_t rmd160[20],shortflag; | |
26 | } *PAX; | |
27 | ||
28 | /*uint64_t komodo_paxtotal() | |
29 | { | |
30 | struct pax_transaction *pax,*tmp; uint64_t total = 0; | |
31 | HASH_ITER(hh,PAX,pax,tmp) | |
32 | { | |
33 | if ( pax->marked == 0 ) | |
34 | total += pax->fiatoshis; | |
35 | } | |
36 | return(total); | |
37 | }*/ | |
38 | ||
39 | uint64_t komodo_paxtotal() | |
40 | { | |
41 | struct pax_transaction *pax,*tmp; uint64_t total = 0; | |
42 | tmp = 0; | |
43 | while ( (pax= (struct pax_transaction *)PAX->hh.next) != 0 && pax != tmp ) | |
44 | { | |
45 | if ( pax->marked == 0 ) | |
46 | total += pax->fiatoshis; | |
47 | tmp = pax; | |
48 | pax = (struct pax_transaction *)pax->hh.next; | |
49 | } | |
50 | return(total); | |
51 | } | |
52 | ||
53 | struct pax_transaction *komodo_paxfind(struct pax_transaction *space,uint256 txid,uint16_t vout) | |
54 | { | |
55 | struct pax_transaction *pax; | |
56 | pthread_mutex_lock(&komodo_mutex); | |
57 | HASH_FIND(hh,PAX,&txid,sizeof(txid),pax); | |
58 | if ( pax != 0 ) | |
59 | memcpy(space,pax,sizeof(*pax)); | |
60 | pthread_mutex_unlock(&komodo_mutex); | |
61 | return(pax); | |
62 | } | |
63 | ||
64 | struct pax_transaction *komodo_paxmark(struct pax_transaction *space,uint256 txid,uint16_t vout,int32_t mark) | |
65 | { | |
66 | struct pax_transaction *pax; | |
67 | pthread_mutex_lock(&komodo_mutex); | |
68 | HASH_FIND(hh,PAX,&txid,sizeof(txid),pax); | |
69 | if ( pax == 0 ) | |
70 | { | |
71 | pax = (struct pax_transaction *)calloc(1,sizeof(*pax)); | |
72 | pax->txid = txid; | |
73 | pax->vout = vout; | |
74 | HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax); | |
75 | } | |
76 | if ( pax != 0 ) | |
77 | { | |
78 | pax->marked = mark; | |
79 | int32_t i; for (i=0; i<32; i++) | |
80 | printf("%02x",((uint8_t *)&txid)[i]); | |
81 | printf(" paxmark.ht %d vout%d\n",mark,vout); | |
82 | memcpy(space,pax,sizeof(*pax)); | |
83 | } | |
84 | pthread_mutex_unlock(&komodo_mutex); | |
85 | return(pax); | |
86 | } | |
87 | ||
88 | void komodo_gateway_deposit(char *coinaddr,uint64_t value,int32_t shortflag,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,int32_t height) // assetchain context | |
89 | { | |
90 | struct pax_transaction *pax; int32_t addflag = 0; | |
91 | pthread_mutex_lock(&komodo_mutex); | |
92 | HASH_FIND(hh,PAX,&txid,sizeof(txid),pax); | |
93 | if ( pax == 0 ) | |
94 | { | |
95 | pax = (struct pax_transaction *)calloc(1,sizeof(*pax)); | |
96 | pax->txid = txid; | |
97 | pax->vout = vout; | |
98 | HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax); | |
99 | } | |
100 | pthread_mutex_unlock(&komodo_mutex); | |
101 | if ( coinaddr != 0 ) | |
102 | { | |
103 | strcpy(pax->coinaddr,coinaddr); | |
104 | pax->komodoshis = value; | |
105 | pax->shortflag = shortflag; | |
106 | strcpy(pax->symbol,symbol); | |
107 | pax->fiatoshis = fiatoshis; | |
108 | memcpy(pax->rmd160,rmd160,20); | |
109 | pax->height = height; | |
110 | if ( pax->marked == 0 ) | |
111 | printf("ADD DEPOSIT %s %.8f -> %s TO PAX ht.%d\n",symbol,dstr(fiatoshis),coinaddr,height); | |
112 | else printf("MARKED.%d DEPOSIT %s %.8f -> %s TO PAX ht.%d\n",pax->marked,symbol,dstr(fiatoshis),coinaddr,height); | |
113 | } | |
114 | else | |
115 | { | |
116 | pax->marked = height; | |
117 | printf("MARK DEPOSIT ht.%d\n",height); | |
118 | } | |
119 | } | |
120 | ||
121 | int32_t komodo_issued_opreturn(uint8_t *shortflagp,char *base,uint256 *txids,uint16_t *vouts,uint8_t *opretbuf,int32_t opretlen) | |
122 | { | |
123 | int32_t i,n=0,j,len; | |
124 | if ( opretbuf[opretlen-5] == '-' ) | |
125 | *shortflagp = 1; | |
126 | else *shortflagp = 0; | |
127 | for (i=0; i<4; i++) | |
128 | base[i] = opretbuf[opretlen-4+i]; | |
129 | if ( (strcmp(base,"KMD") == 0 && ASSETCHAINS_SYMBOL[0] == 0) || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) // shortflag | |
130 | { | |
131 | //printf("BASE.(%s) vs (%s)\n",base,ASSETCHAINS_SYMBOL); | |
132 | opretbuf++, opretlen--; | |
133 | for (n=len=0; n<opretlen/34; n++) | |
134 | { | |
135 | for (j=0; j<32; j++) | |
136 | { | |
137 | ((uint8_t *)&txids[n])[j] = opretbuf[len++]; | |
138 | //printf("%02x",((uint8_t *)&txids[n])[j]); | |
139 | } | |
140 | vouts[n] = opretbuf[len++]; | |
141 | vouts[n] = (opretbuf[len++] << 8) | vouts[n]; | |
142 | //printf(" issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen); | |
143 | } | |
144 | } | |
145 | return(n); | |
146 | } | |
147 | ||
148 | void komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,char *symbol) | |
149 | { | |
150 | struct pax_transaction *pax,*tmp; uint8_t *script,opcode,opret[10000],data[10000]; int32_t i,len=0,opretlen=0,numvouts=1; | |
151 | PENDING_KOMODO_TX = 0; | |
152 | if ( strcmp(symbol,"KMD") != 0 ) | |
153 | opcode = 'I'; | |
154 | else opcode = 'X'; | |
155 | tmp = 0; | |
156 | while ( (pax= (struct pax_transaction *)PAX->hh.next) != 0 && pax != tmp ) | |
157 | { | |
158 | if ( pax->marked != 0 ) | |
159 | continue; | |
160 | txNew->vout.resize(numvouts+1); | |
161 | txNew->vout[numvouts].nValue = pax->fiatoshis; | |
162 | txNew->vout[numvouts].scriptPubKey.resize(25); | |
163 | script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0]; | |
164 | *script++ = 0x76; | |
165 | *script++ = 0xa9; | |
166 | *script++ = 20; | |
167 | memcpy(script,pax->rmd160,20), script += 20; | |
168 | *script++ = 0x88; | |
169 | *script++ = 0xac; | |
170 | for (i=0; i<32; i++) | |
171 | { | |
172 | printf("%02x",((uint8_t *)&pax->txid)[i]); | |
173 | data[len++] = ((uint8_t *)&pax->txid)[i]; | |
174 | } | |
175 | data[len++] = pax->vout & 0xff; | |
176 | data[len++] = (pax->vout >> 8) & 0xff; | |
177 | printf(" vout.%u DEPOSIT %.8f <- paxdeposit.%s\n",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol); | |
178 | if ( strcmp(symbol,"KMD") != 0 ) | |
179 | PENDING_KOMODO_TX += pax->fiatoshis; | |
180 | else PENDING_KOMODO_TX += pax->komodoshis; | |
181 | if ( numvouts++ >= 64 ) | |
182 | break; | |
183 | tmp = pax; | |
184 | pax = (struct pax_transaction *)pax->hh.next; | |
185 | } | |
186 | if ( numvouts > 1 ) | |
187 | { | |
188 | if ( shortflag != 0 ) | |
189 | data[len++] = '-'; | |
190 | for (i=0; symbol[i]!=0; i++) | |
191 | data[len++] = symbol[i]; | |
192 | data[len++] = 0; | |
193 | opretlen = komodo_opreturnscript(opret,opcode,data,len); | |
194 | txNew->vout.resize(numvouts+1); | |
195 | txNew->vout[numvouts].nValue = 0; | |
196 | txNew->vout[numvouts].scriptPubKey.resize(opretlen); | |
197 | script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0]; | |
198 | memcpy(script,opret,opretlen); | |
199 | printf("total numvouts.%d %.8f opretlen.%d\n",numvouts,dstr(PENDING_KOMODO_TX),opretlen); | |
200 | } | |
201 | } | |
202 | ||
203 | int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing | |
204 | { | |
205 | int32_t i,j,n,num,opretlen,offset=1; uint256 hash,txids[64]; uint8_t shortflag; char symbol[16],base[16]; uint16_t vouts[64]; uint8_t *script,opcode; struct pax_transaction *pax,space; | |
206 | n = block.vtx[0].vout.size(); | |
207 | script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data(); | |
208 | if ( n <= 2 || script[0] != 0x6a ) | |
209 | return(0); | |
210 | offset += komodo_scriptitemlen(&opretlen,&script[offset]); | |
211 | //printf("checkdeposit n.%d [%02x] [%c] %d vs %d\n",n,script[0],script[offset],script[offset],'I'); | |
212 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) | |
213 | { | |
214 | opcode = 'X'; | |
215 | strcpy(symbol,"KMD"); | |
216 | } | |
217 | else | |
218 | { | |
219 | strcpy(symbol,ASSETCHAINS_SYMBOL); | |
220 | opcode = 'I'; | |
221 | } | |
222 | if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() ) | |
223 | { | |
224 | if ( (num= komodo_issued_opreturn(&shortflag,base,txids,vouts,&script[offset],opretlen)) > 0 ) | |
225 | { | |
226 | for (i=1; i<n-1; i++) | |
227 | { | |
228 | if ( (pax= komodo_paxfind(&space,txids[i-1],vouts[i-1])) != 0 && ((opcode == 'I' && pax->fiatoshis == block.vtx[0].vout[i].nValue) || (opcode == 'X' && pax->komodoshis == block.vtx[0].vout[i].nValue)) ) | |
229 | { | |
230 | //printf("i.%d match %.8f == %.8f\n",i,dstr(pax != 0 ? pax->fiatoshis:-1),dstr(block.vtx[0].vout[i].nValue)); | |
231 | komodo_paxmark(&space,txids[i-1],vouts[i-1],height); | |
232 | } | |
233 | else | |
234 | { | |
235 | hash = block.GetHash(); | |
236 | for (j=0; j<32; j++) | |
237 | printf("%02x",((uint8_t *)&hash)[j]); | |
238 | printf(" ht.%d blockhash couldnt find vout.[%d]\n",height,i); | |
239 | komodo_paxmark(&space,txids[i-1],vouts[i-1],height); | |
240 | } | |
241 | } | |
242 | } | |
243 | //printf("opretlen.%d num.%d\n",opretlen,num); | |
244 | } | |
245 | return(0); | |
246 | } | |
247 | ||
248 | const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout) | |
249 | { | |
250 | uint8_t rmd160[20],addrtype,shortflag,pubkey33[33]; int32_t i,j,n,len,tokomodo=0; char base[4],coinaddr[64],destaddr[64]; struct pax_transaction space; uint256 txids[64]; uint16_t vouts[64]; int64_t fiatoshis,checktoshis; const char *typestr = "unknown"; | |
251 | tokomodo = (komodo_is_issuer() == 0); | |
252 | if ( opretbuf[0] == 'D' ) | |
253 | { | |
254 | if ( opretlen == 34 ) // any KMD tx | |
255 | { | |
256 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) | |
257 | { | |
258 | for (i=0; i<opretlen; i++) | |
259 | printf("%02x",opretbuf[i]); | |
260 | printf(" opret[%c] tokomodo.%d\n",opretbuf[0],tokomodo); | |
261 | } | |
262 | memset(base,0,sizeof(base)); | |
263 | PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis); | |
264 | if ( fiatoshis < 0 ) | |
265 | fiatoshis = -fiatoshis; | |
266 | bitcoin_address(coinaddr,addrtype,rmd160,20); | |
267 | checktoshis = PAX_fiatdest(tokomodo,destaddr,pubkey33,coinaddr,height,base,fiatoshis); | |
268 | typestr = "deposit"; | |
269 | if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 && shortflag == ASSETCHAINS_SHORTFLAG ) | |
270 | { | |
271 | if ( shortflag == 0 ) | |
272 | { | |
273 | for (i=0; i<32; i++) | |
274 | printf("%02x",((uint8_t *)&txid)[i]); | |
275 | printf(" <- txid.v%u ",vout); | |
276 | for (i=0; i<33; i++) | |
277 | printf("%02x",pubkey33[i]); | |
278 | printf(" checkpubkey check %.8f v %.8f dest.(%s) height.%d\n",dstr(checktoshis),dstr(value),destaddr,height); | |
279 | if ( value >= checktoshis ) | |
280 | { | |
281 | if ( komodo_paxfind(&space,txid,vout) == 0 ) | |
282 | komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,height); | |
283 | } | |
284 | } | |
285 | else // short | |
286 | { | |
287 | for (i=0; i<opretlen; i++) | |
288 | printf("%02x",opretbuf[i]); | |
289 | printf(" opret[%c] tokomodo.%d value %.8f vs check %.8f\n",opretbuf[0],tokomodo,dstr(value),dstr(checktoshis)); | |
290 | if ( value <= checktoshis ) | |
291 | { | |
292 | ||
293 | } | |
294 | } | |
295 | } | |
296 | } | |
297 | } | |
298 | else if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 ) | |
299 | { | |
300 | if ( tokomodo == 0 && opretbuf[0] == 'I' ) // assetchain coinbase | |
301 | { | |
302 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) | |
303 | { | |
304 | for (i=0; i<opretlen; i++) | |
305 | printf("%02x",opretbuf[i]); | |
306 | printf(" opret[%c] tokomodo.%d\n",opretbuf[0],tokomodo); | |
307 | } | |
308 | if ( (n= komodo_issued_opreturn(&shortflag,base,txids,vouts,opretbuf,opretlen)) > 0 && shortflag == ASSETCHAINS_SHORTFLAG ) | |
309 | { | |
310 | for (i=0; i<n; i++) | |
311 | { | |
312 | for (j=0; j<32; j++) | |
313 | printf("%02x",((uint8_t *)&txids[i])[j]); | |
314 | printf(" issuedtxid v%d i.%d of n.%d opretlen.%d\n",vouts[i],i,n,opretlen); | |
315 | if ( komodo_paxmark(&space,txids[i],vouts[i],height) == 0 ) | |
316 | komodo_gateway_deposit(0,0,0,0,0,0,txids[i],vouts[i],height); | |
317 | } | |
318 | } | |
319 | } | |
320 | else if ( tokomodo != 0 && opretbuf[0] == 'X' ) | |
321 | { | |
322 | // verify and update limits | |
323 | } | |
324 | } | |
325 | return(typestr); | |
326 | } | |
327 | ||
328 | void komodo_gateway_voutupdate(char *symbol,int32_t isspecial,int32_t height,int32_t txi,bits256 txid,int32_t vout,int32_t numvouts,uint64_t value,uint8_t *script,int32_t len) | |
329 | { | |
330 | int32_t i,opretlen,offset = 0; uint256 zero,utxid; const char *typestr; | |
331 | typestr = "unknown"; | |
332 | memcpy(&utxid,&txid,sizeof(utxid)); | |
333 | if ( script[offset++] == 0x6a ) | |
334 | { | |
335 | offset += komodo_scriptitemlen(&opretlen,&script[offset]); | |
336 | if ( isspecial != 0 && len >= offset+32*2+4 && strcmp((char *)&script[offset+32*2+4],ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL) == 0 ) | |
337 | typestr = "notarized"; | |
338 | else if ( txi == 0 && vout == 1 && opretlen == 149 ) | |
339 | { | |
340 | typestr = "pricefeed"; | |
341 | komodo_paxpricefeed(height,&script[offset],opretlen); | |
342 | //printf("height.%d pricefeed len.%d\n",height,opretlen); | |
343 | } | |
344 | else komodo_stateupdate(height,0,0,0,utxid,0,0,0,0,0,value,&script[offset],opretlen,vout); | |
345 | } | |
346 | else if ( numvouts >= KOMODO_MINRATIFY ) | |
347 | typestr = "ratify"; | |
348 | } | |
349 | ||
350 | int32_t komodo_gateway_tx(char *symbol,int32_t height,int32_t txi,char *txidstr,uint32_t port) | |
351 | { | |
352 | char *retstr,params[256],*hexstr; uint8_t script[10000]; cJSON *json,*result,*vouts,*item,*sobj; int32_t vout,n,len,isspecial,retval = -1; uint64_t value,vpub_old,vpub_new; bits256 txid; | |
353 | sprintf(params,"[\"%s\", 1]",txidstr); | |
354 | if ( (retstr= komodo_issuemethod((char *)"getrawtransaction",params,port)) != 0 ) | |
355 | { | |
356 | if ( (json= cJSON_Parse(retstr)) != 0 ) | |
357 | { | |
358 | if ( (result= jobj(json,(char *)"result")) != 0 ) | |
359 | { | |
360 | printf("RESULT.(%s)\n",jprint(result,0)); | |
361 | vpub_old = jdouble(result,(char *)"vpub_old") * COIN; | |
362 | vpub_new = jdouble(result,(char *)"vpub_new") * COIN; | |
363 | retval = 0; | |
364 | if ( vpub_old == 0 && vpub_new == 0 && (vouts= jarray(&n,result,(char *)"vout")) != 0 ) | |
365 | { | |
366 | isspecial = 0; | |
367 | txid = jbits256(result,(char *)"txid"); | |
368 | for (vout=0; vout<n; vout++) | |
369 | { | |
370 | item = jitem(vouts,vout); | |
371 | value = SATOSHIDEN * jdouble(item,(char *)"value"); | |
372 | if ( (sobj= jobj(item,(char *)"scriptPubKey")) != 0 ) | |
373 | { | |
374 | if ( (hexstr= jstr(sobj,(char *)"hex")) != 0 ) | |
375 | { | |
376 | len = (int32_t)strlen(hexstr) >> 1; | |
377 | if ( vout == 0 && ((memcmp(&hexstr[2],CRYPTO777_PUBSECPSTR,66) == 0 && len == 35) || (memcmp(&hexstr[6],CRYPTO777_RMD160STR,40) == 0 && len == 25)) ) | |
378 | isspecial = 1; | |
379 | else if ( len <= sizeof(script) ) | |
380 | { | |
381 | decode_hex(script,len,hexstr); | |
382 | komodo_gateway_voutupdate(symbol,isspecial,height,txi,txid,vout,n,value,script,len); | |
383 | } | |
384 | } | |
385 | } | |
386 | } | |
387 | } | |
388 | } else printf("error getting txids.(%s) %p\n",retstr,result); | |
389 | free_json(json); | |
390 | } | |
391 | free(retstr); | |
392 | } | |
393 | return(retval); | |
394 | } | |
395 | ||
396 | int32_t komodo_gateway_block(char *symbol,int32_t height,uint16_t port) | |
397 | { | |
398 | char *retstr,*retstr2,params[128],*txidstr; int32_t i,n,retval = -1; cJSON *json,*tx=0,*result=0,*result2; | |
399 | sprintf(params,"[%d]",height); | |
400 | if ( (retstr= komodo_issuemethod((char *)"getblockhash",params,port)) != 0 ) | |
401 | { | |
402 | if ( (result= cJSON_Parse(retstr)) != 0 ) | |
403 | { | |
404 | if ( (txidstr= jstr(result,(char *)"result")) != 0 && strlen(txidstr) == 64 ) | |
405 | { | |
406 | sprintf(params,"[\"%s\"]",txidstr); | |
407 | if ( (retstr2= komodo_issuemethod((char *)"getblock",params,port)) != 0 ) | |
408 | { | |
409 | //printf("getblock.(%s)\n",retstr2); | |
410 | if ( (json= cJSON_Parse(retstr2)) != 0 ) | |
411 | { | |
412 | if ( (result2= jobj(json,(char *)"result")) != 0 && (tx= jarray(&n,result2,(char *)"tx")) != 0 ) | |
413 | { | |
414 | for (i=0; i<n; i++) | |
415 | if ( komodo_gateway_tx(symbol,height,i,jstri(tx,i),port) < 0 ) | |
416 | break; | |
417 | if ( i == n ) | |
418 | retval = 0; | |
419 | else printf("komodo_gateway_block ht.%d error i.%d vs n.%d\n",height,i,n); | |
420 | } else printf("cant get result.%p or tx.%p\n",result,tx); | |
421 | free_json(json); | |
422 | } else printf("cant parse2.(%s)\n",retstr2); | |
423 | free(retstr2); | |
424 | } else printf("error getblock %s\n",params); | |
425 | } else printf("strlen.%ld (%s)\n",strlen(txidstr),txidstr); | |
426 | free_json(result); | |
427 | } else printf("couldnt parse.(%s)\n",retstr); | |
428 | free(retstr); | |
429 | } else printf("error from getblockhash %d\n",height); | |
430 | return(retval); | |
431 | } | |
432 | ||
433 | void komodo_gateway_iteration(char *symbol) | |
434 | { | |
435 | char *retstr; int32_t i,kmdheight; cJSON *infoobj,*result; uint256 zero; uint16_t port = 7771; | |
436 | if ( KMDHEIGHT <= 0 ) | |
437 | KMDHEIGHT = 1; | |
438 | KOMODO_REALTIME = 0; | |
439 | if ( (retstr= komodo_issuemethod((char *)"getinfo",0,port)) != 0 ) | |
440 | { | |
441 | if ( (infoobj= cJSON_Parse(retstr)) != 0 ) | |
442 | { | |
443 | if ( (result= jobj(infoobj,(char *)"result")) != 0 && (kmdheight= jint(result,(char *)"blocks")) != 0 ) | |
444 | { | |
445 | //printf("gateway KMDHEIGHT.%d kmdheight.%d\n",KMDHEIGHT,kmdheight); | |
446 | for (i=0; i<1000 && KMDHEIGHT<kmdheight; i++,KMDHEIGHT++) | |
447 | { | |
448 | if ( (KMDHEIGHT % 10) == 0 ) | |
449 | { | |
450 | if ( (KMDHEIGHT % 100) == 0 ) | |
451 | fprintf(stderr,"%s.%d ",symbol,KMDHEIGHT); | |
452 | memset(&zero,0,sizeof(zero)); | |
453 | komodo_stateupdate(KMDHEIGHT,0,0,0,zero,0,0,0,0,KMDHEIGHT,0,0,0,0); | |
454 | } | |
455 | if ( komodo_gateway_block(symbol,KMDHEIGHT,port) < 0 ) | |
456 | { | |
457 | printf("error KMDHEIGHT %d\n",KMDHEIGHT); | |
458 | break; | |
459 | } | |
460 | usleep(10000); | |
461 | } | |
462 | if ( KMDHEIGHT >= kmdheight ) | |
463 | KOMODO_REALTIME = (uint32_t)time(NULL); | |
464 | } | |
465 | free_json(infoobj); | |
466 | } | |
467 | free(retstr); | |
468 | } | |
469 | else | |
470 | { | |
471 | printf("error from %s\n",symbol); | |
472 | sleep(30); | |
473 | } | |
474 | KOMODO_DEPOSIT = komodo_paxtotal(); | |
475 | } | |
476 | ||
477 | void komodo_iteration(char *symbol) | |
478 | { | |
479 | char *retstr,*base,*coinaddr,*txidstr,cmd[512]; uint64_t value,fiatoshis; cJSON *array,*item; int32_t i,n,vout,shortflag,height; uint256 txid; uint8_t rmd160[20],addrtype; | |
480 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) | |
481 | { | |
482 | //[{"prev_hash":"5d5c9a49489b558de9e84f991f996dedaae6b9d0f157f82b2fec64662476d5cf","prev_vout":2,"EUR":0.78329000,"fiat":"EUR","height":57930,"KMD":0.10000000,"address":"RDhEGYScNQYetCyG75Kf8Fg61UWPdwc1C5"}] | |
483 | sprintf(cmd,"{\"agent\":\"dpow\",\"method\":\"pending\",\"fiat\":\"%s\"}",symbol); | |
484 | if ( (retstr= issue_curl(cmd)) != 0 ) | |
485 | { | |
486 | if ( (array= cJSON_Parse(retstr)) != 0 ) | |
487 | { | |
488 | if ( (n= cJSON_GetArraySize(array)) > 0 ) | |
489 | { | |
490 | for (i=0; i<n; i++) | |
491 | { | |
492 | item = jitem(array,i); | |
493 | coinaddr = jstr(item,(char *)"address"); | |
494 | value = jdouble(item,(char *)"KMD") * COIN; | |
495 | shortflag = juint(item,(char *)"short"); | |
496 | vout = jint(item,(char *)"prev_vout"); | |
497 | height = jint(item,(char *)"height"); | |
498 | base = jstr(item,(char *)"fiat"); | |
499 | txidstr = jstr(item,(char *)"prev_hash"); | |
500 | if ( coinaddr != 0 && base != 0 && value > 0 && height > 0 ) | |
501 | { | |
502 | fiatoshis = jdouble(item,base) * COIN; | |
503 | decode_hex((uint8_t *)&txid,sizeof(txid),txidstr); | |
504 | bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr); | |
505 | komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,height); | |
506 | } | |
507 | } | |
508 | } | |
509 | } | |
510 | printf("retstr.(%s)\n",retstr); | |
511 | free(retstr); | |
512 | } | |
513 | } | |
514 | } |