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