Skip to content

Commit

Permalink
fix: ut build
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Sep 19, 2024
1 parent 8aea9de commit 5a974dc
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 41 deletions.
3 changes: 2 additions & 1 deletion l1infotreesync/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func buildAppender(client EthClienter, globalExitRoot, rollupManager common.Addr
l, err,
)
}
log.Infof("updateL1InfoTreeSignatureV2: expected root: %s", common.BytesToHash(l1InfoTreeUpdate.CurrentL1InfoRoot[:]).String())
log.Infof("updateL1InfoTreeSignatureV2: expected root: %s",
common.BytesToHash(l1InfoTreeUpdate.CurrentL1InfoRoot[:]).String())

return nil
}
Expand Down
7 changes: 3 additions & 4 deletions reorgdetector/reorgdetector.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ func (rd *ReorgDetector) detectReorgInTrackedList(ctx context.Context) error {
for _, id := range subscriberIDs {
id := id

// This is done like this because of a possible deadlock between AddBlocksToTrack and detectReorgInTrackedList
// because detectReorgInTrackedList would take the trackedBlocksLock and try to notify the subscriber in case of a reorg
// but the subscriber would be trying to add a block to track and save it to trackedBlocks, resulting in a deadlock
// This is done like this because of a possible deadlock
// between AddBlocksToTrack and detectReorgInTrackedList
rd.trackedBlocksLock.RLock()
hdrs, ok := rd.trackedBlocks[id]
rd.trackedBlocksLock.RUnlock()
Expand Down Expand Up @@ -162,7 +161,7 @@ func (rd *ReorgDetector) detectReorgInTrackedList(ctx context.Context) error {
continue
}

log.Info("[ReorgDetector] Reorg detected", "blockNum", hdr.Num)
log.Info("[ReorgDetector] Reorg detected", " blockNum ", hdr.Num)

// Notify the subscriber about the reorg
rd.notifySubscriber(id, hdr)
Expand Down
5 changes: 2 additions & 3 deletions reorgdetector/reorgdetector_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ func (rd *ReorgDetector) getTrackedBlocks(ctx context.Context) (map[string]*head
func (rd *ReorgDetector) saveTrackedBlock(ctx context.Context, id string, b header) error {
rd.trackedBlocksLock.Lock()

// this has to go after the lock, because of a possible deadlock between AddBlocksToTrack and detectReorgInTrackedList
// because AddBlocksToTrack would start a transaction on db, but detectReorgInTrackedList would lock the trackedBlocksLock
// and then try to start a transaction on db, resulting in a deadlock
// this has to go after the lock, because of a possible deadlock
// between AddBlocksToTrack and detectReorgInTrackedList
tx, err := rd.db.BeginRw(ctx)
if err != nil {
return err
Expand Down
16 changes: 8 additions & 8 deletions sync/evmdownloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ func TestDownload(t *testing.T) {
}
expectedBlocks = append(expectedBlocks, b1)
d.On("GetEventsByBlockRange", mock.Anything, uint64(0), uint64(1)).
Return([]EVMBlock{})
Return([]EVMBlock{}, false)
d.On("GetBlockHeader", mock.Anything, uint64(1)).
Return(b1.EVMBlockHeader)
Return(b1.EVMBlockHeader, false)

// iteration 1: wait for next block to be created
d.On("WaitForNewBlocks", mock.Anything, uint64(1)).
Expand All @@ -240,7 +240,7 @@ func TestDownload(t *testing.T) {
}
expectedBlocks = append(expectedBlocks, b2)
d.On("GetEventsByBlockRange", mock.Anything, uint64(2), uint64(2)).
Return([]EVMBlock{b2})
Return([]EVMBlock{b2}, false)

// iteration 3: wait for next block to be created (jump to block 8)
d.On("WaitForNewBlocks", mock.Anything, uint64(2)).
Expand Down Expand Up @@ -270,9 +270,9 @@ func TestDownload(t *testing.T) {
}
expectedBlocks = append(expectedBlocks, b6, b7, b8)
d.On("GetEventsByBlockRange", mock.Anything, uint64(3), uint64(8)).
Return([]EVMBlock{b6, b7})
Return([]EVMBlock{b6, b7}, false)
d.On("GetBlockHeader", mock.Anything, uint64(8)).
Return(b8.EVMBlockHeader)
Return(b8.EVMBlockHeader, false)

// iteration 5: wait for next block to be created (jump to block 30)
d.On("WaitForNewBlocks", mock.Anything, uint64(8)).
Expand All @@ -288,9 +288,9 @@ func TestDownload(t *testing.T) {
}
expectedBlocks = append(expectedBlocks, b19)
d.On("GetEventsByBlockRange", mock.Anything, uint64(9), uint64(19)).
Return([]EVMBlock{})
Return([]EVMBlock{}, false)
d.On("GetBlockHeader", mock.Anything, uint64(19)).
Return(b19.EVMBlockHeader)
Return(b19.EVMBlockHeader, false)

// iteration 7: from block 20 to 30, events on last block
b30 := EVMBlock{
Expand All @@ -302,7 +302,7 @@ func TestDownload(t *testing.T) {
}
expectedBlocks = append(expectedBlocks, b30)
d.On("GetEventsByBlockRange", mock.Anything, uint64(20), uint64(30)).
Return([]EVMBlock{b30})
Return([]EVMBlock{b30}, false)

// iteration 8: wait for next block to be created (jump to block 35)
d.On("WaitForNewBlocks", mock.Anything, uint64(30)).
Expand Down
21 changes: 2 additions & 19 deletions sync/evmdriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,36 +198,19 @@ func TestHandleReorg(t *testing.T) {

// happy path
_, cancel := context.WithCancel(ctx)
downloadCh := make(chan EVMBlock)
firstReorgedBlock := uint64(5)
pm.On("Reorg", ctx, firstReorgedBlock).Return(nil)
go driver.handleReorg(ctx, cancel, downloadCh, firstReorgedBlock)
close(downloadCh)
go driver.handleReorg(ctx, cancel, firstReorgedBlock)
done := <-reorgProcessed
require.True(t, done)

// download ch sends some garbage
_, cancel = context.WithCancel(ctx)
downloadCh = make(chan EVMBlock)
firstReorgedBlock = uint64(6)
pm.On("Reorg", ctx, firstReorgedBlock).Return(nil)
go driver.handleReorg(ctx, cancel, downloadCh, firstReorgedBlock)
downloadCh <- EVMBlock{}
downloadCh <- EVMBlock{}
downloadCh <- EVMBlock{}
close(downloadCh)
done = <-reorgProcessed
require.True(t, done)

// processor fails 2 times
_, cancel = context.WithCancel(ctx)
downloadCh = make(chan EVMBlock)
firstReorgedBlock = uint64(7)
pm.On("Reorg", ctx, firstReorgedBlock).Return(errors.New("foo")).Once()
pm.On("Reorg", ctx, firstReorgedBlock).Return(errors.New("foo")).Once()
pm.On("Reorg", ctx, firstReorgedBlock).Return(nil).Once()
go driver.handleReorg(ctx, cancel, downloadCh, firstReorgedBlock)
close(downloadCh)
go driver.handleReorg(ctx, cancel, firstReorgedBlock)
done = <-reorgProcessed
require.True(t, done)
}
6 changes: 0 additions & 6 deletions tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
)

func TestCheckExpectedRoot(t *testing.T) {
t.Parallel()

createTreeDB := func() *sql.DB {
dbPath := path.Join(t.TempDir(), "file::memory:?cache=shared")
log.Debug("DB created at: ", dbPath)
Expand Down Expand Up @@ -49,8 +47,6 @@ func TestCheckExpectedRoot(t *testing.T) {
}

t.Run("Check when no reorg", func(t *testing.T) {
t.Parallel()

numOfLeavesToAdd := 10
indexToCheck := uint32(numOfLeavesToAdd - 1)

Expand All @@ -71,8 +67,6 @@ func TestCheckExpectedRoot(t *testing.T) {
})

t.Run("Check after rebuild tree when reorg", func(t *testing.T) {
t.Parallel()

numOfLeavesToAdd := 10
indexToCheck := uint32(numOfLeavesToAdd - 1)
treeDB := createTreeDB()
Expand Down

0 comments on commit 5a974dc

Please sign in to comment.