]>
Commit | Line | Data |
---|---|---|
9b0e1808 | 1 | /****************************************************************************** |
713c2a94 | 2 | * Copyright © 2014-2018 The SuperNET Developers. * |
9b0e1808 | 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 |
5416af1d | 17 | #include "komodo_defs.h" |
f38345e9 | 18 | |
aa114a60 | 19 | 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 | 20 | { |
16c7bf6b | 21 | int32_t baseid; struct komodo_state *sp; int64_t netliability,maxallowed,maxval; |
aa114a60 | 22 | *available = *deposited = *issued = *withdrawn = *approved = *redeemed = 0; |
3cbfd2b4 | 23 | if ( (baseid= komodo_baseid(base)) >= 0 ) |
24 | { | |
25 | if ( (sp= komodo_stateptrget(base)) != 0 ) | |
26 | { | |
27 | *deposited = sp->deposited; | |
28 | *issued = sp->issued; | |
29 | *withdrawn = sp->withdrawn; | |
30 | *approved = sp->approved; | |
31 | *redeemed = sp->redeemed; | |
16c7bf6b | 32 | maxval = sp->approved; |
33 | if ( sp->withdrawn > maxval ) | |
34 | maxval = sp->withdrawn; | |
35 | netliability = (sp->issued - maxval) - sp->shorted; | |
aa114a60 | 36 | maxallowed = komodo_maxallowed(baseid); |
37 | if ( netliability < maxallowed ) | |
38 | *available = (maxallowed - netliability); | |
68025c1c | 39 | //printf("%llu - %llu %s %.8f %.8f %.8f %.8f %.8f\n",(long long)maxallowed,(long long)netliability,base,dstr(*deposited),dstr(*issued),dstr(*withdrawn),dstr(*approved),dstr(*redeemed)); |
3cbfd2b4 | 40 | return(0); |
ee5e5744 | 41 | } else printf("pax_fiatstatus cant get basesp.%s\n",base); |
106a1d04 | 42 | } // else printf("pax_fiatstatus illegal base.%s\n",base); |
3cbfd2b4 | 43 | return(-1); |
44 | } | |
45 | ||
51e5d952 | 46 | void pax_keyset(uint8_t *buf,uint256 txid,uint16_t vout,uint8_t type) |
6210f328 | 47 | { |
51e5d952 | 48 | memcpy(buf,&txid,32); |
49 | memcpy(&buf[32],&vout,2); | |
50 | buf[34] = type; | |
51 | } | |
52 | ||
53 | struct pax_transaction *komodo_paxfind(uint256 txid,uint16_t vout,uint8_t type) | |
54 | { | |
55 | struct pax_transaction *pax; uint8_t buf[35]; | |
6210f328 | 56 | pthread_mutex_lock(&komodo_mutex); |
51e5d952 | 57 | pax_keyset(buf,txid,vout,type); |
58 | HASH_FIND(hh,PAX,buf,sizeof(buf),pax); | |
6210f328 | 59 | pthread_mutex_unlock(&komodo_mutex); |
60 | return(pax); | |
61 | } | |
62 | ||
16cd9f2d | 63 | struct pax_transaction *komodo_paxfinds(uint256 txid,uint16_t vout) |
c5c0e527 | 64 | { |
2aba5e49 | 65 | struct pax_transaction *pax; int32_t i; uint8_t types[] = { 'I', 'D', 'X', 'A', 'W' }; |
16cd9f2d | 66 | for (i=0; i<sizeof(types)/sizeof(*types); i++) |
67 | if ( (pax= komodo_paxfind(txid,vout,types[i])) != 0 ) | |
8dfdb3d0 | 68 | return(pax); |
c5c0e527 | 69 | return(0); |
70 | } | |
71 | ||
51e5d952 | 72 | struct pax_transaction *komodo_paxmark(int32_t height,uint256 txid,uint16_t vout,uint8_t type,int32_t mark) |
6210f328 | 73 | { |
51e5d952 | 74 | struct pax_transaction *pax; uint8_t buf[35]; |
6210f328 | 75 | pthread_mutex_lock(&komodo_mutex); |
51e5d952 | 76 | pax_keyset(buf,txid,vout,type); |
77 | HASH_FIND(hh,PAX,buf,sizeof(buf),pax); | |
2ab43fd2 | 78 | if ( pax == 0 ) |
79 | { | |
80 | pax = (struct pax_transaction *)calloc(1,sizeof(*pax)); | |
81 | pax->txid = txid; | |
82 | pax->vout = vout; | |
51e5d952 | 83 | pax->type = type; |
84 | memcpy(pax->buf,buf,sizeof(pax->buf)); | |
85 | HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax); | |
32d525eb | 86 | //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark); |
2ab43fd2 | 87 | } |
6210f328 | 88 | if ( pax != 0 ) |
649a9ebf | 89 | { |
6210f328 | 90 | pax->marked = mark; |
cfd94f88 | 91 | //if ( height > 214700 || pax->height > 214700 ) |
92 | // printf("mark ht.%d %.8f %.8f\n",pax->height,dstr(pax->komodoshis),dstr(pax->fiatoshis)); | |
649a9ebf | 93 | |
94 | } | |
6210f328 | 95 | pthread_mutex_unlock(&komodo_mutex); |
96 | return(pax); | |
97 | } | |
429dabb5 | 98 | |
57f349cb | 99 | void komodo_paxdelete(struct pax_transaction *pax) |
100 | { | |
5720de88 | 101 | return; // breaks when out of order |
57f349cb | 102 | pthread_mutex_lock(&komodo_mutex); |
103 | HASH_DELETE(hh,PAX,pax); | |
104 | pthread_mutex_unlock(&komodo_mutex); | |
105 | } | |
106 | ||
51e5d952 | 107 | 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 | 108 | { |
7c130297 | 109 | struct pax_transaction *pax; uint8_t buf[35]; int32_t addflag = 0; struct komodo_state *sp; char str[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN],*s; |
717dfab6 | 110 | //if ( KOMODO_PAX == 0 ) |
111 | // return; | |
4a6d9acb | 112 | //if ( strcmp(symbol,ASSETCHAINS_SYMBOL) != 0 ) |
113 | // return; | |
bffa91b2 | 114 | sp = komodo_stateptr(str,dest); |
d43c7a93 | 115 | pthread_mutex_lock(&komodo_mutex); |
51e5d952 | 116 | pax_keyset(buf,txid,vout,type); |
117 | HASH_FIND(hh,PAX,buf,sizeof(buf),pax); | |
d43c7a93 | 118 | if ( pax == 0 ) |
b2d64a0f | 119 | { |
d43c7a93 | 120 | pax = (struct pax_transaction *)calloc(1,sizeof(*pax)); |
8d52488f | 121 | pax->txid = txid; |
122 | pax->vout = vout; | |
51e5d952 | 123 | pax->type = type; |
124 | memcpy(pax->buf,buf,sizeof(pax->buf)); | |
125 | HASH_ADD_KEYPTR(hh,PAX,pax->buf,sizeof(pax->buf),pax); | |
7961668b | 126 | addflag = 1; |
f3159f77 | 127 | if ( 0 && ASSETCHAINS_SYMBOL[0] == 0 ) |
1fc75a27 | 128 | { |
129 | int32_t i; for (i=0; i<32; i++) | |
130 | printf("%02x",((uint8_t *)&txid)[i]); | |
5ce8bf22 | 131 | 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 | 132 | } |
b2d64a0f | 133 | } |
8d52488f | 134 | pthread_mutex_unlock(&komodo_mutex); |
d43c7a93 | 135 | if ( coinaddr != 0 ) |
136 | { | |
137 | strcpy(pax->coinaddr,coinaddr); | |
9ccd3f33 | 138 | if ( value != 0 ) |
139 | pax->komodoshis = value; | |
140 | if ( symbol != 0 ) | |
141 | strcpy(pax->symbol,symbol); | |
cd26c1f3 | 142 | if ( source != 0 ) |
143 | strcpy(pax->source,source); | |
9ccd3f33 | 144 | if ( fiatoshis != 0 ) |
145 | pax->fiatoshis = fiatoshis; | |
146 | if ( rmd160 != 0 ) | |
147 | memcpy(pax->rmd160,rmd160,20); | |
148 | if ( height != 0 ) | |
149 | pax->height = height; | |
150 | if ( otherheight != 0 ) | |
151 | pax->otherheight = otherheight; | |
d43c7a93 | 152 | } |
153 | else | |
154 | { | |
155 | pax->marked = height; | |
bff51d67 | 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++]; |
bff51d67 | 174 | //for (i=0; i<32; i++) |
175 | // printf("%02x",((uint8_t *)&pax->txid)[31-i]); | |
ddf827b2 | 176 | pax->vout = opretbuf[len++]; |
177 | pax->vout += ((uint32_t)opretbuf[len++] << 8); | |
bff51d67 | 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 | { |
7c130297 | 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[KOMODO_ASSETCHAIN_MAXLEN]; |
717dfab6 | 202 | //if ( KOMODO_PAX == 0 ) |
203 | // return(0); | |
c83633f8 | 204 | incr = 34 + (iskomodo * (2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 4)); |
085c4ee4 | 205 | //41e77b91cb68dc2aa02fa88550eae6b6d44db676a7e935337b6d1392d9718f03cb0200305c90660400000000fbcbeb1f000000bde801006201000058e7945ad08ddba1eac9c9b6c8e1e97e8016a2d152 |
8b4d7f27 | 206 | |
207 | // 41e94d736ec69d88c08b5d238abeeca609c02357a8317e0d56c328bcb1c259be5d0200485bc80200000000404b4c000000000059470200b80b000061f22ba7d19fe29ac3baebd839af8b7127d1f9075553440046bb4cc7a3b5cd39dffe7206507a3482a00780e617f68b273cce9817ed69298d02001069ca1b0000000080f0fa02000000005b470200b90b000061f22ba7d19fe29ac3baebd839af8b7127d1f90755 | |
208 | ||
40f3d9cc | 209 | //for (i=0; i<opretlen; i++) |
210 | // printf("%02x",opretbuf[i]); | |
b3183e3e | 211 | //printf(" opretlen.%d (%s)\n",opretlen,base); |
40f3d9cc | 212 | //printf(" opretlen.%d vs %d incr.%d (%d)\n",opretlen,(int32_t)(2*sizeof(fiatoshis) + 2*sizeof(height) + 20 + 2),incr,opretlen/incr); |
c3152c23 | 213 | //if ( ASSETCHAINS_SYMBOL[0] == 0 || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) |
63b289ad | 214 | { |
947d956f | 215 | type = opretbuf[0]; |
63b289ad | 216 | opretbuf++, opretlen--; |
e8433f7a | 217 | for (n=0; n<opretlen/incr; n++) |
63b289ad | 218 | { |
05d91eb2 | 219 | if ( iskomodo != 0 ) |
220 | { | |
e8433f7a | 221 | memset(&p,0,sizeof(p)); |
ffec138e | 222 | len += komodo_rwapproval(0,&opretbuf[len],&p); |
b89ab77c | 223 | if ( values != 0 && srcvalues != 0 && kmdheights != 0 && otherheights != 0 && baseids != 0 && rmd160s != 0 ) |
1ed46112 | 224 | { |
63342b19 | 225 | txids[n] = p.txid; |
226 | vouts[n] = p.vout; | |
c65d0868 | 227 | values[n] = (strcmp("KMD",base) == 0) ? p.komodoshis : p.fiatoshis; |
228 | srcvalues[n] = (strcmp("KMD",base) == 0) ? p.fiatoshis : p.komodoshis; | |
a3f1fb69 | 229 | kmdheights[n] = p.height; |
230 | otherheights[n] = p.otherheight; | |
7cc6844b | 231 | memcpy(&rmd160s[n * 20],p.rmd160,20); |
1dbea872 | 232 | baseids[n] = komodo_baseid(p.source); |
109afe64 | 233 | if ( 0 ) |
71c9af7c | 234 | { |
235 | char coinaddr[64]; | |
236 | bitcoin_address(coinaddr,60,&rmd160s[n * 20],20); | |
947d956f | 237 | 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 | 238 | } |
52e87248 | 239 | } |
05d91eb2 | 240 | } |
7cc6844b | 241 | else |
242 | { | |
5cb9312f | 243 | for (i=0; i<4; i++) |
244 | base[i] = opretbuf[opretlen-4+i]; | |
7cc6844b | 245 | for (j=0; j<32; j++) |
246 | { | |
247 | ((uint8_t *)&txids[n])[j] = opretbuf[len++]; | |
a13467f5 | 248 | //printf("%02x",((uint8_t *)&txids[n])[j]); |
7cc6844b | 249 | } |
250 | vouts[n] = opretbuf[len++]; | |
251 | vouts[n] = (opretbuf[len++] << 8) | vouts[n]; | |
cb3bf8df | 252 | baseids[n] = komodo_baseid(base); |
16cd9f2d | 253 | if ( (pax= komodo_paxfinds(txids[n],vouts[n])) != 0 ) |
54661780 | 254 | { |
c3152c23 | 255 | values[n] = (strcmp("KMD",base) == 0) ? pax->komodoshis : pax->fiatoshis; |
256 | srcvalues[n] = (strcmp("KMD",base) == 0) ? pax->fiatoshis : pax->komodoshis; | |
54661780 | 257 | kmdheights[n] = pax->height; |
258 | otherheights[n] = pax->otherheight; | |
259 | memcpy(&rmd160s[n * 20],pax->rmd160,20); | |
260 | } | |
7cc6844b | 261 | } |
a13467f5 | 262 | //printf(" komodo_issued_opreturn issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen); |
63b289ad | 263 | } |
264 | } | |
265 | return(n); | |
266 | } | |
267 | ||
865bd972 | 268 | int32_t komodo_paxcmp(char *symbol,int32_t kmdheight,uint64_t value,uint64_t checkvalue,uint64_t seed) |
269 | { | |
270 | int32_t ratio; | |
271 | if ( seed == 0 && checkvalue != 0 ) | |
272 | { | |
273 | ratio = ((value << 6) / checkvalue); | |
f712270b | 274 | if ( ratio >= 60 && ratio <= 67 ) |
865bd972 | 275 | return(0); |
276 | else | |
277 | { | |
e75dc69d | 278 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) |
865bd972 | 279 | printf("ht.%d ignore mismatched %s value %lld vs checkvalue %lld -> ratio.%d\n",kmdheight,symbol,(long long)value,(long long)checkvalue,ratio); |
280 | return(-1); | |
281 | } | |
282 | } | |
283 | else if ( checkvalue != 0 ) | |
284 | { | |
285 | ratio = ((value << 10) / checkvalue); | |
286 | if ( ratio >= 1023 && ratio <= 1025 ) | |
287 | return(0); | |
288 | } | |
289 | return(value != checkvalue); | |
290 | } | |
291 | ||
f1db88ea | 292 | uint64_t komodo_paxtotal() |
293 | { | |
7c130297 | 294 | struct pax_transaction *pax,*pax2,*tmp,*tmp2; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN],*str; int32_t i,ht; int64_t checktoshis; uint64_t seed,total = 0; struct komodo_state *basesp; |
717dfab6 | 295 | if ( KOMODO_PASSPORT_INITDONE == 0 ) //KOMODO_PAX == 0 || |
6cd01056 | 296 | return(0); |
48a3cd18 | 297 | if ( komodo_isrealtime(&ht) == 0 ) |
f1db88ea | 298 | return(0); |
ca75995d | 299 | else |
300 | { | |
301 | HASH_ITER(hh,PAX,pax,tmp) | |
302 | { | |
5ccb222a | 303 | if ( pax->marked != 0 ) |
304 | continue; | |
85779087 | 305 | if ( pax->type == 'A' || pax->type == 'D' || pax->type == 'X' ) |
306 | str = pax->symbol; | |
307 | else str = pax->source; | |
308 | basesp = komodo_stateptrget(str); | |
115e6a5c | 309 | if ( basesp != 0 && pax->didstats == 0 ) |
ca75995d | 310 | { |
6e458bf3 | 311 | if ( pax->type == 'I' && (pax2= komodo_paxfind(pax->txid,pax->vout,'D')) != 0 ) |
ca75995d | 312 | { |
3cfd4837 | 313 | if ( pax2->fiatoshis != 0 ) |
314 | { | |
315 | pax->komodoshis = pax2->komodoshis; | |
316 | pax->fiatoshis = pax2->fiatoshis; | |
317 | basesp->issued += pax->fiatoshis; | |
318 | pax->didstats = 1; | |
54f87e10 | 319 | if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 ) |
320 | printf("########### %p issued %s += %.8f kmdheight.%d %.8f other.%d\n",basesp,str,dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight); | |
c3111e7b | 321 | pax2->marked = pax->height; |
54f87e10 | 322 | pax->marked = pax->height; |
3cfd4837 | 323 | } |
fa81b5ae | 324 | } |
115e6a5c | 325 | else if ( pax->type == 'W' ) |
326 | { | |
431af13e | 327 | //bitcoin_address(coinaddr,addrtype,rmd160,20); |
1bc5fdf8 | 328 | if ( (checktoshis= komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis)) != 0 ) |
329 | { | |
db7becdc | 330 | if ( komodo_paxcmp(pax->source,pax->height,pax->komodoshis,checktoshis,seed) != 0 ) |
772c13de | 331 | { |
1bc5fdf8 | 332 | pax->marked = pax->height; |
60cd57df | 333 | //printf("WITHDRAW.%s mark <- %d %.8f != %.8f\n",pax->source,pax->height,dstr(checktoshis),dstr(pax->komodoshis)); |
772c13de | 334 | } |
1bc5fdf8 | 335 | else if ( pax->validated == 0 ) |
336 | { | |
d4003105 | 337 | pax->validated = pax->komodoshis = checktoshis; |
109afe64 | 338 | //int32_t j; for (j=0; j<32; j++) |
339 | // printf("%02x",((uint8_t *)&pax->txid)[j]); | |
2405bc66 | 340 | //if ( strcmp(str,ASSETCHAINS_SYMBOL) == 0 ) |
109afe64 | 341 | // printf(" v%d %p got WITHDRAW.%s kmd.%d ht.%d %.8f -> %.8f/%.8f\n",pax->vout,pax,pax->source,pax->height,pax->otherheight,dstr(pax->fiatoshis),dstr(pax->komodoshis),dstr(checktoshis)); |
1bc5fdf8 | 342 | } |
343 | } | |
115e6a5c | 344 | } |
ca75995d | 345 | } |
346 | } | |
347 | } | |
ede04b12 | 348 | komodo_stateptr(symbol,dest); |
f1db88ea | 349 | HASH_ITER(hh,PAX,pax,tmp) |
350 | { | |
115e6a5c | 351 | pax->ready = 0; |
16cd9f2d | 352 | if ( 0 && pax->type == 'A' ) |
353 | printf("%p pax.%s <- %s marked.%d %.8f -> %.8f validated.%d approved.%d\n",pax,pax->symbol,pax->source,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->validated != 0,pax->approved != 0); | |
5ccb222a | 354 | if ( pax->marked != 0 ) |
355 | continue; | |
cfa1fdda | 356 | if ( strcmp(symbol,pax->symbol) == 0 || pax->type == 'A' ) |
f1db88ea | 357 | { |
ede04b12 | 358 | if ( pax->marked == 0 ) |
359 | { | |
360 | if ( komodo_is_issuer() != 0 ) | |
1bc5fdf8 | 361 | { |
0449e3f0 | 362 | if ( pax->validated != 0 && pax->type == 'D' ) |
1bc5fdf8 | 363 | { |
364 | total += pax->fiatoshis; | |
365 | pax->ready = 1; | |
366 | } | |
367 | } | |
371a9c87 | 368 | else if ( pax->approved != 0 && pax->type == 'A' ) |
992c3be5 | 369 | { |
370 | if ( pax->validated != 0 ) | |
02c36c15 | 371 | { |
992c3be5 | 372 | total += pax->komodoshis; |
02c36c15 | 373 | pax->ready = 1; |
374 | } | |
992c3be5 | 375 | else |
376 | { | |
377 | seed = 0; | |
f186b8a1 | 378 | checktoshis = komodo_paxprice(&seed,pax->height,pax->source,(char *)"KMD",(uint64_t)pax->fiatoshis); |
9ae3d21c | 379 | //printf("paxtotal 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); |
380 | //printf(" v%d %.8f k.%d ht.%d\n",pax->vout,dstr(pax->komodoshis),pax->height,pax->otherheight); | |
6e458bf3 | 381 | if ( seed != 0 && checktoshis != 0 ) |
992c3be5 | 382 | { |
115e6a5c | 383 | if ( checktoshis == pax->komodoshis ) |
992c3be5 | 384 | { |
385 | total += pax->komodoshis; | |
386 | pax->validated = pax->komodoshis; | |
115e6a5c | 387 | pax->ready = 1; |
992c3be5 | 388 | } else pax->marked = pax->height; |
389 | } | |
390 | } | |
391 | } | |
a8ffc9b9 | 392 | if ( 0 && pax->ready != 0 ) |
649a9ebf | 393 | printf("%p (%c) pax.%s marked.%d %.8f -> %.8f validated.%d approved.%d ready.%d ht.%d\n",pax,pax->type,pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->validated != 0,pax->approved != 0,pax->ready,pax->height); |
ede04b12 | 394 | } |
f1db88ea | 395 | } |
396 | } | |
5481c677 | 397 | //printf("paxtotal %.8f\n",dstr(total)); |
f1db88ea | 398 | return(total); |
399 | } | |
400 | ||
fa1a55cd | 401 | static int _paxorder(const void *a,const void *b) |
f360ea0a | 402 | { |
fa1a55cd | 403 | #define pax_a (*(struct pax_transaction **)a) |
404 | #define pax_b (*(struct pax_transaction **)b) | |
405 | uint64_t aval,bval; | |
d936b97f | 406 | aval = pax_a->fiatoshis + pax_a->komodoshis + pax_a->height; |
407 | bval = pax_b->fiatoshis + pax_b->komodoshis + pax_b->height; | |
fa1a55cd | 408 | if ( bval > aval ) |
409 | return(-1); | |
410 | else if ( bval < aval ) | |
411 | return(1); | |
412 | return(0); | |
413 | #undef pax_a | |
414 | #undef pax_b | |
415 | } | |
416 | ||
417 | int32_t komodo_pending_withdraws(char *opretstr) // todo: enforce deterministic order | |
418 | { | |
7a1b708f | 419 | struct pax_transaction *pax,*pax2,*tmp,*paxes[64]; uint8_t opretbuf[16384*4]; int32_t i,n,ht,len=0; uint64_t total = 0; |
9aefc81b | 420 | if ( KOMODO_PAX == 0 || KOMODO_PASSPORT_INITDONE == 0 ) |
6cd01056 | 421 | return(0); |
f360ea0a | 422 | if ( komodo_isrealtime(&ht) == 0 || ASSETCHAINS_SYMBOL[0] != 0 ) |
423 | return(0); | |
fa1a55cd | 424 | n = 0; |
f360ea0a | 425 | HASH_ITER(hh,PAX,pax,tmp) |
426 | { | |
f1e6a937 | 427 | if ( pax->type == 'W' ) |
f360ea0a | 428 | { |
6b3eb2c4 | 429 | if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'A')) != 0 ) |
430 | { | |
431 | if ( pax2->approved != 0 ) | |
432 | pax->approved = pax2->approved; | |
433 | } | |
16cd9f2d | 434 | else if ( (pax2= komodo_paxfind(pax->txid,pax->vout,'X')) != 0 ) |
0b262b65 | 435 | pax->approved = pax->height; |
01d3b36a | 436 | //printf("pending_withdraw: pax %s marked.%u approved.%u validated.%llu\n",pax->symbol,pax->marked,pax->approved,(long long)pax->validated); |
7d121b40 | 437 | if ( pax->marked == 0 && pax->approved == 0 && pax->validated != 0 ) //strcmp((char *)"KMD",pax->symbol) == 0 && |
f1e6a937 | 438 | { |
fa1a55cd | 439 | if ( n < sizeof(paxes)/sizeof(*paxes) ) |
ac4eb454 | 440 | { |
fa1a55cd | 441 | paxes[n++] = pax; |
2e575216 | 442 | //int32_t j; for (j=0; j<32; j++) |
443 | // printf("%02x",((uint8_t *)&pax->txid)[j]); | |
444 | //printf(" %s.(kmdht.%d ht.%d marked.%u approved.%d validated %.8f) %.8f\n",pax->source,pax->height,pax->otherheight,pax->marked,pax->approved,dstr(pax->validated),dstr(pax->komodoshis)); | |
ac4eb454 | 445 | } |
f1e6a937 | 446 | } |
f360ea0a | 447 | } |
448 | } | |
fa1a55cd | 449 | opretstr[0] = 0; |
450 | if ( n > 0 ) | |
451 | { | |
452 | opretbuf[len++] = 'A'; | |
57892e5a | 453 | qsort(paxes,n,sizeof(*paxes),_paxorder); |
fa1a55cd | 454 | for (i=0; i<n; i++) |
455 | { | |
456 | if ( len < (sizeof(opretbuf)>>3)*7 ) | |
457 | len += komodo_rwapproval(1,&opretbuf[len],paxes[i]); | |
458 | } | |
459 | if ( len > 0 ) | |
460 | init_hexbytes_noT(opretstr,opretbuf,len); | |
461 | } | |
132aea8c | 462 | //fprintf(stderr,"komodo_pending_withdraws len.%d PAXTOTAL %.8f\n",len,dstr(komodo_paxtotal())); |
f360ea0a | 463 | return(len); |
464 | } | |
a4c67285 | 465 | |
635dd34d | 466 | int32_t komodo_gateway_deposits(CMutableTransaction *txNew,char *base,int32_t tokomodo) |
429dabb5 | 467 | { |
7a1b708f | 468 | struct pax_transaction *pax,*tmp; char symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; uint8_t *script,opcode,opret[16384*4],data[16384*4]; int32_t i,baseid,ht,len=0,opretlen=0,numvouts=1; struct komodo_state *sp; uint64_t available,deposited,issued,withdrawn,approved,redeemed,mask,sum = 0; |
717dfab6 | 469 | if ( KOMODO_PASSPORT_INITDONE == 0 )//KOMODO_PAX == 0 || |
6cd01056 | 470 | return(0); |
76d59c1d | 471 | struct komodo_state *kmdsp = komodo_stateptrget((char *)"KMD"); |
635dd34d | 472 | sp = komodo_stateptr(symbol,dest); |
27bf3c5e | 473 | strcpy(symbol,base); |
b27b2877 | 474 | if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 ) |
716fb382 | 475 | return(0); |
429dabb5 | 476 | PENDING_KOMODO_TX = 0; |
b8cceac5 | 477 | for (i=0; i<3; i++) |
478 | { | |
479 | if ( komodo_isrealtime(&ht) != 0 ) | |
480 | break; | |
481 | sleep(1); | |
482 | } | |
483 | if ( i == 3 ) | |
484 | { | |
deba1c48 | 485 | if ( tokomodo == 0 ) |
486 | printf("%s not realtime ht.%d\n",ASSETCHAINS_SYMBOL,ht); | |
b8cceac5 | 487 | return(0); |
488 | } | |
a3f23ee2 | 489 | if ( tokomodo == 0 ) |
48e96ba1 | 490 | { |
77be6cd9 | 491 | opcode = 'I'; |
57f349cb | 492 | } |
493 | else | |
494 | { | |
495 | opcode = 'X'; | |
74d67d42 | 496 | if ( 1 || komodo_paxtotal() == 0 ) |
57f349cb | 497 | return(0); |
498 | } | |
c169f64b | 499 | HASH_ITER(hh,PAX,pax,tmp) |
429dabb5 | 500 | { |
8b4d7f27 | 501 | if ( pax->type != 'D' && pax->type != 'A' ) |
502 | continue; | |
aa114a60 | 503 | { |
504 | #ifdef KOMODO_ASSETCHAINS_WAITNOTARIZE | |
1d474f88 | 505 | if ( pax->height > 236000 ) |
89ede5f3 | 506 | { |
8aed5783 | 507 | if ( kmdsp != 0 && kmdsp->NOTARIZED_HEIGHT >= pax->height ) |
89ede5f3 | 508 | pax->validated = pax->komodoshis; |
509 | else if ( kmdsp->CURRENT_HEIGHT > pax->height+30 ) | |
510 | pax->validated = pax->ready = 0; | |
511 | } | |
512 | else | |
513 | { | |
514 | if ( kmdsp != 0 && (kmdsp->NOTARIZED_HEIGHT >= pax->height || kmdsp->CURRENT_HEIGHT > pax->height+30) ) // assumes same chain as notarize | |
515 | pax->validated = pax->komodoshis; | |
516 | else pax->validated = pax->ready = 0; | |
517 | } | |
13c01d29 | 518 | #else |
519 | pax->validated = pax->komodoshis; | |
aa114a60 | 520 | #endif |
521 | } | |
25d878f4 | 522 | if ( ASSETCHAINS_SYMBOL[0] != 0 && (pax_fiatstatus(&available,&deposited,&issued,&withdrawn,&approved,&redeemed,symbol) != 0 || available < pax->fiatoshis) ) |
1c05f0b5 | 523 | { |
dd36961a | 524 | //if ( pax->height > 214700 || strcmp(ASSETCHAINS_SYMBOL,symbol) == 0 ) |
525 | // printf("miner.[%s]: skip %s %.8f when avail %.8f deposited %.8f, issued %.8f withdrawn %.8f approved %.8f redeemed %.8f\n",ASSETCHAINS_SYMBOL,symbol,dstr(pax->fiatoshis),dstr(available),dstr(deposited),dstr(issued),dstr(withdrawn),dstr(approved),dstr(redeemed)); | |
4502c41e | 526 | continue; |
1c05f0b5 | 527 | } |
274dd7a5 | 528 | /*printf("pax.%s marked.%d %.8f -> %.8f ready.%d validated.%d\n",pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->ready!=0,pax->validated!=0); |
02c36c15 | 529 | if ( pax->marked != 0 || (pax->type != 'D' && pax->type != 'A') || pax->ready == 0 ) |
a06196fe | 530 | { |
531 | printf("reject 2\n"); | |
e5430f52 | 532 | continue; |
274dd7a5 | 533 | }*/ |
a822a070 | 534 | if ( ASSETCHAINS_SYMBOL[0] != 0 && (strcmp(pax->symbol,symbol) != 0 || pax->validated == 0 || pax->ready == 0) ) |
8bd946ce | 535 | { |
a8ffc9b9 | 536 | if ( strcmp(pax->symbol,ASSETCHAINS_SYMBOL) == 0 ) |
a822a070 | 537 | printf("pax->symbol.%s != %s or null pax->validated %.8f ready.%d ht.(%d %d)\n",pax->symbol,symbol,dstr(pax->validated),pax->ready,kmdsp->CURRENT_HEIGHT,pax->height); |
c495a60a | 538 | pax->marked = pax->height; |
8bd946ce | 539 | continue; |
540 | } | |
fc40c343 | 541 | if ( pax->ready == 0 ) |
542 | continue; | |
582c6afe | 543 | if ( pax->type == 'A' && ASSETCHAINS_SYMBOL[0] == 0 ) |
544 | { | |
76d59c1d | 545 | if ( kmdsp != 0 ) |
582c6afe | 546 | { |
547 | if ( (baseid= komodo_baseid(pax->symbol)) < 0 || ((1LL << baseid) & sp->RTmask) == 0 ) | |
548 | { | |
549 | printf("not RT for (%s) %llx baseid.%d %llx\n",pax->symbol,(long long)sp->RTmask,baseid,(long long)(1LL<<baseid)); | |
550 | continue; | |
551 | } | |
76d59c1d | 552 | } else continue; |
582c6afe | 553 | } |
554 | ||
5481c677 | 555 | //printf("redeem.%d? (%c) %p pax.%s marked.%d %.8f -> %.8f ready.%d validated.%d approved.%d\n",tokomodo,pax->type,pax,pax->symbol,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis),pax->ready!=0,pax->validated!=0,pax->approved!=0); |
49058e97 | 556 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) |
635dd34d | 557 | printf("pax.%s marked.%d %.8f -> %.8f\n",ASSETCHAINS_SYMBOL,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis)); |
1bf41050 | 558 | if ( opcode == 'I' ) |
559 | { | |
560 | sum += pax->fiatoshis; | |
561 | if ( sum > available ) | |
562 | break; | |
563 | } | |
e5430f52 | 564 | txNew->vout.resize(numvouts+1); |
1fc75a27 | 565 | txNew->vout[numvouts].nValue = (opcode == 'I') ? pax->fiatoshis : pax->komodoshis; |
e5430f52 | 566 | txNew->vout[numvouts].scriptPubKey.resize(25); |
567 | script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0]; | |
568 | *script++ = 0x76; | |
569 | *script++ = 0xa9; | |
570 | *script++ = 20; | |
571 | memcpy(script,pax->rmd160,20), script += 20; | |
572 | *script++ = 0x88; | |
573 | *script++ = 0xac; | |
a3f23ee2 | 574 | if ( tokomodo == 0 ) |
16bed6ac | 575 | { |
576 | for (i=0; i<32; i++) | |
16bed6ac | 577 | data[len++] = ((uint8_t *)&pax->txid)[i]; |
16bed6ac | 578 | data[len++] = pax->vout & 0xff; |
579 | data[len++] = (pax->vout >> 8) & 0xff; | |
e5430f52 | 580 | PENDING_KOMODO_TX += pax->fiatoshis; |
16bed6ac | 581 | } |
fbc4209b | 582 | else |
583 | { | |
e0357628 | 584 | len += komodo_rwapproval(1,&data[len],pax); |
fbc4209b | 585 | PENDING_KOMODO_TX += pax->komodoshis; |
1eaffd74 | 586 | printf(" len.%d vout.%u DEPOSIT %.8f <- pax.%s pending ht %d %d %.8f | ",len,pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,pax->height,pax->otherheight,dstr(PENDING_KOMODO_TX)); |
fbc4209b | 587 | } |
d31a8037 | 588 | if ( numvouts++ >= 64 || sum > COIN ) |
e5430f52 | 589 | break; |
429dabb5 | 590 | } |
591 | if ( numvouts > 1 ) | |
592 | { | |
0e3472c1 | 593 | if ( tokomodo != 0 ) |
27bf3c5e | 594 | strcpy(symbol,(char *)"KMD"); |
9ef47e29 | 595 | for (i=0; symbol[i]!=0; i++) |
596 | data[len++] = symbol[i]; | |
a5ad8f02 | 597 | data[len++] = 0; |
16cd9f2d | 598 | for (i=0; i<len; i++) |
599 | printf("%02x",data[i]); | |
600 | printf(" <- data[%d]\n",len); | |
77be6cd9 | 601 | opretlen = komodo_opreturnscript(opret,opcode,data,len); |
50824530 | 602 | txNew->vout.resize(numvouts+1); |
603 | txNew->vout[numvouts].nValue = 0; | |
604 | txNew->vout[numvouts].scriptPubKey.resize(opretlen); | |
605 | script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0]; | |
429dabb5 | 606 | memcpy(script,opret,opretlen); |
8b4d7f27 | 607 | for (i=0; i<8; i++) |
608 | printf("%02x",opret[i]); | |
e03bcdd6 | 609 | printf(" <- opret, MINER deposits.%d (%s) vouts.%d %.8f opretlen.%d\n",tokomodo,ASSETCHAINS_SYMBOL,numvouts,dstr(PENDING_KOMODO_TX),opretlen); |
d9a9d562 | 610 | return(1); |
9ef47e29 | 611 | } |
d9a9d562 | 612 | return(0); |
429dabb5 | 613 | } |
614 | ||
5b6fb570 | 615 | const char *banned_txids[] = |
557d9a23 | 616 | { |
6fe17943 | 617 | "78cb4e21245c26b015b888b14c4f5096e18137d2741a6de9734d62b07014dfca", //233559 |
618 | "00697be658e05561febdee1aafe368b821ca33fbb89b7027365e3d77b5dfede5", //234172 | |
619 | "e909465788b32047c472d73e882d79a92b0d550f90be008f76e1edaee6d742ea", //234187 | |
620 | "f56c6873748a327d0b92b8108f8ec8505a2843a541b1926022883678fb24f9dc", //234188 | |
621 | "abf08be07d8f5b3a433ddcca7ef539e79a3571632efd6d0294ec0492442a0204", //234213 | |
622 | "3b854b996cc982fba8c06e76cf507ae7eed52ab92663f4c0d7d10b3ed879c3b0", //234367 | |
623 | "fa9e474c2cda3cb4127881a40eb3f682feaba3f3328307d518589024a6032cc4", //234635 | |
624 | "ca746fa13e0113c4c0969937ea2c66de036d20274efad4ce114f6b699f1bc0f3", //234662 | |
625 | "43ce88438de4973f21b1388ffe66e68fda592da38c6ef939be10bb1b86387041", //234697 | |
626 | "0aeb748de82f209cd5ff7d3a06f65543904c4c17387c9d87c65fd44b14ad8f8c", //234899 | |
627 | "bbd3a3d9b14730991e1066bd7c626ca270acac4127131afe25f877a5a886eb25", //235252 | |
628 | "fa9943525f2e6c32cbc243294b08187e314d83a2870830180380c3c12a9fd33c", //235253 | |
629 | "a01671c8775328a41304e31a6693bbd35e9acbab28ab117f729eaba9cb769461", //235265 | |
630 | "2ef49d2d27946ad7c5d5e4ab5c089696762ff04e855f8ab48e83bdf0cc68726d", //235295 | |
631 | "c85dcffb16d5a45bd239021ad33443414d60224760f11d535ae2063e5709efee", //235296 | |
c2123afe | 632 | // all vouts banned |
14aa6cc0 | 633 | "c4ea1462c207547cd6fb6a4155ca6d042b22170d29801a465db5c09fec55b19d", //246748 |
c2123afe | 634 | "305dc96d8bc23a69d3db955e03a6a87c1832673470c32fe25473a46cc473c7d1", //247204 |
c7a3aca6 | 635 | }; |
636 | ||
c2123afe | 637 | int32_t komodo_bannedset(int32_t *indallvoutsp,uint256 *array,int32_t max) |
c7a3aca6 | 638 | { |
639 | int32_t i; | |
c2123afe | 640 | if ( sizeof(banned_txids)/sizeof(*banned_txids) > max ) |
641 | { | |
642 | fprintf(stderr,"komodo_bannedset: buffer too small %ld vs %d\n",sizeof(banned_txids)/sizeof(*banned_txids),max); | |
643 | exit(-1); | |
644 | } | |
081ab21c | 645 | for (i=0; i<sizeof(banned_txids)/sizeof(*banned_txids); i++) |
646 | array[i] = uint256S(banned_txids[i]); | |
abdf25c8 | 647 | *indallvoutsp = i-2; |
464fe491 | 648 | return(i); |
c7a3aca6 | 649 | } |
650 | ||
db2b1957 | 651 | void komodo_passport_iteration(); |
652 | ||
ce5dd547 | 653 | int32_t komodo_check_deposit(int32_t height,const CBlock& block,uint32_t prevtime) // verify above block is valid pax pricing |
c7a3aca6 | 654 | { |
c2123afe | 655 | static uint256 array[64]; static int32_t numbanned,indallvouts; |
fb6bb4e8 | 656 | int32_t i,j,k,n,ht,baseid,txn_count,activation,num,opretlen,offset=1,errs=0,notmatched=0,matched=0,kmdheights[256],otherheights[256]; uint256 hash,txids[256]; char symbol[KOMODO_ASSETCHAIN_MAXLEN],base[KOMODO_ASSETCHAIN_MAXLEN]; uint16_t vouts[256]; int8_t baseids[256]; uint8_t *script,opcode,rmd160s[256*20]; uint64_t total,subsidy,available,deposited,issued,withdrawn,approved,redeemed,seed; int64_t checktoshis,values[256],srcvalues[256]; struct pax_transaction *pax; struct komodo_state *sp; CTransaction tx; |
ab0442fc | 657 | activation = 235300; |
c7a3aca6 | 658 | if ( *(int32_t *)&array[0] == 0 ) |
c2123afe | 659 | numbanned = komodo_bannedset(&indallvouts,array,(int32_t)(sizeof(array)/sizeof(*array))); |
1ed46112 | 660 | memset(baseids,0xff,sizeof(baseids)); |
661 | memset(values,0,sizeof(values)); | |
b89ab77c | 662 | memset(srcvalues,0,sizeof(srcvalues)); |
f360ea0a | 663 | memset(rmd160s,0,sizeof(rmd160s)); |
1ed46112 | 664 | memset(kmdheights,0,sizeof(kmdheights)); |
665 | memset(otherheights,0,sizeof(otherheights)); | |
c7a3aca6 | 666 | txn_count = block.vtx.size(); |
7f6ddefd | 667 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) |
c7a3aca6 | 668 | { |
7f6ddefd | 669 | for (i=0; i<txn_count; i++) |
c7a3aca6 | 670 | { |
2e12bbce | 671 | if ( i == 0 && txn_count > 1 && block.vtx[txn_count-1].vout.size() > 0 && block.vtx[txn_count-1].vout[0].nValue == 5000 ) |
dd249664 | 672 | { |
0c03fd07 | 673 | if ( block.vtx[txn_count-1].vin.size() == 1 && GetTransaction(block.vtx[txn_count-1].vin[0].prevout.hash,tx,hash,false) && block.vtx[0].vout[0].scriptPubKey == tx.vout[block.vtx[txn_count-1].vin[0].prevout.n].scriptPubKey ) |
dd249664 | 674 | notmatched = 1; |
675 | } | |
7f6ddefd | 676 | n = block.vtx[i].vin.size(); |
677 | for (j=0; j<n; j++) | |
c7a3aca6 | 678 | { |
c2123afe | 679 | for (k=0; k<numbanned; k++) |
081ab21c | 680 | { |
464fe491 | 681 | if ( block.vtx[i].vin[j].prevout.hash == array[k] && (block.vtx[i].vin[j].prevout.n == 1 || k >= indallvouts) ) |
7f6ddefd | 682 | { |
683 | printf("banned tx.%d being used at ht.%d txi.%d vini.%d\n",k,height,i,j); | |
684 | return(-1); | |
685 | } | |
081ab21c | 686 | } |
c7a3aca6 | 687 | } |
688 | } | |
689 | } | |
690 | n = block.vtx[0].vout.size(); | |
a9bad5a3 | 691 | //script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data(); |
4614c6c9 | 692 | //if ( n <= 2 || script[0] != 0x6a ) |
d083ecd5 | 693 | { |
2abd5dff | 694 | int64_t val,prevtotal = 0; int32_t strangeout=0,overflow = 0; |
6c8cc7ee | 695 | total = 0; |
696 | for (i=1; i<n; i++) | |
7bf32ee2 | 697 | { |
a9bad5a3 | 698 | script = (uint8_t *)block.vtx[0].vout[i].scriptPubKey.data(); |
7bf32ee2 | 699 | if ( (val= block.vtx[0].vout[i].nValue) < 0 || val >= MAX_MONEY ) |
700 | { | |
701 | overflow = 1; | |
702 | break; | |
703 | } | |
56c6c06b | 704 | if ( i > 1 && script[0] != 0x6a && val < 5000 ) |
a9bad5a3 | 705 | strangeout++; |
7bf32ee2 | 706 | total += val; |
707 | if ( total < prevtotal || (val != 0 && total == prevtotal) ) | |
708 | { | |
709 | overflow = 1; | |
710 | break; | |
711 | } | |
712 | prevtotal = total; | |
713 | } | |
f0279d99 | 714 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) |
d083ecd5 | 715 | { |
f0279d99 | 716 | if ( overflow != 0 || total > COIN/10 ) |
717 | { | |
ab0442fc | 718 | if ( height >= activation ) |
b3e591ac | 719 | { |
e2d299ee | 720 | if ( height > 800000 ) |
721 | fprintf(stderr,">>>>>>>> <<<<<<<<<< ht.%d illegal nonz output %.8f n.%d\n",height,dstr(block.vtx[0].vout[1].nValue),n); | |
f0279d99 | 722 | return(-1); |
b3e591ac | 723 | } |
f0279d99 | 724 | } |
cf7f4402 | 725 | else if ( block.nBits == KOMODO_MINDIFF_NBITS && total > 0 ) // to deal with fee stealing |
88d4a95a | 726 | { |
727 | fprintf(stderr,"notary mined ht.%d with extra %.8f\n",height,dstr(total)); | |
54ecb7e4 | 728 | if ( height > KOMODO_NOTARIES_HEIGHT1 ) |
729 | return(-1); | |
88d4a95a | 730 | } |
9e44b3ce | 731 | if ( strangeout != 0 || notmatched != 0 ) |
a9bad5a3 | 732 | { |
f5446e44 | 733 | if ( 0 && strcmp(NOTARY_PUBKEY.c_str(),"03b7621b44118017a16043f19b30cc8a4cfe068ac4e42417bae16ba460c80f3828") == 0 ) |
9c72a5cb | 734 | fprintf(stderr,">>>>>>>>>>>>> DUST ht.%d strangout.%d notmatched.%d <<<<<<<<<\n",height,strangeout,notmatched); |
51f0e500 | 735 | if ( height > 1000000 && strangeout != 0 ) |
736 | return(-1); | |
737 | } | |
58d36187 | 738 | else if ( height > 814000 ) |
51f0e500 | 739 | { |
740 | script = (uint8_t *)block.vtx[0].vout[0].scriptPubKey.data(); | |
9c72a5cb | 741 | return(-1 * (komodo_electednotary(&num,script+1,height,0) >= 0) * (height > 1000000)); |
a9bad5a3 | 742 | } |
f0279d99 | 743 | } |
744 | else | |
745 | { | |
fb6bb4e8 | 746 | checktoshis = 0; |
dc241f13 | 747 | if ( ASSETCHAINS_COMMISSION != 0 && height > 1 ) |
0afe191c | 748 | { |
44bbf01e | 749 | if ( (checktoshis= komodo_checkcommission((CBlock *)&block,height)) < 0 ) |
fb6bb4e8 | 750 | { |
30999b9c | 751 | fprintf(stderr,"ht.%d checktoshis %.8f overflow.%d total %.8f strangeout.%d\n",height,dstr(checktoshis),overflow,dstr(total),strangeout); |
fb6bb4e8 | 752 | return(-1); |
753 | } | |
0afe191c | 754 | } |
15d17928 | 755 | if ( height > 1 && checktoshis == 0 ) |
756 | { | |
757 | checktoshis = ((uint64_t)GetBlockSubsidy(height, Params().GetConsensus()) - block.vtx[0].vout[0].nValue); | |
e0f78699 | 758 | // some pools will need to change their pool fee to be (poolfee % - txfees) |
60a91e3b | 759 | //checktoshis += txn_count * 0.001; // rely on higher level validations to prevent emitting more coins than actual txfees |
15d17928 | 760 | } |
b7aeaa2d | 761 | if ( height >= 2 && (overflow != 0 || total > checktoshis || strangeout != 0) ) |
30999b9c | 762 | { |
763 | fprintf(stderr,"checkdeposit: ht.%d checktoshis %.8f overflow.%d total %.8f strangeout.%d\n",height,dstr(checktoshis),overflow,dstr(total),strangeout); | |
764 | if ( strangeout != 0 ) | |
765 | fprintf(stderr,">>>>>>>>>>>>> %s DUST ht.%d strangout.%d notmatched.%d <<<<<<<<<\n",ASSETCHAINS_SYMBOL,height,strangeout,notmatched); | |
766 | return(-1); | |
767 | } | |
8683bd8d | 768 | } |
a43401b4 | 769 | return(0); |
8683bd8d | 770 | } |
557d9a23 | 771 | } |
772 | ||
cd26c1f3 | 773 | const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source) |
429dabb5 | 774 | { |
16cd9f2d | 775 | uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33]; int32_t didstats,i,j,n,kvheight,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 fee,fiatoshis,komodoshis,checktoshis,values[64],srcvalues[64]; struct pax_transaction *pax,*pax2; struct komodo_state *basesp; double diff; |
1ed46112 | 776 | const char *typestr = "unknown"; |
088b1fa8 | 777 | if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 && opretbuf[0] != 'K' ) |
894c570b | 778 | { |
b27b2877 | 779 | //printf("komodo_opreturn skip %s\n",ASSETCHAINS_SYMBOL); |
716fb382 | 780 | return("assetchain"); |
894c570b | 781 | } |
1ed46112 | 782 | memset(baseids,0xff,sizeof(baseids)); |
783 | memset(values,0,sizeof(values)); | |
06cfcf31 | 784 | memset(srcvalues,0,sizeof(srcvalues)); |
f360ea0a | 785 | memset(rmd160s,0,sizeof(rmd160s)); |
1ed46112 | 786 | memset(kmdheights,0,sizeof(kmdheights)); |
787 | memset(otherheights,0,sizeof(otherheights)); | |
9596e284 | 788 | tokomodo = (komodo_is_issuer() == 0); |
ab6e81ec | 789 | if ( opretbuf[0] == 'K' && opretlen != 40 ) |
15d0fbd4 | 790 | { |
6b5cfbb4 | 791 | komodo_kvupdate(opretbuf,opretlen,value); |
43430608 | 792 | return("kv"); |
15d0fbd4 | 793 | } |
3a48f81a | 794 | else if ( ASSETCHAINS_SYMBOL[0] == 0 && KOMODO_PAX == 0 ) |
02b20e40 | 795 | return("nopax"); |
43430608 | 796 | if ( opretbuf[0] == 'D' ) |
64bb0834 | 797 | { |
9596e284 | 798 | tokomodo = 0; |
3fb80f10 | 799 | if ( opretlen == 38 ) // any KMD tx |
64bb0834 | 800 | { |
05a37d87 | 801 | iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight); |
abdd8672 | 802 | memset(base,0,sizeof(base)); |
803 | PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis); | |
abdd8672 | 804 | bitcoin_address(coinaddr,addrtype,rmd160,20); |
b6acec81 | 805 | checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis); |
806 | if ( komodo_paxcmp(base,kmdheight,value,checktoshis,kmdheight < 225000 ? seed : 0) != 0 ) | |
807 | checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,height,base,fiatoshis); | |
abdd8672 | 808 | typestr = "deposit"; |
063b17cc | 809 | if ( 0 && strcmp("NOK",base) == 0 ) |
0cd63cc8 | 810 | { |
d791f70c | 811 | printf("[%s] %s paxdeposit height.%d vs kmdheight.%d\n",ASSETCHAINS_SYMBOL,base,height,kmdheight); |
0cd63cc8 | 812 | 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); |
813 | for (i=0; i<32; i++) | |
814 | printf("%02x",((uint8_t *)&txid)[i]); | |
815 | printf(" <- txid.v%u ",vout); | |
816 | for (i=0; i<33; i++) | |
817 | printf("%02x",pubkey33[i]); | |
818 | printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height); | |
819 | } | |
3609f6b3 | 820 | if ( strcmp(base,ASSETCHAINS_SYMBOL) == 0 && (kmdheight > 195000 || kmdheight <= height) ) |
6e576848 | 821 | { |
f5e3cdfa | 822 | didstats = 0; |
41b528bb | 823 | if ( komodo_paxcmp(base,kmdheight,value,checktoshis,kmdheight < 225000 ? seed : 0) == 0 ) |
05a37d87 | 824 | { |
b915be35 | 825 | if ( (pax= komodo_paxfind(txid,vout,'D')) == 0 ) |
05a37d87 | 826 | { |
c98b3a08 | 827 | if ( (basesp= komodo_stateptrget(base)) != 0 ) |
05a37d87 | 828 | { |
c98b3a08 | 829 | basesp->deposited += fiatoshis; |
22a31d52 | 830 | didstats = 1; |
a13467f5 | 831 | if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 ) |
0398360f | 832 | printf("########### %p deposited %s += %.8f kmdheight.%d %.8f\n",basesp,base,dstr(fiatoshis),kmdheight,dstr(value)); |
c72ee808 | 833 | } else printf("cant get stateptr.(%s)\n",base); |
4d4194d3 | 834 | komodo_gateway_deposit(coinaddr,value,base,fiatoshis,rmd160,txid,vout,'D',kmdheight,height,(char *)"KMD",0); |
3f9cad6d | 835 | } |
51e5d952 | 836 | if ( (pax= komodo_paxfind(txid,vout,'D')) != 0 ) |
3f9cad6d | 837 | { |
8bd946ce | 838 | pax->height = kmdheight; |
839 | pax->validated = value; | |
88dff0d3 | 840 | pax->komodoshis = value; |
841 | pax->fiatoshis = fiatoshis; | |
22a31d52 | 842 | if ( didstats == 0 && pax->didstats == 0 ) |
3f9cad6d | 843 | { |
ca75995d | 844 | if ( (basesp= komodo_stateptrget(base)) != 0 ) |
845 | { | |
846 | basesp->deposited += fiatoshis; | |
22a31d52 | 847 | didstats = 1; |
a13467f5 | 848 | if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 ) |
c0d7dda6 | 849 | printf("########### %p depositedB %s += %.8f/%.8f kmdheight.%d/%d %.8f/%.8f\n",basesp,base,dstr(fiatoshis),dstr(pax->fiatoshis),kmdheight,pax->height,dstr(value),dstr(pax->komodoshis)); |
ca75995d | 850 | } |
4dabad2c | 851 | } // |
51e5d952 | 852 | if ( didstats != 0 ) |
853 | pax->didstats = 1; | |
eb706ef8 | 854 | if ( (pax2= komodo_paxfind(txid,vout,'I')) != 0 ) |
7ddc4ef7 | 855 | { |
856 | pax2->fiatoshis = pax->fiatoshis; | |
857 | pax2->komodoshis = pax->komodoshis; | |
7ddc4ef7 | 858 | pax->marked = pax2->marked = pax->height; |
da02b3be | 859 | pax2->height = pax->height = height; |
c0955f71 | 860 | if ( pax2->didstats == 0 ) |
25623352 | 861 | { |
bde7e5ef | 862 | if ( (basesp= komodo_stateptrget(base)) != 0 ) |
863 | { | |
864 | basesp->issued += pax2->fiatoshis; | |
865 | pax2->didstats = 1; | |
0c780ec8 | 866 | if ( 0 && strcmp(base,"USD") == 0 ) |
82315b7d | 867 | printf("########### %p issueda %s += %.8f kmdheight.%d %.8f other.%d [%d]\n",basesp,base,dstr(pax2->fiatoshis),pax2->height,dstr(pax2->komodoshis),pax2->otherheight,height); |
bde7e5ef | 868 | } |
25623352 | 869 | } |
7ddc4ef7 | 870 | } |
3f9cad6d | 871 | } |
eb928486 | 872 | } |
d6cc943d | 873 | else |
874 | { | |
875 | if ( (pax= komodo_paxfind(txid,vout,'D')) != 0 ) | |
876 | pax->marked = checktoshis; | |
877 | if ( kmdheight > 238000 && (kmdheight > 214700 || strcmp(base,ASSETCHAINS_SYMBOL) == 0) ) //seed != 0 && | |
878 | printf("pax %s deposit %.8f rejected kmdheight.%d %.8f KMD check %.8f seed.%llu\n",base,dstr(fiatoshis),kmdheight,dstr(value),dstr(checktoshis),(long long)seed); | |
879 | } | |
73d738df | 880 | } //else printf("[%s] %s paxdeposit height.%d vs kmdheight.%d\n",ASSETCHAINS_SYMBOL,base,height,kmdheight); |
78a175f2 | 881 | } //else printf("unsupported size.%d for opreturn D\n",opretlen); |
64bb0834 | 882 | } |
36b8ed99 | 883 | else if ( opretbuf[0] == 'I' ) |
884 | { | |
885 | tokomodo = 0; | |
7b078158 | 886 | if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 && strncmp(ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4],3) == 0 ) |
36b8ed99 | 887 | { |
888 | if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,0)) > 0 ) | |
889 | { | |
890 | for (i=0; i<n; i++) | |
891 | { | |
892 | if ( baseids[i] < 0 ) | |
893 | { | |
00603efa | 894 | static uint32_t counter; |
9a3917ed | 895 | if ( counter++ < 0 ) |
00603efa | 896 | printf("%d of %d illegal baseid.%d, this can be ignored\n",i,n,baseids[i]); |
36b8ed99 | 897 | continue; |
898 | } | |
899 | bitcoin_address(coinaddr,60,&rmd160s[i*20],20); | |
e2879b92 | 900 | komodo_gateway_deposit(coinaddr,0,ASSETCHAINS_SYMBOL,0,0,txids[i],vouts[i],'I',height,0,CURRENCIES[baseids[i]],0); |
36b8ed99 | 901 | komodo_paxmark(height,txids[i],vouts[i],'I',height); |
902 | if ( (pax= komodo_paxfind(txids[i],vouts[i],'I')) != 0 ) | |
903 | { | |
904 | pax->type = opretbuf[0]; | |
905 | strcpy(pax->source,(char *)&opretbuf[opretlen-4]); | |
a6a33512 | 906 | if ( (pax2= komodo_paxfind(txids[i],vouts[i],'D')) != 0 && pax2->fiatoshis != 0 && pax2->komodoshis != 0 ) |
36b8ed99 | 907 | { |
908 | // realtime path? | |
909 | pax->fiatoshis = pax2->fiatoshis; | |
910 | pax->komodoshis = pax2->komodoshis; | |
911 | pax->marked = pax2->marked = pax2->height; | |
5b62b979 | 912 | if ( pax->didstats == 0 ) |
36b8ed99 | 913 | { |
914 | if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) | |
915 | { | |
916 | basesp->issued += pax->fiatoshis; | |
917 | pax->didstats = 1; | |
39863e30 | 918 | pax->height = pax2->height; |
919 | pax->otherheight = height; | |
82315b7d | 920 | if ( 1 && strcmp(CURRENCIES[baseids[i]],"USD") == 0 ) |
921 | printf("########### %p issuedb %s += %.8f kmdheight.%d %.8f other.%d [%d]\n",basesp,CURRENCIES[baseids[i]],dstr(pax->fiatoshis),pax->height,dstr(pax->komodoshis),pax->otherheight,height); | |
36b8ed99 | 922 | } |
923 | } | |
924 | } | |
925 | } | |
57f349cb | 926 | if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'I',height)) != 0 ) |
927 | komodo_paxdelete(pax); | |
928 | if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'D',height)) != 0 ) | |
929 | komodo_paxdelete(pax); | |
36b8ed99 | 930 | } |
174cae41 | 931 | } //else printf("opreturn none issued?\n"); |
36b8ed99 | 932 | } |
36b8ed99 | 933 | } |
7b078158 | 934 | else if ( height < 236000 && opretbuf[0] == 'W' && strncmp(ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4],3) == 0 )//&& opretlen >= 38 ) |
01d9ed2f | 935 | { |
bc653eee | 936 | if ( komodo_baseid((char *)&opretbuf[opretlen-4]) >= 0 && strcmp(ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4]) == 0 ) |
8b4d7f27 | 937 | { |
938 | for (i=0; i<opretlen; i++) | |
939 | printf("%02x",opretbuf[i]); | |
d594eaa4 | 940 | printf(" [%s] reject obsolete withdraw request.%s\n",ASSETCHAINS_SYMBOL,(char *)&opretbuf[opretlen-4]); |
217eb34f | 941 | return(typestr); |
8b4d7f27 | 942 | } |
9596e284 | 943 | tokomodo = 1; |
01d9ed2f | 944 | iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight); |
945 | memset(base,0,sizeof(base)); | |
c3d94aec | 946 | PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&komodoshis); |
01d9ed2f | 947 | bitcoin_address(coinaddr,addrtype,rmd160,20); |
c3d94aec | 948 | checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,value); |
01d9ed2f | 949 | typestr = "withdraw"; |
8b4d7f27 | 950 | //printf(" [%s] WITHDRAW %s.height.%d vs height.%d check %.8f/%.8f vs %.8f tokomodo.%d %d seed.%llx -> (%s) len.%d\n",ASSETCHAINS_SYMBOL,base,kmdheight,height,dstr(checktoshis),dstr(komodoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed,coinaddr,opretlen); |
f5e3cdfa | 951 | didstats = 0; |
a39d7b21 | 952 | //if ( komodo_paxcmp(base,kmdheight,komodoshis,checktoshis,seed) == 0 ) |
01d9ed2f | 953 | { |
51e5d952 | 954 | if ( value != 0 && ((pax= komodo_paxfind(txid,vout,'W')) == 0 || pax->didstats == 0) ) |
0b556992 | 955 | { |
0126d891 | 956 | if ( (basesp= komodo_stateptrget(base)) != 0 ) |
957 | { | |
958 | basesp->withdrawn += value; | |
959 | didstats = 1; | |
dd36961a | 960 | if ( 0 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 ) |
5b489b55 | 961 | printf("########### %p withdrawn %s += %.8f check %.8f\n",basesp,base,dstr(value),dstr(checktoshis)); |
0126d891 | 962 | } |
57f349cb | 963 | if ( 0 && strcmp(base,"RUB") == 0 && (pax == 0 || pax->approved == 0) ) |
4718923d | 964 | printf("notarize %s %.8f -> %.8f kmd.%d other.%d\n",ASSETCHAINS_SYMBOL,dstr(value),dstr(komodoshis),kmdheight,height); |
ea365a71 | 965 | } |
115e6a5c | 966 | komodo_gateway_deposit(coinaddr,0,(char *)"KMD",value,rmd160,txid,vout,'W',kmdheight,height,source,0); |
51e5d952 | 967 | if ( (pax= komodo_paxfind(txid,vout,'W')) != 0 ) |
daeecf95 | 968 | { |
969 | pax->type = opretbuf[0]; | |
1ac3f9ef | 970 | strcpy(pax->source,base); |
971 | strcpy(pax->symbol,"KMD"); | |
daeecf95 | 972 | pax->height = kmdheight; |
973 | pax->otherheight = height; | |
1e22f865 | 974 | pax->komodoshis = komodoshis; |
daeecf95 | 975 | } |
a39d7b21 | 976 | } // else printf("withdraw %s paxcmp ht.%d %d error value %.8f -> %.8f vs %.8f\n",base,kmdheight,height,dstr(value),dstr(komodoshis),dstr(checktoshis)); |
977 | // need to allocate pax | |
a4c67285 | 978 | } |
b2e010ee | 979 | else if ( height < 236000 && tokomodo != 0 && opretbuf[0] == 'A' && ASSETCHAINS_SYMBOL[0] == 0 ) |
a4c67285 | 980 | { |
9596e284 | 981 | tokomodo = 1; |
109afe64 | 982 | if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 ) |
826bfe51 | 983 | { |
984 | for (i=0; i<opretlen; i++) | |
985 | printf("%02x",opretbuf[i]); | |
40f3d9cc | 986 | printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f opretlen.%d\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()),opretlen); |
826bfe51 | 987 | } |
b89ab77c | 988 | if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 ) |
a4c67285 | 989 | { |
990 | for (i=0; i<n; i++) | |
991 | { | |
885643be | 992 | //for (j=0; j<32; j++) |
993 | // printf("%02x",((uint8_t *)&txids[i])[j]); | |
994 | //printf(" v%d %.8f %.8f k.%d ht.%d base.%d\n",vouts[i],dstr(values[i]),dstr(srcvalues[i]),kmdheights[i],otherheights[i],baseids[i]); | |
b89ab77c | 995 | if ( baseids[i] < 0 ) |
8e7b40d9 | 996 | { |
afdb28f3 | 997 | for (i=0; i<opretlen; i++) |
998 | printf("%02x",opretbuf[i]); | |
999 | printf(" opret[%c] else path tokomodo.%d ht.%d before %.8f opretlen.%d\n",opretbuf[0],tokomodo,height,dstr(komodo_paxtotal()),opretlen); | |
8952ee9d | 1000 | //printf("baseids[%d] %d\n",i,baseids[i]); |
afdb28f3 | 1001 | if ( (pax= komodo_paxfind(txids[i],vouts[i],'W')) != 0 || (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 ) |
51752631 | 1002 | { |
1003 | baseids[i] = komodo_baseid(pax->symbol); | |
1004 | printf("override neg1 with (%s)\n",pax->symbol); | |
1005 | } | |
1006 | if ( baseids[i] < 0 ) | |
1007 | continue; | |
8e7b40d9 | 1008 | } |
f5e3cdfa | 1009 | didstats = 0; |
0cc6ad81 | 1010 | seed = 0; |
d515094d | 1011 | checktoshis = komodo_paxprice(&seed,kmdheights[i],CURRENCIES[baseids[i]],(char *)"KMD",(uint64_t)values[i]); |
8b4d7f27 | 1012 | //printf("PAX_fiatdest ht.%d price %s %.8f -> KMD %.8f vs %.8f\n",kmdheights[i],CURRENCIES[baseids[i]],(double)values[i]/COIN,(double)srcvalues[i]/COIN,(double)checktoshis/COIN); |
371a9c87 | 1013 | if ( srcvalues[i] == checktoshis ) |
a4c67285 | 1014 | { |
36a4e92c | 1015 | if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 ) |
0fa85cc3 | 1016 | { |
371a9c87 | 1017 | bitcoin_address(coinaddr,60,&rmd160s[i*20],20); |
5481c677 | 1018 | komodo_gateway_deposit(coinaddr,srcvalues[i],CURRENCIES[baseids[i]],values[i],&rmd160s[i*20],txids[i],vouts[i],'A',kmdheights[i],otherheights[i],CURRENCIES[baseids[i]],kmdheights[i]); |
371a9c87 | 1019 | if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) == 0 ) |
1020 | printf("unexpected null pax for approve\n"); | |
1021 | else pax->validated = checktoshis; | |
1022 | if ( (pax2= komodo_paxfind(txids[i],vouts[i],'W')) != 0 ) | |
1023 | pax2->approved = kmdheights[i]; | |
7aadf9a9 | 1024 | komodo_paxmark(height,txids[i],vouts[i],'W',height); |
371a9c87 | 1025 | //komodo_paxmark(height,txids[i],vouts[i],'A',height); |
bca09689 | 1026 | if ( values[i] != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) |
371a9c87 | 1027 | { |
5481c677 | 1028 | basesp->approved += values[i]; |
371a9c87 | 1029 | didstats = 1; |
d01c0463 | 1030 | //printf("pax.%p ########### %p approved %s += %.8f -> %.8f/%.8f kht.%d %d\n",pax,basesp,CURRENCIES[baseids[i]],dstr(values[i]),dstr(srcvalues[i]),dstr(checktoshis),kmdheights[i],otherheights[i]); |
371a9c87 | 1031 | } |
1032 | //printf(" i.%d (%s) <- %.8f ADDFLAG APPROVED\n",i,coinaddr,dstr(values[i])); | |
0fa85cc3 | 1033 | } |
371a9c87 | 1034 | else if ( pax->didstats == 0 && srcvalues[i] != 0 ) |
3f9cad6d | 1035 | { |
371a9c87 | 1036 | if ( (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) |
1037 | { | |
5481c677 | 1038 | basesp->approved += values[i]; |
371a9c87 | 1039 | didstats = 1; |
d01c0463 | 1040 | //printf("pax.%p ########### %p approved %s += %.8f -> %.8f/%.8f kht.%d %d\n",pax,basesp,CURRENCIES[baseids[i]],dstr(values[i]),dstr(srcvalues[i]),dstr(checktoshis),kmdheights[i],otherheights[i]); |
371a9c87 | 1041 | } |
1042 | } //else printf(" i.%d of n.%d pax.%p baseids[] %d\n",i,n,pax,baseids[i]); | |
1043 | if ( (pax= komodo_paxfind(txids[i],vouts[i],'A')) != 0 ) | |
1044 | { | |
1045 | pax->type = opretbuf[0]; | |
1046 | pax->approved = kmdheights[i]; | |
1047 | pax->validated = checktoshis; | |
1048 | if ( didstats != 0 ) | |
1049 | pax->didstats = 1; | |
7ad9ed4d | 1050 | //if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) |
5481c677 | 1051 | //printf(" i.%d approved.%d <<<<<<<<<<<<< APPROVED %p\n",i,kmdheights[i],pax); |
371a9c87 | 1052 | } |
06d1a655 | 1053 | } |
ea365a71 | 1054 | } |
dd278be2 | 1055 | } //else printf("n.%d from opreturns\n",n); |
bff51d67 | 1056 | //printf("extra.[%d] after %.8f\n",n,dstr(komodo_paxtotal())); |
01d9ed2f | 1057 | } |
b2e010ee | 1058 | else if ( height < 236000 && opretbuf[0] == 'X' && ASSETCHAINS_SYMBOL[0] == 0 ) |
ca3c6549 | 1059 | { |
9596e284 | 1060 | tokomodo = 1; |
c65d0868 | 1061 | if ( (n= komodo_issued_opreturn(base,txids,vouts,values,srcvalues,kmdheights,otherheights,baseids,rmd160s,opretbuf,opretlen,1)) > 0 ) |
4c58b338 | 1062 | { |
1063 | for (i=0; i<n; i++) | |
1064 | { | |
3f9cad6d | 1065 | if ( baseids[i] < 0 ) |
1066 | continue; | |
4c58b338 | 1067 | bitcoin_address(coinaddr,60,&rmd160s[i*20],20); |
51e5d952 | 1068 | komodo_gateway_deposit(coinaddr,0,0,0,0,txids[i],vouts[i],'X',height,0,(char *)"KMD",0); |
aa114a60 | 1069 | komodo_paxmark(height,txids[i],vouts[i],'W',height); |
1070 | komodo_paxmark(height,txids[i],vouts[i],'A',height); | |
1071 | komodo_paxmark(height,txids[i],vouts[i],'X',height); | |
51e5d952 | 1072 | if ( (pax= komodo_paxfind(txids[i],vouts[i],'X')) != 0 ) |
ca75995d | 1073 | { |
1074 | pax->type = opretbuf[0]; | |
6276f755 | 1075 | if ( height < 121842 ) // fields got switched around due to legacy issues and approves |
2ee39fae | 1076 | value = srcvalues[i]; |
1077 | else value = values[i]; | |
1078 | if ( baseids[i] >= 0 && value != 0 && (basesp= komodo_stateptrget(CURRENCIES[baseids[i]])) != 0 ) | |
c6eca157 | 1079 | { |
2ee39fae | 1080 | basesp->redeemed += value; |
546672f5 | 1081 | pax->didstats = 1; |
dff9ec91 | 1082 | if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) |
e62f7114 | 1083 | printf("ht.%d %.8f ########### %p redeemed %s += %.8f %.8f kht.%d ht.%d\n",height,dstr(value),basesp,CURRENCIES[baseids[i]],dstr(value),dstr(srcvalues[i]),kmdheights[i],otherheights[i]); |
c6eca157 | 1084 | } |
ca75995d | 1085 | } |
57f349cb | 1086 | if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'W',height)) != 0 ) |
1087 | komodo_paxdelete(pax); | |
1088 | if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'A',height)) != 0 ) | |
1089 | komodo_paxdelete(pax); | |
1090 | if ( (pax= komodo_paxmark(height,txids[i],vouts[i],'X',height)) != 0 ) | |
1091 | komodo_paxdelete(pax); | |
4c58b338 | 1092 | } |
dff4ef45 | 1093 | } //else printf("komodo_issued_opreturn returned %d\n",n); |
ca3c6549 | 1094 | } |
64bb0834 | 1095 | return(typestr); |
1096 | } | |
1097 | ||
af786d5d | 1098 | int32_t komodo_parsestatefiledata(struct komodo_state *sp,uint8_t *filedata,long *fposp,long datalen,char *symbol,char *dest); |
1099 | ||
3b40d631 | 1100 | void komodo_stateind_set(struct komodo_state *sp,uint32_t *inds,int32_t n,uint8_t *filedata,long datalen,char *symbol,char *dest) |
1101 | { | |
7730b5c4 | 1102 | uint8_t func; long lastK,lastT,lastN,lastV,fpos,lastfpos; int32_t i,count,doissue,iter,numn,numv,numN,numV,numR; uint32_t tmp,prevpos100,offset; |
40537f14 | 1103 | count = numR = numN = numV = numn = numv = 0; |
1104 | lastK = lastT = lastN = lastV = -1; | |
43430608 | 1105 | for (iter=0; iter<2; iter++) |
1106 | { | |
ddef2480 | 1107 | for (lastfpos=fpos=prevpos100=i=0; i<n; i++) |
43430608 | 1108 | { |
1109 | tmp = inds[i]; | |
1110 | if ( (i % 100) == 0 ) | |
1111 | prevpos100 = tmp; | |
1112 | else | |
1113 | { | |
1114 | func = (tmp & 0xff); | |
1115 | offset = (tmp >> 8); | |
1116 | fpos = prevpos100 + offset; | |
4978e8cf | 1117 | if ( lastfpos >= datalen || (filedata[lastfpos] != func && func != 0) ) |
0c6f8301 | 1118 | printf("i.%d/n.%d lastfpos.%ld >= datalen.%ld or [%d] != func.%d\n",i,n,lastfpos,datalen,filedata[lastfpos],func); |
43430608 | 1119 | else if ( iter == 0 ) |
1120 | { | |
1121 | switch ( func ) | |
1122 | { | |
1123 | default: case 'P': case 'U': case 'D': | |
1124 | inds[i] &= 0xffffff00; | |
1125 | break; | |
1126 | case 'K': | |
1127 | lastK = lastfpos; | |
1128 | inds[i] &= 0xffffff00; | |
1129 | break; | |
1130 | case 'T': | |
1131 | lastT = lastfpos; | |
1132 | inds[i] &= 0xffffff00; | |
1133 | break; | |
1134 | case 'N': | |
1135 | lastN = lastfpos; | |
1136 | numN++; | |
1137 | break; | |
1138 | case 'V': | |
1139 | lastV = lastfpos; | |
1140 | numV++; | |
1141 | break; | |
40537f14 | 1142 | case 'R': |
1143 | numR++; | |
1144 | break; | |
43430608 | 1145 | } |
1146 | } | |
1147 | else | |
1148 | { | |
1149 | doissue = 0; | |
1150 | if ( func == 'K' ) | |
1151 | { | |
1152 | if ( lastK == lastfpos ) | |
e31b9616 | 1153 | doissue = 1; |
43430608 | 1154 | } |
1155 | else if ( func == 'T' ) | |
1156 | { | |
1157 | if ( lastT == lastfpos ) | |
e31b9616 | 1158 | doissue = 1; |
43430608 | 1159 | } |
1160 | else if ( func == 'N' ) | |
1161 | { | |
1162 | if ( numn > numN-128 ) | |
1163 | doissue = 1; | |
1164 | numn++; | |
1165 | } | |
1166 | else if ( func == 'V' ) | |
1167 | { | |
e9d73912 | 1168 | if ( KOMODO_PAX != 0 && numv > numV-1440 ) |
43430608 | 1169 | doissue = 1; |
1170 | numv++; | |
1171 | } | |
1172 | else if ( func == 'R' ) | |
1173 | doissue = 1; | |
1174 | if ( doissue != 0 ) | |
1175 | { | |
40537f14 | 1176 | //printf("issue %c total.%d lastfpos.%ld\n",func,count,lastfpos); |
43430608 | 1177 | komodo_parsestatefiledata(sp,filedata,&lastfpos,datalen,symbol,dest); |
1178 | count++; | |
1179 | } | |
1180 | } | |
1181 | } | |
1182 | lastfpos = fpos; | |
1183 | } | |
1184 | } | |
40537f14 | 1185 | printf("numR.%d numV.%d numN.%d count.%d\n",numR,numV,numN,count); |
bf9c62be | 1186 | /*else if ( func == 'K' ) // KMD height: stop after 1st |
43430608 | 1187 | else if ( func == 'T' ) // KMD height+timestamp: stop after 1st |
1188 | ||
1189 | else if ( func == 'N' ) // notarization, scan backwards 1440+ blocks; | |
1190 | else if ( func == 'V' ) // price feed: can stop after 1440+ | |
bf9c62be | 1191 | else if ( func == 'R' ) // opreturn:*/ |
3b40d631 | 1192 | } |
1193 | ||
e9c76158 | 1194 | void *OS_loadfile(char *fname,uint8_t **bufp,long *lenp,long *allocsizep) |
17746e45 | 1195 | { |
1196 | FILE *fp; | |
1197 | long filesize,buflen = *allocsizep; | |
e9c76158 | 1198 | uint8_t *buf = *bufp; |
17746e45 | 1199 | *lenp = 0; |
1200 | if ( (fp= fopen(fname,"rb")) != 0 ) | |
1201 | { | |
1202 | fseek(fp,0,SEEK_END); | |
1203 | filesize = ftell(fp); | |
1204 | if ( filesize == 0 ) | |
1205 | { | |
1206 | fclose(fp); | |
1207 | *lenp = 0; | |
1208 | printf("OS_loadfile null size.(%s)\n",fname); | |
1209 | return(0); | |
1210 | } | |
2baa86a3 | 1211 | if ( filesize > buflen ) |
17746e45 | 1212 | { |
cb7f1d47 | 1213 | *allocsizep = filesize; |
2baa86a3 | 1214 | *bufp = buf = (uint8_t *)realloc(buf,(long)*allocsizep+64); |
17746e45 | 1215 | } |
1216 | rewind(fp); | |
1217 | if ( buf == 0 ) | |
1218 | printf("Null buf ???\n"); | |
1219 | else | |
1220 | { | |
1221 | if ( fread(buf,1,(long)filesize,fp) != (unsigned long)filesize ) | |
1222 | printf("error reading filesize.%ld\n",(long)filesize); | |
1223 | buf[filesize] = 0; | |
1224 | } | |
1225 | fclose(fp); | |
1226 | *lenp = filesize; | |
1227 | //printf("loaded.(%s)\n",buf); | |
1228 | } //else printf("OS_loadfile couldnt load.(%s)\n",fname); | |
1229 | return(buf); | |
1230 | } | |
1231 | ||
1232 | uint8_t *OS_fileptr(long *allocsizep,char *fname) | |
1233 | { | |
e9c76158 | 1234 | long filesize = 0; uint8_t *buf = 0; void *retptr; |
17746e45 | 1235 | *allocsizep = 0; |
1236 | retptr = OS_loadfile(fname,&buf,&filesize,allocsizep); | |
e9c76158 | 1237 | return((uint8_t *)retptr); |
17746e45 | 1238 | } |
1239 | ||
3b40d631 | 1240 | long komodo_stateind_validate(struct komodo_state *sp,char *indfname,uint8_t *filedata,long datalen,uint32_t *prevpos100p,uint32_t *indcounterp,char *symbol,char *dest) |
1241 | { | |
d6066074 | 1242 | FILE *fp; long fsize,lastfpos=0,fpos=0; uint8_t *inds,func; int32_t i,n; uint32_t offset,tmp,prevpos100 = 0; |
3b40d631 | 1243 | *indcounterp = *prevpos100p = 0; |
1244 | if ( (inds= OS_fileptr(&fsize,indfname)) != 0 ) | |
1245 | { | |
5b5c0bf3 | 1246 | lastfpos = 0; |
b883389a | 1247 | fprintf(stderr,"inds.%p validate %s fsize.%ld datalen.%ld n.%ld lastfpos.%ld\n",inds,indfname,fsize,datalen,fsize / sizeof(uint32_t),lastfpos); |
3b40d631 | 1248 | if ( (fsize % sizeof(uint32_t)) == 0 ) |
1249 | { | |
1250 | n = (int32_t)(fsize / sizeof(uint32_t)); | |
1251 | for (i=0; i<n; i++) | |
37c74e15 | 1252 | { |
3b40d631 | 1253 | memcpy(&tmp,&inds[i * sizeof(uint32_t)],sizeof(uint32_t)); |
95880660 | 1254 | if ( 0 && i > n-10 ) |
f7f96087 | 1255 | printf("%d: tmp.%08x [%c] prevpos100.%u\n",i,tmp,tmp&0xff,prevpos100); |
3b40d631 | 1256 | if ( (i % 100) == 0 ) |
1257 | prevpos100 = tmp; | |
1258 | else | |
1259 | { | |
1260 | func = (tmp & 0xff); | |
1261 | offset = (tmp >> 8); | |
1262 | fpos = prevpos100 + offset; | |
5d5c720d | 1263 | if ( lastfpos >= datalen || filedata[lastfpos] != func ) |
3b40d631 | 1264 | { |
75f0def0 | 1265 | printf("validate.%d error (%u %d) prev100 %u -> fpos.%ld datalen.%ld [%d] (%c) vs (%c) lastfpos.%ld\n",i,offset,func,prevpos100,fpos,datalen,lastfpos < datalen ? filedata[lastfpos] : -1,func,filedata[lastfpos],lastfpos); |
3b40d631 | 1266 | return(-1); |
1267 | } | |
1268 | } | |
5b5c0bf3 | 1269 | lastfpos = fpos; |
3b40d631 | 1270 | } |
1271 | *indcounterp = n; | |
1272 | *prevpos100p = prevpos100; | |
3b40d631 | 1273 | if ( sp != 0 ) |
1274 | komodo_stateind_set(sp,(uint32_t *)inds,n,filedata,fpos,symbol,dest); | |
dbd0722f | 1275 | //printf("free inds.%p %s validated[%d] fpos.%ld datalen.%ld, offset %ld vs fsize.%ld\n",inds,indfname,i,fpos,datalen,i * sizeof(uint32_t),fsize); |
2baa86a3 | 1276 | free(inds); |
5b5c0bf3 | 1277 | return(fpos); |
3b40d631 | 1278 | } else printf("wrong filesize %s %ld\n",indfname,fsize); |
1279 | } | |
1280 | free(inds); | |
6cc0f9de | 1281 | fprintf(stderr,"indvalidate return -1\n"); |
3b40d631 | 1282 | return(-1); |
1283 | } | |
1284 | ||
1285 | long komodo_indfile_update(FILE *indfp,uint32_t *prevpos100p,long lastfpos,long newfpos,uint8_t func,uint32_t *indcounterp) | |
1286 | { | |
1287 | uint32_t tmp; | |
1288 | if ( indfp != 0 ) | |
1289 | { | |
5b5c0bf3 | 1290 | tmp = ((uint32_t)(newfpos - *prevpos100p) << 8) | (func & 0xff); |
3b40d631 | 1291 | if ( ftell(indfp)/sizeof(uint32_t) != *indcounterp ) |
b8359369 | 1292 | printf("indfp fpos %ld -> ind.%ld vs counter.%u\n",ftell(indfp),ftell(indfp)/sizeof(uint32_t),*indcounterp); |
43430608 | 1293 | //fprintf(stderr,"ftell.%ld indcounter.%u lastfpos.%ld newfpos.%ld func.%02x\n",ftell(indfp),*indcounterp,lastfpos,newfpos,func); |
1f14a516 | 1294 | fwrite(&tmp,1,sizeof(tmp),indfp), (*indcounterp)++; |
3b40d631 | 1295 | if ( (*indcounterp % 100) == 0 ) |
1296 | { | |
1297 | *prevpos100p = (uint32_t)newfpos; | |
1298 | fwrite(prevpos100p,1,sizeof(*prevpos100p),indfp), (*indcounterp)++; | |
1299 | } | |
1300 | } | |
1301 | return(newfpos); | |
1302 | } | |
1303 | ||
1304 | int32_t komodo_faststateinit(struct komodo_state *sp,char *fname,char *symbol,char *dest) | |
1305 | { | |
1ff645e6 | 1306 | FILE *indfp; char indfname[1024]; uint8_t *filedata; long validated=-1,datalen,fpos,lastfpos; uint32_t tmp,prevpos100,indcounter,starttime; int32_t func,finished = 0; |
af786d5d | 1307 | starttime = (uint32_t)time(NULL); |
3b40d631 | 1308 | safecopy(indfname,fname,sizeof(indfname)-4); |
1309 | strcat(indfname,".ind"); | |
1310 | if ( (filedata= OS_fileptr(&datalen,fname)) != 0 ) | |
1311 | { | |
3a48f81a | 1312 | if ( 1 )//datalen >= (1LL << 32) || GetArg("-genind",0) != 0 || (validated= komodo_stateind_validate(0,indfname,filedata,datalen,&prevpos100,&indcounter,symbol,dest)) < 0 ) |
3b40d631 | 1313 | { |
1314 | lastfpos = fpos = 0; | |
1315 | indcounter = prevpos100 = 0; | |
1316 | if ( (indfp= fopen(indfname,"wb")) != 0 ) | |
1317 | fwrite(&prevpos100,1,sizeof(prevpos100),indfp), indcounter++; | |
1ff645e6 | 1318 | fprintf(stderr,"processing %s %ldKB, validated.%ld\n",fname,datalen/1024,validated); |
3b40d631 | 1319 | while ( (func= komodo_parsestatefiledata(sp,filedata,&fpos,datalen,symbol,dest)) >= 0 ) |
1320 | { | |
1321 | lastfpos = komodo_indfile_update(indfp,&prevpos100,lastfpos,fpos,func,&indcounter); | |
1322 | } | |
1323 | if ( indfp != 0 ) | |
1324 | { | |
1325 | fclose(indfp); | |
01c8221f | 1326 | if ( (fpos= komodo_stateind_validate(0,indfname,filedata,datalen,&prevpos100,&indcounter,symbol,dest)) < 0 ) |
3b40d631 | 1327 | printf("unexpected komodostate.ind validate failure %s datalen.%ld\n",indfname,datalen); |
01c8221f | 1328 | else printf("%s validated fpos.%ld\n",indfname,fpos); |
3b40d631 | 1329 | } |
1330 | finished = 1; | |
1331 | fprintf(stderr,"took %d seconds to process %s %ldKB\n",(int32_t)(time(NULL)-starttime),fname,datalen/1024); | |
1332 | } | |
1ff645e6 | 1333 | else if ( validated > 0 ) |
3b40d631 | 1334 | { |
1335 | if ( (indfp= fopen(indfname,"rb+")) != 0 ) | |
1336 | { | |
1ff645e6 | 1337 | lastfpos = fpos = validated; |
e3c63db2 | 1338 | fprintf(stderr,"datalen.%ld validated %ld -> indcounter %u, prevpos100 %u offset.%ld\n",datalen,validated,indcounter,prevpos100,indcounter * sizeof(uint32_t)); |
1339 | if ( fpos < datalen ) | |
3b40d631 | 1340 | { |
e3c63db2 | 1341 | fseek(indfp,indcounter * sizeof(uint32_t),SEEK_SET); |
1342 | if ( ftell(indfp) == indcounter * sizeof(uint32_t) ) | |
1f14a516 | 1343 | { |
e3c63db2 | 1344 | while ( (func= komodo_parsestatefiledata(sp,filedata,&fpos,datalen,symbol,dest)) >= 0 ) |
1345 | { | |
1346 | lastfpos = komodo_indfile_update(indfp,&prevpos100,lastfpos,fpos,func,&indcounter); | |
1347 | if ( lastfpos != fpos ) | |
1348 | fprintf(stderr,"unexpected lastfpos.%ld != %ld\n",lastfpos,fpos); | |
1349 | } | |
1f14a516 | 1350 | } |
e3c63db2 | 1351 | fclose(indfp); |
3b40d631 | 1352 | } |
4c87a8df | 1353 | if ( komodo_stateind_validate(sp,indfname,filedata,datalen,&prevpos100,&indcounter,symbol,dest) < 0 ) |
3b40d631 | 1354 | printf("unexpected komodostate.ind validate failure %s datalen.%ld\n",indfname,datalen); |
1355 | else | |
1356 | { | |
dbd0722f | 1357 | printf("%s validated updated from validated.%ld to %ld new.[%ld] -> indcounter %u, prevpos100 %u offset.%ld | elapsed %d seconds\n",indfname,validated,fpos,fpos-validated,indcounter,prevpos100,indcounter * sizeof(uint32_t),(int32_t)(time(NULL) - starttime)); |
3b40d631 | 1358 | finished = 1; |
1359 | } | |
1360 | } | |
1361 | } else printf("komodo_faststateinit unexpected case\n"); | |
1362 | free(filedata); | |
1363 | return(finished == 1); | |
1364 | } | |
1365 | return(-1); | |
1366 | } | |
17746e45 | 1367 | |
247428bd | 1368 | uint64_t komodo_interestsum(); |
1369 | ||
30d79208 | 1370 | void komodo_passport_iteration() |
ab918767 | 1371 | { |
a6cbaa25 | 1372 | static long lastpos[34]; static char userpass[33][1024]; static uint32_t lasttime,callcounter,lastinterest; |
59d405e8 | 1373 | int32_t maxseconds = 10; |
7c130297 | 1374 | FILE *fp; uint8_t *filedata; long fpos,datalen,lastfpos; int32_t baseid,limit,n,ht,isrealtime,expired,refid,blocks,longest; struct komodo_state *sp,*refsp; char *retstr,fname[512],*base,symbol[KOMODO_ASSETCHAIN_MAXLEN],dest[KOMODO_ASSETCHAIN_MAXLEN]; uint32_t buf[3],starttime; cJSON *infoobj,*result; uint64_t RTmask = 0; |
430d3c77 | 1375 | expired = 0; |
17899df4 | 1376 | while ( KOMODO_INITDONE == 0 ) |
1377 | { | |
e40060c2 | 1378 | fprintf(stderr,"[%s] PASSPORT iteration waiting for KOMODO_INITDONE\n",ASSETCHAINS_SYMBOL); |
17899df4 | 1379 | sleep(3); |
1380 | } | |
a6cbaa25 | 1381 | if ( komodo_chainactive_timestamp() > lastinterest ) |
1382 | { | |
1383 | komodo_interestsum(); | |
b32a9f1a | 1384 | komodo_longestchain(); |
a6cbaa25 | 1385 | lastinterest = komodo_chainactive_timestamp(); |
1386 | } | |
635dd34d | 1387 | refsp = komodo_stateptr(symbol,dest); |
ab918767 | 1388 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) |
eeaaf554 | 1389 | { |
67cd296f | 1390 | refid = 33; |
2934460c | 1391 | limit = 10000000; |
eeaaf554 | 1392 | jumblr_iteration(); |
1393 | } | |
b307682f | 1394 | else |
716fb382 | 1395 | { |
2934460c | 1396 | limit = 10000000; |
b307682f | 1397 | refid = komodo_baseid(ASSETCHAINS_SYMBOL)+1; // illegal base -> baseid.-1 -> 0 |
1398 | if ( refid == 0 ) | |
1399 | { | |
1400 | KOMODO_PASSPORT_INITDONE = 1; | |
1401 | return; | |
1402 | } | |
716fb382 | 1403 | } |
717dfab6 | 1404 | /*if ( KOMODO_PAX == 0 ) |
6cd01056 | 1405 | { |
1406 | KOMODO_PASSPORT_INITDONE = 1; | |
1407 | return; | |
717dfab6 | 1408 | }*/ |
ba37a6b9 | 1409 | starttime = (uint32_t)time(NULL); |
827756b6 | 1410 | if ( callcounter++ < 1 ) |
1411 | limit = 10000; | |
d9d08bc4 | 1412 | lasttime = starttime; |
959782f9 | 1413 | for (baseid=32; baseid>=0; baseid--) |
b2e010ee | 1414 | { |
e22dcc1e | 1415 | if ( time(NULL) >= starttime+maxseconds ) |
588bc897 | 1416 | break; |
ecf6256c | 1417 | sp = 0; |
1418 | isrealtime = 0; | |
f789edde | 1419 | base = (char *)CURRENCIES[baseid]; |
0cd5ab88 | 1420 | //printf("PASSPORT %s baseid+1 %d refid.%d\n",ASSETCHAINS_SYMBOL,baseid+1,refid); |
1421 | if ( baseid+1 != refid ) // only need to import state from a different coin | |
ab918767 | 1422 | { |
0cd5ab88 | 1423 | if ( baseid == 32 ) // only care about KMD's state |
ab918767 | 1424 | { |
70ad332d | 1425 | refsp->RTmask &= ~(1LL << baseid); |
b2e010ee | 1426 | komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"komodostate"); |
1427 | komodo_nameset(symbol,dest,base); | |
1428 | sp = komodo_stateptrget(symbol); | |
1429 | n = 0; | |
0cd5ab88 | 1430 | if ( lastpos[baseid] == 0 && (filedata= OS_fileptr(&datalen,fname)) != 0 ) |
17746e45 | 1431 | { |
1432 | fpos = 0; | |
0cd5ab88 | 1433 | fprintf(stderr,"%s processing %s %ldKB\n",ASSETCHAINS_SYMBOL,fname,datalen/1024); |
17746e45 | 1434 | while ( komodo_parsestatefiledata(sp,filedata,&fpos,datalen,symbol,dest) >= 0 ) |
1f14a516 | 1435 | lastfpos = fpos; |
0cd5ab88 | 1436 | fprintf(stderr,"%s took %d seconds to process %s %ldKB\n",ASSETCHAINS_SYMBOL,(int32_t)(time(NULL)-starttime),fname,datalen/1024); |
1f14a516 | 1437 | lastpos[baseid] = lastfpos; |
17746e45 | 1438 | free(filedata), filedata = 0; |
1439 | datalen = 0; | |
1440 | } | |
1441 | else if ( (fp= fopen(fname,"rb")) != 0 && sp != 0 ) | |
ab918767 | 1442 | { |
b2e010ee | 1443 | fseek(fp,0,SEEK_END); |
211f7541 | 1444 | //fprintf(stderr,"couldnt OS_fileptr(%s), freading %ldKB\n",fname,ftell(fp)/1024); |
b2e010ee | 1445 | if ( ftell(fp) > lastpos[baseid] ) |
ba37a6b9 | 1446 | { |
b028bc48 | 1447 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) |
b6d301be | 1448 | printf("%s passport refid.%d %s fname.(%s) base.%s %ld %ld\n",ASSETCHAINS_SYMBOL,refid,symbol,fname,base,ftell(fp),lastpos[baseid]); |
b2e010ee | 1449 | fseek(fp,lastpos[baseid],SEEK_SET); |
59d405e8 | 1450 | while ( komodo_parsestatefile(sp,fp,symbol,dest) >= 0 && n < limit ) |
ba37a6b9 | 1451 | { |
59d405e8 | 1452 | if ( n == limit-1 ) |
430d3c77 | 1453 | { |
b2e010ee | 1454 | if ( time(NULL) < starttime+maxseconds ) |
1455 | n = 0; | |
1456 | else | |
1457 | { | |
1458 | //printf("expire passport loop %s -> %s at %ld\n",ASSETCHAINS_SYMBOL,base,lastpos[baseid]); | |
1459 | expired++; | |
1460 | } | |
430d3c77 | 1461 | } |
b2e010ee | 1462 | n++; |
ba37a6b9 | 1463 | } |
b2e010ee | 1464 | lastpos[baseid] = ftell(fp); |
0cd5ab88 | 1465 | if ( 0 && lastpos[baseid] == 0 && strcmp(symbol,"KMD") == 0 ) |
b2e010ee | 1466 | printf("from.(%s) lastpos[%s] %ld isrt.%d\n",ASSETCHAINS_SYMBOL,CURRENCIES[baseid],lastpos[baseid],komodo_isrealtime(&ht)); |
1467 | } //else fprintf(stderr,"%s.%ld ",CURRENCIES[baseid],ftell(fp)); | |
1468 | fclose(fp); | |
13b0e935 | 1469 | } else fprintf(stderr,"load error.(%s) %p\n",fname,sp); |
b2e010ee | 1470 | komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime"); |
1471 | if ( (fp= fopen(fname,"rb")) != 0 ) | |
a7ef556c | 1472 | { |
b2e010ee | 1473 | if ( fread(buf,1,sizeof(buf),fp) == sizeof(buf) ) |
6eb41a2f | 1474 | { |
b2e010ee | 1475 | sp->CURRENT_HEIGHT = buf[0]; |
f636ecd2 | 1476 | if ( buf[0] != 0 && buf[0] >= buf[1] && buf[2] > time(NULL)-60 ) |
b2e010ee | 1477 | { |
1478 | isrealtime = 1; | |
1479 | RTmask |= (1LL << baseid); | |
1480 | memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1])); | |
f636ecd2 | 1481 | } |
1482 | else if ( KOMODO_PAX != 0 && (time(NULL)-buf[2]) > 60 && ASSETCHAINS_SYMBOL[0] != 0 ) | |
b2e010ee | 1483 | fprintf(stderr,"[%s]: %s not RT %u %u %d\n",ASSETCHAINS_SYMBOL,base,buf[0],buf[1],(int32_t)(time(NULL)-buf[2])); |
1484 | } //else fprintf(stderr,"%s size error RT\n",base); | |
1485 | fclose(fp); | |
1486 | } //else fprintf(stderr,"%s open error RT\n",base); | |
1487 | } | |
a7ef556c | 1488 | } |
6238a204 | 1489 | else |
a7ef556c | 1490 | { |
70ad332d | 1491 | refsp->RTmask &= ~(1LL << baseid); |
a7ef556c | 1492 | komodo_statefname(fname,baseid<32?base:(char *)"",(char *)"realtime"); |
1493 | if ( (fp= fopen(fname,"wb")) != 0 ) | |
1494 | { | |
86131275 | 1495 | buf[0] = (uint32_t)chainActive.LastTip()->nHeight; |
a7ef556c | 1496 | buf[1] = (uint32_t)komodo_longestchain(); |
1497 | if ( buf[0] != 0 && buf[0] == buf[1] ) | |
6eb41a2f | 1498 | { |
a7ef556c | 1499 | buf[2] = (uint32_t)time(NULL); |
582c6afe | 1500 | RTmask |= (1LL << baseid); |
635dd34d | 1501 | memcpy(refsp->RTbufs[baseid+1],buf,sizeof(refsp->RTbufs[baseid+1])); |
1502 | if ( refid != 0 ) | |
1503 | memcpy(refsp->RTbufs[0],buf,sizeof(refsp->RTbufs[0])); | |
6eb41a2f | 1504 | } |
a7ef556c | 1505 | if ( fwrite(buf,1,sizeof(buf),fp) != sizeof(buf) ) |
1506 | fprintf(stderr,"[%s] %s error writing realtime\n",ASSETCHAINS_SYMBOL,base); | |
1507 | fclose(fp); | |
f789edde | 1508 | } else fprintf(stderr,"%s create error RT\n",base); |
ecf6256c | 1509 | } |
f1db88ea | 1510 | if ( sp != 0 && isrealtime == 0 ) |
1511 | refsp->RTbufs[0][2] = 0; | |
52f1d788 | 1512 | } |
32885e74 | 1513 | //komodo_paxtotal(); // calls komodo_isrealtime(), which calls komodo_longestchain() |
70ad332d | 1514 | refsp->RTmask |= RTmask; |
430d3c77 | 1515 | if ( expired == 0 && KOMODO_PASSPORT_INITDONE == 0 ) |
1516 | { | |
1517 | KOMODO_PASSPORT_INITDONE = 1; | |
9bcc6321 | 1518 | printf("READY for %s RPC calls at %u! done PASSPORT %s refid.%d\n",ASSETCHAINS_SYMBOL,(uint32_t)time(NULL),ASSETCHAINS_SYMBOL,refid); |
430d3c77 | 1519 | } |
ab918767 | 1520 | } |
ab918767 | 1521 |