Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Commit

Permalink
feat: remove protected storage/memory
Browse files Browse the repository at this point in the history
Integrates thefhevm-go version that no longer uses protected storage and
memory.
  • Loading branch information
dartdart26 committed Jun 11, 2024
1 parent 065c530 commit 90a6334
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 31 deletions.
7 changes: 5 additions & 2 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,15 +533,18 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,

// Create creates a new contract using code as deployment code.
func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
return fhevm.Create(evm.FhevmEnvironment(), caller.Address(), code, gas, value)
contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
return evm.create(caller, &codeAndHash{code: code}, gas, value, contractAddr, CREATE)
}

// Create2 creates a new contract using code as deployment code.
//
// The different between Create2 with Create is Create2 uses keccak256(0xff ++ msg.sender ++ salt ++ keccak256(init_code))[12:]
// instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.
func (evm *EVM) Create2(caller ContractRef, code []byte, gas uint64, endowment *big.Int, salt *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
return fhevm.Create2(evm.FhevmEnvironment(), caller.Address(), code, gas, endowment, salt)
codeAndHash := &codeAndHash{code: code}
contractAddr = crypto.CreateAddress2(caller.Address(), salt.Bytes32(), codeAndHash.Hash().Bytes())
return evm.create(caller, codeAndHash, gas, endowment, contractAddr, CREATE2)
}

// ChainConfig returns the environment's chain configuration
Expand Down
22 changes: 13 additions & 9 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,11 @@ func opMstore8(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]
}

func opSload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
return fhevm.OpSload(pc, interpreter.evm.FhevmEnvironment(), scope)
loc := scope.Stack.peek()
hash := common.Hash(loc.Bytes32())
val := interpreter.evm.StateDB.GetState(scope.Contract.Address(), hash)
loc.SetBytes(val.Bytes())
return nil, nil
}

func opSstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
Expand Down Expand Up @@ -676,8 +680,6 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byt
bigVal = value.ToBig()
}

verifiedBefore := fhevm.DelegateCiphertextHandlesInArgs(interpreter.evm.FhevmEnvironment(), args)
defer fhevm.RestoreVerifiedDepths(interpreter.evm.FhevmEnvironment(), verifiedBefore)
ret, returnGas, err := interpreter.evm.Call(scope.Contract, toAddr, args, gas, bigVal)

if err != nil {
Expand Down Expand Up @@ -714,8 +716,6 @@ func opCallCode(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
bigVal = value.ToBig()
}

verifiedBefore := fhevm.DelegateCiphertextHandlesInArgs(interpreter.evm.FhevmEnvironment(), args)
defer fhevm.RestoreVerifiedDepths(interpreter.evm.FhevmEnvironment(), verifiedBefore)
ret, returnGas, err := interpreter.evm.CallCode(scope.Contract, toAddr, args, gas, bigVal)
if err != nil {
temp.Clear()
Expand Down Expand Up @@ -772,8 +772,6 @@ func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)
// Get arguments from the memory.
args := scope.Memory.GetPtr(int64(inOffset.Uint64()), int64(inSize.Uint64()))

verifiedBefore := fhevm.DelegateCiphertextHandlesInArgs(interpreter.evm.FhevmEnvironment(), args)
defer fhevm.RestoreVerifiedDepths(interpreter.evm.FhevmEnvironment(), verifiedBefore)
ret, returnGas, err := interpreter.evm.StaticCall(scope.Contract, toAddr, args, gas)
if err != nil {
temp.Clear()
Expand All @@ -791,7 +789,10 @@ func opStaticCall(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)
}

func opReturn(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
return fhevm.OpReturn(pc, interpreter.evm.FhevmEnvironment(), scope), errStopToken
offset, size := scope.Stack.pop(), scope.Stack.pop()
ret := scope.Memory.GetPtr(int64(offset.Uint64()), int64(size.Uint64()))

return ret, errStopToken
}

func opRevert(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
Expand All @@ -814,7 +815,10 @@ func opSelfdestruct(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext
if interpreter.readOnly {
return nil, ErrWriteProtection
}
beneficiary, balance := fhevm.OpSelfdestruct(pc, interpreter.evm.FhevmEnvironment(), scope)
beneficiary := scope.Stack.pop()
balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address())
interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance)
interpreter.evm.StateDB.SelfDestruct(scope.Contract.Address())
if tracer := interpreter.evm.Config.Tracer; tracer != nil {
tracer.CaptureEnter(SELFDESTRUCT, scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance)
tracer.CaptureExit([]byte{}, 0, nil)
Expand Down
4 changes: 0 additions & 4 deletions core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
// Increment the call depth which is restricted to 1024
in.evm.depth++
defer func() {
_, span := otel.Tracer("fhevm").Start(ctx, "RemoveVerifiedCipherextsAtCurrentDepth")
fhevm.RemoveVerifiedCipherextsAtCurrentDepth(in.evm.FhevmEnvironment())
span.End()

in.evm.depth--
}()

Expand Down
21 changes: 5 additions & 16 deletions core/vm/operations_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
}
// TODO: For now, every SSTORE referring to a ciphertext incurs the same cost, irrespective
// of the original and current values of the storage slot. Refunds for ciphertexts are not taken into account.
if t := fhevm.GetTypeOfVerifiedCiphertext(evm.FhevmEnvironment(), value); t != nil {
cost += evm.fhevmEnvironment.params.GasCosts.ProtectedStorageSstoreGas[*t]
ct := fhevm.GetCiphertextFromMemory(evm.FhevmEnvironment(), value)
if ct != nil {
cost += evm.fhevmEnvironment.params.GasCosts.FheStorageSstoreGas[ct.Type()]
}
original := evm.StateDB.GetCommittedState(contract.Address(), x.Bytes32())
if original == current {
Expand Down Expand Up @@ -109,26 +110,14 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
func gasSLoadEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
loc := stack.peek()
slot := common.Hash(loc.Bytes32())

// Add cost if value is a ciphertext handle.
protectedStorageGas := uint64(0)
value := evm.StateDB.GetState(contract.Address(), slot)
// If not already loaded, try to load.
if fhevm.GetTypeOfVerifiedCiphertext(evm.FhevmEnvironment(), value) == nil {
t := fhevm.GetTypeOfPersistedCiphertext(evm.FhevmEnvironment(), contract.Address(), value)
if t != nil {
protectedStorageGas += evm.fhevmEnvironment.params.GasCosts.ProtectedStorageSloadGas[*t]
}
}

// Check slot presence in the access list
if _, slotPresent := evm.StateDB.SlotInAccessList(contract.Address(), slot); !slotPresent {
// If the caller cannot afford the cost, this change will be rolled back
// If he does afford it, we can skip checking the same thing later on, during execution
evm.StateDB.AddSlotToAccessList(contract.Address(), slot)
return params.ColdSloadCostEIP2929 + protectedStorageGas, nil
return params.ColdSloadCostEIP2929, nil
}
return params.WarmStorageReadCostEIP2929 + protectedStorageGas, nil
return params.WarmStorageReadCostEIP2929, nil
}

// gasExtCodeCopyEIP2929 implements extcodecopy according to EIP-2929
Expand Down

0 comments on commit 90a6334

Please sign in to comment.