diff --git a/nimbus/db/accounts_cache.nim b/nimbus/db/accounts_cache.nim index 5a83c4e43..c18efbc54 100644 --- a/nimbus/db/accounts_cache.nim +++ b/nimbus/db/accounts_cache.nim @@ -515,6 +515,7 @@ proc deleteAccount*(ac: AccountsCache, address: EthAddress) = acc.kill() proc selfDestruct*(ac: AccountsCache, address: EthAddress) = + ac.setBalance(address, 0.u256) ac.savePoint.selfDestruct.incl address proc selfDestruct6780*(ac: AccountsCache, address: EthAddress) = diff --git a/nimbus/evm/computation.nim b/nimbus/evm/computation.nim index 4d970fa27..12bb203bc 100644 --- a/nimbus/evm/computation.nim +++ b/nimbus/evm/computation.nim @@ -369,18 +369,19 @@ proc execSelfDestruct*(c: Computation, beneficiary: EthAddress) c.vmState.mutateStateDB: let localBalance = c.getBalance(c.msg.contractAddress) - # Transfer to beneficiary - db.addBalance(beneficiary, localBalance) - - # Zero the balance of the address being deleted. - # This must come after sending to beneficiary in case the - # contract named itself as the beneficiary. - db.setBalance(c.msg.contractAddress, 0.u256) - # Register the account to be deleted if c.fork >= FkCancun: + # Zeroing contract balance except beneficiary + # is the same address + db.subBalance(c.msg.contractAddress, localBalance) + + # Transfer to beneficiary + db.addBalance(beneficiary, localBalance) + db.selfDestruct6780(c.msg.contractAddress) else: + # Transfer to beneficiary + db.addBalance(beneficiary, localBalance) db.selfDestruct(c.msg.contractAddress) trace "SELFDESTRUCT", diff --git a/nimbus/transaction/host_services.nim b/nimbus/transaction/host_services.nim index 0c80760a7..399fa9511 100644 --- a/nimbus/transaction/host_services.nim +++ b/nimbus/transaction/host_services.nim @@ -220,18 +220,20 @@ proc copyCode(host: TransactionHost, address: HostAddress, proc selfDestruct(host: TransactionHost, address, beneficiary: HostAddress) {.show.} = host.vmState.mutateStateDB: - let closingBalance = db.getBalance(address) + let localBalance = db.getBalance(address) - # Transfer to beneficiary - db.addBalance(beneficiary, closingBalance) - - # Zero balance of account being deleted. - # This must come after sending to the beneficiary in case the - # contract named itself as the beneficiary. - db.setBalance(address, 0.u256) if host.vmState.fork >= FkCancun: + # Zeroing contract balance except beneficiary + # is the same address + db.subBalance(address, localBalance) + + # Transfer to beneficiary + db.addBalance(beneficiary, localBalance) + db.selfDestruct6780(address) else: + # Transfer to beneficiary + db.addBalance(beneficiary, localBalance) db.selfDestruct(address) template call(host: TransactionHost, msg: EvmcMessage): EvmcResult =