Skip to content

Commit

Permalink
remove blockchain.InsertChain call from downloader (#1674)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Apr 5, 2021
1 parent 70181c4 commit eb1897c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
37 changes: 17 additions & 20 deletions eth/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ func (d *Downloader) processHeaders(origin uint64, pivot uint64, blockNumber uin
}
}

func (d *Downloader) importBlockResults(logPrefix string, results []*fetchResult, execute bool) (uint64, error) {
func (d *Downloader) importBlockResults(logPrefix string, results []*fetchResult) (uint64, error) {
// Check for any early termination requests
if len(results) == 0 {
return 0, nil
Expand All @@ -1545,30 +1545,27 @@ func (d *Downloader) importBlockResults(logPrefix string, results []*fetchResult
for i, result := range results {
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
}
tx, err2 := d.stateDB.Begin(context.Background(), ethdb.RW)
if err2 != nil {
return 0, err2
}
defer tx.Rollback()

var index int
var stopped bool
var err error
if execute {
index, err = d.blockchain.InsertChain(context.Background(), blocks)
stopped, err = core.InsertBodyChain(logPrefix, context.Background(), tx, blocks, true /* newCanonical */)
if stopped {
index = 0
} else {
tx, err2 := d.stateDB.Begin(context.Background(), ethdb.RW)
if err2 != nil {
return 0, err2
}
defer tx.Rollback()
stopped, err = core.InsertBodyChain(logPrefix, context.Background(), tx, blocks, true /* newCanonical */)
if stopped {
index = 0
} else {
index = len(results)
}
if err == nil {
if err1 := tx.Commit(); err1 != nil {
return 0, err1
}
} else {
tx.Rollback()
index = len(results)
}
if err == nil {
if err1 := tx.Commit(); err1 != nil {
return 0, err1
}
} else {
tx.Rollback()
}
if err != nil {
if index < len(results) {
Expand Down
5 changes: 2 additions & 3 deletions eth/downloader/downloader_stagedsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ func (d *Downloader) SpawnBodyDownloadStage(
h := hashes[prefetchedHashes]
if block := prefetchedBlocks.Pop(h); block != nil {
fr := fetchResultFromBlock(block)
execute := false
_, err := d.importBlockResults(logPrefix, []*fetchResult{fr}, execute)
_, err := d.importBlockResults(logPrefix, []*fetchResult{fr})
if err != nil {
return false, err
}
Expand Down Expand Up @@ -160,7 +159,7 @@ func (d *Downloader) processBodiesStage(logPrefix string, to uint64) error {
if len(results) == 0 {
return nil
}
lastNumber, err := d.importBlockResults(logPrefix, results, false /* execute */)
lastNumber, err := d.importBlockResults(logPrefix, results)
if err != nil {
return err
}
Expand Down

0 comments on commit eb1897c

Please sign in to comment.