Skip to content

Commit

Permalink
convert staking array to vector (to correctly destroy members) and ma…
Browse files Browse the repository at this point in the history
…ke it thread_local
  • Loading branch information
dimxy committed Sep 23, 2021
1 parent acd2b3b commit 24f7d50
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 44 deletions.
88 changes: 45 additions & 43 deletions src/komodo_bitcoind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2450,35 +2450,39 @@ int64_t komodo_coinsupply(int64_t *zfundsp,int64_t *sproutfundsp,int32_t height)
return(supply);
}

struct komodo_staking *komodo_addutxo(struct komodo_staking *array,int32_t *numkp,int32_t *maxkp,uint32_t txtime,uint64_t nValue,uint256 txid,int32_t vout,char *address,uint8_t *hashbuf,CScript pk)
void komodo_addutxo(std::vector<komodo_staking> &array,int32_t *maxkp,uint32_t txtime,uint64_t nValue,uint256 txid,int32_t vout,char *address,uint8_t *hashbuf,CScript pk)
{
uint256 hash; uint32_t segid32; struct komodo_staking *kp;
uint256 hash; uint32_t segid32; struct komodo_staking kp;
segid32 = komodo_stakehash(&hash,address,hashbuf,txid,vout);
if ( *numkp >= *maxkp )
if ( array.size() >= *maxkp )
{
*maxkp += 1000;
array = (struct komodo_staking *)realloc(array,sizeof(*array) * (*maxkp));
//fprintf(stderr,"realloc max.%d array.%p\n",*maxkp,array);
}
kp = &array[(*numkp)++];
//fprintf(stderr,"kp.%p num.%d\n",kp,*numkp);
memset(kp,0,sizeof(*kp));
strcpy(kp->address,address);
kp->txid = txid;
kp->vout = vout;
kp->hashval = UintToArith256(hash);
kp->txtime = txtime;
kp->segid32 = segid32;
kp->nValue = nValue;
kp->scriptPubKey = pk;
return(array);
//array = (struct komodo_staking *)realloc(array,sizeof(*array) * (*maxkp));
array.reserve(*maxkp);
//fprintf(stderr,"realloc max.%d array.size().%d\n",*maxkp,array.size());
}
memset(&kp,0,sizeof(kp));
strcpy(kp.address,address);
kp.txid = txid;
kp.vout = vout;
kp.hashval = UintToArith256(hash);
kp.txtime = txtime;
kp.segid32 = segid32;
kp.nValue = nValue;
kp.scriptPubKey = pk;
array.push_back(kp);
//fprintf(stderr,"kp.%p array.size().%d\n",kp,array.size());
}

int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blocktimep,uint32_t *txtimep,uint256 *utxotxidp,int32_t *utxovoutp,uint64_t *utxovaluep,uint8_t *utxosig, uint256 merkleroot)
{
static struct komodo_staking *array; static int32_t numkp,maxkp; static uint32_t lasttime;
// use thread_local to prevent crash in case of accidental thread overlapping
thread_local std::vector<komodo_staking> array;
thread_local int32_t maxkp;
thread_local uint32_t lasttime;

int32_t PoSperc = 0, newStakerActive;
std::set<CBitcoinAddress> setAddress; struct komodo_staking *kp; int32_t winners,segid,minage,nHeight,counter=0,i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; std::vector<COutput> vecOutputs; uint32_t block_from_future_rejecttime,besttime,eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 mindiff,ratio,bnTarget,tmpTarget; CBlockIndex *tipindex,*pindex; CTxDestination address; bool fNegative,fOverflow; uint8_t hashbuf[256]; CTransaction tx; uint256 hashBlock;
std::set<CBitcoinAddress> setAddress; int32_t winners,segid,minage,nHeight,counter=0,i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; std::vector<COutput> vecOutputs; uint32_t block_from_future_rejecttime,besttime,eligible,earliest = 0; CScript best_scriptPubKey; arith_uint256 mindiff,ratio,bnTarget,tmpTarget; CBlockIndex *tipindex,*pindex; CTxDestination address; bool fNegative,fOverflow; uint8_t hashbuf[256]; CTransaction tx; uint256 hashBlock;
uint64_t cbPerc = *utxovaluep, tocoinbase = 0;
if (!EnsureWalletIsAvailable(0))
return 0;
Expand All @@ -2500,7 +2504,7 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt
// this was for VerusHash PoS64
//tmpTarget = komodo_PoWtarget(&PoSperc,bnTarget,nHeight,ASSETCHAINS_STAKED);
bool resetstaker = false;
if ( array != 0 )
if ( array.size() != 0 )
{
LOCK(cs_main);
CBlockIndex* pblockindex = chainActive[tipindex->GetHeight()];
Expand All @@ -2512,15 +2516,14 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt
}
}

