|
|
@ -2321,194 +2321,3 @@ struct komodo_staking *komodo_addutxo(struct komodo_staking *array,int32_t *numk |
|
|
|
kp->scriptPubKey = pk; |
|
|
|
return(array); |
|
|
|
} |
|
|
|
|
|
|
|
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; |
|
|
|
int32_t PoSperc = 0; |
|
|
|
set<CBitcoinAddress> setAddress; struct komodo_staking *kp; int32_t winners,segid,minage,nHeight,counter=0,i,m,siglen=0,nMinDepth = 1,nMaxDepth = 99999999; 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; |
|
|
|
|
|
|
|
bnTarget.SetCompact(nBits, &fNegative, &fOverflow); |
|
|
|
assert(pwalletMain != NULL); |
|
|
|
*utxovaluep = 0; |
|
|
|
memset(utxotxidp,0,sizeof(*utxotxidp)); |
|
|
|
memset(utxovoutp,0,sizeof(*utxovoutp)); |
|
|
|
memset(utxosig,0,72); |
|
|
|
if ( (tipindex= chainActive.Tip()) == 0 ) |
|
|
|
return(0); |
|
|
|
nHeight = tipindex->GetHeight() + 1; |
|
|
|
// Get the PoS% so we can pass it to komodo_stake, this is to adjust PoS dofficulty when it is under the target %!
|
|
|
|
tmpTarget = komodo_PoWtarget(&PoSperc,bnTarget,nHeight,ASSETCHAINS_STAKED); |
|
|
|
if ( (minage= nHeight*3) > 6000 ) // about 100 blocks
|
|
|
|
minage = 6000; |
|
|
|
komodo_segids(hashbuf,nHeight-101,100); |
|
|
|
if ( *blocktimep < tipindex->nTime+60) |
|
|
|
*blocktimep = tipindex->nTime+60; |
|
|
|
|
|
|
|
bool resetstaker = false; |
|
|
|
if ( array != 0 ) |
|
|
|
{ |
|
|
|
CBlockIndex* pblockindex = chainActive[tipindex->GetHeight()]; |
|
|
|
CBlock block; CTxDestination addressout; |
|
|
|
if ( ASSETCHAINS_MARMARA != 0 ) |
|
|
|
resetstaker = true; |
|
|
|
else if( ReadBlockFromDisk(block, pblockindex, 1) && komodo_WhoStaked(&block, addressout) != 0 && IsMine(*pwalletMain,addressout) != 0 ) |
|
|
|
{ |
|
|
|
resetstaker = true; |
|
|
|
fprintf(stderr, "Reset ram staker after mining a block!\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if ( resetstaker || array == 0 || time(NULL) > lasttime+600 ) |
|
|
|
{ |
|
|
|
LOCK2(cs_main, pwalletMain->cs_wallet); |
|
|
|
pwalletMain->AvailableCoins(vecOutputs, false, NULL, true); |
|
|
|
if ( array != 0 ) |
|
|
|
{ |
|
|
|
free(array); |
|
|
|
array = 0; |
|
|
|
maxkp = numkp = 0; |
|
|
|
lasttime = 0; |
|
|
|
} |
|
|
|
if ( ASSETCHAINS_MARMARA == 0 ) |
|
|
|
{ |
|
|
|
BOOST_FOREACH(const COutput& out, vecOutputs) |
|
|
|
{ |
|
|
|
if ( (tipindex= chainActive.Tip()) == 0 || tipindex->GetHeight()+1 > nHeight ) |
|
|
|
{ |
|
|
|
fprintf(stderr,"[%s:%d] chain tip changed during staking loop t.%u counter.%d\n",ASSETCHAINS_SYMBOL,nHeight,(uint32_t)time(NULL),counter); |
|
|
|
return(0); |
|
|
|
} |
|
|
|
counter++; |
|
|
|
if ( out.nDepth < nMinDepth || out.nDepth > nMaxDepth ) |
|
|
|
{ |
|
|
|
//fprintf(stderr,"komodo_staked invalid depth %d\n",(int32_t)out.nDepth);
|
|
|
|
continue; |
|
|
|
} |
|
|
|
CAmount nValue = out.tx->vout[out.i].nValue; |
|
|
|
if ( nValue < COIN || !out.fSpendable ) |
|
|
|
continue; |
|
|
|
const CScript& pk = out.tx->vout[out.i].scriptPubKey; |
|
|
|
if ( ExtractDestination(pk,address) != 0 ) |
|
|
|
{ |
|
|
|
if ( IsMine(*pwalletMain,address) == 0 ) |
|
|
|
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);
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
struct CCcontract_info *cp,C; uint256 txid; int32_t vout,ht,unlockht; CAmount nValue; char coinaddr[64]; CPubKey mypk,Marmarapk,pk; |
|
|
|
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs; |
|
|
|
cp = CCinit(&C,EVAL_MARMARA); |
|
|
|
mypk = pubkey2pk(Mypubkey()); |
|
|
|
Marmarapk = GetUnspendable(cp,0); |
|
|
|
GetCCaddress1of2(cp,coinaddr,Marmarapk,mypk); |
|
|
|
SetCCunspents(unspentOutputs,coinaddr,true); |
|
|
|
for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) |
|
|
|
{ |
|
|
|
txid = it->first.txhash; |
|
|
|
vout = (int32_t)it->first.index; |
|
|
|
if ( (nValue= it->second.satoshis) < COIN ) |
|
|
|
continue; |
|
|
|
if ( myGetTransaction(txid,tx,hashBlock) != 0 && (pindex= komodo_getblockindex(hashBlock)) != 0 && myIsutxo_spentinmempool(ignoretxid,ignorevin,txid,vout) == 0 ) |
|
|
|
{ |
|
|
|
const CScript &scriptPubKey = tx.vout[vout].scriptPubKey; |
|
|
|
if ( DecodeMaramaraCoinbaseOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,pk,ht,unlockht) != 0 && pk == mypk ) |
|
|
|
{ |
|
|
|
array = komodo_addutxo(array,&numkp,&maxkp,(uint32_t)pindex->nTime,(uint64_t)nValue,txid,vout,coinaddr,hashbuf,(CScript)scriptPubKey); |
|
|
|
} |
|
|
|
// else fprintf(stderr,"SKIP addutxo %.8f numkp.%d vs max.%d\n",(double)nValue/COIN,numkp,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);
|
|
|
|
} |
|
|
|
block_from_future_rejecttime = (uint32_t)GetAdjustedTime() + 57; |
|
|
|
for (i=winners=0; i<numkp; i++) |
|
|
|
{ |
|
|
|
if ( fRequestShutdown || !GetBoolArg("-gen",false) ) |
|
|
|
return(0); |
|
|
|
if ( (tipindex= chainActive.Tip()) == 0 || tipindex->GetHeight()+1 > nHeight ) |
|
|
|
{ |
|
|
|
fprintf(stderr,"chain tip changed during staking loop t.%u counter.%d\n",(uint32_t)time(NULL),counter); |
|
|
|
return(0); |
|
|
|
} |
|
|
|
kp = &array[i]; |
|
|
|
eligible = komodo_stake(0,bnTarget,nHeight,kp->txid,kp->vout,0,(uint32_t)tipindex->nTime+27,kp->address,PoSperc); |
|
|
|
if ( eligible > 0 ) |
|
|
|
{ |
|
|
|
besttime = 0; |
|
|
|
if ( eligible == komodo_stake(1,bnTarget,nHeight,kp->txid,kp->vout,eligible,(uint32_t)tipindex->nTime+27,kp->address,PoSperc) ) |
|
|
|
{ |
|
|
|
// have elegible utxo to stake with.
|
|
|
|
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; |
|
|
|
} |
|
|
|
if ( eligible < block_from_future_rejecttime ) // nothing gained by going earlier
|
|
|
|
break; |
|
|
|
} else continue; |
|
|
|
} |
|
|
|
} |
|
|
|
if ( numkp < 500 && array != 0 ) |
|
|
|
{ |
|
|
|
free(array); |
|
|
|
array = 0; |
|
|
|
maxkp = numkp = 0; |
|
|
|
lasttime = 0; |
|
|
|
} |
|
|
|
if ( earliest != 0 ) |
|
|
|
{ |
|
|
|
bool signSuccess; SignatureData sigdata; uint64_t txfee; uint8_t *ptr; uint256 revtxid,utxotxid; |
|
|
|
auto consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus()); |
|
|
|
const CKeyStore& keystore = *pwalletMain; |
|
|
|
txNew.vin.resize(1); |
|
|
|
txNew.vout.resize(1); |
|
|
|
txfee = 0; |
|
|
|
for (i=0; i<32; i++) |
|
|
|
((uint8_t *)&revtxid)[i] = ((uint8_t *)utxotxidp)[31 - i]; |
|
|
|
txNew.vin[0].prevout.hash = revtxid; |
|
|
|
txNew.vin[0].prevout.n = *utxovoutp; |
|
|
|
txNew.vout[0].scriptPubKey = best_scriptPubKey; |
|
|
|
txNew.vout[0].nValue = *utxovaluep - txfee; |
|
|
|
txNew.nLockTime = earliest; |
|
|
|
CTransaction txNewConst(txNew); |
|
|
|
if ( ASSETCHAINS_MARMARA == 0 ) |
|
|
|
{ |
|
|
|
signSuccess = ProduceSignature(TransactionSignatureCreator(&keystore, &txNewConst, 0, *utxovaluep, SIGHASH_ALL), best_scriptPubKey, sigdata, consensusBranchId); |
|
|
|
UpdateTransaction(txNew,0,sigdata); |
|
|
|
ptr = (uint8_t *)&sigdata.scriptSig[0]; |
|
|
|
siglen = sigdata.scriptSig.size(); |
|
|
|
for (i=0; i<siglen; i++) |
|
|
|
utxosig[i] = ptr[i]; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
siglen = MarmaraSignature(utxosig,txNew); |
|
|
|
if ( siglen > 0 ) |
|
|
|
signSuccess = true; |
|
|
|
else signSuccess = false; |
|
|
|
} |
|
|
|
if (!signSuccess) |
|
|
|
fprintf(stderr,"failed to create signature\n"); |
|
|
|
else |
|
|
|
*blocktimep = earliest; |
|
|
|
} |
|
|
|
return(siglen); |
|
|
|
} |
|
|
|