From 66ea34337e6947314fc8bdb9e6d7d21394ddc797 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Thu, 3 Aug 2023 23:50:39 +0800 Subject: [PATCH 1/7] core: ensure txindex will be triggered at least once Signed-off-by: jsvisa --- core/blockchain.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index 3952c31b688f..83a798810b61 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2394,9 +2394,25 @@ func (bc *BlockChain) maintainTxIndex() { } defer sub.Unsubscribe() + // If geth is running in read-only mode(not receiving new blocks), + // we still want to trigger transaction indexing at least once. + timer := time.NewTimer(30 * time.Second) + defer timer.Stop() + for { select { + case <-timer.C: + if done == nil { + head := rawdb.ReadHeadBlock(bc.db) + if head != nil { + done = make(chan struct{}) + go bc.indexBlocks(rawdb.ReadTxIndexTail(bc.db), head.NumberU64(), done) + } + } case head := <-headCh: + if !timer.Stop() { + <-timer.C + } if done == nil { done = make(chan struct{}) go bc.indexBlocks(rawdb.ReadTxIndexTail(bc.db), head.Block.NumberU64(), done) From 685932aa72708a8a8fa68e2bed391615acc0983f Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 4 Aug 2023 14:56:23 +0800 Subject: [PATCH 2/7] core: spin a reindex task before subscribe Signed-off-by: jsvisa --- core/blockchain.go | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 83a798810b61..90ee89ef86e8 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2334,7 +2334,9 @@ func (bc *BlockChain) skipBlock(err error, it *insertIterator) bool { // indexBlocks reindexes or unindexes transactions depending on user configuration func (bc *BlockChain) indexBlocks(tail *uint64, head uint64, done chan struct{}) { - defer func() { close(done) }() + if done != nil { + defer func() { close(done) }() + } // The tail flag is not existent, it means the node is just initialized // and all blocks(may from ancient store) are not indexed yet. @@ -2383,6 +2385,13 @@ func (bc *BlockChain) indexBlocks(tail *uint64, head uint64, done chan struct{}) func (bc *BlockChain) maintainTxIndex() { defer bc.wg.Done() + // Geth should always index/uninindex the unfinished chain segments, + // in case the chain has no progress for a long time. + head := rawdb.ReadHeadBlock(bc.db) + if head != nil { + bc.indexBlocks(rawdb.ReadTxIndexTail(bc.db), head.NumberU64(), nil) + } + // Listening to chain events and manipulate the transaction indexes. var ( done chan struct{} // Non-nil if background unindexing or reindexing routine is active. @@ -2394,25 +2403,9 @@ func (bc *BlockChain) maintainTxIndex() { } defer sub.Unsubscribe() - // If geth is running in read-only mode(not receiving new blocks), - // we still want to trigger transaction indexing at least once. - timer := time.NewTimer(30 * time.Second) - defer timer.Stop() - for { select { - case <-timer.C: - if done == nil { - head := rawdb.ReadHeadBlock(bc.db) - if head != nil { - done = make(chan struct{}) - go bc.indexBlocks(rawdb.ReadTxIndexTail(bc.db), head.NumberU64(), done) - } - } case head := <-headCh: - if !timer.Stop() { - <-timer.C - } if done == nil { done = make(chan struct{}) go bc.indexBlocks(rawdb.ReadTxIndexTail(bc.db), head.Block.NumberU64(), done) From a2e1fdd271eecd70622d2225d82bb4978706642a Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 4 Aug 2023 20:28:47 +0800 Subject: [PATCH 3/7] core: cherry-pick from rj's review Signed-off-by: jsvisa --- core/blockchain.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 90ee89ef86e8..dded70b2f8f5 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2334,9 +2334,7 @@ func (bc *BlockChain) skipBlock(err error, it *insertIterator) bool { // indexBlocks reindexes or unindexes transactions depending on user configuration func (bc *BlockChain) indexBlocks(tail *uint64, head uint64, done chan struct{}) { - if done != nil { - defer func() { close(done) }() - } + defer func() { close(done) }() // The tail flag is not existent, it means the node is just initialized // and all blocks(may from ancient store) are not indexed yet. @@ -2385,13 +2383,6 @@ func (bc *BlockChain) indexBlocks(tail *uint64, head uint64, done chan struct{}) func (bc *BlockChain) maintainTxIndex() { defer bc.wg.Done() - // Geth should always index/uninindex the unfinished chain segments, - // in case the chain has no progress for a long time. - head := rawdb.ReadHeadBlock(bc.db) - if head != nil { - bc.indexBlocks(rawdb.ReadTxIndexTail(bc.db), head.NumberU64(), nil) - } - // Listening to chain events and manipulate the transaction indexes. var ( done chan struct{} // Non-nil if background unindexing or reindexing routine is active. @@ -2403,6 +2394,15 @@ func (bc *BlockChain) maintainTxIndex() { } defer sub.Unsubscribe() + // Launch the initial processing if chain is not empty. This step is + // useful in these scenarios that chain has no progress and indexer + // is never triggered. + head := rawdb.ReadHeadBlock(bc.db) + if head != nil { + done = make(chan struct{}) + bc.indexBlocks(rawdb.ReadTxIndexTail(bc.db), head.NumberU64(), done) + } + for { select { case head := <-headCh: From 4273f248755a48d29002aa6d01de94bca36ea6c1 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Mon, 7 Aug 2023 17:08:32 +0800 Subject: [PATCH 4/7] core: launch indexBlock only if tail is not null Signed-off-by: jsvisa --- core/blockchain.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index dded70b2f8f5..900b410db0b7 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2398,9 +2398,10 @@ func (bc *BlockChain) maintainTxIndex() { // useful in these scenarios that chain has no progress and indexer // is never triggered. head := rawdb.ReadHeadBlock(bc.db) - if head != nil { + tail := rawdb.ReadTxIndexTail(bc.db) + if head != nil && tail != nil { done = make(chan struct{}) - bc.indexBlocks(rawdb.ReadTxIndexTail(bc.db), head.NumberU64(), done) + bc.indexBlocks(tail, head.NumberU64(), done) } for { From 60b2862a6bffa523cc137bcb1d0fca98ebe2728e Mon Sep 17 00:00:00 2001 From: jsvisa Date: Thu, 17 Aug 2023 16:15:24 +0800 Subject: [PATCH 5/7] don't check tail Signed-off-by: jsvisa --- core/blockchain.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 900b410db0b7..7509ea2ec1a3 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2398,10 +2398,9 @@ func (bc *BlockChain) maintainTxIndex() { // useful in these scenarios that chain has no progress and indexer // is never triggered. head := rawdb.ReadHeadBlock(bc.db) - tail := rawdb.ReadTxIndexTail(bc.db) - if head != nil && tail != nil { + if head != nil { done = make(chan struct{}) - bc.indexBlocks(tail, head.NumberU64(), done) + go bc.indexBlocks(rawdb.ReadTxIndexTail(bc.db), head.NumberU64(), done) } for { From 0dda9cf84d51a37eb883b07e9651a4cef6c7d9be Mon Sep 17 00:00:00 2001 From: jsvisa Date: Thu, 17 Aug 2023 16:15:41 +0800 Subject: [PATCH 6/7] no need to indexing if head is 0 Signed-off-by: jsvisa --- core/blockchain.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index 7509ea2ec1a3..2717a3ffdced 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2336,6 +2336,12 @@ func (bc *BlockChain) skipBlock(err error, it *insertIterator) bool { func (bc *BlockChain) indexBlocks(tail *uint64, head uint64, done chan struct{}) { defer func() { close(done) }() + // If head is 0, it means the chain is just initialized and no blocks are inserted, + // so don't need to indexing anything. + if head == 0 { + return + } + // The tail flag is not existent, it means the node is just initialized // and all blocks(may from ancient store) are not indexed yet. if tail == nil { From 71374ae43eeb1ae4152f06cb80ecc0c9947ddfe6 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 21 Aug 2023 13:37:49 -0400 Subject: [PATCH 7/7] Update core/blockchain.go --- core/blockchain.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 2717a3ffdced..1f3aa6e2f164 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2403,8 +2403,7 @@ func (bc *BlockChain) maintainTxIndex() { // Launch the initial processing if chain is not empty. This step is // useful in these scenarios that chain has no progress and indexer // is never triggered. - head := rawdb.ReadHeadBlock(bc.db) - if head != nil { + if head := rawdb.ReadHeadBlock(bc.db); head != nil { done = make(chan struct{}) go bc.indexBlocks(rawdb.ReadTxIndexTail(bc.db), head.NumberU64(), done) }