From 2f32cd2493f4a157557a717f2b88700ab23ed7cd Mon Sep 17 00:00:00 2001 From: ledgerwatch Date: Fri, 29 Nov 2024 12:59:27 +0000 Subject: [PATCH] Fix externalcl integration for erigon3 (#12906) Fixes https://github.com/erigontech/erigon/issues/12225 --- erigon-lib/kv/mdbx/kv_mdbx_temporary.go | 2 +- eth/stagedsync/stage_bodies.go | 2 +- .../sentry_multi_client.go | 3 +- .../block_downloader.go | 3 +- .../engineapi/engine_block_downloader/body.go | 2 +- .../engineapi/engine_block_downloader/core.go | 11 ++++- turbo/engineapi/engine_server.go | 1 - turbo/stages/bodydownload/body_algos.go | 40 ++----------------- turbo/stages/bodydownload/body_test.go | 2 +- 9 files changed, 20 insertions(+), 46 deletions(-) diff --git a/erigon-lib/kv/mdbx/kv_mdbx_temporary.go b/erigon-lib/kv/mdbx/kv_mdbx_temporary.go index aa4e87f5f16..879a2e95fe4 100644 --- a/erigon-lib/kv/mdbx/kv_mdbx_temporary.go +++ b/erigon-lib/kv/mdbx/kv_mdbx_temporary.go @@ -37,7 +37,7 @@ func NewTemporaryMdbx(ctx context.Context, tempdir string) (kv.RwDB, error) { return &TemporaryMdbx{}, err } - db, err := New(kv.ChainDB, log.Root()).InMem(path).Open(ctx) + db, err := New(kv.ChainDB, log.Root()).InMem(path).GrowthStep(64 * datasize.MB).Open(ctx) if err != nil { return &TemporaryMdbx{}, err } diff --git a/eth/stagedsync/stage_bodies.go b/eth/stagedsync/stage_bodies.go index 4b9364252c4..9e403ccf7ce 100644 --- a/eth/stagedsync/stage_bodies.go +++ b/eth/stagedsync/stage_bodies.go @@ -98,7 +98,7 @@ func BodiesForward(s *StageState, u Unwinder, ctx context.Context, tx kv.RwTx, c } } // This will update bd.maxProgress - if _, _, _, _, err = cfg.bd.UpdateFromDb(tx); err != nil { + if err = cfg.bd.UpdateFromDb(tx); err != nil { return err } defer cfg.bd.ClearBodyCache() diff --git a/p2p/sentry/sentry_multi_client/sentry_multi_client.go b/p2p/sentry/sentry_multi_client/sentry_multi_client.go index 21f6d5c6707..64a82af96bd 100644 --- a/p2p/sentry/sentry_multi_client/sentry_multi_client.go +++ b/p2p/sentry/sentry_multi_client/sentry_multi_client.go @@ -202,8 +202,7 @@ func NewMultiClient( if !disableBlockDownload { bd = bodydownload.NewBodyDownload(engine, blockBufferSize, int(syncCfg.BodyCacheLimit), blockReader, logger) if err := db.View(context.Background(), func(tx kv.Tx) error { - _, _, _, _, err := bd.UpdateFromDb(tx) - return err + return bd.UpdateFromDb(tx) }); err != nil { return nil, err } diff --git a/turbo/engineapi/engine_block_downloader/block_downloader.go b/turbo/engineapi/engine_block_downloader/block_downloader.go index b2449124595..7b748cff0fa 100644 --- a/turbo/engineapi/engine_block_downloader/block_downloader.go +++ b/turbo/engineapi/engine_block_downloader/block_downloader.go @@ -222,7 +222,6 @@ func (e *EngineBlockDownloader) loadDownloadedHeaders(tx kv.RwTx) (fromBlock uin func saveHeader(db kv.RwTx, header *types.Header, hash libcommon.Hash) error { blockHeight := header.Number.Uint64() // TODO(yperbasis): do we need to check if the header is already inserted (oldH)? - parentTd, err := rawdb.ReadTd(db, header.ParentHash, blockHeight-1) if err != nil || parentTd == nil { return fmt.Errorf("[saveHeader] parent's total difficulty not found with hash %x and height %d for header %x %d: %v", header.ParentHash, blockHeight-1, hash, blockHeight, err) @@ -235,7 +234,7 @@ func saveHeader(db kv.RwTx, header *types.Header, hash libcommon.Hash) error { return fmt.Errorf("[saveHeader] failed to WriteTd: %w", err) } if err = rawdb.WriteCanonicalHash(db, hash, blockHeight); err != nil { - return fmt.Errorf("[saveHeader] failed to canonical hash: %w", err) + return fmt.Errorf("[saveHeader] failed to save canonical hash: %w", err) } return nil } diff --git a/turbo/engineapi/engine_block_downloader/body.go b/turbo/engineapi/engine_block_downloader/body.go index 645d4a3651e..5d440ff680f 100644 --- a/turbo/engineapi/engine_block_downloader/body.go +++ b/turbo/engineapi/engine_block_downloader/body.go @@ -50,7 +50,7 @@ func (e *EngineBlockDownloader) downloadAndLoadBodiesSyncronously(ctx context.Co timeout := e.timeout // This will update bd.maxProgress - if _, _, _, _, err = e.bd.UpdateFromDb(tx); err != nil { + if err = e.bd.UpdateFromDb(tx); err != nil { return } defer e.bd.ClearBodyCache() diff --git a/turbo/engineapi/engine_block_downloader/core.go b/turbo/engineapi/engine_block_downloader/core.go index 1e9352f4388..adf9f61d7a3 100644 --- a/turbo/engineapi/engine_block_downloader/core.go +++ b/turbo/engineapi/engine_block_downloader/core.go @@ -23,6 +23,7 @@ import ( execution "github.com/erigontech/erigon-lib/gointerfaces/executionproto" "github.com/erigontech/erigon-lib/kv/mdbx" "github.com/erigontech/erigon-lib/kv/membatchwithdb" + "github.com/erigontech/erigon/core/rawdb" "github.com/erigontech/erigon/core/types" "github.com/erigontech/erigon/turbo/stages/headerdownload" ) @@ -79,9 +80,17 @@ func (e *EngineBlockDownloader) download(ctx context.Context, hashToDownload lib memoryMutation := membatchwithdb.NewMemoryBatchWithCustomDB(tx, tmpDb, tmpTx) defer memoryMutation.Rollback() + if block != nil { + err = rawdb.WriteCanonicalHash(memoryMutation, block.Hash(), block.NumberU64()) + if err != nil { + e.logger.Warn("[EngineBlockDownloader] Could not make leading header canonical", "err", err) + e.status.Store(headerdownload.Idle) + return + } + } startBlock, endBlock, startHash, err := e.loadDownloadedHeaders(memoryMutation) if err != nil { - e.logger.Warn("[EngineBlockDownloader] Could load headers", "err", err) + e.logger.Warn("[EngineBlockDownloader] Could not load headers", "err", err) e.status.Store(headerdownload.Idle) return } diff --git a/turbo/engineapi/engine_server.go b/turbo/engineapi/engine_server.go index d8b8348f358..ae27bc21060 100644 --- a/turbo/engineapi/engine_server.go +++ b/turbo/engineapi/engine_server.go @@ -868,7 +868,6 @@ func (e *EngineServer) HandleNewPayload( if !success { return &engine_types.PayloadStatus{Status: engine_types.SyncingStatus}, nil } - status, _, latestValidHash, err := e.chainRW.ValidateChain(ctx, headerHash, headerNumber) if err != nil { return nil, err diff --git a/turbo/stages/bodydownload/body_algos.go b/turbo/stages/bodydownload/body_algos.go index 509af2bc67b..b6580584ce5 100644 --- a/turbo/stages/bodydownload/body_algos.go +++ b/turbo/stages/bodydownload/body_algos.go @@ -19,7 +19,6 @@ package bodydownload import ( "bytes" "context" - "errors" "fmt" "math/big" @@ -33,19 +32,18 @@ import ( "github.com/erigontech/erigon/eth/stagedsync/stages" "github.com/erigontech/erigon/turbo/adapter" "github.com/erigontech/erigon/turbo/services" - "github.com/holiman/uint256" ) // UpdateFromDb reads the state of the database and refreshes the state of the body download -func (bd *BodyDownload) UpdateFromDb(db kv.Tx) (headHeight, headTime uint64, headHash libcommon.Hash, headTd256 *uint256.Int, err error) { +func (bd *BodyDownload) UpdateFromDb(db kv.Tx) (err error) { var headerProgress, bodyProgress uint64 headerProgress, err = stages.GetStageProgress(db, stages.Headers) if err != nil { - return 0, 0, libcommon.Hash{}, nil, err + return err } bodyProgress, err = stages.GetStageProgress(db, stages.Bodies) if err != nil { - return 0, 0, libcommon.Hash{}, nil, err + return err } bd.maxProgress = headerProgress + 1 // Resetting for requesting a new range of blocks @@ -58,37 +56,7 @@ func (bd *BodyDownload) UpdateFromDb(db kv.Tx) (headHeight, headTime uint64, hea clear(bd.requests) clear(bd.peerMap) bd.ClearBodyCache() - headHeight = bodyProgress - var ok bool - headHash, ok, err = bd.br.CanonicalHash(context.Background(), db, headHeight) - if err != nil { - return 0, 0, libcommon.Hash{}, nil, err - } - if !ok { - return 0, 0, libcommon.Hash{}, nil, fmt.Errorf("canonical marker not found: %d", headHeight) - } - var headTd *big.Int - headTd, err = rawdb.ReadTd(db, headHash, headHeight) - if err != nil { - return 0, 0, libcommon.Hash{}, nil, fmt.Errorf("reading total difficulty for head height %d and hash %x: %d, %w", headHeight, headHash, headTd, err) - } - if headTd == nil { - headTd = new(big.Int) - } - headTd256 = new(uint256.Int) - overflow := headTd256.SetFromBig(headTd) - if overflow { - return 0, 0, libcommon.Hash{}, nil, errors.New("headTd higher than 2^256-1") - } - headTime = 0 - headHeader, err := bd.br.Header(context.Background(), db, headHash, headHeight) - if err != nil { - return 0, 0, libcommon.Hash{}, nil, fmt.Errorf("reading header for head height %d and hash %x: %d, %w", headHeight, headHash, headTd, err) - } - if headHeader != nil { - headTime = headHeader.Time - } - return headHeight, headTime, headHash, headTd256, nil + return nil } // RequestMoreBodies - returns nil if nothing to request diff --git a/turbo/stages/bodydownload/body_test.go b/turbo/stages/bodydownload/body_test.go index 4164eb977e7..2545b0d6cd5 100644 --- a/turbo/stages/bodydownload/body_test.go +++ b/turbo/stages/bodydownload/body_test.go @@ -33,7 +33,7 @@ func TestCreateBodyDownload(t *testing.T) { require.NoError(t, err) defer tx.Rollback() bd := bodydownload.NewBodyDownload(ethash.NewFaker(), 128, 100, m.BlockReader, m.Log) - if _, _, _, _, err := bd.UpdateFromDb(tx); err != nil { + if err := bd.UpdateFromDb(tx); err != nil { t.Fatalf("update from db: %v", err) } }