Clean fix and speedup

This commit is contained in:
jl777
2018-04-13 18:44:18 +03:00
parent d4f13ed801
commit f796b1fb8f
3 changed files with 180 additions and 119 deletions

View File

@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright © 2014-2017 The SuperNET Developers. *
* Copyright © 2014-2018 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
@@ -169,14 +169,14 @@ try_again:
curl_handle = curl_easy_init();
init_string(&s);
headers = curl_slist_append(0,"Expect:");
curl_easy_setopt(curl_handle,CURLOPT_USERAGENT,"mozilla/4.0");//"Mozilla/4.0 (compatible; )");
curl_easy_setopt(curl_handle,CURLOPT_USERAGENT,"mozilla/4.0");//"Mozilla/4.0 (compatible; )");
curl_easy_setopt(curl_handle,CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl_handle,CURLOPT_URL, url);
curl_easy_setopt(curl_handle,CURLOPT_WRITEFUNCTION, (void *)accumulatebytes); // send all data to this function
curl_easy_setopt(curl_handle,CURLOPT_WRITEDATA, &s); // we pass our 's' struct to the callback
curl_easy_setopt(curl_handle,CURLOPT_NOSIGNAL, 1L); // supposed to fix "Alarm clock" and long jump crash
curl_easy_setopt(curl_handle,CURLOPT_NOPROGRESS, 1L); // no progress callback
curl_easy_setopt(curl_handle,CURLOPT_NOPROGRESS, 1L); // no progress callback
if ( strncmp(url,"https",5) == 0 )
{
curl_easy_setopt(curl_handle,CURLOPT_SSL_VERIFYPEER,0);
@@ -198,7 +198,7 @@ try_again:
bracket0 = (char *)"[";
bracket1 = (char *)"]";
}
databuf = (char *)malloc(256 + strlen(command) + strlen(params));
sprintf(databuf,"{\"id\":\"jl777\",\"method\":\"%s\",\"params\":%s%s%s}",command,bracket0,params,bracket1);
//printf("url.(%s) userpass.(%s) databuf.(%s)\n",url,userpass,databuf);
@@ -238,7 +238,7 @@ try_again:
free(s.ptr);
sleep((1<<numretries));
goto try_again;
}
else
{
@@ -291,22 +291,22 @@ char *curl_post(CURL **cHandlep,char *url,char *userpass,char *postfields,char *
{
struct MemoryStruct chunk; CURL *cHandle; long code; struct curl_slist *headers = 0;
if ( (cHandle= *cHandlep) == NULL )
*cHandlep = cHandle = curl_easy_init();
*cHandlep = cHandle = curl_easy_init();
else curl_easy_reset(cHandle);
//#ifdef DEBUG
//curl_easy_setopt(cHandle,CURLOPT_VERBOSE, 1);
//curl_easy_setopt(cHandle,CURLOPT_VERBOSE, 1);
//#endif
curl_easy_setopt(cHandle,CURLOPT_USERAGENT,"mozilla/4.0");//"Mozilla/4.0 (compatible; )");
curl_easy_setopt(cHandle,CURLOPT_SSL_VERIFYPEER,0);
//curl_easy_setopt(cHandle,CURLOPT_SSLVERSION,1);
curl_easy_setopt(cHandle,CURLOPT_URL,url);
curl_easy_setopt(cHandle,CURLOPT_CONNECTTIMEOUT,10);
curl_easy_setopt(cHandle,CURLOPT_USERAGENT,"mozilla/4.0");//"Mozilla/4.0 (compatible; )");
curl_easy_setopt(cHandle,CURLOPT_SSL_VERIFYPEER,0);
//curl_easy_setopt(cHandle,CURLOPT_SSLVERSION,1);
curl_easy_setopt(cHandle,CURLOPT_URL,url);
curl_easy_setopt(cHandle,CURLOPT_CONNECTTIMEOUT,10);
if ( userpass != 0 && userpass[0] != 0 )
curl_easy_setopt(cHandle,CURLOPT_USERPWD,userpass);
if ( postfields != 0 && postfields[0] != 0 )
if ( postfields != 0 && postfields[0] != 0 )
{
curl_easy_setopt(cHandle,CURLOPT_POST,1);
curl_easy_setopt(cHandle,CURLOPT_POSTFIELDS,postfields);
curl_easy_setopt(cHandle,CURLOPT_POSTFIELDS,postfields);
}
if ( hdr0 != NULL && hdr0[0] != 0 )
{
@@ -426,10 +426,10 @@ int32_t komodo_verifynotarization(char *symbol,char *dest,int32_t height,int32_t
char params[256],*jsonstr,*hexstr; uint8_t *script,_script[8192]; int32_t n,len,retval = -1; cJSON *json,*txjson,*vouts,*vout,*skey;
script = _script;
/*params[0] = '[';
params[1] = '"';
for (i=0; i<32; i++)
sprintf(&params[i*2 + 2],"%02x",((uint8_t *)&NOTARIZED_DESTTXID)[31-i]);
strcat(params,"\", 1]");*/
params[1] = '"';
for (i=0; i<32; i++)
sprintf(&params[i*2 + 2],"%02x",((uint8_t *)&NOTARIZED_DESTTXID)[31-i]);
strcat(params,"\", 1]");*/
sprintf(params,"[\"%s\", 1]",NOTARIZED_DESTTXID.ToString().c_str());
if ( strcmp(symbol,ASSETCHAINS_SYMBOL[0]==0?(char *)"KMD":ASSETCHAINS_SYMBOL) != 0 )
return(0);
@@ -442,7 +442,7 @@ int32_t komodo_verifynotarization(char *symbol,char *dest,int32_t height,int32_t
if ( ASSETCHAINS_SYMBOL[0] != 0 )
{
jsonstr = komodo_issuemethod(KMDUSERPASS,(char *)"getrawtransaction",params,KMD_PORT);
//printf("userpass.(%s) got (%s)\n",KMDUSERPASS,jsonstr);
//printf("userpass.(%s) got (%s)\n",KMDUSERPASS,jsonstr);
}
}//else jsonstr = _dex_getrawtransaction();
else return(0); // need universal way to issue DEX* API, since notaries mine most blocks, this ok
@@ -500,54 +500,54 @@ int32_t komodo_verifynotarization(char *symbol,char *dest,int32_t height,int32_t
}
/*uint256 komodo_getblockhash(int32_t height)
{
uint256 hash; char params[128],*hexstr,*jsonstr; cJSON *result; int32_t i; uint8_t revbuf[32];
memset(&hash,0,sizeof(hash));
sprintf(params,"[%d]",height);
if ( (jsonstr= komodo_issuemethod(KMDUSERPASS,(char *)"getblockhash",params,BITCOIND_PORT)) != 0 )
{
if ( (result= cJSON_Parse(jsonstr)) != 0 )
{
if ( (hexstr= jstr(result,(char *)"result")) != 0 )
{
if ( is_hexstr(hexstr,0) == 64 )
{
decode_hex(revbuf,32,hexstr);
for (i=0; i<32; i++)
((uint8_t *)&hash)[i] = revbuf[31-i];
}
}
free_json(result);
}
printf("KMD hash.%d (%s) %x\n",height,jsonstr,*(uint32_t *)&hash);
free(jsonstr);
}
return(hash);
}
uint256 _komodo_getblockhash(int32_t height);*/
{
uint256 hash; char params[128],*hexstr,*jsonstr; cJSON *result; int32_t i; uint8_t revbuf[32];
memset(&hash,0,sizeof(hash));
sprintf(params,"[%d]",height);
if ( (jsonstr= komodo_issuemethod(KMDUSERPASS,(char *)"getblockhash",params,BITCOIND_PORT)) != 0 )
{
if ( (result= cJSON_Parse(jsonstr)) != 0 )
{
if ( (hexstr= jstr(result,(char *)"result")) != 0 )
{
if ( is_hexstr(hexstr,0) == 64 )
{
decode_hex(revbuf,32,hexstr);
for (i=0; i<32; i++)
((uint8_t *)&hash)[i] = revbuf[31-i];
}
}
free_json(result);
}
printf("KMD hash.%d (%s) %x\n",height,jsonstr,*(uint32_t *)&hash);
free(jsonstr);
}
return(hash);
}
uint256 _komodo_getblockhash(int32_t height);*/
uint64_t komodo_seed(int32_t height)
{
uint64_t seed = 0;
/*if ( 0 ) // problem during init time, seeds are needed for loading blockindex, so null seeds...
{
uint256 hash,zero; CBlockIndex *pindex;
memset(&hash,0,sizeof(hash));
memset(&zero,0,sizeof(zero));
if ( height > 10 )
height -= 10;
if ( ASSETCHAINS_SYMBOL[0] == 0 )
hash = _komodo_getblockhash(height);
if ( memcmp(&hash,&zero,sizeof(hash)) == 0 )
hash = komodo_getblockhash(height);
int32_t i;
for (i=0; i<32; i++)
printf("%02x",((uint8_t *)&hash)[i]);
printf(" seed.%d\n",height);
seed = arith_uint256(hash.GetHex()).GetLow64();
}
else*/
{
uint256 hash,zero; CBlockIndex *pindex;
memset(&hash,0,sizeof(hash));
memset(&zero,0,sizeof(zero));
if ( height > 10 )
height -= 10;
if ( ASSETCHAINS_SYMBOL[0] == 0 )
hash = _komodo_getblockhash(height);
if ( memcmp(&hash,&zero,sizeof(hash)) == 0 )
hash = komodo_getblockhash(height);
int32_t i;
for (i=0; i<32; i++)
printf("%02x",((uint8_t *)&hash)[i]);
printf(" seed.%d\n",height);
seed = arith_uint256(hash.GetHex()).GetLow64();
}
else*/
{
seed = (height << 13) ^ (height << 2);
seed <<= 21;
@@ -662,7 +662,9 @@ int32_t komodo_block2height(CBlock *block)
void komodo_block2pubkey33(uint8_t *pubkey33,CBlock& block)
{
int32_t n;
memset(pubkey33,0,33);
if ( KOMODO_LOADINGBLOCKS == 0 )
memset(pubkey33,0xff,33);
else memset(pubkey33,0,33);
if ( block.vtx[0].vout.size() > 0 )
{
#ifdef KOMODO_ZCASH
@@ -722,60 +724,68 @@ uint32_t komodo_heightstamp(int32_t height)
return(0);
}
void komodo_pindex_init(CBlockIndex *pindex,int32_t height)
{
int32_t i,num; uint8_t pubkeys[64][33]; CBlock block;
if ( pindex->didinit != 0 )
return;
//printf("pindex.%d komodo_pindex_init notary.%d from height.%d\n",pindex->nHeight,pindex->notaryid,height);
if ( pindex->didinit == 0 )
{
pindex->notaryid = -1;
if ( KOMODO_LOADINGBLOCKS == 0 )
memset(pindex->pubkey33,0xff,33);
else memset(pindex->pubkey33,0,33);
if ( komodo_blockload(block,pindex) == 0 )
{
komodo_block2pubkey33(pindex->pubkey33,block);
//for (i=0; i<33; i++)
// fprintf(stderr,"%02x",pindex->pubkey33[i]);
//fprintf(stderr," set pubkey at height %d/%d\n",pindex->nHeight,height);
pindex->didinit = (KOMODO_LOADINGBLOCKS == 0);
} // else fprintf(stderr,"error loading block at %d/%d",pindex->nHeight,height);
}
if ( pindex->didinit != 0 && pindex->nHeight >= 0 && (num= komodo_notaries(pubkeys,(int32_t)pindex->nHeight,(uint32_t)pindex->nTime)) > 0 )
{
for (i=0; i<num; i++)
{
if ( memcmp(pubkeys[i],pindex->pubkey33,33) == 0 )
{
pindex->notaryid = i;
break;
}
}
if ( 0 && i == num )
{
for (i=0; i<33; i++)
fprintf(stderr,"%02x",pindex->pubkey33[i]);
fprintf(stderr," unmatched pubkey at height %d/%d\n",pindex->nHeight,height);
}
}
}
void komodo_index2pubkey33(uint8_t *pubkey33,CBlockIndex *pindex,int32_t height)
{
CBlock block; int32_t num,i; uint8_t pubkeys[64][33];
//komodo_init(height);
CBlock block; int32_t num,i;
memset(pubkey33,0,33);
if ( pindex != 0 )
{
if ( pindex->pubkey33[0] == 2 || pindex->pubkey33[0] == 3 )
if ( pindex->didinit != 0 )
{
memcpy(pubkey33,pindex->pubkey33,33);
return;
}
if ( komodo_blockload(block,pindex) == 0 )
{
komodo_block2pubkey33(pubkey33,block);
if ( (pubkey33[0] == 2 || pubkey33[0] == 3) )
{
memcpy(pindex->pubkey33,pubkey33,33);
if ( (num= komodo_notaries(pubkeys,(int32_t)pindex->nHeight,(uint32_t)pindex->nTime)) > 0 )
{
pindex->notaryid = -1;
for (i=0; i<num; i++)
{
if ( memcmp(pubkeys[i],pubkey33,33) == 0 )
{
pindex->notaryid = i;
break;
}
}
}
} else pindex->notaryid = -1;
}
}
else
{
// height -> pubkey33
//printf("extending chaintip komodo_index2pubkey33 height.%d need to get pubkey33\n",height);
komodo_pindex_init(pindex,height);
memcpy(pubkey33,pindex->pubkey33,33);
}
}
/*void komodo_connectpindex(CBlockIndex *pindex)
{
CBlock block;
if ( komodo_blockload(block,pindex) == 0 )
komodo_connectblock(pindex,block);
}*/
int8_t komodo_minerid(int32_t height,uint8_t *pubkey33)
{
int32_t num,i,numnotaries; CBlockIndex *pindex; uint32_t timestamp=0; uint8_t _pubkey33[33],pubkeys[64][33];
if ( (pindex= chainActive[height]) != 0 )
{
if ( (pindex->pubkey33[0] == 2 || pindex->pubkey33[0] == 3) )
if ( pindex->didinit != 0 )
{
if ( pubkey33 != 0 )
memcpy(pubkey33,pindex->pubkey33,33);
@@ -802,7 +812,7 @@ int32_t komodo_eligiblenotary(uint8_t pubkeys[66][33],int32_t *mids,int32_t *non
{
if ( (pindex= komodo_chainactive(height-i)) != 0 )
{
if ( pindex->notaryid >= 0 && (pindex->pubkey33[0] == 2 || pindex->pubkey33[0] == 3) )
if ( pindex->notaryid >= 0 && pindex->didinit != 0 )
{
memcpy(pubkeys[i],pindex->pubkey33,33);
mids[i] = pindex->notaryid;
@@ -810,8 +820,9 @@ int32_t komodo_eligiblenotary(uint8_t pubkeys[66][33],int32_t *mids,int32_t *non
}
else
{
komodo_index2pubkey33(pubkey33,pindex,height-i);
memcpy(pubkeys[i],pubkey33,33);
komodo_pindex_init(pindex,height-i);
//komodo_index2pubkey33(pubkey33,pindex,height-i);
memcpy(pubkeys[i],pindex->pubkey33,33);
if ( (mids[i]= komodo_minerid(height-i,pubkey33)) >= 0 )
{
//mids[i] = *(int32_t *)pubkey33;
@@ -830,20 +841,23 @@ int32_t komodo_eligiblenotary(uint8_t pubkeys[66][33],int32_t *mids,int32_t *non
int32_t komodo_minerids(uint8_t *minerids,int32_t height,int32_t width) // deprecate
{
/*int32_t i,n=0;
for (i=0; i<width; i++,n++)
{
if ( height-i <= 0 )
break;
minerids[i] = komodo_minerid(height - i,0);
}
return(n);*/
for (i=0; i<width; i++,n++)
{
if ( height-i <= 0 )
break;
minerids[i] = komodo_minerid(height - i,0);
}
return(n);*/
fprintf(stderr,"komodo_minerids is deprecated\n");
return(-1);
}
int32_t komodo_is_special(uint8_t pubkeys[66][33],int32_t mids[66],int32_t height,uint8_t pubkey33[33],uint32_t timestamp)
{
int32_t i,notaryid=0,minerid,limit,nid; //uint8_t _pubkey33[33];
if ( height >= 790000 )
if ( height >= 225000 )
komodo_chosennotary(&notaryid,height,pubkey33,timestamp);
if ( height >= 700000 )
{
if ( mids[0] >= 0 )
{
@@ -862,8 +876,6 @@ int32_t komodo_is_special(uint8_t pubkeys[66][33],int32_t mids[66],int32_t heigh
}
else
{
if ( height >= 225000 )
komodo_chosennotary(&notaryid,height,pubkey33,timestamp);
if ( height >= 34000 && notaryid >= 0 )
{
if ( height < 79693 )
@@ -889,10 +901,10 @@ int32_t komodo_is_special(uint8_t pubkeys[66][33],int32_t mids[66],int32_t heigh
return(0);
}
int32_t komodo_MoM(int32_t *notarized_heightp,uint256 *MoMp,uint256 *kmdtxidp,int32_t nHeight)
int32_t komodo_MoM(int32_t *notarized_heightp,uint256 *MoMp,uint256 *kmdtxidp,int32_t nHeight,uint256 *MoMoMp,int32_t *MoMoMoffsetp,int32_t *MoMoMdepthp,int32_t *kmdstartip,int32_t *kmdendip)
{
int32_t depth,notarized_ht; uint256 MoM,kmdtxid;
depth = komodo_MoMdata(&notarized_ht,&MoM,&kmdtxid,nHeight);
depth = komodo_MoMdata(&notarized_ht,&MoM,&kmdtxid,nHeight,MoMoMp,MoMoMoffsetp,MoMoMdepthp,kmdstartip,kmdendip);
memset(MoMp,0,sizeof(*MoMp));
memset(kmdtxidp,0,sizeof(*kmdtxidp));
*notarized_heightp = 0;

View File

@@ -53,6 +53,7 @@ using namespace std;
CCriticalSection cs_main;
extern uint8_t NOTARY_PUBKEY33[33];
extern int32_t KOMODO_LOADINGBLOCKS;
BlockMap mapBlockIndex;
CChain chainActive;
@@ -4037,6 +4038,7 @@ bool CheckDiskSpace(uint64_t nAdditionalBytes)
FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
{
static int32_t didinit; long fsize,fpos; int32_t incr = 16*1024*1024;
if (pos.IsNull())
return NULL;
boost::filesystem::path path = GetBlockPosFilename(pos, prefix);
@@ -4048,6 +4050,27 @@ FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
LogPrintf("Unable to open file %s\n", path.string());
return NULL;
}
if ( didinit == 0 && strcmp(prefix,(char *)"blk") == 0 )
{
fpos = ftell(file);
fseek(file,0,SEEK_END);
fsize = ftell(file);
if ( fsize > incr )
{
char *ignore = (char *)malloc(incr);
if ( ignore != 0 )
{
rewind(file);
while ( fread(ignore,1,incr,file) == incr )
fprintf(stderr,".");
free(ignore);
fprintf(stderr,"loaded %ld bytes set fpos.%ld loading.%d\n",(long)ftell(file),(long)fpos,KOMODO_LOADINGBLOCKS);
}
}
fseek(file,fpos,SEEK_SET);
KOMODO_LOADINGBLOCKS = 0;
didinit = 1;
}
if (pos.nPos) {
if (fseek(file, pos.nPos, SEEK_SET)) {
LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
@@ -4491,7 +4514,6 @@ void UnloadBlockIndex()
bool LoadBlockIndex()
{
extern int32_t KOMODO_LOADINGBLOCKS;
// Load block index from databases
KOMODO_LOADINGBLOCKS = 1;
if (!fReindex && !LoadBlockIndexDB())

View File

@@ -116,6 +116,7 @@ bool CheckEquihashSolution(const CBlockHeader *pblock, const CChainParams& param
return true;
}
void komodo_pindex_init(CBlockIndex *pindex,int32_t height);
int32_t komodo_chosennotary(int32_t *notaryidp,int32_t height,uint8_t *pubkey33,uint32_t timestamp);
int32_t komodo_is_special(uint8_t pubkeys[66][33],int32_t mids[66],int32_t height,uint8_t pubkey33[33],uint32_t timestamp);
int32_t komodo_currentheight();
@@ -134,7 +135,7 @@ extern std::string NOTARY_PUBKEY;
bool CheckProofOfWork(int32_t height,uint8_t *pubkey33,uint256 hash, unsigned int nBits, const Consensus::Params& params)
{
extern int32_t KOMODO_REWIND;
bool fNegative,fOverflow; int32_t i,nonzpkeys=0,nonz=0,special=0,special2=0,notaryid=-1,flag = 0, mids[66]; uint32_t timestamp = 0;
bool fNegative,fOverflow; int32_t i,nonzpkeys=0,nonz=0,special=0,special2=0,notaryid=-1,flag = 0, mids[66]; uint32_t timestamp = 0; CBlockIndex *pindex;
arith_uint256 bnTarget; uint8_t pubkeys[66][33];
timestamp = komodo_chainactive_timestamp();
bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
@@ -145,6 +146,13 @@ bool CheckProofOfWork(int32_t height,uint8_t *pubkey33,uint256 hash, unsigned in
}
if ( height > 34000 && ASSETCHAINS_SYMBOL[0] == 0 ) // 0 -> non-special notary
{
if ( KOMODO_LOADINGBLOCKS == 0 )
return(true);
if ( (pindex= komodo_chainactive(height)) != 0 )
{
komodo_pindex_init(pindex,height);
memcpy(pubkey33,pindex->pubkey33,33);
}
special = komodo_chosennotary(&notaryid,height,pubkey33,timestamp);
for (i=0; i<33; i++)
{
@@ -158,7 +166,7 @@ bool CheckProofOfWork(int32_t height,uint8_t *pubkey33,uint256 hash, unsigned in
}
flag = komodo_eligiblenotary(pubkeys,mids,&nonzpkeys,height);
special2 = komodo_is_special(pubkeys,mids,height,pubkey33,timestamp);
//fprintf(stderr,"ht.%d notaryid.%d special.%d flag.%d special2.%d\n",height,notaryid,special,flag,special2);
fprintf(stderr,"ht.%d notaryid.%d special.%d flag.%d special2.%d\n",height,notaryid,special,flag,special2);
if ( notaryid >= 0 )
{
if ( height > 10000 && height < 80000 && (special != 0 || special2 > 0) )
@@ -182,7 +190,26 @@ bool CheckProofOfWork(int32_t height,uint8_t *pubkey33,uint256 hash, unsigned in
return error("CheckProofOfWork(): nBits below minimum work");
// Check proof of work matches claimed amount
if ( UintToArith256(hash) > bnTarget )
{
for (i=31; i>=0; i--)
printf("%02x",((uint8_t *)&hash)[i]);
printf(" hash vs ");
for (i=31; i>=0; i--)
printf("%02x",((uint8_t *)&bnTarget)[i]);
printf(" ht.%d special.%d notaryid.%d ht.%d mod.%d error\n",height,special,notaryid,height,(height % 35));
if ( pindex != 0 )
{
pindex->didinit = 0;
komodo_pindex_init(pindex,height);
}
for (i=0; i<33; i++)
printf("%02x",pubkey33[i]);
printf(" <- pubkey\n");
for (i=0; i<66; i++)
printf("%d ",mids[i]);
printf(" minerids from ht.%d pindex.%p\n",height,pindex);
return false;
}
return true;
}