Tracing Stack State Step by Step
6. Tracing Stack State Step by Step
The Bitcoin Script Execution Model
Bitcoin Script is a stack-based, non-Turing-complete language. Execution proceeds left-to-right. Data-push opcodes push values onto the stack. Operational opcodes pop one or more values, process them, and push results. At the end of script execution, the script is considered valid if the top stack item is a non-zero, non-empty value.
There are two stacks: the main stack and the alt stack. The alt stack is used by OP_TOALTSTACK and OP_FROMALTSTACK.
P2PKH Execution Trace
Starting state:
scriptSig: <sig> <pubkey>
scriptPubKey: OP_DUP OP_HASH160 <hash> OP_EQUALVERIFY OP_CHECKSIG
Combined script (legacy execution): scriptSig first, then scriptPubKey.
| Step | Opcode | Operation | Stack (top → bottom) | |
Pro Tip
When debugging scripts, always start with a high-level disassembly before diving into the stack trace. Tools like bitcoin-cli decodescript are your first line of defense in identifying standard script patterns.
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: