Skip to content

Commit

Permalink
Merge pull request ethereum#5 from zama-ai/petar/protected-storage-su…
Browse files Browse the repository at this point in the history
…icide

Destruct protected storage on `selfdestruct()`
  • Loading branch information
dartdart26 authored Dec 5, 2022
2 parents d053296 + 505c3b7 commit cebcc47
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,10 +973,13 @@ func opSelfdestruct(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext
if interpreter.readOnly {
return nil, ErrWriteProtection
}
protectedStorage := crypto.CreateProtectedStorageContractAddress(scope.Contract.Address())
beneficiary := scope.Stack.pop()
balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address())
balance.Add(balance, interpreter.evm.StateDB.GetBalance(protectedStorage))
interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance)
interpreter.evm.StateDB.Suicide(scope.Contract.Address())
interpreter.evm.StateDB.Suicide(protectedStorage)
if interpreter.cfg.Debug {
interpreter.cfg.Tracer.CaptureEnter(SELFDESTRUCT, scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance)
interpreter.cfg.Tracer.CaptureExit([]byte{}, 0, nil)
Expand Down
7 changes: 7 additions & 0 deletions tests/solidity/zama/handles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ contract HandleOwner is Precompiles {
uint256 public handle;
uint256 public bogus_handle = 42;
Callee callee;
address payable owner;

constructor(address callee_addr) {
callee = Callee(callee_addr);
owner = payable(msg.sender);
}

function store(bytes memory ciphertext) public {
Expand Down Expand Up @@ -89,6 +91,11 @@ contract HandleOwner is Precompiles {
uint256 h = handle + 1;
return h;
}

function destruct() public {
require(msg.sender == owner);
selfdestruct(owner);
}
}

contract Callee is Precompiles {
Expand Down

0 comments on commit cebcc47

Please sign in to comment.