Skip to content

Commit

Permalink
Merge pull request #180 from hyeonLewis/eoa-with-code
Browse files Browse the repository at this point in the history
Add EIP-7702 compatibility
  • Loading branch information
hyeonLewis authored Dec 16, 2024
2 parents c5e0119 + 338a9c6 commit be96359
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
16 changes: 7 additions & 9 deletions blockchain/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package state

import (
"bytes"
"errors"
"fmt"
"math/big"
"sort"
Expand Down Expand Up @@ -1182,23 +1183,20 @@ func (s *StateDB) GetTxHash() common.Hash {
}

var (
errNotExistingAddress = fmt.Errorf("there is no account corresponding to the given address")
errNotContractAddress = fmt.Errorf("given address is not a contract address")
errNotExistingAddress = errors.New("there is no account corresponding to the given address")
errNotProgramAccount = errors.New("given address is not a program account")
)

func (s *StateDB) GetContractStorageRoot(contractAddr common.Address) (common.ExtHash, error) {
acc := s.GetAccount(contractAddr)
if acc == nil {
return common.ExtHash{}, errNotExistingAddress
}
if acc.Type() != account.SmartContractAccountType {
return common.ExtHash{}, errNotContractAddress
pa := account.GetProgramAccount(acc)
if pa == nil {
return common.ExtHash{}, errNotProgramAccount
}
contract, true := acc.(*account.SmartContractAccount)
if !true {
return common.ExtHash{}, errNotContractAddress
}
return contract.GetStorageRoot(), nil
return pa.GetStorageRoot(), nil
}

// Prepare handles the preparatory steps for executing a state transition with.
Expand Down
24 changes: 24 additions & 0 deletions blockchain/state/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import (
"testing/quick"

"github.com/kaiachain/kaia/blockchain/types"
"github.com/kaiachain/kaia/blockchain/types/account"
"github.com/kaiachain/kaia/common"
"github.com/kaiachain/kaia/crypto"
"github.com/kaiachain/kaia/params"
"github.com/kaiachain/kaia/storage/database"
"github.com/kaiachain/kaia/storage/statedb"
Expand Down Expand Up @@ -211,6 +213,28 @@ func TestStateObjects(t *testing.T) {
assert.Equal(t, 128, len(stateDB.stateObjects))
}

// TestCopiedEIP7702 tests that copied EOA has the same code related fields as the original EOA.
// This test has been introduced since the implementation of EIP-7702.
func TestCopiedEIP7702(t *testing.T) {
stateDB, _ := New(common.Hash{}, NewDatabase(database.NewMemoryDBManager()), nil, nil)

testCode := common.Hex2Bytes("0xef0100")
testCodeHash := crypto.Keccak256Hash(testCode)

addr := common.BytesToAddress([]byte{5})
stateDB.SetCodeToEOA(addr, testCode, params.Rules{})

assert.Equal(t, stateDB.GetCodeHash(addr), testCodeHash)
pa := account.GetProgramAccount(stateDB.GetAccount(addr))
assert.Equal(t, pa.GetStorageRoot(), types.EmptyRootHash.ExtendZero())

copy := stateDB.Copy()

assert.Equal(t, copy.GetCodeHash(addr), testCodeHash)
pa = account.GetProgramAccount(copy.GetAccount(addr))
assert.Equal(t, pa.GetStorageRoot(), types.EmptyRootHash.ExtendZero())
}

// Test that invalid pruning options are prohibited.
func TestPruningOptions(t *testing.T) {
opens := func(pruning bool, pruningNum bool) bool {
Expand Down
9 changes: 9 additions & 0 deletions blockchain/types/account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ func TestAccountSerializer(t *testing.T) {
"Empty EOA",
&ExternallyOwnedAccount{
AccountCommon: commonFieldsEmpty,
storageRoot: emptyRoot.ExtendZero(),
codeHash: emptyCodeHash,
codeInfo: codeinfoZero,
},
// 01 ["","","","01",[]]
"0x01c580808001c0",
Expand All @@ -171,6 +174,9 @@ func TestAccountSerializer(t *testing.T) {
"Nonempty EOA",
&ExternallyOwnedAccount{
AccountCommon: commonFields,
storageRoot: emptyRoot.ExtendZero(),
codeHash: emptyCodeHash,
codeInfo: codeinfoZero,
},
// 01 ["0x2a","0x12345678","","0x01",[]]
"0x01c92a84123456788001c0",
Expand All @@ -193,6 +199,9 @@ func TestAccountSerializer(t *testing.T) {
"AccountUpdated EOA",
&ExternallyOwnedAccount{
AccountCommon: commonFieldsUpdated,
storageRoot: emptyRoot.ExtendZero(),
codeHash: emptyCodeHash,
codeInfo: codeinfoZero,
},
// 01 ["0x2a","0x12345678","","0x02","0x038318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75"]
"0x01ea2a84123456788002a1038318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75",
Expand Down
17 changes: 15 additions & 2 deletions blockchain/types/account/externally_owned_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,22 @@ func (e *ExternallyOwnedAccount) Dump() {
}

func (e *ExternallyOwnedAccount) String() string {
return fmt.Sprintf("EOA: %s", e.AccountCommon.String())
return fmt.Sprintf(`EOA:%s
StorageRoot: %s
CodeHash: %s
CodeInfo: %s`,
e.AccountCommon.String(),
e.storageRoot.String(),
common.Bytes2Hex(e.codeHash),
e.codeInfo.String())
}

func (e *ExternallyOwnedAccount) DeepCopy() Account {
return &ExternallyOwnedAccount{
AccountCommon: e.AccountCommon.DeepCopy(),
storageRoot: e.storageRoot,
codeHash: common.CopyBytes(e.codeHash),
codeInfo: e.codeInfo,
}
}

Expand All @@ -114,7 +124,10 @@ func (e *ExternallyOwnedAccount) Equal(a Account) bool {
return false
}

return e.AccountCommon.Equal(e2.AccountCommon)
return e.AccountCommon.Equal(e2.AccountCommon) &&
e.storageRoot == e2.storageRoot &&
bytes.Equal(e.codeHash, e2.codeHash) &&
e.codeInfo == e2.codeInfo
}

func (e *ExternallyOwnedAccount) GetStorageRoot() common.ExtHash {
Expand Down

0 comments on commit be96359

Please sign in to comment.