From 1e9bf2a09ed3d82ac1aa69750a556f3ce127721d Mon Sep 17 00:00:00 2001 From: Aaron Chen Date: Wed, 17 Apr 2024 19:55:31 +0800 Subject: [PATCH 1/2] core/state: fix bug in statedb.Copy and remove unnecessary preallocation (#29563) This change removes an unnecessary preallocation and fixes a flaw with no-op copies of some parts of the statedb --- core/state/statedb.go | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index d3d383389c..ab152dd18d 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -696,18 +696,18 @@ func (s *StateDB) Copy() *StateDB { db: s.db, trie: s.db.CopyTrie(s.trie), originalRoot: s.originalRoot, - accounts: make(map[common.Hash][]byte), - storages: make(map[common.Hash]map[common.Hash][]byte), - accountsOrigin: make(map[common.Address][]byte), - storagesOrigin: make(map[common.Address]map[common.Hash][]byte), + accounts: copySet(s.accounts), + storages: copy2DSet(s.storages), + accountsOrigin: copySet(s.accountsOrigin), + storagesOrigin: copy2DSet(s.storagesOrigin), stateObjects: make(map[common.Address]*stateObject, len(s.journal.dirties)), stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)), stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)), - stateObjectsDestruct: make(map[common.Address]*types.StateAccount, len(s.stateObjectsDestruct)), + stateObjectsDestruct: maps.Clone(s.stateObjectsDestruct), refund: s.refund, logs: make(map[common.Hash][]*types.Log, len(s.logs)), logSize: s.logSize, - preimages: make(map[common.Hash][]byte, len(s.preimages)), + preimages: maps.Clone(s.preimages), journal: newJournal(), hasher: crypto.NewKeccakState(), @@ -750,15 +750,6 @@ func (s *StateDB) Copy() *StateDB { } state.stateObjectsDirty[addr] = struct{}{} } - // Deep copy the destruction markers. - state.stateObjectsDestruct = maps.Clone(s.stateObjectsDestruct) - - // Deep copy the state changes made in the scope of block - // along with their original values. - state.accounts = copySet(s.accounts) - state.storages = copy2DSet(s.storages) - state.accountsOrigin = copySet(state.accountsOrigin) - state.storagesOrigin = copy2DSet(state.storagesOrigin) // Deep copy the logs occurred in the scope of block for hash, logs := range s.logs { @@ -769,8 +760,7 @@ func (s *StateDB) Copy() *StateDB { } state.logs[hash] = cpy } - // Deep copy the preimages occurred in the scope of block - state.preimages = maps.Clone(s.preimages) + // Do we need to copy the access list and transient storage? // In practice: No. At the start of a transaction, these two lists are empty. // In practice, we only ever copy state _between_ transactions/blocks, never From 0da69e84c0d481e42f60cecc1562c208525117eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felf=C3=B6ldi=20Zsolt?= Date: Wed, 17 Apr 2024 17:07:28 +0200 Subject: [PATCH 2/2] beacon/blsync: proceed with empty finalized hash if proof is not expected soon (#29449) * beacon/blsync: proceed with empty finalized hash if proof is not expected soon * Update beacon/blsync/block_sync.go Co-authored-by: Felix Lange * beacon/blsync: fixed linter warning * Update beacon/blsync/block_sync.go Co-authored-by: lightclient <14004106+lightclient@users.noreply.github.com> --------- Co-authored-by: Felix Lange Co-authored-by: lightclient <14004106+lightclient@users.noreply.github.com> --- beacon/blsync/block_sync.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/beacon/blsync/block_sync.go b/beacon/blsync/block_sync.go index ef852dfe99..3ab156354d 100755 --- a/beacon/blsync/block_sync.go +++ b/beacon/blsync/block_sync.go @@ -19,6 +19,7 @@ package blsync import ( "github.com/ethereum/go-ethereum/beacon/light/request" "github.com/ethereum/go-ethereum/beacon/light/sync" + "github.com/ethereum/go-ethereum/beacon/params" "github.com/ethereum/go-ethereum/beacon/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/lru" @@ -117,15 +118,31 @@ func (s *beaconBlockSync) updateEventFeed() { if !ok { return } - finality, ok := s.headTracker.ValidatedFinality() //TODO fetch directly if subscription does not deliver - if !ok || head.Header.Epoch() != finality.Attested.Header.Epoch() { - return - } + validatedHead := head.Header.Hash() headBlock, ok := s.recentBlocks.Get(validatedHead) if !ok { return } + + var finalizedHash common.Hash + if finality, ok := s.headTracker.ValidatedFinality(); ok { + he := head.Header.Epoch() + fe := finality.Attested.Header.Epoch() + switch { + case he == fe: + finalizedHash = finality.Finalized.PayloadHeader.BlockHash() + case he < fe: + return + case he == fe+1: + parent, ok := s.recentBlocks.Get(head.Header.ParentRoot) + if !ok || parent.Slot()/params.EpochLength == fe { + return // head is at first slot of next epoch, wait for finality update + //TODO: try to fetch finality update directly if subscription does not deliver + } + } + } + headInfo := blockHeadInfo(headBlock) if headInfo == s.lastHeadInfo { return @@ -141,6 +158,6 @@ func (s *beaconBlockSync) updateEventFeed() { s.chainHeadFeed.Send(types.ChainHeadEvent{ BeaconHead: head.Header, Block: execBlock, - Finalized: finality.Finalized.PayloadHeader.BlockHash(), + Finalized: finalizedHash, }) }