From 0b92ab0b04192b448e5c30511d252f55fdf5bbb7 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Mon, 13 Sep 2021 15:09:47 +0200 Subject: [PATCH 01/10] core/state: abstracted "write account to trie" method --- cmd/geth/snapshot.go | 5 +++-- core/state/account/account.go | 32 ++++++++++++++++++++++++++++++++ core/state/database.go | 4 ++++ core/state/dump.go | 3 ++- core/state/iterator.go | 3 ++- core/state/pruner/pruner.go | 4 ++-- core/state/state_object.go | 20 +++----------------- core/state/statedb.go | 19 ++++++++----------- core/state/sync.go | 3 ++- core/state/sync_test.go | 3 ++- eth/protocols/snap/handler.go | 4 ++-- eth/protocols/snap/sync.go | 11 ++++++----- eth/protocols/snap/sync_test.go | 11 ++++++----- les/server_handler.go | 16 ++++++++-------- light/trie.go | 13 +++++++++++++ trie/secure_trie.go | 18 ++++++++++++++++++ trie/trie.go | 10 ++++++++++ trie/trie_test.go | 12 +++--------- 18 files changed, 126 insertions(+), 65 deletions(-) create mode 100644 core/state/account/account.go diff --git a/cmd/geth/snapshot.go b/cmd/geth/snapshot.go index 9976db49f601..9d0f200f0838 100644 --- a/cmd/geth/snapshot.go +++ b/cmd/geth/snapshot.go @@ -27,6 +27,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/core/state/pruner" "github.com/ethereum/go-ethereum/core/state/snapshot" "github.com/ethereum/go-ethereum/crypto" @@ -287,7 +288,7 @@ func traverseState(ctx *cli.Context) error { accIter := trie.NewIterator(t.NodeIterator(nil)) for accIter.Next() { accounts += 1 - var acc state.Account + var acc account.Account if err := rlp.DecodeBytes(accIter.Value, &acc); err != nil { log.Error("Invalid account encountered during traversal", "err", err) return err @@ -393,7 +394,7 @@ func traverseRawState(ctx *cli.Context) error { // dig into the storage trie further. if accIter.Leaf() { accounts += 1 - var acc state.Account + var acc account.Account if err := rlp.DecodeBytes(accIter.LeafBlob(), &acc); err != nil { log.Error("Invalid account encountered during traversal", "err", err) return errors.New("invalid account") diff --git a/core/state/account/account.go b/core/state/account/account.go new file mode 100644 index 000000000000..aa3cfff2dd54 --- /dev/null +++ b/core/state/account/account.go @@ -0,0 +1,32 @@ +// Copyright 2021 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package account + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/common" +) + +// Account is the Ethereum consensus representation of accounts. +// These objects are stored in the main account trie. +type Account struct { + Nonce uint64 + Balance *big.Int + Root common.Hash // merkle root of the storage trie + CodeHash []byte +} diff --git a/core/state/database.go b/core/state/database.go index e25d059c05b9..a6e0e46fe207 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -23,6 +23,7 @@ import ( "github.com/VictoriaMetrics/fastcache" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/trie" lru "github.com/hashicorp/golang-lru" @@ -70,6 +71,9 @@ type Trie interface { // trie.MissingNodeError is returned. TryGet(key []byte) ([]byte, error) + // TryUpdateAccount abstract an account write in the trie. + TryUpdateAccount(key []byte, acc account.Account) error + // TryUpdate associates key with value in the trie. If value has length zero, any // existing value is deleted from the trie. The value bytes must not be modified // by the caller while they are stored in the trie. If a node was not found in the diff --git a/core/state/dump.go b/core/state/dump.go index 00faa4ed6a6b..412143579dc1 100644 --- a/core/state/dump.go +++ b/core/state/dump.go @@ -23,6 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" @@ -140,7 +141,7 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey [] it := trie.NewIterator(s.trie.NodeIterator(conf.Start)) for it.Next() { - var data Account + var data account.Account if err := rlp.DecodeBytes(it.Value, &data); err != nil { panic(err) } diff --git a/core/state/iterator.go b/core/state/iterator.go index 6a5c73d3d13c..864b95c4b33f 100644 --- a/core/state/iterator.go +++ b/core/state/iterator.go @@ -21,6 +21,7 @@ import ( "fmt" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" ) @@ -104,7 +105,7 @@ func (it *NodeIterator) step() error { return nil } // Otherwise we've reached an account node, initiate data iteration - var account Account + var account account.Account if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob()), &account); err != nil { return err } diff --git a/core/state/pruner/pruner.go b/core/state/pruner/pruner.go index 4d6e41551184..832e4a86cc98 100644 --- a/core/state/pruner/pruner.go +++ b/core/state/pruner/pruner.go @@ -29,7 +29,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" - "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/core/state/snapshot" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -426,7 +426,7 @@ func extractGenesis(db ethdb.Database, stateBloom *stateBloom) error { // If it's a leaf node, yes we are touching an account, // dig into the storage trie further. if accIter.Leaf() { - var acc state.Account + var acc account.Account if err := rlp.DecodeBytes(accIter.LeafBlob(), &acc); err != nil { return err } diff --git a/core/state/state_object.go b/core/state/state_object.go index 7b366d64c742..db8137f9188f 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -19,7 +19,6 @@ package state import ( "bytes" "fmt" - "io" "math/big" "time" @@ -27,6 +26,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/core/state/account" ) var emptyCodeHash = crypto.Keccak256(nil) @@ -65,7 +65,7 @@ func (s Storage) Copy() Storage { type stateObject struct { address common.Address addrHash common.Hash // hash of ethereum address of the account - data Account + data account.Account db *StateDB // DB error. @@ -97,17 +97,8 @@ func (s *stateObject) empty() bool { return s.data.Nonce == 0 && s.data.Balance.Sign() == 0 && bytes.Equal(s.data.CodeHash, emptyCodeHash) } -// Account is the Ethereum consensus representation of accounts. -// These objects are stored in the main account trie. -type Account struct { - Nonce uint64 - Balance *big.Int - Root common.Hash // merkle root of the storage trie - CodeHash []byte -} - // newObject creates a state object. -func newObject(db *StateDB, address common.Address, data Account) *stateObject { +func newObject(db *StateDB, address common.Address, data account.Account) *stateObject { if data.Balance == nil { data.Balance = new(big.Int) } @@ -128,11 +119,6 @@ func newObject(db *StateDB, address common.Address, data Account) *stateObject { } } -// EncodeRLP implements rlp.Encoder. -func (s *stateObject) EncodeRLP(w io.Writer) error { - return rlp.Encode(w, s.data) -} - // setError remembers the first non-nil error it is called with. func (s *stateObject) setError(err error) { if s.dbErr == nil { diff --git a/core/state/statedb.go b/core/state/statedb.go index a6d47179ba80..6ac2ff3eca3d 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -26,6 +26,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/core/state/snapshot" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -460,12 +461,8 @@ func (s *StateDB) updateStateObject(obj *stateObject) { } // Encode the account and update the account trie addr := obj.Address() - - data, err := rlp.EncodeToBytes(obj) - if err != nil { - panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err)) - } - if err = s.trie.TryUpdate(addr[:], data); err != nil { + if err := s.trie.TryUpdateAccount(addr[:], obj.data); err != nil { + //if err := s.trie.TryUpdateAccount(addr[:], obj.data); err != nil { s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err)) } @@ -512,7 +509,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { } // If no live objects are available, attempt to use snapshots var ( - data *Account + data *account.Account err error ) if s.snap != nil { @@ -524,7 +521,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { if acc == nil { return nil } - data = &Account{ + data = &account.Account{ Nonce: acc.Nonce, Balance: acc.Balance, CodeHash: acc.CodeHash, @@ -551,7 +548,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { if len(enc) == 0 { return nil } - data = new(Account) + data = new(account.Account) if err := rlp.DecodeBytes(enc, data); err != nil { log.Error("Failed to decode state object", "addr", addr, "err", err) return nil @@ -588,7 +585,7 @@ func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) s.snapDestructs[prev.addrHash] = struct{}{} } } - newobj = newObject(s, addr, Account{}) + newobj = newObject(s, addr, account.Account{}) if prev == nil { s.journal.append(createObjectChange{account: &addr}) } else { @@ -942,7 +939,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) { } // The onleaf func is called _serially_, so we can reuse the same account // for unmarshalling every time. - var account Account + var account account.Account root, accountCommitted, err := s.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error { if err := rlp.DecodeBytes(leaf, &account); err != nil { return nil diff --git a/core/state/sync.go b/core/state/sync.go index 7a5852fb1945..167ba3c47665 100644 --- a/core/state/sync.go +++ b/core/state/sync.go @@ -20,6 +20,7 @@ import ( "bytes" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" @@ -43,7 +44,7 @@ func NewStateSync(root common.Hash, database ethdb.KeyValueReader, bloom *trie.S return err } } - var obj Account + var obj account.Account if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil { return err } diff --git a/core/state/sync_test.go b/core/state/sync_test.go index a13fcf56a376..75244538fb8d 100644 --- a/core/state/sync_test.go +++ b/core/state/sync_test.go @@ -23,6 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb/memorydb" @@ -203,7 +204,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) { } results[len(hashQueue)+i] = trie.SyncResult{Hash: crypto.Keccak256Hash(data), Data: data} } else { - var acc Account + var acc account.Account if err := rlp.DecodeBytes(srcTrie.Get(path[0]), &acc); err != nil { t.Fatalf("failed to decode account on path %x: %v", path, err) } diff --git a/eth/protocols/snap/handler.go b/eth/protocols/snap/handler.go index 3d668a2ebb6f..55b78072e5fe 100644 --- a/eth/protocols/snap/handler.go +++ b/eth/protocols/snap/handler.go @@ -23,7 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/core/state" + accounT "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/light" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" @@ -319,7 +319,7 @@ func handleMessage(backend Backend, peer *Peer) error { if err != nil { return p2p.Send(peer.rw, StorageRangesMsg, &StorageRangesPacket{ID: req.ID}) } - var acc state.Account + var acc accounT.Account if err := rlp.DecodeBytes(accTrie.Get(account[:]), &acc); err != nil { return p2p.Send(peer.rw, StorageRangesMsg, &StorageRangesPacket{ID: req.ID}) } diff --git a/eth/protocols/snap/sync.go b/eth/protocols/snap/sync.go index 646df03887f1..fcc0a4b99003 100644 --- a/eth/protocols/snap/sync.go +++ b/eth/protocols/snap/sync.go @@ -31,6 +31,7 @@ import ( "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + accounT "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/core/state/snapshot" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" @@ -125,8 +126,8 @@ type accountRequest struct { type accountResponse struct { task *accountTask // Task which this request is filling - hashes []common.Hash // Account hashes in the returned range - accounts []*state.Account // Expanded accounts in the returned range + hashes []common.Hash // Account hashes in the returned range + accounts []*accounT.Account // Expanded accounts in the returned range cont bool // Whether the account range has a continuation } @@ -2274,9 +2275,9 @@ func (s *Syncer) OnAccounts(peer SyncPeer, id uint64, hashes []common.Hash, acco s.scheduleRevertAccountRequest(req) return err } - accs := make([]*state.Account, len(accounts)) + accs := make([]*accounT.Account, len(accounts)) for i, account := range accounts { - acc := new(state.Account) + acc := new(accounT.Account) if err := rlp.DecodeBytes(account, acc); err != nil { panic(err) // We created these blobs, we must be able to decode them } @@ -2740,7 +2741,7 @@ func (s *Syncer) onHealByteCodes(peer SyncPeer, id uint64, bytecodes [][]byte) e // Note it's not concurrent safe, please handle the concurrent issue outside. func (s *Syncer) onHealState(paths [][]byte, value []byte) error { if len(paths) == 1 { - var account state.Account + var account accounT.Account if err := rlp.DecodeBytes(value, &account); err != nil { return nil } diff --git a/eth/protocols/snap/sync_test.go b/eth/protocols/snap/sync_test.go index 023fc8ee0058..5d86bd32f922 100644 --- a/eth/protocols/snap/sync_test.go +++ b/eth/protocols/snap/sync_test.go @@ -30,6 +30,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/light" @@ -1349,7 +1350,7 @@ func makeAccountTrieNoStorage(n int) (*trie.Trie, entrySlice) { accTrie, _ := trie.New(common.Hash{}, db) var entries entrySlice for i := uint64(1); i <= uint64(n); i++ { - value, _ := rlp.EncodeToBytes(state.Account{ + value, _ := rlp.EncodeToBytes(account.Account{ Nonce: i, Balance: big.NewInt(int64(i)), Root: emptyRoot, @@ -1394,7 +1395,7 @@ func makeBoundaryAccountTrie(n int) (*trie.Trie, entrySlice) { } // Fill boundary accounts for i := 0; i < len(boundaries); i++ { - value, _ := rlp.EncodeToBytes(state.Account{ + value, _ := rlp.EncodeToBytes(account.Account{ Nonce: uint64(0), Balance: big.NewInt(int64(i)), Root: emptyRoot, @@ -1406,7 +1407,7 @@ func makeBoundaryAccountTrie(n int) (*trie.Trie, entrySlice) { } // Fill other accounts if required for i := uint64(1); i <= uint64(n); i++ { - value, _ := rlp.EncodeToBytes(state.Account{ + value, _ := rlp.EncodeToBytes(account.Account{ Nonce: i, Balance: big.NewInt(int64(i)), Root: emptyRoot, @@ -1442,7 +1443,7 @@ func makeAccountTrieWithStorageWithUniqueStorage(accounts, slots int, code bool) stTrie, stEntries := makeStorageTrieWithSeed(uint64(slots), i, db) stRoot := stTrie.Hash() stTrie.Commit(nil) - value, _ := rlp.EncodeToBytes(state.Account{ + value, _ := rlp.EncodeToBytes(account.Account{ Nonce: i, Balance: big.NewInt(int64(i)), Root: stRoot, @@ -1489,7 +1490,7 @@ func makeAccountTrieWithStorage(accounts, slots int, code, boundary bool) (*trie if code { codehash = getCodeHash(i) } - value, _ := rlp.EncodeToBytes(state.Account{ + value, _ := rlp.EncodeToBytes(account.Account{ Nonce: i, Balance: big.NewInt(int64(i)), Root: stRoot, diff --git a/les/server_handler.go b/les/server_handler.go index fa20fd7b3b51..330aced04c9f 100644 --- a/les/server_handler.go +++ b/les/server_handler.go @@ -27,7 +27,7 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/forkid" "github.com/ethereum/go-ethereum/core/rawdb" - "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/les/flowcontrol" "github.com/ethereum/go-ethereum/light" @@ -358,20 +358,20 @@ func (h *serverHandler) AddTxsSync() bool { } // getAccount retrieves an account from the state based on root. -func getAccount(triedb *trie.Database, root, hash common.Hash) (state.Account, error) { +func getAccount(triedb *trie.Database, root, hash common.Hash) (account.Account, error) { trie, err := trie.New(root, triedb) if err != nil { - return state.Account{}, err + return account.Account{}, err } blob, err := trie.TryGet(hash[:]) if err != nil { - return state.Account{}, err + return account.Account{}, err } - var account state.Account - if err = rlp.DecodeBytes(blob, &account); err != nil { - return state.Account{}, err + var acc account.Account + if err = rlp.DecodeBytes(blob, &acc); err != nil { + return account.Account{}, err } - return account, nil + return acc, nil } // getHelperTrie returns the post-processed trie root for the given trie ID and section index diff --git a/light/trie.go b/light/trie.go index 39e928bbe12c..b534da5d84fd 100644 --- a/light/trie.go +++ b/light/trie.go @@ -24,9 +24,11 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" ) @@ -111,6 +113,17 @@ func (t *odrTrie) TryGet(key []byte) ([]byte, error) { return res, err } +func (t *odrTrie) TryUpdateAccount(key []byte, acc account.Account) error { + key = crypto.Keccak256(key) + value, err := rlp.EncodeToBytes(acc) + if err != nil { + return fmt.Errorf("Decoding error in account update: %w", err) + } + return t.do(key, func() error { + return t.trie.TryUpdate(key, value) + }) +} + func (t *odrTrie) TryUpdate(key, value []byte) error { key = crypto.Keccak256(key) return t.do(key, func() error { diff --git a/trie/secure_trie.go b/trie/secure_trie.go index 4d0050bc59d2..b82ce2ddfc7a 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -20,7 +20,9 @@ import ( "fmt" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/rlp" ) // SecureTrie wraps a trie with key hashing. In a secure trie, all @@ -85,6 +87,22 @@ func (t *SecureTrie) TryGetNode(path []byte) ([]byte, int, error) { return t.trie.TryGetNode(path) } +// TryUpdate account will abstract the write of an account to the +// secure trie. +func (t *SecureTrie) TryUpdateAccount(key []byte, acc account.Account) error { + hk := t.hashKey(key) + data, err := rlp.EncodeToBytes(acc) + if err != nil { + return err + } + err = t.trie.TryUpdate(hk, data) + if err != nil { + return err + } + t.getSecKeyCache()[string(hk)] = common.CopyBytes(key) + return nil +} + // Update associates key with value in the trie. Subsequent calls to // Get will return value. If value has length zero, any existing value // is deleted from the trie and calls to Get will return nil. diff --git a/trie/trie.go b/trie/trie.go index 7ea7efa835f8..a7c121d4eaf6 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -25,7 +25,9 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/rlp" ) var ( @@ -246,6 +248,14 @@ func (t *Trie) Update(key, value []byte) { } } +func (t *Trie) TryUpdateAccount(addr []byte, account account.Account) error { + data, err := rlp.EncodeToBytes(account) + if err != nil { + return fmt.Errorf("can't encode object at %x: %w", addr[:], err) + } + return t.TryUpdate(addr, data) +} + // TryUpdate associates key with value in the trie. Subsequent calls to // Get will return value. If value has length zero, any existing value // is deleted from the trie and calls to Get will return nil. diff --git a/trie/trie_test.go b/trie/trie_test.go index ae1871e42309..c55e418d18cc 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -32,6 +32,7 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb/leveldb" @@ -553,13 +554,6 @@ func BenchmarkHash(b *testing.B) { trie.Hash() } -type account struct { - Nonce uint64 - Balance *big.Int - Root common.Hash - Code []byte -} - // Benchmarks the trie Commit following a Hash. Since the trie caches the result of any operation, // we cannot use b.N as the number of hashing rouns, since all rounds apart from // the first one will be NOOP. As such, we'll use b.N as the number of account to @@ -568,7 +562,7 @@ func BenchmarkCommitAfterHash(b *testing.B) { b.Run("no-onleaf", func(b *testing.B) { benchmarkCommitAfterHash(b, nil) }) - var a account + var a account.Account onleaf := func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash) error { rlp.DecodeBytes(leaf, &a) return nil @@ -664,7 +658,7 @@ func makeAccounts(size int) (addresses [][20]byte, accounts [][]byte) { balanceBytes := make([]byte, numBytes) random.Read(balanceBytes) balance := new(big.Int).SetBytes(balanceBytes) - data, _ := rlp.EncodeToBytes(&account{nonce, balance, root, code}) + data, _ := rlp.EncodeToBytes(&account.Account{nonce, balance, root, code}) accounts[i] = data } return addresses, accounts From 1e142f936d3c0fccb809d19291f6fc0cab6b702e Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Mon, 13 Sep 2021 17:30:05 +0200 Subject: [PATCH 02/10] fix appveyor build --- core/state/account/account.go | 2 +- core/state/state_object.go | 2 +- eth/protocols/snap/sync_test.go | 1 - trie/trie.go | 2 +- trie/trie_test.go | 2 +- 5 files changed, 4 insertions(+), 5 deletions(-) diff --git a/core/state/account/account.go b/core/state/account/account.go index aa3cfff2dd54..718fadb48126 100644 --- a/core/state/account/account.go +++ b/core/state/account/account.go @@ -16,7 +16,7 @@ package account -import ( +import ( "math/big" "github.com/ethereum/go-ethereum/common" diff --git a/core/state/state_object.go b/core/state/state_object.go index db8137f9188f..21b9263f0f08 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -23,10 +23,10 @@ import ( "time" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/rlp" - "github.com/ethereum/go-ethereum/core/state/account" ) var emptyCodeHash = crypto.Keccak256(nil) diff --git a/eth/protocols/snap/sync_test.go b/eth/protocols/snap/sync_test.go index 5d86bd32f922..4230e71bc6a8 100644 --- a/eth/protocols/snap/sync_test.go +++ b/eth/protocols/snap/sync_test.go @@ -29,7 +29,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" - "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state/account" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" diff --git a/trie/trie.go b/trie/trie.go index a7c121d4eaf6..311ae5de36d6 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -24,8 +24,8 @@ import ( "sync" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/core/state/account" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" ) diff --git a/trie/trie_test.go b/trie/trie_test.go index c55e418d18cc..29dc650e54d4 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -658,7 +658,7 @@ func makeAccounts(size int) (addresses [][20]byte, accounts [][]byte) { balanceBytes := make([]byte, numBytes) random.Read(balanceBytes) balance := new(big.Int).SetBytes(balanceBytes) - data, _ := rlp.EncodeToBytes(&account.Account{nonce, balance, root, code}) + data, _ := rlp.EncodeToBytes(&account.Account{Nonce: nonce, Balance: balance, Root: root, CodeHash: code}) accounts[i] = data } return addresses, accounts From 14ad53aba9c48fed2d74037ba9f3c6993816759e Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Fri, 17 Sep 2021 11:07:47 +0200 Subject: [PATCH 03/10] Apply suggestions from code review Co-authored-by: Martin Holst Swende --- light/trie.go | 2 +- trie/secure_trie.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/light/trie.go b/light/trie.go index b534da5d84fd..381a4098ca40 100644 --- a/light/trie.go +++ b/light/trie.go @@ -117,7 +117,7 @@ func (t *odrTrie) TryUpdateAccount(key []byte, acc account.Account) error { key = crypto.Keccak256(key) value, err := rlp.EncodeToBytes(acc) if err != nil { - return fmt.Errorf("Decoding error in account update: %w", err) + return fmt.Errorf("decoding error in account update: %w", err) } return t.do(key, func() error { return t.trie.TryUpdate(key, value) diff --git a/trie/secure_trie.go b/trie/secure_trie.go index b82ce2ddfc7a..bc2ddb027cae 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -95,8 +95,7 @@ func (t *SecureTrie) TryUpdateAccount(key []byte, acc account.Account) error { if err != nil { return err } - err = t.trie.TryUpdate(hk, data) - if err != nil { + if err := t.trie.TryUpdate(hk, data); err != nil { return err } t.getSecKeyCache()[string(hk)] = common.CopyBytes(key) From 2d803b7aad8c512e9523f82dcf26925ad2bf9029 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Fri, 17 Sep 2021 19:45:23 +0200 Subject: [PATCH 04/10] review feedback --- cmd/geth/snapshot.go | 6 +++--- .../{account/account.go => accounts/accounts.go} | 0 core/state/database.go | 4 ++-- core/state/dump.go | 4 ++-- core/state/iterator.go | 4 ++-- core/state/pruner/pruner.go | 4 ++-- core/state/state_object.go | 12 +++++++++--- core/state/statedb.go | 14 +++++++------- core/state/sync.go | 4 ++-- core/state/sync_test.go | 4 ++-- eth/protocols/snap/handler.go | 4 ++-- eth/protocols/snap/sync.go | 12 ++++++------ eth/protocols/snap/sync_test.go | 12 ++++++------ les/server_handler.go | 12 ++++++------ light/trie.go | 4 ++-- trie/secure_trie.go | 4 ++-- trie/trie.go | 4 ++-- trie/trie_test.go | 6 +++--- 18 files changed, 60 insertions(+), 54 deletions(-) rename core/state/{account/account.go => accounts/accounts.go} (100%) diff --git a/cmd/geth/snapshot.go b/cmd/geth/snapshot.go index 9d0f200f0838..8a0ca0b8e669 100644 --- a/cmd/geth/snapshot.go +++ b/cmd/geth/snapshot.go @@ -27,7 +27,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" - "github.com/ethereum/go-ethereum/core/state/account" + accs "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/core/state/pruner" "github.com/ethereum/go-ethereum/core/state/snapshot" "github.com/ethereum/go-ethereum/crypto" @@ -288,7 +288,7 @@ func traverseState(ctx *cli.Context) error { accIter := trie.NewIterator(t.NodeIterator(nil)) for accIter.Next() { accounts += 1 - var acc account.Account + var acc accs.Account if err := rlp.DecodeBytes(accIter.Value, &acc); err != nil { log.Error("Invalid account encountered during traversal", "err", err) return err @@ -394,7 +394,7 @@ func traverseRawState(ctx *cli.Context) error { // dig into the storage trie further. if accIter.Leaf() { accounts += 1 - var acc account.Account + var acc accs.Account if err := rlp.DecodeBytes(accIter.LeafBlob(), &acc); err != nil { log.Error("Invalid account encountered during traversal", "err", err) return errors.New("invalid account") diff --git a/core/state/account/account.go b/core/state/accounts/accounts.go similarity index 100% rename from core/state/account/account.go rename to core/state/accounts/accounts.go diff --git a/core/state/database.go b/core/state/database.go index a6e0e46fe207..45889bd4501d 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -23,7 +23,7 @@ import ( "github.com/VictoriaMetrics/fastcache" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" - "github.com/ethereum/go-ethereum/core/state/account" + accounts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/trie" lru "github.com/hashicorp/golang-lru" @@ -72,7 +72,7 @@ type Trie interface { TryGet(key []byte) ([]byte, error) // TryUpdateAccount abstract an account write in the trie. - TryUpdateAccount(key []byte, acc account.Account) error + TryUpdateAccount(key []byte, acc *accounts.Account) error // TryUpdate associates key with value in the trie. If value has length zero, any // existing value is deleted from the trie. The value bytes must not be modified diff --git a/core/state/dump.go b/core/state/dump.go index 412143579dc1..e4f451d63389 100644 --- a/core/state/dump.go +++ b/core/state/dump.go @@ -23,7 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/ethereum/go-ethereum/core/state/account" + acts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" @@ -141,7 +141,7 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey [] it := trie.NewIterator(s.trie.NodeIterator(conf.Start)) for it.Next() { - var data account.Account + var data acts.Account if err := rlp.DecodeBytes(it.Value, &data); err != nil { panic(err) } diff --git a/core/state/iterator.go b/core/state/iterator.go index 864b95c4b33f..3d93f2a58e2a 100644 --- a/core/state/iterator.go +++ b/core/state/iterator.go @@ -21,7 +21,7 @@ import ( "fmt" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/state/account" + accounts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" ) @@ -105,7 +105,7 @@ func (it *NodeIterator) step() error { return nil } // Otherwise we've reached an account node, initiate data iteration - var account account.Account + var account accounts.Account if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob()), &account); err != nil { return err } diff --git a/core/state/pruner/pruner.go b/core/state/pruner/pruner.go index 832e4a86cc98..17def67a8ebb 100644 --- a/core/state/pruner/pruner.go +++ b/core/state/pruner/pruner.go @@ -29,7 +29,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" - "github.com/ethereum/go-ethereum/core/state/account" + accounts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/core/state/snapshot" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -426,7 +426,7 @@ func extractGenesis(db ethdb.Database, stateBloom *stateBloom) error { // If it's a leaf node, yes we are touching an account, // dig into the storage trie further. if accIter.Leaf() { - var acc account.Account + var acc accounts.Account if err := rlp.DecodeBytes(accIter.LeafBlob(), &acc); err != nil { return err } diff --git a/core/state/state_object.go b/core/state/state_object.go index 21b9263f0f08..dc5e1a81db81 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -19,11 +19,12 @@ package state import ( "bytes" "fmt" + "io" "math/big" "time" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/state/account" + accounts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/rlp" @@ -65,7 +66,7 @@ func (s Storage) Copy() Storage { type stateObject struct { address common.Address addrHash common.Hash // hash of ethereum address of the account - data account.Account + data accounts.Account db *StateDB // DB error. @@ -98,7 +99,7 @@ func (s *stateObject) empty() bool { } // newObject creates a state object. -func newObject(db *StateDB, address common.Address, data account.Account) *stateObject { +func newObject(db *StateDB, address common.Address, data accounts.Account) *stateObject { if data.Balance == nil { data.Balance = new(big.Int) } @@ -119,6 +120,11 @@ func newObject(db *StateDB, address common.Address, data account.Account) *state } } +// EncodeRLP implements rlp.Encoder. +func (s *stateObject) EncodeRLP(io.Writer) error { + panic("deprecated code, should not be called") +} + // setError remembers the first non-nil error it is called with. func (s *stateObject) setError(err error) { if s.dbErr == nil { diff --git a/core/state/statedb.go b/core/state/statedb.go index 6ac2ff3eca3d..f8d10c2dc816 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -26,7 +26,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" - "github.com/ethereum/go-ethereum/core/state/account" + accounts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/core/state/snapshot" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -461,7 +461,7 @@ func (s *StateDB) updateStateObject(obj *stateObject) { } // Encode the account and update the account trie addr := obj.Address() - if err := s.trie.TryUpdateAccount(addr[:], obj.data); err != nil { + if err := s.trie.TryUpdateAccount(addr[:], &obj.data); err != nil { //if err := s.trie.TryUpdateAccount(addr[:], obj.data); err != nil { s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err)) } @@ -509,7 +509,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { } // If no live objects are available, attempt to use snapshots var ( - data *account.Account + data *accounts.Account err error ) if s.snap != nil { @@ -521,7 +521,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { if acc == nil { return nil } - data = &account.Account{ + data = &accounts.Account{ Nonce: acc.Nonce, Balance: acc.Balance, CodeHash: acc.CodeHash, @@ -548,7 +548,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { if len(enc) == 0 { return nil } - data = new(account.Account) + data = new(accounts.Account) if err := rlp.DecodeBytes(enc, data); err != nil { log.Error("Failed to decode state object", "addr", addr, "err", err) return nil @@ -585,7 +585,7 @@ func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) s.snapDestructs[prev.addrHash] = struct{}{} } } - newobj = newObject(s, addr, account.Account{}) + newobj = newObject(s, addr, accounts.Account{}) if prev == nil { s.journal.append(createObjectChange{account: &addr}) } else { @@ -939,7 +939,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) { } // The onleaf func is called _serially_, so we can reuse the same account // for unmarshalling every time. - var account account.Account + var account accounts.Account root, accountCommitted, err := s.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error { if err := rlp.DecodeBytes(leaf, &account); err != nil { return nil diff --git a/core/state/sync.go b/core/state/sync.go index 167ba3c47665..43f78849c71b 100644 --- a/core/state/sync.go +++ b/core/state/sync.go @@ -20,7 +20,7 @@ import ( "bytes" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/state/account" + accounts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" @@ -44,7 +44,7 @@ func NewStateSync(root common.Hash, database ethdb.KeyValueReader, bloom *trie.S return err } } - var obj account.Account + var obj accounts.Account if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil { return err } diff --git a/core/state/sync_test.go b/core/state/sync_test.go index 75244538fb8d..3dfdf3c68ac5 100644 --- a/core/state/sync_test.go +++ b/core/state/sync_test.go @@ -23,7 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" - "github.com/ethereum/go-ethereum/core/state/account" + accs "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb/memorydb" @@ -204,7 +204,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) { } results[len(hashQueue)+i] = trie.SyncResult{Hash: crypto.Keccak256Hash(data), Data: data} } else { - var acc account.Account + var acc accs.Account if err := rlp.DecodeBytes(srcTrie.Get(path[0]), &acc); err != nil { t.Fatalf("failed to decode account on path %x: %v", path, err) } diff --git a/eth/protocols/snap/handler.go b/eth/protocols/snap/handler.go index 55b78072e5fe..35ab5f8147fe 100644 --- a/eth/protocols/snap/handler.go +++ b/eth/protocols/snap/handler.go @@ -23,7 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" - accounT "github.com/ethereum/go-ethereum/core/state/account" + accounts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/light" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" @@ -319,7 +319,7 @@ func handleMessage(backend Backend, peer *Peer) error { if err != nil { return p2p.Send(peer.rw, StorageRangesMsg, &StorageRangesPacket{ID: req.ID}) } - var acc accounT.Account + var acc accounts.Account if err := rlp.DecodeBytes(accTrie.Get(account[:]), &acc); err != nil { return p2p.Send(peer.rw, StorageRangesMsg, &StorageRangesPacket{ID: req.ID}) } diff --git a/eth/protocols/snap/sync.go b/eth/protocols/snap/sync.go index fcc0a4b99003..33110ba05835 100644 --- a/eth/protocols/snap/sync.go +++ b/eth/protocols/snap/sync.go @@ -31,7 +31,7 @@ import ( "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" - accounT "github.com/ethereum/go-ethereum/core/state/account" + acts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/core/state/snapshot" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" @@ -126,8 +126,8 @@ type accountRequest struct { type accountResponse struct { task *accountTask // Task which this request is filling - hashes []common.Hash // Account hashes in the returned range - accounts []*accounT.Account // Expanded accounts in the returned range + hashes []common.Hash // Account hashes in the returned range + accounts []*acts.Account // Expanded accounts in the returned range cont bool // Whether the account range has a continuation } @@ -2275,9 +2275,9 @@ func (s *Syncer) OnAccounts(peer SyncPeer, id uint64, hashes []common.Hash, acco s.scheduleRevertAccountRequest(req) return err } - accs := make([]*accounT.Account, len(accounts)) + accs := make([]*acts.Account, len(accounts)) for i, account := range accounts { - acc := new(accounT.Account) + acc := new(acts.Account) if err := rlp.DecodeBytes(account, acc); err != nil { panic(err) // We created these blobs, we must be able to decode them } @@ -2741,7 +2741,7 @@ func (s *Syncer) onHealByteCodes(peer SyncPeer, id uint64, bytecodes [][]byte) e // Note it's not concurrent safe, please handle the concurrent issue outside. func (s *Syncer) onHealState(paths [][]byte, value []byte) error { if len(paths) == 1 { - var account accounT.Account + var account acts.Account if err := rlp.DecodeBytes(value, &account); err != nil { return nil } diff --git a/eth/protocols/snap/sync_test.go b/eth/protocols/snap/sync_test.go index 4230e71bc6a8..da720a8f3c28 100644 --- a/eth/protocols/snap/sync_test.go +++ b/eth/protocols/snap/sync_test.go @@ -29,7 +29,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" - "github.com/ethereum/go-ethereum/core/state/account" + acts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/light" @@ -1349,7 +1349,7 @@ func makeAccountTrieNoStorage(n int) (*trie.Trie, entrySlice) { accTrie, _ := trie.New(common.Hash{}, db) var entries entrySlice for i := uint64(1); i <= uint64(n); i++ { - value, _ := rlp.EncodeToBytes(account.Account{ + value, _ := rlp.EncodeToBytes(acts.Account{ Nonce: i, Balance: big.NewInt(int64(i)), Root: emptyRoot, @@ -1394,7 +1394,7 @@ func makeBoundaryAccountTrie(n int) (*trie.Trie, entrySlice) { } // Fill boundary accounts for i := 0; i < len(boundaries); i++ { - value, _ := rlp.EncodeToBytes(account.Account{ + value, _ := rlp.EncodeToBytes(acts.Account{ Nonce: uint64(0), Balance: big.NewInt(int64(i)), Root: emptyRoot, @@ -1406,7 +1406,7 @@ func makeBoundaryAccountTrie(n int) (*trie.Trie, entrySlice) { } // Fill other accounts if required for i := uint64(1); i <= uint64(n); i++ { - value, _ := rlp.EncodeToBytes(account.Account{ + value, _ := rlp.EncodeToBytes(acts.Account{ Nonce: i, Balance: big.NewInt(int64(i)), Root: emptyRoot, @@ -1442,7 +1442,7 @@ func makeAccountTrieWithStorageWithUniqueStorage(accounts, slots int, code bool) stTrie, stEntries := makeStorageTrieWithSeed(uint64(slots), i, db) stRoot := stTrie.Hash() stTrie.Commit(nil) - value, _ := rlp.EncodeToBytes(account.Account{ + value, _ := rlp.EncodeToBytes(acts.Account{ Nonce: i, Balance: big.NewInt(int64(i)), Root: stRoot, @@ -1489,7 +1489,7 @@ func makeAccountTrieWithStorage(accounts, slots int, code, boundary bool) (*trie if code { codehash = getCodeHash(i) } - value, _ := rlp.EncodeToBytes(account.Account{ + value, _ := rlp.EncodeToBytes(acts.Account{ Nonce: i, Balance: big.NewInt(int64(i)), Root: stRoot, diff --git a/les/server_handler.go b/les/server_handler.go index 330aced04c9f..9c04c56f2be8 100644 --- a/les/server_handler.go +++ b/les/server_handler.go @@ -27,7 +27,7 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/forkid" "github.com/ethereum/go-ethereum/core/rawdb" - "github.com/ethereum/go-ethereum/core/state/account" + accounts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/les/flowcontrol" "github.com/ethereum/go-ethereum/light" @@ -358,18 +358,18 @@ func (h *serverHandler) AddTxsSync() bool { } // getAccount retrieves an account from the state based on root. -func getAccount(triedb *trie.Database, root, hash common.Hash) (account.Account, error) { +func getAccount(triedb *trie.Database, root, hash common.Hash) (accounts.Account, error) { trie, err := trie.New(root, triedb) if err != nil { - return account.Account{}, err + return accounts.Account{}, err } blob, err := trie.TryGet(hash[:]) if err != nil { - return account.Account{}, err + return accounts.Account{}, err } - var acc account.Account + var acc accounts.Account if err = rlp.DecodeBytes(blob, &acc); err != nil { - return account.Account{}, err + return accounts.Account{}, err } return acc, nil } diff --git a/light/trie.go b/light/trie.go index 381a4098ca40..2c657eeb3f78 100644 --- a/light/trie.go +++ b/light/trie.go @@ -24,7 +24,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" - "github.com/ethereum/go-ethereum/core/state/account" + accounts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" @@ -113,7 +113,7 @@ func (t *odrTrie) TryGet(key []byte) ([]byte, error) { return res, err } -func (t *odrTrie) TryUpdateAccount(key []byte, acc account.Account) error { +func (t *odrTrie) TryUpdateAccount(key []byte, acc *accounts.Account) error { key = crypto.Keccak256(key) value, err := rlp.EncodeToBytes(acc) if err != nil { diff --git a/trie/secure_trie.go b/trie/secure_trie.go index bc2ddb027cae..fa3cb869bc17 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -20,7 +20,7 @@ import ( "fmt" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/state/account" + accounts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" ) @@ -89,7 +89,7 @@ func (t *SecureTrie) TryGetNode(path []byte) ([]byte, int, error) { // TryUpdate account will abstract the write of an account to the // secure trie. -func (t *SecureTrie) TryUpdateAccount(key []byte, acc account.Account) error { +func (t *SecureTrie) TryUpdateAccount(key []byte, acc *accounts.Account) error { hk := t.hashKey(key) data, err := rlp.EncodeToBytes(acc) if err != nil { diff --git a/trie/trie.go b/trie/trie.go index 311ae5de36d6..823a37e10e31 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -24,7 +24,7 @@ import ( "sync" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/state/account" + accounts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" @@ -248,7 +248,7 @@ func (t *Trie) Update(key, value []byte) { } } -func (t *Trie) TryUpdateAccount(addr []byte, account account.Account) error { +func (t *Trie) TryUpdateAccount(addr []byte, account accounts.Account) error { data, err := rlp.EncodeToBytes(account) if err != nil { return fmt.Errorf("can't encode object at %x: %w", addr[:], err) diff --git a/trie/trie_test.go b/trie/trie_test.go index 29dc650e54d4..7d826726b87c 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -32,7 +32,7 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/state/account" + accs "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb/leveldb" @@ -562,7 +562,7 @@ func BenchmarkCommitAfterHash(b *testing.B) { b.Run("no-onleaf", func(b *testing.B) { benchmarkCommitAfterHash(b, nil) }) - var a account.Account + var a accs.Account onleaf := func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash) error { rlp.DecodeBytes(leaf, &a) return nil @@ -658,7 +658,7 @@ func makeAccounts(size int) (addresses [][20]byte, accounts [][]byte) { balanceBytes := make([]byte, numBytes) random.Read(balanceBytes) balance := new(big.Int).SetBytes(balanceBytes) - data, _ := rlp.EncodeToBytes(&account.Account{Nonce: nonce, Balance: balance, Root: root, CodeHash: code}) + data, _ := rlp.EncodeToBytes(&accs.Account{Nonce: nonce, Balance: balance, Root: root, CodeHash: code}) accounts[i] = data } return addresses, accounts From c03d79e98ffeabf31ff9d9add3880c2c397d3953 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 21 Sep 2021 13:17:50 +0200 Subject: [PATCH 05/10] core/state/accounts: move Account to core/types --- cmd/geth/snapshot.go | 6 +++--- core/state/accounts/accounts.go | 32 -------------------------------- core/state/database.go | 4 ++-- core/state/dump.go | 4 ++-- core/state/iterator.go | 4 ++-- core/state/pruner/pruner.go | 3 +-- core/state/state_object.go | 6 +++--- core/state/statedb.go | 11 +++++------ core/state/sync.go | 4 ++-- core/state/sync_test.go | 4 ++-- eth/protocols/snap/handler.go | 4 ++-- eth/protocols/snap/sync.go | 12 ++++++------ eth/protocols/snap/sync_test.go | 12 ++++++------ les/server_handler.go | 12 ++++++------ light/trie.go | 3 +-- trie/secure_trie.go | 4 ++-- trie/trie.go | 4 ++-- trie/trie_test.go | 6 +++--- 18 files changed, 50 insertions(+), 85 deletions(-) delete mode 100644 core/state/accounts/accounts.go diff --git a/cmd/geth/snapshot.go b/cmd/geth/snapshot.go index 8a0ca0b8e669..85b5b66f9d07 100644 --- a/cmd/geth/snapshot.go +++ b/cmd/geth/snapshot.go @@ -27,9 +27,9 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" - accs "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/core/state/pruner" "github.com/ethereum/go-ethereum/core/state/snapshot" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" @@ -288,7 +288,7 @@ func traverseState(ctx *cli.Context) error { accIter := trie.NewIterator(t.NodeIterator(nil)) for accIter.Next() { accounts += 1 - var acc accs.Account + var acc types.Account if err := rlp.DecodeBytes(accIter.Value, &acc); err != nil { log.Error("Invalid account encountered during traversal", "err", err) return err @@ -394,7 +394,7 @@ func traverseRawState(ctx *cli.Context) error { // dig into the storage trie further. if accIter.Leaf() { accounts += 1 - var acc accs.Account + var acc types.Account if err := rlp.DecodeBytes(accIter.LeafBlob(), &acc); err != nil { log.Error("Invalid account encountered during traversal", "err", err) return errors.New("invalid account") diff --git a/core/state/accounts/accounts.go b/core/state/accounts/accounts.go deleted file mode 100644 index 718fadb48126..000000000000 --- a/core/state/accounts/accounts.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2021 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package account - -import ( - "math/big" - - "github.com/ethereum/go-ethereum/common" -) - -// Account is the Ethereum consensus representation of accounts. -// These objects are stored in the main account trie. -type Account struct { - Nonce uint64 - Balance *big.Int - Root common.Hash // merkle root of the storage trie - CodeHash []byte -} diff --git a/core/state/database.go b/core/state/database.go index 45889bd4501d..85bbfd2a84f3 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -23,7 +23,7 @@ import ( "github.com/VictoriaMetrics/fastcache" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" - accounts "github.com/ethereum/go-ethereum/core/state/accounts" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/trie" lru "github.com/hashicorp/golang-lru" @@ -72,7 +72,7 @@ type Trie interface { TryGet(key []byte) ([]byte, error) // TryUpdateAccount abstract an account write in the trie. - TryUpdateAccount(key []byte, acc *accounts.Account) error + TryUpdateAccount(key []byte, acc *types.Account) error // TryUpdate associates key with value in the trie. If value has length zero, any // existing value is deleted from the trie. The value bytes must not be modified diff --git a/core/state/dump.go b/core/state/dump.go index e4f451d63389..0fa28522ca9f 100644 --- a/core/state/dump.go +++ b/core/state/dump.go @@ -23,7 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - acts "github.com/ethereum/go-ethereum/core/state/accounts" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" @@ -141,7 +141,7 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey [] it := trie.NewIterator(s.trie.NodeIterator(conf.Start)) for it.Next() { - var data acts.Account + var data types.Account if err := rlp.DecodeBytes(it.Value, &data); err != nil { panic(err) } diff --git a/core/state/iterator.go b/core/state/iterator.go index 3d93f2a58e2a..cfc8ba968613 100644 --- a/core/state/iterator.go +++ b/core/state/iterator.go @@ -21,7 +21,7 @@ import ( "fmt" "github.com/ethereum/go-ethereum/common" - accounts "github.com/ethereum/go-ethereum/core/state/accounts" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" ) @@ -105,7 +105,7 @@ func (it *NodeIterator) step() error { return nil } // Otherwise we've reached an account node, initiate data iteration - var account accounts.Account + var account types.Account if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob()), &account); err != nil { return err } diff --git a/core/state/pruner/pruner.go b/core/state/pruner/pruner.go index 17def67a8ebb..05261d3cf80c 100644 --- a/core/state/pruner/pruner.go +++ b/core/state/pruner/pruner.go @@ -29,7 +29,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" - accounts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/core/state/snapshot" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -426,7 +425,7 @@ func extractGenesis(db ethdb.Database, stateBloom *stateBloom) error { // If it's a leaf node, yes we are touching an account, // dig into the storage trie further. if accIter.Leaf() { - var acc accounts.Account + var acc types.Account if err := rlp.DecodeBytes(accIter.LeafBlob(), &acc); err != nil { return err } diff --git a/core/state/state_object.go b/core/state/state_object.go index dc5e1a81db81..c09bd49d6d5f 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -24,7 +24,7 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - accounts "github.com/ethereum/go-ethereum/core/state/accounts" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/rlp" @@ -66,7 +66,7 @@ func (s Storage) Copy() Storage { type stateObject struct { address common.Address addrHash common.Hash // hash of ethereum address of the account - data accounts.Account + data types.Account db *StateDB // DB error. @@ -99,7 +99,7 @@ func (s *stateObject) empty() bool { } // newObject creates a state object. -func newObject(db *StateDB, address common.Address, data accounts.Account) *stateObject { +func newObject(db *StateDB, address common.Address, data types.Account) *stateObject { if data.Balance == nil { data.Balance = new(big.Int) } diff --git a/core/state/statedb.go b/core/state/statedb.go index f8d10c2dc816..4106b6c1dbbe 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -26,7 +26,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" - accounts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/core/state/snapshot" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -509,7 +508,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { } // If no live objects are available, attempt to use snapshots var ( - data *accounts.Account + data *types.Account err error ) if s.snap != nil { @@ -521,7 +520,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { if acc == nil { return nil } - data = &accounts.Account{ + data = &types.Account{ Nonce: acc.Nonce, Balance: acc.Balance, CodeHash: acc.CodeHash, @@ -548,7 +547,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { if len(enc) == 0 { return nil } - data = new(accounts.Account) + data = new(types.Account) if err := rlp.DecodeBytes(enc, data); err != nil { log.Error("Failed to decode state object", "addr", addr, "err", err) return nil @@ -585,7 +584,7 @@ func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) s.snapDestructs[prev.addrHash] = struct{}{} } } - newobj = newObject(s, addr, accounts.Account{}) + newobj = newObject(s, addr, types.Account{}) if prev == nil { s.journal.append(createObjectChange{account: &addr}) } else { @@ -939,7 +938,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) { } // The onleaf func is called _serially_, so we can reuse the same account // for unmarshalling every time. - var account accounts.Account + var account types.Account root, accountCommitted, err := s.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error { if err := rlp.DecodeBytes(leaf, &account); err != nil { return nil diff --git a/core/state/sync.go b/core/state/sync.go index 43f78849c71b..8678ead52475 100644 --- a/core/state/sync.go +++ b/core/state/sync.go @@ -20,7 +20,7 @@ import ( "bytes" "github.com/ethereum/go-ethereum/common" - accounts "github.com/ethereum/go-ethereum/core/state/accounts" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" @@ -44,7 +44,7 @@ func NewStateSync(root common.Hash, database ethdb.KeyValueReader, bloom *trie.S return err } } - var obj accounts.Account + var obj types.Account if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil { return err } diff --git a/core/state/sync_test.go b/core/state/sync_test.go index 3dfdf3c68ac5..d3d4aa079861 100644 --- a/core/state/sync_test.go +++ b/core/state/sync_test.go @@ -23,7 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" - accs "github.com/ethereum/go-ethereum/core/state/accounts" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb/memorydb" @@ -204,7 +204,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) { } results[len(hashQueue)+i] = trie.SyncResult{Hash: crypto.Keccak256Hash(data), Data: data} } else { - var acc accs.Account + var acc types.Account if err := rlp.DecodeBytes(srcTrie.Get(path[0]), &acc); err != nil { t.Fatalf("failed to decode account on path %x: %v", path, err) } diff --git a/eth/protocols/snap/handler.go b/eth/protocols/snap/handler.go index 35ab5f8147fe..ef94301dd73c 100644 --- a/eth/protocols/snap/handler.go +++ b/eth/protocols/snap/handler.go @@ -23,7 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" - accounts "github.com/ethereum/go-ethereum/core/state/accounts" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/light" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" @@ -319,7 +319,7 @@ func handleMessage(backend Backend, peer *Peer) error { if err != nil { return p2p.Send(peer.rw, StorageRangesMsg, &StorageRangesPacket{ID: req.ID}) } - var acc accounts.Account + var acc types.Account if err := rlp.DecodeBytes(accTrie.Get(account[:]), &acc); err != nil { return p2p.Send(peer.rw, StorageRangesMsg, &StorageRangesPacket{ID: req.ID}) } diff --git a/eth/protocols/snap/sync.go b/eth/protocols/snap/sync.go index 33110ba05835..f408829effbb 100644 --- a/eth/protocols/snap/sync.go +++ b/eth/protocols/snap/sync.go @@ -31,8 +31,8 @@ import ( "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" - acts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/core/state/snapshot" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" @@ -126,8 +126,8 @@ type accountRequest struct { type accountResponse struct { task *accountTask // Task which this request is filling - hashes []common.Hash // Account hashes in the returned range - accounts []*acts.Account // Expanded accounts in the returned range + hashes []common.Hash // Account hashes in the returned range + accounts []*types.Account // Expanded accounts in the returned range cont bool // Whether the account range has a continuation } @@ -2275,9 +2275,9 @@ func (s *Syncer) OnAccounts(peer SyncPeer, id uint64, hashes []common.Hash, acco s.scheduleRevertAccountRequest(req) return err } - accs := make([]*acts.Account, len(accounts)) + accs := make([]*types.Account, len(accounts)) for i, account := range accounts { - acc := new(acts.Account) + acc := new(types.Account) if err := rlp.DecodeBytes(account, acc); err != nil { panic(err) // We created these blobs, we must be able to decode them } @@ -2741,7 +2741,7 @@ func (s *Syncer) onHealByteCodes(peer SyncPeer, id uint64, bytecodes [][]byte) e // Note it's not concurrent safe, please handle the concurrent issue outside. func (s *Syncer) onHealState(paths [][]byte, value []byte) error { if len(paths) == 1 { - var account acts.Account + var account types.Account if err := rlp.DecodeBytes(value, &account); err != nil { return nil } diff --git a/eth/protocols/snap/sync_test.go b/eth/protocols/snap/sync_test.go index da720a8f3c28..b7b708c41c2e 100644 --- a/eth/protocols/snap/sync_test.go +++ b/eth/protocols/snap/sync_test.go @@ -29,7 +29,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" - acts "github.com/ethereum/go-ethereum/core/state/accounts" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/light" @@ -1349,7 +1349,7 @@ func makeAccountTrieNoStorage(n int) (*trie.Trie, entrySlice) { accTrie, _ := trie.New(common.Hash{}, db) var entries entrySlice for i := uint64(1); i <= uint64(n); i++ { - value, _ := rlp.EncodeToBytes(acts.Account{ + value, _ := rlp.EncodeToBytes(types.Account{ Nonce: i, Balance: big.NewInt(int64(i)), Root: emptyRoot, @@ -1394,7 +1394,7 @@ func makeBoundaryAccountTrie(n int) (*trie.Trie, entrySlice) { } // Fill boundary accounts for i := 0; i < len(boundaries); i++ { - value, _ := rlp.EncodeToBytes(acts.Account{ + value, _ := rlp.EncodeToBytes(types.Account{ Nonce: uint64(0), Balance: big.NewInt(int64(i)), Root: emptyRoot, @@ -1406,7 +1406,7 @@ func makeBoundaryAccountTrie(n int) (*trie.Trie, entrySlice) { } // Fill other accounts if required for i := uint64(1); i <= uint64(n); i++ { - value, _ := rlp.EncodeToBytes(acts.Account{ + value, _ := rlp.EncodeToBytes(types.Account{ Nonce: i, Balance: big.NewInt(int64(i)), Root: emptyRoot, @@ -1442,7 +1442,7 @@ func makeAccountTrieWithStorageWithUniqueStorage(accounts, slots int, code bool) stTrie, stEntries := makeStorageTrieWithSeed(uint64(slots), i, db) stRoot := stTrie.Hash() stTrie.Commit(nil) - value, _ := rlp.EncodeToBytes(acts.Account{ + value, _ := rlp.EncodeToBytes(types.Account{ Nonce: i, Balance: big.NewInt(int64(i)), Root: stRoot, @@ -1489,7 +1489,7 @@ func makeAccountTrieWithStorage(accounts, slots int, code, boundary bool) (*trie if code { codehash = getCodeHash(i) } - value, _ := rlp.EncodeToBytes(acts.Account{ + value, _ := rlp.EncodeToBytes(types.Account{ Nonce: i, Balance: big.NewInt(int64(i)), Root: stRoot, diff --git a/les/server_handler.go b/les/server_handler.go index 9c04c56f2be8..3965b0739aaf 100644 --- a/les/server_handler.go +++ b/les/server_handler.go @@ -27,7 +27,7 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/forkid" "github.com/ethereum/go-ethereum/core/rawdb" - accounts "github.com/ethereum/go-ethereum/core/state/accounts" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/les/flowcontrol" "github.com/ethereum/go-ethereum/light" @@ -358,18 +358,18 @@ func (h *serverHandler) AddTxsSync() bool { } // getAccount retrieves an account from the state based on root. -func getAccount(triedb *trie.Database, root, hash common.Hash) (accounts.Account, error) { +func getAccount(triedb *trie.Database, root, hash common.Hash) (types.Account, error) { trie, err := trie.New(root, triedb) if err != nil { - return accounts.Account{}, err + return types.Account{}, err } blob, err := trie.TryGet(hash[:]) if err != nil { - return accounts.Account{}, err + return types.Account{}, err } - var acc accounts.Account + var acc types.Account if err = rlp.DecodeBytes(blob, &acc); err != nil { - return accounts.Account{}, err + return types.Account{}, err } return acc, nil } diff --git a/light/trie.go b/light/trie.go index 2c657eeb3f78..a3d03a5f5ffb 100644 --- a/light/trie.go +++ b/light/trie.go @@ -24,7 +24,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" - accounts "github.com/ethereum/go-ethereum/core/state/accounts" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" @@ -113,7 +112,7 @@ func (t *odrTrie) TryGet(key []byte) ([]byte, error) { return res, err } -func (t *odrTrie) TryUpdateAccount(key []byte, acc *accounts.Account) error { +func (t *odrTrie) TryUpdateAccount(key []byte, acc *types.Account) error { key = crypto.Keccak256(key) value, err := rlp.EncodeToBytes(acc) if err != nil { diff --git a/trie/secure_trie.go b/trie/secure_trie.go index fa3cb869bc17..8685d2a28f71 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -20,7 +20,7 @@ import ( "fmt" "github.com/ethereum/go-ethereum/common" - accounts "github.com/ethereum/go-ethereum/core/state/accounts" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" ) @@ -89,7 +89,7 @@ func (t *SecureTrie) TryGetNode(path []byte) ([]byte, int, error) { // TryUpdate account will abstract the write of an account to the // secure trie. -func (t *SecureTrie) TryUpdateAccount(key []byte, acc *accounts.Account) error { +func (t *SecureTrie) TryUpdateAccount(key []byte, acc *types.Account) error { hk := t.hashKey(key) data, err := rlp.EncodeToBytes(acc) if err != nil { diff --git a/trie/trie.go b/trie/trie.go index 823a37e10e31..9a53a7ab2295 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -24,7 +24,7 @@ import ( "sync" "github.com/ethereum/go-ethereum/common" - accounts "github.com/ethereum/go-ethereum/core/state/accounts" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" @@ -248,7 +248,7 @@ func (t *Trie) Update(key, value []byte) { } } -func (t *Trie) TryUpdateAccount(addr []byte, account accounts.Account) error { +func (t *Trie) TryUpdateAccount(addr []byte, account types.Account) error { data, err := rlp.EncodeToBytes(account) if err != nil { return fmt.Errorf("can't encode object at %x: %w", addr[:], err) diff --git a/trie/trie_test.go b/trie/trie_test.go index 7d826726b87c..23442c30931e 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -32,7 +32,7 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/ethereum/go-ethereum/common" - accs "github.com/ethereum/go-ethereum/core/state/accounts" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb/leveldb" @@ -562,7 +562,7 @@ func BenchmarkCommitAfterHash(b *testing.B) { b.Run("no-onleaf", func(b *testing.B) { benchmarkCommitAfterHash(b, nil) }) - var a accs.Account + var a types.Account onleaf := func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash) error { rlp.DecodeBytes(leaf, &a) return nil @@ -658,7 +658,7 @@ func makeAccounts(size int) (addresses [][20]byte, accounts [][]byte) { balanceBytes := make([]byte, numBytes) random.Read(balanceBytes) balance := new(big.Int).SetBytes(balanceBytes) - data, _ := rlp.EncodeToBytes(&accs.Account{Nonce: nonce, Balance: balance, Root: root, CodeHash: code}) + data, _ := rlp.EncodeToBytes(&types.Account{Nonce: nonce, Balance: balance, Root: root, CodeHash: code}) accounts[i] = data } return addresses, accounts From 80f1ddf18247dc435bda1d8e037c2f9aed00ea48 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 21 Sep 2021 22:20:06 +0200 Subject: [PATCH 06/10] core/types: rename Account -> StateAccount --- cmd/geth/snapshot.go | 4 ++-- core/state/database.go | 2 +- core/state/dump.go | 2 +- core/state/iterator.go | 2 +- core/state/pruner/pruner.go | 2 +- core/state/state_object.go | 4 ++-- core/state/statedb.go | 10 +++++----- core/state/sync.go | 2 +- core/state/sync_test.go | 2 +- eth/protocols/snap/handler.go | 2 +- eth/protocols/snap/sync.go | 10 +++++----- eth/protocols/snap/sync_test.go | 10 +++++----- les/server_handler.go | 10 +++++----- light/trie.go | 2 +- trie/secure_trie.go | 2 +- trie/trie.go | 2 +- trie/trie_test.go | 4 ++-- 17 files changed, 36 insertions(+), 36 deletions(-) diff --git a/cmd/geth/snapshot.go b/cmd/geth/snapshot.go index 85b5b66f9d07..d3903e0af4b0 100644 --- a/cmd/geth/snapshot.go +++ b/cmd/geth/snapshot.go @@ -288,7 +288,7 @@ func traverseState(ctx *cli.Context) error { accIter := trie.NewIterator(t.NodeIterator(nil)) for accIter.Next() { accounts += 1 - var acc types.Account + var acc types.StateAccount if err := rlp.DecodeBytes(accIter.Value, &acc); err != nil { log.Error("Invalid account encountered during traversal", "err", err) return err @@ -394,7 +394,7 @@ func traverseRawState(ctx *cli.Context) error { // dig into the storage trie further. if accIter.Leaf() { accounts += 1 - var acc types.Account + var acc types.StateAccount if err := rlp.DecodeBytes(accIter.LeafBlob(), &acc); err != nil { log.Error("Invalid account encountered during traversal", "err", err) return errors.New("invalid account") diff --git a/core/state/database.go b/core/state/database.go index 85bbfd2a84f3..3fcc07dbd1b2 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -72,7 +72,7 @@ type Trie interface { TryGet(key []byte) ([]byte, error) // TryUpdateAccount abstract an account write in the trie. - TryUpdateAccount(key []byte, acc *types.Account) error + TryUpdateAccount(key []byte, acc *types.StateAccount) error // TryUpdate associates key with value in the trie. If value has length zero, any // existing value is deleted from the trie. The value bytes must not be modified diff --git a/core/state/dump.go b/core/state/dump.go index 0fa28522ca9f..bfcc03543516 100644 --- a/core/state/dump.go +++ b/core/state/dump.go @@ -141,7 +141,7 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey [] it := trie.NewIterator(s.trie.NodeIterator(conf.Start)) for it.Next() { - var data types.Account + var data types.StateAccount if err := rlp.DecodeBytes(it.Value, &data); err != nil { panic(err) } diff --git a/core/state/iterator.go b/core/state/iterator.go index cfc8ba968613..611df52431eb 100644 --- a/core/state/iterator.go +++ b/core/state/iterator.go @@ -105,7 +105,7 @@ func (it *NodeIterator) step() error { return nil } // Otherwise we've reached an account node, initiate data iteration - var account types.Account + var account types.StateAccount if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob()), &account); err != nil { return err } diff --git a/core/state/pruner/pruner.go b/core/state/pruner/pruner.go index 05261d3cf80c..37772ca35c55 100644 --- a/core/state/pruner/pruner.go +++ b/core/state/pruner/pruner.go @@ -425,7 +425,7 @@ func extractGenesis(db ethdb.Database, stateBloom *stateBloom) error { // If it's a leaf node, yes we are touching an account, // dig into the storage trie further. if accIter.Leaf() { - var acc types.Account + var acc types.StateAccount if err := rlp.DecodeBytes(accIter.LeafBlob(), &acc); err != nil { return err } diff --git a/core/state/state_object.go b/core/state/state_object.go index c09bd49d6d5f..6e7d205675e4 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -66,7 +66,7 @@ func (s Storage) Copy() Storage { type stateObject struct { address common.Address addrHash common.Hash // hash of ethereum address of the account - data types.Account + data types.StateAccount db *StateDB // DB error. @@ -99,7 +99,7 @@ func (s *stateObject) empty() bool { } // newObject creates a state object. -func newObject(db *StateDB, address common.Address, data types.Account) *stateObject { +func newObject(db *StateDB, address common.Address, data types.StateAccount) *stateObject { if data.Balance == nil { data.Balance = new(big.Int) } diff --git a/core/state/statedb.go b/core/state/statedb.go index 4106b6c1dbbe..c75fe5ce46ba 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -508,7 +508,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { } // If no live objects are available, attempt to use snapshots var ( - data *types.Account + data *types.StateAccount err error ) if s.snap != nil { @@ -520,7 +520,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { if acc == nil { return nil } - data = &types.Account{ + data = &types.StateAccount{ Nonce: acc.Nonce, Balance: acc.Balance, CodeHash: acc.CodeHash, @@ -547,7 +547,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject { if len(enc) == 0 { return nil } - data = new(types.Account) + data = new(types.StateAccount) if err := rlp.DecodeBytes(enc, data); err != nil { log.Error("Failed to decode state object", "addr", addr, "err", err) return nil @@ -584,7 +584,7 @@ func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) s.snapDestructs[prev.addrHash] = struct{}{} } } - newobj = newObject(s, addr, types.Account{}) + newobj = newObject(s, addr, types.StateAccount{}) if prev == nil { s.journal.append(createObjectChange{account: &addr}) } else { @@ -938,7 +938,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) { } // The onleaf func is called _serially_, so we can reuse the same account // for unmarshalling every time. - var account types.Account + var account types.StateAccount root, accountCommitted, err := s.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error { if err := rlp.DecodeBytes(leaf, &account); err != nil { return nil diff --git a/core/state/sync.go b/core/state/sync.go index 8678ead52475..734961d9c512 100644 --- a/core/state/sync.go +++ b/core/state/sync.go @@ -44,7 +44,7 @@ func NewStateSync(root common.Hash, database ethdb.KeyValueReader, bloom *trie.S return err } } - var obj types.Account + var obj types.StateAccount if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil { return err } diff --git a/core/state/sync_test.go b/core/state/sync_test.go index d3d4aa079861..beb8fcfd9c46 100644 --- a/core/state/sync_test.go +++ b/core/state/sync_test.go @@ -204,7 +204,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) { } results[len(hashQueue)+i] = trie.SyncResult{Hash: crypto.Keccak256Hash(data), Data: data} } else { - var acc types.Account + var acc types.StateAccount if err := rlp.DecodeBytes(srcTrie.Get(path[0]), &acc); err != nil { t.Fatalf("failed to decode account on path %x: %v", path, err) } diff --git a/eth/protocols/snap/handler.go b/eth/protocols/snap/handler.go index ef94301dd73c..6f345716b067 100644 --- a/eth/protocols/snap/handler.go +++ b/eth/protocols/snap/handler.go @@ -319,7 +319,7 @@ func handleMessage(backend Backend, peer *Peer) error { if err != nil { return p2p.Send(peer.rw, StorageRangesMsg, &StorageRangesPacket{ID: req.ID}) } - var acc types.Account + var acc types.StateAccount if err := rlp.DecodeBytes(accTrie.Get(account[:]), &acc); err != nil { return p2p.Send(peer.rw, StorageRangesMsg, &StorageRangesPacket{ID: req.ID}) } diff --git a/eth/protocols/snap/sync.go b/eth/protocols/snap/sync.go index f408829effbb..9ef9d7571107 100644 --- a/eth/protocols/snap/sync.go +++ b/eth/protocols/snap/sync.go @@ -126,8 +126,8 @@ type accountRequest struct { type accountResponse struct { task *accountTask // Task which this request is filling - hashes []common.Hash // Account hashes in the returned range - accounts []*types.Account // Expanded accounts in the returned range + hashes []common.Hash // Account hashes in the returned range + accounts []*types.StateAccount // Expanded accounts in the returned range cont bool // Whether the account range has a continuation } @@ -2275,9 +2275,9 @@ func (s *Syncer) OnAccounts(peer SyncPeer, id uint64, hashes []common.Hash, acco s.scheduleRevertAccountRequest(req) return err } - accs := make([]*types.Account, len(accounts)) + accs := make([]*types.StateAccount, len(accounts)) for i, account := range accounts { - acc := new(types.Account) + acc := new(types.StateAccount) if err := rlp.DecodeBytes(account, acc); err != nil { panic(err) // We created these blobs, we must be able to decode them } @@ -2741,7 +2741,7 @@ func (s *Syncer) onHealByteCodes(peer SyncPeer, id uint64, bytecodes [][]byte) e // Note it's not concurrent safe, please handle the concurrent issue outside. func (s *Syncer) onHealState(paths [][]byte, value []byte) error { if len(paths) == 1 { - var account types.Account + var account types.StateAccount if err := rlp.DecodeBytes(value, &account); err != nil { return nil } diff --git a/eth/protocols/snap/sync_test.go b/eth/protocols/snap/sync_test.go index b7b708c41c2e..47ab1f026dd2 100644 --- a/eth/protocols/snap/sync_test.go +++ b/eth/protocols/snap/sync_test.go @@ -1349,7 +1349,7 @@ func makeAccountTrieNoStorage(n int) (*trie.Trie, entrySlice) { accTrie, _ := trie.New(common.Hash{}, db) var entries entrySlice for i := uint64(1); i <= uint64(n); i++ { - value, _ := rlp.EncodeToBytes(types.Account{ + value, _ := rlp.EncodeToBytes(types.StateAccount{ Nonce: i, Balance: big.NewInt(int64(i)), Root: emptyRoot, @@ -1394,7 +1394,7 @@ func makeBoundaryAccountTrie(n int) (*trie.Trie, entrySlice) { } // Fill boundary accounts for i := 0; i < len(boundaries); i++ { - value, _ := rlp.EncodeToBytes(types.Account{ + value, _ := rlp.EncodeToBytes(types.StateAccount{ Nonce: uint64(0), Balance: big.NewInt(int64(i)), Root: emptyRoot, @@ -1406,7 +1406,7 @@ func makeBoundaryAccountTrie(n int) (*trie.Trie, entrySlice) { } // Fill other accounts if required for i := uint64(1); i <= uint64(n); i++ { - value, _ := rlp.EncodeToBytes(types.Account{ + value, _ := rlp.EncodeToBytes(types.StateAccount{ Nonce: i, Balance: big.NewInt(int64(i)), Root: emptyRoot, @@ -1442,7 +1442,7 @@ func makeAccountTrieWithStorageWithUniqueStorage(accounts, slots int, code bool) stTrie, stEntries := makeStorageTrieWithSeed(uint64(slots), i, db) stRoot := stTrie.Hash() stTrie.Commit(nil) - value, _ := rlp.EncodeToBytes(types.Account{ + value, _ := rlp.EncodeToBytes(types.StateAccount{ Nonce: i, Balance: big.NewInt(int64(i)), Root: stRoot, @@ -1489,7 +1489,7 @@ func makeAccountTrieWithStorage(accounts, slots int, code, boundary bool) (*trie if code { codehash = getCodeHash(i) } - value, _ := rlp.EncodeToBytes(types.Account{ + value, _ := rlp.EncodeToBytes(types.StateAccount{ Nonce: i, Balance: big.NewInt(int64(i)), Root: stRoot, diff --git a/les/server_handler.go b/les/server_handler.go index 3965b0739aaf..f36a87a51301 100644 --- a/les/server_handler.go +++ b/les/server_handler.go @@ -358,18 +358,18 @@ func (h *serverHandler) AddTxsSync() bool { } // getAccount retrieves an account from the state based on root. -func getAccount(triedb *trie.Database, root, hash common.Hash) (types.Account, error) { +func getAccount(triedb *trie.Database, root, hash common.Hash) (types.StateAccount, error) { trie, err := trie.New(root, triedb) if err != nil { - return types.Account{}, err + return types.StateAccount{}, err } blob, err := trie.TryGet(hash[:]) if err != nil { - return types.Account{}, err + return types.StateAccount{}, err } - var acc types.Account + var acc types.StateAccount if err = rlp.DecodeBytes(blob, &acc); err != nil { - return types.Account{}, err + return types.StateAccount{}, err } return acc, nil } diff --git a/light/trie.go b/light/trie.go index a3d03a5f5ffb..4ab6f4ace075 100644 --- a/light/trie.go +++ b/light/trie.go @@ -112,7 +112,7 @@ func (t *odrTrie) TryGet(key []byte) ([]byte, error) { return res, err } -func (t *odrTrie) TryUpdateAccount(key []byte, acc *types.Account) error { +func (t *odrTrie) TryUpdateAccount(key []byte, acc *types.StateAccount) error { key = crypto.Keccak256(key) value, err := rlp.EncodeToBytes(acc) if err != nil { diff --git a/trie/secure_trie.go b/trie/secure_trie.go index 8685d2a28f71..18be12d34a48 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -89,7 +89,7 @@ func (t *SecureTrie) TryGetNode(path []byte) ([]byte, int, error) { // TryUpdate account will abstract the write of an account to the // secure trie. -func (t *SecureTrie) TryUpdateAccount(key []byte, acc *types.Account) error { +func (t *SecureTrie) TryUpdateAccount(key []byte, acc *types.StateAccount) error { hk := t.hashKey(key) data, err := rlp.EncodeToBytes(acc) if err != nil { diff --git a/trie/trie.go b/trie/trie.go index 9a53a7ab2295..bad0c9ac5fce 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -248,7 +248,7 @@ func (t *Trie) Update(key, value []byte) { } } -func (t *Trie) TryUpdateAccount(addr []byte, account types.Account) error { +func (t *Trie) TryUpdateAccount(addr []byte, account types.StateAccount) error { data, err := rlp.EncodeToBytes(account) if err != nil { return fmt.Errorf("can't encode object at %x: %w", addr[:], err) diff --git a/trie/trie_test.go b/trie/trie_test.go index 23442c30931e..be0df8a54426 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -562,7 +562,7 @@ func BenchmarkCommitAfterHash(b *testing.B) { b.Run("no-onleaf", func(b *testing.B) { benchmarkCommitAfterHash(b, nil) }) - var a types.Account + var a types.StateAccount onleaf := func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash) error { rlp.DecodeBytes(leaf, &a) return nil @@ -658,7 +658,7 @@ func makeAccounts(size int) (addresses [][20]byte, accounts [][]byte) { balanceBytes := make([]byte, numBytes) random.Read(balanceBytes) balance := new(big.Int).SetBytes(balanceBytes) - data, _ := rlp.EncodeToBytes(&types.Account{Nonce: nonce, Balance: balance, Root: root, CodeHash: code}) + data, _ := rlp.EncodeToBytes(&types.StateAccount{Nonce: nonce, Balance: balance, Root: root, CodeHash: code}) accounts[i] = data } return addresses, accounts From a7bacbe238960792b28b2b1ae4c397415700e568 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 21 Sep 2021 22:21:08 +0200 Subject: [PATCH 07/10] core/state: restore EncodeRLP for stateObject --- core/state/state_object.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/state/state_object.go b/core/state/state_object.go index 6e7d205675e4..73e9cb78ed3b 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -121,8 +121,8 @@ func newObject(db *StateDB, address common.Address, data types.StateAccount) *st } // EncodeRLP implements rlp.Encoder. -func (s *stateObject) EncodeRLP(io.Writer) error { - panic("deprecated code, should not be called") +func (s *stateObject) EncodeRLP(w io.Writer) error { + return rlp.Encode(w, &s.data) } // setError remembers the first non-nil error it is called with. From e97755c55f3c606690f543a42c4cd0c08a15a505 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 22 Sep 2021 09:32:24 +0200 Subject: [PATCH 08/10] core/types: add the missing file --- core/types/state_account.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 core/types/state_account.go diff --git a/core/types/state_account.go b/core/types/state_account.go new file mode 100644 index 000000000000..68804bf311f4 --- /dev/null +++ b/core/types/state_account.go @@ -0,0 +1,32 @@ +// Copyright 2021 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package types + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/common" +) + +// StateAccount is the Ethereum consensus representation of accounts. +// These objects are stored in the main account trie. +type StateAccount struct { + Nonce uint64 + Balance *big.Int + Root common.Hash // merkle root of the storage trie + CodeHash []byte +} From cd90b1b3c2f0cb0b7baeae9fdc9ea273a2032318 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Wed, 22 Sep 2021 14:40:52 +0200 Subject: [PATCH 09/10] more review feedback --- core/state/database.go | 2 +- core/state/statedb.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/core/state/database.go b/core/state/database.go index 3fcc07dbd1b2..bbcd2358e5b8 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -72,7 +72,7 @@ type Trie interface { TryGet(key []byte) ([]byte, error) // TryUpdateAccount abstract an account write in the trie. - TryUpdateAccount(key []byte, acc *types.StateAccount) error + TryUpdateAccount(key []byte, account *types.StateAccount) error // TryUpdate associates key with value in the trie. If value has length zero, any // existing value is deleted from the trie. The value bytes must not be modified diff --git a/core/state/statedb.go b/core/state/statedb.go index c75fe5ce46ba..e3541339eaa5 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -461,7 +461,6 @@ func (s *StateDB) updateStateObject(obj *stateObject) { // Encode the account and update the account trie addr := obj.Address() if err := s.trie.TryUpdateAccount(addr[:], &obj.data); err != nil { - //if err := s.trie.TryUpdateAccount(addr[:], obj.data); err != nil { s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err)) } From adf1a0a0f7f13f320f38f16ff63cc602aa33ea87 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Wed, 22 Sep 2021 14:51:51 +0200 Subject: [PATCH 10/10] more review feedback --- trie/trie.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/trie/trie.go b/trie/trie.go index bad0c9ac5fce..915e6f87d475 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -248,12 +248,12 @@ func (t *Trie) Update(key, value []byte) { } } -func (t *Trie) TryUpdateAccount(addr []byte, account types.StateAccount) error { - data, err := rlp.EncodeToBytes(account) +func (t *Trie) TryUpdateAccount(key []byte, acc *types.StateAccount) error { + data, err := rlp.EncodeToBytes(acc) if err != nil { - return fmt.Errorf("can't encode object at %x: %w", addr[:], err) + return fmt.Errorf("can't encode object at %x: %w", key[:], err) } - return t.TryUpdate(addr, data) + return t.TryUpdate(key, data) } // TryUpdate associates key with value in the trie. Subsequent calls to