Unverified Commit 817553cc authored by rjl493456442's avatar rjl493456442 Committed by GitHub

core, trie: track state change set with account address (#27815)

parent 43a1a48e
...@@ -173,10 +173,10 @@ func (ch resetObjectChange) revert(s *StateDB) { ...@@ -173,10 +173,10 @@ func (ch resetObjectChange) revert(s *StateDB) {
s.storages[ch.prev.addrHash] = ch.prevStorage s.storages[ch.prev.addrHash] = ch.prevStorage
} }
if ch.prevAccountOriginExist { if ch.prevAccountOriginExist {
s.accountsOrigin[ch.prev.addrHash] = ch.prevAccountOrigin s.accountsOrigin[ch.prev.address] = ch.prevAccountOrigin
} }
if ch.prevStorageOrigin != nil { if ch.prevStorageOrigin != nil {
s.storagesOrigin[ch.prev.addrHash] = ch.prevStorageOrigin s.storagesOrigin[ch.prev.address] = ch.prevStorageOrigin
} }
} }
......
...@@ -328,9 +328,9 @@ func (s *stateObject) updateTrie() (Trie, error) { ...@@ -328,9 +328,9 @@ func (s *stateObject) updateTrie() (Trie, error) {
// Cache the original value of mutated storage slots // Cache the original value of mutated storage slots
if origin == nil { if origin == nil {
if origin = s.db.storagesOrigin[s.addrHash]; origin == nil { if origin = s.db.storagesOrigin[s.address]; origin == nil {
origin = make(map[common.Hash][]byte) origin = make(map[common.Hash][]byte)
s.db.storagesOrigin[s.addrHash] = origin s.db.storagesOrigin[s.address] = origin
} }
} }
// Track the original value of slot only if it's mutated first time // Track the original value of slot only if it's mutated first time
......
This diff is collapsed.
...@@ -32,6 +32,7 @@ import ( ...@@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/triestate" "github.com/ethereum/go-ethereum/trie/triestate"
...@@ -171,11 +172,11 @@ func (test *stateTest) String() string { ...@@ -171,11 +172,11 @@ func (test *stateTest) String() string {
func (test *stateTest) run() bool { func (test *stateTest) run() bool {
var ( var (
roots []common.Hash roots []common.Hash
accountList []map[common.Hash][]byte accountList []map[common.Address][]byte
storageList []map[common.Hash]map[common.Hash][]byte storageList []map[common.Address]map[common.Hash][]byte
onCommit = func(states *triestate.Set) { onCommit = func(states *triestate.Set) {
accountList = append(accountList, copyAccounts(states.Accounts)) accountList = append(accountList, copySet(states.Accounts))
storageList = append(storageList, copyStorages(states.Storages)) storageList = append(storageList, copy2DSet(states.Storages))
} }
disk = rawdb.NewMemoryDatabase() disk = rawdb.NewMemoryDatabase()
tdb = trie.NewDatabaseWithConfig(disk, &trie.Config{OnCommit: onCommit}) tdb = trie.NewDatabaseWithConfig(disk, &trie.Config{OnCommit: onCommit})
...@@ -235,8 +236,9 @@ func (test *stateTest) run() bool { ...@@ -235,8 +236,9 @@ func (test *stateTest) run() bool {
// - the account was indeed not present in trie // - the account was indeed not present in trie
// - the account is present in new trie, nil->nil is regarded as invalid // - the account is present in new trie, nil->nil is regarded as invalid
// - the slots transition is correct // - the slots transition is correct
func (test *stateTest) verifyAccountCreation(next common.Hash, db *trie.Database, otr, ntr *trie.Trie, addrHash common.Hash, slots map[common.Hash][]byte) error { func (test *stateTest) verifyAccountCreation(next common.Hash, db *trie.Database, otr, ntr *trie.Trie, addr common.Address, slots map[common.Hash][]byte) error {
// Verify account change // Verify account change
addrHash := crypto.Keccak256Hash(addr.Bytes())
oBlob, err := otr.Get(addrHash.Bytes()) oBlob, err := otr.Get(addrHash.Bytes())
if err != nil { if err != nil {
return err return err
...@@ -285,8 +287,9 @@ func (test *stateTest) verifyAccountCreation(next common.Hash, db *trie.Database ...@@ -285,8 +287,9 @@ func (test *stateTest) verifyAccountCreation(next common.Hash, db *trie.Database
// - the account was indeed present in trie // - the account was indeed present in trie
// - the account in old trie matches the provided value // - the account in old trie matches the provided value
// - the slots transition is correct // - the slots transition is correct
func (test *stateTest) verifyAccountUpdate(next common.Hash, db *trie.Database, otr, ntr *trie.Trie, addrHash common.Hash, origin []byte, slots map[common.Hash][]byte) error { func (test *stateTest) verifyAccountUpdate(next common.Hash, db *trie.Database, otr, ntr *trie.Trie, addr common.Address, origin []byte, slots map[common.Hash][]byte) error {
// Verify account change // Verify account change
addrHash := crypto.Keccak256Hash(addr.Bytes())
oBlob, err := otr.Get(addrHash.Bytes()) oBlob, err := otr.Get(addrHash.Bytes())
if err != nil { if err != nil {
return err return err
...@@ -338,7 +341,7 @@ func (test *stateTest) verifyAccountUpdate(next common.Hash, db *trie.Database, ...@@ -338,7 +341,7 @@ func (test *stateTest) verifyAccountUpdate(next common.Hash, db *trie.Database,
return nil return nil
} }
func (test *stateTest) verify(root common.Hash, next common.Hash, db *trie.Database, accountsOrigin map[common.Hash][]byte, storagesOrigin map[common.Hash]map[common.Hash][]byte) error { func (test *stateTest) verify(root common.Hash, next common.Hash, db *trie.Database, accountsOrigin map[common.Address][]byte, storagesOrigin map[common.Address]map[common.Hash][]byte) error {
otr, err := trie.New(trie.StateTrieID(root), db) otr, err := trie.New(trie.StateTrieID(root), db)
if err != nil { if err != nil {
return err return err
...@@ -347,12 +350,12 @@ func (test *stateTest) verify(root common.Hash, next common.Hash, db *trie.Datab ...@@ -347,12 +350,12 @@ func (test *stateTest) verify(root common.Hash, next common.Hash, db *trie.Datab
if err != nil { if err != nil {
return err return err
} }
for addrHash, account := range accountsOrigin { for addr, account := range accountsOrigin {
var err error var err error
if len(account) == 0 { if len(account) == 0 {
err = test.verifyAccountCreation(next, db, otr, ntr, addrHash, storagesOrigin[addrHash]) err = test.verifyAccountCreation(next, db, otr, ntr, addr, storagesOrigin[addr])
} else { } else {
err = test.verifyAccountUpdate(next, db, otr, ntr, addrHash, accountsOrigin[addrHash], storagesOrigin[addrHash]) err = test.verifyAccountUpdate(next, db, otr, ntr, addr, accountsOrigin[addr], storagesOrigin[addr])
} }
if err != nil { if err != nil {
return err return err
......
...@@ -22,7 +22,7 @@ import "github.com/ethereum/go-ethereum/common" ...@@ -22,7 +22,7 @@ import "github.com/ethereum/go-ethereum/common"
// The value refers to the original content of state before the transition // The value refers to the original content of state before the transition
// is made. Nil means that the state was not present previously. // is made. Nil means that the state was not present previously.
type Set struct { type Set struct {
Accounts map[common.Hash][]byte // Mutated account set, nil means the account was not present Accounts map[common.Address][]byte // Mutated account set, nil means the account was not present
Storages map[common.Hash]map[common.Hash][]byte // Mutated storage set, nil means the slot was not present Storages map[common.Address]map[common.Hash][]byte // Mutated storage set, nil means the slot was not present
Incomplete map[common.Hash]struct{} // Indicator whether the storage slot is incomplete due to large deletion Incomplete map[common.Address]struct{} // Indicator whether the storage slot is incomplete due to large deletion
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment