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