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 | |
3cbfd2b4 |
18 | int32_t pax_fiatstatus(uint64_t *deposited,uint64_t *issued,uint64_t *withdrawn,uint64_t *approved,uint64_t *redeemed,char *base) |
19 | { |
20 | int32_t baseid; struct komodo_state *sp; |
21 | if ( (baseid= komodo_baseid(base)) >= 0 ) |
22 | { |
23 | if ( (sp= komodo_stateptrget(base)) != 0 ) |
24 | { |
25 | *deposited = sp->deposited; |
26 | *issued = sp->issued; |
27 | *withdrawn = sp->withdrawn; |
28 | *approved = sp->approved; |
29 | *redeemed = sp->redeemed; |
1db587b5 |
30 | //printf("%p %s %.8f %.8f %.8f %.8f %.8f\n",sp,base,dstr(*deposited),dstr(*issued),dstr(*withdrawn),dstr(*approved),dstr(*redeemed)); |
3cbfd2b4 |
31 | return(0); |
ee5e5744 |
32 | } else printf("pax_fiatstatus cant get basesp.%s\n",base); |
33 | } else printf("pax_fiatstatus illegal base.%s\n",base); |
3cbfd2b4 |
34 | return(-1); |
35 | } |
36 | |
dfe40c43 |
37 | struct pax_transaction *komodo_paxfind(uint256 txid,uint16_t vout) |
6210f328 |
38 | { |
39 | struct pax_transaction *pax; |
40 | pthread_mutex_lock(&komodo_mutex); |
41 | HASH_FIND(hh,PAX,&txid,sizeof(txid),pax); |
6210f328 |
42 | pthread_mutex_unlock(&komodo_mutex); |
43 | return(pax); |
44 | } |
45 | |
dfe40c43 |
46 | struct pax_transaction *komodo_paxmark(int32_t height,uint256 txid,uint16_t vout,int32_t mark) |
6210f328 |
47 | { |
48 | struct pax_transaction *pax; |
49 | pthread_mutex_lock(&komodo_mutex); |
50 | HASH_FIND(hh,PAX,&txid,sizeof(txid),pax); |
2ab43fd2 |
51 | if ( pax == 0 ) |
52 | { |
53 | pax = (struct pax_transaction *)calloc(1,sizeof(*pax)); |
54 | pax->txid = txid; |
55 | pax->vout = vout; |
56 | HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax); |
32d525eb |
57 | //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark); |
2ab43fd2 |
58 | } |
6210f328 |
59 | if ( pax != 0 ) |
60 | { |
61 | pax->marked = mark; |
f1e6bb01 |
62 | //int32_t i; for (i=0; i<32; i++) |
63 | // printf("%02x",((uint8_t *)&txid)[i]); |
64 | //printf(" paxmark.ht %d vout%d\n",mark,vout); |
6210f328 |
65 | } |
66 | pthread_mutex_unlock(&komodo_mutex); |
67 | return(pax); |
68 | } |
429dabb5 |
69 | |
d6be719d |
70 | void komodo_gateway_deposit(char *coinaddr,uint64_t value,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,int32_t height,int32_t otherheight,char *source,int32_t approved) // assetchain context |
d43c7a93 |
71 | { |
04652b71 |
72 | struct pax_transaction *pax; int32_t addflag = 0; struct komodo_state *sp,*basesp; char str[16],dest[16],*s; |
bffa91b2 |
73 | sp = komodo_stateptr(str,dest); |
d43c7a93 |
74 | pthread_mutex_lock(&komodo_mutex); |
75 | HASH_FIND(hh,PAX,&txid,sizeof(txid),pax); |
76 | if ( pax == 0 ) |
b2d64a0f |
77 | { |
d43c7a93 |
78 | pax = (struct pax_transaction *)calloc(1,sizeof(*pax)); |
8d52488f |
79 | pax->txid = txid; |
80 | pax->vout = vout; |
81 | HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax); |
7961668b |
82 | addflag = 1; |
1fc75a27 |
83 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) |
84 | { |
85 | int32_t i; for (i=0; i<32; i++) |
86 | printf("%02x",((uint8_t *)&txid)[i]); |
5ce8bf22 |
87 | printf(" v.%d [%s] kht.%d ht.%d create pax.%p symbol.%s source.%s\n",vout,ASSETCHAINS_SYMBOL,height,otherheight,pax,symbol,source); |
1fc75a27 |
88 | } |
b2d64a0f |
89 | } |
8d52488f |
90 | pthread_mutex_unlock(&komodo_mutex); |
d43c7a93 |
91 | if ( coinaddr != 0 ) |
92 | { |
93 | strcpy(pax->coinaddr,coinaddr); |
9ccd3f33 |
94 | if ( value != 0 ) |
95 | pax->komodoshis = value; |
96 | if ( symbol != 0 ) |
97 | strcpy(pax->symbol,symbol); |
cd26c1f3 |
98 | if ( source != 0 ) |
99 | strcpy(pax->source,source); |
9ccd3f33 |
100 | if ( fiatoshis != 0 ) |
101 | pax->fiatoshis = fiatoshis; |
102 | if ( rmd160 != 0 ) |
103 | memcpy(pax->rmd160,rmd160,20); |
104 | if ( height != 0 ) |
105 | pax->height = height; |
106 | if ( otherheight != 0 ) |
107 | pax->otherheight = otherheight; |
02862b72 |
108 | if ( pax->didstats == 0 && fiatoshis != 0 ) |
7961668b |
109 | { |
1db587b5 |
110 | if ( (pax->approved= approved) != 0 ) |
d6be719d |
111 | { |
1db587b5 |
112 | if ( (basesp= komodo_stateptrget(symbol)) != 0 ) |
bafc0147 |
113 | { |
042e3b9f |
114 | pax->didstats = 1; |
1db587b5 |
115 | basesp->approved += fiatoshis; |
116 | printf("########### %p approved %s += %.8f\n",basesp,symbol,dstr(fiatoshis)); |
117 | } |
118 | } |
119 | else |
120 | { |
121 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) |
122 | { |
33319f82 |
123 | if ( (basesp= komodo_stateptrget(symbol)) != 0 ) |
0fa85cc3 |
124 | { |
042e3b9f |
125 | pax->didstats = 1; |
1db587b5 |
126 | basesp->withdrawn += fiatoshis; |
33319f82 |
127 | printf("########### %p withdrawn %s += %.8f\n",basesp,symbol,dstr(fiatoshis)); |
0fa85cc3 |
128 | } |
bafc0147 |
129 | } |
130 | else |
131 | { |
1db587b5 |
132 | if ( (basesp= komodo_stateptrget(symbol)) != 0 ) |
bafc0147 |
133 | { |
042e3b9f |
134 | pax->didstats = 1; |
1db587b5 |
135 | basesp->deposited += fiatoshis; |
136 | printf("########### %p deposited %s += %.8f\n",basesp,symbol,dstr(fiatoshis)); |
bafc0147 |
137 | } |
138 | } |
1db587b5 |
139 | } |
1db587b5 |
140 | } |
141 | if ( pax->marked == 0 ) |
142 | { |
143 | if ( addflag != 0 ) |
144 | { |
145 | if ( (pax->approved= approved) != 0 ) |
146 | s = (char *)"APPROVED"; |
147 | else s = (char *)((ASSETCHAINS_SYMBOL[0] == 0) ? "WITHDRAW" : "DEPOSIT"); |
3cbfd2b4 |
148 | printf("[%s] addflag.%d ADD %s/%s %s %.8f -> %s TO PAX ht.%d otherht.%d total %.8f\n",ASSETCHAINS_SYMBOL,addflag,s,symbol,source,dstr(ASSETCHAINS_SYMBOL[0]==0?pax->komodoshis:pax->fiatoshis),pax->coinaddr,pax->height,pax->otherheight,dstr(komodo_paxtotal())); |
d6be719d |
149 | } |
7961668b |
150 | } |
0dca8d4c |
151 | //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 |
152 | } |
153 | else |
154 | { |
155 | pax->marked = height; |
1fc75a27 |
156 | printf("pax.%p MARK DEPOSIT ht.%d other.%d\n",pax,height,otherheight); |
d43c7a93 |
157 | } |
d43c7a93 |
158 | } |
159 | |
7cc6844b |
160 | int32_t komodo_rwapproval(int32_t rwflag,uint8_t *opretbuf,struct pax_transaction *pax) |
161 | { |
162 | int32_t i,len = 0; |
ddf827b2 |
163 | if ( rwflag == 1 ) |
164 | { |
165 | for (i=0; i<32; i++) |
166 | opretbuf[len++] = ((uint8_t *)&pax->txid)[i]; |
167 | opretbuf[len++] = pax->vout & 0xff; |
168 | opretbuf[len++] = (pax->vout >> 8) & 0xff; |
169 | } |
170 | else |
171 | { |
172 | for (i=0; i<32; i++) |
a6972662 |
173 | ((uint8_t *)&pax->txid)[i] = opretbuf[len++]; |
fbd1641c |
174 | for (i=0; i<32; i++) |
a6972662 |
175 | printf("%02x",((uint8_t *)&pax->txid)[31-i]); |
ddf827b2 |
176 | pax->vout = opretbuf[len++]; |
177 | pax->vout += ((uint32_t)opretbuf[len++] << 8); |
ad66994d |
178 | printf(" txid v.%d\n",pax->vout); |
ddf827b2 |
179 | } |
ad66994d |
180 | len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->komodoshis),&pax->komodoshis); |
ddf827b2 |
181 | len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->fiatoshis),&pax->fiatoshis); |
182 | len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->height),&pax->height); |
183 | len += iguana_rwnum(rwflag,&opretbuf[len],sizeof(pax->otherheight),&pax->otherheight); |
184 | if ( rwflag != 0 ) |
185 | { |
186 | memcpy(&opretbuf[len],pax->rmd160,20), len += 20; |
187 | for (i=0; i<4; i++) |
188 | opretbuf[len++] = pax->source[i]; |
189 | } |
190 | else |
191 | { |
192 | memcpy(pax->rmd160,&opretbuf[len],20), len += 20; |
193 | for (i=0; i<4; i++) |
194 | pax->source[i] = opretbuf[len++]; |
195 | } |
7cc6844b |
196 | return(len); |
197 | } |
198 | |
b89ab77c |
199 | int32_t komodo_issued_opreturn(char *base,uint256 *txids,uint16_t *vouts,int64_t *values,int64_t *srcvalues,int32_t *kmdheights,int32_t *otherheights,int8_t *baseids,uint8_t *rmd160s,uint8_t *opretbuf,int32_t opretlen,int32_t iskomodo) |
63b289ad |
200 | { |
54661780 |
201 | struct pax_transaction p,*pax; int32_t i,n=0,j,len=0,incr,height,otherheight; uint8_t type,rmd160[20]; uint64_t fiatoshis; char symbol[16]; |
16bed6ac |
202 | incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4)); |
63b289ad |
203 | for (i=0; i<4; i++) |
204 | base[i] = opretbuf[opretlen-4+i]; |
0e1af8ed |
205 | for (i=0; i<opretlen; i++) |
206 | printf("%02x",opretbuf[i]); |
207 | printf(" opretlen.%d (%s)\n",opretlen,base); |
6d898f42 |
208 | //printf(" opretlen.%d vs %d incr.%d\n",opretlen,(int32_t)(2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4),incr); |
986df802 |
209 | if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) |
63b289ad |
210 | { |
947d956f |
211 | type = opretbuf[0]; |
63b289ad |
212 | opretbuf++, opretlen--; |
e8433f7a |
213 | for (n=0; n<opretlen/incr; n++) |
63b289ad |
214 | { |
05d91eb2 |
215 | if ( iskomodo != 0 ) |
216 | { |
e8433f7a |
217 | memset(&p,0,sizeof(p)); |
ffec138e |
218 | len += komodo_rwapproval(0,&opretbuf[len],&p); |
b89ab77c |
219 | if ( values != 0 && srcvalues != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 && rmd160s != 0 ) |
1ed46112 |
220 | { |
63342b19 |
221 | txids[n] = p.txid; |
222 | vouts[n] = p.vout; |
a3f1fb69 |
223 | values[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? p.komodoshis : p.fiatoshis; |
b89ab77c |
224 | srcvalues[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? p.fiatoshis : p.komodoshis; |
a3f1fb69 |
225 | kmdheights[n] = p.height; |
226 | otherheights[n] = p.otherheight; |
7cc6844b |
227 | memcpy(&rmd160s[n * 20],p.rmd160,20); |
1dbea872 |
228 | baseids[n] = komodo_baseid(p.source); |
71c9af7c |
229 | { |
230 | char coinaddr[64]; |
231 | bitcoin_address(coinaddr,60,&rmd160s[n * 20],20); |
947d956f |
232 | printf(">>>>>>> %s: (%s) fiat %.8f kmdheight.%d other.%d -> %s %.8f\n",type=='A'?"approvedA":"issuedX",baseids[n]>=0?CURRENCIES[baseids[n]]:"???",dstr(p.fiatoshis),kmdheights[n],otherheights[n],coinaddr,dstr(values[n])); |
71c9af7c |
233 | } |
52e87248 |
234 | } |
05d91eb2 |
235 | } |
7cc6844b |
236 | else |
237 | { |
238 | for (j=0; j<32; j++) |
239 | { |
240 | ((uint8_t *)&txids[n])[j] = opretbuf[len++]; |
241 | //printf("%02x",((uint8_t *)&txids[n])[j]); |
242 | } |
243 | vouts[n] = opretbuf[len++]; |
244 | vouts[n] = (opretbuf[len++] << 8) | vouts[n]; |
cb3bf8df |
245 | baseids[n] = komodo_baseid(base); |
54661780 |
246 | if ( (pax= komodo_paxfind(txids[n],vouts[n])) != 0 ) |
247 | { |
248 | values[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? pax->komodoshis : pax->fiatoshis; |
249 | srcvalues[n] = (ASSETCHAINS_SYMBOL[0] == 0) ? pax->fiatoshis : pax->komodoshis; |
250 | kmdheights[n] = pax->height; |
251 | otherheights[n] = pax->otherheight; |
252 | memcpy(&rmd160s[n * 20],pax->rmd160,20); |
253 | } |
7cc6844b |
254 | } |
6d898f42 |
255 | //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen); |
63b289ad |
256 | } |
257 | } |
258 | return(n); |
259 | } |
260 | |
f1db88ea |
261 | uint64_t komodo_paxtotal() |
262 | { |
a73709a3 |
263 | struct pax_transaction *pax,*pax2,*tmp,*tmp2; char symbol[16],dest[16],*str; int32_t i,ht; int64_t checktoshis; uint64_t seed,total = 0; struct komodo_state *basesp; |
48a3cd18 |
264 | if ( komodo_isrealtime(&ht) == 0 ) |
f1db88ea |
265 | return(0); |
ca75995d |
266 | else |
267 | { |
268 | HASH_ITER(hh,PAX,pax,tmp) |
269 | { |
16e7ab0f |
270 | //if ( pax->didstats == 0 && pax->type == 'I' ) |
ca75995d |
271 | { |
042e3b9f |
272 | //for (i=0; i<32; i++) |
273 | // printf("%02x",((uint8_t *)&pax->txid)[i]); |
ceabe3df |
274 | if ( pax->type == 'X' || pax->type == 'A' || pax->type == 'D' || pax->type == 'I' ) |
a73709a3 |
275 | str = pax->symbol; |
276 | else str = pax->source; |
c5f534f4 |
277 | if ( komodo_baseid(str) >= 0 && pax->didstats == 0 && pax->fiatoshis != 0 ) |
ca75995d |
278 | { |
c5f534f4 |
279 | if ( (basesp= komodo_stateptrget(str)) != 0 ) |
ca75995d |
280 | { |
c5f534f4 |
281 | if ( pax->type == 'I' ) |
282 | { |
283 | basesp->issued += pax->fiatoshis; |
284 | pax->didstats = 1; |
285 | } |
286 | else if ( pax->type == 'X' ) |
287 | { |
288 | basesp->redeemed += pax->fiatoshis; |
289 | pax->didstats = 1; |
290 | } |
fa81b5ae |
291 | } |
292 | } |
4adf625a |
293 | printf(" stats.%d type.%c (%s) k.%d %.8f h.%d %.8f I.%.8f X.%.8f\n",pax->didstats,pax->type,str,pax->height,dstr(pax->komodoshis),pax->otherheight,dstr(pax->fiatoshis),dstr(basesp->issued),dstr(basesp->redeemed)); |
ca75995d |
294 | } |
295 | } |
296 | } |
ede04b12 |
297 | komodo_stateptr(symbol,dest); |
f1db88ea |
298 | HASH_ITER(hh,PAX,pax,tmp) |
299 | { |
10694486 |
300 | //printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis)); |
ede04b12 |
301 | if ( strcmp(symbol,pax->symbol) == 0 ) |
f1db88ea |
302 | { |
ede04b12 |
303 | if ( pax->marked == 0 ) |
304 | { |
305 | if ( komodo_is_issuer() != 0 ) |
306 | total += pax->fiatoshis; |
f2db1a97 |
307 | else if ( pax->approved != 0 ) |
992c3be5 |
308 | { |
309 | if ( pax->validated != 0 ) |
310 | total += pax->komodoshis; |
311 | else |
312 | { |
313 | seed = 0; |
f186b8a1 |
314 | checktoshis = komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis); |
0fa85cc3 |
315 | //printf("PAX_fiatdest ht.%d price %s %.8f -> KMD %.8f vs %.8f\n",pax->height,pax->symbol,(double)pax->fiatoshis/COIN,(double)pax->komodoshis/COIN,(double)checktoshis/COIN); |
316 | //printf(" v%d %.8f k.%d ht.%d\n",pax->vout,dstr(pax->komodoshis),pax->height,pax->otherheight); |
992c3be5 |
317 | if ( seed != 0 ) |
318 | { |
e8cbe5fb |
319 | if ( checktoshis >= pax->komodoshis ) |
992c3be5 |
320 | { |
321 | total += pax->komodoshis; |
322 | pax->validated = pax->komodoshis; |
323 | } else pax->marked = pax->height; |
324 | } |
325 | } |
326 | } |
ede04b12 |
327 | } |
f1db88ea |
328 | } |
329 | } |
986df802 |
330 | //printf("paxtotal %.8f\n",dstr(total)); |
f1db88ea |
331 | return(total); |
332 | } |
333 | |
7cc6844b |
334 | int32_t komodo_pending_withdraws(char *opretstr) |
f360ea0a |
335 | { |
ef1a9636 |
336 | struct pax_transaction *pax,*tmp; uint8_t opretbuf[16384]; int32_t ht,len=0; uint64_t total = 0; |
f360ea0a |
337 | if ( komodo_isrealtime(&ht) == 0 || ASSETCHAINS_SYMBOL[0] != 0 ) |
338 | return(0); |
339 | HASH_ITER(hh,PAX,pax,tmp) |
340 | { |
f97d3439 |
341 | //printf("pax %s marked.%u approved.%u\n",pax->symbol,pax->marked,pax->approved); |
f360ea0a |
342 | if ( pax->marked == 0 && strcmp((char *)"KMD",pax->symbol) == 0 && pax->approved == 0 ) |
343 | { |
344 | // add 'A' opreturn entry |
7cc6844b |
345 | if ( len == 0 ) |
346 | opretbuf[len++] = 'A'; |
347 | len += komodo_rwapproval(1,&opretbuf[len],pax); |
75e47988 |
348 | printf("%s.(marked.%u approved.%d) %p\n",pax->source,pax->marked,pax->approved,pax); |
f360ea0a |
349 | } |
350 | } |
7cc6844b |
351 | if ( len > 0 ) |
352 | init_hexbytes_noT(opretstr,opretbuf,len); |
353 | else opretstr[0] = 0; |
2afc2dfd |
354 | printf("komodo_pending_withdraws len.%d PAXTOTAL %.8f\n",len,dstr(komodo_paxtotal())); |
f360ea0a |
355 | return(len); |
356 | } |
a4c67285 |
357 | |
635dd34d |
358 | int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo) |
429dabb5 |
359 | { |
ef1a9636 |
360 | struct pax_transaction *pax,*tmp; char symbol[16],dest[16]; uint8_t *script,opcode,opret[16384],data[16384]; int32_t i,baseid,ht,len=0,opretlen=0,numvouts=1; struct komodo_state *sp; uint64_t mask; |
635dd34d |
361 | sp = komodo_stateptr(symbol,dest); |
27bf3c5e |
362 | strcpy(symbol,base); |
429dabb5 |
363 | PENDING_KOMODO_TX = 0; |
a3f23ee2 |
364 | if ( tokomodo == 0 ) |
48e96ba1 |
365 | { |
77be6cd9 |
366 | opcode = 'I'; |
48a3cd18 |
367 | if ( komodo_isrealtime(&ht) == 0 ) |
48e96ba1 |
368 | return(0); |
54661780 |
369 | } else opcode = 'X'; |
c169f64b |
370 | HASH_ITER(hh,PAX,pax,tmp) |
429dabb5 |
371 | { |
986df802 |
372 | //printf("pax.%s marked.%d %.8f -> %.8f\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis)); |
f186b8a1 |
373 | if ( strcmp(symbol,"KMD") == 0 && (pax->approved == 0 || pax->validated == 0) ) |
992c3be5 |
374 | continue; |
375 | if ( pax->marked != 0 || strcmp(pax->symbol,symbol) != 0 ) |
e5430f52 |
376 | continue; |
3cbfd2b4 |
377 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) |
635dd34d |
378 | printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis)); |
e5430f52 |
379 | txNew->vout.resize(numvouts+1); |
1fc75a27 |
380 | txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis; |
e5430f52 |
381 | txNew->vout[numvouts].scriptPubKey.resize(25); |
382 | script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0]; |
383 | *script++ = 0x76; |
384 | *script++ = 0xa9; |
385 | *script++ = 20; |
386 | memcpy(script,pax->rmd160,20), script += 20; |
387 | *script++ = 0x88; |
388 | *script++ = 0xac; |
a3f23ee2 |
389 | if ( tokomodo == 0 ) |
16bed6ac |
390 | { |
391 | for (i=0; i<32; i++) |
16bed6ac |
392 | data[len++] = ((uint8_t *)&pax->txid)[i]; |
16bed6ac |
393 | data[len++] = pax->vout & 0xff; |
394 | data[len++] = (pax->vout >> 8) & 0xff; |
e5430f52 |
395 | PENDING_KOMODO_TX += pax->fiatoshis; |
16bed6ac |
396 | } |
fbc4209b |
397 | else |
398 | { |
e0357628 |
399 | len += komodo_rwapproval(1,&data[len],pax); |
fbc4209b |
400 | PENDING_KOMODO_TX += pax->komodoshis; |
05d91eb2 |
401 | printf(" vout.%u DEPOSIT %.8f <- pax.%s pending %.8f | ",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,dstr(PENDING_KOMODO_TX)); |
fbc4209b |
402 | } |
e5430f52 |
403 | if ( numvouts++ >= 64 ) |
404 | break; |
429dabb5 |
405 | } |
406 | if ( numvouts > 1 ) |
407 | { |
0e3472c1 |
408 | if ( tokomodo != 0 ) |
27bf3c5e |
409 | strcpy(symbol,(char *)"KMD"); |
9ef47e29 |
410 | for (i=0; symbol[i]!=0; i++) |
411 | data[len++] = symbol[i]; |
a5ad8f02 |
412 | data[len++] = 0; |
77be6cd9 |
413 | opretlen = komodo_opreturnscript(opret,opcode,data,len); |
50824530 |
414 | txNew->vout.resize(numvouts+1); |
415 | txNew->vout[numvouts].nValue = 0; |
416 | txNew->vout[numvouts].scriptPubKey.resize(opretlen); |
417 | script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0]; |
429dabb5 |
418 | memcpy(script,opret,opretlen); |
23d955d4 |
419 | printf("MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen); |
d9a9d562 |
420 | return(1); |
9ef47e29 |
421 | } |
d9a9d562 |
422 | return(0); |
429dabb5 |
423 | } |
424 | |
8f64a3a9 |
425 | int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing |
557d9a23 |
426 | { |
b89ab77c |
427 | int32_t i,j,n,num,opretlen,offset=1,errs=0,matched=0,kmdheights[64],otherheights[64]; uint256 hash,txids[64]; char symbol[16],base[16]; uint16_t vouts[64]; int8_t baseids[64]; uint8_t *script,opcode,rmd160s[64*20]; int64_t values[64],srcvalues[64]; struct pax_transaction *pax; |
1ed46112 |
428 | memset(baseids,0xff,sizeof(baseids)); |
429 | memset(values,0,sizeof(values)); |
b89ab77c |
430 | memset(srcvalues,0,sizeof(srcvalues)); |
f360ea0a |
431 | memset(rmd160s,0,sizeof(rmd160s)); |
1ed46112 |
432 | memset(kmdheights,0,sizeof(kmdheights)); |
433 | memset(otherheights,0,sizeof(otherheights)); |
557d9a23 |
434 | n = block.vtx[0].vout.size(); |
435 | script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data(); |
12d47153 |
436 | if ( n <= 2 || script[0] != 0x6a ) |
437 | return(0); |
438 | offset += komodo_scriptitemlen(&opretlen,&script[offset]); |
77be6cd9 |
439 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) |
440 | { |
85de2861 |
441 | //for (i=0; i<opretlen; i++) |
442 | // printf("%02x",script[i]); |
443 | //printf(" height.%d checkdeposit n.%d [%02x] [%c] %d vs %d\n",height,n,script[0],script[offset],script[offset],'X'); |
77be6cd9 |
444 | opcode = 'X'; |
09a235e1 |
445 | strcpy(symbol,(char *)"KMD"); |
77be6cd9 |
446 | } |
447 | else |
448 | { |
449 | strcpy(symbol,ASSETCHAINS_SYMBOL); |
450 | opcode = 'I'; |
451 | } |
452 | if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() ) |
557d9a23 |
453 | { |
b89ab77c |
454 | if ( (num= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,&script[offset],opretlen,opcode == 'X')) > 0 ) |
557d9a23 |
455 | { |
456 | for (i=1; i<n-1; i++) |
457 | { |
dfe40c43 |
458 | if ( (pax= komodo_paxfind(txids[i-1],vouts[i-1])) != 0 ) |
5a2c5b6f |
459 | { |
ca75995d |
460 | pax->type = opcode; |
f10b8e2e |
461 | if ( ((opcode == 'I' && pax->fiatoshis == block.vtx[0].vout[i].nValue) || (opcode == 'X' && pax->komodoshis == block.vtx[0].vout[i].nValue)) ) |
462 | { |
c35c0838 |
463 | if ( pax->marked != 0 && height >= 80820 ) |
f10b8e2e |
464 | errs++; |
465 | else matched++; |
8c6619bc |
466 | if ( opcode == 'X' ) |
1d996b5f |
467 | printf("errs.%d i.%d match %.8f == %.8f pax.%p\n",errs,i,dstr(pax != 0 ? pax->komodoshis:-1),dstr(block.vtx[0].vout[i].nValue),pax); |
f10b8e2e |
468 | } |
469 | else |
470 | { |
05d91eb2 |
471 | if ( opcode == 'X' ) |
472 | { |
473 | for (j=0; j<32; j++) |
947d956f |
474 | printf("%02x",((uint8_t *)&txids[i-1])[j]); |
475 | printf(" cant paxfind X txid\n"); |
05d91eb2 |
476 | } |
f10b8e2e |
477 | } |
5a2c5b6f |
478 | } |
cb040aa1 |
479 | else |
480 | { |
05d91eb2 |
481 | if ( opcode == 'X' ) |
482 | { |
1d996b5f |
483 | hash = block.GetHash(); |
05d91eb2 |
484 | for (j=0; j<32; j++) |
947d956f |
485 | printf("%02x",((uint8_t *)&hash)[j]); |
486 | printf(" ht.%d blockhash X couldnt find vout.[%d]\n",height,i); |
ce94767e |
487 | } else if ( opcode == 'I' ) |
488 | matched++; |
cb040aa1 |
489 | } |
557d9a23 |
490 | } |
81041ab2 |
491 | if ( matched != num ) |
492 | { |
0dca8d4c |
493 | // can easily happen depending on order of loading |
7eb21300 |
494 | if ( height > 100000 && opcode == 'X' ) |
52b2e68d |
495 | { |
496 | printf("REJECT: ht.%d (%c) matched.%d vs num.%d\n",height,opcode,matched,num); |
497 | return(-1); |
498 | } |
81041ab2 |
499 | } |
557d9a23 |
500 | } |
b98053e2 |
501 | //printf("opretlen.%d num.%d\n",opretlen,num); |
557d9a23 |
502 | } |
503 | return(0); |
504 | } |
505 | |
e97061cb |
506 | int32_t komodo_paxcmp(uint64_t value,uint64_t checkvalue,uint64_t seed) |
507 | { |
e5e64f55 |
508 | int32_t ratio; |
e97061cb |
509 | if ( seed == 0 ) |
510 | { |
02862b72 |
511 | ratio = ((value << 10) / checkvalue); |
e5e64f55 |
512 | if ( ratio >= 1023 && ratio <= 1025 ) |
513 | return(0); |
02862b72 |
514 | else |
515 | { |
516 | printf("value %lld vs checkvalue %lld -> ratio.%d\n",(long long)value,(long long)checkvalue,ratio); |
517 | return(-1); |
518 | } |
e97061cb |
519 | } |
520 | return(value != checkvalue); |
521 | } |
522 | |
cd26c1f3 |
523 | const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source) |
429dabb5 |
524 | { |
e97061cb |
525 | uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33]; int32_t didstats,i,j,n,len,tokomodo,kmdheight,otherheights[64],kmdheights[64]; int8_t baseids[64]; char base[4],coinaddr[64],destaddr[64]; uint256 txids[64]; uint16_t vouts[64]; uint64_t convtoshis,seed; int64_t fiatoshis,komodoshis,checktoshis,values[64],srcvalues[64]; struct pax_transaction *pax; struct komodo_state *basesp; double diff; |
1ed46112 |
526 | const char *typestr = "unknown"; |
527 | memset(baseids,0xff,sizeof(baseids)); |
528 | memset(values,0,sizeof(values)); |
06cfcf31 |
529 | memset(srcvalues,0,sizeof(srcvalues)); |
f360ea0a |
530 | memset(rmd160s,0,sizeof(rmd160s)); |
1ed46112 |
531 | memset(kmdheights,0,sizeof(kmdheights)); |
532 | memset(otherheights,0,sizeof(otherheights)); |
95db8239 |
533 | tokomodo = (komodo_is_issuer() == 0); |
0a611c1e |
534 | if ( opretbuf[0] == 'D' ) |
64bb0834 |
535 | { |
05a37d87 |
536 | if ( opretlen == 38 ) // any KMD tx |
64bb0834 |
537 | { |
05a37d87 |
538 | iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight); |
abdd8672 |
539 | memset(base,0,sizeof(base)); |
540 | PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis); |
abdd8672 |
541 | bitcoin_address(coinaddr,addrtype,rmd160,20); |
542 | checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis); |
543 | typestr = "deposit"; |
1c6e217d |
544 | printf("(%s) (%s) kmdheight.%d vs height.%d check %.8f vs %.8f tokomodo.%d %d seed.%llx\n",ASSETCHAINS_SYMBOL,base,kmdheight,height,dstr(checktoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed); |
05a37d87 |
545 | if ( kmdheight <= height ) |
6e576848 |
546 | { |
c98b3a08 |
547 | for (i=0; i<32; i++) |
548 | printf("%02x",((uint8_t *)&txid)[i]); |
549 | printf(" <- txid.v%u ",vout); |
550 | for (i=0; i<33; i++) |
551 | printf("%02x",pubkey33[i]); |
552 | printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height); |
f5e3cdfa |
553 | didstats = 0; |
e69107f8 |
554 | if ( komodo_paxcmp(value,checktoshis,seed) == 0 ) |
05a37d87 |
555 | { |
3f9cad6d |
556 | if ( (pax= komodo_paxfind(txid,vout)) == 0 ) |
05a37d87 |
557 | { |
c98b3a08 |
558 | if ( (basesp= komodo_stateptrget(base)) != 0 ) |
05a37d87 |
559 | { |
c98b3a08 |
560 | basesp->deposited += fiatoshis; |
f5e3cdfa |
561 | didstats = 1; |
c98b3a08 |
562 | printf("########### %p deposited %s += %.8f\n",basesp,base,dstr(fiatoshis)); |
563 | } |
645ecdd5 |
564 | //if ( strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) //tokomodo == 0 && |
f5e3cdfa |
565 | { |
d6be719d |
566 | komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,kmdheight,height,(char *)"KMD",0); |
f5e3cdfa |
567 | } |
3f9cad6d |
568 | } |
a73709a3 |
569 | if ( (pax= komodo_paxfind(txid,vout)) != 0 ) |
3f9cad6d |
570 | { |
ca75995d |
571 | pax->type = opretbuf[0]; |
572 | if ( pax->didstats == 0 ) |
3f9cad6d |
573 | { |
ca75995d |
574 | if ( (basesp= komodo_stateptrget(base)) != 0 ) |
575 | { |
576 | basesp->deposited += fiatoshis; |
577 | didstats = 1; |
578 | printf("########### %p deposited %s += %.8f\n",basesp,base,dstr(fiatoshis)); |
579 | } |
3f9cad6d |
580 | } |
581 | } |
582 | if ( didstats != 0 && (pax= komodo_paxfind(txid,vout)) != 0 ) |
ca75995d |
583 | { |
3f9cad6d |
584 | pax->didstats = 1; |
ca75995d |
585 | pax->type = opretbuf[0]; |
586 | } |
8e3430ee |
587 | } |
8bc1bcad |
588 | } |
64bb0834 |
589 | } |
590 | } |
01d9ed2f |
591 | else if ( opretbuf[0] == 'W' && opretlen >= 38 ) |
592 | { |
593 | iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight); |
594 | memset(base,0,sizeof(base)); |
c3d94aec |
595 | PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&komodoshis); |
01d9ed2f |
596 | bitcoin_address(coinaddr,addrtype,rmd160,20); |
c3d94aec |
597 | checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,value); |
01d9ed2f |
598 | typestr = "withdraw"; |
c3d94aec |
599 | printf("%s.height.%d vs height.%d check %.8f/%.8f vs %.8f tokomodo.%d %d seed.%llx -> (%s)\n",ASSETCHAINS_SYMBOL,kmdheight,height,dstr(checktoshis),dstr(komodoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed,coinaddr); |
f5e3cdfa |
600 | didstats = 0; |
e97061cb |
601 | if ( komodo_paxcmp(komodoshis,checktoshis,seed) == 0 ) //checktoshis <= komodoshis+(komodoshis >> 10) ) |
01d9ed2f |
602 | { |
042e3b9f |
603 | if ( value != 0 & ((pax= komodo_paxfind(txid,vout)) == 0 || pax->didstats == 0) ) |
0b556992 |
604 | { |
33319f82 |
605 | if ( (basesp= komodo_stateptrget(base)) != 0 ) |
0fa85cc3 |
606 | { |
5ce8bf22 |
607 | basesp->withdrawn += value; |
f5e3cdfa |
608 | didstats = 1; |
33319f82 |
609 | printf("########### %p withdrawn %s += %.8f\n",basesp,base,dstr(value)); |
0fa85cc3 |
610 | } |
0b556992 |
611 | printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height); |
ea365a71 |
612 | } |
d6be719d |
613 | komodo_gateway_deposit(coinaddr,komodoshis,(char *)"KMD",value,rmd160,txid,vout,kmdheight,height,source,0); |
ca75995d |
614 | if ( (pax= komodo_paxfind(txid,vout)) != 0 ) |
615 | { |
616 | if ( didstats != 0 ) |
617 | pax->didstats = 1; |
618 | pax->type = opretbuf[0]; |
619 | } |
a4c67285 |
620 | } |
621 | } |
622 | else if ( tokomodo != 0 && opretbuf[0] == 'A' ) |
623 | { |
52b2e68d |
624 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) |
826bfe51 |
625 | { |
626 | for (i=0; i<opretlen; i++) |
627 | printf("%02x",opretbuf[i]); |
74a9722e |
628 | printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal())); |
826bfe51 |
629 | } |
b89ab77c |
630 | if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 ) |
a4c67285 |
631 | { |
632 | for (i=0; i<n; i++) |
633 | { |
b89ab77c |
634 | if ( baseids[i] < 0 ) |
635 | continue; |
f5e3cdfa |
636 | didstats = 0; |
0cc6ad81 |
637 | seed = 0; |
992c3be5 |
638 | checktoshis = komodo_paxprice(&seed,kmdheights[i],CURRENCIES[baseids[i]],(char *)"KMD",(uint64_t)srcvalues[i]); |
639 | printf("PAX_fiatdest ht.%d price %s %.8f -> KMD %.8f vs %.8f\n",kmdheights[i],CURRENCIES[baseids[i]],(double)srcvalues[i]/COIN,(double)values[i]/COIN,(double)checktoshis/COIN); |
26ffac88 |
640 | for (j=0; j<32; j++) |
641 | printf("%02x",((uint8_t *)&txids[i])[j]); |
71c9af7c |
642 | printf(" v%d %.8f k.%d ht.%d base.%d\n",vouts[i],dstr(values[i]),kmdheights[i],otherheights[i],baseids[i]); |
b89ab77c |
643 | if ( (pax= komodo_paxfind(txids[i],vouts[i])) == 0 ) |
a4c67285 |
644 | { |
f360ea0a |
645 | bitcoin_address(coinaddr,60,&rmd160s[i*20],20); |
0cc6ad81 |
646 | komodo_gateway_deposit(coinaddr,values[i],CURRENCIES[baseids[i]],srcvalues[i],&rmd160s[i*20],txids[i],vouts[i],kmdheights[i],otherheights[i],CURRENCIES[baseids[i]],kmdheights[i]); |
042e3b9f |
647 | if ( srcvalues[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) |
0fa85cc3 |
648 | { |
3cbfd2b4 |
649 | basesp->approved += srcvalues[i]; |
f5e3cdfa |
650 | didstats = 1; |
ad16127c |
651 | printf("########### %p approved %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i])); |
0fa85cc3 |
652 | } |
3cbfd2b4 |
653 | printf(" i.%d (%s) <- %.8f ADDFLAG APPROVED\n",i,coinaddr,dstr(values[i])); |
3f9cad6d |
654 | } |
042e3b9f |
655 | else if ( pax->didstats == 0 && srcvalues[i] != 0 ) |
3f9cad6d |
656 | { |
657 | if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) |
658 | { |
659 | basesp->approved += srcvalues[i]; |
660 | didstats = 1; |
661 | printf("########### %p approved %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i])); |
662 | } |
663 | } |
664 | else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]); |
dfe40c43 |
665 | if ( (pax= komodo_paxfind(txids[i],vouts[i])) != 0 ) |
06d1a655 |
666 | { |
ca75995d |
667 | pax->type = opretbuf[0]; |
f97d3439 |
668 | pax->approved = kmdheights[i]; |
f5e3cdfa |
669 | if ( didstats != 0 ) |
670 | pax->didstats = 1; |
dfe40c43 |
671 | printf(" i.%d approved.%d <<<<<<<<<<<<< APPROVED %p\n",i,kmdheights[i],pax); |
06d1a655 |
672 | } |
ea365a71 |
673 | } |
0b556992 |
674 | } |
ecfc4f44 |
675 | printf("extra.[%d] after %.8f\n",n,dstr(komodo_paxtotal())); |
01d9ed2f |
676 | } |
20e09846 |
677 | else if ( opretbuf[0] == 'I' ) |
429dabb5 |
678 | { |
f5e3cdfa |
679 | if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 ) |
429dabb5 |
680 | { |
b89ab77c |
681 | if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 ) |
429dabb5 |
682 | { |
63b289ad |
683 | for (i=0; i<n; i++) |
ec35af90 |
684 | { |
f5e3cdfa |
685 | if ( baseids[i] < 0 ) |
b971ca89 |
686 | { |
687 | printf("%d of %d illegal baseid.%d\n",i,n,baseids[i]); |
f5e3cdfa |
688 | continue; |
b971ca89 |
689 | } |
f5e3cdfa |
690 | didstats = 0; |
9ccd3f33 |
691 | bitcoin_address(coinaddr,60,&rmd160s[i*20],20); |
610030d0 |
692 | if ( srcvalues[i] != 0 && ((pax= komodo_paxfind(txids[i],vouts[i])) == 0 || pax->didstats == 0) ) |
3cbfd2b4 |
693 | { |
d61cc479 |
694 | if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) |
0fa85cc3 |
695 | { |
3cbfd2b4 |
696 | basesp->issued += srcvalues[i]; |
4adf625a |
697 | printf("########### %p issued %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i])); |
0fa85cc3 |
698 | } |
f5e3cdfa |
699 | didstats = 1; |
700 | } |
701 | if ( komodo_paxmark(height,txids[i],vouts[i],height) == 0 ) |
702 | { |
645ecdd5 |
703 | //if ( tokomodo == 0 ) |
fa81b5ae |
704 | //printf("I: %s\n",CURRENCIES[baseids[i]]); |
20e09846 |
705 | komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],height,0,CURRENCIES[baseids[i]],0); |
3cbfd2b4 |
706 | } |
ca75995d |
707 | if ( (pax= komodo_paxfind(txids[i],vouts[i])) != 0 ) |
708 | { |
709 | pax->type = opretbuf[0]; |
3f2d5618 |
710 | strcpy(pax->source,(char *)&opretbuf[opretlen-4]); |
ca75995d |
711 | if ( didstats != 0 ) |
712 | pax->didstats = 1; |
713 | } |
ec35af90 |
714 | } |
c1bc1804 |
715 | } else printf("opreturn none issued?\n"); |
b971ca89 |
716 | } else printf("opreturn matches KMD? %s\n",(char *)&opretbuf[opretlen-4]); |
429dabb5 |
717 | } |
ca3c6549 |
718 | else if ( opretbuf[0] == 'X' ) |
719 | { |
4c58b338 |
720 | printf("got X opreturn height.%d\n",height); |
721 | if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 ) |
722 | { |
723 | for (i=0; i<n; i++) |
724 | { |
3f9cad6d |
725 | if ( baseids[i] < 0 ) |
726 | continue; |
f5e3cdfa |
727 | didstats = 0; |
4c58b338 |
728 | bitcoin_address(coinaddr,60,&rmd160s[i*20],20); |
cddf571e |
729 | printf("i.%d of %d: %.8f -> %s\n",i,n,dstr(values[i]),coinaddr); |
042e3b9f |
730 | if ( srcvalues[i] != 0 && ((pax= komodo_paxfind(txids[i],vouts[i])) == 0 || pax->didstats == 0) ) |
3cbfd2b4 |
731 | { |
3f9cad6d |
732 | if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) |
0fa85cc3 |
733 | { |
3cbfd2b4 |
734 | basesp->redeemed += srcvalues[i]; |
f5e3cdfa |
735 | didstats = 1; |
3f9cad6d |
736 | printf("########### %p redeemed %s += %.8f\n",basesp,CURRENCIES[baseids[i]],dstr(srcvalues[i])); |
0fa85cc3 |
737 | } |
f5e3cdfa |
738 | didstats = 1; |
739 | } |
740 | if ( komodo_paxmark(height,txids[i],vouts[i],height) == 0 ) |
4c58b338 |
741 | komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],height,0,(char *)"KMD",0); |
ca75995d |
742 | if ( (pax= komodo_paxfind(txids[i],vouts[i])) != 0 ) |
743 | { |
744 | pax->type = opretbuf[0]; |
745 | if ( didstats != 0 ) |
746 | pax->didstats = 1; |
747 | } |
4c58b338 |
748 | } |
e988a431 |
749 | } else printf("komodo_issued_opreturn returned %d\n",n); |
ca3c6549 |
750 | } |
64bb0834 |
751 | return(typestr); |
752 | } |
753 | |
30d79208 |
754 | void komodo_passport_iteration() |
ab918767 |
755 | { |
635dd34d |
756 | static long lastpos[34]; static char userpass[33][1024]; |
757 | FILE *fp; int32_t baseid,isrealtime,refid,blocks,longest; struct komodo_state *sp,*refsp; char *retstr,fname[512],*base,symbol[16],dest[16]; uint32_t buf[3]; cJSON *infoobj,*result; uint64_t RTmask = 0; |
17899df4 |
758 | while ( KOMODO_INITDONE == 0 ) |
759 | { |
760 | fprintf(stderr,"PASSPORT iteration waiting for KOMODO_INITDONE\n"); |
761 | sleep(3); |
762 | } |
635dd34d |
763 | refsp = komodo_stateptr(symbol,dest); |
ab918767 |
764 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) |
67cd296f |
765 | refid = 33; |
635dd34d |
766 | else refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0 |
3085016b |
767 | //printf("PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,refid); |
52f1d788 |
768 | for (baseid=0; baseid<=32; baseid++) |
508b0d3c |
769 | { |
ecf6256c |
770 | sp = 0; |
771 | isrealtime = 0; |
f789edde |
772 | base = (char *)CURRENCIES[baseid]; |
c1bc1804 |
773 | if ( strcmp("EUR",base) != 0 && baseid < 32 ) |
774 | continue; |
95a6016d |
775 | if ( baseid+1 != refid ) |
ab918767 |
776 | { |
055db9b6 |
777 | komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate"); |
52f1d788 |
778 | komodo_nameset(symbol,dest,base); |
30376a70 |
779 | //port = komodo_port(base,10,&magic) + 1; |
52f1d788 |
780 | if ( (fp= fopen(fname,"rb")) != 0 && (sp= komodo_stateptrget(symbol)) != 0 ) |
ab918767 |
781 | { |
52f1d788 |
782 | fseek(fp,0,SEEK_END); |
783 | if ( ftell(fp) > lastpos[baseid] ) |
ab918767 |
784 | { |
c7caa0da |
785 | //printf("passport refid.%d %s fname.(%s) base.%s\n",refid,symbol,fname,base); |
52f1d788 |
786 | fseek(fp,lastpos[baseid],SEEK_SET); |
9534c87a |
787 | while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 ) |
52f1d788 |
788 | ; |
789 | lastpos[baseid] = ftell(fp); |
a9bb99d2 |
790 | //printf("from.(%s) lastpos[%s] %ld\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid]); |
52f1d788 |
791 | } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp)); |
792 | fclose(fp); |
ecf6256c |
793 | } |
a7ef556c |
794 | komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime"); |
795 | if ( (fp= fopen(fname,"rb")) != 0 ) |
796 | { |
797 | if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) ) |
798 | { |
f1db88ea |
799 | sp->CURRENT_HEIGHT = buf[0]; |
fe0210c0 |
800 | if ( buf[0] != 0 && buf[0] == buf[1] && buf[2] > time(NULL)-60 ) |
6eb41a2f |
801 | { |
a7ef556c |
802 | isrealtime = 1; |
6eb41a2f |
803 | RTmask |= (1LL << baseid); |
635dd34d |
804 | memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1])); |
140fccf8 |
805 | } //else fprintf(stderr,"%s not RT\n",base); |
bffa91b2 |
806 | } //else fprintf(stderr,"%s size error RT\n",base); |
a7ef556c |
807 | fclose(fp); |
209dc96c |
808 | } else fprintf(stderr,"%s open error RT\n",base); |
a7ef556c |
809 | } |
635dd34d |
810 | else |
a7ef556c |
811 | { |
812 | komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime"); |
813 | if ( (fp= fopen(fname,"wb")) != 0 ) |
814 | { |
30376a70 |
815 | buf[0] = (uint32_t)chainActive.Tip()->nHeight; |
a7ef556c |
816 | buf[1] = (uint32_t)komodo_longestchain(); |
817 | if ( buf[0] != 0 && buf[0] == buf[1] ) |
6eb41a2f |
818 | { |
a7ef556c |
819 | buf[2] = (uint32_t)time(NULL); |
48a3cd18 |
820 | RTmask |= (1LL << baseid) | 1; |
635dd34d |
821 | memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1])); |
822 | if ( refid != 0 ) |
823 | memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0])); |
6eb41a2f |
824 | } |
a7ef556c |
825 | if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) ) |
826 | fprintf(stderr,"[%s] %s error writing realtime\n",ASSETCHAINS_SYMBOL,base); |
827 | fclose(fp); |
f789edde |
828 | } else fprintf(stderr,"%s create error RT\n",base); |
ecf6256c |
829 | } |
f1db88ea |
830 | if ( sp != 0 && isrealtime == 0 ) |
831 | refsp->RTbufs[0][2] = 0; |
52f1d788 |
832 | } |
635dd34d |
833 | refsp->RTmask = RTmask; |
4767f1fc |
834 | KOMODO_PASSPORT_INITDONE = 1; |
ab918767 |
835 | } |
ab918767 |
836 | |