Skip to content

Commit 95414ba

Browse files
committed
fix: dont do anything on reverse migrate
1 parent bfce67c commit 95414ba

File tree

4 files changed

+76
-156
lines changed

4 files changed

+76
-156
lines changed

ipfs-9-to-10/migration/config_conv.go

Lines changed: 46 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ var (
2121
// configuration from one version to another
2222
type convArray func([]string) []string
2323

24+
// convAddrs does an inplace conversion of the swarm, announce
25+
// and noAnnounce arrays of strings from one version to another
26+
type convAddrs func([]string, []string, []string) ([]string, []string, []string)
27+
2428
// convertFile converts a config file from one version to another
25-
func convertFile(path string, enableQuic bool, convBootstrap convArray, convSwarm convArray) error {
29+
func convertFile(path string, convBootstrap convArray, convAddresses convAddrs) error {
2630
in, err := os.Open(path)
2731
if err != nil {
2832
return err
@@ -35,7 +39,7 @@ func convertFile(path string, enableQuic bool, convBootstrap convArray, convSwar
3539
return err
3640
}
3741

38-
err = convert(in, out, enableQuic, convBootstrap, convSwarm)
42+
err = convert(in, out, convBootstrap, convAddresses)
3943

4044
in.Close()
4145

@@ -51,7 +55,7 @@ func convertFile(path string, enableQuic bool, convBootstrap convArray, convSwar
5155
}
5256

5357
// convert converts the config from one version to another
54-
func convert(in io.Reader, out io.Writer, enableQuic bool, convBootstrap convArray, convSwarm convArray) error {
58+
func convert(in io.Reader, out io.Writer, convBootstrap convArray, convAddresses convAddrs) error {
5559
data, err := ioutil.ReadAll(in)
5660
if err != nil {
5761
return err
@@ -64,19 +68,8 @@ func convert(in io.Reader, out io.Writer, enableQuic bool, convBootstrap convArr
6468
// Convert bootstrap config
6569
convertBootstrap(confMap, convBootstrap)
6670

67-
// Convert swarm config
68-
if enableQuic {
69-
// When enabling quic
70-
// - Remove experimental option from config
71-
confEnabled, ok := removeExperimentalQuic(confMap)
72-
// - Only convert swarm config if experimental quic option was present
73-
// and not enabled
74-
if ok && !confEnabled {
75-
convertSwarm(confMap, convSwarm)
76-
}
77-
} else {
78-
convertSwarm(confMap, convSwarm)
79-
}
71+
// Convert addresses config
72+
convertAddresses(confMap, convAddresses)
8073

8174
fixed, err := json.MarshalIndent(confMap, "", " ")
8275
if err != nil {
@@ -90,57 +83,45 @@ func convert(in io.Reader, out io.Writer, enableQuic bool, convBootstrap convArr
9083
return err
9184
}
9285

93-
// Remove Experimental.QUIC flag
94-
func removeExperimentalQuic(confMap map[string]interface{}) (bool, bool) {
95-
confExpi := confMap["Experimental"].(map[string]interface{})
96-
if confExpi == nil {
97-
return false, false
98-
}
99-
enabledi, ok := confExpi["QUIC"]
100-
if !ok {
101-
return false, false
102-
}
103-
104-
enabled, ok := enabledi.(bool)
105-
106-
delete(confExpi, "QUIC")
107-
108-
return enabled, ok
109-
}
110-
11186
// Convert Bootstrap addresses to/from QUIC
11287
func convertBootstrap(confMap map[string]interface{}, conv convArray) {
11388
bootstrapi, _ := confMap["Bootstrap"].([]interface{})
11489
if bootstrapi == nil {
11590
log.Log("No Bootstrap field in config, skipping")
11691
return
11792
}
118-
bootstrap := make([]string, len(bootstrapi))
119-
for i := range bootstrapi {
120-
bootstrap[i] = bootstrapi[i].(string)
121-
}
122-
confMap["Bootstrap"] = conv(bootstrap)
93+
confMap["Bootstrap"] = conv(toStringArray(bootstrapi))
12394
}
12495

125-
// Convert Addresses.Swarm to/from QUIC
126-
func convertSwarm(confMap map[string]interface{}, conv convArray) {
96+
// Convert Addresses.Swarm, Addresses.Announce, Addresses.NoAnnounce to/from QUIC
97+
func convertAddresses(confMap map[string]interface{}, conv convAddrs) {
12798
addressesi, _ := confMap["Addresses"].(map[string]interface{})
12899
if addressesi == nil {
129100
log.Log("Addresses field missing or of the wrong type")
130101
return
131102
}
132103

133-
swarmi, _ := addressesi["Swarm"].([]interface{})
134-
if swarmi == nil {
135-
log.Log("Addresses.Swarm field missing or of the wrong type")
136-
return
104+
swarm := toStringArray(addressesi["Swarm"])
105+
announce := toStringArray(addressesi["Announce"])
106+
noAnnounce := toStringArray(addressesi["NoAnnounce"])
107+
108+
s, a, na := conv(swarm, announce, noAnnounce)
109+
addressesi["Swarm"] = s
110+
addressesi["Announce"] = a
111+
addressesi["NoAnnounce"] = na
112+
}
113+
114+
func toStringArray(el interface{}) []string {
115+
listi, _ := el.([]interface{})
116+
if listi == nil {
117+
return []string{}
137118
}
138119

139-
swarm := make([]string, len(swarmi))
140-
for i := range swarmi {
141-
swarm[i] = swarmi[i].(string)
120+
list := make([]string, len(listi))
121+
for i := range listi {
122+
list[i] = listi[i].(string)
142123
}
143-
addressesi["Swarm"] = conv(swarm)
124+
return list
144125
}
145126

146127
// Add QUIC Bootstrap address
@@ -166,48 +147,34 @@ func ver9to10Bootstrap(bootstrap []string) []string {
166147
return res
167148
}
168149

169-
func ver10to9Bootstrap(bootstrap []string) []string {
170-
// No need to remove the QUIC bootstrapper, just leave things as they are
171-
return bootstrap
150+
// For each TCP address, add a QUIC address
151+
func ver9to10Addresses(swarm, announce, noAnnounce []string) ([]string, []string, []string) {
152+
for _, addr := range append(swarm, append(announce, noAnnounce...)...) {
153+
// If the old configuration already has a quic address in it, assume
154+
// the user has already set up their addresses for quic and leave
155+
// things as they are
156+
if strings.Contains(addr, "/udp/quic") {
157+
return swarm, announce, noAnnounce
158+
}
159+
}
160+
161+
return addQuic(swarm), addQuic(announce), addQuic(noAnnounce)
172162
}
173163

174164
var tcpRegexp = regexp.MustCompile(`/tcp/([0-9]+)`)
175165

176-
// For each TCP listener, add a QUIC listener
177-
func ver9to10Swarm(swarm []string) []string {
178-
res := make([]string, 0, len(swarm)*2)
179-
for _, addr := range swarm {
166+
func addQuic(addrs []string) []string {
167+
res := make([]string, 0, len(addrs)*2)
168+
for _, addr := range addrs {
180169
res = append(res, addr)
181-
182-
// If the old configuration already has a quic address in it, assume
183-
// the user has already set up their swarm addresses for quic and leave
184-
// things as they are
185-
if strings.Contains(addr, "/udp/quic") {
186-
return swarm
187-
}
188170
}
189171

190172
// For each tcp address, add a corresponding quic address
191-
for _, addr := range swarm {
173+
for _, addr := range addrs {
192174
if tcpRegexp.MatchString(addr) {
193175
res = append(res, tcpRegexp.ReplaceAllString(addr, `/udp/$1/quic`))
194176
}
195177
}
196178

197179
return res
198180
}
199-
200-
var quicRegexp = regexp.MustCompile(`/udp/[0-9]+/quic`)
201-
202-
// Remove QUIC listeners
203-
func ver10to9Swarm(swarm []string) []string {
204-
// Remove quic addresses
205-
res := make([]string, 0, len(swarm))
206-
for _, addr := range swarm {
207-
if !quicRegexp.MatchString(addr) {
208-
res = append(res, addr)
209-
}
210-
}
211-
212-
return res
213-
}

ipfs-9-to-10/migration/conv_test.go

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@ import (
99

1010
var config = `{
1111
"Addresses": {
12+
"Announce": [
13+
"/ip4/1.2.3.4/tcp/1"
14+
],
15+
"NoAnnounce": [
16+
"/ip4/5.6.7.8/tcp/2"
17+
],
1218
"Swarm": [
13-
"/ip4/0.0.0.0/tcp/0"
19+
"/ip4/0.0.0.0/tcp/0",
20+
"/ip4/1.2.3.4/tcp/1",
21+
"/ip4/5.6.7.8/tcp/2"
1422
]
1523
},
1624
"Bootstrap": [
@@ -19,17 +27,26 @@ var config = `{
1927
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
2028
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
2129
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa"
22-
],
23-
"Experimental": {
24-
"QUIC": false
25-
}
30+
]
2631
}`
2732

2833
var expForwardConfig = `{
2934
"Addresses": {
35+
"Announce": [
36+
"/ip4/1.2.3.4/tcp/1",
37+
"/ip4/1.2.3.4/udp/1/quic"
38+
],
39+
"NoAnnounce": [
40+
"/ip4/5.6.7.8/tcp/2",
41+
"/ip4/5.6.7.8/udp/2/quic"
42+
],
3043
"Swarm": [
3144
"/ip4/0.0.0.0/tcp/0",
32-
"/ip4/0.0.0.0/udp/0/quic"
45+
"/ip4/1.2.3.4/tcp/1",
46+
"/ip4/5.6.7.8/tcp/2",
47+
"/ip4/0.0.0.0/udp/0/quic",
48+
"/ip4/1.2.3.4/udp/1/quic",
49+
"/ip4/5.6.7.8/udp/2/quic"
3350
]
3451
},
3552
"Bootstrap": [
@@ -39,30 +56,12 @@ var expForwardConfig = `{
3956
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
4057
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
4158
"/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ"
42-
],
43-
"Experimental": {}
44-
}`
45-
46-
var expRevertedConfig = `{
47-
"Addresses": {
48-
"Swarm": [
49-
"/ip4/0.0.0.0/tcp/0"
50-
]
51-
},
52-
"Bootstrap": [
53-
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
54-
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
55-
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
56-
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
57-
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
58-
"/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ"
59-
],
60-
"Experimental": {}
59+
]
6160
}`
6261

6362
func TestConversion(t *testing.T) {
6463
conf9to10 := new(bytes.Buffer)
65-
err := convert(strings.NewReader(config), conf9to10, true, ver9to10Bootstrap, ver9to10Swarm)
64+
err := convert(strings.NewReader(config), conf9to10, ver9to10Bootstrap, ver9to10Addresses)
6665
if err != nil {
6766
t.Fatal(err)
6867
}
@@ -71,16 +70,6 @@ func TestConversion(t *testing.T) {
7170
if noSpace(forward) != noSpace(expForwardConfig) {
7271
t.Fatalf("Mismatch\nConversion produced:\n%s\nExpected:\n%s\n", forward, expForwardConfig)
7372
}
74-
75-
conf10to9 := new(bytes.Buffer)
76-
err = convert(strings.NewReader(conf9to10.String()), conf10to9, false, ver10to9Bootstrap, ver10to9Swarm)
77-
if err != nil {
78-
t.Fatal(err)
79-
}
80-
reverted := conf10to9.String()
81-
if noSpace(reverted) != noSpace(expRevertedConfig) {
82-
t.Fatalf("Mismatch\nConversion produced:\n%s\nExpected:\n%s\n", reverted, expRevertedConfig)
83-
}
8473
}
8574

8675
var whitespaceRe = regexp.MustCompile(`\s`)

ipfs-9-to-10/migration/migration.go

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (m Migration) Apply(opts migrate.Options) error {
4444
log.Log("> Upgrading config to new format")
4545

4646
path := filepath.Join(opts.Path, "config")
47-
if err := convertFile(path, true, ver9to10Bootstrap, ver9to10Swarm); err != nil {
47+
if err := convertFile(path, ver9to10Bootstrap, ver9to10Addresses); err != nil {
4848
return err
4949
}
5050

@@ -88,32 +88,11 @@ func (m Migration) Revert(opts migrate.Options) error {
8888
return err
8989
}
9090

91-
phasefile := filepath.Join(opts.Path, "revert-phase")
92-
path := filepath.Join(opts.Path, "config")
93-
94-
phase, err := readPhase(phasefile)
95-
if err != nil {
96-
return fmt.Errorf("reading revert phase: %s", err)
91+
if err := repo.WriteVersion("9"); err != nil {
92+
return err
9793
}
98-
99-
defer os.Remove(phasefile)
100-
for ; phase < 2; phase++ {
101-
switch phase {
102-
case 0:
103-
if err := convertFile(path, false, ver10to9Bootstrap, ver10to9Swarm); err != nil {
104-
return err
105-
}
106-
case 1:
107-
if err := repo.WriteVersion("9"); err != nil {
108-
return err
109-
}
110-
if opts.Verbose {
111-
fmt.Println("lowered version number to 9")
112-
}
113-
}
114-
if err := writePhase(phasefile, phase+1); err != nil {
115-
return err
116-
}
94+
if opts.Verbose {
95+
fmt.Println("lowered version number to 9")
11796
}
11897

11998
return nil

sharness/t0130-migration-9-to-10.sh

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ test_expect_success "remember old swarm config" '
3232
ipfs config Addresses.Swarm | sort > swarm_old
3333
'
3434

35-
test_expect_success "remember old experimental config" '
36-
ipfs config Experimental | sort > experimental_old
37-
'
38-
3935
# no need to launch daemon as this works offline
4036

4137
test_expect_success "run migration 9 to 10" '
@@ -53,12 +49,6 @@ test_expect_success "migration added quic listener" '
5349
ipfs config Addresses.Swarm | grep "quic"
5450
'
5551

56-
test_expect_success "migration removed experimental quic config" '
57-
grep -v "QUIC\|{\|}" experimental_old | sort > experimental_old_no_quic &&
58-
ipfs config Experimental | grep -v "{\|}" | sort > experimental_new &&
59-
test_cmp experimental_old_no_quic experimental_new
60-
'
61-
6252
test_expect_success "revert migration 10 to 9 succeeds" '
6353
ipfs-9-to-10 -revert -verbose -path="$IPFS_PATH"
6454
'
@@ -69,9 +59,4 @@ test_expect_success "does not revert bootstrap address" '
6959
ipfs config Bootstrap | grep "/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ"
7060
'
7161

72-
test_expect_success "expect reverts swarm config" '
73-
ipfs config Addresses.Swarm | sort > swarm_reverted &&
74-
test_cmp swarm_reverted swarm_old
75-
'
76-
7762
test_done

0 commit comments

Comments
 (0)