Merge branch 'new_staker' into mastertest

This commit is contained in:
blackjok3r
2019-01-23 17:46:36 +08:00
17 changed files with 301 additions and 236 deletions

View File

@@ -129,6 +129,7 @@ void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams,
}
}
#include "komodo_defs.h"
#include "cc/CCinclude.h"
extern CCriticalSection cs_metrics;
void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len);
@@ -146,9 +147,9 @@ int32_t komodo_staked(CMutableTransaction &txNew,uint32_t nBits,uint32_t *blockt
int32_t verus_staked(CBlock *pBlock, CMutableTransaction &txNew, uint32_t &nBits, arith_uint256 &hashResult, uint8_t *utxosig, CPubKey &pk);
int32_t komodo_notaryvin(CMutableTransaction &txNew,uint8_t *notarypub33);
int32_t decode_hex(uint8_t *bytes,int32_t n,char *hex);
int32_t komodo_is_notarytx(const CTransaction& tx);
CScript Marmara_scriptPubKey(int32_t height,CPubKey pk);
CScript MarmaraCoinbaseOpret(uint8_t funcid,int32_t height,CPubKey pk);
bool Getscriptaddress(char *destaddr,const CScript &scriptPubKey);
int32_t komodo_is_notarytx(const CTransaction& tx);
CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32_t gpucount, bool isStake)
@@ -522,6 +523,19 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32
//if ( blocktime > pindexPrev->GetMedianTimePast()+60 )
// blocktime = pindexPrev->GetMedianTimePast() + 60;
siglen = komodo_staked(txStaked, pblock->nBits, &blocktime, &txtime, &utxotxid, &utxovout, &utxovalue, utxosig);
// if you skip this check it will create a block too far into the future and not pass ProcessBlock or AcceptBlock.
// This has been moved from the mining loop to save CPU, and to also make ac_staked work with the verus miner.
while ( blocktime-57 > GetAdjustedTime() )
{
sleep(1);
if ( (rand() % 100) < 1 )
fprintf(stderr, "%u seconds until elegible, waiting.\n", blocktime-((uint32_t)GetAdjustedTime()+57));
if ( chainActive.LastTip()->GetHeight() >= stakeHeight )
{
fprintf(stderr, "Block Arrived, reset staking loop.\n");
return(0);
}
}
}
if ( siglen > 0 )
@@ -535,10 +549,10 @@ CBlockTemplate* CreateNewBlock(CPubKey _pk,const CScript& _scriptPubKeyIn, int32
pblocktemplate->vTxSigOps.push_back(GetLegacySigOpCount(txStaked));
nFees += txfees;
pblock->nTime = blocktime;
printf("staking PoS ht.%d t%u lag.%u\n",(int32_t)chainActive.LastTip()->GetHeight()+1,blocktime,(uint32_t)(GetAdjustedTime() - (blocktime-13)));
} else return(0); //fprintf(stderr,"no utxos eligible for staking\n");
//printf("staking PoS ht.%d t%u lag.%u\n",(int32_t)chainActive.LastTip()->GetHeight()+1,blocktime,(uint32_t)(GetAdjustedTime() - (blocktime-13)));
} else return(0); //fprintf(stderr,"no utxos eligible for staking\n");
}
// Create coinbase tx
CMutableTransaction txNew = CreateNewContextualCMutableTransaction(consensusParams, nHeight);
txNew.vin.resize(1);
@@ -565,7 +579,7 @@ printf("staking PoS ht.%d t%u lag.%u\n",(int32_t)chainActive.LastTip()->GetHeigh
txNew.vout[1].nValue = 0;
txNew.vout[1].scriptPubKey = MarmaraCoinbaseOpret('C',nHeight,pk);
}
else if ( nHeight > 1 && ASSETCHAINS_SYMBOL[0] != 0 && (ASSETCHAINS_OVERRIDE_PUBKEY33[0] != 0 || ASSETCHAINS_SCRIPTPUB.size() > 1) && ASSETCHAINS_COMMISSION != 0 && (commission= komodo_commission((CBlock*)&pblocktemplate->block,(int32_t)nHeight)) != 0 )
else if ( nHeight > 1 && ASSETCHAINS_SYMBOL[0] != 0 && (ASSETCHAINS_OVERRIDE_PUBKEY33[0] != 0 || ASSETCHAINS_SCRIPTPUB.size() > 1) && (ASSETCHAINS_COMMISSION != 0 || ASSETCHAINS_FOUNDERS_REWARD != 0) && (commission= komodo_commission((CBlock*)&pblocktemplate->block,(int32_t)nHeight)) != 0 )
{
int32_t i; uint8_t *ptr;
txNew.vout.resize(2);
@@ -608,6 +622,11 @@ printf("staking PoS ht.%d t%u lag.%u\n",(int32_t)chainActive.LastTip()->GetHeigh
if (scriptPubKeyIn.IsPayToScriptHash() || scriptPubKeyIn.IsPayToCryptoCondition())
{
fprintf(stderr,"CreateNewBlock: attempt to add timelock to pay2sh or pay2cc\n");
if ( ASSETCHAINS_SYMBOL[0] == 0 || (ASSETCHAINS_SYMBOL[0] != 0 && !isStake) )
{
LEAVE_CRITICAL_SECTION(cs_main);
LEAVE_CRITICAL_SECTION(mempool.cs);
}
return 0;
}
@@ -824,7 +843,7 @@ CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey, int32_t nHeight,
scriptPubKey = Marmara_scriptPubKey(nHeight,pubkey);
if ( ASSETCHAINS_STAKED != 0 && KOMODO_MININGTHREADS == 0 )
isStake = true;
return CreateNewBlock(pubkey,scriptPubKey, gpucount, isStake);
return CreateNewBlock(pubkey, scriptPubKey, gpucount, isStake);
}
void komodo_broadcast(CBlock *pblock,int32_t limit)
@@ -1227,7 +1246,7 @@ void static BitcoinMiner_noeq()
if ( ptr == 0 )
{
static uint32_t counter;
if ( counter++ < 10 )
if ( ASSETCHAINS_STAKED == 0 && counter++ < 10 )
fprintf(stderr,"created illegal block, retry\n");
continue;
}
@@ -1270,6 +1289,7 @@ void static BitcoinMiner_noeq()
pblock->nSolution = solnPlaceholder;
savebits = pblock->nBits;
arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);
HASHTarget = arith_uint256().SetCompact(savebits);
arith_uint256 mask(ASSETCHAINS_NONCEMASK[ASSETCHAINS_ALGO]);
Mining_start = 0;
@@ -1287,31 +1307,53 @@ void static BitcoinMiner_noeq()
if ( ASSETCHAINS_STAKED != 0 )
{
int32_t percPoS,z;
hashTarget = komodo_PoWtarget(&percPoS,hashTarget,Mining_height,ASSETCHAINS_STAKED);
for (z=31; z>=0; z--)
fprintf(stderr,"%02x",((uint8_t *)&hashTarget)[z]);
fprintf(stderr," PoW for staked coin PoS %d%% vs target %d%%\n",percPoS,(int32_t)ASSETCHAINS_STAKED);
int32_t percPoS,z; bool fNegative,fOverflow;
HASHTarget_POW = komodo_PoWtarget(&percPoS,HASHTarget,Mining_height,ASSETCHAINS_STAKED);
HASHTarget.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow);
LogPrintf("Block %d : PoS %d%% vs target %d%%\n", Mining_height, percPoS, (int32_t)ASSETCHAINS_STAKED);
}
while (true)
{
arith_uint256 arNonce = UintToArith256(pblock->nNonce);
int64_t *extraPtr;
// This seems to be a really bad way to do this, but its better than copy pasting the entire miner function at this stage.
CVerusHashWriter ss = CVerusHashWriter(SER_GETHASH, PROTOCOL_VERSION);
ss << *((CBlockHeader *)pblock);
int64_t *extraPtr = ss.xI64p();
if ( ASSETCHAINS_ALGO == ASSETCHAINS_VERUSHASH )
extraPtr = ss.xI64p();
CVerusHash &vh = ss.GetState();
uint256 hashResult = uint256();
vh.ClearExtra();
CVerusHashV2Writer ss2 = CVerusHashV2Writer(SER_GETHASH, PROTOCOL_VERSION);
ss2 << *((CBlockHeader *)pblock);
if ( ASSETCHAINS_ALGO == ASSETCHAINS_VERUSHASHV2 )
extraPtr = ss2.xI64p();
CVerusHashV2 &vh2 = ss2.GetState();
vh2.ClearExtra();
int64_t i, count = ASSETCHAINS_NONCEMASK[ASSETCHAINS_ALGO] + 1;
int64_t hashesToGo = ASSETCHAINS_HASHESPERROUND[ASSETCHAINS_ALGO];
if ( ASSETCHAINS_STAKED > 0 && ASSETCHAINS_STAKED < 100 )
{
if ( KOMODO_MININGTHREADS > 0 )
hashTarget = HASHTarget_POW;
else
hashTarget = HASHTarget;
}
else if ( ASSETCHAINS_STAKED == 100 && Mining_height > 100 )
hashTarget = HASHTarget;
// for speed check NONCEMASK at a time
for (i = 0; i < count; i++)
{
*extraPtr = i;
vh.ExtraHash((unsigned char *)&hashResult);
if ( ASSETCHAINS_ALGO == ASSETCHAINS_VERUSHASH )
vh.ExtraHash((unsigned char *)&hashResult);
else if ( ASSETCHAINS_ALGO == ASSETCHAINS_VERUSHASHV2 )
vh2.ExtraHash((unsigned char *)&hashResult);
if ( UintToArith256(hashResult) <= hashTarget )
{
@@ -1400,8 +1442,8 @@ void static BitcoinMiner_noeq()
#else
printf("%lu mega hashes complete - working\n", (ASSETCHAINS_NONCEMASK[ASSETCHAINS_ALGO] + 1) / 1048576);
#endif
pblock->nBits = savebits;
break;
}
}
}
@@ -1616,18 +1658,16 @@ void static BitcoinMiner()
} //else fprintf(stderr,"duplicate at j.%d\n",j);
} else Mining_start = 0;
} else Mining_start = 0;
if ( ASSETCHAINS_STAKED > 0 )
{
int32_t percPoS,z; bool fNegative,fOverflow;
HASHTarget_POW = komodo_PoWtarget(&percPoS,HASHTarget,Mining_height,ASSETCHAINS_STAKED);
HASHTarget.SetCompact(KOMODO_MINDIFF_NBITS,&fNegative,&fOverflow);
if ( ASSETCHAINS_STAKED < 100 && KOMODO_MININGTHREADS == 0 )
{
//for (z=31; z>=0; z--)
// fprintf(stderr,"%02x",((uint8_t *)&HASHTarget_POW)[z]);
if ( ASSETCHAINS_STAKED < 100 )
LogPrintf("Block %d : PoS %d%% vs target %d%% \n",Mining_height,percPoS,(int32_t)ASSETCHAINS_STAKED);
}
}
while (true)
{
/*if ( KOMODO_INSYNC == 0 )
@@ -1713,12 +1753,6 @@ void static BitcoinMiner()
}
else
{
while ( B.nTime-57 > GetAdjustedTime() )
{
sleep(1);
if ( chainActive.LastTip()->GetHeight() >= Mining_height )
return(false);
}
uint256 tmp = B.GetHash();
int32_t z; for (z=31; z>=0; z--)
fprintf(stderr,"%02x",((uint8_t *)&tmp)[z]);
@@ -1927,12 +1961,12 @@ void static BitcoinMiner()
for (int i = 0; i < nThreads; i++) {
#ifdef ENABLE_WALLET
if (ASSETCHAINS_ALGO == ASSETCHAINS_EQUIHASH || (ASSETCHAINS_STAKED != 0 && KOMODO_MININGTHREADS == 0) )
if ( ASSETCHAINS_ALGO == ASSETCHAINS_EQUIHASH )
minerThreads->create_thread(boost::bind(&BitcoinMiner, pwallet));
else
minerThreads->create_thread(boost::bind(&BitcoinMiner_noeq, pwallet));
#else
if (ASSETCHAINS_ALGO == ASSETCHAINS_EQUIHASH || (ASSETCHAINS_STAKED != 0 && KOMODO_MININGTHREADS == 0) )
if (ASSETCHAINS_ALGO == ASSETCHAINS_EQUIHASH )
minerThreads->create_thread(&BitcoinMiner);
else
minerThreads->create_thread(&BitcoinMiner_noeq);