Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
Show First 20 Lines • Show All 1,703 Lines • ▼ Show 20 Lines | bool CChainState::ConnectBlock(const CBlock &block, CValidationState &state, | ||||
LogPrint(BCLog::BENCH, " - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n", | LogPrint(BCLog::BENCH, " - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n", | ||||
MILLI * (nTime2 - nTime1), nTimeForks * MICRO, | MILLI * (nTime2 - nTime1), nTimeForks * MICRO, | ||||
nTimeForks * MILLI / nBlocksTotal); | nTimeForks * MILLI / nBlocksTotal); | ||||
std::vector<int> prevheights; | std::vector<int> prevheights; | ||||
Amount nFees = Amount::zero(); | Amount nFees = Amount::zero(); | ||||
int nInputs = 0; | int nInputs = 0; | ||||
// Sigops counting. We need to do it again because of P2SH. | |||||
uint64_t nSigOpsCount = 0; | |||||
const uint64_t currentBlockSize = | |||||
::GetSerializeSize(block, PROTOCOL_VERSION); | |||||
const uint64_t nMaxSigOpsCount = GetMaxBlockSigOpsCount(currentBlockSize); | |||||
// Limit the total executed signature operations in the block, a consensus | // Limit the total executed signature operations in the block, a consensus | ||||
// rule. Tracking during the CPU-consuming part (validation of uncached | // rule. Tracking during the CPU-consuming part (validation of uncached | ||||
// inputs) is per-input atomic and validation in each thread stops very | // inputs) is per-input atomic and validation in each thread stops very | ||||
// quickly after the limit is exceeded, so an adversary cannot cause us to | // quickly after the limit is exceeded, so an adversary cannot cause us to | ||||
// exceed the limit by much at all. | // exceed the limit by much at all. | ||||
CheckInputsLimiter nSigChecksBlockLimiter( | CheckInputsLimiter nSigChecksBlockLimiter( | ||||
GetMaxBlockSigChecksCount(options.getExcessiveBlockSize())); | GetMaxBlockSigChecksCount(options.getExcessiveBlockSize())); | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | for (const auto &ptx : block.vtx) { | ||||
// GetTransactionSigOpCount counts 2 types of sigops: | // GetTransactionSigOpCount counts 2 types of sigops: | ||||
// * legacy (always) | // * legacy (always) | ||||
// * p2sh (when P2SH enabled in flags and excludes coinbase) | // * p2sh (when P2SH enabled in flags and excludes coinbase) | ||||
auto txSigOpsCount = GetTransactionSigOpCount(tx, view, flags); | auto txSigOpsCount = GetTransactionSigOpCount(tx, view, flags); | ||||
if (txSigOpsCount > MAX_TX_SIGOPS_COUNT) { | if (txSigOpsCount > MAX_TX_SIGOPS_COUNT) { | ||||
return state.DoS(100, false, REJECT_INVALID, "bad-txn-sigops"); | return state.DoS(100, false, REJECT_INVALID, "bad-txn-sigops"); | ||||
} | } | ||||
nSigOpsCount += txSigOpsCount; | |||||
if (nSigOpsCount > nMaxSigOpsCount) { | |||||
return state.DoS(100, error("ConnectBlock(): too many sigops"), | |||||
REJECT_INVALID, "bad-blk-sigops"); | |||||
} | |||||
// The following checks do not apply to the coinbase. | // The following checks do not apply to the coinbase. | ||||
if (isCoinBase) { | if (isCoinBase) { | ||||
continue; | continue; | ||||
} | } | ||||
// Check that transaction is BIP68 final BIP68 lock checks (as | // Check that transaction is BIP68 final BIP68 lock checks (as | ||||
// opposed to nLockTime checks) must be in ConnectBlock because they | // opposed to nLockTime checks) must be in ConnectBlock because they | ||||
// require the UTXO set. | // require the UTXO set. | ||||
▲ Show 20 Lines • Show All 1,901 Lines • ▼ Show 20 Lines | static bool ContextualCheckBlock(const CBlock &block, CValidationState &state, | ||||
const int64_t nLockTimeCutoff = (nLockTimeFlags & LOCKTIME_MEDIAN_TIME_PAST) | const int64_t nLockTimeCutoff = (nLockTimeFlags & LOCKTIME_MEDIAN_TIME_PAST) | ||||
? nMedianTimePast | ? nMedianTimePast | ||||
: block.GetBlockTime(); | : block.GetBlockTime(); | ||||
const bool fIsMagneticAnomalyEnabled = | const bool fIsMagneticAnomalyEnabled = | ||||
IsMagneticAnomalyEnabled(params, pindexPrev); | IsMagneticAnomalyEnabled(params, pindexPrev); | ||||
// Keep track of the sigops count. | |||||
uint64_t nSigOps = 0; | |||||
const auto currentBlockSize = ::GetSerializeSize(block, PROTOCOL_VERSION); | |||||
auto nMaxSigOpsCount = GetMaxBlockSigOpsCount(currentBlockSize); | |||||
// Note that pindexPrev may be null if reindexing genesis block. | // Note that pindexPrev may be null if reindexing genesis block. | ||||
const auto scriptFlags = pindexPrev | const auto scriptFlags = pindexPrev | ||||
? GetNextBlockScriptFlags(params, pindexPrev) | ? GetNextBlockScriptFlags(params, pindexPrev) | ||||
: SCRIPT_VERIFY_NONE; | : SCRIPT_VERIFY_NONE; | ||||
// Check transactions: | // Check transactions: | ||||
// - canonical ordering | // - canonical ordering | ||||
// - ensure they are finalized | // - ensure they are finalized | ||||
Show All 27 Lines | for (const auto &ptx : block.vtx) { | ||||
// Count the sigops for the current transaction. If the tx or total | // Count the sigops for the current transaction. If the tx or total | ||||
// sigops counts are too high, then the block is invalid. | // sigops counts are too high, then the block is invalid. | ||||
const auto txSigOps = GetSigOpCountWithoutP2SH(tx, scriptFlags); | const auto txSigOps = GetSigOpCountWithoutP2SH(tx, scriptFlags); | ||||
if (txSigOps > MAX_TX_SIGOPS_COUNT) { | if (txSigOps > MAX_TX_SIGOPS_COUNT) { | ||||
return state.DoS(100, false, REJECT_INVALID, "bad-txn-sigops", | return state.DoS(100, false, REJECT_INVALID, "bad-txn-sigops", | ||||
false, "out-of-bounds SigOpCount"); | false, "out-of-bounds SigOpCount"); | ||||
} | } | ||||
nSigOps += txSigOps; | |||||
if (nSigOps > nMaxSigOpsCount) { | |||||
return state.DoS(100, false, REJECT_INVALID, "bad-blk-sigops", | |||||
false, "out-of-bounds SigOpCount"); | |||||
} | |||||
if (!ContextualCheckTransaction(params, tx, state, nHeight, | if (!ContextualCheckTransaction(params, tx, state, nHeight, | ||||
nLockTimeCutoff, nMedianTimePast)) { | nLockTimeCutoff, nMedianTimePast)) { | ||||
// state set by ContextualCheckTransaction. | // state set by ContextualCheckTransaction. | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 1,959 Lines • Show Last 20 Lines |