Skip to content

Commit dcad332

Browse files
holimangzliudan
authored andcommitted
core/vm/program: evm bytecode-building utility (ethereum#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.
1 parent 627d77a commit dcad332

File tree

5 files changed

+884
-202
lines changed

5 files changed

+884
-202
lines changed

core/state/database.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ func NewDatabase(db ethdb.Database) Database {
132132
return NewDatabaseWithConfig(db, nil)
133133
}
134134

135+
// NewDatabaseForTesting is similar to NewDatabase, but it initializes the caching
136+
// db by using an ephemeral memory db with default config for testing.
137+
func NewDatabaseForTesting() Database {
138+
return NewDatabase(rawdb.NewMemoryDatabase())
139+
}
140+
135141
// NewDatabaseWithConfig creates a backing store for state. The returned database
136142
// is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
137143
// large memory cache.

0 commit comments

Comments
 (0)