fcd36118 |
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 | |
16 | #ifndef H_KOMODO_H |
17 | #define H_KOMODO_H |
18 | |
3eea72f2 |
19 | // Todo: handle reorg: clear all entries above reorged height |
20 | |
9499e4de |
21 | #include <stdint.h> |
22 | #include <stdio.h> |
975b2ddb |
23 | #include <pthread.h> |
88f973c2 |
24 | #include <ctype.h> |
9499e4de |
25 | |
09cc09ab |
26 | void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals); |
58c94994 |
27 | void komodo_init(); |
d9d3a941 |
28 | int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *notarized_desttxidp); |
3c0f5d94 |
29 | char *komodo_issuemethod(char *method,char *params,uint16_t port); |
9499e4de |
30 | |
b62d7030 |
31 | int32_t NOTARIZED_HEIGHT,Num_nutxos; |
3b34b59c |
32 | uint256 NOTARIZED_HASH,NOTARIZED_DESTTXID; |
975b2ddb |
33 | pthread_mutex_t komodo_mutex; |
fd1799a6 |
34 | char USERPASS[1024]; uint16_t BITCOIND_PORT = 7771; |
d2471545 |
35 | |
46beb55e |
36 | #include "komodo_utils.h" |
8d6bd385 |
37 | #include "cJSON.c" |
a40be56e |
38 | #include "komodo_bitcoind.h" |
3eea72f2 |
39 | #include "komodo_interest.h" |
40 | #ifdef KOMODO_PAX |
41 | #include "komodo_pax.h" |
42 | #endif |
43 | #include "komodo_notary.h" |
88593976 |
44 | #include "komodo_gateway.h" |
9499e4de |
45 | |
88593976 |
46 | void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals) |
5cea7725 |
47 | { |
e9595d76 |
48 | static FILE *fp; static int32_t errs; char fname[512]; int32_t ht,func; uint8_t num,pubkeys[64][33]; |
e402ebee |
49 | #ifdef WIN32 |
50 | sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"komodostate"); |
51 | #else |
073594aa |
52 | sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),(char *)"komodostate"); |
e402ebee |
53 | #endif |
bafc61f3 |
54 | if ( fp == 0 ) |
55 | { |
56 | if ( (fp= fopen(fname,"rb+")) != 0 ) |
57 | { |
58 | while ( (func= fgetc(fp)) != EOF ) |
59 | { |
c50ec0fb |
60 | if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) ) |
61 | errs++; |
5203fc4b |
62 | //printf("func.(%d %c) ht.%d\n",func,func,ht); |
bafc61f3 |
63 | if ( func == 'P' ) |
64 | { |
65 | if ( (num= fgetc(fp)) < 64 ) |
dd7b22ad |
66 | { |
52ed4002 |
67 | if ( fread(pubkeys,33,num,fp) != num ) |
68 | errs++; |
732fdf87 |
69 | else |
70 | { |
5203fc4b |
71 | printf("updated %d pubkeys at ht.%d\n",num,ht); |
72 | komodo_notarysinit(ht,pubkeys,num); |
732fdf87 |
73 | } |
74 | } else printf("illegal num.%d\n",num); |
9997caa0 |
75 | //printf("P[%d]\n",num); |
bafc61f3 |
76 | } |
77 | else if ( func == 'N' ) |
78 | { |
52ed4002 |
79 | if ( fread(&NOTARIZED_HEIGHT,1,sizeof(NOTARIZED_HEIGHT),fp) != sizeof(NOTARIZED_HEIGHT) ) |
80 | errs++; |
81 | if ( fread(&NOTARIZED_HASH,1,sizeof(NOTARIZED_HASH),fp) != sizeof(NOTARIZED_HASH) ) |
82 | errs++; |
1e81ccb7 |
83 | if ( fread(&NOTARIZED_DESTTXID,1,sizeof(NOTARIZED_DESTTXID),fp) != sizeof(NOTARIZED_DESTTXID) ) |
52ed4002 |
84 | errs++; |
81bac2f0 |
85 | printf("load NOTARIZED %d %s\n",NOTARIZED_HEIGHT,NOTARIZED_HASH.ToString().c_str()); |
1e81ccb7 |
86 | komodo_notarized_update(ht,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID); |
bafc61f3 |
87 | } |
671187dd |
88 | else if ( func == 'U' ) |
89 | { |
90 | uint8_t n,nid; uint256 hash; uint64_t mask; |
91 | n = fgetc(fp); |
92 | nid = fgetc(fp); |
9997caa0 |
93 | //printf("U %d %d\n",n,nid); |
671187dd |
94 | if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) ) |
95 | errs++; |
96 | if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) ) |
97 | errs++; |
3eea72f2 |
98 | komodo_nutxoadd(ht,nid,hash,mask,n); |
4355e769 |
99 | } |
100 | else if ( func == 'D' ) |
101 | { |
9997caa0 |
102 | //printf("D[%d]\n",ht); |
671187dd |
103 | } |
1e81ccb7 |
104 | #ifdef KOMODO_PAX |
5cea7725 |
105 | else if ( func == 'V' ) |
106 | { |
107 | int32_t numpvals; uint32_t pvals[128]; |
108 | numpvals = fgetc(fp); |
4a41b0b2 |
109 | if ( numpvals*sizeof(uint32_t) <= sizeof(pvals) && fread(pvals,sizeof(uint32_t),numpvals,fp) == numpvals ) |
5cea7725 |
110 | { |
111 | komodo_pvals(ht,pvals,numpvals); |
7ad554ba |
112 | //printf("load pvals ht.%d numpvals.%d\n",ht,numpvals); |
4a41b0b2 |
113 | } else printf("error loading pvals[%d]\n",numpvals); |
5cea7725 |
114 | } |
1e81ccb7 |
115 | #endif |
bafc61f3 |
116 | else printf("illegal func.(%d %c)\n",func,func); |
117 | } |
118 | } else fp = fopen(fname,"wb+"); |
119 | printf("fname.(%s) fpos.%ld\n",fname,ftell(fp)); |
120 | } |
3eea72f2 |
121 | if ( fp != 0 ) // write out funcid, height, other fields, call side effect function |
bafc61f3 |
122 | { |
4355e769 |
123 | if ( height < 0 ) |
124 | { |
9997caa0 |
125 | height = -height; |
126 | //printf("func D[%d] errs.%d\n",height,errs); |
4355e769 |
127 | fputc('D',fp); |
128 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
129 | errs++; |
130 | } |
9997caa0 |
131 | else if ( notarypubs != 0 && numnotaries > 0 ) |
bafc61f3 |
132 | { |
9997caa0 |
133 | //printf("func P[%d] errs.%d\n",numnotaries,errs); |
bafc61f3 |
134 | fputc('P',fp); |
c50ec0fb |
135 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
136 | errs++; |
bafc61f3 |
137 | fputc(numnotaries,fp); |
52ed4002 |
138 | if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries ) |
139 | errs++; |
3eea72f2 |
140 | komodo_notarysinit(height,notarypubs,numnotaries); |
bafc61f3 |
141 | } |
671187dd |
142 | else if ( voutmask != 0 && numvouts > 0 ) |
143 | { |
9997caa0 |
144 | //printf("func U %d %d errs.%d hashsize.%ld\n",numvouts,notaryid,errs,sizeof(txhash)); |
671187dd |
145 | fputc('U',fp); |
c50ec0fb |
146 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
147 | errs++; |
671187dd |
148 | fputc(numvouts,fp); |
149 | fputc(notaryid,fp); |
150 | if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) ) |
151 | errs++; |
152 | if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) ) |
153 | errs++; |
3eea72f2 |
154 | komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts); |
671187dd |
155 | } |
1e81ccb7 |
156 | #ifdef KOMODO_PAX |
4a41b0b2 |
157 | else if ( pvals != 0 && numpvals > 0 ) |
158 | { |
159 | fputc('V',fp); |
160 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
161 | errs++; |
162 | fputc(numpvals,fp); |
163 | if ( fwrite(pvals,sizeof(uint32_t),numpvals,fp) != numpvals ) |
164 | errs++; |
165 | komodo_pvals(height,pvals,numpvals); |
ef7a3548 |
166 | //printf("save pvals height.%d numpvals.%d\n",height,numpvals); |
4a41b0b2 |
167 | } |
1e81ccb7 |
168 | #endif |
6dabcca4 |
169 | else if ( height != 0 ) |
671187dd |
170 | { |
9997caa0 |
171 | //printf("func N ht.%d errs.%d\n",NOTARIZED_HEIGHT,errs); |
671187dd |
172 | fputc('N',fp); |
c50ec0fb |
173 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
174 | errs++; |
671187dd |
175 | if ( fwrite(&NOTARIZED_HEIGHT,1,sizeof(NOTARIZED_HEIGHT),fp) != sizeof(NOTARIZED_HEIGHT) ) |
176 | errs++; |
177 | if ( fwrite(&NOTARIZED_HASH,1,sizeof(NOTARIZED_HASH),fp) != sizeof(NOTARIZED_HASH) ) |
178 | errs++; |
1e81ccb7 |
179 | if ( fwrite(&NOTARIZED_DESTTXID,1,sizeof(NOTARIZED_DESTTXID),fp) != sizeof(NOTARIZED_DESTTXID) ) |
671187dd |
180 | errs++; |
1e81ccb7 |
181 | komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_DESTTXID); |
671187dd |
182 | } |
47f47733 |
183 | fflush(fp); |
bafc61f3 |
184 | } |
185 | } |
186 | |
1390456b |
187 | int32_t komodo_voutupdate(int32_t notaryid,uint8_t *scriptbuf,int32_t scriptlen,int32_t height,uint256 txhash,int32_t i,int32_t j,uint64_t *voutmaskp,int32_t *specialtxp,int32_t *notarizedheightp) |
188 | { |
0ac1f6fc |
189 | static uint256 zero; int32_t opretlen,nid,k,len = 0; uint256 kmdtxid,desttxid; uint8_t crypto777[33]; |
9c406099 |
190 | if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) |
1390456b |
191 | { |
c23dd601 |
192 | decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR); |
986d3459 |
193 | /*for (k=0; k<33; k++) |
e1be360e |
194 | printf("%02x",crypto777[k]); |
195 | printf(" crypto777 "); |
196 | for (k=0; k<scriptlen; k++) |
197 | printf("%02x",scriptbuf[k]); |
986d3459 |
198 | printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/ |
1390456b |
199 | if ( memcmp(crypto777,scriptbuf+1,33) == 0 ) |
200 | { |
201 | *specialtxp = 1; |
202 | printf(">>>>>>>> "); |
203 | } |
a5f315c7 |
204 | else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 ) |
1390456b |
205 | { |
84e843cd |
206 | //printf("found notary.k%d\n",k); |
207 | if ( notaryid < 64 ) |
1390456b |
208 | { |
84e843cd |
209 | if ( notaryid < 0 ) |
210 | { |
a5f315c7 |
211 | notaryid = nid; |
84e843cd |
212 | *voutmaskp |= (1LL << j); |
213 | } |
a5f315c7 |
214 | else if ( notaryid != nid ) |
84e843cd |
215 | { |
a5f315c7 |
216 | printf("mismatch notaryid.%d k.%d\n",notaryid,nid); |
84e843cd |
217 | notaryid = 64; |
218 | *voutmaskp = 0; |
219 | } |
220 | else *voutmaskp |= (1LL << j); |
1390456b |
221 | } |
1390456b |
222 | } |
223 | } |
224 | if ( j == 1 && scriptbuf[len++] == 0x6a ) |
225 | { |
226 | if ( (opretlen= scriptbuf[len++]) == 0x4c ) |
227 | opretlen = scriptbuf[len++]; |
228 | else if ( opretlen == 0x4d ) |
229 | { |
230 | opretlen = scriptbuf[len++]; |
231 | opretlen = (opretlen << 8) + scriptbuf[len++]; |
232 | } |
3eea72f2 |
233 | if ( opretlen >= 32*2+4 && strcmp(KOMODO_SOURCE,(char *)&scriptbuf[len+32*2+4]) == 0 ) |
1390456b |
234 | { |
4a41b0b2 |
235 | len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid); |
236 | len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp); |
3eea72f2 |
237 | len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid); |
484f8777 |
238 | for (k=0; k<scriptlen; k++) |
239 | printf("%02x",scriptbuf[k]); |
240 | printf(" <- script ht.%d i.%d j.%d\n",height,i,j); |
bc1d085a |
241 | if ( *notarizedheightp > NOTARIZED_HEIGHT && *notarizedheightp < height ) |
1390456b |
242 | { |
f9d24468 |
243 | printf("ht.%d NOTARIZED.%d KMD.%s BTCTXID.%s (%s)\n",height,*notarizedheightp,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]); |
4a41b0b2 |
244 | NOTARIZED_HEIGHT = *notarizedheightp; |
245 | NOTARIZED_HASH = kmdtxid; |
1e81ccb7 |
246 | NOTARIZED_DESTTXID = desttxid; |
4a41b0b2 |
247 | komodo_stateupdate(height,0,0,0,zero,0,0,0,0); |
f9d24468 |
248 | } else printf("reject ht.%d NOTARIZED.%d %s.%s DESTTXID.%s (%s)\n",height,*notarizedheightp,KOMODO_SOURCE,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]); |
1390456b |
249 | } |
1e81ccb7 |
250 | #ifdef KOMODO_PAX |
4a41b0b2 |
251 | else if ( i == 0 && scriptbuf[len] == 'P' ) |
252 | { |
253 | double KMDBTC,BTCUSD,CNYUSD; uint32_t numpvals,timestamp,pvals[128]; |
254 | numpvals = dpow_readprices(&scriptbuf[++len],×tamp,&KMDBTC,&BTCUSD,&CNYUSD,pvals); |
255 | komodo_stateupdate(height,0,0,0,zero,0,0,pvals,numpvals); |
484f8777 |
256 | printf("vout OP_RETURN.%d prices numpvals.%d opretlen.%d\n",height,numpvals,opretlen); |
4a41b0b2 |
257 | } |
1e81ccb7 |
258 | #endif |
1390456b |
259 | } |
260 | return(notaryid); |
261 | } |
262 | |
651989c7 |
263 | void komodo_connectblock(CBlockIndex *pindex,CBlock& block) |
50027f06 |
264 | { |
e9595d76 |
265 | uint64_t signedmask,voutmask; |
dd7b22ad |
266 | uint8_t scriptbuf[4096],pubkeys[64][33]; uint256 kmdtxid,btctxid,txhash; |
e9595d76 |
267 | int32_t i,j,k,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count; |
77117dbe |
268 | komodo_init(); |
9b0e1808 |
269 | #ifdef KOMODO_ISSUER |
270 | komodo_gateway_issuer(); |
271 | #else |
272 | #endif |
68916cc6 |
273 | if ( pindex != 0 ) |
b501ded2 |
274 | { |
656eddcd |
275 | height = pindex->nHeight; |
01cc012f |
276 | txn_count = block.vtx.size(); |
277 | for (i=0; i<txn_count; i++) |
dc64de68 |
278 | { |
352f8081 |
279 | txhash = block.vtx[i].GetHash(); |
01cc012f |
280 | numvouts = block.vtx[i].vout.size(); |
352f8081 |
281 | notaryid = -1; |
1390456b |
282 | voutmask = specialtx = notarizedheight = 0; |
01cc012f |
283 | for (j=0; j<numvouts; j++) |
dc64de68 |
284 | { |
1390456b |
285 | len = block.vtx[i].vout[j].scriptPubKey.size(); |
286 | if ( len <= sizeof(scriptbuf) ) |
01cc012f |
287 | { |
189d9dee |
288 | #ifdef KOMODO_ZCASH |
484f8777 |
289 | memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len); |
189d9dee |
290 | #else |
291 | memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len); |
292 | #endif |
acb3f1d8 |
293 | notaryid = komodo_voutupdate(notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,¬arizedheight); |
a01acef4 |
294 | if ( 0 && i > 0 ) |
e69a0833 |
295 | { |
296 | for (k=0; k<len; k++) |
297 | printf("%02x",scriptbuf[k]); |
a8832194 |
298 | printf(" <- notaryid.%d ht.%d i.%d j.%d numvouts.%d numvins.%d voutmask.%llx txid.(%s)\n",notaryid,height,i,j,numvouts,numvins,(long long)voutmask,txhash.ToString().c_str()); |
e69a0833 |
299 | } |
01cc012f |
300 | } |
352f8081 |
301 | } |
cf9538e0 |
302 | if ( i != 0 && notaryid >= 0 && notaryid < 64 && voutmask != 0 ) |
3eea72f2 |
303 | { |
304 | komodo_stateupdate(height,0,0,notaryid,txhash,voutmask,numvouts,0,0); |
305 | //komodo_nutxoadd(height,notaryid,txhash,voutmask,numvouts); |
306 | } |
1390456b |
307 | signedmask = 0; |
308 | numvins = block.vtx[i].vin.size(); |
5bdebffe |
309 | for (j=0; j<numvins; j++) |
352f8081 |
310 | { |
4355e769 |
311 | if ( (k= komodo_nutxofind(height,block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) >= 0 ) |
1390456b |
312 | signedmask |= (1LL << k); |
21cc1d3e |
313 | else if ( 0 && signedmask != 0 ) |
bc6fd011 |
314 | printf("signedmask.%llx but ht.%d i.%d j.%d not found (%s %d)\n",(long long)signedmask,height,i,j,block.vtx[i].vin[j].prevout.hash.ToString().c_str(),block.vtx[i].vin[j].prevout.n); |
1390456b |
315 | } |
06f0ed43 |
316 | if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) ) |
1390456b |
317 | { |
e69a0833 |
318 | printf("NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",(long long)signedmask,numvins,height,i,notarizedheight,specialtx); |
3eea72f2 |
319 | if ( specialtx != 0 && numvouts > 2 && komodo_ratify_threshold(height,signedmask) > 0 ) |
84e843cd |
320 | { |
29e69b85 |
321 | numvalid = 0; |
dd7b22ad |
322 | memset(pubkeys,0,sizeof(pubkeys)); |
84e843cd |
323 | for (j=1; j<numvouts; j++) |
324 | { |
325 | len = block.vtx[i].vout[j].scriptPubKey.size(); |
326 | if ( len <= sizeof(scriptbuf) ) |
327 | { |
189d9dee |
328 | #ifdef KOMODO_ZCASH |
484f8777 |
329 | memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len); |
189d9dee |
330 | #else |
331 | memcpy(scriptbuf,(uint8_t *)&block.vtx[i].vout[j].scriptPubKey[0],len); |
332 | #endif |
84e843cd |
333 | if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) |
334 | { |
29e69b85 |
335 | memcpy(pubkeys[numvalid++],scriptbuf+1,33); |
e7f99312 |
336 | for (k=0; k<33; k++) |
84e843cd |
337 | printf("%02x",scriptbuf[k+1]); |
338 | printf(" <- new notary.[%d]\n",j-1); |
339 | } |
340 | } |
341 | } |
29e69b85 |
342 | if ( numvalid > 13 ) |
d8ce705a |
343 | { |
344 | memset(&txhash,0,sizeof(txhash)); |
5cea7725 |
345 | komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0,0,0); |
d8ce705a |
346 | } |
835c617e |
347 | printf("new notaries.%d newheight.%d from height.%d\n",numvouts-1,KOMODO_PUBKEYS_HEIGHT(height),height); |
84e843cd |
348 | } |
a96439f5 |
349 | } |
656eddcd |
350 | } |
44c4fbbd |
351 | } else printf("komodo_connectblock: unexpected null pindex\n"); |
3d35aa5b |
352 | } |
353 | |
0f24f245 |
354 | |
fcd36118 |
355 | #endif |