@cryptotaxi247 / kubo / commits / 29bf59dde

bootstrap: use ipfsaddr for boostrap peers :warning:

:warning: this commit makes your current configs unusable, as the default bootstrap peers. You may need to edit your config. Go from: ```js Bootstrap: [ { "Address": "/ip4/104.131.131.82/tcp/4001", "PeerID": "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" } ] ``` To: ```js Bootstrap: [ "/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" ] ```

Juan Batiz-Benet committed Jan 20, 2015 at 16:05 UTC 29bf59dded42bf661cff9832b6ada8c53ec4faaa
9 files changed +107 -91
core/bootstrap.go
+13 -20
@@ -220,32 +220,25 @@ func bootstrapConnect(ctx context.Context,
220 return nil
221 }
222
223 -func toPeerInfos(bpeers []config.BootstrapPeer) ([]peer.PeerInfo, error) {
223 +func toPeerInfos(bpeers []config.BootstrapPeer) []peer.PeerInfo {
224 var peers []peer.PeerInfo
225 for _, bootstrap := range bpeers {
226 - p, err := toPeerInfo(bootstrap)
227 - if err != nil {
228 - return nil, err
229 - }
230 - peers = append(peers, p)
226 + peers = append(peers, toPeerInfo(bootstrap))
227 }
232 - return peers, nil
228 + return peers
229 }
230
235 -func toPeerInfo(bootstrap config.BootstrapPeer) (p peer.PeerInfo, err error) {
236 - id, err := peer.IDB58Decode(bootstrap.PeerID)
237 - if err != nil {
238 - return
239 - }
240 - maddr, err := ma.NewMultiaddr(bootstrap.Address)
241 - if err != nil {
242 - return
243 - }
244 - p = peer.PeerInfo{
245 - ID: id,
246 - Addrs: []ma.Multiaddr{maddr},
231 +func toPeerInfo(bp config.BootstrapPeer) peer.PeerInfo {
232 + // for now, we drop the "ipfs addr" part of the multiaddr. the rest
233 + // of the codebase currently uses addresses without the peerid part.
234 + m := bp.Multiaddr()
235 + s := ma.Split(m)
236 + m = ma.Join(s[:len(s)-1]...)
237 +
238 + return peer.PeerInfo{
239 + ID: bp.ID(),
240 + Addrs: []ma.Multiaddr{m},
241 }
248 - return
242 }
243
244 func randomSubsetOfPeers(in []peer.PeerInfo, max int) []peer.PeerInfo {
core/commands/bootstrap.go
+25 -11
@@ -3,6 +3,7 @@ package commands
3 import (
4 "bytes"
5 "io"
6 + "sort"
7
8 cmds "github.com/jbenet/go-ipfs/commands"
9 repo "github.com/jbenet/go-ipfs/repo"
@@ -197,8 +198,13 @@ var bootstrapListCmd = &cmds.Command{
198 return
199 }
200
200 - peers := cfg.Bootstrap
201 + peers, err := cfg.BootstrapPeers()
202 + if err != nil {
203 + res.SetError(err, cmds.ErrNormal)
204 + return
205 + }
206 res.SetOutput(&BootstrapOutput{peers})
207 + return
208 },
209 Type: BootstrapOutput{},
210 Marshalers: cmds.MarshalerMap{
@@ -219,9 +225,10 @@ func bootstrapMarshaler(res cmds.Response) (io.Reader, error) {
225
226 func bootstrapWritePeers(w io.Writer, prefix string, peers []config.BootstrapPeer) error {
227
222 - for _, peer := range peers {
223 - s := prefix + peer.Address + "/" + peer.PeerID + "\n"
224 - _, err := w.Write([]byte(s))
228 + pstrs := config.BootstrapPeerStrings(peers)
229 + sort.Stable(sort.StringSlice(pstrs))
230 + for _, peer := range pstrs {
231 + _, err := w.Write([]byte(peer + "\n"))
232 if err != nil {
233 return err
234 }
@@ -235,14 +242,14 @@ func bootstrapAdd(r repo.Repo, cfg *config.Config, peers []config.BootstrapPeer)
242 for _, peer := range peers {
243 duplicate := false
244 for _, peer2 := range cfg.Bootstrap {
238 - if peer.Address == peer2.Address && peer.PeerID == peer2.PeerID {
245 + if peer.Equal(peer2) {
246 duplicate = true
247 break
248 }
249 }
250
251 if !duplicate {
245 - cfg.Bootstrap = append(cfg.Bootstrap, peer)
252 + cfg.Bootstrap = append(cfg.Bootstrap, peer.String())
253 added = append(added, peer)
254 }
255 }
@@ -258,10 +265,15 @@ func bootstrapRemove(r repo.Repo, cfg *config.Config, toRemove []config.Bootstra
265 removed := make([]config.BootstrapPeer, 0, len(toRemove))
266 keep := make([]config.BootstrapPeer, 0, len(cfg.Bootstrap))
267
261 - for _, peer := range cfg.Bootstrap {
268 + peers, err := cfg.BootstrapPeers()
269 + if err != nil {
270 + return nil, err
271 + }
272 +
273 + for _, peer := range peers {
274 found := false
275 for _, peer2 := range toRemove {
264 - if peer.Address == peer2.Address && peer.PeerID == peer2.PeerID {
276 + if peer.Equal(peer2) {
277 found = true
278 removed = append(removed, peer)
279 break
@@ -272,7 +284,7 @@ func bootstrapRemove(r repo.Repo, cfg *config.Config, toRemove []config.Bootstra
284 keep = append(keep, peer)
285 }
286 }
275 - cfg.Bootstrap = keep
287 + cfg.SetBootstrapPeers(keep)
288
289 if err := r.SetConfig(cfg); err != nil {
290 return nil, err
@@ -282,8 +294,10 @@ func bootstrapRemove(r repo.Repo, cfg *config.Config, toRemove []config.Bootstra
294 }
295
296 func bootstrapRemoveAll(r repo.Repo, cfg *config.Config) ([]config.BootstrapPeer, error) {
285 - removed := make([]config.BootstrapPeer, len(cfg.Bootstrap))
286 - copy(removed, cfg.Bootstrap)
297 + removed, err := cfg.BootstrapPeers()
298 + if err != nil {
299 + return nil, err
300 + }
301
302 cfg.Bootstrap = nil
303 if err := r.SetConfig(cfg); err != nil {
core/core.go
+10 -3
@@ -318,10 +318,9 @@ func (n *IpfsNode) Bootstrap(cfg BootstrapConfig) error {
318 // freshest bootstrap peers from config. this responds to live changes.
319 if cfg.BootstrapPeers == nil {
320 cfg.BootstrapPeers = func() []peer.PeerInfo {
321 - bpeers := n.Repo.Config().Bootstrap
322 - ps, err := toPeerInfos(bpeers)
321 + ps, err := n.loadBootstrapPeers()
322 if err != nil {
324 - log.Warningf("failed to parse bootstrap peers from config: %s", bpeers)
323 + log.Warningf("failed to parse bootstrap peers from config: %s", n.Repo.Config().Bootstrap)
324 return nil
325 }
326 return ps
@@ -370,6 +369,14 @@ func (n *IpfsNode) loadPrivateKey() error {
369 return nil
370 }
371
372 +func (n *IpfsNode) loadBootstrapPeers() ([]peer.PeerInfo, error) {
373 + parsed, err := n.Repo.Config().BootstrapPeers()
374 + if err != nil {
375 + return nil, err
376 + }
377 + return toPeerInfos(parsed), nil
378 +}
379 +
380 // SetupOfflineRouting loads the local nodes private key and
381 // uses it to instantiate a routing system in offline mode.
382 // This is primarily used for offline ipns modifications.
repo/config/bootstrap_peers.go
+31 -45
@@ -1,12 +1,9 @@
1 package config
2
3 import (
4 - "strings"
5 -
6 - ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
7 - mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
8 -
4 errors "github.com/jbenet/go-ipfs/util/debugerror"
5 +
6 + iaddr "github.com/jbenet/go-ipfs/util/ipfsaddr"
7 )
8
9 // DefaultBootstrapAddresses are the hardcoded bootstrap addresses
@@ -16,21 +13,25 @@ import (
13 // Note: this is here -- and not inside cmd/ipfs/init.go -- because of an
14 // import dependency issue. TODO: move this into a config/default/ package.
15 var DefaultBootstrapAddresses = []string{
19 - "/ip4/104.131.131.82/tcp/4001/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", // mars.i.ipfs.io
20 - "/ip4/104.236.176.52/tcp/4001/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z", // neptune (to be neptune.i.ipfs.io)
21 - "/ip4/104.236.179.241/tcp/4001/QmSoLpPVmHKQ4XTPdz8tjDFgdeRFkpV8JgYq8JVJ69RrZm", // pluto (to be pluto.i.ipfs.io)
22 - "/ip4/162.243.248.213/tcp/4001/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm", // uranus (to be uranus.i.ipfs.io)
23 - "/ip4/128.199.219.111/tcp/4001/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu", // saturn (to be saturn.i.ipfs.io)
24 - "/ip4/104.236.76.40/tcp/4001/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64", // venus (to be venus.i.ipfs.io)
25 - "/ip4/178.62.158.247/tcp/4001/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd", // earth (to be earth.i.ipfs.io)
26 - "/ip4/178.62.61.185/tcp/4001/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3", // mercury (to be mercury.i.ipfs.io)
27 - "/ip4/104.236.151.122/tcp/4001/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx", // jupiter (to be jupiter.i.ipfs.io)
16 + "/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", // mars.i.ipfs.io
17 + "/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z", // neptune (to be neptune.i.ipfs.io)
18 + "/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLpPVmHKQ4XTPdz8tjDFgdeRFkpV8JgYq8JVJ69RrZm", // pluto (to be pluto.i.ipfs.io)
19 + "/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm", // uranus (to be uranus.i.ipfs.io)
20 + "/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu", // saturn (to be saturn.i.ipfs.io)
21 + "/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64", // venus (to be venus.i.ipfs.io)
22 + "/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd", // earth (to be earth.i.ipfs.io)
23 + "/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3", // mercury (to be mercury.i.ipfs.io)
24 + "/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx", // jupiter (to be jupiter.i.ipfs.io)
25 }
26
27 // BootstrapPeer is a peer used to bootstrap the network.
31 -type BootstrapPeer struct {
32 - Address string
33 - PeerID string // until multiaddr supports ipfs, use another field.
28 +type BootstrapPeer iaddr.IPFSAddr
29 +
30 +// ErrInvalidPeerAddr signals an address is not a valid peer address.
31 +var ErrInvalidPeerAddr = errors.New("invalid peer address")
32 +
33 +func (c *Config) BootstrapPeers() ([]BootstrapPeer, error) {
34 + return ParseBootstrapPeers(c.Bootstrap)
35 }
36
37 // DefaultBootstrapPeers returns the (parsed) set of default bootstrap peers.
@@ -45,39 +46,16 @@ This is a problem with the ipfs codebase. Please report it to the dev team.`, er
46 return ps, nil
47 }
48
48 -func (bp *BootstrapPeer) String() string {
49 - return bp.Address + "/" + bp.PeerID
49 +func (c *Config) SetBootstrapPeers(bps []BootstrapPeer) {
50 + c.Bootstrap = BootstrapPeerStrings(bps)
51 }
52
53 func ParseBootstrapPeer(addr string) (BootstrapPeer, error) {
53 - // to be replaced with just multiaddr parsing, once ptp is a multiaddr protocol
54 - idx := strings.LastIndex(addr, "/")
55 - if idx == -1 {
56 - return BootstrapPeer{}, errors.New("invalid address")
57 - }
58 - addrS := addr[:idx]
59 - peeridS := addr[idx+1:]
60 -
61 - // make sure addrS parses as a multiaddr.
62 - if len(addrS) > 0 {
63 - maddr, err := ma.NewMultiaddr(addrS)
64 - if err != nil {
65 - return BootstrapPeer{}, err
66 - }
67 -
68 - addrS = maddr.String()
69 - }
70 -
71 - // make sure idS parses as a peer.ID
72 - _, err := mh.FromB58String(peeridS)
54 + ia, err := iaddr.ParseString(addr)
55 if err != nil {
74 - return BootstrapPeer{}, err
56 + return nil, err
57 }
76 -
77 - return BootstrapPeer{
78 - Address: addrS,
79 - PeerID: peeridS,
80 - }, nil
58 + return BootstrapPeer(ia), err
59 }
60
61 func ParseBootstrapPeers(addrs []string) ([]BootstrapPeer, error) {
@@ -91,3 +69,11 @@ func ParseBootstrapPeers(addrs []string) ([]BootstrapPeer, error) {
69 }
70 return peers, nil
71 }
72 +
73 +func BootstrapPeerStrings(bps []BootstrapPeer) []string {
74 + bpss := make([]string, len(bps))
75 + for i, p := range bps {
76 + bpss[i] = p.String()
77 + }
78 + return bpss
79 +}
repo/config/config.go
+8 -8
@@ -16,14 +16,14 @@ var log = u.Logger("config")
16
17 // Config is used to load IPFS config files.
18 type Config struct {
19 - Identity Identity // local node's peer identity
20 - Datastore Datastore // local node's storage
21 - Addresses Addresses // local node's addresses
22 - Mounts Mounts // local node's mount points
23 - Version Version // local node's version management
24 - Bootstrap []BootstrapPeer // local nodes's bootstrap peers
25 - Tour Tour // local node's tour position
26 - Gateway Gateway // local node's gateway server options
19 + Identity Identity // local node's peer identity
20 + Datastore Datastore // local node's storage
21 + Addresses Addresses // local node's addresses
22 + Mounts Mounts // local node's mount points
23 + Version Version // local node's version management
24 + Bootstrap []string // local nodes's bootstrap peer addresses
25 + Tour Tour // local node's tour position
26 + Gateway Gateway // local node's gateway server options
27 }
28
29 const (
repo/config/init.go
+1 -1
@@ -38,7 +38,7 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
38 API: "/ip4/127.0.0.1/tcp/5001",
39 },
40
41 - Bootstrap: bootstrapPeers,
41 + Bootstrap: BootstrapPeerStrings(bootstrapPeers),
42 Datastore: *ds,
43 Identity: identity,
44
test/3nodetest/client/run.sh
+1 -1
@@ -1,4 +1,4 @@
1 -ipfs bootstrap add /ip4/$BOOTSTRAP_PORT_4011_TCP_ADDR/tcp/$BOOTSTRAP_PORT_4011_TCP_PORT/QmNXuBh8HFsWq68Fid8dMbGNQTh7eG6hV9rr1fQyfmfomE
1 +ipfs bootstrap add /ip4/$BOOTSTRAP_PORT_4011_TCP_ADDR/tcp/$BOOTSTRAP_PORT_4011_TCP_PORT/ipfs/QmNXuBh8HFsWq68Fid8dMbGNQTh7eG6hV9rr1fQyfmfomE
2
3
4 echo "3nodetest> starting client daemon"
test/3nodetest/server/run.sh
+2 -2
@@ -1,12 +1,12 @@
1 # must be connected to bootstrap node
2 -ipfs bootstrap add /ip4/$BOOTSTRAP_PORT_4011_TCP_ADDR/tcp/$BOOTSTRAP_PORT_4011_TCP_PORT/QmNXuBh8HFsWq68Fid8dMbGNQTh7eG6hV9rr1fQyfmfomE
2 +ipfs bootstrap add /ip4/$BOOTSTRAP_PORT_4011_TCP_ADDR/tcp/$BOOTSTRAP_PORT_4011_TCP_PORT/ipfs/QmNXuBh8HFsWq68Fid8dMbGNQTh7eG6hV9rr1fQyfmfomE
3
4 # wait for daemon to start/bootstrap
5 # alternatively use ipfs swarm connect
6 echo "3nodetest> starting server daemon"
7 ipfs daemon &
8 sleep 3
9 -# TODO instead of bootrapping: ipfs swarm connect /ip4/$BOOTSTRAP_PORT_4011_TCP_ADDR/tcp/$BOOTSTRAP_PORT_4011_TCP_PORT/QmNXuBh8HFsWq68Fid8dMbGNQTh7eG6hV9rr1fQyfmfomE
9 +# TODO instead of bootrapping: ipfs swarm connect /ip4/$BOOTSTRAP_PORT_4011_TCP_ADDR/tcp/$BOOTSTRAP_PORT_4011_TCP_PORT/ipfs/QmNXuBh8HFsWq68Fid8dMbGNQTh7eG6hV9rr1fQyfmfomE
10
11 # must mount this volume from data container
12 ipfs add -q /data/filetiny > tmptiny
util/ipfsaddr/ipfsaddr.go
+16
@@ -15,6 +15,8 @@ var ErrInvalidAddr = errors.New("invalid ipfs address")
15 type IPFSAddr interface {
16 ID() peer.ID
17 Multiaddr() ma.Multiaddr
18 + String() string
19 + Equal(b interface{}) bool
20 }
21
22 type ipfsAddr struct {
@@ -30,6 +32,20 @@ func (a ipfsAddr) Multiaddr() ma.Multiaddr {
32 return a.ma
33 }
34
35 +func (a ipfsAddr) String() string {
36 + return a.ma.String()
37 +}
38 +
39 +func (a ipfsAddr) Equal(b interface{}) bool {
40 + if ib, ok := b.(IPFSAddr); ok {
41 + return a.Multiaddr().Equal(ib.Multiaddr())
42 + }
43 + if mb, ok := b.(ma.Multiaddr); ok {
44 + return a.Multiaddr().Equal(mb)
45 + }
46 + return false
47 +}
48 +
49 // ParseString parses a string representation of an address into an IPFSAddr
50 func ParseString(str string) (a IPFSAddr, err error) {
51 if str == "" {