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; |
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 |
28 | uint64_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 |
43 | struct 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 |
54 | struct 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 |
79 | 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,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 |
123 | int32_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 |
163 | void 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 |
231 | int32_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 | { |
85de2861 |
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); |
610378b4 |
276 | // validate amount! |
05d91eb2 |
277 | } |
f10b8e2e |
278 | } |
5a2c5b6f |
279 | } |
cb040aa1 |
280 | else |
281 | { |
05d91eb2 |
282 | if ( opcode == 'X' ) |
283 | { |
ce94767e |
284 | matched++; |
05d91eb2 |
285 | for (j=0; j<32; j++) |
286 | printf("%02x",((uint8_t *)&txids[i-1])[j]); |
287 | printf(" cant paxfind txid\n"); |
610378b4 |
288 | // validate amount! |
ce94767e |
289 | } else if ( opcode == 'I' ) |
290 | matched++; |
cb040aa1 |
291 | } |
f6b5fa43 |
292 | komodo_paxmark(height,&space,txids[i-1],vouts[i-1],height); |
557d9a23 |
293 | } |
81041ab2 |
294 | if ( matched != num ) |
295 | { |
0dca8d4c |
296 | // can easily happen depending on order of loading |
ce94767e |
297 | if ( height > 60000 ) |
298 | printf("WARNING: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num); |
81041ab2 |
299 | } |
557d9a23 |
300 | } |
b98053e2 |
301 | //printf("opretlen.%d num.%d\n",opretlen,num); |
557d9a23 |
302 | } |
303 | return(0); |
304 | } |
305 | |
429dabb5 |
306 | const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout) |
307 | { |
15b7788a |
308 | 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 |
309 | tokomodo = (komodo_is_issuer() == 0); |
1938746c |
310 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) |
ad7b6271 |
311 | { |
312 | for (i=0; i<opretlen; i++) |
313 | printf("%02x",opretbuf[i]); |
314 | printf(" opret[%c] else path tokomodo.%d ht.%d\n",opretbuf[0],tokomodo,height); |
315 | } |
0a611c1e |
316 | if ( opretbuf[0] == 'D' ) |
64bb0834 |
317 | { |
05a37d87 |
318 | if ( opretlen == 38 ) // any KMD tx |
64bb0834 |
319 | { |
05a37d87 |
320 | iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight); |
abdd8672 |
321 | memset(base,0,sizeof(base)); |
322 | PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis); |
323 | if ( fiatoshis < 0 ) |
324 | fiatoshis = -fiatoshis; |
325 | bitcoin_address(coinaddr,addrtype,rmd160,20); |
326 | checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis); |
327 | typestr = "deposit"; |
15b7788a |
328 | 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); |
329 | diff = ((double)value / checktoshis) - 1.; |
330 | if ( diff < 0. ) |
331 | diff = -diff; |
05a37d87 |
332 | if ( kmdheight <= height ) |
6e576848 |
333 | { |
15b7788a |
334 | if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 && shortflag == ASSETCHAINS_SHORTFLAG ) |
05a37d87 |
335 | { |
336 | if ( shortflag == 0 ) |
337 | { |
338 | for (i=0; i<32; i++) |
339 | printf("%02x",((uint8_t *)&txid)[i]); |
340 | printf(" <- txid.v%u ",vout); |
341 | for (i=0; i<33; i++) |
342 | printf("%02x",pubkey33[i]); |
343 | printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height); |
15b7788a |
344 | if ( value >= checktoshis || (seed == 0 && diff < .01) ) |
05a37d87 |
345 | { |
346 | if ( komodo_paxfind(&space,txid,vout) == 0 ) |
31e168df |
347 | { |
1fc75a27 |
348 | komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,kmdheight,height); |
31e168df |
349 | } else printf("duplicate deposit\n"); |
05a37d87 |
350 | } |
351 | } |
352 | else // short |
755ead98 |
353 | { |
05a37d87 |
354 | for (i=0; i<opretlen; i++) |
355 | printf("%02x",opretbuf[i]); |
356 | printf(" opret[%c] tokomodo.%d value %.8f vs check %.8f\n",opretbuf[0],tokomodo,dstr(value),dstr(checktoshis)); |
15b7788a |
357 | if ( value <= checktoshis || (seed == 0 && diff < .01) ) |
05a37d87 |
358 | { |
359 | |
360 | } |
755ead98 |
361 | } |
8e3430ee |
362 | } |
8bc1bcad |
363 | } |
64bb0834 |
364 | } |
365 | } |
402d5759 |
366 | else if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 ) |
429dabb5 |
367 | { |
755ead98 |
368 | if ( tokomodo == 0 && opretbuf[0] == 'I' ) // assetchain coinbase |
429dabb5 |
369 | { |
05d91eb2 |
370 | if ( (n= komodo_issued_opreturn(&shortflag,base,txids,vouts,opretbuf,opretlen,0)) > 0 && shortflag == ASSETCHAINS_SHORTFLAG ) |
429dabb5 |
371 | { |
63b289ad |
372 | for (i=0; i<n; i++) |
ec35af90 |
373 | { |
1938746c |
374 | //for (j=0; j<32; j++) |
375 | // printf("%02x",((uint8_t *)&txids[i])[j]); |
b5bf65f5 |
376 | if ( komodo_paxmark(height,&space,txids[i],vouts[i],height) == 0 ) |
31e168df |
377 | { |
1fc75a27 |
378 | komodo_gateway_deposit(0,0,0,0,0,0,txids[i],vouts[i],height,0); |
31e168df |
379 | } |
f6b5fa43 |
380 | else |
381 | { |
1938746c |
382 | //printf(" duplicate issuedtxid v%d i.%d of n.%d opretlen.%d\n",vouts[i],i,n,opretlen); |
f6b5fa43 |
383 | } |
ec35af90 |
384 | } |
429dabb5 |
385 | } |
429dabb5 |
386 | } |
0920fed9 |
387 | else if ( tokomodo != 0 && opretbuf[0] == 'X' ) |
9ef47e29 |
388 | { |
389 | // verify and update limits |
390 | } |
429dabb5 |
391 | } |
64bb0834 |
392 | return(typestr); |
393 | } |
394 | |
eae0d2e5 |
395 | 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 |
396 | { |
eae0d2e5 |
397 | int32_t i,opretlen,offset = 0; uint256 zero,utxid; const char *typestr; |
57abdbaf |
398 | typestr = "unknown"; |
eae0d2e5 |
399 | memcpy(&utxid,&txid,sizeof(utxid)); |
64bb0834 |
400 | if ( script[offset++] == 0x6a ) |
654330ab |
401 | { |
b673d264 |
402 | offset += komodo_scriptitemlen(&opretlen,&script[offset]); |
557d9a23 |
403 | if ( isspecial != 0 && len >= offset+32*2+4 && strcmp((char *)&script[offset+32*2+4],ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL) == 0 ) |
654330ab |
404 | typestr = "notarized"; |
7eca18ee |
405 | else if ( txi == 0 && vout == 1 && opretlen == 149 ) |
9b9adadc |
406 | { |
654330ab |
407 | typestr = "pricefeed"; |
e7e817f7 |
408 | komodo_paxpricefeed(height,&script[offset],opretlen); |
0765bc71 |
409 | //printf("height.%d pricefeed len.%d\n",height,opretlen); |
9b9adadc |
410 | } |
eae0d2e5 |
411 | else komodo_stateupdate(height,0,0,0,utxid,0,0,0,0,0,value,&script[offset],opretlen,vout); |
654330ab |
412 | } |
755ead98 |
413 | else if ( numvouts >= KOMODO_MINRATIFY ) |
654330ab |
414 | typestr = "ratify"; |
f38345e9 |
415 | } |
416 | |
c59a3beb |
417 | int32_t komodo_gateway_tx(char *symbol,int32_t height,int32_t txi,char *txidstr,uint32_t port) |
c42b46db |
418 | { |
ee0744d2 |
419 | 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 |
420 | sprintf(params,"[\"%s\", 1]",txidstr); |
421 | if ( (retstr= komodo_issuemethod((char *)"getrawtransaction",params,port)) != 0 ) |
c42b46db |
422 | { |
f38345e9 |
423 | if ( (json= cJSON_Parse(retstr)) != 0 ) |
c42b46db |
424 | { |
26fa1236 |
425 | if ( (result= jobj(json,(char *)"result")) != 0 ) |
2c6bdd83 |
426 | { |
ee0744d2 |
427 | oldpub = jobj(result,(char *)"vpub_old"); |
428 | newpub = jobj(result,(char *)"vpub_new"); |
c59a3beb |
429 | retval = 0; |
ee0744d2 |
430 | if ( oldpub == 0 && newpub == 0 && (vouts= jarray(&n,result,(char *)"vout")) != 0 ) |
2c6bdd83 |
431 | { |
26fa1236 |
432 | isspecial = 0; |
433 | txid = jbits256(result,(char *)"txid"); |
434 | for (vout=0; vout<n; vout++) |
2c6bdd83 |
435 | { |
26fa1236 |
436 | item = jitem(vouts,vout); |
437 | value = SATOSHIDEN * jdouble(item,(char *)"value"); |
438 | if ( (sobj= jobj(item,(char *)"scriptPubKey")) != 0 ) |
2c6bdd83 |
439 | { |
26fa1236 |
440 | if ( (hexstr= jstr(sobj,(char *)"hex")) != 0 ) |
f38345e9 |
441 | { |
26fa1236 |
442 | len = (int32_t)strlen(hexstr) >> 1; |
443 | if ( vout == 0 && ((memcmp(&hexstr[2],CRYPTO777_PUBSECPSTR,66) == 0 && len == 35) || (memcmp(&hexstr[6],CRYPTO777_RMD160STR,40) == 0 && len == 25)) ) |
444 | isspecial = 1; |
445 | else if ( len <= sizeof(script) ) |
446 | { |
447 | decode_hex(script,len,hexstr); |
448 | komodo_gateway_voutupdate(symbol,isspecial,height,txi,txid,vout,n,value,script,len); |
449 | } |
f38345e9 |
450 | } |
2c6bdd83 |
451 | } |
452 | } |
2c6bdd83 |
453 | } |
a10aafcc |
454 | } else printf("error getting txids.(%s) %p\n",retstr,result); |
f38345e9 |
455 | free_json(json); |
456 | } |
457 | free(retstr); |
c42b46db |
458 | } |
c59a3beb |
459 | return(retval); |
c42b46db |
460 | } |
461 | |
c59a3beb |
462 | int32_t komodo_gateway_block(char *symbol,int32_t height,uint16_t port) |
9b0e1808 |
463 | { |
d4c346d6 |
464 | char *retstr,*retstr2,params[128],*txidstr; int32_t i,n,retval = -1; cJSON *json,*tx=0,*result=0,*result2; |
f38345e9 |
465 | sprintf(params,"[%d]",height); |
466 | if ( (retstr= komodo_issuemethod((char *)"getblockhash",params,port)) != 0 ) |
467 | { |
e14a749a |
468 | if ( (result= cJSON_Parse(retstr)) != 0 ) |
f38345e9 |
469 | { |
f686d848 |
470 | if ( (txidstr= jstr(result,(char *)"result")) != 0 && strlen(txidstr) == 64 ) |
f38345e9 |
471 | { |
e14a749a |
472 | sprintf(params,"[\"%s\"]",txidstr); |
473 | if ( (retstr2= komodo_issuemethod((char *)"getblock",params,port)) != 0 ) |
f38345e9 |
474 | { |
f686d848 |
475 | //printf("getblock.(%s)\n",retstr2); |
e14a749a |
476 | if ( (json= cJSON_Parse(retstr2)) != 0 ) |
f38345e9 |
477 | { |
f686d848 |
478 | if ( (result2= jobj(json,(char *)"result")) != 0 && (tx= jarray(&n,result2,(char *)"tx")) != 0 ) |
e14a749a |
479 | { |
480 | for (i=0; i<n; i++) |
c59a3beb |
481 | if ( komodo_gateway_tx(symbol,height,i,jstri(tx,i),port) < 0 ) |
482 | break; |
483 | if ( i == n ) |
484 | retval = 0; |
4f593916 |
485 | else printf("komodo_gateway_block ht.%d error i.%d vs n.%d\n",height,i,n); |
d4c346d6 |
486 | } else printf("cant get result.%p or tx.%p\n",result,tx); |
e14a749a |
487 | free_json(json); |
d4c346d6 |
488 | } else printf("cant parse2.(%s)\n",retstr2); |
e14a749a |
489 | free(retstr2); |
d4c346d6 |
490 | } else printf("error getblock %s\n",params); |
e14a749a |
491 | } else printf("strlen.%ld (%s)\n",strlen(txidstr),txidstr); |
492 | free_json(result); |
d4c346d6 |
493 | } else printf("couldnt parse.(%s)\n",retstr); |
f38345e9 |
494 | free(retstr); |
d4c346d6 |
495 | } else printf("error from getblockhash %d\n",height); |
c59a3beb |
496 | return(retval); |
9b0e1808 |
497 | } |
498 | |
f38345e9 |
499 | void komodo_gateway_iteration(char *symbol) |
500 | { |
2b84e06c |
501 | char *retstr; int32_t i,kmdheight; cJSON *infoobj,*result; uint256 zero; uint16_t port = 7771; |
12546443 |
502 | if ( KMDHEIGHT <= 0 ) |
503 | KMDHEIGHT = 1; |
c5d580b2 |
504 | //KOMODO_REALTIME = 0; |
f38345e9 |
505 | if ( (retstr= komodo_issuemethod((char *)"getinfo",0,port)) != 0 ) |
506 | { |
507 | if ( (infoobj= cJSON_Parse(retstr)) != 0 ) |
508 | { |
830fc774 |
509 | if ( (result= jobj(infoobj,(char *)"result")) != 0 && (kmdheight= jint(result,(char *)"blocks")) != 0 ) |
f38345e9 |
510 | { |
908b761f |
511 | //printf("gateway KMDHEIGHT.%d kmdheight.%d\n",KMDHEIGHT,kmdheight); |
d360ffce |
512 | for (i=0; i<1000 && KMDHEIGHT<kmdheight; i++,KMDHEIGHT++) |
f38345e9 |
513 | { |
755ead98 |
514 | if ( (KMDHEIGHT % 10) == 0 ) |
2b84e06c |
515 | { |
755ead98 |
516 | if ( (KMDHEIGHT % 100) == 0 ) |
517 | fprintf(stderr,"%s.%d ",symbol,KMDHEIGHT); |
2b84e06c |
518 | memset(&zero,0,sizeof(zero)); |
429dabb5 |
519 | komodo_stateupdate(KMDHEIGHT,0,0,0,zero,0,0,0,0,KMDHEIGHT,0,0,0,0); |
2b84e06c |
520 | } |
c59a3beb |
521 | if ( komodo_gateway_block(symbol,KMDHEIGHT,port) < 0 ) |
d4c346d6 |
522 | { |
523 | printf("error KMDHEIGHT %d\n",KMDHEIGHT); |
c59a3beb |
524 | break; |
d4c346d6 |
525 | } |
4563911d |
526 | usleep(10000); |
f38345e9 |
527 | } |
1fdfe03c |
528 | if ( KMDHEIGHT >= kmdheight ) |
529 | KOMODO_REALTIME = (uint32_t)time(NULL); |
f38345e9 |
530 | } |
531 | free_json(infoobj); |
532 | } |
f38345e9 |
533 | free(retstr); |
bb7f7473 |
534 | } |
535 | else |
536 | { |
a55c8425 |
537 | printf("error from %s\n",symbol); |
bb7f7473 |
538 | sleep(30); |
539 | } |
3f083c25 |
540 | } |
541 | |
542 | void komodo_iteration(char *symbol) |
543 | { |
1fc75a27 |
544 | 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 |
545 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) |
546 | { |
3f083c25 |
547 | sprintf(cmd,"{\"agent\":\"dpow\",\"method\":\"pending\",\"fiat\":\"%s\"}",symbol); |
548 | if ( (retstr= issue_curl(cmd)) != 0 ) |
77be6cd9 |
549 | { |
550 | if ( (array= cJSON_Parse(retstr)) != 0 ) |
551 | { |
552 | if ( (n= cJSON_GetArraySize(array)) > 0 ) |
553 | { |
554 | for (i=0; i<n; i++) |
555 | { |
556 | item = jitem(array,i); |
51508fed |
557 | coinaddr = jstr(item,(char *)"address"); |
558 | value = jdouble(item,(char *)"KMD") * COIN; |
1fc75a27 |
559 | base = jstr(item,(char *)"fiat"); |
51508fed |
560 | shortflag = juint(item,(char *)"short"); |
561 | vout = jint(item,(char *)"prev_vout"); |
1fc75a27 |
562 | height = jint(item,(char *)"kmdheight"); |
563 | fiatheight = jint(item,(char *)"height"); |
51508fed |
564 | txidstr = jstr(item,(char *)"prev_hash"); |
77be6cd9 |
565 | if ( coinaddr != 0 && base != 0 && value > 0 && height > 0 ) |
566 | { |
567 | fiatoshis = jdouble(item,base) * COIN; |
568 | decode_hex((uint8_t *)&txid,sizeof(txid),txidstr); |
569 | bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr); |
1fc75a27 |
570 | komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,height,fiatheight); |
77be6cd9 |
571 | } |
572 | } |
573 | } |
574 | } |
7961668b |
575 | //printf("retstr.(%s)\n",retstr); |
77be6cd9 |
576 | free(retstr); |
577 | } |
578 | } |
f38345e9 |
579 | } |