go-ethereum/core/vm/program
Martin HS 6d3d252a5e
core/vm/program: evm bytecode-building utility (#30725)
In many cases, there is a need to create somewhat nontrivial bytecode. A
recent example is the verkle statetests, where we want a `CREATE2`- op
to create a contract, which can then be invoked, and when invoked does a
selfdestruct-to-self.

It is overkill to go full solidity, but it is also a bit tricky do
assemble this by concatenating bytes. This PR takes an approach that
has been used in in goevmlab for several years.

Using this utility, the case can be expressed as: 
```golang
	// Some runtime code
	runtime := program.New().Ops(vm.ADDRESS, vm.SELFDESTRUCT).Bytecode()
	// A constructor returning the runtime code
	initcode := program.New().ReturnData(runtime).Bytecode()
	// A factory invoking the constructor
	outer := program.New().Create2AndCall(initcode, nil).Bytecode()
```

We have a lot of places in the codebase where we concatenate bytes, cast
from `vm.OpCode` . By taking tihs approach instead, thos places can be made a
bit more maintainable/robust.
2024-11-20 08:40:21 +01:00
..
program.go core/vm/program: evm bytecode-building utility (#30725) 2024-11-20 08:40:21 +01:00
program_test.go core/vm/program: evm bytecode-building utility (#30725) 2024-11-20 08:40:21 +01:00
readme.md core/vm/program: evm bytecode-building utility (#30725) 2024-11-20 08:40:21 +01:00

readme.md

What is this

In many cases, we have a need to create somewhat nontrivial bytecode, for testing various quirks related to state transition or evm execution.

For example, we want to have a CREATE2- op create a contract, which is then invoked, and when invoked does a selfdestruct-to-self.

It is overkill to go full solidity, but it is also a bit tricky do assemble this by concatenating bytes.

This utility takes an approach from goevmlab where it has been used for several years, a go-lang utility to assemble evm bytecode.

Using this utility, the case above can be expressed as:

	// Some runtime code
	runtime := program.New().Ops(vm.ADDRESS, vm.SELFDESTRUCT).Bytecode()
	// A constructor returning the runtime code
	initcode := program.New().ReturnData(runtime).Bytecode()
	// A factory invoking the constructor
	outer := program.New().Create2AndCall(initcode, nil).Bytecode()

Warning

This package is a utility for testing, not for production. As such:

  • There are not package guarantees. We might iterate heavily on this package, and do backwards-incompatible changes without warning
  • There are no quality-guarantees. These utilities may produce evm-code that is non-functional. YMMV.
  • There are no stability-guarantees. The utility will panic if the inputs do not align / make sense.