From 0f737b4725812fd51164a9575d97577474adcbc5 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Tue, 14 Jan 2025 10:56:15 +0800 Subject: [PATCH] core: replace suicide with selfdestruct to improve code consistency (#27716) --- XDCx/tradingstate/statedb.go | 2 +- XDCxlending/lendingstate/statedb.go | 2 +- accounts/abi/bind/backend.go | 2 +- core/state/journal.go | 8 ++++---- core/state/state_object.go | 31 ++++++++++++++++++----------- core/state/state_test.go | 10 +++++----- core/state/statedb.go | 30 +++++++++++++--------------- core/state/statedb_test.go | 7 +++---- core/vm/gas_table.go | 2 +- core/vm/instructions.go | 4 ++-- core/vm/interface.go | 6 +++--- core/vm/operations_acl.go | 2 +- 12 files changed, 55 insertions(+), 51 deletions(-) diff --git a/XDCx/tradingstate/statedb.go b/XDCx/tradingstate/statedb.go index 7520947db1..91010e9bff 100644 --- a/XDCx/tradingstate/statedb.go +++ b/XDCx/tradingstate/statedb.go @@ -89,7 +89,7 @@ func (t *TradingStateDB) Error() error { } // Exist reports whether the given orderId address exists in the state. -// Notably this also returns true for suicided exchanges. +// Notably this also returns true for self-destructed exchanges. func (t *TradingStateDB) Exist(addr common.Hash) bool { return t.getStateExchangeObject(addr) != nil } diff --git a/XDCxlending/lendingstate/statedb.go b/XDCxlending/lendingstate/statedb.go index a1b3899803..146cf4a8c8 100644 --- a/XDCxlending/lendingstate/statedb.go +++ b/XDCxlending/lendingstate/statedb.go @@ -84,7 +84,7 @@ func (ls *LendingStateDB) Error() error { } // Exist reports whether the given tradeId address exists in the state. -// Notably this also returns true for suicided lenddinges. +// Notably this also returns true for self-destructed lenddinges. func (ls *LendingStateDB) Exist(addr common.Hash) bool { return ls.getLendingExchange(addr) != nil } diff --git a/accounts/abi/bind/backend.go b/accounts/abi/bind/backend.go index b067b7da2a..5a00933abf 100644 --- a/accounts/abi/bind/backend.go +++ b/accounts/abi/bind/backend.go @@ -29,7 +29,7 @@ import ( var ( // ErrNoCode is returned by call and transact operations for which the requested // recipient contract to operate on does not exist in the state db or does not - // have any code associated with it (i.e. suicided). + // have any code associated with it (i.e. self-destructed). ErrNoCode = errors.New("no contract code at given address") // ErrNoPendingState is raised when attempting to perform a pending state action diff --git a/core/state/journal.go b/core/state/journal.go index d8f748fa64..2be83be064 100644 --- a/core/state/journal.go +++ b/core/state/journal.go @@ -41,9 +41,9 @@ type ( resetObjectChange struct { prev *stateObject } - suicideChange struct { + selfDestructChange struct { account *common.Address - prev bool // whether account had already suicided + prev bool // whether account had already self-destructed prevbalance *big.Int } @@ -104,10 +104,10 @@ func (ch resetObjectChange) undo(s *StateDB) { s.setStateObject(ch.prev) } -func (ch suicideChange) undo(s *StateDB) { +func (ch selfDestructChange) undo(s *StateDB) { obj := s.getStateObject(*ch.account) if obj != nil { - obj.suicided = ch.prev + obj.selfDestructed = ch.prev obj.setBalance(ch.prevbalance) } } diff --git a/core/state/state_object.go b/core/state/state_object.go index d49ae247ce..ef436ca34f 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -61,10 +61,10 @@ func (s Storage) Copy() Storage { // Account values can be accessed and modified through the object. // Finally, call CommitTrie to write the modified storage trie into a database. type stateObject struct { - address common.Address - addrHash common.Hash // hash of ethereum address of the account - data Account db *StateDB + address common.Address // address of ethereum account + addrHash common.Hash // hash of ethereum address of the account + data Account // Account data with all mutations applied in the scope of block // DB error. // State objects are used by the consensus core and VM which are @@ -82,13 +82,20 @@ type stateObject struct { fakeStorage Storage // Fake storage which constructed by caller for debugging purpose. // Cache flags. - // When an object is marked suicided it will be delete from the trie - // during the "update" phase of the state transition. dirtyCode bool // true if the code was updated - suicided bool - touched bool - deleted bool - onDirty func(addr common.Address) // Callback method to mark a state object newly dirty + + // Flag whether the account was marked as self-destructed. The self-destructed + // account is still accessible in the scope of same transaction. + selfDestructed bool + + // Flag whether the account was marked as deleted. A self-destructed account + // or an account that is considered as empty will be marked as deleted at + // the end of transaction and no longer accessible anymore. + deleted bool + + touched bool + + onDirty func(addr common.Address) // Callback method to mark a state object newly dirty } // empty returns whether the account is considered empty. @@ -136,8 +143,8 @@ func (s *stateObject) setError(err error) { } } -func (s *stateObject) markSuicided() { - s.suicided = true +func (s *stateObject) markSelfdestructed() { + s.selfDestructed = true if s.onDirty != nil { s.onDirty(s.Address()) s.onDirty = nil @@ -363,7 +370,7 @@ func (s *stateObject) deepCopy(db *StateDB, onDirty func(addr common.Address)) * stateObject.code = s.code stateObject.dirtyStorage = s.dirtyStorage.Copy() stateObject.cachedStorage = s.dirtyStorage.Copy() - stateObject.suicided = s.suicided + stateObject.selfDestructed = s.selfDestructed stateObject.dirtyCode = s.dirtyCode stateObject.deleted = s.deleted return stateObject diff --git a/core/state/state_test.go b/core/state/state_test.go index c4b49628d2..257f029a8b 100644 --- a/core/state/state_test.go +++ b/core/state/state_test.go @@ -18,11 +18,11 @@ package state import ( "bytes" - "github.com/XinFinOrg/XDPoSChain/core/rawdb" "math/big" "testing" "github.com/XinFinOrg/XDPoSChain/common" + "github.com/XinFinOrg/XDPoSChain/core/rawdb" "github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/ethdb" checker "gopkg.in/check.v1" @@ -152,7 +152,7 @@ func TestSnapshot2(t *testing.T) { so0.SetBalance(big.NewInt(42)) so0.SetNonce(43) so0.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e'}), []byte{'c', 'a', 'f', 'e'}) - so0.suicided = false + so0.selfDestructed = false so0.deleted = false state.setStateObject(so0) @@ -164,7 +164,7 @@ func TestSnapshot2(t *testing.T) { so1.SetBalance(big.NewInt(52)) so1.SetNonce(53) so1.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e', '2'}), []byte{'c', 'a', 'f', 'e', '2'}) - so1.suicided = true + so1.selfDestructed = true so1.deleted = true state.setStateObject(so1) @@ -224,8 +224,8 @@ func compareStateObjects(so0, so1 *stateObject, t *testing.T) { } } - if so0.suicided != so1.suicided { - t.Fatalf("suicided mismatch: have %v, want %v", so0.suicided, so1.suicided) + if so0.selfDestructed != so1.selfDestructed { + t.Fatalf("self-destructed mismatch: have %v, want %v", so0.selfDestructed, so1.selfDestructed) } if so0.deleted != so1.deleted { t.Fatalf("Deleted mismatch: have %v, want %v", so0.deleted, so1.deleted) diff --git a/core/state/statedb.go b/core/state/statedb.go index d357e6e1bf..9003f58fde 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -214,7 +214,7 @@ func (s *StateDB) AddRefund(gas uint64) { } // Exist reports whether the given account address exists in the state. -// Notably this also returns true for suicided accounts. +// Notably this also returns true for self-destructed accounts. func (s *StateDB) Exist(addr common.Address) bool { return s.getStateObject(addr) != nil } @@ -336,10 +336,10 @@ func (s *StateDB) StorageTrie(addr common.Address) Trie { return cpy.updateTrie(s.db) } -func (s *StateDB) HasSuicided(addr common.Address) bool { +func (s *StateDB) HasSelfDestructed(addr common.Address) bool { stateObject := s.getStateObject(addr) if stateObject != nil { - return stateObject.suicided + return stateObject.selfDestructed } return false } @@ -401,25 +401,23 @@ func (s *StateDB) SetStorage(addr common.Address, storage map[common.Hash]common } } -// Suicide marks the given account as suicided. +// SelfDestruct marks the given account as selfdestructed. // This clears the account balance. // // The account's state object is still available until the state is committed, -// getStateObject will return a non-nil account after Suicide. -func (s *StateDB) Suicide(addr common.Address) bool { +// getStateObject will return a non-nil account after SelfDestruct. +func (s *StateDB) SelfDestruct(addr common.Address) { stateObject := s.getStateObject(addr) if stateObject == nil { - return false + return } - s.journal = append(s.journal, suicideChange{ + s.journal = append(s.journal, selfDestructChange{ account: &addr, - prev: stateObject.suicided, + prev: stateObject.selfDestructed, prevbalance: new(big.Int).Set(stateObject.Balance()), }) - stateObject.markSuicided() + stateObject.markSelfdestructed() stateObject.data.Balance = new(big.Int) - - return true } // SetTransientState sets transient storage for a given account. It @@ -679,7 +677,7 @@ func (s *StateDB) GetRefund() uint64 { func (s *StateDB) Finalise(deleteEmptyObjects bool) { for addr := range s.stateObjectsDirty { stateObject := s.stateObjects[addr] - if stateObject.suicided || (deleteEmptyObjects && stateObject.empty()) { + if stateObject.selfDestructed || (deleteEmptyObjects && stateObject.empty()) { s.deleteStateObject(stateObject) } else { stateObject.updateRoot(s.db) @@ -710,7 +708,7 @@ func (s *StateDB) SetTxContext(thash common.Hash, ti int) { s.txIndex = ti } -// DeleteSuicides flags the suicided objects for deletion so that it +// DeleteSuicides flags the self-destructed objects for deletion so that it // won't be referenced again when called / queried up on. // // DeleteSuicides should not be used for consensus related updates @@ -724,7 +722,7 @@ func (s *StateDB) DeleteSuicides() { // If the object has been removed by a suicide // flag the object as deleted. - if stateObject.suicided { + if stateObject.selfDestructed { stateObject.deleted = true } delete(s.stateObjectsDirty, addr) @@ -745,7 +743,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error) for addr, stateObject := range s.stateObjects { _, isDirty := s.stateObjectsDirty[addr] switch { - case stateObject.suicided || (isDirty && deleteEmptyObjects && stateObject.empty()): + case stateObject.selfDestructed || (isDirty && deleteEmptyObjects && stateObject.empty()): // If the object has been removed, don't bother syncing it // and just mark it for deletion in the trie. s.deleteStateObject(stateObject) diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index 5166224b35..a4af4661e4 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -29,7 +29,6 @@ import ( "testing/quick" check "gopkg.in/check.v1" - "github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/core/rawdb" "github.com/XinFinOrg/XDPoSChain/core/types" @@ -261,9 +260,9 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction { }, }, { - name: "Suicide", + name: "SelfDestruct", fn: func(a testAction, s *StateDB) { - s.Suicide(addr) + s.SelfDestruct(addr) }, }, { @@ -405,7 +404,7 @@ func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error { } // Check basic accessor methods. checkeq("Exist", state.Exist(addr), checkstate.Exist(addr)) - checkeq("HasSuicided", state.HasSuicided(addr), checkstate.HasSuicided(addr)) + checkeq("HasSelfDestructed", state.HasSelfDestructed(addr), checkstate.HasSelfDestructed(addr)) checkeq("GetBalance", state.GetBalance(addr), checkstate.GetBalance(addr)) checkeq("GetNonce", state.GetNonce(addr), checkstate.GetNonce(addr)) checkeq("GetCode", state.GetCode(addr), checkstate.GetCode(addr)) diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index 748d1f3ddd..1f9424aed5 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -469,7 +469,7 @@ func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, me } } - if !evm.StateDB.HasSuicided(contract.Address()) { + if !evm.StateDB.HasSelfDestructed(contract.Address()) { evm.StateDB.AddRefund(params.SelfdestructRefundGas) } return gas, nil diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 6a0ddea1b1..2802c62bcf 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -415,7 +415,7 @@ func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) // If the precompile account is not transferred any amount on a private or // customized chain, the return value will be zero. // -// (5) Caller tries to get the code hash for an account which is marked as suicided +// (5) Caller tries to get the code hash for an account which is marked as self-destructed // // in the current transaction, the code hash of this account should be returned. // @@ -837,7 +837,7 @@ func opSelfdestruct(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext beneficiary := scope.Stack.pop() balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address()) interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance) - interpreter.evm.StateDB.Suicide(scope.Contract.Address()) + interpreter.evm.StateDB.SelfDestruct(scope.Contract.Address()) if interpreter.evm.Config.Debug { interpreter.evm.Config.Tracer.CaptureEnter(SELFDESTRUCT, scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance) interpreter.evm.Config.Tracer.CaptureExit([]byte{}, 0, nil) diff --git a/core/vm/interface.go b/core/vm/interface.go index c6c3ee1cfd..105be10b71 100644 --- a/core/vm/interface.go +++ b/core/vm/interface.go @@ -51,11 +51,11 @@ type StateDB interface { GetTransientState(addr common.Address, key common.Hash) common.Hash SetTransientState(addr common.Address, key, value common.Hash) - Suicide(common.Address) bool - HasSuicided(common.Address) bool + SelfDestruct(common.Address) + HasSelfDestructed(common.Address) bool // Exist reports whether the given account exists in state. - // Notably this should also return true for suicided accounts. + // Notably this should also return true for self-destructed accounts. Exist(common.Address) bool // Empty returns whether the given account is empty. Empty // is defined according to EIP161 (balance = nonce = code = 0). diff --git a/core/vm/operations_acl.go b/core/vm/operations_acl.go index dfef062856..6894f795db 100644 --- a/core/vm/operations_acl.go +++ b/core/vm/operations_acl.go @@ -235,7 +235,7 @@ func makeSelfdestructGasFn(refundsEnabled bool) gasFunc { if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 { gas += params.CreateBySelfdestructGas } - if refundsEnabled && !evm.StateDB.HasSuicided(contract.Address()) { + if refundsEnabled && !evm.StateDB.HasSelfDestructed(contract.Address()) { evm.StateDB.AddRefund(params.SelfdestructRefundGas) } return gas, nil