From 737c1de6941ee8b701fc16ffa448efeecb7b87df Mon Sep 17 00:00:00 2001 From: Freddy Caceres Date: Thu, 11 Aug 2022 01:07:38 -0400 Subject: [PATCH] Merge pull request from GHSA-f92v-grc2-w2fg (#1234) --- 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 d5400aaf..92ee7e4f 100644 --- a/x/evm/keeper/statedb.go +++ b/x/evm/keeper/statedb.go @@ -1,7 +1,6 @@ package keeper import ( - "bytes" "fmt" "math/big" @@ -188,7 +187,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) } @@ -198,12 +197,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 52b29e20..e2c82a6c 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() {