<p><a href="https://en.bitcoin.it/wiki/Script">Quoting Bitcoin wiki:</a></p>

<blockquote>
  <p>A transaction is valid if nothing in the combined script triggers failure and the top stack item is true (non-zero). </p>
</blockquote>

<p><a href="https://github.com/bitcoin/bitcoin/blob/25cc1cf8dc03b9ba0ae886d354855adc207b5b6e/src/script/interpreter.cpp#L993">From the source code:</a></p>

<pre><code>if (CastToBool(stack.back()) == false)
    return false;
</code></pre>

<p>(In case you don't know what vector.back() does, <a href="http://www.cplusplus.com/reference/vector/vector/back/">it returns the last item</a>.)</p>

CC BY-SA 3.0
2014-10-26T15:42:11.200Z
They really coded up `== false`?
djechlin
CC BY-SA 3.0
2014-10-26T15:43:32.777Z
@djechlin CastToBool only returns true or false.
Nick ODell
CC BY-SA 3.0
2014-10-26T17:31:09.790Z
I think djechlin was surprised they didn't do if (!Cast... Or just return CastTo...
morsecoder
CC BY-SA 3.0
2014-10-26T15:01:41.023Z
2014-10-26T15:01:41.023Z
Nick ODell
<p>When executing bitcoin validation scripts, does the final stack have to end with just <code>true</code> on the stack, or can there be some elements under the <code>true</code>? In other words, are both of these final stack configurations valid, or just the first?</p>

<p>Stack1:</p>

<pre><code>true
</code></pre>

<p>Stack2:</p>

<pre><code>0 {pub_key} true
</code></pre>

<script>
Bitcoin Script Execution - Must end with just `true`?
morsecoder
# Bitcoin Script Execution - Must end with just `true`?

(Asked by: username_0 on 2014-10-26)

When executing bitcoin validation scripts, does the final stack have to end with just true on the stack, or can there be some elements under the true? In other words, are both of these final stack configurations valid, or just the first?
Stack1:
true

Stack2:
0 {pub_key} true

**Tags:** script


---

## 1 answers

### Answer by username_1 on 2014-10-26. Score: 5 | Accepted answer

Quoting Bitcoin wiki:

A transaction is valid if nothing in the combined script triggers failure and the top stack item is true (non-zero). 

From the source code:
if (CastToBool(stack.back()) == false)
    return false;

(In case you don't know what vector.back() does, it returns the last item.)

> **Comments:**
> **username_2**: They really coded up `== false`?  
> **username_1**: @username_2 CastToBool only returns true or false.  
> **username_0**: I think username_2 was surprised they didn't do if (!Cast... Or just return CastTo...  

---

