Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Commit 6c0bc6e

Browse files
committed
one more change
1 parent 093e701 commit 6c0bc6e

File tree

2 files changed

+19
-108
lines changed

2 files changed

+19
-108
lines changed

tracker/blocktracker.go

Lines changed: 19 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -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

9593
func (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

14297
func (t *BlockTracker) MaxBlockBacklog() uint64 {
14398
return t.config.MaxBlockBacklog
14499
}
145100

146101
func (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

155109
func (t *BlockTracker) BlocksBlocked() []*ethgo.Block {
@@ -212,13 +166,8 @@ func (t *BlockTracker) blockAtIndex(hash ethgo.Hash) int {
212166
}
213167

214168
func (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
}

tracker/blocktracker_test.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,6 @@ import (
1010
"github.com/umbracle/ethgo/testutil"
1111
)
1212

13-
func TestBlockTracker_PopulateBlocks(t *testing.T) {
14-
t.Parallel()
15-
16-
// more than maxBackLog blocks
17-
{
18-
l := testutil.MockList{}
19-
l.Create(0, 15, func(b *testutil.MockBlock) {})
20-
21-
m := &testutil.MockClient{}
22-
m.AddScenario(l)
23-
24-
tt0 := NewBlockTracker(m, hclog.NewNullLogger())
25-
26-
require.NoError(t, tt0.Init())
27-
require.True(t, testutil.CompareBlocks(l.ToBlocks()[5:], tt0.blocks))
28-
}
29-
// less than maxBackLog
30-
{
31-
l0 := testutil.MockList{}
32-
l0.Create(0, 5, func(b *testutil.MockBlock) {})
33-
34-
m1 := &testutil.MockClient{}
35-
m1.AddScenario(l0)
36-
37-
tt1 := NewBlockTracker(m1, hclog.NewNullLogger())
38-
tt1.provider = m1
39-
40-
require.NoError(t, tt1.Init())
41-
require.True(t, testutil.CompareBlocks(l0.ToBlocks(), tt1.blocks))
42-
}
43-
}
44-
4513
func TestBlockTracker_Events(t *testing.T) {
4614
t.Parallel()
4715

0 commit comments

Comments
 (0)