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

Commit

Permalink
Impove test coverage of x/evm/statedb package
Browse files Browse the repository at this point in the history
Closes: #876
- coverage: 99.3% of statements
  • Loading branch information
yihuang committed Jan 6, 2022
1 parent f5b61e9 commit 3de7452
Show file tree
Hide file tree
Showing 5 changed files with 598 additions and 303 deletions.
17 changes: 0 additions & 17 deletions x/evm/statedb/access_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,6 @@ func newAccessList() *accessList {
}
}

// Copy creates an independent copy of an accessList.
func (al *accessList) Copy() *accessList {
cp := newAccessList()
for k, v := range al.addresses {
cp.addresses[k] = v
}
cp.slots = make([]map[common.Hash]struct{}, len(al.slots))
for i, slotMap := range al.slots {
newSlotmap := make(map[common.Hash]struct{}, len(slotMap))
for k := range slotMap {
newSlotmap[k] = struct{}{}
}
cp.slots[i] = newSlotmap
}
return cp
}

// AddAddress adds an address to the access list, and returns 'true' if the operation
// caused a change (addr was not previously in the list).
func (al *accessList) AddAddress(address common.Address) bool {
Expand Down
78 changes: 56 additions & 22 deletions x/evm/statedb/mock_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
package statedb_test

import (
"bytes"
"errors"
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/tharsis/ethermint/x/evm/statedb"
)

var _ statedb.Keeper = &MockKeeper{}
var (
_ statedb.Keeper = &MockKeeper{}
errAddress common.Address = common.BigToAddress(big.NewInt(100))
emptyCodeHash = crypto.Keccak256(nil)
)

type MockKeeper struct {
errAddress common.Address
type MockAcount struct {
account statedb.Account
states statedb.Storage
}

accounts map[common.Address]statedb.Account
states map[common.Address]statedb.Storage
type MockKeeper struct {
accounts map[common.Address]MockAcount
codes map[common.Hash][]byte
}

func NewMockKeeper() *MockKeeper {
return &MockKeeper{
errAddress: common.BigToAddress(big.NewInt(1)),

accounts: make(map[common.Address]statedb.Account),
states: make(map[common.Address]statedb.Storage),
accounts: make(map[common.Address]MockAcount),
codes: make(map[common.Hash][]byte),
}
}
Expand All @@ -33,35 +39,49 @@ func (k MockKeeper) GetAccount(ctx sdk.Context, addr common.Address) *statedb.Ac
if !ok {
return nil
}
return &acct
return &acct.account
}

func (k MockKeeper) GetState(ctx sdk.Context, addr common.Address, key common.Hash) common.Hash {
return k.states[addr][key]
return k.accounts[addr].states[key]
}

func (k MockKeeper) GetCode(ctx sdk.Context, codeHash common.Hash) []byte {
return k.codes[codeHash]
}

func (k MockKeeper) ForEachStorage(ctx sdk.Context, addr common.Address, cb func(key, value common.Hash) bool) {
for k, v := range k.states[addr] {
if !cb(k, v) {
return
if acct, ok := k.accounts[addr]; ok {
for k, v := range acct.states {
if !cb(k, v) {
return
}
}
}
}

func (k MockKeeper) SetAccount(ctx sdk.Context, addr common.Address, account statedb.Account) error {
k.accounts[addr] = account
if addr == errAddress {
return errors.New("mock db error")
}
acct, exists := k.accounts[addr]
if exists {
// update
acct.account = account
k.accounts[addr] = acct
} else {
k.accounts[addr] = MockAcount{account: account, states: make(statedb.Storage)}
}
return nil
}

func (k MockKeeper) SetState(ctx sdk.Context, addr common.Address, key common.Hash, value []byte) {
if len(value) == 0 {
delete(k.states[addr], key)
} else {
k.states[addr][key] = common.BytesToHash(value)
if acct, ok := k.accounts[addr]; ok {
if len(value) == 0 {
delete(acct.states, key)
} else {
acct.states[key] = common.BytesToHash(value)
}
}
}

Expand All @@ -70,11 +90,25 @@ func (k MockKeeper) SetCode(ctx sdk.Context, codeHash []byte, code []byte) {
}

func (k MockKeeper) DeleteAccount(ctx sdk.Context, addr common.Address) error {
if addr == errAddress {
return errors.New("mock db error")
}
old := k.accounts[addr]
delete(k.accounts, addr)
delete(k.states, addr)
if len(old.CodeHash) > 0 {
delete(k.codes, common.BytesToHash(old.CodeHash))
if !bytes.Equal(old.account.CodeHash, emptyCodeHash) {
delete(k.codes, common.BytesToHash(old.account.CodeHash))
}
return nil
}

func (k MockKeeper) Clone() *MockKeeper {
accounts := make(map[common.Address]MockAcount, len(k.accounts))
for k, v := range k.accounts {
accounts[k] = v
}
codes := make(map[common.Hash][]byte, len(k.codes))
for k, v := range k.codes {
codes[k] = v
}
return &MockKeeper{accounts, codes}
}
3 changes: 0 additions & 3 deletions x/evm/statedb/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ func (s *stateObject) setBalance(amount *big.Int) {
s.account.Balance = amount
}

// Return the gas back to the origin. Used by the Virtual machine or Closures
func (s *stateObject) ReturnGas(gas *big.Int) {}

//
// Attribute accessors
//
Expand Down
15 changes: 0 additions & 15 deletions x/evm/statedb/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ func (s *StateDB) Keeper() Keeper {
return s.keeper
}

// Context returns the embedded `sdk.Context`
func (s *StateDB) Context() sdk.Context {
return s.ctx
}

// AddLog adds a log, called by evm.
func (s *StateDB) AddLog(log *ethtypes.Log) {
s.journal.append(addLogChange{})
Expand Down Expand Up @@ -139,16 +134,6 @@ func (s *StateDB) GetNonce(addr common.Address) uint64 {
return 0
}

// TxIndex returns the current transaction index.
func (s *StateDB) TxIndex() uint {
return s.txConfig.TxIndex
}

// BlockHash returns the current block hash.
func (s *StateDB) BlockHash() common.Hash {
return s.txConfig.BlockHash
}

// GetCode returns the code of account, nil if not exists.
func (s *StateDB) GetCode(addr common.Address) []byte {
stateObject := s.getStateObject(addr)
Expand Down
Loading

0 comments on commit 3de7452

Please sign in to comment.