The Bitcoin Cash VM is strictly limited to prevent maliciously-designed transactions from requiring excessive resources during transaction validation.
Loops were originally excluded from this design on the mistaken assumption that they risk consuming excessive validation resources. In reality, a small number of VM operations make up the majority of resource usage during transaction validation, and any implementation of bounded loops can be many orders of magnitude faster than the most pathological VM bytecode constructions.
Introducing this basic control flow structure would make BCH contracts significantly more powerful and efficient.
This proposal includes two new BCH virtual machine (VM) operations, OP_BEGIN
and OP_UNTIL
, which enable a variety of loop constructions in BCH contracts without increasing the processing or memory requirements of the VM.
The precise implementation is influenced by the BEGIN ... UNTIL
indefinite loop construction in the Forth programming language (upon which the rest of the Bitcoin Cash VM is based). This particular construction offers maximal flexibility: definite loops can be easily emulated with these indefinite loops, but indefinite loops could not be emulated with definite loops.
Review and feedback are appreciated!