]>
Commit | Line | Data |
---|---|---|
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 | ||
9499e4de | 19 | #include <stdint.h> |
20 | #include <stdio.h> | |
975b2ddb | 21 | #include <pthread.h> |
d2471545 | 22 | #include "uthash.h" |
9499e4de | 23 | |
6ef202b2 | 24 | #include "komodo_interest.h" |
25 | ||
9499e4de | 26 | #define KOMODO_TESTNET_EXPIRATION 60000 |
732fdf87 | 27 | #define KOMODO_ELECTION_GAP 1000 |
926d837c | 28 | #define KOMODO_PUBKEYS_HEIGHT(height) ((int32_t)(((((height)+KOMODO_ELECTION_GAP*.5)/KOMODO_ELECTION_GAP) + 1) * KOMODO_ELECTION_GAP)) |
9499e4de | 29 | |
24778ada | 30 | int32_t IS_KOMODO_NOTARY,USE_EXTERNAL_PUBKEY,NOTARIZED_HEIGHT,Num_nutxos,KOMODO_NUMNOTARIES = 64; |
9499e4de | 31 | std::string NOTARY_PUBKEY; |
32 | uint8_t NOTARY_PUBKEY33[33]; | |
5203fc4b | 33 | uint256 NOTARIZED_HASH,NOTARIZED_BTCTXID; |
975b2ddb | 34 | pthread_mutex_t komodo_mutex; |
d2471545 | 35 | |
732fdf87 | 36 | struct nutxo_entry { UT_hash_handle hh; uint256 txhash; uint64_t voutmask; int32_t notaryid,height; } *NUTXOS; |
37 | struct knotary_entry { UT_hash_handle hh; uint8_t pubkey[33],notaryid; }; | |
38 | struct knotaries_entry { int32_t height,numnotaries; struct knotary_entry *Notaries; } Pubkeys[10000]; | |
5203fc4b | 39 | struct notarized_checkpoint { uint256 notarized_hash,notarized_btctxid; int32_t nHeight,notarized_height; } *NPOINTS; int32_t NUM_NPOINTS; |
9499e4de | 40 | |
926d837c | 41 | int32_t komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts); |
9499e4de | 42 | // add opreturn funcid |
43 | // pricefeeds | |
44 | ||
9499e4de | 45 | #define CRYPTO777_PUBSECPSTR "020e46e79a2a8d12b9b5d12c7a91adb4e454edfae43c0a0cb805427d2ac7613fd9" |
46 | ||
13023121 | 47 | const char *Notaries[][2] = |
9499e4de | 48 | { |
49 | { "jl777_testA", "03b7621b44118017a16043f19b30cc8a4cfe068ac4e42417bae16ba460c80f3828" }, | |
50 | { "jl777_testB", "02ebfc784a4ba768aad88d44d1045d240d47b26e248cafaf1c5169a42d7a61d344" }, | |
51 | { "pondsea_SH", "02209073bc0943451498de57f802650311b1f12aa6deffcd893da198a544c04f36" }, | |
52 | { "crackers_EU", "0340c66cf2c41c41efb420af57867baa765e8468c12aa996bfd816e1e07e410728" }, | |
53 | { "pondsea_EU", "0225aa6f6f19e543180b31153d9e6d55d41bc7ec2ba191fd29f19a2f973544e29d" }, | |
54 | { "locomb_EU", "025c6d26649b9d397e63323d96db42a9d3caad82e1d6076970efe5056c00c0779b" }, | |
55 | { "fullmoon_AE", "0204a908350b8142698fdb6fabefc97fe0e04f537adc7522ba7a1e8f3bec003d4a" }, | |
56 | { "movecrypto_EU", "021ab53bc6cf2c46b8a5456759f9d608966eff87384c2b52c0ac4cc8dd51e9cc42" }, | |
57 | { "badass_EU", "0209d48554768dd8dada988b98aca23405057ac4b5b46838a9378b95c3e79b9b9e" }, | |
58 | { "crackers_NA", "029e1c01131974f4cd3f564cc0c00eb87a0f9721043fbc1ca60f9bd0a1f73f64a1" }, | |
59 | { "proto_EU", "03681ffdf17c8f4f0008cefb7fa0779c5e888339cdf932f0974483787a4d6747c1" }, | |
60 | { "jeezy_EU", "023cb3e593fb85c5659688528e9a4f1c4c7f19206edc7e517d20f794ba686fd6d6" }, | |
61 | { "farl4web_EU", "035caa40684ace968677dca3f09098aa02b70e533da32390a7654c626e0cf908e1" }, | |
62 | { "nxtswe_EU", "032fb104e5eaa704a38a52c126af8f67e870d70f82977e5b2f093d5c1c21ae5899" }, | |
63 | { "traderbill_EU", "03196e8de3e2e5d872f31d79d6a859c8704a2198baf0af9c7b21e29656a7eb455f" }, | |
64 | { "vanbreuk_EU", "024f3cad7601d2399c131fd070e797d9cd8533868685ddbe515daa53c2e26004c3" }, | |
65 | { "titomane_EU", "03517fcac101fed480ae4f2caf775560065957930d8c1facc83e30077e45bdd199" }, | |
66 | { "supernet_AE", "029d93ef78197dc93892d2a30e5a54865f41e0ca3ab7eb8e3dcbc59c8756b6e355" }, | |
67 | { "supernet_EU", "02061c6278b91fd4ac5cab4401100ffa3b2d5a277e8f71db23401cc071b3665546" }, | |
68 | { "supernet_NA", "033c073366152b6b01535e15dd966a3a8039169584d06e27d92a69889b720d44e1" }, | |
69 | { "yassin_EU", "033fb7231bb66484081952890d9a03f91164fb27d392d9152ec41336b71b15fbd0" }, | |
70 | { "durerus_EU", "02bcbd287670bdca2c31e5d50130adb5dea1b53198f18abeec7211825f47485d57" }, | |
71 | { "badass_SH", "026b49dd3923b78a592c1b475f208e23698d3f085c4c3b4906a59faf659fd9530b" }, | |
72 | { "badass_NA", "02afa1a9f948e1634a29dc718d218e9d150c531cfa852843a1643a02184a63c1a7" }, | |
73 | { "pondsea_NA", "031bcfdbb62268e2ff8dfffeb9ddff7fe95fca46778c77eebff9c3829dfa1bb411" }, | |
74 | { "rnr_EU", "0287aa4b73988ba26cf6565d815786caf0d2c4af704d7883d163ee89cd9977edec" }, | |
75 | { "crackers_SH", "02313d72f9a16055737e14cfc528dcd5d0ef094cfce23d0348fe974b6b1a32e5f0" }, | |
76 | { "grewal_SH", "03212a73f5d38a675ee3cdc6e82542a96c38c3d1c79d25a1ed2e42fcf6a8be4e68" }, | |
77 | { "polycryptoblock_NA", "02708dcda7c45fb54b78469673c2587bfdd126e381654819c4c23df0e00b679622" }, | |
78 | { "titomane_NA", "0387046d9745414fb58a0fa3599078af5073e10347e4657ef7259a99cb4f10ad47" }, | |
79 | { "titomane_AE", "03cda6ca5c2d02db201488a54a548dbfc10533bdc275d5ea11928e8d6ab33c2185" }, | |
80 | { "kolo_EU", "03f5c08dadffa0ffcafb8dd7ffc38c22887bd02702a6c9ac3440deddcf2837692b" }, | |
81 | { "artik_NA", "0224e31f93eff0cc30eaf0b2389fbc591085c0e122c4d11862c1729d090106c842" }, | |
82 | { "eclips_EU", "0339369c1f5a2028d44be7be6f8ec3b907fdec814f87d2dead97cab4edb71a42e9" }, | |
d383827f | 83 | { "titomane_SH", "035f49d7a308dd9a209e894321f010d21b7793461b0c89d6d9231a3fe5f68d9960" }, |
9499e4de | 84 | }; |
85 | ||
5203fc4b | 86 | |
01cc012f | 87 | int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp) |
88 | { | |
89 | int32_t i; uint64_t x; | |
90 | if ( rwflag == 0 ) | |
91 | { | |
92 | x = 0; | |
93 | for (i=len-1; i>=0; i--) | |
94 | { | |
95 | x <<= 8; | |
96 | x |= serialized[i]; | |
97 | } | |
98 | switch ( len ) | |
99 | { | |
100 | case 1: *(uint8_t *)endianedp = (uint8_t)x; break; | |
101 | case 2: *(uint16_t *)endianedp = (uint16_t)x; break; | |
102 | case 4: *(uint32_t *)endianedp = (uint32_t)x; break; | |
103 | case 8: *(uint64_t *)endianedp = (uint64_t)x; break; | |
104 | } | |
105 | } | |
106 | else | |
107 | { | |
108 | x = 0; | |
109 | switch ( len ) | |
110 | { | |
111 | case 1: x = *(uint8_t *)endianedp; break; | |
112 | case 2: x = *(uint16_t *)endianedp; break; | |
113 | case 4: x = *(uint32_t *)endianedp; break; | |
114 | case 8: x = *(uint64_t *)endianedp; break; | |
115 | } | |
116 | for (i=0; i<len; i++,x >>= 8) | |
117 | serialized[i] = (uint8_t)(x & 0xff); | |
118 | } | |
119 | return(len); | |
120 | } | |
121 | ||
122 | int32_t iguana_rwbignum(int32_t rwflag,uint8_t *serialized,int32_t len,uint8_t *endianedp) | |
123 | { | |
124 | int32_t i; | |
125 | if ( rwflag == 0 ) | |
126 | { | |
127 | for (i=0; i<len; i++) | |
53b58ced | 128 | endianedp[i] = serialized[i]; |
01cc012f | 129 | } |
130 | else | |
131 | { | |
132 | for (i=0; i<len; i++) | |
53b58ced | 133 | serialized[i] = endianedp[i]; |
01cc012f | 134 | } |
135 | return(len); | |
136 | } | |
137 | ||
138 | int32_t _unhex(char c) | |
139 | { | |
140 | if ( c >= '0' && c <= '9' ) | |
141 | return(c - '0'); | |
142 | else if ( c >= 'a' && c <= 'f' ) | |
143 | return(c - 'a' + 10); | |
144 | else if ( c >= 'A' && c <= 'F' ) | |
145 | return(c - 'A' + 10); | |
146 | return(-1); | |
147 | } | |
148 | ||
149 | int32_t is_hexstr(char *str,int32_t n) | |
150 | { | |
151 | int32_t i; | |
152 | if ( str == 0 || str[0] == 0 ) | |
153 | return(0); | |
154 | for (i=0; str[i]!=0; i++) | |
155 | { | |
156 | if ( n > 0 && i >= n ) | |
157 | break; | |
158 | if ( _unhex(str[i]) < 0 ) | |
159 | break; | |
160 | } | |
161 | if ( n == 0 ) | |
162 | return(i); | |
163 | return(i == n); | |
164 | } | |
165 | ||
166 | int32_t unhex(char c) | |
167 | { | |
168 | int32_t hex; | |
169 | if ( (hex= _unhex(c)) < 0 ) | |
170 | { | |
171 | //printf("unhex: illegal hexchar.(%c)\n",c); | |
172 | } | |
173 | return(hex); | |
174 | } | |
175 | ||
176 | unsigned char _decode_hex(char *hex) { return((unhex(hex[0])<<4) | unhex(hex[1])); } | |
177 | ||
178 | int32_t decode_hex(uint8_t *bytes,int32_t n,char *hex) | |
179 | { | |
180 | int32_t adjust,i = 0; | |
181 | //printf("decode.(%s)\n",hex); | |
182 | if ( is_hexstr(hex,n) == 0 ) | |
183 | { | |
184 | memset(bytes,0,n); | |
185 | return(n); | |
186 | } | |
187 | if ( n == 0 || (hex[n*2+1] == 0 && hex[n*2] != 0) ) | |
188 | { | |
189 | if ( n > 0 ) | |
190 | { | |
191 | bytes[0] = unhex(hex[0]); | |
192 | printf("decode_hex n.%d hex[0] (%c) -> %d hex.(%s) [n*2+1: %d] [n*2: %d %c] len.%ld\n",n,hex[0],bytes[0],hex,hex[n*2+1],hex[n*2],hex[n*2],(long)strlen(hex)); | |
193 | } | |
194 | bytes++; | |
195 | hex++; | |
196 | adjust = 1; | |
197 | } else adjust = 0; | |
198 | if ( n > 0 ) | |
199 | { | |
200 | for (i=0; i<n; i++) | |
201 | bytes[i] = _decode_hex(&hex[i*2]); | |
202 | } | |
203 | //bytes[i] = 0; | |
204 | return(n + adjust); | |
205 | } | |
206 | ||
bbe9aa6f | 207 | int32_t komodo_threshold(int32_t height,uint64_t signedmask) |
208 | { | |
209 | int32_t numnotaries,i,wt = 0; | |
210 | numnotaries = Pubkeys[height / KOMODO_ELECTION_GAP].numnotaries; | |
211 | for (i=0; i<numnotaries; i++) | |
212 | if ( ((1LL << i) & signedmask) != 0 ) | |
213 | wt++; | |
b927fb35 | 214 | if ( wt > (numnotaries >> 1) || (wt > 7 && (signedmask & 3) != 0) ) |
bbe9aa6f | 215 | return(1); // N/2+1 || N/3 + devsig |
216 | else return(0); | |
217 | } | |
218 | ||
b927fb35 | 219 | uint32_t komodo_txtime(uint256 hash) |
220 | { | |
221 | CTransaction tx; | |
222 | uint256 hashBlock; | |
223 | if (!GetTransaction(hash, tx, hashBlock, true)) | |
224 | { | |
225 | //printf("null GetTransaction\n"); | |
226 | return(tx.nLockTime); | |
227 | } | |
b927fb35 | 228 | return(0); |
229 | } | |
230 | ||
732fdf87 | 231 | void komodo_nutxoadd(int32_t addflag,int32_t height,int32_t notaryid,uint256 txhash,uint64_t voutmask,int32_t numvouts) |
232 | { | |
233 | struct nutxo_entry *np; | |
234 | if ( numvouts > 1 && notaryid < 64 ) // change to ADD_HASH() and file based | |
235 | { | |
975b2ddb | 236 | pthread_mutex_lock(&komodo_mutex); |
732fdf87 | 237 | np = (struct nutxo_entry *)calloc(1,sizeof(*np)); |
238 | np->height = height; | |
239 | np->txhash = txhash; | |
240 | np->voutmask = voutmask; | |
241 | np->notaryid = notaryid; | |
242 | HASH_ADD_KEYPTR(hh,NUTXOS,&np->txhash,sizeof(np->txhash),np); | |
dcb546ff | 243 | printf("Add NUTXO[%d] <- %s notaryid.%d t%u %s %llx\n",Num_nutxos,Notaries[notaryid][0],notaryid,komodo_txtime(txhash),txhash.ToString().c_str(),(long long)voutmask); |
732fdf87 | 244 | if ( addflag != 0 ) |
245 | komodo_stateupdate(height,0,0,notaryid,txhash,voutmask,numvouts); | |
246 | Num_nutxos++; | |
975b2ddb | 247 | pthread_mutex_unlock(&komodo_mutex); |
732fdf87 | 248 | } |
249 | } | |
250 | ||
251 | int32_t komodo_nutxofind(int32_t height,uint256 txhash,int32_t vout) // change to HASH_FIND() | |
252 | { | |
253 | struct nutxo_entry *np; | |
975b2ddb | 254 | pthread_mutex_lock(&komodo_mutex); |
732fdf87 | 255 | HASH_FIND(hh,NUTXOS,&txhash,sizeof(txhash),np); |
975b2ddb | 256 | pthread_mutex_unlock(&komodo_mutex); |
732fdf87 | 257 | if ( np != 0 && ((1LL << vout) & np->voutmask) != 0 ) |
258 | return(np->notaryid); | |
259 | return(-1); | |
260 | } | |
261 | ||
262 | void komodo_notarysinit(int32_t height,uint8_t pubkeys[64][33],int32_t num) | |
263 | { | |
264 | int32_t k,i,htind; struct knotary_entry *kp; struct knotaries_entry N; | |
265 | memset(&N,0,sizeof(N)); | |
975b2ddb | 266 | pthread_mutex_lock(&komodo_mutex); |
732fdf87 | 267 | for (k=0; k<num; k++) |
268 | { | |
926d837c | 269 | kp = (struct knotary_entry *)calloc(1,sizeof(*kp)); |
732fdf87 | 270 | memcpy(kp->pubkey,pubkeys[k],33); |
271 | kp->notaryid = k; | |
272 | HASH_ADD_KEYPTR(hh,N.Notaries,kp->pubkey,33,kp); | |
273 | for (i=0; i<33; i++) | |
274 | printf("%02x",pubkeys[k][i]); | |
275 | printf(" notarypubs.[%d]\n",k); | |
276 | } | |
277 | N.numnotaries = num; | |
278 | htind = KOMODO_PUBKEYS_HEIGHT(height) / KOMODO_ELECTION_GAP; | |
279 | if ( htind == 1 ) | |
280 | htind = 0; | |
281 | for (i=htind; i<sizeof(Pubkeys)/sizeof(*Pubkeys); i++) | |
282 | { | |
283 | Pubkeys[i] = N; | |
284 | Pubkeys[i].height = i * KOMODO_ELECTION_GAP; | |
285 | } | |
975b2ddb | 286 | pthread_mutex_unlock(&komodo_mutex); |
732fdf87 | 287 | } |
288 | ||
a5f315c7 | 289 | int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33) |
732fdf87 | 290 | { |
291 | // -1 if not notary, 0 if notary, 1 if special notary | |
dae9e3bb | 292 | struct knotary_entry *kp; int32_t numnotaries,modval = -1; |
293 | *notaryidp = -1; | |
975b2ddb | 294 | pthread_mutex_lock(&komodo_mutex); |
732fdf87 | 295 | HASH_FIND(hh,Pubkeys[height/KOMODO_ELECTION_GAP].Notaries,pubkey33,33,kp); |
975b2ddb | 296 | pthread_mutex_unlock(&komodo_mutex); |
732fdf87 | 297 | if ( kp != 0 ) |
298 | { | |
299 | if ( (numnotaries= Pubkeys[height/KOMODO_ELECTION_GAP].numnotaries) > 0 ) | |
300 | { | |
dae9e3bb | 301 | *notaryidp = kp->notaryid; |
732fdf87 | 302 | modval = ((height % numnotaries) == kp->notaryid); |
0ed83449 | 303 | //printf("found notary.%d ht.%d modval.%d\n",kp->notaryid,height,modval); |
732fdf87 | 304 | } else printf("unexpected zero notaries at height.%d\n",height); |
305 | } | |
bbe9aa6f | 306 | //int32_t i; for (i=0; i<33; i++) |
307 | // printf("%02x",pubkey33[i]); | |
308 | //printf(" ht.%d notary.%d special.%d\n",height,*notaryidp,modval); | |
732fdf87 | 309 | return(modval); |
310 | } | |
6f3bcbea | 311 | |
5203fc4b | 312 | void komodo_notarized_update(int32_t nHeight,int32_t notarized_height,uint256 notarized_hash,uint256 notarized_btctxid) |
313 | { | |
314 | struct notarized_checkpoint *np; | |
f856c809 | 315 | NPOINTS = (struct notarized_checkpoint *)realloc(NPOINTS,(NUM_NPOINTS+1) * sizeof(*NPOINTS)); |
5203fc4b | 316 | np = &NPOINTS[NUM_NPOINTS++]; |
317 | memset(np,0,sizeof(*np)); | |
318 | np->nHeight = nHeight; | |
319 | np->notarized_height = notarized_height; | |
320 | np->notarized_hash = notarized_hash; | |
321 | np->notarized_btctxid = notarized_btctxid; | |
322 | } | |
323 | ||
324 | int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *notarized_btctxidp) | |
de36931b | 325 | { |
5203fc4b | 326 | struct notarized_checkpoint *np = 0; int32_t i; |
327 | if ( NUM_NPOINTS > 0 ) | |
328 | { | |
329 | for (i=0; i<NUM_NPOINTS; i++) | |
330 | { | |
331 | if ( NPOINTS[i].nHeight >= nHeight ) | |
332 | break; | |
333 | np = &NPOINTS[i]; | |
334 | } | |
335 | } | |
336 | if ( np != 0 ) | |
337 | { | |
338 | *notarized_hashp = np->notarized_hash; | |
339 | *notarized_btctxidp = np->notarized_btctxid; | |
340 | return(np->notarized_height); | |
341 | } | |
de36931b | 342 | memset(notarized_hashp,0,sizeof(*notarized_hashp)); |
343 | return(-1); | |
344 | } | |
345 | ||
4355e769 | 346 | int32_t komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts) |
bafc61f3 | 347 | { |
732fdf87 | 348 | static FILE *fp; static int32_t errs,didinit; char fname[512]; int32_t ht,k,func; uint8_t num,pubkeys[64][33]; |
e402ebee | 349 | #ifdef WIN32 |
350 | sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"komodostate"); | |
351 | #else | |
073594aa | 352 | sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),(char *)"komodostate"); |
e402ebee | 353 | #endif |
bafc61f3 | 354 | if ( fp == 0 ) |
355 | { | |
356 | if ( (fp= fopen(fname,"rb+")) != 0 ) | |
357 | { | |
358 | while ( (func= fgetc(fp)) != EOF ) | |
359 | { | |
c50ec0fb | 360 | if ( fread(&ht,1,sizeof(ht),fp) != sizeof(ht) ) |
361 | errs++; | |
5203fc4b | 362 | //printf("func.(%d %c) ht.%d\n",func,func,ht); |
bafc61f3 | 363 | if ( func == 'P' ) |
364 | { | |
365 | if ( (num= fgetc(fp)) < 64 ) | |
dd7b22ad | 366 | { |
52ed4002 | 367 | if ( fread(pubkeys,33,num,fp) != num ) |
368 | errs++; | |
732fdf87 | 369 | else |
370 | { | |
5203fc4b | 371 | printf("updated %d pubkeys at ht.%d\n",num,ht); |
372 | komodo_notarysinit(ht,pubkeys,num); | |
732fdf87 | 373 | } |
374 | } else printf("illegal num.%d\n",num); | |
9997caa0 | 375 | //printf("P[%d]\n",num); |
bafc61f3 | 376 | } |
377 | else if ( func == 'N' ) | |
378 | { | |
52ed4002 | 379 | if ( fread(&NOTARIZED_HEIGHT,1,sizeof(NOTARIZED_HEIGHT),fp) != sizeof(NOTARIZED_HEIGHT) ) |
380 | errs++; | |
381 | if ( fread(&NOTARIZED_HASH,1,sizeof(NOTARIZED_HASH),fp) != sizeof(NOTARIZED_HASH) ) | |
382 | errs++; | |
5203fc4b | 383 | if ( fread(&NOTARIZED_BTCTXID,1,sizeof(NOTARIZED_BTCTXID),fp) != sizeof(NOTARIZED_BTCTXID) ) |
52ed4002 | 384 | errs++; |
81bac2f0 | 385 | printf("load NOTARIZED %d %s\n",NOTARIZED_HEIGHT,NOTARIZED_HASH.ToString().c_str()); |
5203fc4b | 386 | komodo_notarized_update(ht,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_BTCTXID); |
bafc61f3 | 387 | } |
671187dd | 388 | else if ( func == 'U' ) |
389 | { | |
390 | uint8_t n,nid; uint256 hash; uint64_t mask; | |
391 | n = fgetc(fp); | |
392 | nid = fgetc(fp); | |
9997caa0 | 393 | //printf("U %d %d\n",n,nid); |
671187dd | 394 | if ( fread(&mask,1,sizeof(mask),fp) != sizeof(mask) ) |
395 | errs++; | |
396 | if ( fread(&hash,1,sizeof(hash),fp) != sizeof(hash) ) | |
397 | errs++; | |
5203fc4b | 398 | komodo_nutxoadd(0,ht,nid,hash,mask,n); |
4355e769 | 399 | } |
400 | else if ( func == 'D' ) | |
401 | { | |
9997caa0 | 402 | //printf("D[%d]\n",ht); |
671187dd | 403 | } |
bafc61f3 | 404 | else printf("illegal func.(%d %c)\n",func,func); |
405 | } | |
406 | } else fp = fopen(fname,"wb+"); | |
407 | printf("fname.(%s) fpos.%ld\n",fname,ftell(fp)); | |
408 | } | |
409 | if ( fp != 0 ) | |
410 | { | |
4355e769 | 411 | if ( height < 0 ) |
412 | { | |
9997caa0 | 413 | height = -height; |
414 | //printf("func D[%d] errs.%d\n",height,errs); | |
4355e769 | 415 | fputc('D',fp); |
416 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) | |
417 | errs++; | |
418 | } | |
9997caa0 | 419 | else if ( notarypubs != 0 && numnotaries > 0 ) |
bafc61f3 | 420 | { |
9997caa0 | 421 | //printf("func P[%d] errs.%d\n",numnotaries,errs); |
bafc61f3 | 422 | fputc('P',fp); |
c50ec0fb | 423 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
424 | errs++; | |
bafc61f3 | 425 | fputc(numnotaries,fp); |
52ed4002 | 426 | if ( fwrite(notarypubs,33,numnotaries,fp) != numnotaries ) |
427 | errs++; | |
bafc61f3 | 428 | } |
671187dd | 429 | else if ( voutmask != 0 && numvouts > 0 ) |
430 | { | |
9997caa0 | 431 | //printf("func U %d %d errs.%d hashsize.%ld\n",numvouts,notaryid,errs,sizeof(txhash)); |
671187dd | 432 | fputc('U',fp); |
c50ec0fb | 433 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
434 | errs++; | |
671187dd | 435 | fputc(numvouts,fp); |
436 | fputc(notaryid,fp); | |
437 | if ( fwrite(&voutmask,1,sizeof(voutmask),fp) != sizeof(voutmask) ) | |
438 | errs++; | |
439 | if ( fwrite(&txhash,1,sizeof(txhash),fp) != sizeof(txhash) ) | |
440 | errs++; | |
441 | } | |
6dabcca4 | 442 | else if ( height != 0 ) |
671187dd | 443 | { |
9997caa0 | 444 | //printf("func N ht.%d errs.%d\n",NOTARIZED_HEIGHT,errs); |
671187dd | 445 | fputc('N',fp); |
c50ec0fb | 446 | if ( fwrite(&height,1,sizeof(height),fp) != sizeof(height) ) |
447 | errs++; | |
671187dd | 448 | if ( fwrite(&NOTARIZED_HEIGHT,1,sizeof(NOTARIZED_HEIGHT),fp) != sizeof(NOTARIZED_HEIGHT) ) |
449 | errs++; | |
450 | if ( fwrite(&NOTARIZED_HASH,1,sizeof(NOTARIZED_HASH),fp) != sizeof(NOTARIZED_HASH) ) | |
451 | errs++; | |
5203fc4b | 452 | if ( fwrite(&NOTARIZED_BTCTXID,1,sizeof(NOTARIZED_BTCTXID),fp) != sizeof(NOTARIZED_BTCTXID) ) |
671187dd | 453 | errs++; |
5203fc4b | 454 | komodo_notarized_update(height,NOTARIZED_HEIGHT,NOTARIZED_HASH,NOTARIZED_BTCTXID); |
671187dd | 455 | } |
47f47733 | 456 | fflush(fp); |
bafc61f3 | 457 | } |
458 | } | |
459 | ||
77117dbe | 460 | void komodo_init() |
461 | { | |
462 | static int didinit; uint256 zero; int32_t k; uint8_t pubkeys[64][33]; | |
463 | if ( didinit == 0 ) | |
464 | { | |
465 | didinit = 1; | |
466 | pthread_mutex_init(&komodo_mutex,NULL); | |
467 | decode_hex(NOTARY_PUBKEY33,33,(char *)NOTARY_PUBKEY.c_str()); | |
468 | KOMODO_NUMNOTARIES = (int32_t)(sizeof(Notaries)/sizeof(*Notaries)); | |
469 | for (k=0; k<KOMODO_NUMNOTARIES; k++) | |
470 | { | |
471 | if ( Notaries[k][0] == 0 || Notaries[k][1] == 0 || Notaries[k][0][0] == 0 || Notaries[k][1][0] == 0 ) | |
472 | break; | |
473 | decode_hex(pubkeys[k],33,(char *)Notaries[k][1]); | |
474 | } | |
475 | komodo_notarysinit(0,pubkeys,KOMODO_NUMNOTARIES); | |
476 | memset(&zero,0,sizeof(zero)); | |
477 | komodo_stateupdate(0,0,0,0,zero,0,0); | |
478 | } | |
479 | } | |
480 | ||
1390456b | 481 | 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) |
482 | { | |
a5f315c7 | 483 | int32_t k,opretlen,nid,len = 0; uint256 kmdtxid,btctxid; uint8_t crypto777[33]; |
9c406099 | 484 | if ( scriptlen == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) |
1390456b | 485 | { |
c23dd601 | 486 | decode_hex(crypto777,33,(char *)CRYPTO777_PUBSECPSTR); |
986d3459 | 487 | /*for (k=0; k<33; k++) |
e1be360e | 488 | printf("%02x",crypto777[k]); |
489 | printf(" crypto777 "); | |
490 | for (k=0; k<scriptlen; k++) | |
491 | printf("%02x",scriptbuf[k]); | |
986d3459 | 492 | printf(" <- script ht.%d i.%d j.%d cmp.%d\n",height,i,j,memcmp(crypto777,scriptbuf+1,33));*/ |
1390456b | 493 | if ( memcmp(crypto777,scriptbuf+1,33) == 0 ) |
494 | { | |
495 | *specialtxp = 1; | |
496 | printf(">>>>>>>> "); | |
497 | } | |
a5f315c7 | 498 | else if ( komodo_chosennotary(&nid,height,scriptbuf + 1) >= 0 ) |
1390456b | 499 | { |
84e843cd | 500 | //printf("found notary.k%d\n",k); |
501 | if ( notaryid < 64 ) | |
1390456b | 502 | { |
84e843cd | 503 | if ( notaryid < 0 ) |
504 | { | |
a5f315c7 | 505 | notaryid = nid; |
84e843cd | 506 | *voutmaskp |= (1LL << j); |
507 | } | |
a5f315c7 | 508 | else if ( notaryid != nid ) |
84e843cd | 509 | { |
a5f315c7 | 510 | printf("mismatch notaryid.%d k.%d\n",notaryid,nid); |
84e843cd | 511 | notaryid = 64; |
512 | *voutmaskp = 0; | |
513 | } | |
514 | else *voutmaskp |= (1LL << j); | |
1390456b | 515 | } |
1390456b | 516 | } |
517 | } | |
518 | if ( j == 1 && scriptbuf[len++] == 0x6a ) | |
519 | { | |
520 | if ( (opretlen= scriptbuf[len++]) == 0x4c ) | |
521 | opretlen = scriptbuf[len++]; | |
522 | else if ( opretlen == 0x4d ) | |
523 | { | |
524 | opretlen = scriptbuf[len++]; | |
525 | opretlen = (opretlen << 8) + scriptbuf[len++]; | |
526 | } | |
527 | if ( opretlen >= 32*2+4 ) | |
528 | { | |
529 | len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid); | |
530 | len += iguana_rwnum(0,&scriptbuf[len],4,(uint8_t *)notarizedheightp); | |
531 | len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&btctxid); | |
5b760d3f | 532 | //for (k=0; k<scriptlen; k++) |
533 | // printf("%02x",scriptbuf[k]); | |
534 | //printf(" <- script ht.%d i.%d j.%d\n",height,i,j); | |
5203fc4b | 535 | printf("ht.%d NOTARIZED.%d KMD.%s BTCTXID.%s\n",height,*notarizedheightp,kmdtxid.ToString().c_str(),btctxid.ToString().c_str()); |
1390456b | 536 | if ( *notarizedheightp > NOTARIZED_HEIGHT ) |
537 | { | |
d8ce705a | 538 | static uint256 zero; |
1390456b | 539 | NOTARIZED_HEIGHT = *notarizedheightp; |
540 | NOTARIZED_HASH = kmdtxid; | |
5203fc4b | 541 | NOTARIZED_BTCTXID = btctxid; |
4355e769 | 542 | komodo_stateupdate(height,0,0,0,zero,0,0); |
1390456b | 543 | } |
544 | } | |
545 | } | |
546 | return(notaryid); | |
547 | } | |
548 | ||
651989c7 | 549 | void komodo_connectblock(CBlockIndex *pindex,CBlock& block) |
50027f06 | 550 | { |
bafc61f3 | 551 | static int32_t didinit; |
acb3f1d8 | 552 | char *scriptstr,*opreturnstr; uint64_t signedmask,voutmask; |
dd7b22ad | 553 | uint8_t scriptbuf[4096],pubkeys[64][33]; uint256 kmdtxid,btctxid,txhash; |
29e69b85 | 554 | int32_t i,j,k,numvalid,specialtx,notarizedheight,notaryid,len,numvouts,numvins,height,txn_count,flag; |
77117dbe | 555 | komodo_init(); |
68916cc6 | 556 | if ( pindex != 0 ) |
b501ded2 | 557 | { |
656eddcd | 558 | height = pindex->nHeight; |
01cc012f | 559 | txn_count = block.vtx.size(); |
560 | for (i=0; i<txn_count; i++) | |
dc64de68 | 561 | { |
352f8081 | 562 | txhash = block.vtx[i].GetHash(); |
01cc012f | 563 | numvouts = block.vtx[i].vout.size(); |
352f8081 | 564 | notaryid = -1; |
1390456b | 565 | voutmask = specialtx = notarizedheight = 0; |
01cc012f | 566 | for (j=0; j<numvouts; j++) |
dc64de68 | 567 | { |
1390456b | 568 | len = block.vtx[i].vout[j].scriptPubKey.size(); |
569 | if ( len <= sizeof(scriptbuf) ) | |
01cc012f | 570 | { |
1390456b | 571 | memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len); |
acb3f1d8 | 572 | notaryid = komodo_voutupdate(notaryid,scriptbuf,len,height,txhash,i,j,&voutmask,&specialtx,¬arizedheight); |
a01acef4 | 573 | if ( 0 && i > 0 ) |
e69a0833 | 574 | { |
575 | for (k=0; k<len; k++) | |
576 | printf("%02x",scriptbuf[k]); | |
a8832194 | 577 | 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 | 578 | } |
01cc012f | 579 | } |
352f8081 | 580 | } |
a5f315c7 | 581 | if ( notaryid >= 0 && notaryid < 64 && voutmask != 0 ) |
97cfbf8b | 582 | komodo_nutxoadd(1,height,notaryid,txhash,voutmask,numvouts); |
1390456b | 583 | signedmask = 0; |
584 | numvins = block.vtx[i].vin.size(); | |
5bdebffe | 585 | for (j=0; j<numvins; j++) |
352f8081 | 586 | { |
4355e769 | 587 | if ( (k= komodo_nutxofind(height,block.vtx[i].vin[j].prevout.hash,block.vtx[i].vin[j].prevout.n)) >= 0 ) |
1390456b | 588 | signedmask |= (1LL << k); |
21cc1d3e | 589 | else if ( 0 && signedmask != 0 ) |
bc6fd011 | 590 | 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 | 591 | } |
06f0ed43 | 592 | if ( signedmask != 0 && (notarizedheight != 0 || specialtx != 0) ) |
1390456b | 593 | { |
e69a0833 | 594 | printf("NOTARY SIGNED.%llx numvins.%d ht.%d txi.%d notaryht.%d specialtx.%d\n",(long long)signedmask,numvins,height,i,notarizedheight,specialtx); |
732fdf87 | 595 | if ( specialtx != 0 && numvouts > 2 && komodo_threshold(height,signedmask) > 0 ) |
84e843cd | 596 | { |
29e69b85 | 597 | numvalid = 0; |
dd7b22ad | 598 | memset(pubkeys,0,sizeof(pubkeys)); |
84e843cd | 599 | for (j=1; j<numvouts; j++) |
600 | { | |
601 | len = block.vtx[i].vout[j].scriptPubKey.size(); | |
602 | if ( len <= sizeof(scriptbuf) ) | |
603 | { | |
604 | memcpy(scriptbuf,block.vtx[i].vout[j].scriptPubKey.data(),len); | |
605 | if ( len == 35 && scriptbuf[0] == 33 && scriptbuf[34] == 0xac ) | |
606 | { | |
29e69b85 | 607 | memcpy(pubkeys[numvalid++],scriptbuf+1,33); |
e7f99312 | 608 | for (k=0; k<33; k++) |
84e843cd | 609 | printf("%02x",scriptbuf[k+1]); |
610 | printf(" <- new notary.[%d]\n",j-1); | |
611 | } | |
612 | } | |
613 | } | |
29e69b85 | 614 | if ( numvalid > 13 ) |
d8ce705a | 615 | { |
616 | memset(&txhash,0,sizeof(txhash)); | |
4355e769 | 617 | komodo_stateupdate(height,pubkeys,numvalid,0,txhash,0,0); |
d8ce705a | 618 | } |
835c617e | 619 | printf("new notaries.%d newheight.%d from height.%d\n",numvouts-1,KOMODO_PUBKEYS_HEIGHT(height),height); |
84e843cd | 620 | } |
a96439f5 | 621 | } |
656eddcd | 622 | } |
44c4fbbd | 623 | } else printf("komodo_connectblock: unexpected null pindex\n"); |
3d35aa5b | 624 | } |
625 | ||
4355e769 | 626 | void komodo_disconnect(CBlockIndex *pindex,CBlock& block) |
627 | { | |
77117dbe | 628 | komodo_init(); |
9997caa0 | 629 | //uint256 zero; |
630 | //printf("disconnect ht.%d\n",pindex->nHeight); | |
631 | //memset(&zero,0,sizeof(zero)); | |
632 | //komodo_stateupdate(-pindex->nHeight,0,0,0,zero,0,0); | |
4355e769 | 633 | } |
634 | ||
dfda5498 | 635 | int32_t komodo_block2height(CBlock *block) |
636 | { | |
bc556d7e | 637 | int32_t i,n,height = 0; uint8_t *ptr = (uint8_t *)block->vtx[0].vin[0].scriptSig.data(); |
77117dbe | 638 | komodo_init(); |
0ebcd665 | 639 | if ( block->vtx[0].vin[0].scriptSig.size() > 5 ) |
dfda5498 | 640 | { |
7b0be31f | 641 | //for (i=0; i<6; i++) |
642 | // printf("%02x",ptr[i]); | |
bc556d7e | 643 | n = ptr[0]; |
644 | for (i=0; i<n; i++) | |
186dfa6f | 645 | { |
6dabcca4 | 646 | //03bb81000101(bb 187) (81 48001) (00 12288256) <- coinbase.6 ht.12288256 |
647 | height += ((uint32_t)ptr[i+1] << (i*8)); | |
7b0be31f | 648 | //printf("(%02x %x %d) ",ptr[i+1],((uint32_t)ptr[i+1] << (i*8)),height); |
186dfa6f | 649 | } |
7b0be31f | 650 | //printf(" <- coinbase.%d ht.%d\n",(int32_t)block->vtx[0].vin[0].scriptSig.size(),height); |
dfda5498 | 651 | } |
652 | return(height); | |
653 | } | |
654 | ||
f2dd868d | 655 | void komodo_block2pubkey33(uint8_t *pubkey33,CBlock& block) |
656 | { | |
dfda5498 | 657 | uint8_t *ptr = (uint8_t *)block.vtx[0].vout[0].scriptPubKey.data(); |
77117dbe | 658 | komodo_init(); |
f2dd868d | 659 | memcpy(pubkey33,ptr+1,33); |
660 | } | |
661 | ||
0d24f3ed | 662 | void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex,int32_t height) |
9997caa0 | 663 | { |
664 | CBlock block; | |
77117dbe | 665 | komodo_init(); |
9499e4de | 666 | memset(pubkey33,0,33); |
0d24f3ed | 667 | if ( pindex != 0 ) |
668 | { | |
5f197aee | 669 | if ( ReadBlockFromDisk(block,(const CBlockIndex *)pindex) != 0 ) |
e778963c | 670 | { |
0d24f3ed | 671 | komodo_block2pubkey33(pubkey33,block); |
e778963c | 672 | } |
0d24f3ed | 673 | } |
674 | else | |
675 | { | |
676 | // height -> pubkey33 | |
732fdf87 | 677 | //printf("extending chaintip komodo_index2pubkey33 height.%d need to get pubkey33\n",height); |
0d24f3ed | 678 | } |
9997caa0 | 679 | } |
680 | ||
c0968b8e | 681 | int32_t komodo_opreturnscript(uint8_t *script,uint8_t *opret,int32_t opretlen) |
682 | { | |
683 | int32_t offset = 0; | |
684 | script[offset++] = 0x6a; | |
685 | if ( opretlen >= 0x4c ) | |
686 | { | |
687 | if ( opretlen > 0xff ) | |
688 | { | |
689 | script[offset++] = 0x4d; | |
690 | script[offset++] = opretlen & 0xff; | |
691 | script[offset++] = (opretlen >> 8) & 0xff; | |
692 | } | |
693 | else | |
694 | { | |
695 | script[offset++] = 0x4c; | |
696 | script[offset++] = opretlen; | |
697 | } | |
698 | } else script[offset++] = opretlen; | |
699 | memcpy(&script[offset],opret,opretlen); | |
700 | return(opretlen + offset); | |
701 | } | |
702 | ||
7652ed92 | 703 | int32_t komodo_opreturn(uint8_t *opret,int32_t maxsize) |
704 | { | |
c0968b8e | 705 | int32_t i,n; uint8_t data[4096]; |
7652ed92 | 706 | for (i=0; i<8; i++) |
c0968b8e | 707 | data[i] = i; |
708 | n = komodo_opreturnscript(opret,data,i); | |
709 | return(n); | |
7652ed92 | 710 | } |
0f24f245 | 711 | |
fcd36118 | 712 | #endif |