Skip to content

Commit

Permalink
Merge pull request dashpay#10 from barrystyle/0.14-sigops
Browse files Browse the repository at this point in the history
Attempt to speed up processing of spam/sigops abuse blocks.
  • Loading branch information
barrystyle authored Aug 17, 2019
2 parents 21ba708 + 2a79a23 commit 3bcc1bb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
58 changes: 38 additions & 20 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2123,9 +2123,10 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd

nInputs += tx.vin.size();
nSigOps += GetLegacySigOpCount(tx);
if (nSigOps > MaxBlockSigOps(fDIP0001Active_context))
return state.DoS(100, error("ConnectBlock(): too many sigops"),
REJECT_INVALID, "bad-blk-sigops");
if (!IgnoreSigopsLimits(pindex->nHeight))
if (nSigOps > MaxBlockSigOps(fDIP0001Active_context))
return state.DoS(100, error("ConnectBlock(): too many sigops"),
REJECT_INVALID, "bad-blk-sigops");

if (!tx.IsCoinBase())
{
Expand Down Expand Up @@ -2192,9 +2193,10 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
// this is to prevent a "rogue miner" from creating
// an incredibly-expensive-to-validate block.
nSigOps += GetP2SHSigOpCount(tx, view);
if (nSigOps > MaxBlockSigOps(fDIP0001Active_context))
return state.DoS(100, error("ConnectBlock(): too many sigops"),
REJECT_INVALID, "bad-blk-sigops");
if (!IgnoreSigopsLimits(pindex->nHeight))
if (nSigOps > MaxBlockSigOps(fDIP0001Active_context))
return state.DoS(100, error("ConnectBlock(): too many sigops"),
REJECT_INVALID, "bad-blk-sigops");
}

}
Expand Down Expand Up @@ -3450,19 +3452,22 @@ bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::P
}

// Check transactions
for (const auto& tx : block.vtx)
if (!CheckTransaction(*tx, state))
return state.Invalid(false, state.GetRejectCode(), state.GetRejectReason(),
strprintf("Transaction check failed (tx hash %s) %s", tx->GetHash().ToString(), state.GetDebugMessage()));

unsigned int nSigOps = 0;
for (const auto& tx : block.vtx)
{
nSigOps += GetLegacySigOpCount(*tx);
if (!IgnoreSigopsLimits(-1))
for (const auto& tx : block.vtx)
if (!CheckTransaction(*tx, state))
return state.Invalid(false, state.GetRejectCode(), state.GetRejectReason(),
strprintf("Transaction check failed (tx hash %s) %s", tx->GetHash().ToString(), state.GetDebugMessage()));

// Don't know height here, use failsafe
if (!IgnoreSigopsLimits(-1)) {
unsigned int nSigOps = 0;
for (const auto& tx : block.vtx) {
nSigOps += GetLegacySigOpCount(*tx);
}
// sigops limits (relaxed)
if (nSigOps > MaxBlockSigOps(true))
return state.DoS(100, false, REJECT_INVALID, "bad-blk-sigops", false, "out-of-bounds SigOpCount");
}
// sigops limits (relaxed)
if (nSigOps > MaxBlockSigOps(true))
return state.DoS(100, false, REJECT_INVALID, "bad-blk-sigops", false, "out-of-bounds SigOpCount");

if (fCheckPOW && fCheckMerkleRoot)
block.fChecked = true;
Expand Down Expand Up @@ -3570,8 +3575,9 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Co
}

// Check sigops
if (nSigOps > MaxBlockSigOps(fDIP0001Active_context))
return state.DoS(10, false, REJECT_INVALID, "bad-blk-sigops", false, "out-of-bounds SigOpCount");
if (!IgnoreSigopsLimits(nHeight))
if (nSigOps > MaxBlockSigOps(fDIP0001Active_context))
return state.DoS(10, false, REJECT_INVALID, "bad-blk-sigops", false, "out-of-bounds SigOpCount");

// Enforce rule that the coinbase starts with serialized block height
// After DIP3/DIP4 activation, we don't enforce the height in the input script anymore.
Expand Down Expand Up @@ -4911,6 +4917,18 @@ int CurrentProtocol() {
MIN_PEER_PROTO_VERSION_MAINNET);
}

//! Returns true if we can ignore sigops limits temporarily
bool fFailsafe = false;
bool IgnoreSigopsLimits(int nHeight) {
bool fIgnore = (nHeight >= CONSENSUS_SIGOPABUSE_START &&
nHeight <= CONSENSUS_SIGOPABUSE_FINISH);
if (nHeight == -1)
return fFailsafe;
else
fFailsafe = fIgnore;
return fIgnore;
}

class CMainCleanup
{
public:
Expand Down
6 changes: 6 additions & 0 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ struct LockPoints;

//! Blockheight to begin fake stake checks
static const int CONSENSUS_FAKESTAKE_HEIGHT = 1048576;
//! Blockheight range covering sigops abuse/spam
static const int CONSENSUS_SIGOPABUSE_START = 139900;
static const int CONSENSUS_SIGOPABUSE_FINISH = 165000;

/** Default for accepting alerts from the P2P network. */
static const bool DEFAULT_ALERTS = true;
Expand Down Expand Up @@ -590,4 +593,7 @@ bool LoadMempool();
//! Returns the current minimum protocol version in use
int CurrentProtocol();

//! Returns true if we can ignore sigops limits temporarily
bool IgnoreSigopsLimits(int nHeight);

#endif // BITCOIN_VALIDATION_H

0 comments on commit 3bcc1bb

Please sign in to comment.