Skip to content

Commit

Permalink
Fix externalcl integration for erigon3 (#12906)
Browse files Browse the repository at this point in the history
Fixes #12225
  • Loading branch information
AlexeyAkhunov authored Nov 29, 2024
1 parent 4b12ea3 commit 2f32cd2
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 46 deletions.
2 changes: 1 addition & 1 deletion erigon-lib/kv/mdbx/kv_mdbx_temporary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion eth/stagedsync/stage_bodies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions p2p/sentry/sentry_multi_client/sentry_multi_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions turbo/engineapi/engine_block_downloader/block_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion turbo/engineapi/engine_block_downloader/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 10 additions & 1 deletion turbo/engineapi/engine_block_downloader/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion turbo/engineapi/engine_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 4 additions & 36 deletions turbo/stages/bodydownload/body_algos.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package bodydownload
import (
"bytes"
"context"
"errors"
"fmt"
"math/big"

Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion turbo/stages/bodydownload/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 2f32cd2

Please sign in to comment.