@@ -24,7 +24,6 @@ type BlockTracker struct {
2424 blockChsLock sync.Mutex
2525 provider bt.BlockProvider
2626 closeCh chan struct {}
27- once sync.Once
2827 logger hclog.Logger
2928}
3029
@@ -65,16 +64,15 @@ func NewBlockTracker(provider bt.BlockProvider, logger hclog.Logger, opts ...Con
6564 }
6665
6766 return & BlockTracker {
68- blocks : [] * ethgo. Block {} ,
69- blockChs : [] chan * bt. BlockEvent {} ,
67+ blocks : nil ,
68+ blockChs : nil ,
7069 config : config ,
7170 subscriber : tracker ,
7271 provider : provider ,
7372 closeCh : make (chan struct {}),
7473 logger : logger ,
7574 blocksLock : sync.Mutex {},
7675 blockChsLock : sync.Mutex {},
77- once : sync.Once {},
7876 }
7977}
8078
@@ -93,63 +91,19 @@ func (t *BlockTracker) AcquireLock() bt.Lock {
9391}
9492
9593func (t * BlockTracker ) Init () error {
96- var (
97- block * ethgo.Block
98- err error
99- i uint64
100- )
101-
102- t .once .Do (func () {
103- block , err = t .provider .GetBlockByNumber (ethgo .Latest , false )
104- if err != nil {
105- return
106- }
107-
108- if block .Number == 0 {
109- return
110- }
111-
112- blocks := make ([]* ethgo.Block , t .config .MaxBlockBacklog )
113-
114- for i = 0 ; i < t .config .MaxBlockBacklog ; i ++ {
115- blocks [t .config .MaxBlockBacklog - i - 1 ] = block
116- if block .Number == 0 {
117- break
118- }
119-
120- block , err = t .provider .GetBlockByHash (block .ParentHash , false )
121- if err != nil {
122- return
123- } else if block == nil {
124- // if block does not exist (for example reorg happened) GetBlockByHash will return nil, nil
125- err = fmt .Errorf ("block with hash %s not found" , block .ParentHash )
126-
127- return
128- }
129- }
130-
131- if i != t .config .MaxBlockBacklog {
132- // less than maxBacklog elements
133- blocks = blocks [t .config .MaxBlockBacklog - i - 1 :]
134- }
135-
136- t .blocks = blocks
137- })
138-
139- return err
94+ return nil
14095}
14196
14297func (t * BlockTracker ) MaxBlockBacklog () uint64 {
14398 return t .config .MaxBlockBacklog
14499}
145100
146101func (t * BlockTracker ) LastBlocked () * ethgo.Block {
147- target := t .blocks [len (t .blocks )- 1 ]
148- if target == nil {
102+ if len (t .blocks ) == 0 {
149103 return nil
150104 }
151105
152- return target .Copy ()
106+ return t . blocks [ len ( t . blocks ) - 1 ] .Copy ()
153107}
154108
155109func (t * BlockTracker ) BlocksBlocked () []* ethgo.Block {
@@ -212,13 +166,8 @@ func (t *BlockTracker) blockAtIndex(hash ethgo.Hash) int {
212166}
213167
214168func (t * BlockTracker ) handleReconcileImpl (block * ethgo.Block ) ([]* ethgo.Block , int , error ) {
215- // The state is empty
216- if len (t .blocks ) == 0 {
217- return []* ethgo.Block {block }, - 1 , nil
218- }
219-
220169 // Append to the head of the chain
221- if t .blocks [len (t .blocks )- 1 ].Hash == block .ParentHash {
170+ if len ( t . blocks ) > 0 && t .blocks [len (t .blocks )- 1 ].Hash == block .ParentHash {
222171 return []* ethgo.Block {block }, - 1 , nil
223172 }
224173
@@ -227,38 +176,34 @@ func (t *BlockTracker) handleReconcileImpl(block *ethgo.Block) ([]*ethgo.Block,
227176 return nil , indx + 1 , nil
228177 }
229178
230- // Fork in the middle of the chain
231- if indx := t .blockAtIndex (block .ParentHash ); indx != - 1 {
232- return []* ethgo.Block {block }, indx + 1 , nil
233- }
234-
235179 // Backfill. We dont know the parent of the block.
236180 // Need to query the chain until we find a known block
237181 var (
238182 added = []* ethgo.Block {block }
239183 count uint64 = 0
240- indx int
184+ indx = 0 // if indx stays 0 - it means remove all from the current state
241185 err error
242186 )
243187
244- for ; count < t .config .MaxBlockBacklog ; count ++ {
245- hash := block .ParentHash
188+ for ; count < t .config .MaxBlockBacklog && block .Number > 1 ; count ++ {
189+ parentHash := block .ParentHash
190+
191+ // if there is a parent at some index, break loop and update from where should we delete
192+ if indx = t .blockAtIndex (parentHash ); indx != - 1 {
193+ indx ++
194+
195+ break
196+ }
246197
247- block , err = t .provider .GetBlockByHash (hash , false )
198+ block , err = t .provider .GetBlockByHash (parentHash , false )
248199 if err != nil {
249200 return nil , - 1 , fmt .Errorf ("parent with hash retrieving error: %w" , err )
250201 } else if block == nil {
251202 // if block does not exist (for example reorg happened) GetBlockByHash will return nil, nil
252- return nil , - 1 , fmt .Errorf ("parent with hash %s not found" , hash )
203+ return nil , - 1 , fmt .Errorf ("parent with hash %s not found" , parentHash )
253204 }
254205
255206 added = append (added , block )
256-
257- if indx = t .blockAtIndex (block .ParentHash ); indx != - 1 {
258- indx ++
259-
260- break
261- }
262207 }
263208
264209 // need the blocks in reverse order
@@ -268,8 +213,6 @@ func (t *BlockTracker) handleReconcileImpl(block *ethgo.Block) ([]*ethgo.Block,
268213 }
269214
270215 if count == t .config .MaxBlockBacklog {
271- indx = 0 // remove all from the current state
272-
273216 t .logger .Info ("reconcile did not found parent for new blocks" , "hash" , added [0 ].Hash )
274217 }
275218
@@ -292,7 +235,7 @@ func (t *BlockTracker) HandleBlockEvent(block *ethgo.Block) (*bt.BlockEvent, err
292235 blockEvnt := & bt.BlockEvent {}
293236
294237 // there are some blocks to remove
295- if indx >= 0 {
238+ if indx >= 0 && indx < len ( t . blocks ) {
296239 for i := indx ; i < len (t .blocks ); i ++ {
297240 blockEvnt .Removed = append (blockEvnt .Removed , t .blocks [i ])
298241 }
0 commit comments