Skip to content

Commit

Permalink
make txindexer more similar to upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
darioush committed Aug 23, 2024
1 parent a6b902b commit 6a40675
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
28 changes: 16 additions & 12 deletions core/txindexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,9 @@ func newTxIndexer(limit uint64, chain *BlockChain) *txIndexer {
chain: chain,
}
chain.wg.Add(1)
var (
headCh = make(chan ChainEvent, 1)
sub = chain.SubscribeChainAcceptedEvent(headCh)
)
go func() {
defer chain.wg.Done()
if sub == nil {
log.Warn("could not create chain accepted subscription to unindex txs")
return
}
defer sub.Unsubscribe()

indexer.loop(headCh)
indexer.loop(chain)
}()

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

// Defensively ensure tail is not nil.
if tail == nil {
tail = new(uint64)
}

if head-indexer.limit+1 >= *tail {
// Unindex a part of stale indices and forward index tail to HEAD-limit
rawdb.UnindexTransactions(indexer.db, *tail, head-indexer.limit+1, stop, false)
Expand All @@ -116,7 +111,7 @@ func (indexer *txIndexer) run(tail *uint64, head uint64, stop chan struct{}, don

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

// If the user just upgraded to a new version which supports transaction
Expand All @@ -131,7 +126,16 @@ func (indexer *txIndexer) loop(headCh <-chan ChainEvent) {
done chan struct{} // Non-nil if background routine is active.
lastHead uint64 // The latest announced chain head (whose tx indexes are assumed created)
lastTail = rawdb.ReadTxIndexTail(indexer.db) // The oldest indexed block, nil means nothing indexed

headCh = make(chan ChainEvent, 1)
sub = chain.SubscribeChainAcceptedEvent(headCh)
)
if sub == nil {
log.Warn("could not create chain accepted subscription to unindex txs")
return
}
defer sub.Unsubscribe()

log.Info("Initialized transaction unindexer", "limit", indexer.limit)

// Launch the initial processing if chain is not empty (head != genesis).
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46
github.com/go-cmd/cmd v1.4.1
github.com/golang/protobuf v1.5.4
github.com/google/uuid v1.6.0
github.com/gorilla/rpc v1.2.0
github.com/gorilla/websocket v1.4.2
Expand Down Expand Up @@ -86,6 +85,7 @@ require (
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
Expand Down

0 comments on commit 6a40675

Please sign in to comment.