05 - nVersion
Last updated
Last updated
nVersion is the first parameter in the transaction pre-image. It's value indicates the version of the node client that should be used to evaluate this transaction (4 bytes)
nVersion can be evaluated by splitting the first 4 bytes of the pre-image and moving the value to the top of the stack. In this way, a user can pre-set a version flag and enforce its use in a future transaction that spends the output.
This functionality was originally available in script using the OP_VER
, OP_VERIF
and OP_VERNOTIF
opcodes, however these have been disabled and are no-longer part of the scripting language.
In its original form a wallet could potentially construct a transaction that only a sub-set of nodes would try to put into a block. The functionality isn't used by miners today and so application of this technique is limited.
To replicate the funtionality of OP_VER, the following script can be used:
<tx_preimg>
...
Pre-image has been validated
<tx_preimg>
OP_4
add 4 to the stack
<tx_preimg> 4
OP_SPLIT
Split version from pre-image
<version> <r_tx_preimg>
OP_SWAP
Swap version to front, or drop if not needed
<r_tx_preimg> <version>
...
Script continues
To replicate the funtionality of OP_VERIF
or OP_VERNOTIF
, the following script outline can be used:
<tx_preimg>
...
Pre-image has been validated
<tx_preimg>
OP_4
add 4 to the stack
<r_tx_preimg> 0x04
OP_SPLIT
Split version from pre-image
<version> <r_tx_preimg>
OP_SWAP / OP_NIP
Swap version to front, or drop if not needed
<r_tx_preimg> <version>
<expected_version>
To test the version, add a constant to the stack.
<r_tx_preimg> <version> <expected_version>
OP_EQUAL
Equality test
<r_tx_preimg>
OP_IF / OP_NOTIF
Enter if statement path
<r_tx_preimg>
...
process
<r_tx_preimg>
OP_ELSE
or alternative path (optional)
<r_tx_preimg>
...
alternative process (optional)
<remainder_data_items>
OP_ENDIF
End process - If-structure script may leave one or more different data items on the stack. These are passed to the rest of the script.
<remainder_data_items>
...
rest of script