<p>The term &quot;consensus checks&quot; refers to the rules that determine whether a block is valid; specifically, if a consensus check on a block (or a transaction contained within a block) fails, then the block is considered invalid.  If we get to the end of the block and none of the consensus checks has failed, then the block is valid.</p>
<p>Policy checks are a superset of consensus checks (meaning that policy checks are in addition to consensus checks, so that passing policy checks implies passing consensus checks), and policy checks only apply to transactions that are being considered for addition to the mempool. The purpose of policy checks is generally to (a) close off DoS vectors and (b) to make future consensus changes safer to deploy, by preventing relay of transactions that would violate such future consensus changes well in advance.</p>
<p>You're specifically asking about transaction script checks, but note that there are many policy (and consensus) checks on transactions that occur outside of just script validation.  Also, the links in your question are to the &quot;policy/&quot; and &quot;consensus/&quot; subdirectories in Bitcoin Core's source tree, but there are many policy and consensus checks that happen outside of those directories.</p>
<p>Here are some examples of policy checks in script validation, that are not (yet) consensus checks. These first ones are from BIP62, which proposed a set of rules to limit transaction malleability.</p>
<ul>
<li><a href="https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/script/interpreter.h#L48-L51" rel="noreferrer">STRICTENC</a></li>
<li><a href="https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/script/interpreter.h#L56-L58" rel="noreferrer">LOW_S</a></li>
<li><a href="https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/script/interpreter.h#L63-L64" rel="noreferrer">SIG_PUSHONLY</a></li>
<li><a href="https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/script/interpreter.h#L66-L70" rel="noreferrer">MINIMALDATA</a></li>
<li><a href="https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/script/interpreter.h#L84-L91" rel="noreferrer">CLEANSTACK</a></li>
</ul>
<p>Here are some policy checks relating to making softfork deployment safe in the future:</p>
<ul>
<li><a href="https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/script/interpreter.h#L74-L82" rel="noreferrer">DISCOURAGE_UPGRADABLE_NOPS</a></li>
<li><a href="https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/script/interpreter.h#L107-L109" rel="noreferrer">DISCOURAGE_UPGRADABLE_WITNESS</a></li>
</ul>
<p>There are some miscellaneous other policy script checks, such as requiring that all pubkeys in segwit scripts be compressed, or that inputs to OP_IF/OP_NOTIF be minimally encoded. And then of course all future consensus changes (such as Taproot changes) that have not yet been activated on the network are policy checks as well. An (almost) full list of what transaction script validation checks are performed as part of policy checks when a transaction is being validated for the mempool can be found <a href="https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/policy/policy.h#L56-L79" rel="noreferrer">here</a>. (Note that this list includes consensus checks as well.)</p>
<p>However that is not quite a complete set of transaction script validation rules that are enforced as policy checks. During mempool acceptance, we perform other checks on a transaction as well, some of which depend on a transaction's scripts. Some examples of these include:</p>
<ul>
<li>Non-p2sh, non-segwit spends must be P2PKH, P2PK, or bare multisig</li>
<li>scriptSig or witness must be no more than a given size</li>
<li>Various rules on outputs created (no more than one OP_RETURN, must be P2SH/v0 segwit/P2PKH/P2PK/MULTISIG, value must be above dust threshold)</li>
<li>Limits on how many signature checks are performed by the transaction</li>
</ul>
<p>There are a lot of these to list, but tracing the code in mempool acceptance (particularly <a href="https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/validation.cpp#L550" rel="noreferrer">here</a>) and inspecting functions like <code>CheckTransaction</code>, <code>IsStandardTx</code>, and <code>CheckTxInputs</code> should yield the remaining policy script checks, on top of the ones enforced inside the script interpreter.</p>
<p>Transaction script consensus checks are a much smaller set. Some of the script flags that are now consensus checks include:</p>
<ul>
<li>P2SH</li>
<li>DERSIG</li>
<li>NULLDUMMY</li>
<li>CHECKLOCKTIMEVERIFY</li>
<li>CHECKSEQUENCEVERIFY</li>
<li>WITNESS</li>
</ul>
<p>The full list (with enabling conditions) can be seen <a href="https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/validation.cpp#L1822-L1871" rel="noreferrer">here</a>. Note that as consensus rules have changed in Bitcoin's history, not all the script flags mentioned in that function are enabled on all blocks.</p>
<p>Most (all?) of the remaining consensus checks performed on transactions can be found in <a href="https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/validation.cpp#L1887-L2188" rel="noreferrer"><code>ConnectBlock()</code></a>, <a href="https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/validation.cpp#L3273" rel="noreferrer"><code>CheckBlock()</code></a>, and <a href="https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/validation.cpp#L3466-L3546" rel="noreferrer"><code>ContextualCheckBlock()</code></a>.</p>

CC BY-SA 4.0
2020-12-02T15:53:46.697Z
2020-12-02T16:13:08.083Z
2020-12-02T16:13:08.083Z
sdaftuar
<p>What is the difference between <a href="https://github.com/bitcoin/bitcoin/tree/master/src/policy" rel="nofollow noreferrer">policy</a> and <a href="https://github.com/bitcoin/bitcoin/tree/master/src/consensus" rel="nofollow noreferrer">consensus</a> when it comes to a Bitcoin Core node validating transaction scripts? Can you give some examples of checks that are considered policy and checks that are considered consensus?</p>
<p>This question was asked during <a href="https://bitcoincore.reviews/19698" rel="nofollow noreferrer">this</a> Bitcoin Core PR review club session on <a href="https://github.com/bitcoin/bitcoin/pull/19698" rel="nofollow noreferrer">PR 19698</a>.</p>
<p>There was further discussion in <a href="https://bitcoincore.reviews/22871" rel="nofollow noreferrer">this</a> Bitcoin Core PR review club on <a href="https://github.com/bitcoin/bitcoin/pull/22871" rel="nofollow noreferrer">PR 22871</a> and there is a section on policy in this Gloria Zhao <a href="https://gist.github.com/glozow/dc4e9d5c5b14ade7cdfac40f43adb18a#policy" rel="nofollow noreferrer">gist</a>.</p>

