@@ -36,13 +36,14 @@ 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 )
39+ Reset (section uint64 , lastSectionHead 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.
45+ // Commit finalizes the section metadata and stores it into the database. This
46+ // interface will usually be a batch writer.
4647 Commit () error
4748}
4849
@@ -100,11 +101,34 @@ func NewChainIndexer(chainDb, indexDb ethdb.Database, backend ChainIndexerBacken
100101 return c
101102}
102103
104+ // AddKnownSectionHead marks a new section head as known/processed if it is newer
105+ // than the already known best section head
106+ func (c * ChainIndexer ) AddKnownSectionHead (section uint64 , shead common.Hash ) {
107+ c .lock .Lock ()
108+ defer c .lock .Unlock ()
109+
110+ if section < c .storedSections {
111+ return
112+ }
113+ c .setSectionHead (section , shead )
114+ c .setValidSections (section + 1 )
115+ }
116+
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+
103123// Start creates a goroutine to feed chain head events into the indexer for
104124// cascading background processing. Children do not need to be started, they
105125// are notified about new events by their parents.
106- func (c * ChainIndexer ) Start (currentHeader * types.Header , chainEventer func (ch chan <- ChainEvent ) event.Subscription ) {
107- go c .eventLoop (currentHeader , chainEventer )
126+ func (c * ChainIndexer ) Start (chain IndexerChain ) {
127+ ch := make (chan ChainEvent , 10 )
128+ sub := chain .SubscribeChainEvent (ch )
129+ currentHeader := chain .CurrentHeader ()
130+
131+ go c .eventLoop (currentHeader , ch , sub )
108132}
109133
110134// Close tears down all goroutines belonging to the indexer and returns any error
@@ -125,12 +149,14 @@ func (c *ChainIndexer) Close() error {
125149 errs = append (errs , err )
126150 }
127151 }
152+
128153 // Close all children
129154 for _ , child := range c .children {
130155 if err := child .Close (); err != nil {
131156 errs = append (errs , err )
132157 }
133158 }
159+
134160 // Return any failures
135161 switch {
136162 case len (errs ) == 0 :
@@ -147,12 +173,10 @@ func (c *ChainIndexer) Close() error {
147173// eventLoop is a secondary - optional - event loop of the indexer which is only
148174// started for the outermost indexer to push chain head events into a processing
149175// queue.
150- func (c * ChainIndexer ) eventLoop (currentHeader * types.Header , chainEventer func ( ch chan <- ChainEvent ) event.Subscription ) {
176+ func (c * ChainIndexer ) eventLoop (currentHeader * types.Header , ch chan ChainEvent , sub event.Subscription ) {
151177 // Mark the chain indexer as active, requiring an additional teardown
152178 atomic .StoreUint32 (& c .active , 1 )
153179
154- events := make (chan ChainEvent , 10 )
155- sub := chainEventer (events )
156180 defer sub .Unsubscribe ()
157181
158182 // Fire the initial new head event to start any outstanding processing
@@ -169,7 +193,7 @@ func (c *ChainIndexer) eventLoop(currentHeader *types.Header, chainEventer func(
169193 errc <- nil
170194 return
171195
172- case ev , ok := <- events :
196+ case ev , ok := <- ch :
173197 // Received a new event, ensure it's not nil (closing) and update
174198 if ! ok {
175199 errc := <- c .quit
@@ -178,7 +202,9 @@ func (c *ChainIndexer) eventLoop(currentHeader *types.Header, chainEventer func(
178202 }
179203 header := ev .Block .Header ()
180204 if header .ParentHash != prevHash {
181- c .newHead (FindCommonAncestor (c .chainDb , prevHeader , header ).Number .Uint64 (), true )
205+ if h := FindCommonAncestor (c .chainDb , prevHeader , header ); h != nil {
206+ c .newHead (h .Number .Uint64 (), true )
207+ }
182208 }
183209 c .newHead (header .Number .Uint64 (), false )
184210
@@ -233,9 +259,10 @@ func (c *ChainIndexer) newHead(head uint64, reorg bool) {
233259// down into the processing backend.
234260func (c * ChainIndexer ) updateLoop () {
235261 var (
236- updating bool
237- updated time. Time
262+ updated time. Time
263+ updateMsg bool
238264 )
265+
239266 for {
240267 select {
241268 case errc := <- c .quit :
@@ -250,7 +277,7 @@ func (c *ChainIndexer) updateLoop() {
250277 // Periodically print an upgrade log message to the user
251278 if time .Since (updated ) > 8 * time .Second {
252279 if c .knownSections > c .storedSections + 1 {
253- updating = true
280+ updateMsg = true
254281 c .log .Info ("Upgrading chain index" , "percentage" , c .storedSections * 100 / c .knownSections )
255282 }
256283 updated = time .Now ()
@@ -259,7 +286,7 @@ func (c *ChainIndexer) updateLoop() {
259286 section := c .storedSections
260287 var oldHead common.Hash
261288 if section > 0 {
262- oldHead = c .sectionHead (section - 1 )
289+ oldHead = c .SectionHead (section - 1 )
263290 }
264291 // Process the newly defined section in the background
265292 c .lock .Unlock ()
@@ -270,11 +297,11 @@ func (c *ChainIndexer) updateLoop() {
270297 c .lock .Lock ()
271298
272299 // If processing succeeded and no reorgs occcurred, mark the section completed
273- if err == nil && oldHead == c .sectionHead (section - 1 ) {
300+ if err == nil && oldHead == c .SectionHead (section - 1 ) {
274301 c .setSectionHead (section , newHead )
275302 c .setValidSections (section + 1 )
276- if c .storedSections == c .knownSections && updating {
277- updating = false
303+ if c .storedSections == c .knownSections && updateMsg {
304+ updateMsg = false
278305 c .log .Info ("Finished upgrading chain index" )
279306 }
280307
@@ -311,7 +338,11 @@ func (c *ChainIndexer) processSection(section uint64, lastHead common.Hash) (com
311338 c .log .Trace ("Processing new chain section" , "section" , section )
312339
313340 // Reset and partial processing
314- c .backend .Reset (section )
341+
342+ if err := c .backend .Reset (section , lastHead ); err != nil {
343+ c .setValidSections (0 )
344+ return common.Hash {}, err
345+ }
315346
316347 for number := section * c .sectionSize ; number < (section + 1 )* c .sectionSize ; number ++ {
317348 hash := GetCanonicalHash (c .chainDb , number )
@@ -341,7 +372,7 @@ func (c *ChainIndexer) Sections() (uint64, uint64, common.Hash) {
341372 c .lock .Lock ()
342373 defer c .lock .Unlock ()
343374
344- return c .storedSections , c .storedSections * c .sectionSize - 1 , c .sectionHead (c .storedSections - 1 )
375+ return c .storedSections , c .storedSections * c .sectionSize - 1 , c .SectionHead (c .storedSections - 1 )
345376}
346377
347378// AddChildIndexer adds a child ChainIndexer that can use the output of this one
@@ -383,7 +414,7 @@ func (c *ChainIndexer) setValidSections(sections uint64) {
383414
384415// sectionHead retrieves the last block hash of a processed section from the
385416// index database.
386- func (c * ChainIndexer ) sectionHead (section uint64 ) common.Hash {
417+ func (c * ChainIndexer ) SectionHead (section uint64 ) common.Hash {
387418 var data [8 ]byte
388419 binary .BigEndian .PutUint64 (data [:], section )
389420
0 commit comments