Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request from GHSA-f92v-grc2-w2fg (#1234)
Browse files Browse the repository at this point in the history
  • Loading branch information
facs95 authored Aug 11, 2022
1 parent b1cd16e commit 737c1de
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
9 changes: 1 addition & 8 deletions x/evm/keeper/statedb.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"bytes"
"fmt"
"math/big"

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions x/evm/keeper/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand All @@ -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() {
Expand Down

0 comments on commit 737c1de

Please sign in to comment.