Skip to content

Commit 6a40675

Browse files
author
Darioush Jalali
committed
make txindexer more similar to upstream
1 parent a6b902b commit 6a40675

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

core/txindexer.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,9 @@ func newTxIndexer(limit uint64, chain *BlockChain) *txIndexer {
6464
chain: chain,
6565
}
6666
chain.wg.Add(1)
67-
var (
68-
headCh = make(chan ChainEvent, 1)
69-
sub = chain.SubscribeChainAcceptedEvent(headCh)
70-
)
7167
go func() {
7268
defer chain.wg.Done()
73-
if sub == nil {
74-
log.Warn("could not create chain accepted subscription to unindex txs")
75-
return
76-
}
77-
defer sub.Unsubscribe()
78-
79-
indexer.loop(headCh)
69+
indexer.loop(chain)
8070
}()
8171

8272
var msg string
@@ -108,6 +98,11 @@ func (indexer *txIndexer) run(tail *uint64, head uint64, stop chan struct{}, don
10898
return
10999
}
110100

101+
// Defensively ensure tail is not nil.
102+
if tail == nil {
103+
tail = new(uint64)
104+
}
105+
111106
if head-indexer.limit+1 >= *tail {
112107
// Unindex a part of stale indices and forward index tail to HEAD-limit
113108
rawdb.UnindexTransactions(indexer.db, *tail, head-indexer.limit+1, stop, false)
@@ -116,7 +111,7 @@ func (indexer *txIndexer) run(tail *uint64, head uint64, stop chan struct{}, don
116111

117112
// loop is the scheduler of the indexer, assigning indexing/unindexing tasks depending
118113
// on the received chain event.
119-
func (indexer *txIndexer) loop(headCh <-chan ChainEvent) {
114+
func (indexer *txIndexer) loop(chain *BlockChain) {
120115
defer close(indexer.closed)
121116

122117
// If the user just upgraded to a new version which supports transaction
@@ -131,7 +126,16 @@ func (indexer *txIndexer) loop(headCh <-chan ChainEvent) {
131126
done chan struct{} // Non-nil if background routine is active.
132127
lastHead uint64 // The latest announced chain head (whose tx indexes are assumed created)
133128
lastTail = rawdb.ReadTxIndexTail(indexer.db) // The oldest indexed block, nil means nothing indexed
129+
130+
headCh = make(chan ChainEvent, 1)
131+
sub = chain.SubscribeChainAcceptedEvent(headCh)
134132
)
133+
if sub == nil {
134+
log.Warn("could not create chain accepted subscription to unindex txs")
135+
return
136+
}
137+
defer sub.Unsubscribe()
138+
135139
log.Info("Initialized transaction unindexer", "limit", indexer.limit)
136140

137141
// Launch the initial processing if chain is not empty (head != genesis).

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ require (
1717
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08
1818
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46
1919
github.com/go-cmd/cmd v1.4.1
20-
github.com/golang/protobuf v1.5.4
2120
github.com/google/uuid v1.6.0
2221
github.com/gorilla/rpc v1.2.0
2322
github.com/gorilla/websocket v1.4.2
@@ -86,6 +85,7 @@ require (
8685
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
8786
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
8887
github.com/gogo/protobuf v1.3.2 // indirect
88+
github.com/golang/protobuf v1.5.4 // indirect
8989
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
9090
github.com/google/btree v1.1.2 // indirect
9191
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect

0 commit comments

Comments
 (0)