-
Notifications
You must be signed in to change notification settings - Fork 14
/
migrations.go
270 lines (249 loc) · 7.74 KB
/
migrations.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
259
260
261
262
263
264
265
266
267
268
269
270
package config
import (
"reflect"
"strings"
"github.com/libp2p/go-libp2p-core/peer"
)
func migrate_1_Services(cfg *Config) bool {
emptyServices := Services{}
if reflect.DeepEqual(cfg.Services, emptyServices) {
cfg.Services = DefaultServicesConfig()
return true
}
if len(cfg.Services.EscrowPubKeys) == 0 || len(cfg.Services.GuardPubKeys) == 0 {
cfg.Services = DefaultServicesConfig()
return true
}
return false
}
func migrate_2_StatusUrl(cfg *Config) bool {
if strings.Contains(cfg.Services.StatusServerDomain, "db.btfs.io") {
ds := DefaultServicesConfig()
cfg.Services.StatusServerDomain = ds.StatusServerDomain
return true
}
return false
}
func migrate_3_StorageSettings(cfg *Config, fromV0, inited, hasHval bool) bool {
// 1) Enable host
// a) Upgrade from 0.x.x -> 1.x.x and has hval (bt client)
// b) New profile and has hval (bt client)
// 2) Enable renter if it is a new upgrade from 0.x.x version
if fromV0 {
Profiles["storage-client"].Transform(cfg)
}
if hasHval && (fromV0 || inited) {
Profiles["storage-host"].Transform(cfg)
}
return true
}
func migrate_4_SwarmKey(cfg *Config) bool {
if cfg.Swarm.SwarmKey == "" {
cfg.Swarm.SwarmKey = DefaultSwarmKey
return true
}
return false
}
// checks to see if the current config contains known obsolete ip addresses
// for bootstrap nodes.
// Replaces all bootstrap nodes with default values if so.
func migrate_5_Bootstrap_node(cfg *Config) bool {
// Only migrate on prod settings
if cfg.Swarm.SwarmKey != DefaultSwarmKey {
return false
}
obns := []string{
"3.120.224.94",
"18.196.49.234",
"/btfs/", // migrate to ipfs 0.5.0+ protocol
}
peers, _ := DefaultBootstrapPeers()
return doMigrateNodes(cfg, obns, peers)
}
func migrate_6_EnableAutoRelay(cfg *Config) bool {
if cfg.Swarm.EnableAutoRelay != DefaultEnableAutoRelay {
cfg.Swarm.EnableAutoRelay = DefaultEnableAutoRelay
return true
}
return false
}
// checks to see if the current config contains known obsolete ip addresses
// for testnet bootstrap nodes.
// Replaces all testnet bootstrap nodes with default values if so.
func migrate_7_Testnet_Bootstrap_node(cfg *Config) bool {
// Only migrate on testnet settings
if cfg.Swarm.SwarmKey != DefaultTestnetSwarmKey {
return false
}
obns := []string{
"52.57.56.230",
"13.59.69.165/tcp/43113",
"13.229.73.63/tcp/38869",
"3.126.51.74/tcp/38131",
"/btfs/", // migrate to ipfs 0.5.0+ protocol
}
peers, _ := DefaultTestnetBootstrapPeers()
return doMigrateNodes(cfg, obns, peers)
}
func doMigrateNodes(cfg *Config, obsoleteBootstrapNodes []string, defaultPeers []peer.AddrInfo) bool {
currentBootstrapNodeList := cfg.Bootstrap
for _, obsoleteNode := range obsoleteBootstrapNodes {
for _, bootstrapNode := range currentBootstrapNodeList {
if strings.Contains(bootstrapNode, obsoleteNode) {
cfg.SetBootstrapPeers(defaultPeers)
return true
}
}
}
return false
}
func migrate_8_AnnounceDefault(cfg *Config, beforeV1B2 bool) bool {
if beforeV1B2 {
cfg.Addresses.Announce = []string{}
return true
}
return false
}
func migrate_9_WalletDomain(cfg *Config) bool {
if strings.Contains(cfg.Services.EscrowDomain, "dev") || strings.Contains(cfg.Services.EscrowDomain, "staging") {
if len(cfg.Services.ExchangeDomain) == 0 {
ds := DefaultServicesConfigTestnet()
cfg.Services.ExchangeDomain = ds.ExchangeDomain
cfg.Services.SolidityDomain = ds.SolidityDomain
return true
}
} else {
if len(cfg.Services.ExchangeDomain) == 0 {
ds := DefaultServicesConfig()
cfg.Services.ExchangeDomain = ds.ExchangeDomain
cfg.Services.SolidityDomain = ds.SolidityDomain
return true
}
}
return false
}
// check if HTTPHeaders, API and Gateway config are fully configured, then clean HTTPHeaders
func migrate_10_CleanAPIHTTPHeaders(cfg *Config) bool {
condCount := 3
httpHeaderFullConfig := map[string][]string{
"Access-Control-Allow-Origin": {"*"},
"Access-Control-Allow-Methods": {"PUT", "GET", "POST", "OPTIONS"},
"Access-Control-Allow-Credentials": {"true"},
}
if reflect.DeepEqual(cfg.API.HTTPHeaders, httpHeaderFullConfig) {
condCount--
}
addressesAPIFullConfig := Strings{"/ip4/0.0.0.0/tcp/5001"}
if reflect.DeepEqual(cfg.Addresses.API, addressesAPIFullConfig) {
condCount--
}
addressesGatewayFullConfig := Strings{"/ip4/0.0.0.0/tcp/8080"}
if reflect.DeepEqual(cfg.Addresses.Gateway, addressesGatewayFullConfig) {
condCount--
}
if condCount == 0 {
// clean httpheaders configuration
cfg.API.HTTPHeaders = make(map[string][]string)
return true
}
return false
}
func migrate_11_ExchangeDomain(cfg *Config) bool {
// migrate staging domain -> staging
if strings.Contains(cfg.Services.EscrowDomain, "staging") &&
strings.Contains(cfg.Services.ExchangeDomain, "dev") {
ds := DefaultServicesConfigTestnet()
cfg.Services.ExchangeDomain = ds.ExchangeDomain
return true
}
return false
}
func migrate_12_FullnodeDomain(cfg *Config) bool {
if strings.Contains(cfg.Services.EscrowDomain, "dev") || strings.Contains(cfg.Services.EscrowDomain, "staging") {
if len(cfg.Services.FullnodeDomain) == 0 {
ds := DefaultServicesConfigTestnet()
cfg.Services.FullnodeDomain = ds.FullnodeDomain
return true
}
} else {
if len(cfg.Services.FullnodeDomain) == 0 {
ds := DefaultServicesConfig()
cfg.Services.FullnodeDomain = ds.FullnodeDomain
return true
}
}
return false
}
func migrate_13_HostContractManager(cfg *Config) bool {
if cfg.UI.Host.ContractManager == nil {
cfg.UI.Host.ContractManager = &ContractManager{
LowWater: 100,
HighWater: 300,
Threshold: 10 * 1000 * 1000,
}
return true
}
return false
}
func migrate_14_TestnetBootstrapNodes(cfg *Config) bool {
// Only migrate on testnet settings
if cfg.Swarm.SwarmKey != DefaultTestnetSwarmKey {
return false
}
obns := []string{
"13.59.69.165",
"13.229.73.63",
"3.126.51.74",
}
peers, _ := DefaultTestnetBootstrapPeers()
return doMigrateNodes(cfg, obns, peers)
}
func migrate_15_MissingRemoteAPI(cfg *Config) bool {
if len(cfg.Addresses.RemoteAPI) == 0 {
cfg.Addresses.RemoteAPI = Strings{"/ip4/0.0.0.0/tcp/5101"}
return true
}
return false
}
func migrate_16_TrongridDomain(cfg *Config) bool {
if strings.Contains(cfg.Services.EscrowDomain, "dev") || strings.Contains(cfg.Services.EscrowDomain, "staging") {
if len(cfg.Services.TrongridDomain) == 0 {
ds := DefaultServicesConfigTestnet()
cfg.Services.TrongridDomain = ds.TrongridDomain
return true
}
} else {
if len(cfg.Services.TrongridDomain) == 0 {
ds := DefaultServicesConfig()
cfg.Services.TrongridDomain = ds.TrongridDomain
return true
}
}
return false
}
// MigrateConfig migrates config options to the latest known version
// It may correct incompatible configs as well
// inited = just initialized in the same call
// hasHval = passed in Hval in the same call
func MigrateConfig(cfg *Config, inited, hasHval bool) bool {
updated := false
upToV1 := migrate_1_Services(cfg)
updated = upToV1 || updated
updated = migrate_2_StatusUrl(cfg) || updated
updated = migrate_3_StorageSettings(cfg, upToV1, inited, hasHval) || updated
upToV1B2 := migrate_4_SwarmKey(cfg)
updated = upToV1B2 || updated
updated = migrate_5_Bootstrap_node(cfg) || updated
updated = migrate_6_EnableAutoRelay(cfg) || updated
updated = migrate_7_Testnet_Bootstrap_node(cfg) || updated
updated = migrate_8_AnnounceDefault(cfg, upToV1B2) || updated
updated = migrate_9_WalletDomain(cfg) || updated
updated = migrate_10_CleanAPIHTTPHeaders(cfg) || updated
updated = migrate_11_ExchangeDomain(cfg) || updated
updated = migrate_12_FullnodeDomain(cfg) || updated
updated = migrate_13_HostContractManager(cfg) || updated
updated = migrate_14_TestnetBootstrapNodes(cfg) || updated
updated = migrate_15_MissingRemoteAPI(cfg) || updated
updated = migrate_16_TrongridDomain(cfg) || updated
return updated
}