core/vm: improve comments in self destruct

This commit is contained in:
lightclient 2026-01-12 08:39:32 -07:00
parent 83513e66dd
commit 9dda9f02b2
No known key found for this signature in database
GPG key ID: 657913021EF45A6A

View file

@ -876,21 +876,25 @@ func opStop(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) {
} }
func opSelfdestruct(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { func opSelfdestruct(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) {
beneficiary := scope.Stack.pop() var (
balance := evm.StateDB.GetBalance(scope.Contract.Address()) this = scope.Contract.Address()
balance = evm.StateDB.GetBalance(this)
top = scope.Stack.pop()
beneficiary = common.Address(top.Bytes20())
)
// The funds are burned immediately if the beneficiary is the caller itself, // The funds are burned immediately if the beneficiary is the caller itself,
// in this case, the beneficiary's balance is not increased. // in this case, the beneficiary's balance is not increased.
if beneficiary.Bytes20() != scope.Contract.Address() { if beneficiary != this {
evm.StateDB.AddBalance(beneficiary.Bytes20(), balance, tracing.BalanceIncreaseSelfdestruct) evm.StateDB.AddBalance(beneficiary, balance, tracing.BalanceIncreaseSelfdestruct)
} }
// Clear any leftover funds for the account being destructed. // Clear any leftover funds for the account being destructed.
evm.StateDB.SubBalance(scope.Contract.Address(), balance, tracing.BalanceDecreaseSelfdestruct) evm.StateDB.SubBalance(this, balance, tracing.BalanceDecreaseSelfdestruct)
evm.StateDB.SelfDestruct(scope.Contract.Address()) evm.StateDB.SelfDestruct(this)
if tracer := evm.Config.Tracer; tracer != nil { if tracer := evm.Config.Tracer; tracer != nil {
if tracer.OnEnter != nil { if tracer.OnEnter != nil {
tracer.OnEnter(evm.depth, byte(SELFDESTRUCT), scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance.ToBig()) tracer.OnEnter(evm.depth, byte(SELFDESTRUCT), this, beneficiary, []byte{}, 0, balance.ToBig())
} }
if tracer.OnExit != nil { if tracer.OnExit != nil {
tracer.OnExit(evm.depth, []byte{}, 0, nil, false) tracer.OnExit(evm.depth, []byte{}, 0, nil, false)
@ -900,28 +904,35 @@ func opSelfdestruct(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) {
} }
func opSelfdestruct6780(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { func opSelfdestruct6780(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) {
beneficiary := scope.Stack.pop() var (
balance := evm.StateDB.GetBalance(scope.Contract.Address()) this = scope.Contract.Address()
balance = evm.StateDB.GetBalance(this)
top = scope.Stack.pop()
beneficiary = common.Address(top.Bytes20())
)
if evm.StateDB.IsNewContract(scope.Contract.Address()) { if evm.StateDB.IsNewContract(this) {
// If the contract is not preexisting, the balance is immediately burned // Contract is newly created in this transaction, so it will be actually
// on selfdestruct-to-self. In this case, the beneficiary's balance is // deleted from the state.
// not increased. if this == beneficiary {
if scope.Contract.Address() != beneficiary.Bytes20() { // Since the contract will be deleted from state, it isn't necessary to
evm.StateDB.AddBalance(beneficiary.Bytes20(), balance, tracing.BalanceIncreaseSelfdestruct) // also subtract the balance beforehand as it will affect the tracing.
} else {
// Otherwise, we can proceed with transfer.
evm.StateDB.AddBalance(beneficiary, balance, tracing.BalanceIncreaseSelfdestruct)
} }
evm.StateDB.SubBalance(scope.Contract.Address(), balance, tracing.BalanceDecreaseSelfdestruct) evm.StateDB.SubBalance(this, balance, tracing.BalanceDecreaseSelfdestruct)
} else { } else {
// If the contract is preexisting, send all Ether in the account to the // Contract already exists in the state, therefore it will send it's full
// target. Note the balance isn't burned on selfdestruct-to-self. // balance and remain in the state.
evm.StateDB.SubBalance(scope.Contract.Address(), balance, tracing.BalanceDecreaseSelfdestruct) evm.StateDB.SubBalance(this, balance, tracing.BalanceDecreaseSelfdestruct)
evm.StateDB.AddBalance(beneficiary.Bytes20(), balance, tracing.BalanceIncreaseSelfdestruct) evm.StateDB.AddBalance(beneficiary, balance, tracing.BalanceIncreaseSelfdestruct)
} }
evm.StateDB.SelfDestruct6780(scope.Contract.Address()) evm.StateDB.SelfDestruct6780(this)
if tracer := evm.Config.Tracer; tracer != nil { if tracer := evm.Config.Tracer; tracer != nil {
if tracer.OnEnter != nil { if tracer.OnEnter != nil {
tracer.OnEnter(evm.depth, byte(SELFDESTRUCT), scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance.ToBig()) tracer.OnEnter(evm.depth, byte(SELFDESTRUCT), this, beneficiary, []byte{}, 0, balance.ToBig())
} }
if tracer.OnExit != nil { if tracer.OnExit != nil {
tracer.OnExit(evm.depth, []byte{}, 0, nil, false) tracer.OnExit(evm.depth, []byte{}, 0, nil, false)