Integration
This document is based on go-ethereum v1.13.5
Steps
Step 1: update core/state_transition.go
core/state_transition.go
replace the last return with:
which will impact tests as the returned gas wonโt be the same.
Step 2: update core/vm/contracts.go
core/vm/contracts.go
update the PrecompiledContract
interface to:
Update all previous uses of this interface
Add:
to all precompiled contract maps (e.g. PrecompiledContractsHomestead
)
We used 93 as the address of the precompile here, but you can choose any other address as far as client libraries know where to find it.
Implement the fheLib
precompile
fheLib
precompileRewrite RunPrecompiledContract
RunPrecompiledContract
Step 3: update core/vm/errors.go
core/vm/errors.go
Register errors at initialization in fhevm-go
to be recognized at runtime
fhevm-go
to be recognized at runtimeStep 4: update core/vm/evm.go
core/vm/evm.go
Update EVM
struct with new fields
EVM
struct with new fieldsWhile implementing fhevmEnvironment
as:
Update NewEVM
In:
Initialize
isGasEstimation
usingconfig.IsGasEstimation
Initialize
isEthCall
usingconfig.IsEthCall
Initialize
fhevmEnvironment
withFhevmImplementation{interpreter: nil, logger: &fhevm.DefaultLogger{}, data: fhevm.NewFhevmData(), params: fhevm.DefaultFhevmParams()}
After initializing
evm.interpreter
make sure to pointfhevmEnvironment
to itevm.fhevmEnvironment.interpreter = evm.interpreter
then initialize itfhevm.InitFhevm(&evm.fhevmEnvironment)
Update RunPrecompiledContract
After changing precompiled contract interface in 2, we have to change usages of:
to:
Rewrite Create
and Create2
by a call to their fhevm implementation
Create
and Create2
by a call to their fhevm implementationImplement EVMEnvironment interface
Now implement the fhevm.EVMEnvironment
interface for FhevmImplementation
:
Step 5: update core/vm/instructions.go
core/vm/instructions.go
Update opSload
, opSstore
and opReturn
opSload
, opSstore
and opReturn
Rewrite opSload
, opSstore
and opReturn
by a call to their fhevm implementation:
Update opCall
, opCallCode
and opStaticCall
opCall
, opCallCode
and opStaticCall
In opCall
, opCallCode
and opStaticCall
, add lines to delegate ciphertexts before the call and to restore at the end of function (using defer
)
There might be other functions that calls other contracts (e.g. opDelegateCall
), in which case you will also need to do the same modifications as below. Basically, anything that will execute code after incrementing the depth would need this.
Add the 2 following lines:
The call function is named differently in the 3 functions to update:
Update opSelfdestruct
opSelfdestruct
In:
Replace the following lines:
with this call to the fhevm:
Step 6: update core/vm/interpreter.go
core/vm/interpreter.go
Update Config
struct with new fields
Config
struct with new fieldsImplements the GetMemory
, GetStack
and GetContract
methods
GetMemory
, GetStack
and GetContract
methodsUpdate Run
method
Run
methodIn:
Add the deletion of verified ciphertexts at current depth in the
defer
at the top:
Step 7: update core/vm/stack.go
core/vm/stack.go
Implement the following methods
Step 8: update internal/ethapi/api.go
internal/ethapi/api.go
Add
isGasEstimation, isEthCall bool
arguments tofunc doCall
and pass them invm.Config
during EVM creation:
Add
isGasEstimation, isEthCall bool
arguments tofunc DoCall
and forward them in the call todoCall
Update usages of
doCall
andDoCall
by simply settingIsEthCall
totrue
when itโs a call, andIsGasEstimation
totrue
when itโs estimating gas
Step 9: update graphql/graphql.go
graphql/graphql.go
Update usages of doCall
and DoCall
by simply setting IsEthCall
to true
when itโs a call, and IsGasEstimation
to true
when itโs estimating gas
Last updated