From b1ff642f3adf19449c75f1b98908e48ef7f87724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Mon, 4 Oct 2021 15:01:13 +0300 Subject: [PATCH] eth: minor sync polishes --- eth/backend.go | 4 +++ eth/catalyst/api.go | 25 ++++++--------- eth/catalyst/sync.go | 74 -------------------------------------------- 3 files changed, 14 insertions(+), 89 deletions(-) delete mode 100644 eth/catalyst/sync.go diff --git a/eth/backend.go b/eth/backend.go index d56be0528f7e..735ae45027c6 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -529,6 +529,10 @@ func (s *Ethereum) SetSynced() { atomic.StoreUint32(&s.h func (s *Ethereum) ArchiveMode() bool { return s.config.NoPruning } func (s *Ethereum) BloomIndexer() *core.ChainIndexer { return s.bloomIndexer } func (s *Ethereum) Merger() *core.Merger { return s.merger } +func (s *Ethereum) SyncMode() downloader.SyncMode { + mode, _ := s.handler.chainSync.modeAndLocalHead() + return mode +} // Protocols returns all the currently configured // network protocols to start. diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go index 64a9bf308439..ff0d66c32fad 100644 --- a/eth/catalyst/api.go +++ b/eth/catalyst/api.go @@ -84,7 +84,6 @@ type ConsensusAPI struct { eth *eth.Ethereum les *les.LightEthereum engine consensus.Engine // engine is the post-merge consensus engine, only for block creation - syncer *syncer // syncer is responsible for triggering chain sync preparedBlocks map[int]*ExecutableData } @@ -114,7 +113,6 @@ func NewConsensusAPI(eth *eth.Ethereum, les *les.LightEthereum) *ConsensusAPI { eth: eth, les: les, engine: engine, - syncer: newSyncer(), preparedBlocks: make(map[int]*ExecutableData), } } @@ -217,11 +215,11 @@ func (api *ConsensusAPI) ConsensusValidated(params ConsensusValidatedParams) err func (api *ConsensusAPI) ForkchoiceUpdated(params ForkChoiceParams) error { var emptyHash = common.Hash{} - if !bytes.Equal(params.FinalizedBlockHash[:], emptyHash[:]) { - if err := api.checkTerminalTotalDifficulty(params.FinalizedBlockHash); err != nil { + if !bytes.Equal(params.HeadBlockHash[:], emptyHash[:]) { + if err := api.checkTerminalTotalDifficulty(params.HeadBlockHash); err != nil { return err } - return api.setHead(params.FinalizedBlockHash) + return api.setHead(params.HeadBlockHash) } return nil } @@ -249,22 +247,19 @@ func (api *ConsensusAPI) ExecutePayload(params ExecutableData) (GenericStringRes td := api.eth.BlockChain().GetTdByHash(parent.Hash()) ttd := api.eth.BlockChain().Config().TerminalTotalDifficulty - if !api.eth.Synced() { - if td.Cmp(ttd) > 0 { - // first pos block - api.eth.SetSynced() - } else { - // TODO (MariusVanDerWijden) if the node is not synced and we received a finalized block - // we should trigger the reverse header sync here. - return SYNCING, errors.New("node is not synced yet") - } - } else if td.Cmp(ttd) < 0 { + if td.Cmp(ttd) < 0 { return INVALID, fmt.Errorf("can not execute payload on top of block with low td got: %v threshold %v", td, ttd) } block, err := ExecutableDataToBlock(api.eth.BlockChain().Config(), parent.Header(), params) if err != nil { return INVALID, err } + if !api.eth.BlockChain().HasBlock(block.ParentHash(), block.NumberU64()-1) { + if err := api.eth.Downloader().BeaconSync(api.eth.SyncMode(), block.Header()); err != nil { + return SYNCING, err + } + return SYNCING, nil + } if err := api.eth.BlockChain().InsertBlock(block); err != nil { return INVALID, err } diff --git a/eth/catalyst/sync.go b/eth/catalyst/sync.go deleted file mode 100644 index 61fd1ef9c166..000000000000 --- a/eth/catalyst/sync.go +++ /dev/null @@ -1,74 +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 catalyst - -import ( - "sync" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/log" -) - -type syncer struct { - running bool - newBlocks map[common.Hash]*types.Block - lock sync.Mutex -} - -func newSyncer() *syncer { - return &syncer{ - newBlocks: make(map[common.Hash]*types.Block), - } -} - -// onNewBlock is the action for receiving new block event -func (s *syncer) onNewBlock(block *types.Block) { - s.lock.Lock() - defer s.lock.Unlock() - - if s.running { - return - } - s.newBlocks[block.Hash()] = block -} - -func (s *syncer) hasBlock(hash common.Hash) bool { - s.lock.Lock() - defer s.lock.Unlock() - - _, present := s.newBlocks[hash] - return present -} - -// onNewHead is the action for receiving new head event -func (s *syncer) onNewHead(head common.Hash) { - s.lock.Lock() - defer s.lock.Unlock() - - if s.running { - return - } - _, present := s.newBlocks[head] - if !present { - log.Error("Chain head is set with an unknown header") - return - } - s.running = true - - // todo call the SetHead function exposed by the downloader -}