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