if (txout.IsEmpty() && !IsCoinBase() && !IsCoinStake())
return DoS(100, error("CTransaction::CheckTransaction() : txout empty for user transaction"));
- // ppcoin: enforce minimum output amount
- if ((!txout.IsEmpty()) && txout.nValue < MIN_TXOUT_AMOUNT)
+ // NovaCoin: enforce minimum output amount for user transactions
+ // (and for all transactions until 20 Sep 2013)
+ if ((!IsCoinBase() || nTime < CHAINCHECKS_SWITCH_TIME)
+ && (!txout.IsEmpty()) && txout.nValue < MIN_TXOUT_AMOUNT)
return DoS(100, error("CTransaction::CheckTransaction() : txout.nValue below minimum"));
if (txout.nValue > MAX_MONEY)
if (IsCoinBase())
{
if (vin[0].scriptSig.size() < 2 || vin[0].scriptSig.size() > 100)
- return DoS(100, error("CTransaction::CheckTransaction() : coinbase script size"));
+ return DoS(100, error("CTransaction::CheckTransaction() : coinbase script size is invalid"));
}
else
{
}
int64 nSubsidy = bnUpperBound.getuint64();
+
nSubsidy = (nSubsidy / CENT) * CENT;
if (fDebug && GetBoolArg("-printcreation"))
printf("GetProofOfWorkReward() : create=%s nBits=0x%08x nSubsidy=%"PRI64d"\n", FormatMoney(nSubsidy).c_str(), nBits, nSubsidy);
uiInterface.NotifyBlocksChanged();
}
- printf("InvalidChainFound: invalid block=%s height=%d trust=%s date=%s\n",
+ CBigNum bnBestInvalidBlockTrust = pindexNew->bnChainTrust - pindexNew->pprev->bnChainTrust;
+ CBigNum bnBestBlockTrust = pindexBest->nHeight != 0 ? (pindexBest->bnChainTrust - pindexBest->pprev->bnChainTrust) : pindexBest->bnChainTrust;
+
+ printf("InvalidChainFound: invalid block=%s height=%d trust=%s blocktrust=%s date=%s\n",
pindexNew->GetBlockHash().ToString().substr(0,20).c_str(), pindexNew->nHeight,
- pindexNew->bnChainTrust.ToString().c_str(), DateTimeStrFormat("%x %H:%M:%S",
- pindexNew->GetBlockTime()).c_str());
- printf("InvalidChainFound: current best=%s height=%d trust=%s date=%s\n",
- hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, bnBestChainTrust.ToString().c_str(),
+ pindexNew->bnChainTrust.ToString().c_str(), bnBestInvalidBlockTrust.ToString().c_str(),
+ DateTimeStrFormat("%x %H:%M:%S", pindexNew->GetBlockTime()).c_str());
+ printf("InvalidChainFound: current best=%s height=%d trust=%s blocktrust=%s date=%s\n",
+ hashBestChain.ToString().substr(0,20).c_str(), nBestHeight,
+ bnBestChainTrust.ToString().c_str(),
+ bnBestBlockTrust.ToString().c_str(),
DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());
}
+
void CBlock::UpdateTime(const CBlockIndex* pindexPrev)
{
nTime = max(GetBlockTime(), GetAdjustedTime());
bnBestChainTrust = pindexNew->bnChainTrust;
nTimeBestReceived = GetTime();
nTransactionsUpdated++;
- printf("SetBestChain: new best=%s height=%d trust=%s date=%s\n",
+
+ CBigNum bnBestBlockTrust = pindexBest->nHeight != 0 ? (pindexBest->bnChainTrust - pindexBest->pprev->bnChainTrust) : pindexBest->bnChainTrust;
+
+ printf("SetBestChain: new best=%s height=%d trust=%s blocktrust=%s date=%s\n",
hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, bnBestChainTrust.ToString().c_str(),
+ bnBestBlockTrust.ToString().c_str(),
DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());
// Check the version of the last 100 blocks to see if we need to upgrade:
if (vtx[i].IsCoinBase())
return DoS(100, error("CheckBlock() : more than one coinbase"));
- // ppcoin: only the second transaction can be the optional coinstake
- for (unsigned int i = 2; i < vtx.size(); i++)
- if (vtx[i].IsCoinStake())
- return DoS(100, error("CheckBlock() : coinstake in wrong position"));
-
- // ppcoin: coinbase output should be empty if proof-of-stake block
- if (IsProofOfStake() && (vtx[0].vout.size() != 1 || !vtx[0].vout[0].IsEmpty()))
- return error("CheckBlock() : coinbase output not empty for proof-of-stake block");
-
// Check coinbase timestamp
if (GetBlockTime() > (int64)vtx[0].nTime + nMaxClockDrift)
return DoS(50, error("CheckBlock() : coinbase timestamp is too early"));
- // Check coinstake timestamp
- if (IsProofOfStake() && !CheckCoinStakeTimestamp(GetBlockTime(), (int64)vtx[1].nTime))
- return DoS(50, error("CheckBlock() : coinstake timestamp violation nTimeBlock=%"PRI64d" nTimeTx=%u", GetBlockTime(), vtx[1].nTime));
+ if (IsProofOfStake())
+ {
+ // ppcoin: coinbase output should be empty if proof-of-stake block
+ if (vtx[0].vout.size() != 1 || !vtx[0].vout[0].IsEmpty())
+ return error("CheckBlock() : coinbase output not empty for proof-of-stake block");
+
+ // Second transaction must be coinstake, the rest must not be
+ if (vtx.empty() || !vtx[1].IsCoinStake())
+ return DoS(100, error("CheckBlock() : second tx is not coinstake"));
+ for (unsigned int i = 2; i < vtx.size(); i++)
+ if (vtx[i].IsCoinStake())
+ return DoS(100, error("CheckBlock() : more than one coinstake"));
+
+ // Check coinstake timestamp
+ if (!CheckCoinStakeTimestamp(GetBlockTime(), (int64)vtx[1].nTime))
+ return DoS(50, error("CheckBlock() : coinstake timestamp violation nTimeBlock=%"PRI64d" nTimeTx=%u", GetBlockTime(), vtx[1].nTime));
+ }
+ else
+ {
+ // Coinbase fee paid until 20 Sep 2013
+ int64 nFee = GetBlockTime() < CHAINCHECKS_SWITCH_TIME ? vtx[0].GetMinFee() - MIN_TX_FEE : 0;
- // Check coinbase reward
- if (vtx[0].GetValueOut() > (IsProofOfWork()? (GetProofOfWorkReward(nBits) - vtx[0].GetMinFee() + MIN_TX_FEE) : 0))
- return DoS(50, error("CheckBlock() : coinbase reward exceeded %s > %s",
+ // Check coinbase reward
+ if (vtx[0].GetValueOut() > (GetProofOfWorkReward(nBits) - nFee))
+ return DoS(50, error("CheckBlock() : coinbase reward exceeded %s > %s",
FormatMoney(vtx[0].GetValueOut()).c_str(),
- FormatMoney(IsProofOfWork()? GetProofOfWorkReward(nBits) : 0).c_str()));
+ FormatMoney(GetProofOfWorkReward(nBits) - nFee).c_str()));
+ }
// Check transactions
BOOST_FOREACH(const CTransaction& tx, vtx)
if (fCheckMerkleRoot && hashMerkleRoot != BuildMerkleTree())
return DoS(100, error("CheckBlock() : hashMerkleRoot mismatch"));
- // ppcoin: check block signature
- if (!CheckBlockSignature())
- return DoS(100, error("CheckBlock() : bad block signature"));
+ // NovaCoin: check proof-of-stake block signature
+ if (IsProofOfStake() || (!fTestNet && GetBlockTime() < CHAINCHECKS_SWITCH_TIME))
+ {
+ if (!CheckBlockSignature())
+ return DoS(100, error("CheckBlock() : bad block signature"));
+ }
return true;
}
return true;
}
+CBigNum CBlockIndex::GetBlockTrust() const
+{
+ CBigNum bnTarget;
+
+ // Old protocol
+ if (!fTestNet && GetBlockTime() < CHAINCHECKS_SWITCH_TIME)
+ {
+ CBigNum bnTarget;
+ bnTarget.SetCompact(nBits);
+
+ if (bnTarget <= 0)
+ return 0;
+ return (IsProofOfStake()? (CBigNum(1)<<256) / (bnTarget+1) : 1);
+ }
+
+ // New protocol
+ if (pprev == NULL || pprev->nHeight < 10)
+ return 1;
+
+ const CBlockIndex* currentIndex = pprev;
+
+ if(IsProofOfStake())
+ {
+ bnTarget.SetCompact(nBits);
+ if (bnTarget <= 0)
+ return 0;
+
+ if (!pprev->IsProofOfWork())
+ return (CBigNum(1)<<256) / (3 * (bnTarget+1));
+
+ int nPoWCount = 0;
+
+ // Check last 12 blocks type
+ while (pprev->nHeight - currentIndex->nHeight < 12)
+ {
+ if (currentIndex->IsProofOfWork())
+ nPoWCount++;
+ currentIndex = currentIndex->pprev;
+ }
+
+ // Return 1/3 of score if less than 3 PoW blocks found
+ if (nPoWCount < 3)
+ return (CBigNum(1)<<256) / (3 * (bnTarget+1));
+
+ return (CBigNum(1)<<256) / (bnTarget+1);
+ }
+ else
+ {
+ if (!(pprev->IsProofOfStake() && pprev->pprev->IsProofOfStake()))
+ return 1 + (2 * (pprev->bnChainTrust - pprev->pprev->bnChainTrust) / 3);
+
+ int nPoSCount = 0;
+
+ // Check last 12 blocks type
+ while (pprev->nHeight - currentIndex->nHeight < 12)
+ {
+ if (currentIndex->IsProofOfStake())
+ nPoSCount++;
+ currentIndex = currentIndex->pprev;
+ }
+
+ // Return 2/3 of previous block score if less than 7 PoS blocks found
+ if (nPoSCount < 7)
+ return 1 + (2 * (pprev->bnChainTrust - pprev->pprev->bnChainTrust) / 3);
+
+ return (pprev->bnChainTrust - pprev->pprev->bnChainTrust);
+ }
+}
+
bool CBlockIndex::IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired, unsigned int nToCheck)
{
unsigned int nFound = 0;
return true;
}
-void static ThreadBitcoinMiner(void* parg);
-
-static bool fGenerateBitcoins = false;
-static bool fLimitProcessors = false;
-static int nLimitProcessors = -1;
-
void BitcoinMiner(CWallet *pwallet, bool fProofOfStake)
{
- void *scratchbuf = scrypt_buffer_alloc();
-
- printf("CPUMiner started for proof-of-%s\n", fProofOfStake? "stake" : "work");
SetThreadPriority(THREAD_PRIORITY_LOWEST);
// Make this thread recognisable as the mining thread
CReserveKey reservekey(pwallet);
unsigned int nExtraNonce = 0;
- while (fGenerateBitcoins || fProofOfStake)
+ while (fProofOfStake)
{
if (fShutdown)
return;
Sleep(1000);
if (fShutdown)
return;
- if ((!fGenerateBitcoins) && !fProofOfStake)
+ if (!fProofOfStake)
return;
}
//
// Create new block
//
- unsigned int nTransactionsUpdatedLast = nTransactionsUpdated;
CBlockIndex* pindexPrev = pindexBest;
auto_ptr<CBlock> pblock(CreateNewBlock(pwallet, fProofOfStake));
continue;
}
strMintWarning = "";
- printf("CPUMiner : proof-of-stake block found %s\n", pblock->GetHash().ToString().c_str());
+ printf("StakeMiner : proof-of-stake block found %s\n", pblock->GetHash().ToString().c_str());
SetThreadPriority(THREAD_PRIORITY_NORMAL);
CheckWork(pblock.get(), *pwalletMain, reservekey);
SetThreadPriority(THREAD_PRIORITY_LOWEST);
Sleep(500);
continue;
}
-
- printf("Running BitcoinMiner with %"PRIszu" transactions in block (%u bytes)\n", pblock->vtx.size(),
- ::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
-
- //
- // Pre-build hash buffers
- //
- char pmidstatebuf[32+16]; char* pmidstate = alignup<16>(pmidstatebuf);
- char pdatabuf[128+16]; char* pdata = alignup<16>(pdatabuf);
- char phash1buf[64+16]; char* phash1 = alignup<16>(phash1buf);
-
- FormatHashBuffers(pblock.get(), pmidstate, pdata, phash1);
-
- unsigned int& nBlockTime = *(unsigned int*)(pdata + 64 + 4);
- unsigned int& nBlockNonce = *(unsigned int*)(pdata + 64 + 12);
-
-
- //
- // Search
- //
- int64 nStart = GetTime();
- uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
-
- unsigned int max_nonce = 0xffff0000;
- block_header res_header;
- uint256 result;
-
- loop
- {
- unsigned int nHashesDone = 0;
- unsigned int nNonceFound;
-
- nNonceFound = scanhash_scrypt(
- (block_header *)&pblock->nVersion,
- scratchbuf,
- max_nonce,
- nHashesDone,
- UBEGIN(result),
- &res_header
- );
-
- // Check if something found
- if (nNonceFound != (unsigned int) -1)
- {
- if (result <= hashTarget)
- {
- // Found a solution
- pblock->nNonce = nNonceFound;
- assert(result == pblock->GetHash());
- if (!pblock->SignBlock(*pwalletMain))
- {
-// strMintWarning = strMintMessage;
- break;
- }
- strMintWarning = "";
-
- SetThreadPriority(THREAD_PRIORITY_NORMAL);
- CheckWork(pblock.get(), *pwalletMain, reservekey);
- SetThreadPriority(THREAD_PRIORITY_LOWEST);
- break;
- }
- }
-
- // Meter hashes/sec
- static int64 nHashCounter;
- if (nHPSTimerStart == 0)
- {
- nHPSTimerStart = GetTimeMillis();
- nHashCounter = 0;
- }
- else
- nHashCounter += nHashesDone;
- if (GetTimeMillis() - nHPSTimerStart > 4000)
- {
- static CCriticalSection cs;
- {
- LOCK(cs);
- if (GetTimeMillis() - nHPSTimerStart > 4000)
- {
- dHashesPerSec = 1000.0 * nHashCounter / (GetTimeMillis() - nHPSTimerStart);
- nHPSTimerStart = GetTimeMillis();
- nHashCounter = 0;
- static int64 nLogTime;
- if (GetTime() - nLogTime > 30 * 60)
- {
- nLogTime = GetTime();
- printf("hashmeter %3d CPUs %6.0f khash/s\n", vnThreadsRunning[THREAD_MINER], dHashesPerSec/1000.0);
- }
- }
- }
- }
-
- // Check for stop or if block needs to be rebuilt
- if (fShutdown)
- return;
- if (!fGenerateBitcoins)
- return;
- if (fLimitProcessors && vnThreadsRunning[THREAD_MINER] > nLimitProcessors)
- return;
- if (vNodes.empty())
- break;
- if (nBlockNonce >= 0xffff0000)
- break;
- if (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60)
- break;
- if (pindexPrev != pindexBest)
- break;
-
- // Update nTime every few seconds
- pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, pblock->GetMaxTransactionTime());
- pblock->nTime = max(pblock->GetBlockTime(), pindexPrev->GetBlockTime() - nMaxClockDrift);
- pblock->UpdateTime(pindexPrev);
- nBlockTime = ByteReverse(pblock->nTime);
-
- if (pblock->GetBlockTime() >= (int64)pblock->vtx[0].nTime + nMaxClockDrift)
- break; // need to update coinbase timestamp
- }
}
-
- scrypt_buffer_free(scratchbuf);
-}
-
-void static ThreadBitcoinMiner(void* parg)
-{
- CWallet* pwallet = (CWallet*)parg;
- try
- {
- vnThreadsRunning[THREAD_MINER]++;
- BitcoinMiner(pwallet, false);
- vnThreadsRunning[THREAD_MINER]--;
- }
- catch (std::exception& e) {
- vnThreadsRunning[THREAD_MINER]--;
- PrintException(&e, "ThreadBitcoinMiner()");
- } catch (...) {
- vnThreadsRunning[THREAD_MINER]--;
- PrintException(NULL, "ThreadBitcoinMiner()");
- }
- nHPSTimerStart = 0;
- if (vnThreadsRunning[THREAD_MINER] == 0)
- dHashesPerSec = 0;
- printf("ThreadBitcoinMiner exiting, %d threads remaining\n", vnThreadsRunning[THREAD_MINER]);
}
-
-void GenerateBitcoins(bool fGenerate, CWallet* pwallet)
-{
- fGenerateBitcoins = fGenerate;
- nLimitProcessors = GetArg("-genproclimit", -1);
- if (nLimitProcessors == 0)
- fGenerateBitcoins = false;
- fLimitProcessors = (nLimitProcessors != -1);
-
- if (fGenerate)
- {
- int nProcessors = boost::thread::hardware_concurrency();
- printf("%d processors\n", nProcessors);
- if (nProcessors < 1)
- nProcessors = 1;
- if (fLimitProcessors && nProcessors > nLimitProcessors)
- nProcessors = nLimitProcessors;
- int nAddThreads = nProcessors - vnThreadsRunning[THREAD_MINER];
- printf("Starting %d BitcoinMiner threads\n", nAddThreads);
- for (int i = 0; i < nAddThreads; i++)
- {
- if (!NewThread(ThreadBitcoinMiner, pwallet))
- printf("Error: NewThread(ThreadBitcoinMiner) failed\n");
- Sleep(10);
- }
- }
-}