Skip to content

Commit

Permalink
refactor: Use Account.IsContract() in statedb instead of manual check…
Browse files Browse the repository at this point in the history
…, add IsContract tests
  • Loading branch information
drklee3 committed Feb 26, 2024
1 parent 02443b5 commit 3085c1e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
37 changes: 37 additions & 0 deletions x/evm/statedb/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,40 @@ func TestAccountIsEmpty(t *testing.T) {
})
}
}

func TestAccountIsContract(t *testing.T) {
tests := []struct {
name string
account statedb.Account
expected bool
}{
{
name: "Empty account",
account: statedb.Account{
Balance: big.NewInt(0),
Nonce: 0,
CodeHash: emptyCodeHash,
},
expected: false,
},
{
name: "Non-empty code hash",
account: statedb.Account{
Balance: big.NewInt(0),
Nonce: 0,
CodeHash: []byte{1, 2, 3},
},
expected: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(
t,
tt.expected,
tt.account.IsContract(),
)
})
}
}
6 changes: 4 additions & 2 deletions x/evm/statedb/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package statedb

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

Expand Down Expand Up @@ -112,6 +111,8 @@ func (s *StateDB) Empty(addr common.Address) bool {
return true
}

// This relies on the Account.CodeHash to be set correctly when the account
// code is set.
return account.IsEmpty()
}

Expand Down Expand Up @@ -142,7 +143,8 @@ func (s *StateDB) GetCode(addr common.Address) []byte {
return nil
}

if bytes.Equal(account.CodeHash, emptyCodeHash) {
// If the account has an empty codehash, there is no code.
if !account.IsContract() {
return nil
}

Expand Down

0 comments on commit 3085c1e

Please sign in to comment.