diff --git a/core/blockchain.go b/core/blockchain.go index 40828f15f5..366782800d 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2368,10 +2368,15 @@ func (bc *BlockChain) maintainTxIndex(ancients uint64) { } pruneTo := last - ancientLimit + storedSections := rawdb.ReadStoredBloomSections(bc.db) + if storedSections*params.BloomBitsBlocks-1 < pruneTo { + log.Warn("Attempt to prune the ancient blocks that bloom filter haven't finished yet, postpone to next round", "storedSections", storedSections, "pruneTo", pruneTo) + return + } // Double ensure we don't prune the blocks having dangling transaction indices if txIndexTail != nil && pruneTo > *txIndexTail { - log.Warn("Attempt to prune the ancient blocks that still have tx indices, postpone to next round") + log.Warn("Attempt to prune the ancient blocks that still have tx indices, postpone to next round", "txIndexTail", *txIndexTail, "pruneTo", pruneTo) return } diff --git a/core/rawdb/accessors_indexes.go b/core/rawdb/accessors_indexes.go index 25a44354bf..369996834b 100644 --- a/core/rawdb/accessors_indexes.go +++ b/core/rawdb/accessors_indexes.go @@ -18,6 +18,7 @@ package rawdb import ( "bytes" + "encoding/binary" "math/big" "github.com/ethereum/go-ethereum/common" @@ -141,6 +142,19 @@ func ReadReceipt(db ethdb.Reader, hash common.Hash, config *params.ChainConfig) return nil, common.Hash{}, 0, 0 } +// ReadStoredBloomSections reads the number of valid sections from the index database +func ReadStoredBloomSections(db ethdb.Database) uint64 { + var storedSections uint64 + + table := NewTable(db, string(BloomBitsIndexPrefix)) + data, _ := table.Get([]byte("count")) + if len(data) == 8 { + storedSections = binary.BigEndian.Uint64(data) + } + + return storedSections +} + // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given // section and bit index from the. func ReadBloomBits(db ethdb.KeyValueReader, bit uint, section uint64, head common.Hash) ([]byte, error) {