Skip to content

Commit 2788ee3

Browse files
committed
wip: kubo 0.18 config migrations
ipfs/kubo#9475 ipfs/kubo#9467 ipfs/kubo#9326
1 parent 0786320 commit 2788ee3

File tree

2 files changed

+278
-0
lines changed

2 files changed

+278
-0
lines changed

fs-repo-9-to-10/migration/config_conv.go

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,170 @@ func convert(in io.Reader, out io.Writer, convBootstrap convArray, convAddresses
8383
return err
8484
}
8585

86+
// Convert Routing.Type to implicit default
87+
// https://github.com/ipfs/kubo/pull/9475
88+
func convertRouting(confMap map[string]interface{}) {
89+
routing, _ := confMap["Routing"].(map[string]interface{})
90+
if routing == nil {
91+
log.Log("No Routing field in config, skipping")
92+
return
93+
}
94+
95+
rType, ok := routing["Type"].(string)
96+
if !ok {
97+
log.Log("No Routing.Type field in config, skipping")
98+
return
99+
}
100+
if rType == "dht" || rType == "" {
101+
delete(routing, "Type")
102+
} else {
103+
log.Log("Routing.Type settings is different than the old default, skipping")
104+
}
105+
}
106+
func undoRouting(confMap map[string]interface{}) {
107+
routing, ok := confMap["Routing"].(map[string]interface{})
108+
if routing == nil {
109+
log.Log("No Routing field in config, skipping")
110+
return
111+
}
112+
113+
t, ok := routing["Type"].(string)
114+
if !ok || t == "" {
115+
routing["Type"] = "dht"
116+
} else {
117+
log.Log("Custom Routing.Type settings, skipping")
118+
}
119+
}
120+
121+
// Convert Reprovider to implicit defaults
122+
// https://github.com/ipfs/kubo/pull/9326
123+
func convertReprovider(confMap map[string]interface{}) {
124+
reprovider, _ := confMap["Reprovider"].(map[string]interface{})
125+
if reprovider == nil {
126+
log.Log("No Reprovider field in config, skipping")
127+
return
128+
}
129+
130+
interval, ok := reprovider["Interval"].(string)
131+
if !ok {
132+
log.Log("No Reprovider.Interval field in config, skipping")
133+
return
134+
}
135+
if interval == "12h" {
136+
delete(reprovider, "Interval")
137+
} else {
138+
log.Log("Reprovider.Interval settings is different than the old default, skipping")
139+
}
140+
141+
strategy, ok := reprovider["Strategy"].(string)
142+
if !ok {
143+
log.Log("No Reprovider.Strategy field in config, skipping")
144+
return
145+
}
146+
if strategy == "all" {
147+
delete(reprovider, "Strategy")
148+
} else {
149+
log.Log("Reprovider.Strategy settings is different than the old default, skipping")
150+
}
151+
}
152+
func undoReprovider(confMap map[string]interface{}) {
153+
reprovider, _ := confMap["Reprovider"].(map[string]interface{})
154+
if reprovider == nil {
155+
log.Log("No Reprovider field in config, skipping")
156+
return
157+
}
158+
159+
_, ok := reprovider["Interval"].(string)
160+
if !ok {
161+
reprovider["Interval"] = "12h"
162+
} else {
163+
log.Log("Custom Reprovider.Interval settings present, skipping")
164+
}
165+
166+
_, ok = reprovider["Strategy"].(string)
167+
if !ok {
168+
reprovider["Strategy"] = "all"
169+
} else {
170+
log.Log("Custom Reprovider.Strategy settings present, skipping")
171+
}
172+
}
173+
174+
// Convert Swarm.ConnMgr to implicit defaults
175+
// https://github.com/ipfs/kubo/pull/9467
176+
func convertConnMgr(confMap map[string]interface{}) {
177+
swarm, _ := confMap["Swarm"].(map[string]interface{})
178+
if swarm == nil {
179+
log.Log("No Swarm field in config, skipping")
180+
return
181+
}
182+
connmgr, _ := swarm["ConnMgr"].(map[string]interface{})
183+
if connmgr == nil {
184+
log.Log("No Swarm.ConnMgr field in config, skipping")
185+
return
186+
}
187+
cmType, ok := connmgr["Type"].(string)
188+
if !ok {
189+
log.Log("No Swarm.ConnMgr.Type field in config, skipping")
190+
return
191+
}
192+
cmLowWater, ok := connmgr["LowWater"].(float64)
193+
if !ok {
194+
log.Log("No Swarm.ConnMgr.LowWater field in config, skipping")
195+
return
196+
}
197+
cmHighWater, ok := connmgr["HighWater"].(float64)
198+
if !ok {
199+
log.Log("No Swarm.ConnMgr.HighWater field in config, skipping")
200+
return
201+
}
202+
cmGrace, ok := connmgr["GracePeriod"].(string)
203+
if !ok {
204+
log.Log("No Swarm.ConnMgr.GracePeriod field in config, skipping")
205+
return
206+
}
207+
208+
if cmType == "basic" &&
209+
int(cmLowWater) == 600 &&
210+
int(cmHighWater) == 900 &&
211+
cmGrace == "20s" {
212+
// remove defaults from config, switch user to implicit ones
213+
delete(connmgr, "Type")
214+
delete(connmgr, "LowWater")
215+
delete(connmgr, "HighWater")
216+
delete(connmgr, "GracePeriod")
217+
} else {
218+
log.Log("Swarm.ConnMgr settings are different than the old defaults, skipping")
219+
}
220+
}
221+
func undoConnMgr(confMap map[string]interface{}) {
222+
swarm, _ := confMap["Swarm"].(map[string]interface{})
223+
if swarm == nil {
224+
log.Log("No Swarm field in config, skipping")
225+
return
226+
}
227+
connmgr, _ := swarm["ConnMgr"].(map[string]interface{})
228+
if connmgr == nil {
229+
log.Log("No Swarm.ConnMgr field in config, skipping")
230+
return
231+
}
232+
_, ok := connmgr["Type"].(string)
233+
if !ok {
234+
connmgr["Type"] = "basic"
235+
}
236+
_, ok = connmgr["LowWater"].(float64)
237+
if !ok {
238+
connmgr["LowWater"] = 600
239+
}
240+
_, ok = connmgr["HighWater"].(float64)
241+
if !ok {
242+
connmgr["HighWater"] = 900
243+
}
244+
_, ok = connmgr["GracePeriod"].(string)
245+
if !ok {
246+
connmgr["GracePeriod"] = "20s"
247+
}
248+
}
249+
86250
// Convert Bootstrap addresses to/from QUIC
87251
func convertBootstrap(confMap map[string]interface{}, conv convArray) {
88252
bootstrapi, _ := confMap["Bootstrap"].([]interface{})
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package mg9
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io/ioutil"
7+
"strings"
8+
"testing"
9+
)
10+
11+
var beforeConfig = `{
12+
"Reprovider": {
13+
"Interval": "12h",
14+
"Strategy": "all"
15+
},
16+
"Routing": {
17+
"Methods": {},
18+
"Routers": {},
19+
"Type": "dht"
20+
},
21+
"Swarm": {
22+
"ConnMgr": {
23+
"GracePeriod": "20s",
24+
"HighWater": 900,
25+
"LowWater": 600,
26+
"Type": "basic"
27+
}
28+
}
29+
}`
30+
31+
var afterConfig = `{
32+
"Reprovider": {},
33+
"Routing": {
34+
"Methods": {},
35+
"Routers": {}
36+
},
37+
"Swarm": {
38+
"ConnMgr": {}
39+
}
40+
}`
41+
42+
func TestKubo18Migration(t *testing.T) {
43+
out := new(bytes.Buffer)
44+
45+
data, err := ioutil.ReadAll(strings.NewReader(beforeConfig))
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
50+
confMap := make(map[string]interface{})
51+
if err = json.Unmarshal(data, &confMap); err != nil {
52+
t.Fatal(err)
53+
}
54+
55+
// Kubo 0.18
56+
convertRouting(confMap)
57+
convertReprovider(confMap)
58+
convertConnMgr(confMap)
59+
60+
fixed, err := json.MarshalIndent(confMap, "", " ")
61+
if err != nil {
62+
t.Fatal(err)
63+
}
64+
65+
if _, err := out.Write(fixed); err != nil {
66+
t.Fatal(err)
67+
}
68+
_, err = out.Write([]byte("\n"))
69+
if err != nil {
70+
t.Fatal(err)
71+
}
72+
73+
forward := out.String()
74+
if noSpace(forward) != noSpace(afterConfig) {
75+
t.Fatalf("Mismatch\nConversion produced:\n%s\nExpected:\n%s\n", forward, afterConfig)
76+
}
77+
}
78+
79+
func TestKubo18MigrationReversal(t *testing.T) {
80+
out := new(bytes.Buffer)
81+
82+
data, err := ioutil.ReadAll(strings.NewReader(afterConfig))
83+
if err != nil {
84+
t.Fatal(err)
85+
}
86+
87+
confMap := make(map[string]interface{})
88+
if err = json.Unmarshal(data, &confMap); err != nil {
89+
t.Fatal(err)
90+
}
91+
92+
// Kubo 0.18
93+
undoRouting(confMap)
94+
undoReprovider(confMap)
95+
undoConnMgr(confMap)
96+
97+
fixed, err := json.MarshalIndent(confMap, "", " ")
98+
if err != nil {
99+
t.Fatal(err)
100+
}
101+
102+
if _, err := out.Write(fixed); err != nil {
103+
t.Fatal(err)
104+
}
105+
_, err = out.Write([]byte("\n"))
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
110+
undone := out.String()
111+
if noSpace(undone) != noSpace(beforeConfig) {
112+
t.Fatalf("Mismatch\nConversion produced:\n%s\nExpected:\n%s\n", undone, beforeConfig)
113+
}
114+
}

0 commit comments

Comments
 (0)