From 08740b5fbbaae9bc6769de5f7890173955a05644 Mon Sep 17 00:00:00 2001 From: taratorio <94537774+taratorio@users.noreply.github.com> Date: Wed, 9 Oct 2024 17:24:14 +0100 Subject: [PATCH] freezeblocks: fix max visible segment when there is a gap in .idx files --- .../freezeblocks/block_snapshots.go | 2 +- .../freezeblocks/block_snapshots_test.go | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/turbo/snapshotsync/freezeblocks/block_snapshots.go b/turbo/snapshotsync/freezeblocks/block_snapshots.go index 9971fde51b8..f518564f505 100644 --- a/turbo/snapshotsync/freezeblocks/block_snapshots.go +++ b/turbo/snapshotsync/freezeblocks/block_snapshots.go @@ -609,7 +609,7 @@ func (s *RoSnapshots) recalcVisibleFiles() { continue } if seg.indexes == nil { - continue + break } for len(newVisibleSegments) > 0 && newVisibleSegments[len(newVisibleSegments)-1].src.isSubSetOf(seg) { newVisibleSegments[len(newVisibleSegments)-1].src = nil diff --git a/turbo/snapshotsync/freezeblocks/block_snapshots_test.go b/turbo/snapshotsync/freezeblocks/block_snapshots_test.go index f18c6ba694c..5350ed8032e 100644 --- a/turbo/snapshotsync/freezeblocks/block_snapshots_test.go +++ b/turbo/snapshotsync/freezeblocks/block_snapshots_test.go @@ -18,6 +18,7 @@ package freezeblocks import ( "context" + "os" "path/filepath" "testing" "testing/fstest" @@ -26,6 +27,7 @@ import ( "golang.org/x/exp/slices" "github.com/erigontech/erigon-lib/log/v3" + "github.com/erigontech/erigon/turbo/testlog" "github.com/erigontech/erigon-lib/chain/networkname" "github.com/erigontech/erigon-lib/chain/snapcfg" @@ -599,3 +601,32 @@ func TestCalculateVisibleSegments(t *testing.T) { require.Equal(5, getSeg(s, coresnaptype.Enums.Transactions).DirtySegments.Len()) } } + +func TestCalculateVisibleSegmentsWhenGapsInIdx(t *testing.T) { + logger := testlog.Logger(t, log.LvlCrit) + dir, require := t.TempDir(), require.New(t) + createFile := func(from, to uint64, name snaptype.Type) { + createTestSegmentFile(t, from, to, name.Enum(), dir, 1, logger) + } + + for i := uint64(0); i < 3; i++ { + createFile(i*500_000, (i+1)*500_000, coresnaptype.Headers) + createFile(i*500_000, (i+1)*500_000, coresnaptype.Bodies) + createFile(i*500_000, (i+1)*500_000, coresnaptype.Transactions) + } + + missingIdxFile := filepath.Join(dir, snaptype.IdxFileName(1, 500_000, 1_000_000, coresnaptype.Headers.Name())) + err := os.Remove(missingIdxFile) + require.NoError(err) + + cfg := ethconfig.BlocksFreezing{ChainName: networkname.MainnetChainName} + s := NewRoSnapshots(cfg, dir, 0, logger) + defer s.Close() + + require.NoError(s.ReopenFolder()) + idx := s.idxAvailability() + require.Equal(500_000-1, int(idx)) + + require.Equal(1, len(getSeg(s, coresnaptype.Enums.Headers).VisibleSegments)) + require.Equal(3, getSeg(s, coresnaptype.Enums.Headers).DirtySegments.Len()) +}