@@ -36,17 +36,25 @@ import (
3636type ChainIndexerBackend interface {
3737 // Reset initiates the processing of a new chain segment, potentially terminating
3838 // any partially completed operations (in case of a reorg).
39- Reset (section uint64 , lastSectionHead common.Hash ) error
39+ Reset (section uint64 , prevHead common.Hash ) error
4040
4141 // Process crunches through the next header in the chain segment. The caller
4242 // will ensure a sequential order of headers.
4343 Process (header * types.Header )
4444
45- // Commit finalizes the section metadata and stores it into the database. This
46- // interface will usually be a batch writer.
45+ // Commit finalizes the section metadata and stores it into the database.
4746 Commit () error
4847}
4948
49+ // ChainIndexerChain interface is used for connecting the indexer to a blockchain
50+ type ChainIndexerChain interface {
51+ // CurrentHeader retrieves the latest locally known header.
52+ CurrentHeader () * types.Header
53+
54+ // SubscribeChainEvent subscribes to new head header notifications.
55+ SubscribeChainEvent (ch chan <- ChainEvent ) event.Subscription
56+ }
57+
5058// ChainIndexer does a post-processing job for equally sized sections of the
5159// canonical chain (like BlooomBits and CHT structures). A ChainIndexer is
5260// connected to the blockchain through the event system by starting a
@@ -114,21 +122,14 @@ func (c *ChainIndexer) AddKnownSectionHead(section uint64, shead common.Hash) {
114122 c .setValidSections (section + 1 )
115123}
116124
117- // IndexerChain interface is used for connecting the indexer to a blockchain
118- type IndexerChain interface {
119- CurrentHeader () * types.Header
120- SubscribeChainEvent (ch chan <- ChainEvent ) event.Subscription
121- }
122-
123125// Start creates a goroutine to feed chain head events into the indexer for
124126// cascading background processing. Children do not need to be started, they
125127// are notified about new events by their parents.
126- func (c * ChainIndexer ) Start (chain IndexerChain ) {
127- ch := make (chan ChainEvent , 10 )
128- sub := chain .SubscribeChainEvent (ch )
129- currentHeader := chain .CurrentHeader ()
128+ func (c * ChainIndexer ) Start (chain ChainIndexerChain ) {
129+ events := make (chan ChainEvent , 10 )
130+ sub := chain .SubscribeChainEvent (events )
130131
131- go c .eventLoop (currentHeader , ch , sub )
132+ go c .eventLoop (chain . CurrentHeader (), events , sub )
132133}
133134
134135// Close tears down all goroutines belonging to the indexer and returns any error
@@ -149,14 +150,12 @@ func (c *ChainIndexer) Close() error {
149150 errs = append (errs , err )
150151 }
151152 }
152-
153153 // Close all children
154154 for _ , child := range c .children {
155155 if err := child .Close (); err != nil {
156156 errs = append (errs , err )
157157 }
158158 }
159-
160159 // Return any failures
161160 switch {
162161 case len (errs ) == 0 :
@@ -173,7 +172,7 @@ func (c *ChainIndexer) Close() error {
173172// eventLoop is a secondary - optional - event loop of the indexer which is only
174173// started for the outermost indexer to push chain head events into a processing
175174// queue.
176- func (c * ChainIndexer ) eventLoop (currentHeader * types.Header , ch chan ChainEvent , sub event.Subscription ) {
175+ func (c * ChainIndexer ) eventLoop (currentHeader * types.Header , events chan ChainEvent , sub event.Subscription ) {
177176 // Mark the chain indexer as active, requiring an additional teardown
178177 atomic .StoreUint32 (& c .active , 1 )
179178
@@ -193,7 +192,7 @@ func (c *ChainIndexer) eventLoop(currentHeader *types.Header, ch chan ChainEvent
193192 errc <- nil
194193 return
195194
196- case ev , ok := <- ch :
195+ case ev , ok := <- events :
197196 // Received a new event, ensure it's not nil (closing) and update
198197 if ! ok {
199198 errc := <- c .quit
@@ -202,6 +201,8 @@ func (c *ChainIndexer) eventLoop(currentHeader *types.Header, ch chan ChainEvent
202201 }
203202 header := ev .Block .Header ()
204203 if header .ParentHash != prevHash {
204+ // Reorg to the common ancestor (might not exist in light sync mode, skip reorg then)
205+ // TODO(karalabe, zsfelfoldi): This seems a bit brittle, can we detect this case explicitly?
205206 if h := FindCommonAncestor (c .chainDb , prevHeader , header ); h != nil {
206207 c .newHead (h .Number .Uint64 (), true )
207208 }
@@ -259,8 +260,8 @@ func (c *ChainIndexer) newHead(head uint64, reorg bool) {
259260// down into the processing backend.
260261func (c * ChainIndexer ) updateLoop () {
261262 var (
262- updated time. Time
263- updateMsg bool
263+ updating bool
264+ updated time. Time
264265 )
265266
266267 for {
@@ -277,7 +278,7 @@ func (c *ChainIndexer) updateLoop() {
277278 // Periodically print an upgrade log message to the user
278279 if time .Since (updated ) > 8 * time .Second {
279280 if c .knownSections > c .storedSections + 1 {
280- updateMsg = true
281+ updating = true
281282 c .log .Info ("Upgrading chain index" , "percentage" , c .storedSections * 100 / c .knownSections )
282283 }
283284 updated = time .Now ()
@@ -300,8 +301,8 @@ func (c *ChainIndexer) updateLoop() {
300301 if err == nil && oldHead == c .SectionHead (section - 1 ) {
301302 c .setSectionHead (section , newHead )
302303 c .setValidSections (section + 1 )
303- if c .storedSections == c .knownSections && updateMsg {
304- updateMsg = false
304+ if c .storedSections == c .knownSections && updating {
305+ updating = false
305306 c .log .Info ("Finished upgrading chain index" )
306307 }
307308
@@ -412,7 +413,7 @@ func (c *ChainIndexer) setValidSections(sections uint64) {
412413 c .storedSections = sections // needed if new > old
413414}
414415
415- // sectionHead retrieves the last block hash of a processed section from the
416+ // SectionHead retrieves the last block hash of a processed section from the
416417// index database.
417418func (c * ChainIndexer ) SectionHead (section uint64 ) common.Hash {
418419 var data [8 ]byte
0 commit comments