OP Stack Hacks are explicitly things that you can do with the OP Stack that are not currently intended for production use.OP Stack Hacks are not for the faint of heart. You will not be able to receive significant developer support for OP Stack Hacks — be prepared to get your hands dirty and to work without support.
op-geth/core/vm/contracts.go.
1
Add to `PrecompiledContractsBerlin` on line 82 (or a later fork, if the list of precompiles changes again)
- add a structure named after your new precompile, and
-
use an address that is unlikely to ever clash with a standard precompile and avoids the EIP-7587 reserved range (0x1337, for example):
2
Add the lines for the precompile
3
Stop `op-geth` and recompile:
4
Restart `op-geth`
5
Run these commands to see the result of calling the precompile successfully, and the result of an error:
How does it work?
This is the precompile interface definition:RequiredGaswhich returns the gas cost for the call. This function takes an array of bytes as input, and returns a single value, the gas cost.Runwhich runs the actual precompile. This function also takes an array of bytes, but it returns two values: the call’s output (a byte array) and an error.
map from addresses to the PrecompiledContract definitions:
common.BytesToAddress([]byte{<bytes to convert to address go here>}). In this case you have two bytes, 0x13 and 0x37. Together you get the address 0x0…1337.
The syntax for a precompiled contract interface is &<variable name>{}.
The next step is to define the precompiled contract itself.
cast) is going to have at least four bytes for the function signature.
return a, b is the way we return two values from a function in Go. The normal output is nil, nothing, because we return an error.