core/txindexer : add wait group

This commit is contained in:
ucwong 2025-03-28 19:11:48 +08:00
parent 141968a48b
commit db3e02e921

View file

@ -19,6 +19,7 @@ package core
import ( import (
"errors" "errors"
"fmt" "fmt"
"sync"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
@ -54,6 +55,8 @@ type txIndexer struct {
progress chan chan TxIndexProgress progress chan chan TxIndexProgress
term chan chan struct{} term chan chan struct{}
closed chan struct{} closed chan struct{}
wg sync.WaitGroup
} }
// newTxIndexer initializes the transaction indexer. // newTxIndexer initializes the transaction indexer.
@ -66,6 +69,7 @@ func newTxIndexer(limit uint64, chain *BlockChain) *txIndexer {
term: make(chan chan struct{}), term: make(chan chan struct{}),
closed: make(chan struct{}), closed: make(chan struct{}),
} }
indexer.wg.Add(1)
go indexer.loop(chain) go indexer.loop(chain)
var msg string var msg string
@ -199,6 +203,7 @@ func (indexer *txIndexer) repair(head uint64) {
// on the received chain event. // on the received chain event.
func (indexer *txIndexer) loop(chain *BlockChain) { func (indexer *txIndexer) loop(chain *BlockChain) {
defer close(indexer.closed) defer close(indexer.closed)
defer indexer.wg.Done()
// Listening to chain events and manipulate the transaction indexes. // Listening to chain events and manipulate the transaction indexes.
var ( var (
@ -306,4 +311,6 @@ func (indexer *txIndexer) close() {
<-ch <-ch
case <-indexer.closed: case <-indexer.closed:
} }
indexer.wg.Wait()
} }