OP_RETURN

Revision as of 19:20, 8 January 2020 by Joshua Henslee (talk | contribs)

OP_RETURN is a script opcode used to end the script and return the top value on the stack. This opcode is analogous to the return function in programming languages. The OP_RETURN opcode has a controversial history in Bitcoin and as a result has been primarily used as a means of storing data on the blockchain.

In 2010 an exploit was discovered in Bitcoin where users could steal anyone's funds leveraging the OP_RETURN opcode. Satoshi Nakamoto modified the opcode to always return false as an urgent fix.

Storing data on-chain

Due to the functionality of this opcode being modified to always return false instead of the top value on the stack, nodes do not validate any opcodes or data in the script after the OP_RETURN.

The max script size was limited to 520 bytes in the Bitcoin client early on (possibly as a justification to prevent DoS attacks), rendering the OP_PUSHDATA opcodes useless. Thus, users of the network leveraged this opcode to store arbitrary data in scripts.

Return of OP_RETURN

The Genesis upgrade in February 2020 restores the original functionality of the opcode. This will not impact the ability to store arbitrary data on-chain, as it is advised to now use OP_FALSE OP_RETURN in scripts that still seek to do so.

Post Genesis, the script:

OP_FALSE OP_RETURN

will return false and terminate the script as originally designed.

The Genesis upgrade essentially re-enables the original functionality of the push data opcodes via lifting the maximum transaction size such that OP_RETURN is no longer the only means of pushing data onto the stack. Given this, the push data opcodes should be used in lieu of OP_RETURN.