Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
Show First 20 Lines • Show All 304 Lines • ▼ Show 20 Lines | static bool IsCurrentForFeeEstimation() { | ||||
return true; | return true; | ||||
} | } | ||||
static bool IsMagneticAnomalyEnabledForCurrentBlock(const Config &config) { | static bool IsMagneticAnomalyEnabledForCurrentBlock(const Config &config) { | ||||
AssertLockHeld(cs_main); | AssertLockHeld(cs_main); | ||||
return IsMagneticAnomalyEnabled(config, chainActive.Tip()); | return IsMagneticAnomalyEnabled(config, chainActive.Tip()); | ||||
} | } | ||||
static bool IsGreatWallEnabledForCurrentBlock(const Config &config) { | |||||
AssertLockHeld(cs_main); | |||||
return IsGreatWallEnabled(config, chainActive.Tip()); | |||||
} | |||||
// Command-line argument "-replayprotectionactivationtime=<timestamp>" will | // Command-line argument "-replayprotectionactivationtime=<timestamp>" will | ||||
// cause the node to switch to replay protected SigHash ForkID value when the | // cause the node to switch to replay protected SigHash ForkID value when the | ||||
// median timestamp of the previous 11 blocks is greater than or equal to | // median timestamp of the previous 11 blocks is greater than or equal to | ||||
// <timestamp>. Defaults to the pre-defined timestamp when not set. | // <timestamp>. Defaults to the pre-defined timestamp when not set. | ||||
static bool IsReplayProtectionEnabled(const Config &config, | static bool IsReplayProtectionEnabled(const Config &config, | ||||
int64_t nMedianTimePast) { | int64_t nMedianTimePast) { | ||||
return nMedianTimePast >= | return nMedianTimePast >= | ||||
gArgs.GetArg( | gArgs.GetArg( | ||||
▲ Show 20 Lines • Show All 309 Lines • ▼ Show 20 Lines | for (const CTxIn &txin : tx.vin) { | ||||
if (IsReplayProtectionEnabledForCurrentBlock(config)) { | if (IsReplayProtectionEnabledForCurrentBlock(config)) { | ||||
extraFlags |= SCRIPT_ENABLE_REPLAY_PROTECTION; | extraFlags |= SCRIPT_ENABLE_REPLAY_PROTECTION; | ||||
} | } | ||||
if (IsMagneticAnomalyEnabledForCurrentBlock(config)) { | if (IsMagneticAnomalyEnabledForCurrentBlock(config)) { | ||||
extraFlags |= SCRIPT_ENABLE_CHECKDATASIG; | extraFlags |= SCRIPT_ENABLE_CHECKDATASIG; | ||||
} | } | ||||
if (IsGreatWallEnabledForCurrentBlock(config)) { | |||||
extraFlags |= SCRIPT_ENABLE_SCHNORR; | |||||
} | |||||
// Check inputs based on the set of flags we activate. | // Check inputs based on the set of flags we activate. | ||||
uint32_t scriptVerifyFlags = STANDARD_SCRIPT_VERIFY_FLAGS; | uint32_t scriptVerifyFlags = STANDARD_SCRIPT_VERIFY_FLAGS; | ||||
if (!config.GetChainParams().RequireStandard()) { | if (!config.GetChainParams().RequireStandard()) { | ||||
scriptVerifyFlags = | scriptVerifyFlags = | ||||
SCRIPT_ENABLE_SIGHASH_FORKID | | SCRIPT_ENABLE_SIGHASH_FORKID | | ||||
gArgs.GetArg("-promiscuousmempoolflags", scriptVerifyFlags); | gArgs.GetArg("-promiscuousmempoolflags", scriptVerifyFlags); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 553 Lines • ▼ Show 20 Lines | for (size_t i = 0; i < tx.vin.size(); i++) { | ||||
if (check2()) { | if (check2()) { | ||||
return state.Invalid( | return state.Invalid( | ||||
false, REJECT_NONSTANDARD, | false, REJECT_NONSTANDARD, | ||||
strprintf("non-mandatory-script-verify-flag (%s)", | strprintf("non-mandatory-script-verify-flag (%s)", | ||||
ScriptErrorString(check.GetScriptError()))); | ScriptErrorString(check.GetScriptError()))); | ||||
} | } | ||||
} | } | ||||
// We also, regardless, need to check whether the transaction would | |||||
// be valid on the other side of the upgrade, so as to avoid | |||||
// splitting the network between upgraded and non-upgraded nodes. | |||||
// Note that this will create strange error messages like | |||||
// "non-mandatory-script-verify-flag (Non-canonical DER signature)" | |||||
// -- the tx was refused entry due to STRICTENC, a mandatory flag, | |||||
// but after the upgrade the signature would have been interpreted | |||||
// as valid Schnorr and thus STRICTENC would not happen. | |||||
CScriptCheck check3(scriptPubKey, amount, tx, i, | |||||
mandatoryFlags ^ SCRIPT_ENABLE_SCHNORR, | |||||
sigCacheStore, txdata); | |||||
if (check3()) { | |||||
return state.Invalid( | |||||
false, REJECT_NONSTANDARD, | |||||
strprintf("non-mandatory-script-verify-flag (%s)", | |||||
ScriptErrorString(check.GetScriptError()))); | |||||
} | |||||
// Failures of other flags indicate a transaction that is invalid in | // Failures of other flags indicate a transaction that is invalid in | ||||
// new blocks, e.g. a invalid P2SH. We DoS ban such nodes as they | // new blocks, e.g. a invalid P2SH. We DoS ban such nodes as they | ||||
// are not following the protocol. That said during an upgrade | // are not following the protocol. That said during an upgrade | ||||
// careful thought should be taken as to the correct behavior - we | // careful thought should be taken as to the correct behavior - we | ||||
// may want to continue peering with non-upgraded nodes even after | // may want to continue peering with non-upgraded nodes even after | ||||
// soft-fork super-majority signaling has occurred. | // soft-fork super-majority signaling has occurred. | ||||
return state.DoS( | return state.DoS( | ||||
100, false, REJECT_INVALID, | 100, false, REJECT_INVALID, | ||||
▲ Show 20 Lines • Show All 300 Lines • ▼ Show 20 Lines | int32_t ComputeBlockVersion(const CBlockIndex *pindexPrev, | ||||
const Consensus::Params ¶ms) { | const Consensus::Params ¶ms) { | ||||
int32_t nVersion = VERSIONBITS_TOP_BITS; | int32_t nVersion = VERSIONBITS_TOP_BITS; | ||||
return nVersion; | return nVersion; | ||||
} | } | ||||
// Returns the script flags which should be checked for a given block | // Returns the script flags which should be checked for a given block | ||||
static uint32_t GetBlockScriptFlags(const Config &config, | static uint32_t GetBlockScriptFlags(const Config &config, | ||||
const CBlockIndex *pChainTip) { | const CBlockIndex *pChainTip) { | ||||
if (pChainTip == nullptr) { | |||||
return SCRIPT_VERIFY_NONE; | |||||
} | |||||
AssertLockHeld(cs_main); | AssertLockHeld(cs_main); | ||||
const Consensus::Params &consensusParams = | const Consensus::Params &consensusParams = | ||||
config.GetChainParams().GetConsensus(); | config.GetChainParams().GetConsensus(); | ||||
uint32_t flags = SCRIPT_VERIFY_NONE; | uint32_t flags = SCRIPT_VERIFY_NONE; | ||||
// P2SH didn't become active until Apr 1 2012 | // P2SH didn't become active until Apr 1 2012 | ||||
if (pChainTip->GetMedianTimePast() >= P2SH_ACTIVATION_TIME) { | if (pChainTip->GetMedianTimePast() >= P2SH_ACTIVATION_TIME) { | ||||
Show All 35 Lines | static uint32_t GetBlockScriptFlags(const Config &config, | ||||
// alternative. We also start enforcing push only signatures and | // alternative. We also start enforcing push only signatures and | ||||
// clean stack. | // clean stack. | ||||
if (IsMagneticAnomalyEnabled(config, pChainTip)) { | if (IsMagneticAnomalyEnabled(config, pChainTip)) { | ||||
flags |= SCRIPT_ENABLE_CHECKDATASIG; | flags |= SCRIPT_ENABLE_CHECKDATASIG; | ||||
flags |= SCRIPT_VERIFY_SIGPUSHONLY; | flags |= SCRIPT_VERIFY_SIGPUSHONLY; | ||||
flags |= SCRIPT_VERIFY_CLEANSTACK; | flags |= SCRIPT_VERIFY_CLEANSTACK; | ||||
} | } | ||||
// When the great wall fork is enabled, we start accepting 65/64-byte | |||||
// Schnorr signatures in CHECKSIG and CHECKDATASIG respectively, and their | |||||
// verify variants. We also stop accepting 65 byte signatures in | |||||
// CHECKMULTISIG and its verify variant. | |||||
if (IsGreatWallEnabled(config, pChainTip)) { | |||||
flags |= SCRIPT_ENABLE_SCHNORR; | |||||
} | |||||
// We make sure this node will have replay protection during the next hard | // We make sure this node will have replay protection during the next hard | ||||
// fork. | // fork. | ||||
if (IsReplayProtectionEnabled(config, pChainTip)) { | if (IsReplayProtectionEnabled(config, pChainTip)) { | ||||
flags |= SCRIPT_ENABLE_REPLAY_PROTECTION; | flags |= SCRIPT_ENABLE_REPLAY_PROTECTION; | ||||
} | } | ||||
return flags; | return flags; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 614 Lines • ▼ Show 20 Lines | LogPrint(BCLog::BENCH, "- Disconnect block: %.2fms\n", | ||||
(GetTimeMicros() - nStart) * MILLI); | (GetTimeMicros() - nStart) * MILLI); | ||||
// Write the chain state to disk, if necessary. | // Write the chain state to disk, if necessary. | ||||
if (!FlushStateToDisk(config.GetChainParams(), state, | if (!FlushStateToDisk(config.GetChainParams(), state, | ||||
FLUSH_STATE_IF_NEEDED)) { | FLUSH_STATE_IF_NEEDED)) { | ||||
return false; | return false; | ||||
} | } | ||||
if (GetBlockScriptFlags(config, pindexDelete) != | |||||
GetBlockScriptFlags(config, pindexDelete->pprev)) { | |||||
// We are de-activating a consensus upgrade. To be safe, we need to | |||||
// re-assess the entire mempool in case some transactions have now | |||||
// become invalid under the old rules. (Since this is a reorg, we will | |||||
// very likely land on another new post-upgrade tip that has the same | |||||
// rules. But this is not guaranteed.) | |||||
LogPrint(BCLog::MEMPOOL, | |||||
"Re-validating mempool for reorg that includes a possible " | |||||
"consensus rule downgrade"); | |||||
if (disconnectpool) { | |||||
disconnectpool->addForBlock(g_mempool.takeAll()); | |||||
} | |||||
} | |||||
// If this block was deactivating the replay protection, then we need to | // If this block was deactivating the replay protection, then we need to | ||||
// remove transactions that are replay protected from the mempool. There is | // remove transactions that are replay protected from the mempool. There is | ||||
// no easy way to do this so we'll just discard the whole mempool and then | // no easy way to do this so we'll just discard the whole mempool and then | ||||
// add the transaction of the block we just disconnected back. | // add the transaction of the block we just disconnected back. | ||||
if (IsReplayProtectionEnabled(config, pindexDelete) && | if (IsReplayProtectionEnabled(config, pindexDelete) && | ||||
!IsReplayProtectionEnabled(config, pindexDelete->pprev)) { | !IsReplayProtectionEnabled(config, pindexDelete->pprev)) { | ||||
LogPrint(BCLog::MEMPOOL, "Clearing mempool for reorg"); | LogPrint(BCLog::MEMPOOL, "Clearing mempool for reorg"); | ||||
▲ Show 20 Lines • Show All 266 Lines • ▼ Show 20 Lines | static bool ConnectTip(const Config &config, CValidationState &state, | ||||
LogPrint(BCLog::BENCH, | LogPrint(BCLog::BENCH, | ||||
" - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n", | " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n", | ||||
(nTime5 - nTime4) * MILLI, nTimeChainState * MICRO, | (nTime5 - nTime4) * MILLI, nTimeChainState * MICRO, | ||||
nTimeChainState * MILLI / nBlocksTotal); | nTimeChainState * MILLI / nBlocksTotal); | ||||
// Remove conflicting transactions from the mempool.; | // Remove conflicting transactions from the mempool.; | ||||
g_mempool.removeForBlock(blockConnecting.vtx, pindexNew->nHeight); | g_mempool.removeForBlock(blockConnecting.vtx, pindexNew->nHeight); | ||||
disconnectpool.removeForBlock(blockConnecting.vtx); | disconnectpool.removeForBlock(blockConnecting.vtx); | ||||
if (GetBlockScriptFlags(config, pindexNew) != | |||||
GetBlockScriptFlags(config, pindexNew->pprev)) { | |||||
// We are activating a consensus upgrade. To be safe, we need to | |||||
// re-assess the entire mempool in case some transactions have now | |||||
// become invalid under the new rules. | |||||
LogPrint(BCLog::MEMPOOL, | |||||
"Re-validating mempool for consensus rule upgrade"); | |||||
disconnectpool.addForBlock(g_mempool.takeAll()); | |||||
deadalnix: This is still not topologically correct. | |||||
} | |||||
// Update chainActive & related variables. | // Update chainActive & related variables. | ||||
UpdateTip(config, pindexNew); | UpdateTip(config, pindexNew); | ||||
int64_t nTime6 = GetTimeMicros(); | int64_t nTime6 = GetTimeMicros(); | ||||
nTimePostConnect += nTime6 - nTime5; | nTimePostConnect += nTime6 - nTime5; | ||||
nTimeTotal += nTime6 - nTime1; | nTimeTotal += nTime6 - nTime1; | ||||
LogPrint(BCLog::BENCH, | LogPrint(BCLog::BENCH, | ||||
" - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n", | " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n", | ||||
▲ Show 20 Lines • Show All 252 Lines • ▼ Show 20 Lines | while (fContinue && nHeight != pindexMostWork->nHeight) { | ||||
// temporarily to release the lock. | // temporarily to release the lock. | ||||
fContinue = false; | fContinue = false; | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
if (fBlocksDisconnected) { | if (fBlocksDisconnected || !disconnectpool.isEmpty()) { | ||||
// If any blocks were disconnected, disconnectpool may be non empty. Add | // If any blocks were disconnected, we need to update the mempool even | ||||
// any disconnected transactions back to the mempool. | // if disconnectpool is empty. The disconnectpool may also be non-empty | ||||
// if there was a rule-restricting consensus upgrade. | |||||
deadalnixUnsubmitted Not Done Inline ActionsRevert. deadalnix: Revert. | |||||
disconnectpool.updateMempoolForReorg(config, true); | disconnectpool.updateMempoolForReorg(config, true); | ||||
} | } | ||||
g_mempool.check(pcoinsTip.get()); | g_mempool.check(pcoinsTip.get()); | ||||
// Callbacks/notifications for a new best chain. | // Callbacks/notifications for a new best chain. | ||||
if (fInvalidFound) { | if (fInvalidFound) { | ||||
CheckForkWarningConditionsOnNewFork(pindexMostWork); | CheckForkWarningConditionsOnNewFork(pindexMostWork); | ||||
▲ Show 20 Lines • Show All 2,765 Lines • Show Last 20 Lines |
This is still not topologically correct.