Skip to content

Commit 26e6a34

Browse files
author
Darioush Jalali
authored
tx indexer fix: avoids using sleep in test (#1151)
* avoids using sleep in test * track all goroutines * trying harder * add some debug information in case of fail * Update core/blockchain.go Signed-off-by: Darioush Jalali <darioush.jalali@avalabs.org> * Update core/blockchain.go Signed-off-by: Darioush Jalali <darioush.jalali@avalabs.org> --------- Signed-off-by: Darioush Jalali <darioush.jalali@avalabs.org>
1 parent ac4e239 commit 26e6a34

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

core/blockchain.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,20 @@ func NewBlockChain(
428428
// Start tx indexer/unindexer if required.
429429
if bc.cacheConfig.TxLookupLimit != 0 {
430430
bc.wg.Add(1)
431-
go bc.maintainTxIndex()
431+
var (
432+
headCh = make(chan ChainEvent, 1) // Buffered to avoid locking up the event feed
433+
sub = bc.SubscribeChainAcceptedEvent(headCh)
434+
)
435+
go func() {
436+
defer bc.wg.Done()
437+
if sub == nil {
438+
log.Warn("could not create chain accepted subscription to unindex txs")
439+
return
440+
}
441+
defer sub.Unsubscribe()
442+
443+
bc.maintainTxIndex(headCh)
444+
}()
432445
}
433446
return bc, nil
434447
}
@@ -442,6 +455,7 @@ func (bc *BlockChain) unindexBlocks(tail uint64, head uint64, done chan struct{}
442455
txUnindexTimer.Inc(time.Since(start).Milliseconds())
443456
bc.txIndexTailLock.Unlock()
444457
close(done)
458+
bc.wg.Done()
445459
}()
446460

447461
if head-txLookupLimit+1 >= tail {
@@ -454,8 +468,7 @@ func (bc *BlockChain) unindexBlocks(tail uint64, head uint64, done chan struct{}
454468
// transaction index. This does not support reconstruction of removed indexes.
455469
// Invariant: If TxLookupLimit is 0, it means all tx indices will be preserved.
456470
// Meaning that this function should never be called.
457-
func (bc *BlockChain) maintainTxIndex() {
458-
defer bc.wg.Done()
471+
func (bc *BlockChain) maintainTxIndex(headCh <-chan ChainEvent) {
459472
txLookupLimit := bc.cacheConfig.TxLookupLimit
460473

461474
// If the user just upgraded to a new version which supports transaction
@@ -466,15 +479,8 @@ func (bc *BlockChain) maintainTxIndex() {
466479

467480
// Any reindexing done, start listening to chain events and moving the index window
468481
var (
469-
done chan struct{} // Non-nil if background unindexing or reindexing routine is active.
470-
headCh = make(chan ChainEvent, 1) // Buffered to avoid locking up the event feed
482+
done chan struct{} // Non-nil if background unindexing or reindexing routine is active.
471483
)
472-
sub := bc.SubscribeChainAcceptedEvent(headCh)
473-
if sub == nil {
474-
log.Warn("could not create chain accepted subscription to unindex txs")
475-
return
476-
}
477-
defer sub.Unsubscribe()
478484
log.Info("Initialized transaction unindexer", "limit", txLookupLimit)
479485

480486
// Launch the initial processing if chain is not empty. This step is
@@ -483,6 +489,7 @@ func (bc *BlockChain) maintainTxIndex() {
483489
if head := bc.CurrentBlock(); head != nil && head.Number.Uint64() > txLookupLimit {
484490
done = make(chan struct{})
485491
tail := rawdb.ReadTxIndexTail(bc.db)
492+
bc.wg.Add(1)
486493
go bc.unindexBlocks(*tail, head.Number.Uint64(), done)
487494
}
488495

@@ -498,6 +505,7 @@ func (bc *BlockChain) maintainTxIndex() {
498505
done = make(chan struct{})
499506
// Note: tail will not be nil since it is initialized in this function.
500507
tail := rawdb.ReadTxIndexTail(bc.db)
508+
bc.wg.Add(1)
501509
go bc.unindexBlocks(*tail, headNum, done)
502510
}
503511
case <-done:

core/blockchain_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"math/big"
99
"os"
1010
"testing"
11-
"time"
1211

1312
"github.com/ava-labs/subnet-evm/consensus/dummy"
1413
"github.com/ava-labs/subnet-evm/core/rawdb"
@@ -697,6 +696,7 @@ func TestTransactionSkipIndexing(t *testing.T) {
697696
require.NoError(err)
698697
currentBlockNumber := chain.CurrentBlock().Number.Uint64()
699698
checkTxIndicesHelper(t, nil, currentBlockNumber+1, currentBlockNumber+1, currentBlockNumber, chainDB, false) // check all indices has been skipped
699+
chain.Stop()
700700

701701
// test2: specify lookuplimit with tx index skipping enabled. Blocks should not be indexed but tail should be updated.
702702
conf.TxLookupLimit = 2
@@ -705,6 +705,7 @@ func TestTransactionSkipIndexing(t *testing.T) {
705705
currentBlockNumber = chain.CurrentBlock().Number.Uint64()
706706
tail := currentBlockNumber - conf.TxLookupLimit + 1
707707
checkTxIndicesHelper(t, &tail, currentBlockNumber+1, currentBlockNumber+1, currentBlockNumber, chainDB, false) // check all indices has been skipped
708+
chain.Stop()
708709

709710
// test3: tx index skipping and unindexer disabled. Blocks should be indexed and tail should be updated.
710711
conf.TxLookupLimit = 0
@@ -714,6 +715,7 @@ func TestTransactionSkipIndexing(t *testing.T) {
714715
require.NoError(err)
715716
currentBlockNumber = chain.CurrentBlock().Number.Uint64()
716717
checkTxIndicesHelper(t, nil, 0, currentBlockNumber, currentBlockNumber, chainDB, false) // check all indices has been indexed
718+
chain.Stop()
717719

718720
// now change tx index skipping to true and check that the indices are skipped for the last block
719721
// and old indices are removed up to the tail, but [tail, current) indices are still there.
@@ -724,6 +726,7 @@ func TestTransactionSkipIndexing(t *testing.T) {
724726
currentBlockNumber = chain.CurrentBlock().Number.Uint64()
725727
tail = currentBlockNumber - conf.TxLookupLimit + 1
726728
checkTxIndicesHelper(t, &tail, tail, currentBlockNumber-1, currentBlockNumber, chainDB, false)
729+
chain.Stop()
727730
}
728731

729732
// TestCanonicalHashMarker tests all the canonical hash markers are updated/deleted
@@ -1196,10 +1199,7 @@ func createAndInsertChain(db ethdb.Database, cacheConfig *CacheConfig, gspec *Ge
11961199
return nil, err
11971200
}
11981201
}
1199-
12001202
chain.DrainAcceptorQueue()
1201-
time.Sleep(500 * time.Millisecond) // Wait for indices initialisation
12021203

1203-
chain.Stop()
12041204
return chain, nil
12051205
}

core/test_blockchain.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,13 +1669,14 @@ func checkTxIndicesHelper(t *testing.T, expectedTail *uint64, indexedFrom uint64
16691669
if expectedTail == nil {
16701670
require.Nil(rawdb.ReadTxIndexTail(db))
16711671
} else {
1672+
var stored uint64
16721673
tailValue := *expectedTail
16731674
require.Eventually(
16741675
func() bool {
1675-
stored := *rawdb.ReadTxIndexTail(db)
1676+
stored = *rawdb.ReadTxIndexTail(db)
16761677
return tailValue == stored
16771678
},
1678-
10*time.Second, 100*time.Millisecond, "expected tail to be %d eventually", tailValue)
1679+
10*time.Second, 100*time.Millisecond, "expected tail to be %d eventually (was %d)", tailValue, stored)
16791680
}
16801681

16811682
for i := uint64(0); i <= head; i++ {

0 commit comments

Comments
 (0)