<script><bitcoin-core-development><consensus><mempool><p2p>
What is the difference between policy and consensus when it comes to a Bitcoin Core node validating scripts?
Michael Folkson
# What is the difference between policy and consensus when it comes to a Bitcoin Core node validating scripts?

(Asked by: username_0 on 2020-12-02)

What is the difference between [policy](https://github.com/bitcoin/bitcoin/tree/master/src/policy) and [consensus](https://github.com/bitcoin/bitcoin/tree/master/src/consensus) when it comes to a Bitcoin Core node validating transaction scripts? Can you give some examples of checks that are considered policy and checks that are considered consensus?

This question was asked during [this](https://bitcoincore.reviews/19698) Bitcoin Core PR review club session on [PR 19698](https://github.com/bitcoin/bitcoin/pull/19698).

There was further discussion in [this](https://bitcoincore.reviews/22871) Bitcoin Core PR review club on [PR 22871](https://github.com/bitcoin/bitcoin/pull/22871) and there is a section on policy in this Gloria Zhao [gist](https://gist.github.com/glozow/dc4e9d5c5b14ade7cdfac40f43adb18a#policy).

**Tags:** script, bitcoin-core-development, consensus, mempool, p2p


---

## 1 answers

### Answer by username_1 on 2020-12-02. Score: 7 | Accepted answer

The term "consensus checks" refers to the rules that determine whether a block is valid; specifically, if a consensus check on a block (or a transaction contained within a block) fails, then the block is considered invalid. If we get to the end of the block and none of the consensus checks has failed, then the block is valid.

Policy checks are a superset of consensus checks (meaning that policy checks are in addition to consensus checks, so that passing policy checks implies passing consensus checks), and policy checks only apply to transactions that are being considered for addition to the mempool. The purpose of policy checks is generally to (a) close off DoS vectors and (b) to make future consensus changes safer to deploy, by preventing relay of transactions that would violate such future consensus changes well in advance.

You're specifically asking about transaction script checks, but note that there are many policy (and consensus) checks on transactions that occur outside of just script validation. Also, the links in your question are to the "policy/" and "consensus/" subdirectories in Bitcoin Core's source tree, but there are many policy and consensus checks that happen outside of those directories.

Here are some examples of policy checks in script validation, that are not (yet) consensus checks. These first ones are from BIP62, which proposed a set of rules to limit transaction malleability.

* [STRICTENC](https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/script/interpreter.h#L48-L51)
* [LOW\_S](https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/script/interpreter.h#L56-L58)
* [SIG\_PUSHONLY](https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/script/interpreter.h#L63-L64)
* [MINIMALDATA](https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/script/interpreter.h#L66-L70)
* [CLEANSTACK](https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/script/interpreter.h#L84-L91)

Here are some policy checks relating to making softfork deployment safe in the future:

* [DISCOURAGE\_UPGRADABLE\_NOPS](https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/script/interpreter.h#L74-L82)
* [DISCOURAGE\_UPGRADABLE\_WITNESS](https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/script/interpreter.h#L107-L109)

There are some miscellaneous other policy script checks, such as requiring that all pubkeys in segwit scripts be compressed, or that inputs to OP\_IF/OP\_NOTIF be minimally encoded. And then of course all future consensus changes (such as Taproot changes) that have not yet been activated on the network are policy checks as well. An (almost) full list of what transaction script validation checks are performed as part of policy checks when a transaction is being validated for the mempool can be found [here](https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/policy/policy.h#L56-L79). (Note that this list includes consensus checks as well.)

However that is not quite a complete set of transaction script validation rules that are enforced as policy checks. During mempool acceptance, we perform other checks on a transaction as well, some of which depend on a transaction's scripts. Some examples of these include:

* Non-p2sh, non-segwit spends must be P2PKH, P2PK, or bare multisig
* scriptSig or witness must be no more than a given size
* Various rules on outputs created (no more than one OP\_RETURN, must be P2SH/v0 segwit/P2PKH/P2PK/MULTISIG, value must be above dust threshold)
* Limits on how many signature checks are performed by the transaction

There are a lot of these to list, but tracing the code in mempool acceptance (particularly [here](https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/validation.cpp#L550)) and inspecting functions like `CheckTransaction`, `IsStandardTx`, and `CheckTxInputs` should yield the remaining policy script checks, on top of the ones enforced inside the script interpreter.

Transaction script consensus checks are a much smaller set. Some of the script flags that are now consensus checks include:

* P2SH
* DERSIG
* NULLDUMMY
* CHECKLOCKTIMEVERIFY
* CHECKSEQUENCEVERIFY
* WITNESS

The full list (with enabling conditions) can be seen [here](https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/validation.cpp#L1822-L1871). Note that as consensus rules have changed in Bitcoin's history, not all the script flags mentioned in that function are enabled on all blocks.

Most (all?) of the remaining consensus checks performed on transactions can be found in [`ConnectBlock()`](https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/validation.cpp#L1887-L2188), [`CheckBlock()`](https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/validation.cpp#L3273), and [`ContextualCheckBlock()`](https://github.com/bitcoin/bitcoin/blob/0a13d15c143906bb6fd5d30c25df03ce5e20418d/src/validation.cpp#L3466-L3546).


---

