X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=blobdiff_plain;f=src%2Fmain.cpp;h=9a521fece45ab99f80b9220a6e65d0596dda9c67;hp=bf8bc4b2fb2da17671746b957b0e69defe9c4de4;hb=28f9882707d389250e307ebf58dcf981340f1381;hpb=1cb6116de830fb37b6961a8bd424b9368b397bee diff --git a/src/main.cpp b/src/main.cpp index bf8bc4b..9a521fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include "net.h" #include "init.h" #include "ui_interface.h" +#include "checkqueue.h" #include "kernel.h" #include "zerocoin/Zerocoin.h" #include @@ -59,6 +60,7 @@ uint256 nBestInvalidTrust = 0; uint256 hashBestChain = 0; CBlockIndex* pindexBest = NULL; int64 nTimeBestReceived = 0; +int nScriptCheckThreads = 0; CMedianFilter cPeerBlockCounts(5, 0); // Amount of blocks that other nodes claim to have @@ -724,7 +726,7 @@ bool CTxMemPool::accept(CTxDB& txdb, CTransaction &tx, bool fCheckInputs, // Check against previous transactions // This is done last to help prevent CPU exhaustion denial-of-service attacks. - if (!tx.ConnectInputs(txdb, mapInputs, mapUnused, CDiskTxPos(1,1,1), pindexBest, false, STANDARD_SCRIPT_VERIFY_FLAGS)) + if (!tx.ConnectInputs(txdb, mapInputs, mapUnused, CDiskTxPos(1,1,1), pindexBest, false, false, true, SIG_SWITCH_TIME < tx.nTime ? STRICT_FLAGS : SOFT_FLAGS)) { return error("CTxMemPool::accept() : ConnectInputs failed %s", hash.ToString().substr(0,10).c_str()); } @@ -1458,13 +1460,26 @@ unsigned int CTransaction::GetP2SHSigOpCount(const MapPrevTx& inputs) const return nSigOps; } +bool CScriptCheck::operator()() const { + const CScript &scriptSig = ptxTo->vin[nIn].scriptSig; + if (!VerifyScript(scriptSig, scriptPubKey, *ptxTo, nIn, nFlags, nHashType)) + return error("CScriptCheck() : %s VerifySignature failed", ptxTo->GetHash().ToString().substr(0,10).c_str()); + return true; +} + +bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType) +{ + return CScriptCheck(txFrom, txTo, nIn, flags, nHashType)(); +} + bool CTransaction::ConnectInputs(CTxDB& txdb, MapPrevTx inputs, map& mapTestPool, const CDiskTxPos& posThisTx, - const CBlockIndex* pindexBlock, bool fBlock, bool fMiner, unsigned int flags) + const CBlockIndex* pindexBlock, bool fBlock, bool fMiner, bool fScriptChecks, unsigned int flags, std::vector *pvChecks) { // Take over previous transactions' spent pointers // fBlock is true when this is called from AcceptBlock when a new best-block is added to the blockchain // fMiner is true when called from the internal bitcoin miner // ... both are false when called from CTransaction::AcceptToMemoryPool + if (!IsCoinBase()) { int64 nValueIn = 0; @@ -1495,6 +1510,10 @@ bool CTransaction::ConnectInputs(CTxDB& txdb, MapPrevTx inputs, mapreserve(vin.size()); + // The first loop above does all the inexpensive checks. // Only if ALL inputs pass do we perform expensive ECDSA signature checks. // Helps prevent CPU exhaustion attacks. @@ -1514,17 +1533,25 @@ bool CTransaction::ConnectInputs(CTxDB& txdb, MapPrevTx inputs, mappush_back(CScriptCheck()); + check.swap(pvChecks->back()); + } + else if (!check()) + { + if (flags & STRICT_FLAGS) { - if (VerifySignature(txPrev, *this, i, flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, 0)) - return error("ConnectInputs() : %s STANDARD_NOT_MANDATORY_VERIFY_FLAGS VerifySignature failed", GetHash().ToString().substr(0,10).c_str()); + // Don't trigger DoS code in case of STRICT_FLAGS caused failure. + CScriptCheck check(txPrev, *this, i, flags & ~STRICT_FLAGS, 0); + if (check()) + return error("ConnectInputs() : %s strict VerifySignature failed", GetHash().ToString().substr(0,10).c_str()); } - return DoS(100,error("ConnectInputs() : %s STANDARD_MANDATORY_VERIFY_FLAGS VerifySignature failed", GetHash().ToString().substr(0,10).c_str())); + return DoS(100,error("ConnectInputs() : %s VerifySignature failed", GetHash().ToString().substr(0,10).c_str())); } } @@ -1595,7 +1622,7 @@ bool CTransaction::ClientConnectInputs() // Verify signature if (!VerifySignature(txPrev, *this, i, SCRIPT_VERIFY_NOCACHE | SCRIPT_VERIFY_P2SH, 0)) - return error("ConnectInputs() : VerifySignature failed"); + return error("ClientConnectInputs() : VerifySignature failed"); ///// this is redundant with the mempool.mapNextTx stuff, ///// not sure which I want to get rid of @@ -1646,6 +1673,19 @@ bool CBlock::DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex) return true; } +static CCheckQueue scriptcheckqueue(128); + +void ThreadScriptCheck(void*) { + vnThreadsRunning[THREAD_SCRIPTCHECK]++; + RenameThread("novacoin-scriptch"); + scriptcheckqueue.Thread(); + vnThreadsRunning[THREAD_SCRIPTCHECK]--; +} + +void ThreadScriptCheckQuit() { + scriptcheckqueue.Quit(); +} + bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck) { // Check it again in case a previous version let a bad block in, but skip BlockSig checking @@ -1667,6 +1707,7 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck) // two in the chain that violate it. This prevents exploiting the issue against nodes in their // initial block download. bool fEnforceBIP30 = true; // Always active in NovaCoin + bool fScriptChecks = pindex->nHeight >= Checkpoints::GetTotalBlocksEstimate(); //// issue here: it doesn't know the version unsigned int nTxPos; @@ -1678,6 +1719,8 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck) nTxPos = pindex->nBlockPos + ::GetSerializeSize(CBlock(), SER_DISK, CLIENT_VERSION) - (2 * GetSizeOfCompactSize(0)) + GetSizeOfCompactSize(vtx.size()); map mapQueuedChanges; + CCheckQueueControl control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL); + int64 nFees = 0; int64 nValueIn = 0; int64 nValueOut = 0; @@ -1726,13 +1769,18 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck) if (!tx.IsCoinStake()) nFees += nTxValueIn - nTxValueOut; - if (!tx.ConnectInputs(txdb, mapInputs, mapQueuedChanges, posThisTx, pindex, true, false, SCRIPT_VERIFY_NOCACHE | SCRIPT_VERIFY_P2SH)) + std::vector vChecks; + if (!tx.ConnectInputs(txdb, mapInputs, mapQueuedChanges, posThisTx, pindex, true, false, fScriptChecks, SCRIPT_VERIFY_NOCACHE | SCRIPT_VERIFY_P2SH, nScriptCheckThreads ? &vChecks : NULL)) return false; + control.Add(vChecks); } mapQueuedChanges[hashTx] = CTxIndex(posThisTx, tx.vout.size()); } + if (!control.Wait()) + return DoS(100, false); + if (IsProofOfWork()) { int64 nBlockReward = GetProofOfWorkReward(nBits, fProtocol048 ? nFees : 0); @@ -2852,37 +2900,41 @@ bool LoadBlockIndex(bool fAllowNew) } } - string strPubKey = ""; - - // if checkpoint master key changed must reset sync-checkpoint - if (!txdb.ReadCheckpointPubKey(strPubKey) || strPubKey != CSyncCheckpoint::strMasterPubKey) { - // write checkpoint master key to db - txdb.TxnBegin(); - if (!txdb.WriteCheckpointPubKey(CSyncCheckpoint::strMasterPubKey)) - return error("LoadBlockIndex() : failed to write new checkpoint master key to db"); - if (!txdb.TxnCommit()) - return error("LoadBlockIndex() : failed to commit new checkpoint master key to db"); - if ((!fTestNet) && !Checkpoints::ResetSyncCheckpoint()) - return error("LoadBlockIndex() : failed to reset sync-checkpoint"); - } + CTxDB txdb("r+"); + string strPubKey = ""; + if (!txdb.ReadCheckpointPubKey(strPubKey) || strPubKey != CSyncCheckpoint::strMasterPubKey) + { + // write checkpoint master key to db + txdb.TxnBegin(); + if (!txdb.WriteCheckpointPubKey(CSyncCheckpoint::strMasterPubKey)) + return error("LoadBlockIndex() : failed to write new checkpoint master key to db"); + if (!txdb.TxnCommit()) + return error("LoadBlockIndex() : failed to commit new checkpoint master key to db"); + if ((!fTestNet) && !Checkpoints::ResetSyncCheckpoint()) + return error("LoadBlockIndex() : failed to reset sync-checkpoint"); + } - // upgrade time set to zero if blocktreedb initialized - if (txdb.ReadModifierUpgradeTime(nModifierUpgradeTime)) - { - if (nModifierUpgradeTime) - printf(" Upgrade Info: blocktreedb upgrade detected at timestamp %d\n", nModifierUpgradeTime); + // upgrade time set to zero if blocktreedb initialized + if (txdb.ReadModifierUpgradeTime(nModifierUpgradeTime)) + { + if (nModifierUpgradeTime) + printf(" Upgrade Info: blocktreedb upgrade detected at timestamp %d\n", nModifierUpgradeTime); + else + printf(" Upgrade Info: no blocktreedb upgrade detected.\n"); + } else - printf(" Upgrade Info: no blocktreedb upgrade detected.\n"); - } - else - { - nModifierUpgradeTime = GetTime(); - printf(" Upgrade Info: upgrading blocktreedb at timestamp %u\n", nModifierUpgradeTime); - if (!txdb.WriteModifierUpgradeTime(nModifierUpgradeTime)) - return error("LoadBlockIndex() : failed to write upgrade info"); - } + { + nModifierUpgradeTime = GetTime(); + printf(" Upgrade Info: upgrading blocktreedb at timestamp %u\n", nModifierUpgradeTime); + if (!txdb.WriteModifierUpgradeTime(nModifierUpgradeTime)) + return error("LoadBlockIndex() : failed to write upgrade info"); + } +#ifndef USE_LEVELDB + txdb.Close(); +#endif + } return true; } @@ -3933,6 +3985,12 @@ bool SendMessages(CNode* pto, bool fSendTrickle) pto->PushMessage("ping"); } + // Start block sync + if (pto->fStartSync) { + pto->fStartSync = false; + pto->PushGetBlocks(pindexBest, uint256(0)); + } + // Resend wallet transactions that haven't gotten in a block yet ResendWalletTransactions();