<p>The behaviour of the mempool or orphan tx pool should not influence the validity of blocks at all.</p>

<p>When a block comes in:</p>

<ul>
<li>main.cpp:<strong>ProcessMessage</strong> deals with processing messages, and dispatches to:</li>
<li>main.cpp:<strong>ProcessNewBlock</strong> deals with specifically processing block messages, which stores it on disk using AcceptBlock and then calls:</li>
<li>main.cpp:<strong>ActivateBestChain</strong> tries to verify the potentially new best chain and switch to it. For every atomic "reorganization" step, it calls:</li>
<li>main.cpp:<strong>ActivatebestChainStep</strong> which will try to verify new blocks to be added to the tip of the chain using:</li>
<li>main.cpp:<strong>ConnectTip</strong> which does the actual processing using:</li>
<li>main.cpp:<strong>ConnectBlock</strong>, which will iterate through the block's transactions and verify their inputs using:</li>
<li>main.cpp:<strong>CheckInputs</strong> which builds up a list of CScriptCheck objects for each script execution to be performed, which are verified from another thread. ConnectBlock waits for these threads to finish and deal with the result. In those threads, we call:</li>
<li>main.cpp:<strong>CScriptCheck::operator()</strong> which performs a single encapsulated script execution, using:</li>
<li>script/interpreter.cpp:<strong>VerifyScript</strong> to do a verification of script, which consists of executing a scriptPubKey + its spending scriptSig using:</li>
<li>script/interpreter.cpp:<strong>EvalScript</strong>.</li>
</ul>

<p>So, EvalScript() should be executed for all scriptPubKeys which are spent by a block, before that blocks ends up being accepted as a tip.</p>

CC BY-SA 3.0
2015-10-24T18:07:23.820Z
2015-10-24T18:07:23.820Z
Pieter Wuille
<p>Today I was reading bitcoind src, and trying to find out where scriptPubKey is executed when a node received new block with transaction, for example transaction from new block may not be exist in mempool.</p>

<ol>
<li><p>Find function that execute script:</p>

<p><a href="https://github.com/bitcoin/bitcoin/search?utf8=%E2%9C%93&amp;q=EvalScript" rel="nofollow">https://github.com/bitcoin/bitcoin/search?utf8=%E2%9C%93&amp;q=EvalScript</a></p>

<ul>
<li><strong>src/script/interpreter.cpp</strong>   in this file source code of EvalScript function</li>
</ul>

<p>Used only in this 2 files:</p>

<ul>
<li><p><strong>src/policy/policy.cpp</strong></p></li>
<li><p><strong>src/script/sign.cpp</strong></p></li>
</ul></li>
<li><p>Review <strong>sign.cpp</strong></p>

<p>Evalscript -> CombineSignatures -> <strong>/src/bitcoin-tx.cpp</strong>: MutateTxSign -> MutateTx -> CommandLineRawTx -> main  </p>

<p>This functions used when we create new transactions</p></li>
<li><p>Review   <strong>policy.cpp</strong></p>

<p>Evalscript -> AreInputsStandard -> <strong>/src/main.cpp: AcceptToMemoryPool</strong></p>

<p>AcceptToMemoryPool</p>

<ul>
<li><p><strong>src/main.cpp</strong>   later</p></li>
<li><p><strong>src/wallet/wallet.cpp</strong>   -> CommitTransaction    -  this is create own transaction functional </p></li>
<li><p><strong>src/rpcrawtransaction.cpp</strong> -> sendrawtrnsaction - rpc function send rawtx</p></li>
<li><p><strong>src/txmempool.cpp</strong>  -> no calls only comments</p></li>
</ul></li>
</ol>

<p>Last one is  <strong>src/main.cpp</strong></p>

<p>AcceptToMemoryPool -> ProcessMessage  on message  "tx" , new single tx to pool received
  AcceptToMemoryPool -> DisconnectTip</p>

<p><strong>DisconnectTip -> InvalidateBlock</strong>    invaludate block and send all transaction back to pool with all verifications and script eval</p>

<p><strong>DisconnectTip -> ActivateBestChainStep</strong> :</p>

<pre><code>  // Disconnect active blocks which are no longer in the best chain.
bool fBlocksDisconnected = false;
while (chainActive.Tip() &amp;&amp; chainActive.Tip() != pindexFork) {
    if (!DisconnectTip(state))
        return false;
    fBlocksDisconnected = true;
}
</code></pre>

