Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[evm] rename suicide to selfDestruct #4215

Merged
merged 2 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 44 additions & 44 deletions action/protocol/execution/evm/evmstatedbadapter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 IoTeX Foundation
// Copyright (c) 2024 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
Expand Down Expand Up @@ -55,8 +55,8 @@
refundSnapshot map[int]uint64
cachedContract contractMap
contractSnapshot map[int]contractMap // snapshots of contracts
suicided deleteAccount // account/contract calling Suicide
suicideSnapshot map[int]deleteAccount // snapshots of suicide accounts
selfDestructed deleteAccount // account/contract calling SelfDestruct
selfDestructedSnapshot map[int]deleteAccount // snapshots of SelfDestruct accounts
preimages preimageMap
preimageSnapshot map[int]preimageMap
accessList *accessList // per-transaction access list
Expand Down Expand Up @@ -170,23 +170,23 @@
opts ...StateDBAdapterOption,
) (*StateDBAdapter, error) {
s := &StateDBAdapter{
sm: sm,
logs: []*action.Log{},
err: nil,
blockHeight: blockHeight,
executionHash: executionHash,
lastAddBalanceAmount: new(big.Int),
refundSnapshot: make(map[int]uint64),
cachedContract: make(contractMap),
contractSnapshot: make(map[int]contractMap),
suicided: make(deleteAccount),
suicideSnapshot: make(map[int]deleteAccount),
preimages: make(preimageMap),
preimageSnapshot: make(map[int]preimageMap),
accessList: newAccessList(),
accessListSnapshot: make(map[int]*accessList),
logsSnapshot: make(map[int]int),
txLogsSnapshot: make(map[int]int),
sm: sm,
logs: []*action.Log{},
err: nil,
blockHeight: blockHeight,
executionHash: executionHash,
lastAddBalanceAmount: new(big.Int),
refundSnapshot: make(map[int]uint64),
cachedContract: make(contractMap),
contractSnapshot: make(map[int]contractMap),
selfDestructed: make(deleteAccount),
selfDestructedSnapshot: make(map[int]deleteAccount),
preimages: make(preimageMap),
preimageSnapshot: make(map[int]preimageMap),
accessList: newAccessList(),
accessListSnapshot: make(map[int]*accessList),
logsSnapshot: make(map[int]int),
txLogsSnapshot: make(map[int]int),
}
for _, opt := range opts {
if err := opt(s); err != nil {
Expand Down Expand Up @@ -447,8 +447,8 @@
// a separate patch file will be created later to provide missing logs before the hard-fork
// TODO: remove this gating once the hard-fork has passed
if stateDB.suicideTxLogMismatchPanic {
// before calling Suicide, EVM will transfer the contract's balance to beneficiary
// need to create a transaction log on successful suicide
// before calling SelfDestruct, EVM will transfer the contract's balance to beneficiary
// need to create a transaction log on successful SelfDestruct
if stateDB.lastAddBalanceAmount.Cmp(actBalance) == 0 {
if stateDB.lastAddBalanceAmount.Cmp(big.NewInt(0)) > 0 {
from, _ := address.FromBytes(evmAddr[:])
Expand All @@ -460,19 +460,19 @@
})
}
} else {
log.L().Panic("suicide contract's balance does not match",
zap.String("suicide", actBalance.String()),
log.L().Panic("SelfDestruct contract's balance does not match",
zap.String("SelfDestruct", actBalance.String()),

Check warning on line 464 in action/protocol/execution/evm/evmstatedbadapter.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/execution/evm/evmstatedbadapter.go#L463-L464

Added lines #L463 - L464 were not covered by tests
zap.String("beneficiary", stateDB.lastAddBalanceAmount.String()))
}
}
// mark it as deleted
stateDB.suicided[addrHash] = struct{}{}
stateDB.selfDestructed[addrHash] = struct{}{}
}

// HasSelfDestructed returns whether the contract has been killed
func (stateDB *StateDBAdapter) HasSelfDestructed(evmAddr common.Address) bool {
addrHash := hash.BytesToHash160(evmAddr.Bytes())
_, ok := stateDB.suicided[addrHash]
_, ok := stateDB.selfDestructed[addrHash]
return ok
}

Expand Down Expand Up @@ -592,9 +592,9 @@
stateDB.logError(err)
return
}
ds, ok := stateDB.suicideSnapshot[snapshot]
ds, ok := stateDB.selfDestructedSnapshot[snapshot]
if !ok {
// this should not happen, b/c we save the suicide accounts on a successful return of Snapshot(), but check anyway
// this should not happen, b/c we save the SelfDestruct accounts on a successful return of Snapshot(), but check anyway
log.L().Error("Failed to get snapshot.", zap.Int("snapshot", snapshot))
return
}
Expand Down Expand Up @@ -644,14 +644,14 @@
}
}
}
// restore the suicide accounts
stateDB.suicided = nil
stateDB.suicided = ds
// restore the SelfDestruct accounts
stateDB.selfDestructed = nil
stateDB.selfDestructed = ds
if stateDB.fixSnapshotOrder {
delete(stateDB.suicideSnapshot, snapshot)
delete(stateDB.selfDestructedSnapshot, snapshot)
for i := snapshot + 1; ; i++ {
if _, ok := stateDB.suicideSnapshot[i]; ok {
delete(stateDB.suicideSnapshot, i)
if _, ok := stateDB.selfDestructedSnapshot[i]; ok {
delete(stateDB.selfDestructedSnapshot, i)
} else {
break
}
Expand Down Expand Up @@ -713,7 +713,7 @@
}
}
sn := stateDB.sm.Snapshot()
if _, ok := stateDB.suicideSnapshot[sn]; ok {
if _, ok := stateDB.selfDestructedSnapshot[sn]; ok {
err := errors.New("unexpected error: duplicate snapshot version")
if stateDB.fixSnapshotOrder {
log.L().Panic("Failed to snapshot.", zap.Error(err))
Expand All @@ -730,12 +730,12 @@
stateDB.logsSnapshot[sn] = len(stateDB.logs)
stateDB.txLogsSnapshot[sn] = len(stateDB.transactionLogs)
}
// save a copy of current suicide accounts
// save a copy of current SelfDestruct accounts
sa := make(deleteAccount)
for k, v := range stateDB.suicided {
for k, v := range stateDB.selfDestructed {
sa[k] = v
}
stateDB.suicideSnapshot[sn] = sa
stateDB.selfDestructedSnapshot[sn] = sa
if !stateDB.fixSnapshotOrder {
for _, addr := range stateDB.cachedContractAddrs() {
c[addr] = stateDB.cachedContract[addr].Snapshot()
Expand Down Expand Up @@ -996,8 +996,8 @@
return errors.Wrap(err, "failed to decode address hash")
}
copy(addr[:], addrBytes)
if _, ok := stateDB.suicided[addr]; ok {
// no need to update a suicide account/contract
if _, ok := stateDB.selfDestructed[addr]; ok {
// no need to update a SelfDestruct account/contract
continue
}
contract := stateDB.cachedContract[addr]
Expand All @@ -1014,7 +1014,7 @@
}
// delete suicided accounts/contract
addrStrs = make([]string, 0)
for addr := range stateDB.suicided {
for addr := range stateDB.selfDestructed {
addrStrs = append(addrStrs, hex.EncodeToString(addr[:]))
}
sort.Strings(addrStrs)
Expand All @@ -1028,7 +1028,7 @@
copy(addr[:], addrBytes)
if _, err := stateDB.sm.DelState(protocol.LegacyKeyOption(addr)); err != nil {
stateDB.logError(err)
return errors.Wrapf(err, "failed to delete suicide account/contract %x", addr[:])
return errors.Wrapf(err, "failed to delete SelfDestruct account/contract %x", addr[:])
}
}
// write preimages to DB
Expand Down Expand Up @@ -1083,8 +1083,8 @@
stateDB.refundSnapshot = make(map[int]uint64)
stateDB.cachedContract = make(contractMap)
stateDB.contractSnapshot = make(map[int]contractMap)
stateDB.suicided = make(deleteAccount)
stateDB.suicideSnapshot = make(map[int]deleteAccount)
stateDB.selfDestructed = make(deleteAccount)
stateDB.selfDestructedSnapshot = make(map[int]deleteAccount)
stateDB.preimages = make(preimageMap)
stateDB.preimageSnapshot = make(map[int]preimageMap)
stateDB.accessList = newAccessList()
Expand Down
30 changes: 15 additions & 15 deletions action/protocol/execution/evm/evmstatedbadapter_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 IoTeX Foundation
// Copyright (c) 2024 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
Expand Down Expand Up @@ -448,8 +448,8 @@ func TestSnapshotRevertAndCommit(t *testing.T) {
}
// set refund
stateDB.refund = test.refund
// set suicide
for _, e := range test.suicide {
// set SelfDestruct
for _, e := range test.selfDestruct {
if e.amount != nil {
stateDB.AddBalance(e.addr, uint256.MustFromBig(e.amount))
}
Expand Down Expand Up @@ -639,9 +639,9 @@ func TestSnapshotRevertAndCommit(t *testing.T) {
}
}
}
// test suicide/exist
for _, e := range test.suicide {
require.Equal(e.suicide, stateDB.HasSelfDestructed(e.addr))
// test SelfDestruct/exist
for _, e := range test.selfDestruct {
require.Equal(e.selfDestruct, stateDB.HasSelfDestructed(e.addr))
require.Equal(e.exist, stateDB.Exist(e.addr))
}
// test logs
Expand All @@ -666,13 +666,13 @@ func TestSnapshotRevertAndCommit(t *testing.T) {
require.Equal(1, stateDB.Snapshot())
if fixSnapshot {
require.Equal(1, len(stateDB.contractSnapshot))
require.Equal(1, len(stateDB.suicideSnapshot))
require.Equal(1, len(stateDB.selfDestructedSnapshot))
require.Equal(1, len(stateDB.preimageSnapshot))
require.Equal(1, len(stateDB.accessListSnapshot))
require.Equal(1, len(stateDB.refundSnapshot))
} else {
require.Equal(3, len(stateDB.contractSnapshot))
require.Equal(3, len(stateDB.suicideSnapshot))
require.Equal(3, len(stateDB.selfDestructedSnapshot))
require.Equal(3, len(stateDB.preimageSnapshot))
// refund fix and accessList are introduced after fixSnapshot
// so their snapshot are always properly cleared
Expand All @@ -682,7 +682,7 @@ func TestSnapshotRevertAndCommit(t *testing.T) {
// commit snapshot 0's state
require.NoError(stateDB.CommitContracts())
stateDB.clear()
//[TODO] need e2etest to verify state factory commit/re-open (whether result from state/balance/suicide/exist is same)
//[TODO] need e2etest to verify state factory commit/re-open (whether result from state/balance/SelfDestruct/exist is same)
}

t.Run("contract snapshot/revert/commit", func(t *testing.T) {
Expand Down Expand Up @@ -734,8 +734,8 @@ func TestClearSnapshots(t *testing.T) {
for _, e := range test.states {
stateDB.SetState(e.addr, e.k, e.v)
}
// set suicide
for _, e := range test.suicide {
// set SelfDestruct
for _, e := range test.selfDestruct {
stateDB.SelfDestruct(e.addr)
require.Equal(e.exist, stateDB.Exist(e.addr))
}
Expand All @@ -752,23 +752,23 @@ func TestClearSnapshots(t *testing.T) {

if stateDB.fixSnapshotOrder {
// snapshot 1, 2 cleared, only 0 left in map
require.Equal(1, len(stateDB.suicideSnapshot))
require.Equal(1, len(stateDB.selfDestructedSnapshot))
require.Equal(1, len(stateDB.contractSnapshot))
require.Equal(1, len(stateDB.preimageSnapshot))
require.Equal(2, stateDB.Snapshot())
// now there are 2 snapshots: 0 and the newly added one
require.Equal(2, len(stateDB.suicideSnapshot))
require.Equal(2, len(stateDB.selfDestructedSnapshot))
require.Equal(2, len(stateDB.contractSnapshot))
require.Equal(2, len(stateDB.preimageSnapshot))
require.Equal(2, len(stateDB.logsSnapshot))
} else {
// snapshot not cleared
require.Equal(3, len(stateDB.suicideSnapshot))
require.Equal(3, len(stateDB.selfDestructedSnapshot))
require.Equal(3, len(stateDB.contractSnapshot))
require.Equal(3, len(stateDB.preimageSnapshot))
require.Equal(2, stateDB.Snapshot())
// still 3 old snapshots
require.Equal(3, len(stateDB.suicideSnapshot))
require.Equal(3, len(stateDB.selfDestructedSnapshot))
require.Equal(3, len(stateDB.contractSnapshot))
require.Equal(3, len(stateDB.preimageSnapshot))
// log snapshot added after fixSnapshotOrder, so it is cleared and 1 remains
Expand Down
17 changes: 11 additions & 6 deletions action/protocol/execution/evm/testdata_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Copyright (c) 2024 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.

package evm

import (
Expand Down Expand Up @@ -35,11 +40,11 @@ type (
v common.Hash
}
sui struct {
amount *big.Int
beneficiary common.Address
addr common.Address
suicide bool
exist bool
amount *big.Int
beneficiary common.Address
addr common.Address
selfDestruct bool
exist bool
}
image struct {
hash common.Hash
Expand All @@ -56,7 +61,7 @@ type (
codes []code
states []evmSet
refund uint64
suicide []sui
selfDestruct []sui
preimage []image
accessList []access
logs []*types.Log
Expand Down
2 changes: 1 addition & 1 deletion blockchain/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ type (
SumatraBlockHeight uint64 `yaml:"sumatraHeight"`
// TsunamiBlockHeight is the start height to
// 1. enable delegate endorsement
// 2. generate transaction log for Suicide() call in EVM
// 2. generate transaction log for SelfDestruct() call in EVM
// 3. raise block gas limit to 50M
TsunamiBlockHeight uint64 `yaml:"tsunamiHeight"`
// UpernavikBlockHeight is the start height to
Expand Down
Loading