Skip to content

Commit

Permalink
fix SELFDESTRUCT witness recording
Browse files Browse the repository at this point in the history
Signed-off-by: Ignacio Hagopian <[email protected]>
  • Loading branch information
jsign committed Feb 15, 2024
1 parent a8db450 commit 3f562b4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
8 changes: 8 additions & 0 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,14 @@ func (s *StateDB) Selfdestruct6780(addr common.Address) {
}
}

func (s *StateDB) WasCreatedInCurrentTx(addr common.Address) bool {
stateObject := s.getStateObject(addr)
if stateObject == nil {
return false
}
return stateObject.created
}

// SetTransientState sets transient storage for a given account. It
// adds the change to the journal so that it can be rolled back
// to its previous value if there is a revert.
Expand Down
16 changes: 16 additions & 0 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,22 @@ func opSelfdestruct(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext
tracer.CaptureEnter(SELFDESTRUCT, scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance)
tracer.CaptureExit([]byte{}, 0, nil)
}
if interpreter.evm.chainRules.IsPrague {
contractAddr := scope.Contract.Address()
beneficiaryAddr := beneficiary.Bytes20()
// If the beneficiary isn't the contract, we need to touch the beneficiary's balance.
// If the beneficiary is the contract itself, there're two possibilities:
// 1. The contract was created in the same transaction: the balance is already touched (no need to touch again)
// 2. The contract wasn't created in the same transaction: there's no net change in balance,
// and SELFDESTRUCT will perform no action on the account header. (we touch since we did SubBalance+AddBalance above)
if contractAddr != beneficiaryAddr || interpreter.evm.StateDB.WasCreatedInCurrentTx(contractAddr) {
statelessGas := interpreter.evm.Accesses.TouchAddressOnReadAndComputeGas(beneficiaryAddr[:], uint256.Int{}, trieUtils.BalanceLeafKey)
if !scope.Contract.UseGas(statelessGas) {
scope.Contract.Gas = 0
return nil, ErrOutOfGas
}
}
}
return nil, errStopToken
}

Expand Down
2 changes: 2 additions & 0 deletions core/vm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type StateDB interface {

Selfdestruct6780(common.Address)

WasCreatedInCurrentTx(common.Address) bool

// Exist reports whether the given account exists in state.
// Notably this should also return true for self-destructed accounts.
Exist(common.Address) bool
Expand Down
21 changes: 0 additions & 21 deletions trie/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,27 +213,6 @@ func (trie *VerkleTrie) UpdateStorage(address common.Address, key, value []byte)
}

func (t *VerkleTrie) DeleteAccount(addr common.Address) error {
var (
err error
values = make([][]byte, verkle.NodeWidth)
stem = t.pointCache.GetTreeKeyVersionCached(addr[:])
)

for i := 0; i < verkle.NodeWidth; i++ {
values[i] = zero[:]
}

switch root := t.root.(type) {
case *verkle.InternalNode:
err = root.InsertValuesAtStem(stem, values, t.FlatdbNodeResolver)
default:
return errInvalidRootType
}
if err != nil {
return fmt.Errorf("DeleteAccount (%x) error: %v", addr, err)
}
// TODO figure out if the code size needs to be updated, too

return nil
}

Expand Down

0 comments on commit 3f562b4

Please sign in to comment.