diff --git a/erigon-lib/downloader/downloader.go b/erigon-lib/downloader/downloader.go index eae70e7fc..33cb15da2 100644 --- a/erigon-lib/downloader/downloader.go +++ b/erigon-lib/downloader/downloader.go @@ -140,13 +140,13 @@ func New(ctx context.Context, cfg *downloadercfg.Cfg, dirs datadir.Dirs, logger return d, nil } -const prohibitNewDownloadsFileName = "prohibit_new_downloads.lock" +const ProhibitNewDownloadsFileName = "prohibit_new_downloads.lock" // Erigon "download once" - means restart/upgrade/downgrade will not download files (and will be fast) // After "download once" - Erigon will produce and seed new files // Downloader will able: seed new files (already existing on FS), download uncomplete parts of existing files (if Verify found some bad parts) func (d *Downloader) prohibitNewDownloads() error { - fPath := filepath.Join(d.SnapDir(), prohibitNewDownloadsFileName) + fPath := filepath.Join(d.SnapDir(), ProhibitNewDownloadsFileName) f, err := os.Create(fPath) if err != nil { return err @@ -158,7 +158,7 @@ func (d *Downloader) prohibitNewDownloads() error { return nil } func (d *Downloader) newDownloadsAreProhibited() bool { - return dir.FileExist(filepath.Join(d.SnapDir(), prohibitNewDownloadsFileName)) + return dir.FileExist(filepath.Join(d.SnapDir(), ProhibitNewDownloadsFileName)) } func (d *Downloader) MainLoopInBackground(silent bool) { diff --git a/migrations/migrations.go b/migrations/migrations.go index 9f734d1ac..7bcd4824d 100644 --- a/migrations/migrations.go +++ b/migrations/migrations.go @@ -4,9 +4,10 @@ import ( "bytes" "context" "fmt" - "github.com/ledgerwatch/erigon-lib/common" "path/filepath" + "github.com/ledgerwatch/erigon-lib/common" + "github.com/ledgerwatch/erigon-lib/common/datadir" "github.com/ledgerwatch/erigon-lib/kv" "github.com/ledgerwatch/erigon/core/rawdb" @@ -35,6 +36,7 @@ var migrations = map[kv.Label][]Migration{ dbSchemaVersion5, TxsBeginEnd, TxsV3, + ProhibitNewDownloadsLock, }, kv.TxPoolDB: {}, kv.SentryDB: {}, diff --git a/migrations/prohibit_new_downloads_lock.go b/migrations/prohibit_new_downloads_lock.go new file mode 100644 index 000000000..77dcc27fb --- /dev/null +++ b/migrations/prohibit_new_downloads_lock.go @@ -0,0 +1,49 @@ +package migrations + +import ( + "context" + "os" + "path/filepath" + + "github.com/ledgerwatch/erigon-lib/common/datadir" + "github.com/ledgerwatch/erigon-lib/common/dir" + "github.com/ledgerwatch/erigon-lib/downloader" + "github.com/ledgerwatch/erigon-lib/kv" + "github.com/ledgerwatch/erigon/eth/stagedsync/stages" + "github.com/ledgerwatch/log/v3" +) + +var ProhibitNewDownloadsLock = Migration{ + Name: "prohibit_new_downloads_lock", + Up: func(db kv.RwDB, dirs datadir.Dirs, progress []byte, BeforeCommit Callback, logger log.Logger) (err error) { + tx, err := db.BeginRw(context.Background()) + if err != nil { + return err + } + defer tx.Rollback() + + snapshotsStageProgress, err := stages.GetStageProgress(tx, stages.Snapshots) + if err != nil { + return err + } + if snapshotsStageProgress > 0 { + fPath := filepath.Join(dirs.Snap, downloader.ProhibitNewDownloadsFileName) + if !dir.FileExist(fPath) { + f, err := os.Create(fPath) + if err != nil { + return err + } + defer f.Close() + if err := f.Sync(); err != nil { + return err + } + } + } + + // This migration is no-op, but it forces the migration mechanism to apply it and thus write the DB schema version info + if err := BeforeCommit(tx, nil, true); err != nil { + return err + } + return tx.Commit() + }, +}