-
Notifications
You must be signed in to change notification settings - Fork 467
/
Copy pathmigrate.go
258 lines (228 loc) · 7.35 KB
/
migrate.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package migration
import (
"encoding/json"
"fmt"
"io/ioutil"
"math"
"path/filepath"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/venus/fixtures/networks"
"github.com/filecoin-project/venus/pkg/config"
"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/pkg/repo"
"github.com/filecoin-project/venus/venus-shared/types"
logging "github.com/ipfs/go-log/v2"
)
var migrateLog = logging.Logger("data_migrate")
type UpgradeFunc func(string) error
type versionInfo struct {
version uint
upgrade UpgradeFunc
}
var versionMap = []versionInfo{
{version: 3, upgrade: Version3Upgrade},
{version: 4, upgrade: Version4Upgrade},
{version: 5, upgrade: Version5Upgrade},
{version: 6, upgrade: Version6Upgrade},
{version: 7, upgrade: Version7Upgrade},
}
// TryToMigrate used to migrate data(db,config,file,etc) in local repo
func TryToMigrate(repoPath string) error {
localVersion, err := repo.ReadVersion(repoPath)
if err != nil {
return err
}
for _, up := range versionMap {
if up.version > localVersion {
err = up.upgrade(repoPath)
if err != nil {
return err
}
migrateLog.Infof("success to upgrade version %d to version %d", localVersion, up.version)
localVersion = up.version
}
}
return nil
}
// Version3Upgrade 3 for a config filed named apiAuthUrl
func Version3Upgrade(repoPath string) error {
fsrRepo, err := repo.OpenFSRepo(repoPath, 2)
if err != nil {
return err
}
cfg := fsrRepo.Config()
switch cfg.NetworkParams.NetworkType {
case constants.NetworkMainnet:
fallthrough
case constants.Network2k:
fallthrough
case constants.NetworkCalibnet:
fallthrough
case constants.NetworkNerpa:
fallthrough
case constants.NetworkInterop:
cfg.API.VenusAuthURL = ""
}
err = fsrRepo.ReplaceConfig(cfg)
if err != nil {
return err
}
err = fsrRepo.Close()
if err != nil {
return err
}
return repo.WriteVersion(repoPath, 3)
}
func Version4Upgrade(repoPath string) (err error) {
var fsrRepo repo.Repo
if fsrRepo, err = repo.OpenFSRepo(repoPath, 3); err != nil {
return
}
cfg := fsrRepo.Config()
switch cfg.NetworkParams.NetworkType {
case constants.NetworkMainnet:
cfg.NetworkParams.ForkUpgradeParam = config.DefaultForkUpgradeParam
case constants.Network2k:
cfg.NetworkParams.ForkUpgradeParam = networks.Net2k().Network.ForkUpgradeParam
case constants.NetworkCalibnet:
cfg.NetworkParams.ForkUpgradeParam = networks.Calibration().Network.ForkUpgradeParam
case constants.NetworkForce:
cfg.NetworkParams.ForkUpgradeParam = networks.ForceNet().Network.ForkUpgradeParam
case constants.NetworkButterfly:
cfg.NetworkParams.ForkUpgradeParam = networks.ButterflySnapNet().Network.ForkUpgradeParam
default:
return fsrRepo.Close()
}
if err = fsrRepo.ReplaceConfig(cfg); err != nil {
return
}
if err = fsrRepo.Close(); err != nil {
return
}
return repo.WriteVersion(repoPath, 4)
}
//Version5Upgrade
func Version5Upgrade(repoPath string) (err error) {
var fsrRepo repo.Repo
if fsrRepo, err = repo.OpenFSRepo(repoPath, 4); err != nil {
return
}
cfg := fsrRepo.Config()
switch cfg.NetworkParams.NetworkType {
case constants.NetworkMainnet:
cfg.NetworkParams.GenesisNetworkVersion = network.Version0
cfg.NetworkParams.ForkUpgradeParam.UpgradeChocolateHeight = 1231620
case constants.Network2k:
cfg.NetworkParams.GenesisNetworkVersion = network.Version0
cfg.NetworkParams.ForkUpgradeParam.UpgradeChocolateHeight = -17
case constants.NetworkCalibnet:
cfg.NetworkParams.GenesisNetworkVersion = network.Version0
cfg.NetworkParams.ForkUpgradeParam.UpgradeChocolateHeight = 312746
case constants.NetworkForce:
cfg.NetworkParams.GenesisNetworkVersion = network.Version0
cfg.NetworkParams.ForkUpgradeParam.UpgradeChocolateHeight = math.MaxInt32
case constants.NetworkInterop:
cfg.NetworkParams.GenesisNetworkVersion = network.Version0
cfg.NetworkParams.ForkUpgradeParam.UpgradeChocolateHeight = -17
default:
return fsrRepo.Close()
}
if err = fsrRepo.ReplaceConfig(cfg); err != nil {
return
}
if err = fsrRepo.Close(); err != nil {
return
}
return repo.WriteVersion(repoPath, 5)
}
//Version6Upgrade
func Version6Upgrade(repoPath string) (err error) {
var fsrRepo repo.Repo
if fsrRepo, err = repo.OpenFSRepo(repoPath, 5); err != nil {
return
}
cfg := fsrRepo.Config()
switch cfg.NetworkParams.NetworkType {
case constants.NetworkMainnet:
cfg.NetworkParams.GenesisNetworkVersion = network.Version0
cfg.NetworkParams.ForkUpgradeParam.UpgradeChocolateHeight = 1231620
case constants.Network2k:
cfg.NetworkParams.GenesisNetworkVersion = network.Version0
cfg.NetworkParams.ForkUpgradeParam.UpgradeChocolateHeight = -17
case constants.NetworkCalibnet:
cfg.NetworkParams.GenesisNetworkVersion = network.Version0
cfg.NetworkParams.ForkUpgradeParam.UpgradeChocolateHeight = 312746
case constants.NetworkForce:
cfg.NetworkParams.GenesisNetworkVersion = network.Version0
cfg.NetworkParams.ForkUpgradeParam.UpgradeChocolateHeight = -17
case constants.NetworkInterop:
cfg.NetworkParams.GenesisNetworkVersion = network.Version14
cfg.NetworkParams.ForkUpgradeParam.UpgradeChocolateHeight = -17
default:
return fsrRepo.Close()
}
if err = fsrRepo.ReplaceConfig(cfg); err != nil {
return
}
if err = fsrRepo.Close(); err != nil {
return
}
return repo.WriteVersion(repoPath, 6)
}
//Version7Upgrade
func Version7Upgrade(repoPath string) (err error) {
var fsrRepo repo.Repo
if fsrRepo, err = repo.OpenFSRepo(repoPath, 6); err != nil {
return
}
cfg := fsrRepo.Config()
switch cfg.NetworkParams.NetworkType {
case constants.NetworkMainnet:
cfg.NetworkParams.GenesisNetworkVersion = network.Version0
cfg.NetworkParams.ForkUpgradeParam.UpgradeOhSnapHeight = 999999999999
case constants.Network2k:
cfg.NetworkParams.GenesisNetworkVersion = network.Version0
cfg.NetworkParams.ForkUpgradeParam.UpgradeOhSnapHeight = -18
case constants.NetworkCalibnet:
cfg.NetworkParams.GenesisNetworkVersion = network.Version0
cfg.NetworkParams.ForkUpgradeParam.UpgradeOhSnapHeight = 682006
case constants.NetworkButterfly:
cfg.NetworkParams.GenesisNetworkVersion = network.Version14
cfg.NetworkParams.ForkUpgradeParam.UpgradeOhSnapHeight = 240
case constants.NetworkForce:
cfg.NetworkParams.GenesisNetworkVersion = network.Version0
cfg.NetworkParams.ForkUpgradeParam.UpgradeOhSnapHeight = -18
case constants.NetworkInterop:
cfg.NetworkParams.GenesisNetworkVersion = network.Version0
cfg.NetworkParams.ForkUpgradeParam.UpgradeOhSnapHeight = -18
default:
return fsrRepo.Close()
}
// In order to migrate maxfee
type MpoolCfg struct {
MaxFee float64 `json:"maxFee"`
}
type tempCfg struct {
Mpool *MpoolCfg `json:"mpool"`
}
data, err := ioutil.ReadFile(filepath.Join(repoPath, "config.json"))
if err != nil {
migrateLog.Errorf("open config file failed: %v", err)
} else {
// If maxFee value is String(10 FIL), unmarshal failure is expected
// If maxFee value is Number(10000000000000000000), need convert to FIL(10 FIL)
tmpCfg := tempCfg{}
if err := json.Unmarshal(data, &tmpCfg); err == nil {
maxFee := types.MustParseFIL(fmt.Sprintf("%fattofil", tmpCfg.Mpool.MaxFee))
cfg.Mpool.MaxFee = maxFee
migrateLog.Info("convert mpool.maxFee from %v to %s", tmpCfg.Mpool.MaxFee, maxFee.String())
}
}
if err = fsrRepo.ReplaceConfig(cfg); err != nil {
return
}
if err = fsrRepo.Close(); err != nil {
return
}
return repo.WriteVersion(repoPath, 7)
}