X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=blobdiff_plain;f=src%2Fmain.cpp;h=9a521fece45ab99f80b9220a6e65d0596dda9c67;hp=d8fedf4fcb099729d6219146737fdc16e385c892;hb=28f9882707d389250e307ebf58dcf981340f1381;hpb=fa9796de83b7a3ec401a4b502c246a55d79ef0c1 diff --git a/src/main.cpp b/src/main.cpp index d8fedf4..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 @@ -311,7 +313,7 @@ bool CTransaction::IsStandard() const return false; if (!txin.scriptSig.IsPushOnly()) return false; - if (fEnforceCanonical && !txin.scriptSig.HasCanonicalPushes()) { + if (!txin.scriptSig.HasCanonicalPushes()) { return false; } } @@ -325,7 +327,7 @@ bool CTransaction::IsStandard() const if (txout.nValue == 0) { return false; } - if (fEnforceCanonical && !txout.scriptPubKey.HasCanonicalPushes()) { + if (!txout.scriptPubKey.HasCanonicalPushes()) { return false; } } @@ -375,7 +377,7 @@ bool CTransaction::AreInputsStandard(const MapPrevTx& mapInputs) const // beside "push data" in the scriptSig the // IsStandard() call returns false vector > stack; - if (!EvalScript(stack, vin[i].scriptSig, *this, i, 0)) + if (!EvalScript(stack, vin[i].scriptSig, *this, i, false, 0)) return false; if (whichType == TX_SCRIPTHASH) @@ -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, false)) + 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, bool fStrictPayToScriptHash) + 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,16 +1533,24 @@ bool CTransaction::ConnectInputs(CTxDB& txdb, MapPrevTx inputs, mappush_back(CScriptCheck()); + check.swap(pvChecks->back()); + } + else if (!check()) + { + if (flags & STRICT_FLAGS) + { + // 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 VerifySignature failed", GetHash().ToString().substr(0,10).c_str())); } } @@ -1551,7 +1578,7 @@ bool CTransaction::ConnectInputs(CTxDB& txdb, MapPrevTx inputs, mapnBits, nTime) - GetMinFee(1, false, GMF_BLOCK, nTxSize) + CENT; if (nReward > nCalculatedReward) - return DoS(100, error("CheckInputs() : coinstake pays too much(actual=%"PRI64d" vs calculated=%"PRI64d")", nReward, nCalculatedReward)); + return DoS(100, error("ConnectInputs() : coinstake pays too much(actual=%"PRI64d" vs calculated=%"PRI64d")", nReward, nCalculatedReward)); } else { @@ -1594,8 +1621,8 @@ bool CTransaction::ClientConnectInputs() return false; // Verify signature - if (!VerifySignature(txPrev, *this, i, true, 0)) - return error("ConnectInputs() : VerifySignature failed"); + if (!VerifySignature(txPrev, *this, i, SCRIPT_VERIFY_NOCACHE | SCRIPT_VERIFY_P2SH, 0)) + 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,7 +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 fStrictPayToScriptHash = true; // Always active in NovaCoin + bool fScriptChecks = pindex->nHeight >= Checkpoints::GetTotalBlocksEstimate(); //// issue here: it doesn't know the version unsigned int nTxPos; @@ -1679,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; @@ -1713,15 +1755,12 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck) if (!tx.FetchInputs(txdb, mapQueuedChanges, true, false, mapInputs, fInvalid)) return false; - if (fStrictPayToScriptHash) - { - // Add in sigops done by pay-to-script-hash inputs; - // this is to prevent a "rogue miner" from creating - // an incredibly-expensive-to-validate block. - nSigOps += tx.GetP2SHSigOpCount(mapInputs); - if (nSigOps > MAX_BLOCK_SIGOPS) - return DoS(100, error("ConnectBlock() : too many sigops")); - } + // Add in sigops done by pay-to-script-hash inputs; + // this is to prevent a "rogue miner" from creating + // an incredibly-expensive-to-validate block. + nSigOps += tx.GetP2SHSigOpCount(mapInputs); + if (nSigOps > MAX_BLOCK_SIGOPS) + return DoS(100, error("ConnectBlock() : too many sigops")); int64 nTxValueIn = tx.GetValueIn(mapInputs); int64 nTxValueOut = tx.GetValueOut(); @@ -1730,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, fStrictPayToScriptHash)) + 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); @@ -2132,7 +2176,7 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos) // ppcoin: compute stake modifier uint64 nStakeModifier = 0; bool fGeneratedStakeModifier = false; - if (!ComputeNextStakeModifier(pindexNew->pprev, nStakeModifier, fGeneratedStakeModifier)) + if (!ComputeNextStakeModifier(pindexNew, nStakeModifier, fGeneratedStakeModifier)) return error("AddToBlockIndex() : ComputeNextStakeModifier() failed"); pindexNew->SetStakeModifier(nStakeModifier, fGeneratedStakeModifier); pindexNew->nStakeModifierChecksum = GetStakeModifierChecksum(pindexNew); @@ -2844,24 +2888,52 @@ bool LoadBlockIndex(bool fAllowNew) if (!block.AddToBlockIndex(nFile, nBlockPos)) return error("LoadBlockIndex() : genesis block not accepted"); - // ppcoin: initialize synchronized checkpoint + // initialize synchronized checkpoint if (!Checkpoints::WriteSyncCheckpoint((!fTestNet ? hashGenesisBlock : hashGenesisBlockTestNet))) return error("LoadBlockIndex() : failed to init sync checkpoint"); - } - string strPubKey = ""; + // upgrade time set to zero if txdb initialized + { + if (!txdb.WriteModifierUpgradeTime(0)) + return error("LoadBlockIndex() : failed to init upgrade info"); + printf(" Upgrade Info: ModifierUpgradeTime txdb initialization\n"); + } + } - // 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); + 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"); + } + +#ifndef USE_LEVELDB + txdb.Close(); +#endif } return true; @@ -3027,6 +3099,14 @@ string GetWarnings(string strFor) strStatusBar = strMiscWarning; } + // if detected unmet upgrade requirement enter safe mode + // Note: Modifier upgrade requires blockchain redownload if past protocol switch + if (IsFixedModifierInterval(nModifierUpgradeTime + 60*60*24)) // 1 day margin + { + nPriority = 5000; + strStatusBar = strRPC = "WARNING: Blockchain redownload required approaching or past v.0.4.4.6u4 upgrade deadline."; + } + // if detected invalid checkpoint enter safe mode if (Checkpoints::hashInvalidCheckpoint != 0) { @@ -3905,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();