Skip to content

Commit

Permalink
Make GetCodeHash on parity with Ethereum (sei-protocol#1769)
Browse files Browse the repository at this point in the history
Make GetCodeHash in parity with Ethereum
  • Loading branch information
codchen authored Jul 16, 2024
1 parent e6b8aed commit 1b47795
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 13 deletions.
17 changes: 17 additions & 0 deletions x/evm/keeper/balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package keeper

import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/x/evm/state"
)

func (k *Keeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress) *big.Int {
denom := k.GetBaseDenom(ctx)
allUsei := k.BankKeeper().GetBalance(ctx, addr, denom).Amount
lockedUsei := k.BankKeeper().LockedCoins(ctx, addr).AmountOf(denom) // LockedCoins doesn't use iterators
usei := allUsei.Sub(lockedUsei)
wei := k.BankKeeper().GetWeiBalance(ctx, addr)
return usei.Mul(state.SdkUseiToSweiMultiplier).Add(wei).BigInt()
}
6 changes: 6 additions & 0 deletions x/evm/keeper/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/sei-protocol/sei-chain/utils"
"github.com/sei-protocol/sei-chain/x/evm/types"
)

Expand Down Expand Up @@ -37,6 +38,11 @@ func (k *Keeper) GetCodeHash(ctx sdk.Context, addr common.Address) common.Hash {
store := k.PrefixStore(ctx, types.CodeHashKeyPrefix)
bz := store.Get(addr[:])
if bz == nil {
// per Ethereum behavior, if an address has no code or balance, return Hash(0)
if k.GetBalance(ctx, k.GetSeiAddressOrDefault(ctx, addr)).Cmp(utils.Big0) == 0 {
return common.Hash{}
}
// if an address has no code but some balance, return EmptyCodeHash
return ethtypes.EmptyCodeHash
}
return common.BytesToHash(bz)
Expand Down
5 changes: 5 additions & 0 deletions x/evm/keeper/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/sei-protocol/sei-chain/testutil/keeper"
Expand All @@ -14,6 +15,10 @@ func TestCode(t *testing.T) {
k, ctx := keeper.MockEVMKeeper()
_, addr := keeper.MockAddressPair()

require.Equal(t, common.Hash{}, k.GetCodeHash(ctx, addr))

k.BankKeeper().MintCoins(ctx, "evm", sdk.NewCoins(sdk.NewCoin("usei", sdk.OneInt())))
k.BankKeeper().SendCoinsFromModuleToAccount(ctx, "evm", sdk.AccAddress(addr[:]), sdk.NewCoins(sdk.NewCoin("usei", sdk.OneInt())))
require.Equal(t, ethtypes.EmptyCodeHash, k.GetCodeHash(ctx, addr))
require.Nil(t, k.GetCode(ctx, addr))
require.Equal(t, 0, k.GetCodeSize(ctx, addr))
Expand Down
7 changes: 1 addition & 6 deletions x/evm/state/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,7 @@ func (s *DBImpl) AddBalance(evmAddr common.Address, amt *big.Int, reason tracing
func (s *DBImpl) GetBalance(evmAddr common.Address) *big.Int {
s.k.PrepareReplayedAddr(s.ctx, evmAddr)
seiAddr := s.getSeiAddress(evmAddr)
denom := s.k.GetBaseDenom(s.ctx)
allUsei := s.k.BankKeeper().GetBalance(s.ctx, seiAddr, denom).Amount
lockedUsei := s.k.BankKeeper().LockedCoins(s.ctx, seiAddr).AmountOf(denom) // LockedCoins doesn't use iterators
usei := allUsei.Sub(lockedUsei)
wei := s.k.BankKeeper().GetWeiBalance(s.ctx, seiAddr)
return usei.Mul(SdkUseiToSweiMultiplier).Add(wei).BigInt()
return s.k.GetBalance(s.ctx, seiAddr)
}

// should only be called during simulation
Expand Down
7 changes: 2 additions & 5 deletions x/evm/state/check.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package state

import (
"bytes"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/sei-protocol/sei-chain/utils"
)

Expand All @@ -14,7 +11,7 @@ func (s *DBImpl) Exist(addr common.Address) bool {
s.k.PrepareReplayedAddr(s.ctx, addr)
// check if the address exists as a contract
codeHash := s.GetCodeHash(addr)
if codeHash.Cmp(ethtypes.EmptyCodeHash) != 0 && s.GetCodeHash(addr).Cmp(common.Hash{}) != 0 {
if codeHash.Cmp(common.Hash{}) != 0 {
return true
}

Expand All @@ -36,5 +33,5 @@ func (s *DBImpl) Exist(addr common.Address) bool {
// is defined according to EIP161 (balance = nonce = code = 0).
func (s *DBImpl) Empty(addr common.Address) bool {
s.k.PrepareReplayedAddr(s.ctx, addr)
return s.GetBalance(addr).Cmp(utils.Big0) == 0 && s.GetNonce(addr) == 0 && bytes.Equal(s.GetCodeHash(addr).Bytes(), ethtypes.EmptyCodeHash.Bytes())
return s.GetBalance(addr).Cmp(utils.Big0) == 0 && s.GetNonce(addr) == 0 && s.GetCodeHash(addr).Cmp(common.Hash{}) == 0
}
4 changes: 2 additions & 2 deletions x/evm/state/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package state_test
import (
"testing"

ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper"
"github.com/sei-protocol/sei-chain/x/evm/state"
Expand All @@ -15,7 +15,7 @@ func TestCode(t *testing.T) {
_, addr := testkeeper.MockAddressPair()
statedb := state.NewDBImpl(ctx, k, false)

require.Equal(t, ethtypes.EmptyCodeHash, statedb.GetCodeHash(addr))
require.Equal(t, common.Hash{}, statedb.GetCodeHash(addr))
require.Nil(t, statedb.GetCode(addr))
require.Equal(t, 0, statedb.GetCodeSize(addr))

Expand Down
3 changes: 3 additions & 0 deletions x/evm/state/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package state

import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
Expand All @@ -26,4 +28,5 @@ type EVMKeeper interface {
GetNonce(sdk.Context, common.Address) uint64
SetNonce(sdk.Context, common.Address, uint64)
PrepareReplayedAddr(ctx sdk.Context, addr common.Address)
GetBalance(ctx sdk.Context, addr sdk.AccAddress) *big.Int
}

0 comments on commit 1b47795

Please sign in to comment.