mirror of
https://github.com/ChristopherA/Learning-Bitcoin-from-the-Command-Line.git
synced 2025-06-08 00:16:26 +00:00
Update 7_2_Running_a_Bitcoin_Script.md
This commit is contained in:
parent
5ba1e9eb58
commit
25e703b6dd
@ -10,11 +10,11 @@ A Bitcoin Script has three parts: it has a line of input; it has a stack for sto
|
||||
|
||||
### Understand the Ordering
|
||||
|
||||
Bitcoin Scripts are run from left to right. That sounds easy enough, because it's the same way you read. However, it might actually be the most non-intuitive element of Bitcoin Script, because it means that functions don't look as you'd expect. Instead, _the operands go before the operator._
|
||||
Bitcoin Scripts are run from left to right. That sounds easy enough, because it's the same way you read. However, it might actually be the most non-intuitive element of Bitcoin Script, because it means that functions don't look like you'd expect. Instead, _the operands go before the operator._
|
||||
|
||||
For example, if you were adding together "1" and "2", your Bitcoin Script for that would be `1 2 OP_ADD`, _not_ "1 + 2". Since we know that OP_ADD operator takes two inputs, we know that the two inputs before it are its operands. (At least that's true at the most basic level of Scripting.)
|
||||
For example, if you were adding together "1" and "2", your Bitcoin Script for that would be `1 2 OP_ADD`, _not_ "1 + 2". Since we know that OP_ADD operator takes two inputs, we know that the two inputs before it are its operands.
|
||||
|
||||
> **WARNING:** Technically, everything in Bitcoin Script is an opcode, thus it would be most appropriate to record the above example as `OP_1 OP_2 OP_ADD`. We leave the "OP" prefix off constants, but include it on all actual operators. Some writers prefer to also leave the "OP" prefix off operators, but we have opted not to.
|
||||
> **WARNING:** Technically, everything in Bitcoin Script is an opcode, thus it would be most appropriate to record the above example as `OP_1 OP_2 OP_ADD`. In our examples, we don't worry about how the constants will be evaluated, as that's a topic of translation, as is explained in [§8.2: Building the Structure of P2SH](8_2_Building_the_Structure_of_P2SH.md). Some writers prefer to also leave the "OP" prefix off operators, but we have opted not to.
|
||||
|
||||
### Understand the Stack
|
||||
|
||||
@ -22,7 +22,7 @@ It's actually not quite correct to say that an operator applies to the inputs be
|
||||
|
||||
_What is a stack?_ A stack is a LIFO (last-in-first-out) data structure. It has two access functions: push and pop. Push places a new object on top of the stack, pushing down everything below it. Pop removes the top object from the stack.
|
||||
|
||||
Whenever Bitcoin Script encounters a constant, it pushes it on the stack. So the above example of `1 2 OP_ADD1` would actually look like this as it was processed:
|
||||
Whenever Bitcoin Script encounters a constant, it pushes it on the stack. So the above example of `1 2 OP_ADD` would actually look like this as it was processed:
|
||||
```
|
||||
Script: 1 2 OP_ADD
|
||||
Stack: [ ]
|
||||
@ -70,7 +70,7 @@ Stack: [ 1 ]
|
||||
|
||||
## Understand the Usage of Bitcoin Script
|
||||
|
||||
That's pretty much Bitcoin Scripting ... other than a few intricacies for how this Scripting language interacts with Bitcoin itself.
|
||||
That's pretty much Bitcoin Scripting ... other than a few intricacies for how this scripting language interacts with Bitcoin itself.
|
||||
|
||||
### Understand scriptSig and scriptPubKey
|
||||
|
||||
@ -96,11 +96,11 @@ Script: OP_EQUAL
|
||||
Stack: [ 99 99 ]
|
||||
|
||||
Script:
|
||||
Stack: [ TRUE ]
|
||||
Stack: [ True ]
|
||||
```
|
||||
This abstraction isn't quite accurate: for security reasons, the `scriptSig` is run, then the contents of the stack are transferred for the `scriptPubKey` to run, but it's accurate enough for understanding how the key of `scriptSig` fits into the lock of `scriptPubKey`.
|
||||
|
||||
> **WARNING** The above is a non-standard transaction type. It would not actually be accepted by nodes running Bitcoin Core with the standard settings. [§8.1: Building a Bitcoin Script with P2SH](8_1_Building_a_Bitcoin_Script_with_P2SH.md) discusses how you actually _could_ use a Bitcoin Script like this, using the power of P2SH.
|
||||
> **WARNING** The above is a non-standard transaction type. It would not actually be accepted by nodes running Bitcoin Core with the standard settings. [§8.1: Building a Bitcoin Script with P2SH](8_1_Building_a_Bitcoin_Script_with_P2SH.md) discusses how you actually _could_ use a Bitcoin Script like this, via the power of P2SH.
|
||||
|
||||
### Get the Results
|
||||
|
||||
@ -109,7 +109,7 @@ Bitcoin will verify a transaction and allow the UTXO to be respent if two criter
|
||||
1. The execution did not get marked as invalid at any point, for example with a failed OP_VERIFY or the usage of a disabled opcode.
|
||||
2. The top item in the stack at the end of execution is true (non-zero).
|
||||
|
||||
In the above example, the transaction would succeed because the stack has a `TRUE` at its top. But, it would be just as permissible to end with a full stack and the number `42` on top.
|
||||
In the above example, the transaction would succeed because the stack has a `True` at its top. But, it would be just as permissible to end with a full stack and the number `42` on top.
|
||||
|
||||
## Summary: Running a Bitcoin Script
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user