if ( resetstaker || array == 0 || time(NULL) > lasttime+600 )
if ( resetstaker || array.size() == 0 || time(NULL) > lasttime+600 )
{
LOCK2(cs_main, pwalletMain->cs_wallet);
pwalletMain->AvailableCoins(vecOutputs, false, NULL, true);
if ( array != 0 )
if ( array.size() != 0 )
{
free(array);
array = 0;
maxkp = numkp = 0;
array.clear();
maxkp = 0;
lasttime = 0;
}
BOOST_FOREACH(const COutput& out, vecOutputs)
Expand All @@ -2546,16 +2549,16 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt
continue;
if ( myGetTransaction(out.tx->GetHash(),tx,hashBlock) != 0 && (pindex= komodo_getblockindex(hashBlock)) != 0 )
{
array = komodo_addutxo(array,&numkp,&maxkp,(uint32_t)pindex->nTime,(uint64_t)nValue,out.tx->GetHash(),out.i,(char *)CBitcoinAddress(address).ToString().c_str(),hashbuf,(CScript)pk);
//fprintf(stderr,"addutxo numkp.%d vs max.%d\n",numkp,maxkp);
komodo_addutxo(array,&maxkp,(uint32_t)pindex->nTime,(uint64_t)nValue,out.tx->GetHash(),out.i,(char *)CBitcoinAddress(address).ToString().c_str(),hashbuf,(CScript)pk);
//fprintf(stderr,"addutxo array.size().%d vs max.%d\n",array.size(),maxkp);
}
}
}
lasttime = (uint32_t)time(NULL);
//fprintf(stderr,"finished kp data of utxo for staking %u ht.%d numkp.%d maxkp.%d\n",(uint32_t)time(NULL),nHeight,numkp,maxkp);
//fprintf(stderr,"finished kp data of utxo for staking %u ht.%d array.size().%d maxkp.%d\n",(uint32_t)time(NULL),nHeight,array.size(),maxkp);
}
block_from_future_rejecttime = (uint32_t)GetTime() + ASSETCHAINS_STAKED_BLOCK_FUTURE_MAX;
for (i=winners=0; i<numkp; i++)
for (i=winners=0; i<array.size(); i++)
{
if ( fRequestShutdown || !GetBoolArg("-gen",false) )
return(0);
Expand All @@ -2564,23 +2567,23 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt
fprintf(stderr,"[%s:%d] chain tip changed during staking loop t.%u counter.%d\n",ASSETCHAINS_SYMBOL,nHeight,(uint32_t)time(NULL),i);
return(0);
}
kp = &array[i];
eligible = komodo_stake(0,bnTarget,nHeight,kp->txid,kp->vout,0,(uint32_t)tipindex->nTime+ASSETCHAINS_STAKED_BLOCK_FUTURE_HALF,kp->address,PoSperc);
struct komodo_staking &kp = array[i];
eligible = komodo_stake(0,bnTarget,nHeight,kp.txid,kp.vout,0,(uint32_t)tipindex->nTime+ASSETCHAINS_STAKED_BLOCK_FUTURE_HALF,kp.address,PoSperc);
if ( eligible > 0 )
{
besttime = 0;
if ( eligible == komodo_stake(1,bnTarget,nHeight,kp->txid,kp->vout,eligible,(uint32_t)tipindex->nTime+ASSETCHAINS_STAKED_BLOCK_FUTURE_HALF,kp->address,PoSperc) )
if ( eligible == komodo_stake(1,bnTarget,nHeight,kp.txid,kp.vout,eligible,(uint32_t)tipindex->nTime+ASSETCHAINS_STAKED_BLOCK_FUTURE_HALF,kp.address,PoSperc) )
{
// have elegible utxo to stake with.
if ( earliest == 0 || eligible < earliest || (eligible == earliest && (*utxovaluep == 0 || kp->nValue < *utxovaluep)) )
if ( earliest == 0 || eligible < earliest || (eligible == earliest && (*utxovaluep == 0 || kp.nValue < *utxovaluep)) )
{
// is better than the previous best, so use it instead.
earliest = eligible;
best_scriptPubKey = kp->scriptPubKey;
*utxovaluep = (uint64_t)kp->nValue;
decode_hex((uint8_t *)utxotxidp,32,(char *)kp->txid.GetHex().c_str());
*utxovoutp = kp->vout;
*txtimep = kp->txtime;
best_scriptPubKey = kp.scriptPubKey;
*utxovaluep = (uint64_t)kp.nValue;
decode_hex((uint8_t *)utxotxidp,32,(char *)kp.txid.GetHex().c_str());
*utxovoutp = kp.vout;
*txtimep = kp.txtime;
}
/*if ( eligible < block_from_future_rejecttime )
{
Expand All @@ -2591,11 +2594,10 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt
}
}
}
if ( numkp < 500 && array != 0 )
if ( array.size() < 500 && array.size() != 0 )
{
free(array);
array = 0;
maxkp = numkp = 0;
array.clear();
maxkp = 0;
lasttime = 0;
}
if ( earliest != 0 )
Expand Down
2 changes: 1 addition & 1 deletion src/komodo_bitcoind.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,6 @@ struct komodo_staking
CScript scriptPubKey;
};

struct komodo_staking *komodo_addutxo(struct komodo_staking *array,int32_t *numkp,int32_t *maxkp,uint32_t txtime,uint64_t nValue,uint256 txid,int32_t vout,char *address,uint8_t *hashbuf,CScript pk);
void komodo_addutxo(std::vector<komodo_staking> &array,int32_t *maxkp,uint32_t txtime,uint64_t nValue,uint256 txid,int32_t vout,char *address,uint8_t *hashbuf,CScript pk);

int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blocktimep,uint32_t *txtimep,uint256 *utxotxidp,int32_t *utxovoutp,uint64_t *utxovaluep,uint8_t *utxosig, uint256 merkleroot);

0 comments on commit 24f7d50

Please sign in to comment.