Skip to content

Commit

Permalink
snapshots: fix filesByRange (#9472) (#9489)
Browse files Browse the repository at this point in the history
The method was iterating over snapshots.segments.Min().segments, but
passing the index to view.Segments().

This might lead to a crash, because those 2 collections are different,
and the indexes might not match.

view.Segments() only contains the snapshot files that are fully
downloaded and indexed.

The code is changed to only use the view files (as it was before).
  • Loading branch information
battlmonstr committed Feb 22, 2024
1 parent ab1ee7b commit f12e451
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
9 changes: 9 additions & 0 deletions erigon-lib/downloader/snaptype/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,13 @@ var (
BorSnapshotTypes = []Type{BorEvents, BorSpans}

CaplinSnapshotTypes = []Type{BeaconBlocks}

AllTypes = []Type{
Headers,
Bodies,
Transactions,
BorEvents,
BorSpans,
BeaconBlocks,
}
)
32 changes: 19 additions & 13 deletions turbo/snapshotsync/freezeblocks/block_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -2143,26 +2143,32 @@ func (m *Merger) FindMergeRanges(currentRanges []Range, maxBlockNum uint64) (toM

func (m *Merger) filesByRange(snapshots *RoSnapshots, from, to uint64) (map[snaptype.Enum][]string, error) {
toMerge := map[snaptype.Enum][]string{}

view := snapshots.View()
defer view.Close()

if _, first, ok := snapshots.segments.Min(); ok {
for i, sn := range first.segments {
if sn.from < from {
continue
}
if sn.to > to {
break
}
for _, t := range snaptype.AllTypes {
toMerge[t.Enum()] = m.filesByRangeOfType(view, from, to, t)
}

snapshots.segments.Scan(func(key snaptype.Enum, value *segments) bool {
toMerge[key] = append(toMerge[key], view.Segments(key.Type())[i].FilePath())
return true
})
return toMerge, nil
}

func (m *Merger) filesByRangeOfType(view *View, from, to uint64, snapshotType snaptype.Type) []string {
paths := make([]string, 0)

for _, sn := range view.Segments(snapshotType) {
if sn.from < from {
continue
}
if sn.to > to {
break
}

paths = append(paths, sn.FilePath())
}

return toMerge, nil
return paths
}

// Merge does merge segments in given ranges
Expand Down

0 comments on commit f12e451

Please sign in to comment.