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