Skip to content

Commit 7376e6c

Browse files
committed
feat: update bootstrappers
1 parent 46ecb9d commit 7376e6c

File tree

7 files changed

+470
-2
lines changed

7 files changed

+470
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ install:
55
test: test_go sharness
66

77
test_go:
8-
go test ./ipfs-5-to-6/... # go test ./... fails see #66
8+
go test ./ipfs-5-to-6/... ./ipfs-7-to-8/... # go test ./... fails see #66
99

1010
sharness:
1111
make -C sharness

ipfs-7-to-8/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import (
4+
migrate "github.com/ipfs/fs-repo-migrations/go-migrate"
5+
mg7 "github.com/ipfs/fs-repo-migrations/ipfs-7-to-8/migration"
6+
)
7+
8+
func main() {
9+
m := mg7.Migration{}
10+
migrate.Main(&m)
11+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package mg7
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"io/ioutil"
8+
"os"
9+
"strings"
10+
11+
"github.com/ipfs/go-cid"
12+
)
13+
14+
var (
15+
dnsAddr = "bootstrap.libp2p.io"
16+
dnsBootstrapPeers = []string{
17+
"QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
18+
"QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
19+
"QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
20+
"QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
21+
}
22+
smallKeyBootstrapPeers = []string{
23+
"QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z",
24+
"QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm",
25+
"QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3",
26+
"QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx",
27+
"QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
28+
"QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu",
29+
"QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64",
30+
"QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd",
31+
}
32+
oldBootstrapAddrs = []string{
33+
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
34+
"/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
35+
"/ip4/128.199.219.111/tcp/4001/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu",
36+
"/ip4/104.236.76.40/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64",
37+
"/ip4/178.62.158.247/tcp/4001/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd",
38+
"/ip6/2604:a880:1:20::203:d001/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
39+
"/ip6/2400:6180:0:d0::151:6001/tcp/4001/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu",
40+
"/ip6/2604:a880:800:10::4a:5001/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64",
41+
"/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd",
42+
}
43+
)
44+
45+
// convFunc does an inplace conversion of the "Bootstrap"
46+
// configuration from one version to another
47+
type convFunc func([]string) []string
48+
49+
// convertFile converts a config file from one version to another, the
50+
// converted config is stored in
51+
func convertFile(orig string, new string, convFunc convFunc) error {
52+
in, err := os.Open(orig)
53+
if err != nil {
54+
return err
55+
}
56+
out, err := os.Create(new)
57+
if err != nil {
58+
return err
59+
}
60+
return convert(in, out, convFunc)
61+
}
62+
63+
// convert converts the config from one version to another, returns
64+
// the converted config as a map[string]interface{}
65+
func convert(in io.Reader, out io.Writer, convFunc convFunc) error {
66+
data, err := ioutil.ReadAll(in)
67+
if err != nil {
68+
return err
69+
}
70+
confMap := make(map[string]interface{})
71+
if err = json.Unmarshal(data, &confMap); err != nil {
72+
return err
73+
}
74+
bootstrapi, _ := confMap["Bootstrap"].([]interface{})
75+
if bootstrapi == nil {
76+
bootstrapi, _ := confMap["bootstrap"].([]interface{})
77+
if bootstrapi == nil {
78+
return fmt.Errorf("Bootstrap field missing or of the wrong type")
79+
}
80+
}
81+
bootstrap := make([]string, len(bootstrapi))
82+
for i := range bootstrapi {
83+
bootstrap[i] = bootstrapi[i].(string)
84+
}
85+
res := convFunc(bootstrap)
86+
confMap["Bootstrap"] = res
87+
fixed, err := json.MarshalIndent(confMap, "", " ")
88+
if err != nil {
89+
return err
90+
}
91+
out.Write(fixed)
92+
out.Write([]byte("\n"))
93+
return nil
94+
}
95+
96+
func ver7to8(bootstrap []string) []string {
97+
// Make sure the dnsaddrs bootstrap peers are included
98+
var res []string
99+
for _, p := range dnsBootstrapPeers {
100+
res = append(res, fmt.Sprintf("/dnsaddr/%s/p2p/%s", dnsAddr, p))
101+
}
102+
103+
// Filter out peers that we added above, or that have an ID known to belong
104+
// to a peer with a small key
105+
for _, addr := range bootstrap {
106+
if ok, err := isDNSBootstrapPeer(addr); !ok && err == nil {
107+
if ok, err = isSmallKeyPeer(addr); !ok && err == nil {
108+
// Replace /ipfs with /p2p
109+
addr = strings.Replace(addr, "/ipfs", "/p2p", -1)
110+
res = append(res, addr)
111+
}
112+
}
113+
}
114+
115+
return res
116+
}
117+
118+
func ver8to7(bootstrap []string) []string {
119+
// Make sure the old addresses are included
120+
res := append([]string{}, oldBootstrapAddrs...)
121+
122+
oldPeerIDs := make(map[string]struct{})
123+
for _, addr := range oldBootstrapAddrs {
124+
pid, err := getAddrPeerID(addr)
125+
if err != nil {
126+
panic(err)
127+
}
128+
oldPeerIDs[pid] = struct{}{}
129+
}
130+
131+
// Filter out old addresses added above, and addresses with the new DNS
132+
// addresses
133+
for _, btAddr := range bootstrap {
134+
pid, err := getAddrPeerID(btAddr)
135+
if err == nil {
136+
if _, ok := oldPeerIDs[pid]; !ok && !strings.Contains(btAddr, dnsAddr) {
137+
res = append(res, btAddr)
138+
}
139+
}
140+
}
141+
142+
return res
143+
}
144+
145+
func getAddrPeerID(addr string) (string, error) {
146+
// eg /ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
147+
parts := strings.Split(addr, "/")
148+
if len(parts) == 0 {
149+
return "", fmt.Errorf("Could not parse peer ID from addr '%s'", addr)
150+
}
151+
last := parts[len(parts)-1]
152+
if _, err := cid.Decode(last); err != nil {
153+
return "", fmt.Errorf("Could not parse peer ID from addr '%s'", addr)
154+
}
155+
return last, nil
156+
}
157+
158+
func addrPeerIDInList(peers []string, addr string) (bool, error) {
159+
addrID, err := getAddrPeerID(addr)
160+
if err != nil {
161+
return false, err
162+
}
163+
for _, p := range peers {
164+
if p == addrID {
165+
return true, nil
166+
}
167+
}
168+
return false, nil
169+
}
170+
171+
func isDNSBootstrapPeer(addr string) (bool, error) {
172+
return addrPeerIDInList(dnsBootstrapPeers, addr)
173+
}
174+
175+
func isSmallKeyPeer(addr string) (bool, error) {
176+
return addrPeerIDInList(smallKeyBootstrapPeers, addr)
177+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package mg7
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func arrayMatch(a []interface{}, b []string) bool {
12+
if len(a) != len(b) {
13+
return false
14+
}
15+
am := make(map[string]struct{})
16+
for _, i := range a {
17+
am[i.(string)] = struct{}{}
18+
}
19+
for _, i := range b {
20+
if _, ok := am[i]; !ok {
21+
return false
22+
}
23+
}
24+
return true
25+
}
26+
27+
func matchesExpected(t *testing.T, res []byte, exp []string) bool {
28+
confMap := make(map[string]interface{})
29+
if err := json.Unmarshal(res, &confMap); err != nil {
30+
t.Fatal(err)
31+
}
32+
33+
return arrayMatch(confMap["Bootstrap"].([]interface{}), exp)
34+
}
35+
36+
func TestOldToNew(t *testing.T) {
37+
in := strings.NewReader(oldConfig)
38+
out := new(bytes.Buffer)
39+
if err := convert(in, out, ver7to8); err != nil {
40+
t.Fatal(err)
41+
}
42+
43+
if !matchesExpected(t, out.Bytes(), expectedMigrateForward) {
44+
t.Fatal(fmt.Errorf("Converted does not match expected result\n%s\n%s\n", out.String(), expectedMigrateForward))
45+
}
46+
}
47+
48+
func TestNewToOld(t *testing.T) {
49+
in := strings.NewReader(newConfig)
50+
out := new(bytes.Buffer)
51+
if err := convert(in, out, ver8to7); err != nil {
52+
t.Fatal(err)
53+
}
54+
55+
if !matchesExpected(t, out.Bytes(), expectedMigrateBackward) {
56+
t.Fatal(fmt.Errorf("Converted does not match expected result\n%s\n%s\n", out.String(), expectedMigrateBackward))
57+
}
58+
}
59+
60+
var oldConfig = `{
61+
"Some": {
62+
"Other": "Config"
63+
},
64+
"Bootstrap": [
65+
"/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
66+
"/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z",
67+
"/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
68+
"/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm",
69+
"/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu",
70+
"/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64",
71+
"/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd",
72+
"/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3",
73+
"/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx",
74+
"/ip6/2604:a880:1:20::1f9:9001/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z",
75+
"/ip6/2604:a880:1:20::203:d001/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
76+
"/ip6/2604:a880:0:1010::23:d001/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm",
77+
"/ip6/2400:6180:0:d0::151:6001/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu",
78+
"/ip6/2604:a880:800:10::4a:5001/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64",
79+
"/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd",
80+
"/ip6/2a03:b0c0:1:d0::e7:1/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3",
81+
"/ip6/2604:a880:1:20::1d9:6001/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx"
82+
]
83+
}
84+
`
85+
86+
var newConfig = `{
87+
"Some": {
88+
"Other": "Config"
89+
},
90+
"Bootstrap": [
91+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
92+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
93+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
94+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
95+
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
96+
"/ip4/104.131.131.83/tcp/4001/p2p/QmcafeMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLaaaa"
97+
]
98+
}
99+
`
100+
101+
var expectedMigrateForward = []string{
102+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
103+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
104+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
105+
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
106+
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
107+
}
108+
109+
var expectedMigrateBackward = []string{
110+
"/ip4/104.131.131.83/tcp/4001/p2p/QmcafeMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLaaaa",
111+
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
112+
"/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
113+
"/ip4/128.199.219.111/tcp/4001/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu",
114+
"/ip4/104.236.76.40/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64",
115+
"/ip4/178.62.158.247/tcp/4001/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd",
116+
"/ip6/2604:a880:1:20::203:d001/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
117+
"/ip6/2400:6180:0:d0::151:6001/tcp/4001/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu",
118+
"/ip6/2604:a880:800:10::4a:5001/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64",
119+
"/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd",
120+
}

0 commit comments

Comments
 (0)