From 144741832007a26dbe950512acbda4ed95b2a451 Mon Sep 17 00:00:00 2001 From: Freddy Caceres Date: Thu, 4 Aug 2022 15:47:01 -0400 Subject: [PATCH] Merge pull request from GHSA-f92v-grc2-w2fg MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * remove delete code in DeleteAccount * update unit test * refactor test Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> --- x/evm/keeper/statedb.go | 9 +-------- x/evm/keeper/statedb_test.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/x/evm/keeper/statedb.go b/x/evm/keeper/statedb.go index a70d1427d6..32efb19426 100644 --- a/x/evm/keeper/statedb.go +++ b/x/evm/keeper/statedb.go @@ -1,7 +1,6 @@ package keeper import ( - "bytes" "fmt" "math/big" @@ -186,7 +185,7 @@ func (k *Keeper) DeleteAccount(ctx sdk.Context, addr common.Address) error { } // NOTE: only Ethereum accounts (contracts) can be selfdestructed - ethAcct, ok := acct.(ethermint.EthAccountI) + _, ok := acct.(ethermint.EthAccountI) if !ok { return sdkerrors.Wrapf(types.ErrInvalidAccount, "type %T, address %s", acct, addr) } @@ -196,12 +195,6 @@ func (k *Keeper) DeleteAccount(ctx sdk.Context, addr common.Address) error { return err } - // remove code - codeHashBz := ethAcct.GetCodeHash().Bytes() - if !bytes.Equal(codeHashBz, types.EmptyCodeHash) { - k.SetCode(ctx, codeHashBz, nil) - } - // clear storage k.ForEachStorage(ctx, addr, func(key, _ common.Hash) bool { k.SetState(ctx, addr, key, nil) diff --git a/x/evm/keeper/statedb_test.go b/x/evm/keeper/statedb_test.go index 52b29e20a5..e2c82a6cbe 100644 --- a/x/evm/keeper/statedb_test.go +++ b/x/evm/keeper/statedb_test.go @@ -15,6 +15,7 @@ import ( ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" + "github.com/evmos/ethermint/crypto/ethsecp256k1" "github.com/evmos/ethermint/tests" "github.com/evmos/ethermint/x/evm/statedb" "github.com/evmos/ethermint/x/evm/types" @@ -420,12 +421,26 @@ func (suite *KeeperTestSuite) TestSuicide() { suite.Require().NoError(db.Commit()) db = suite.StateDB() + // Generate 2nd address + privkey, _ := ethsecp256k1.GenerateKey() + key, err := privkey.ToECDSA() + suite.Require().NoError(err) + addr2 := crypto.PubkeyToAddress(key.PublicKey) + + // Add code and state to account 2 + db.SetCode(addr2, code) + suite.Require().Equal(code, db.GetCode(addr2)) + for i := 0; i < 5; i++ { + db.SetState(addr2, common.BytesToHash([]byte(fmt.Sprintf("key%d", i))), common.BytesToHash([]byte(fmt.Sprintf("value%d", i)))) + } + // Call Suicide suite.Require().Equal(true, db.Suicide(suite.address)) // Check suicided is marked suite.Require().Equal(true, db.HasSuicided(suite.address)) + // Commit state suite.Require().NoError(db.Commit()) db = suite.StateDB() @@ -441,6 +456,10 @@ func (suite *KeeperTestSuite) TestSuicide() { // Check account is deleted suite.Require().Equal(common.Hash{}, db.GetCodeHash(suite.address)) + + // Check code is still present in addr2 and suicided is false + suite.Require().NotNil(db.GetCode(addr2)) + suite.Require().Equal(false, db.HasSuicided(addr2)) } func (suite *KeeperTestSuite) TestExist() {