core/rawdb: fix the transaction indexer (#22395)

This commit is contained in:
gary rong 2021-03-01 17:26:10 +08:00 committed by GitHub
parent d96870428f
commit 19d7a37abb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -243,14 +243,14 @@ func indexTransactions(db ethdb.Database, from uint64, to uint64, interrupt chan
} }
} }
} }
// If there exists uncommitted data, flush them. // Flush the new indexing tail and the last committed data. It can also happen
if batch.ValueSize() > 0 { // that the last batch is empty because nothing to index, but the tail has to
WriteTxIndexTail(batch, lastNum) // Also write the tail there // be flushed anyway.
WriteTxIndexTail(batch, lastNum)
if err := batch.Write(); err != nil { if err := batch.Write(); err != nil {
log.Crit("Failed writing batch to db", "error", err) log.Crit("Failed writing batch to db", "error", err)
return return
} }
}
select { select {
case <-interrupt: case <-interrupt:
log.Debug("Transaction indexing interrupted", "blocks", blocks, "txs", txs, "tail", lastNum, "elapsed", common.PrettyDuration(time.Since(start))) log.Debug("Transaction indexing interrupted", "blocks", blocks, "txs", txs, "tail", lastNum, "elapsed", common.PrettyDuration(time.Since(start)))
@ -334,14 +334,14 @@ func unindexTransactions(db ethdb.Database, from uint64, to uint64, interrupt ch
} }
} }
} }
// Commit the last batch if there exists uncommitted data // Flush the new indexing tail and the last committed data. It can also happen
if batch.ValueSize() > 0 { // that the last batch is empty because nothing to unindex, but the tail has to
// be flushed anyway.
WriteTxIndexTail(batch, nextNum) WriteTxIndexTail(batch, nextNum)
if err := batch.Write(); err != nil { if err := batch.Write(); err != nil {
log.Crit("Failed writing batch to db", "error", err) log.Crit("Failed writing batch to db", "error", err)
return return
} }
}
select { select {
case <-interrupt: case <-interrupt:
log.Debug("Transaction unindexing interrupted", "blocks", blocks, "txs", txs, "tail", to, "elapsed", common.PrettyDuration(time.Since(start))) log.Debug("Transaction unindexing interrupted", "blocks", blocks, "txs", txs, "tail", to, "elapsed", common.PrettyDuration(time.Since(start)))