]>
Commit | Line | Data |
---|---|---|
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 | 18 | struct pax_transaction |
64bb0834 | 19 | { |
6210f328 | 20 | UT_hash_handle hh; |
429dabb5 | 21 | uint256 txid; |
22 | uint64_t komodoshis,fiatoshis; | |
c030a184 | 23 | int32_t marked,height; |
429dabb5 | 24 | uint16_t vout; |
8ec26042 | 25 | char symbol[16],coinaddr[64]; uint8_t rmd160[20],shortflag; |
6210f328 | 26 | } *PAX; |
27 | ||
53c47c90 | 28 | /*uint64_t komodo_paxtotal() |
2ab43fd2 | 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); | |
53c47c90 | 37 | }*/ |
38 | ||
77be6cd9 | 39 | uint64_t komodo_paxtotal() |
53c47c90 | 40 | { |
41 | struct pax_transaction *pax,*tmp; uint64_t total = 0; | |
42 | tmp = 0; | |
55a913ee | 43 | while ( (pax= (struct pax_transaction *)PAX->hh.next) != 0 && pax != tmp ) |
53c47c90 | 44 | { |
45 | if ( pax->marked == 0 ) | |
46 | total += pax->fiatoshis; | |
47 | tmp = pax; | |
55a913ee | 48 | pax = (struct pax_transaction *)pax->hh.next; |
53c47c90 | 49 | } |
50 | return(total); | |
2ab43fd2 | 51 | } |
52 | ||
6210f328 | 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); | |
2ab43fd2 | 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 | } | |
6210f328 | 76 | if ( pax != 0 ) |
77 | { | |
78 | pax->marked = mark; | |
2f4cbc94 | 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); | |
6210f328 | 82 | memcpy(space,pax,sizeof(*pax)); |
83 | } | |
84 | pthread_mutex_unlock(&komodo_mutex); | |
85 | return(pax); | |
86 | } | |
429dabb5 | 87 | |
d43c7a93 | 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 | { | |
b2d64a0f | 90 | struct pax_transaction *pax; int32_t addflag = 0; |
d43c7a93 | 91 | pthread_mutex_lock(&komodo_mutex); |
92 | HASH_FIND(hh,PAX,&txid,sizeof(txid),pax); | |
93 | if ( pax == 0 ) | |
b2d64a0f | 94 | { |
d43c7a93 | 95 | pax = (struct pax_transaction *)calloc(1,sizeof(*pax)); |
8d52488f | 96 | pax->txid = txid; |
97 | pax->vout = vout; | |
98 | HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax); | |
b2d64a0f | 99 | } |
8d52488f | 100 | pthread_mutex_unlock(&komodo_mutex); |
d43c7a93 | 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 | } | |
d43c7a93 | 119 | } |
120 | ||
0fc3f7d3 | 121 | int32_t komodo_issued_opreturn(uint8_t *shortflagp,char *base,uint256 *txids,uint16_t *vouts,uint8_t *opretbuf,int32_t opretlen) |
63b289ad | 122 | { |
e2c35fcd | 123 | int32_t i,n=0,j,len; |
63b289ad | 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]; | |
9ef47e29 | 129 | if ( (strcmp(base,"KMD") == 0 && ASSETCHAINS_SYMBOL[0] == 0) || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) // shortflag |
63b289ad | 130 | { |
e647aac3 | 131 | //printf("BASE.(%s) vs (%s)\n",base,ASSETCHAINS_SYMBOL); |
63b289ad | 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++]; | |
e647aac3 | 138 | //printf("%02x",((uint8_t *)&txids[n])[j]); |
63b289ad | 139 | } |
140 | vouts[n] = opretbuf[len++]; | |
141 | vouts[n] = (opretbuf[len++] << 8) | vouts[n]; | |
e647aac3 | 142 | //printf(" issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen); |
63b289ad | 143 | } |
144 | } | |
145 | return(n); | |
146 | } | |
147 | ||
9ef47e29 | 148 | void komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,char *symbol) |
429dabb5 | 149 | { |
77be6cd9 | 150 | struct pax_transaction *pax,*tmp; uint8_t *script,opcode,opret[10000],data[10000]; int32_t i,len=0,opretlen=0,numvouts=1; |
429dabb5 | 151 | PENDING_KOMODO_TX = 0; |
77be6cd9 | 152 | if ( strcmp(symbol,"KMD") != 0 ) |
153 | opcode = 'I'; | |
154 | else opcode = 'X'; | |
53c47c90 | 155 | tmp = 0; |
52bc445a | 156 | while ( (pax= (struct pax_transaction *)PAX->hh.next) != 0 && pax != tmp ) |
429dabb5 | 157 | { |
15a04b78 | 158 | if ( pax->marked != 0 ) |
6210f328 | 159 | continue; |
50824530 | 160 | txNew->vout.resize(numvouts+1); |
93760f48 | 161 | txNew->vout[numvouts].nValue = pax->fiatoshis; |
50824530 | 162 | txNew->vout[numvouts].scriptPubKey.resize(25); |
163 | script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0]; | |
429dabb5 | 164 | *script++ = 0x76; |
165 | *script++ = 0xa9; | |
166 | *script++ = 20; | |
93760f48 | 167 | memcpy(script,pax->rmd160,20), script += 20; |
429dabb5 | 168 | *script++ = 0x88; |
169 | *script++ = 0xac; | |
170 | for (i=0; i<32; i++) | |
171 | { | |
93760f48 | 172 | printf("%02x",((uint8_t *)&pax->txid)[i]); |
173 | data[len++] = ((uint8_t *)&pax->txid)[i]; | |
429dabb5 | 174 | } |
93760f48 | 175 | data[len++] = pax->vout & 0xff; |
176 | data[len++] = (pax->vout >> 8) & 0xff; | |
77be6cd9 | 177 | printf(" vout.%u DEPOSIT %.8f <- paxdeposit.%s\n",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol); |
178 | if ( strcmp(symbol,"KMD") != 0 ) | |
9ef47e29 | 179 | PENDING_KOMODO_TX += pax->fiatoshis; |
77be6cd9 | 180 | else PENDING_KOMODO_TX += pax->komodoshis; |
6210f328 | 181 | if ( numvouts++ >= 64 ) |
182 | break; | |
53c47c90 | 183 | tmp = pax; |
52bc445a | 184 | pax = (struct pax_transaction *)pax->hh.next; |
429dabb5 | 185 | } |
186 | if ( numvouts > 1 ) | |
187 | { | |
9ef47e29 | 188 | if ( shortflag != 0 ) |
a5ad8f02 | 189 | data[len++] = '-'; |
9ef47e29 | 190 | for (i=0; symbol[i]!=0; i++) |
191 | data[len++] = symbol[i]; | |
a5ad8f02 | 192 | data[len++] = 0; |
77be6cd9 | 193 | opretlen = komodo_opreturnscript(opret,opcode,data,len); |
50824530 | 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]; | |
429dabb5 | 198 | memcpy(script,opret,opretlen); |
e2133528 | 199 | printf("total numvouts.%d %.8f opretlen.%d\n",numvouts,dstr(PENDING_KOMODO_TX),opretlen); |
9ef47e29 | 200 | } |
429dabb5 | 201 | } |
202 | ||
8f64a3a9 | 203 | int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing |
557d9a23 | 204 | { |
77be6cd9 | 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; |
557d9a23 | 206 | n = block.vtx[0].vout.size(); |
207 | script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data(); | |
12d47153 | 208 | if ( n <= 2 || script[0] != 0x6a ) |
209 | return(0); | |
210 | offset += komodo_scriptitemlen(&opretlen,&script[offset]); | |
8f64a3a9 | 211 | //printf("checkdeposit n.%d [%02x] [%c] %d vs %d\n",n,script[0],script[offset],script[offset],'I'); |
77be6cd9 | 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() ) | |
557d9a23 | 223 | { |
12d47153 | 224 | if ( (num= komodo_issued_opreturn(&shortflag,base,txids,vouts,&script[offset],opretlen)) > 0 ) |
557d9a23 | 225 | { |
226 | for (i=1; i<n-1; i++) | |
227 | { | |
77be6cd9 | 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)) ) |
5a2c5b6f | 229 | { |
68f2120f | 230 | //printf("i.%d match %.8f == %.8f\n",i,dstr(pax != 0 ? pax->fiatoshis:-1),dstr(block.vtx[0].vout[i].nValue)); |
cb040aa1 | 231 | komodo_paxmark(&space,txids[i-1],vouts[i-1],height); |
5a2c5b6f | 232 | } |
cb040aa1 | 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); | |
2ef81a6e | 239 | komodo_paxmark(&space,txids[i-1],vouts[i-1],height); |
cb040aa1 | 240 | } |
557d9a23 | 241 | } |
242 | } | |
b98053e2 | 243 | //printf("opretlen.%d num.%d\n",opretlen,num); |
557d9a23 | 244 | } |
245 | return(0); | |
246 | } | |
247 | ||
429dabb5 | 248 | const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout) |
249 | { | |
6210f328 | 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"; |
95db8239 | 251 | tokomodo = (komodo_is_issuer() == 0); |
0a611c1e | 252 | if ( opretbuf[0] == 'D' ) |
64bb0834 | 253 | { |
755ead98 | 254 | if ( opretlen == 34 ) // any KMD tx |
64bb0834 | 255 | { |
c17d9869 | 256 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) |
6e576848 | 257 | { |
258 | for (i=0; i<opretlen; i++) | |
259 | printf("%02x",opretbuf[i]); | |
260 | printf(" opret[%c] tokomodo.%d\n",opretbuf[0],tokomodo); | |
261 | } | |
552bdf22 | 262 | memset(base,0,sizeof(base)); |
64bb0834 | 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); | |
32888d61 | 267 | checktoshis = PAX_fiatdest(tokomodo,destaddr,pubkey33,coinaddr,height,base,fiatoshis); |
64bb0834 | 268 | typestr = "deposit"; |
755ead98 | 269 | if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 && shortflag == ASSETCHAINS_SHORTFLAG ) |
429dabb5 | 270 | { |
755ead98 | 271 | if ( shortflag == 0 ) |
8e3430ee | 272 | { |
755ead98 | 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 | } | |
8e3430ee | 284 | } |
755ead98 | 285 | else // short |
8e3430ee | 286 | { |
755ead98 | 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 | } | |
8e3430ee | 294 | } |
8bc1bcad | 295 | } |
64bb0834 | 296 | } |
297 | } | |
402d5759 | 298 | else if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 ) |
429dabb5 | 299 | { |
755ead98 | 300 | if ( tokomodo == 0 && opretbuf[0] == 'I' ) // assetchain coinbase |
429dabb5 | 301 | { |
c17d9869 | 302 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) |
6e576848 | 303 | { |
304 | for (i=0; i<opretlen; i++) | |
305 | printf("%02x",opretbuf[i]); | |
306 | printf(" opret[%c] tokomodo.%d\n",opretbuf[0],tokomodo); | |
307 | } | |
a55c8425 | 308 | if ( (n= komodo_issued_opreturn(&shortflag,base,txids,vouts,opretbuf,opretlen)) > 0 && shortflag == ASSETCHAINS_SHORTFLAG ) |
429dabb5 | 309 | { |
63b289ad | 310 | for (i=0; i<n; i++) |
ec35af90 | 311 | { |
8e3430ee | 312 | for (j=0; j<32; j++) |
021f8bc6 | 313 | printf("%02x",((uint8_t *)&txids[i])[j]); |
e2c35fcd | 314 | printf(" issuedtxid v%d i.%d of n.%d opretlen.%d\n",vouts[i],i,n,opretlen); |
6210f328 | 315 | if ( komodo_paxmark(&space,txids[i],vouts[i],height) == 0 ) |
c030a184 | 316 | komodo_gateway_deposit(0,0,0,0,0,0,txids[i],vouts[i],height); |
ec35af90 | 317 | } |
429dabb5 | 318 | } |
429dabb5 | 319 | } |
0920fed9 | 320 | else if ( tokomodo != 0 && opretbuf[0] == 'X' ) |
9ef47e29 | 321 | { |
322 | // verify and update limits | |
323 | } | |
429dabb5 | 324 | } |
64bb0834 | 325 | return(typestr); |
326 | } | |
327 | ||
eae0d2e5 | 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) |
f38345e9 | 329 | { |
eae0d2e5 | 330 | int32_t i,opretlen,offset = 0; uint256 zero,utxid; const char *typestr; |
57abdbaf | 331 | typestr = "unknown"; |
eae0d2e5 | 332 | memcpy(&utxid,&txid,sizeof(utxid)); |
64bb0834 | 333 | if ( script[offset++] == 0x6a ) |
654330ab | 334 | { |
b673d264 | 335 | offset += komodo_scriptitemlen(&opretlen,&script[offset]); |
557d9a23 | 336 | if ( isspecial != 0 && len >= offset+32*2+4 && strcmp((char *)&script[offset+32*2+4],ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL) == 0 ) |
654330ab | 337 | typestr = "notarized"; |
7eca18ee | 338 | else if ( txi == 0 && vout == 1 && opretlen == 149 ) |
9b9adadc | 339 | { |
654330ab | 340 | typestr = "pricefeed"; |
e7e817f7 | 341 | komodo_paxpricefeed(height,&script[offset],opretlen); |
0765bc71 | 342 | //printf("height.%d pricefeed len.%d\n",height,opretlen); |
9b9adadc | 343 | } |
eae0d2e5 | 344 | else komodo_stateupdate(height,0,0,0,utxid,0,0,0,0,0,value,&script[offset],opretlen,vout); |
654330ab | 345 | } |
755ead98 | 346 | else if ( numvouts >= KOMODO_MINRATIFY ) |
654330ab | 347 | typestr = "ratify"; |
f38345e9 | 348 | } |
349 | ||
c59a3beb | 350 | int32_t komodo_gateway_tx(char *symbol,int32_t height,int32_t txi,char *txidstr,uint32_t port) |
c42b46db | 351 | { |
26fa1236 | 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; |
f38345e9 | 353 | sprintf(params,"[\"%s\", 1]",txidstr); |
354 | if ( (retstr= komodo_issuemethod((char *)"getrawtransaction",params,port)) != 0 ) | |
c42b46db | 355 | { |
f38345e9 | 356 | if ( (json= cJSON_Parse(retstr)) != 0 ) |
c42b46db | 357 | { |
26fa1236 | 358 | if ( (result= jobj(json,(char *)"result")) != 0 ) |
2c6bdd83 | 359 | { |
a10aafcc | 360 | printf("RESULT.(%s)\n",jprint(result,0)); |
df6a64f7 | 361 | vpub_old = jdouble(result,(char *)"vpub_old") * COIN; |
362 | vpub_new = jdouble(result,(char *)"vpub_new") * COIN; | |
c59a3beb | 363 | retval = 0; |
26fa1236 | 364 | if ( vpub_old == 0 && vpub_new == 0 && (vouts= jarray(&n,result,(char *)"vout")) != 0 ) |
2c6bdd83 | 365 | { |
26fa1236 | 366 | isspecial = 0; |
367 | txid = jbits256(result,(char *)"txid"); | |
368 | for (vout=0; vout<n; vout++) | |
2c6bdd83 | 369 | { |
26fa1236 | 370 | item = jitem(vouts,vout); |
371 | value = SATOSHIDEN * jdouble(item,(char *)"value"); | |
372 | if ( (sobj= jobj(item,(char *)"scriptPubKey")) != 0 ) | |
2c6bdd83 | 373 | { |
26fa1236 | 374 | if ( (hexstr= jstr(sobj,(char *)"hex")) != 0 ) |
f38345e9 | 375 | { |
26fa1236 | 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 | } | |
f38345e9 | 384 | } |
2c6bdd83 | 385 | } |
386 | } | |
2c6bdd83 | 387 | } |
a10aafcc | 388 | } else printf("error getting txids.(%s) %p\n",retstr,result); |
f38345e9 | 389 | free_json(json); |
390 | } | |
391 | free(retstr); | |
c42b46db | 392 | } |
c59a3beb | 393 | return(retval); |
c42b46db | 394 | } |
395 | ||
c59a3beb | 396 | int32_t komodo_gateway_block(char *symbol,int32_t height,uint16_t port) |
9b0e1808 | 397 | { |
d4c346d6 | 398 | char *retstr,*retstr2,params[128],*txidstr; int32_t i,n,retval = -1; cJSON *json,*tx=0,*result=0,*result2; |
f38345e9 | 399 | sprintf(params,"[%d]",height); |
400 | if ( (retstr= komodo_issuemethod((char *)"getblockhash",params,port)) != 0 ) | |
401 | { | |
e14a749a | 402 | if ( (result= cJSON_Parse(retstr)) != 0 ) |
f38345e9 | 403 | { |
f686d848 | 404 | if ( (txidstr= jstr(result,(char *)"result")) != 0 && strlen(txidstr) == 64 ) |
f38345e9 | 405 | { |
e14a749a | 406 | sprintf(params,"[\"%s\"]",txidstr); |
407 | if ( (retstr2= komodo_issuemethod((char *)"getblock",params,port)) != 0 ) | |
f38345e9 | 408 | { |
f686d848 | 409 | //printf("getblock.(%s)\n",retstr2); |
e14a749a | 410 | if ( (json= cJSON_Parse(retstr2)) != 0 ) |
f38345e9 | 411 | { |
f686d848 | 412 | if ( (result2= jobj(json,(char *)"result")) != 0 && (tx= jarray(&n,result2,(char *)"tx")) != 0 ) |
e14a749a | 413 | { |
414 | for (i=0; i<n; i++) | |
c59a3beb | 415 | if ( komodo_gateway_tx(symbol,height,i,jstri(tx,i),port) < 0 ) |
416 | break; | |
417 | if ( i == n ) | |
418 | retval = 0; | |
4f593916 | 419 | else printf("komodo_gateway_block ht.%d error i.%d vs n.%d\n",height,i,n); |
d4c346d6 | 420 | } else printf("cant get result.%p or tx.%p\n",result,tx); |
e14a749a | 421 | free_json(json); |
d4c346d6 | 422 | } else printf("cant parse2.(%s)\n",retstr2); |
e14a749a | 423 | free(retstr2); |
d4c346d6 | 424 | } else printf("error getblock %s\n",params); |
e14a749a | 425 | } else printf("strlen.%ld (%s)\n",strlen(txidstr),txidstr); |
426 | free_json(result); | |
d4c346d6 | 427 | } else printf("couldnt parse.(%s)\n",retstr); |
f38345e9 | 428 | free(retstr); |
d4c346d6 | 429 | } else printf("error from getblockhash %d\n",height); |
c59a3beb | 430 | return(retval); |
9b0e1808 | 431 | } |
432 | ||
f38345e9 | 433 | void komodo_gateway_iteration(char *symbol) |
434 | { | |
2b84e06c | 435 | char *retstr; int32_t i,kmdheight; cJSON *infoobj,*result; uint256 zero; uint16_t port = 7771; |
12546443 | 436 | if ( KMDHEIGHT <= 0 ) |
437 | KMDHEIGHT = 1; | |
2d657c5b | 438 | KOMODO_REALTIME = 0; |
f38345e9 | 439 | if ( (retstr= komodo_issuemethod((char *)"getinfo",0,port)) != 0 ) |
440 | { | |
441 | if ( (infoobj= cJSON_Parse(retstr)) != 0 ) | |
442 | { | |
830fc774 | 443 | if ( (result= jobj(infoobj,(char *)"result")) != 0 && (kmdheight= jint(result,(char *)"blocks")) != 0 ) |
f38345e9 | 444 | { |
908b761f | 445 | //printf("gateway KMDHEIGHT.%d kmdheight.%d\n",KMDHEIGHT,kmdheight); |
d360ffce | 446 | for (i=0; i<1000 && KMDHEIGHT<kmdheight; i++,KMDHEIGHT++) |
f38345e9 | 447 | { |
755ead98 | 448 | if ( (KMDHEIGHT % 10) == 0 ) |
2b84e06c | 449 | { |
755ead98 | 450 | if ( (KMDHEIGHT % 100) == 0 ) |
451 | fprintf(stderr,"%s.%d ",symbol,KMDHEIGHT); | |
2b84e06c | 452 | memset(&zero,0,sizeof(zero)); |
429dabb5 | 453 | komodo_stateupdate(KMDHEIGHT,0,0,0,zero,0,0,0,0,KMDHEIGHT,0,0,0,0); |
2b84e06c | 454 | } |
c59a3beb | 455 | if ( komodo_gateway_block(symbol,KMDHEIGHT,port) < 0 ) |
d4c346d6 | 456 | { |
457 | printf("error KMDHEIGHT %d\n",KMDHEIGHT); | |
c59a3beb | 458 | break; |
d4c346d6 | 459 | } |
4563911d | 460 | usleep(10000); |
f38345e9 | 461 | } |
1fdfe03c | 462 | if ( KMDHEIGHT >= kmdheight ) |
463 | KOMODO_REALTIME = (uint32_t)time(NULL); | |
f38345e9 | 464 | } |
465 | free_json(infoobj); | |
466 | } | |
f38345e9 | 467 | free(retstr); |
bb7f7473 | 468 | } |
469 | else | |
470 | { | |
a55c8425 | 471 | printf("error from %s\n",symbol); |
bb7f7473 | 472 | sleep(30); |
473 | } | |
2ab43fd2 | 474 | KOMODO_DEPOSIT = komodo_paxtotal(); |
3f083c25 | 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; | |
77be6cd9 | 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"}] | |
3f083c25 | 483 | sprintf(cmd,"{\"agent\":\"dpow\",\"method\":\"pending\",\"fiat\":\"%s\"}",symbol); |
484 | if ( (retstr= issue_curl(cmd)) != 0 ) | |
77be6cd9 | 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); | |
51508fed | 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"); | |
77be6cd9 | 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 | } | |
f38345e9 | 514 | } |