<blockquote>
  <p>Does P2SH validation create a second stack using a copy of the redeem script, and then validate that stack before moving on to deserializing the redeem script (and then validating that script as normal)?</p>
</blockquote>

<p>Kind of. It really isn't a second stack but rather a copy of the original stack. Once the scriptPubKey execution is completed, the original stack before scriptPubKey execution is restored and then the redeemScript is executed.</p>

<p>The flow is something like this:</p>

<ol>
<li>Execute scriptSig which creates <code>stack</code></li>
<li>Copy <code>stack</code> to <code>stackCopy</code></li>
<li>Execute scriptPubKey using <code>stack</code></li>
<li>If P2SH, replace <code>stack</code> with <code>stackCopy</code></li>
<li>If P2SH, Pop top item off of <code>stack</code> and execute as redeemScript using <code>stack</code></li>
</ol>

CC BY-SA 3.0
2018-04-02T18:21:16.510Z
2018-04-02T18:26:22.543Z
2018-04-02T18:26:22.543Z
Ava Chow
<p>I'm trying to visualize how a typical P2SH script gets executed, as I realize that it's a special form of script that gets executed slightly differently to a standard script.</p>

<p><strong>Before P2SH:</strong></p>

<p>Before P2SH added new validation rules, nodes would execute the script like this:</p>

<p><img src="https://i.sstatic.net/aGYFI.gif" alt="Pre-P2SH"></p>

<p>In other words, an old node would just be happy to check the the HASH160 of the redeem script matches the HASH160 in the scriptPubKey. No MULTISIG validation would need to take place, because OP_1 (TRUE) would be left on top of the stack.</p>

<p><strong>After P2SH:</strong></p>

<p>With the new validation rules, I'm guessing the script executes something like this:</p>

<p><img src="https://i.sstatic.net/8GIXk.gif" alt="Post-P2SH"></p>

<p><strong>Question:</strong></p>

<p>Does P2SH validation <strong>create a second stack using a copy of the redeem script</strong>, and then validate that stack before moving on to deserializing the redeem script (and then validating that script as normal)?</p>

<p>I'm hoping that my visualization of P2SH execution isn't misleading.</p>

<script><p2sh>
How does a P2SH script get executed?
inersha
# How does a P2SH script get executed?

(Asked by: username_0 on 2018-04-02)

I'm trying to visualize how a typical P2SH script gets executed, as I realize that it's a special form of script that gets executed slightly differently to a standard script.

**Before P2SH:**

Before P2SH added new validation rules, nodes would execute the script like this:

![Pre-P2SH](https://i.sstatic.net/aGYFI.gif)

In other words, an old node would just be happy to check the the HASH160 of the redeem script matches the HASH160 in the scriptPubKey. No MULTISIG validation would need to take place, because OP\_1 (TRUE) would be left on top of the stack.

**After P2SH:**

With the new validation rules, I'm guessing the script executes something like this:

![Post-P2SH](https://i.sstatic.net/8GIXk.gif)

**Question:**

Does P2SH validation **create a second stack using a copy of the redeem script**, and then validate that stack before moving on to deserializing the redeem script (and then validating that script as normal)?

I'm hoping that my visualization of P2SH execution isn't misleading.

**Tags:** script, p2sh


---

## 1 answers

### Answer by username_1 on 2018-04-02. Score: 4 | Accepted answer

> Does P2SH validation create a second stack using a copy of the redeem script, and then validate that stack before moving on to deserializing the redeem script (and then validating that script as normal)?

Kind of. It really isn't a second stack but rather a copy of the original stack. Once the scriptPubKey execution is completed, the original stack before scriptPubKey execution is restored and then the redeemScript is executed.

The flow is something like this:

1. Execute scriptSig which creates `stack`
2. Copy `stack` to `stackCopy`
3. Execute scriptPubKey using `stack`
4. If P2SH, replace `stack` with `stackCopy`
5. If P2SH, Pop top item off of `stack` and execute as redeemScript using `stack`


---

