-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathprohibit_new_downloads2.go
97 lines (86 loc) · 2.81 KB
/
prohibit_new_downloads2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.
package migrations
import (
"context"
"encoding/json"
"io/fs"
"os"
"path/filepath"
"github.com/erigontech/erigon-lib/common/datadir"
"github.com/erigontech/erigon-lib/common/dir"
"github.com/erigontech/erigon-lib/downloader"
"github.com/erigontech/erigon-lib/downloader/snaptype"
"github.com/erigontech/erigon-lib/kv"
"github.com/erigontech/erigon-lib/log/v3"
coresnaptype "github.com/erigontech/erigon/core/snaptype"
"github.com/erigontech/erigon/polygon/heimdall"
)
// Switch to the second version of download.lock.
var ProhibitNewDownloadsLock2 = Migration{
Name: "prohibit_new_downloads_lock2",
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()
fPath := filepath.Join(dirs.Snap, downloader.ProhibitNewDownloadsFileName)
exists, err := dir.FileExist(fPath)
if err != nil {
return err
}
if !exists {
if err := BeforeCommit(tx, nil, true); err != nil {
return err
}
return tx.Commit()
}
content, err := os.ReadFile(fPath)
if err != nil {
return err
}
if len(content) == 0 { // old format, need to change to all snaptypes except blob sidecars
locked := []string{}
for _, t := range coresnaptype.BlockSnapshotTypes {
locked = append(locked, t.Name())
}
for _, t := range coresnaptype.E3StateTypes {
locked = append(locked, t.Name())
}
for _, t := range heimdall.SnapshotTypes() {
locked = append(locked, t.Name())
}
for _, t := range snaptype.CaplinSnapshotTypes {
if t.Name() != snaptype.BlobSidecars.Name() {
locked = append(locked, t.Name())
}
}
newContent, err := json.Marshal(locked)
if err != nil {
return err
}
if err := os.WriteFile(fPath, newContent, fs.FileMode(os.O_TRUNC|os.O_WRONLY)); 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()
},
}