]> 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
429dabb5 18struct pax_transaction
64bb0834 19{
6210f328 20 UT_hash_handle hh;
429dabb5 21 uint256 txid;
22 uint64_t komodoshis,fiatoshis;
d95563be 23 int32_t marked,height,otherheight;
429dabb5 24 uint16_t vout;
8ec26042 25 char symbol[16],coinaddr[64]; uint8_t rmd160[20],shortflag;
6210f328 26} *PAX;
27
77be6cd9 28uint64_t komodo_paxtotal()
53c47c90 29{
e5430f52 30 struct pax_transaction *pax,*tmp; uint64_t total = 0;
c169f64b 31 HASH_ITER(hh,PAX,pax,tmp)
32 {
33 if ( pax->marked == 0 )
34 {
35 if ( komodo_is_issuer() != 0 )
36 total += pax->fiatoshis;
37 else total += pax->komodoshis;
38 }
39 }
53c47c90 40 return(total);
2ab43fd2 41}
42
6210f328 43struct pax_transaction *komodo_paxfind(struct pax_transaction *space,uint256 txid,uint16_t vout)
44{
45 struct pax_transaction *pax;
46 pthread_mutex_lock(&komodo_mutex);
47 HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
48 if ( pax != 0 )
49 memcpy(space,pax,sizeof(*pax));
50 pthread_mutex_unlock(&komodo_mutex);
51 return(pax);
52}
53
b5bf65f5 54struct pax_transaction *komodo_paxmark(int32_t height,struct pax_transaction *space,uint256 txid,uint16_t vout,int32_t mark)
6210f328 55{
56 struct pax_transaction *pax;
57 pthread_mutex_lock(&komodo_mutex);
58 HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
2ab43fd2 59 if ( pax == 0 )
60 {
61 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
62 pax->txid = txid;
63 pax->vout = vout;
64 HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
32d525eb 65 //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark);
2ab43fd2 66 }
6210f328 67 if ( pax != 0 )
68 {
69 pax->marked = mark;
f1e6bb01 70 //int32_t i; for (i=0; i<32; i++)
71 // printf("%02x",((uint8_t *)&txid)[i]);
72 //printf(" paxmark.ht %d vout%d\n",mark,vout);
6210f328 73 memcpy(space,pax,sizeof(*pax));
74 }
75 pthread_mutex_unlock(&komodo_mutex);
76 return(pax);
77}
429dabb5 78
1fc75a27 79void 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,int32_t otherheight) // assetchain context
d43c7a93 80{
b2d64a0f 81 struct pax_transaction *pax; int32_t addflag = 0;
d43c7a93 82 pthread_mutex_lock(&komodo_mutex);
83 HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
84 if ( pax == 0 )
b2d64a0f 85 {
d43c7a93 86 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
8d52488f 87 pax->txid = txid;
88 pax->vout = vout;
89 HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
7961668b 90 addflag = 1;
1fc75a27 91 if ( ASSETCHAINS_SYMBOL[0] == 0 )
92 {
93 int32_t i; for (i=0; i<32; i++)
94 printf("%02x",((uint8_t *)&txid)[i]);
95 printf(" v.%d [%s] kht.%d ht.%d create pax.%p\n",vout,ASSETCHAINS_SYMBOL,height,otherheight,pax);
96 }
b2d64a0f 97 }
8d52488f 98 pthread_mutex_unlock(&komodo_mutex);
d43c7a93 99 if ( coinaddr != 0 )
100 {
101 strcpy(pax->coinaddr,coinaddr);
102 pax->komodoshis = value;
103 pax->shortflag = shortflag;
104 strcpy(pax->symbol,symbol);
105 pax->fiatoshis = fiatoshis;
106 memcpy(pax->rmd160,rmd160,20);
107 pax->height = height;
1fc75a27 108 pax->otherheight = otherheight;
d43c7a93 109 if ( pax->marked == 0 )
7961668b 110 {
111 if ( addflag != 0 )
00cf544e 112 printf("[%s] addflag.%d ADD DEPOSIT %s %.8f -> %s TO PAX ht.%d otherht.%d total %.8f\n",ASSETCHAINS_SYMBOL,addflag,symbol,dstr(fiatoshis),coinaddr,height,otherheight,dstr(komodo_paxtotal()));
7961668b 113 }
0dca8d4c 114 //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 115 }
116 else
117 {
118 pax->marked = height;
1fc75a27 119 printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight);
d43c7a93 120 }
d43c7a93 121}
122
05d91eb2 123int32_t komodo_issued_opreturn(uint8_t *shortflagp,char *base,uint256 *txids,uint16_t *vouts,uint8_t *opretbuf,int32_t opretlen,int32_t iskomodo)
63b289ad 124{
e2c35fcd 125 int32_t i,n=0,j,len;
63b289ad 126 if ( opretbuf[opretlen-5] == '-' )
127 *shortflagp = 1;
128 else *shortflagp = 0;
129 for (i=0; i<4; i++)
130 base[i] = opretbuf[opretlen-4+i];
0e3472c1 131 if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) // shortflag
63b289ad 132 {
133 opretbuf++, opretlen--;
134 for (n=len=0; n<opretlen/34; n++)
135 {
136 for (j=0; j<32; j++)
137 {
138 ((uint8_t *)&txids[n])[j] = opretbuf[len++];
1938746c 139 //printf("%02x",((uint8_t *)&txids[n])[j]);
63b289ad 140 }
141 vouts[n] = opretbuf[len++];
142 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
1938746c 143 //printf(" issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
05d91eb2 144 if ( iskomodo != 0 )
145 {
146 uint64_t fiatoshis; int32_t height,otherheight,shortflag; char symbol[16];
147 len += iguana_rwnum(0,&opretbuf[len],sizeof(fiatoshis),&fiatoshis);
148 len += iguana_rwnum(0,&opretbuf[len],sizeof(height),&height);
149 len += iguana_rwnum(0,&opretbuf[len],sizeof(otherheight),&otherheight);
150 if ( opretbuf[len] == '-' )
151 shortflag = 1, len++;
152 else shortflag = 0;
153 for (i=0; opretbuf[len+i]!=0&&i<3; i++)
154 symbol[i] = opretbuf[len+i];
155 symbol[i] = 0;
156 printf(">>>>>>> iskomodo X: (%s) fiat %.8f kmdheight.%d other.%d short.%d\n",symbol,dstr(fiatoshis),height,otherheight,shortflag);
157 }
63b289ad 158 }
159 }
160 return(n);
161}
162
a3f23ee2 163void komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,char *symbol,int32_t tokomodo)
429dabb5 164{
77be6cd9 165 struct pax_transaction *pax,*tmp; uint8_t *script,opcode,opret[10000],data[10000]; int32_t i,len=0,opretlen=0,numvouts=1;
429dabb5 166 PENDING_KOMODO_TX = 0;
a3f23ee2 167 if ( tokomodo == 0 )
77be6cd9 168 opcode = 'I';
169 else opcode = 'X';
c169f64b 170 HASH_ITER(hh,PAX,pax,tmp)
429dabb5 171 {
e5430f52 172 if ( pax->marked != 0 )
173 continue;
7ca711ba 174 if ( ASSETCHAINS_SYMBOL[0] != 0 )
175 printf("pax.%p marked.%d %.8f -> %.8f\n",pax,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
e5430f52 176 txNew->vout.resize(numvouts+1);
1fc75a27 177 txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis;
e5430f52 178 txNew->vout[numvouts].scriptPubKey.resize(25);
179 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
180 *script++ = 0x76;
181 *script++ = 0xa9;
182 *script++ = 20;
183 memcpy(script,pax->rmd160,20), script += 20;
184 *script++ = 0x88;
185 *script++ = 0xac;
186 for (i=0; i<32; i++)
187 {
1938746c 188 //printf("%02x",((uint8_t *)&pax->txid)[i]);
e5430f52 189 data[len++] = ((uint8_t *)&pax->txid)[i];
429dabb5 190 }
e5430f52 191 data[len++] = pax->vout & 0xff;
192 data[len++] = (pax->vout >> 8) & 0xff;
a3f23ee2 193 if ( tokomodo == 0 )
e5430f52 194 PENDING_KOMODO_TX += pax->fiatoshis;
fbc4209b 195 else
196 {
1fc75a27 197 //[{"prev_hash":"5d5c9a49489b558de9e84f991f996dedaae6b9d0f157f82b2fec64662476d5cf","prev_vout":2,"EUR":0.10000000,"fiat":"EUR","kmdheight":57930,"height":153,"KMD":0.78329000,"address":"RDhEGYScNQYetCyG75Kf8Fg61UWPdwc1C5","rmd160":"306c507eea639e7220b3069ed9f49f3bc97eaca1"}]
198 len += iguana_rwnum(1,&data[len],sizeof(pax->fiatoshis),&pax->fiatoshis);
199 len += iguana_rwnum(1,&data[len],sizeof(pax->height),&pax->height);
200 len += iguana_rwnum(1,&data[len],sizeof(pax->otherheight),&pax->otherheight);
201 if ( pax->shortflag != 0 )
202 data[len++] = '-';
203 for (i=0; pax->symbol[i]!=0&&i<3; i++)
204 data[len++] = pax->symbol[i];
205 data[len++] = 0;
fbc4209b 206 PENDING_KOMODO_TX += pax->komodoshis;
05d91eb2 207 printf(" vout.%u DEPOSIT %.8f <- pax.%s pending %.8f | ",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,dstr(PENDING_KOMODO_TX));
fbc4209b 208 }
e5430f52 209 if ( numvouts++ >= 64 )
210 break;
429dabb5 211 }
212 if ( numvouts > 1 )
213 {
9ef47e29 214 if ( shortflag != 0 )
a5ad8f02 215 data[len++] = '-';
0e3472c1 216 if ( tokomodo != 0 )
217 strcpy(symbol,"KMD");
9ef47e29 218 for (i=0; symbol[i]!=0; i++)
219 data[len++] = symbol[i];
a5ad8f02 220 data[len++] = 0;
77be6cd9 221 opretlen = komodo_opreturnscript(opret,opcode,data,len);
50824530 222 txNew->vout.resize(numvouts+1);
223 txNew->vout[numvouts].nValue = 0;
224 txNew->vout[numvouts].scriptPubKey.resize(opretlen);
225 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
429dabb5 226 memcpy(script,opret,opretlen);
23d955d4 227 printf("MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen);
9ef47e29 228 }
429dabb5 229}
230
8f64a3a9 231int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
557d9a23 232{
81041ab2 233 int32_t i,j,n,num,opretlen,offset=1,errs=0,matched=0; 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;
557d9a23 234 n = block.vtx[0].vout.size();
235 script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
12d47153 236 if ( n <= 2 || script[0] != 0x6a )
237 return(0);
238 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
77be6cd9 239 if ( ASSETCHAINS_SYMBOL[0] == 0 )
240 {
76aa815e 241 for (i=0; i<opretlen; i++)
242 printf("%02x",script[i]);
243 printf(" height.%d checkdeposit n.%d [%02x] [%c] %d vs %d\n",height,n,script[0],script[offset],script[offset],'X');
77be6cd9 244 opcode = 'X';
245 strcpy(symbol,"KMD");
246 }
247 else
248 {
249 strcpy(symbol,ASSETCHAINS_SYMBOL);
250 opcode = 'I';
251 }
252 if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
557d9a23 253 {
05d91eb2 254 if ( (num= komodo_issued_opreturn(&shortflag,base,txids,vouts,&script[offset],opretlen,opcode == 'X')) > 0 )
557d9a23 255 {
256 for (i=1; i<n-1; i++)
257 {
f10b8e2e 258 if ( (pax= komodo_paxfind(&space,txids[i-1],vouts[i-1])) != 0 )
5a2c5b6f 259 {
f10b8e2e 260 if ( ((opcode == 'I' && pax->fiatoshis == block.vtx[0].vout[i].nValue) || (opcode == 'X' && pax->komodoshis == block.vtx[0].vout[i].nValue)) )
261 {
262 if ( pax->marked != 0 )
263 errs++;
264 else matched++;
05d91eb2 265 if ( opcode == 'X' )
266 printf("errs.%d i.%d match %.8f == %.8f\n",errs,i,dstr(pax != 0 ? pax->fiatoshis:-1),dstr(block.vtx[0].vout[i].nValue));
f10b8e2e 267 }
268 else
269 {
270 hash = block.GetHash();
05d91eb2 271 if ( opcode == 'X' )
272 {
273 for (j=0; j<32; j++)
274 printf("%02x",((uint8_t *)&hash)[j]);
275 printf(" ht.%d blockhash couldnt find vout.[%d]\n",height,i);
276 }
f10b8e2e 277 }
5a2c5b6f 278 }
cb040aa1 279 else
280 {
05d91eb2 281 if ( opcode == 'X' )
282 {
ce94767e 283 matched++;
05d91eb2 284 for (j=0; j<32; j++)
285 printf("%02x",((uint8_t *)&txids[i-1])[j]);
286 printf(" cant paxfind txid\n");
ce94767e 287 } else if ( opcode == 'I' )
288 matched++;
cb040aa1 289 }
f6b5fa43 290 komodo_paxmark(height,&space,txids[i-1],vouts[i-1],height);
557d9a23 291 }
81041ab2 292 if ( matched != num )
293 {
0dca8d4c 294 // can easily happen depending on order of loading
ce94767e 295 if ( height > 60000 )
296 printf("WARNING: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num);
81041ab2 297 }
557d9a23 298 }
b98053e2 299 //printf("opretlen.%d num.%d\n",opretlen,num);
557d9a23 300 }
301 return(0);
302}
303
429dabb5 304const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout)
305{
15b7788a 306 uint8_t rmd160[20],addrtype,shortflag,pubkey33[33]; int32_t i,j,n,len,tokomodo,kmdheight; char base[4],coinaddr[64],destaddr[64]; struct pax_transaction space; uint256 txids[64]; uint16_t vouts[64]; double diff; uint64_t seed; int64_t fiatoshis,checktoshis; const char *typestr = "unknown";
95db8239 307 tokomodo = (komodo_is_issuer() == 0);
1938746c 308 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
ad7b6271 309 {
310 for (i=0; i<opretlen; i++)
311 printf("%02x",opretbuf[i]);
312 printf(" opret[%c] else path tokomodo.%d ht.%d\n",opretbuf[0],tokomodo,height);
313 }
0a611c1e 314 if ( opretbuf[0] == 'D' )
64bb0834 315 {
05a37d87 316 if ( opretlen == 38 ) // any KMD tx
64bb0834 317 {
05a37d87 318 iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
abdd8672 319 memset(base,0,sizeof(base));
320 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
321 if ( fiatoshis < 0 )
322 fiatoshis = -fiatoshis;
323 bitcoin_address(coinaddr,addrtype,rmd160,20);
324 checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
325 typestr = "deposit";
15b7788a 326 printf("kmdheight.%d vs height.%d check %.8f vs %.8f tokomodo.%d %d seed.%llx\n",kmdheight,height,dstr(checktoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed);
327 diff = ((double)value / checktoshis) - 1.;
328 if ( diff < 0. )
329 diff = -diff;
05a37d87 330 if ( kmdheight <= height )
6e576848 331 {
15b7788a 332 if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 && shortflag == ASSETCHAINS_SHORTFLAG )
05a37d87 333 {
334 if ( shortflag == 0 )
335 {
336 for (i=0; i<32; i++)
337 printf("%02x",((uint8_t *)&txid)[i]);
338 printf(" <- txid.v%u ",vout);
339 for (i=0; i<33; i++)
340 printf("%02x",pubkey33[i]);
341 printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
15b7788a 342 if ( value >= checktoshis || (seed == 0 && diff < .01) )
05a37d87 343 {
344 if ( komodo_paxfind(&space,txid,vout) == 0 )
31e168df 345 {
1fc75a27 346 komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,kmdheight,height);
31e168df 347 } else printf("duplicate deposit\n");
05a37d87 348 }
349 }
350 else // short
755ead98 351 {
05a37d87 352 for (i=0; i<opretlen; i++)
353 printf("%02x",opretbuf[i]);
354 printf(" opret[%c] tokomodo.%d value %.8f vs check %.8f\n",opretbuf[0],tokomodo,dstr(value),dstr(checktoshis));
15b7788a 355 if ( value <= checktoshis || (seed == 0 && diff < .01) )
05a37d87 356 {
357
358 }
755ead98 359 }
8e3430ee 360 }
8bc1bcad 361 }
64bb0834 362 }
363 }
402d5759 364 else if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
429dabb5 365 {
755ead98 366 if ( tokomodo == 0 && opretbuf[0] == 'I' ) // assetchain coinbase
429dabb5 367 {
05d91eb2 368 if ( (n= komodo_issued_opreturn(&shortflag,base,txids,vouts,opretbuf,opretlen,0)) > 0 && shortflag == ASSETCHAINS_SHORTFLAG )
429dabb5 369 {
63b289ad 370 for (i=0; i<n; i++)
ec35af90 371 {
1938746c 372 //for (j=0; j<32; j++)
373 // printf("%02x",((uint8_t *)&txids[i])[j]);
b5bf65f5 374 if ( komodo_paxmark(height,&space,txids[i],vouts[i],height) == 0 )
31e168df 375 {
1fc75a27 376 komodo_gateway_deposit(0,0,0,0,0,0,txids[i],vouts[i],height,0);
31e168df 377 }
f6b5fa43 378 else
379 {
1938746c 380 //printf(" duplicate issuedtxid v%d i.%d of n.%d opretlen.%d\n",vouts[i],i,n,opretlen);
f6b5fa43 381 }
ec35af90 382 }
429dabb5 383 }
429dabb5 384 }
0920fed9 385 else if ( tokomodo != 0 && opretbuf[0] == 'X' )
9ef47e29 386 {
387 // verify and update limits
388 }
429dabb5 389 }
64bb0834 390 return(typestr);
391}
392
eae0d2e5 393void 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)
f38345e9 394{
eae0d2e5 395 int32_t i,opretlen,offset = 0; uint256 zero,utxid; const char *typestr;
57abdbaf 396 typestr = "unknown";
eae0d2e5 397 memcpy(&utxid,&txid,sizeof(utxid));
64bb0834 398 if ( script[offset++] == 0x6a )
654330ab 399 {
b673d264 400 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
557d9a23 401 if ( isspecial != 0 && len >= offset+32*2+4 && strcmp((char *)&script[offset+32*2+4],ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL) == 0 )
654330ab 402 typestr = "notarized";
7eca18ee 403 else if ( txi == 0 && vout == 1 && opretlen == 149 )
9b9adadc 404 {
654330ab 405 typestr = "pricefeed";
e7e817f7 406 komodo_paxpricefeed(height,&script[offset],opretlen);
0765bc71 407 //printf("height.%d pricefeed len.%d\n",height,opretlen);
9b9adadc 408 }
eae0d2e5 409 else komodo_stateupdate(height,0,0,0,utxid,0,0,0,0,0,value,&script[offset],opretlen,vout);
654330ab 410 }
755ead98 411 else if ( numvouts >= KOMODO_MINRATIFY )
654330ab 412 typestr = "ratify";
f38345e9 413}
414
c59a3beb 415int32_t komodo_gateway_tx(char *symbol,int32_t height,int32_t txi,char *txidstr,uint32_t port)
c42b46db 416{
ee0744d2 417 char *retstr,params[256],*hexstr; uint8_t script[10000]; cJSON *oldpub,*newpub,*json,*result,*vouts,*item,*sobj; int32_t vout,n,len,isspecial,retval = -1; uint64_t value; bits256 txid;
f38345e9 418 sprintf(params,"[\"%s\", 1]",txidstr);
419 if ( (retstr= komodo_issuemethod((char *)"getrawtransaction",params,port)) != 0 )
c42b46db 420 {
f38345e9 421 if ( (json= cJSON_Parse(retstr)) != 0 )
c42b46db 422 {
26fa1236 423 if ( (result= jobj(json,(char *)"result")) != 0 )
2c6bdd83 424 {
ee0744d2 425 oldpub = jobj(result,(char *)"vpub_old");
426 newpub = jobj(result,(char *)"vpub_new");
c59a3beb 427 retval = 0;
ee0744d2 428 if ( oldpub == 0 && newpub == 0 && (vouts= jarray(&n,result,(char *)"vout")) != 0 )
2c6bdd83 429 {
26fa1236 430 isspecial = 0;
431 txid = jbits256(result,(char *)"txid");
432 for (vout=0; vout<n; vout++)
2c6bdd83 433 {
26fa1236 434 item = jitem(vouts,vout);
435 value = SATOSHIDEN * jdouble(item,(char *)"value");
436 if ( (sobj= jobj(item,(char *)"scriptPubKey")) != 0 )
2c6bdd83 437 {
26fa1236 438 if ( (hexstr= jstr(sobj,(char *)"hex")) != 0 )
f38345e9 439 {
26fa1236 440 len = (int32_t)strlen(hexstr) >> 1;
441 if ( vout == 0 && ((memcmp(&hexstr[2],CRYPTO777_PUBSECPSTR,66) == 0 && len == 35) || (memcmp(&hexstr[6],CRYPTO777_RMD160STR,40) == 0 && len == 25)) )
442 isspecial = 1;
443 else if ( len <= sizeof(script) )
444 {
445 decode_hex(script,len,hexstr);
446 komodo_gateway_voutupdate(symbol,isspecial,height,txi,txid,vout,n,value,script,len);
447 }
f38345e9 448 }
2c6bdd83 449 }
450 }
2c6bdd83 451 }
a10aafcc 452 } else printf("error getting txids.(%s) %p\n",retstr,result);
f38345e9 453 free_json(json);
454 }
455 free(retstr);
c42b46db 456 }
c59a3beb 457 return(retval);
c42b46db 458}
459
c59a3beb 460int32_t komodo_gateway_block(char *symbol,int32_t height,uint16_t port)
9b0e1808 461{
d4c346d6 462 char *retstr,*retstr2,params[128],*txidstr; int32_t i,n,retval = -1; cJSON *json,*tx=0,*result=0,*result2;
f38345e9 463 sprintf(params,"[%d]",height);
464 if ( (retstr= komodo_issuemethod((char *)"getblockhash",params,port)) != 0 )
465 {
e14a749a 466 if ( (result= cJSON_Parse(retstr)) != 0 )
f38345e9 467 {
f686d848 468 if ( (txidstr= jstr(result,(char *)"result")) != 0 && strlen(txidstr) == 64 )
f38345e9 469 {
e14a749a 470 sprintf(params,"[\"%s\"]",txidstr);
471 if ( (retstr2= komodo_issuemethod((char *)"getblock",params,port)) != 0 )
f38345e9 472 {
f686d848 473 //printf("getblock.(%s)\n",retstr2);
e14a749a 474 if ( (json= cJSON_Parse(retstr2)) != 0 )
f38345e9 475 {
f686d848 476 if ( (result2= jobj(json,(char *)"result")) != 0 && (tx= jarray(&n,result2,(char *)"tx")) != 0 )
e14a749a 477 {
478 for (i=0; i<n; i++)
c59a3beb 479 if ( komodo_gateway_tx(symbol,height,i,jstri(tx,i),port) < 0 )
480 break;
481 if ( i == n )
482 retval = 0;
4f593916 483 else printf("komodo_gateway_block ht.%d error i.%d vs n.%d\n",height,i,n);
d4c346d6 484 } else printf("cant get result.%p or tx.%p\n",result,tx);
e14a749a 485 free_json(json);
d4c346d6 486 } else printf("cant parse2.(%s)\n",retstr2);
e14a749a 487 free(retstr2);
d4c346d6 488 } else printf("error getblock %s\n",params);
e14a749a 489 } else printf("strlen.%ld (%s)\n",strlen(txidstr),txidstr);
490 free_json(result);
d4c346d6 491 } else printf("couldnt parse.(%s)\n",retstr);
f38345e9 492 free(retstr);
d4c346d6 493 } else printf("error from getblockhash %d\n",height);
c59a3beb 494 return(retval);
9b0e1808 495}
496
f38345e9 497void komodo_gateway_iteration(char *symbol)
498{
2b84e06c 499 char *retstr; int32_t i,kmdheight; cJSON *infoobj,*result; uint256 zero; uint16_t port = 7771;
12546443 500 if ( KMDHEIGHT <= 0 )
501 KMDHEIGHT = 1;
c5d580b2 502 //KOMODO_REALTIME = 0;
f38345e9 503 if ( (retstr= komodo_issuemethod((char *)"getinfo",0,port)) != 0 )
504 {
505 if ( (infoobj= cJSON_Parse(retstr)) != 0 )
506 {
830fc774 507 if ( (result= jobj(infoobj,(char *)"result")) != 0 && (kmdheight= jint(result,(char *)"blocks")) != 0 )
f38345e9 508 {
908b761f 509 //printf("gateway KMDHEIGHT.%d kmdheight.%d\n",KMDHEIGHT,kmdheight);
d360ffce 510 for (i=0; i<1000 && KMDHEIGHT<kmdheight; i++,KMDHEIGHT++)
f38345e9 511 {
755ead98 512 if ( (KMDHEIGHT % 10) == 0 )
2b84e06c 513 {
755ead98 514 if ( (KMDHEIGHT % 100) == 0 )
515 fprintf(stderr,"%s.%d ",symbol,KMDHEIGHT);
2b84e06c 516 memset(&zero,0,sizeof(zero));
429dabb5 517 komodo_stateupdate(KMDHEIGHT,0,0,0,zero,0,0,0,0,KMDHEIGHT,0,0,0,0);
2b84e06c 518 }
c59a3beb 519 if ( komodo_gateway_block(symbol,KMDHEIGHT,port) < 0 )
d4c346d6 520 {
521 printf("error KMDHEIGHT %d\n",KMDHEIGHT);
c59a3beb 522 break;
d4c346d6 523 }
4563911d 524 usleep(10000);
f38345e9 525 }
1fdfe03c 526 if ( KMDHEIGHT >= kmdheight )
527 KOMODO_REALTIME = (uint32_t)time(NULL);
f38345e9 528 }
529 free_json(infoobj);
530 }
f38345e9 531 free(retstr);
bb7f7473 532 }
533 else
534 {
a55c8425 535 printf("error from %s\n",symbol);
bb7f7473 536 sleep(30);
537 }
3f083c25 538}
539
540void komodo_iteration(char *symbol)
541{
1fc75a27 542 char *retstr,*base,*coinaddr,*txidstr,cmd[512]; uint64_t value,fiatoshis; cJSON *array,*item; int32_t i,n,vout,shortflag,height,fiatheight; uint256 txid; uint8_t rmd160[20],addrtype;
77be6cd9 543 if ( ASSETCHAINS_SYMBOL[0] == 0 )
544 {
3f083c25 545 sprintf(cmd,"{\"agent\":\"dpow\",\"method\":\"pending\",\"fiat\":\"%s\"}",symbol);
546 if ( (retstr= issue_curl(cmd)) != 0 )
77be6cd9 547 {
548 if ( (array= cJSON_Parse(retstr)) != 0 )
549 {
550 if ( (n= cJSON_GetArraySize(array)) > 0 )
551 {
552 for (i=0; i<n; i++)
553 {
554 item = jitem(array,i);
51508fed 555 coinaddr = jstr(item,(char *)"address");
556 value = jdouble(item,(char *)"KMD") * COIN;
1fc75a27 557 base = jstr(item,(char *)"fiat");
51508fed 558 shortflag = juint(item,(char *)"short");
559 vout = jint(item,(char *)"prev_vout");
1fc75a27 560 height = jint(item,(char *)"kmdheight");
561 fiatheight = jint(item,(char *)"height");
51508fed 562 txidstr = jstr(item,(char *)"prev_hash");
77be6cd9 563 if ( coinaddr != 0 && base != 0 && value > 0 && height > 0 )
564 {
565 fiatoshis = jdouble(item,base) * COIN;
566 decode_hex((uint8_t *)&txid,sizeof(txid),txidstr);
567 bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr);
1fc75a27 568 komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,height,fiatheight);
77be6cd9 569 }
570 }
571 }
572 }
7961668b 573 //printf("retstr.(%s)\n",retstr);
77be6cd9 574 free(retstr);
575 }
576 }
f38345e9 577}
This page took 0.191015 seconds and 4 git commands to generate.