<p>If we have orphan take out transactions from orphan to pool with all verifications</p>

<p>NO more other links found</p>

<p>From this I can conclude that in case node with bitcoind received new block 
with transactions that not in mempool we add this tx to blockchain without scriptPubKey executing.</p>

<p>Consequently if same "honest" miner include tx with invalid script, at example incorrect signature that spent same one coins, all bitcoind nodes add it to blockchain without eval script and not detect this</p>

<p>It is not possible to be true!  Where is my error?</p>

<attack><mining-software>
Is the scriptPubKey of a previously unseen transaction in an incoming block checked? If so, where?
bitaps.com
# Is the scriptPubKey of a previously unseen transaction in an incoming block checked? If so, where?

(Asked by: username_0 on 2015-10-24)

Today I was reading bitcoind src, and trying to find out where scriptPubKey is executed when a node received new block with transaction, for example transaction from new block may not be exist in mempool.

1. Find function that execute script:

   <https://github.com/bitcoin/bitcoin/search?utf8=%E2%9C%93&q=EvalScript>

   * **src/script/interpreter.cpp** in this file source code of EvalScript function

   Used only in this 2 files:

   * **src/policy/policy.cpp**
   * **src/script/sign.cpp**
2. Review **sign.cpp**

   Evalscript -> CombineSignatures -> **/src/bitcoin-tx.cpp**: MutateTxSign -> MutateTx -> CommandLineRawTx -> main

   This functions used when we create new transactions
3. Review **policy.cpp**

   Evalscript -> AreInputsStandard -> **/src/main.cpp: AcceptToMemoryPool**

   AcceptToMemoryPool

   * **src/main.cpp** later
   * **src/wallet/wallet.cpp** -> CommitTransaction - this is create own transaction functional
   * **src/rpcrawtransaction.cpp** -> sendrawtrnsaction - rpc function send rawtx
   * **src/txmempool.cpp** -> no calls only comments

Last one is **src/main.cpp**

AcceptToMemoryPool -> ProcessMessage on message "tx" , new single tx to pool received
AcceptToMemoryPool -> DisconnectTip

**DisconnectTip -> InvalidateBlock** invaludate block and send all transaction back to pool with all verifications and script eval

**DisconnectTip -> ActivateBestChainStep** :

```
  // Disconnect active blocks which are no longer in the best chain.
bool fBlocksDisconnected = false;
while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
    if (!DisconnectTip(state))
        return false;
    fBlocksDisconnected = true;
}

```

If we have orphan take out transactions from orphan to pool with all verifications

NO more other links found

From this I can conclude that in case node with bitcoind received new block
with transactions that not in mempool we add this tx to blockchain without scriptPubKey executing.

Consequently if same "honest" miner include tx with invalid script, at example incorrect signature that spent same one coins, all bitcoind nodes add it to blockchain without eval script and not detect this

It is not possible to be true! Where is my error?

**Tags:** attack, mining-software


---

## 1 answers

### Answer by username_1 on 2015-10-24. Score: 6 | Accepted answer

The behaviour of the mempool or orphan tx pool should not influence the validity of blocks at all.

When a block comes in:

* main.cpp:**ProcessMessage** deals with processing messages, and dispatches to:
* main.cpp:**ProcessNewBlock** deals with specifically processing block messages, which stores it on disk using AcceptBlock and then calls:
* main.cpp:**ActivateBestChain** tries to verify the potentially new best chain and switch to it. For every atomic "reorganization" step, it calls:
* main.cpp:**ActivatebestChainStep** which will try to verify new blocks to be added to the tip of the chain using:
* main.cpp:**ConnectTip** which does the actual processing using:
* main.cpp:**ConnectBlock**, which will iterate through the block's transactions and verify their inputs using:
* main.cpp:**CheckInputs** which builds up a list of CScriptCheck objects for each script execution to be performed, which are verified from another thread. ConnectBlock waits for these threads to finish and deal with the result. In those threads, we call:
* main.cpp:**CScriptCheck::operator()** which performs a single encapsulated script execution, using:
* script/interpreter.cpp:**VerifyScript** to do a verification of script, which consists of executing a scriptPubKey + its spending scriptSig using:
* script/interpreter.cpp:**EvalScript**.

So, EvalScript() should be executed for all scriptPubKeys which are spent by a block, before that blocks ends up being accepted as a tip.


---

