Skip to content

Commit

Permalink
chore: deprecate go-ipfs-addr
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Jun 1, 2019
1 parent e8c2852 commit 5d468e2
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 260 deletions.
24 changes: 0 additions & 24 deletions core/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"sync"
"time"

config "github.com/ipfs/go-ipfs-config"
logging "github.com/ipfs/go-log"
"github.com/jbenet/goprocess"
"github.com/jbenet/goprocess/context"
Expand Down Expand Up @@ -216,26 +215,3 @@ func randomSubsetOfPeers(in []peer.AddrInfo, max int) []peer.AddrInfo {
}
return out
}

type Peers []config.BootstrapPeer

func (bpeers Peers) ToPeerInfos() []peer.AddrInfo {
pinfos := make(map[peer.ID]*peer.AddrInfo)
for _, bootstrap := range bpeers {
pinfo, ok := pinfos[bootstrap.ID()]
if !ok {
pinfo = new(peer.AddrInfo)
pinfos[bootstrap.ID()] = pinfo
pinfo.ID = bootstrap.ID()
}

pinfo.Addrs = append(pinfo.Addrs, bootstrap.Transport())
}

var peers []peer.AddrInfo
for _, pinfo := range pinfos {
peers = append(peers, *pinfo)
}

return peers
}
31 changes: 0 additions & 31 deletions core/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package bootstrap

import (
"fmt"
"testing"

config "github.com/ipfs/go-ipfs-config"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/test"
)
Expand All @@ -25,32 +23,3 @@ func TestSubsetWhenMaxIsGreaterThanLengthOfSlice(t *testing.T) {
t.Fail()
}
}

func TestMultipleAddrsPerPeer(t *testing.T) {
var bsps []config.BootstrapPeer
for i := 0; i < 10; i++ {
pid, err := test.RandPeerID()
if err != nil {
t.Fatal(err)
}

addr := fmt.Sprintf("/ip4/127.0.0.1/tcp/5001/ipfs/%s", pid.Pretty())
bsp1, err := config.ParseBootstrapPeer(addr)
if err != nil {
t.Fatal(err)
}

addr = fmt.Sprintf("/ip4/127.0.0.1/udp/5002/utp/ipfs/%s", pid.Pretty())
bsp2, err := config.ParseBootstrapPeer(addr)
if err != nil {
t.Fatal(err)
}

bsps = append(bsps, bsp1, bsp2)
}

pinfos := Peers.ToPeerInfos(bsps)
if len(pinfos) != len(bsps)/2 {
t.Fatal("expected fewer peers")
}
}
130 changes: 75 additions & 55 deletions core/commands/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"errors"
"fmt"
"io"
"sort"

Expand All @@ -11,6 +12,8 @@ import (

cmds "github.com/ipfs/go-ipfs-cmds"
config "github.com/ipfs/go-ipfs-config"
peer "github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
)

type BootstrapOutput struct {
Expand Down Expand Up @@ -64,26 +67,13 @@ in the bootstrap list).
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
deflt, _ := req.Options[defaultOptionName].(bool)

var inputPeers []config.BootstrapPeer
if deflt {
// parse separately for meaningful, correct error.
defltPeers, err := config.DefaultBootstrapPeers()
if err != nil {
return err
}

inputPeers = defltPeers
} else {
inputPeers := config.DefaultBootstrapAddresses
if !deflt {
if err := req.ParseBodyArgs(); err != nil {
return err
}

parsedPeers, err := config.ParseBootstrapPeers(req.Arguments)
if err != nil {
return err
}

inputPeers = parsedPeers
inputPeers = req.Arguments
}

if len(inputPeers) == 0 {
Expand All @@ -110,7 +100,7 @@ in the bootstrap list).
return err
}

return cmds.EmitOnce(res, &BootstrapOutput{config.BootstrapPeerStrings(added)})
return cmds.EmitOnce(res, &BootstrapOutput{added})
},
Type: BootstrapOutput{},
Encoders: cmds.EncoderMap{
Expand All @@ -127,11 +117,6 @@ var bootstrapAddDefaultCmd = &cmds.Command{
in the bootstrap list).`,
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
defltPeers, err := config.DefaultBootstrapPeers()
if err != nil {
return err
}

cfgRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return err
Expand All @@ -148,12 +133,12 @@ in the bootstrap list).`,
return err
}

added, err := bootstrapAdd(r, cfg, defltPeers)
added, err := bootstrapAdd(r, cfg, config.DefaultBootstrapAddresses)
if err != nil {
return err
}

return cmds.EmitOnce(res, &BootstrapOutput{config.BootstrapPeerStrings(added)})
return cmds.EmitOnce(res, &BootstrapOutput{added})
},
Type: BootstrapOutput{},
Encoders: cmds.EncoderMap{
Expand Down Expand Up @@ -201,26 +186,20 @@ var bootstrapRemoveCmd = &cmds.Command{
return err
}

var removed []config.BootstrapPeer
var removed []string
if all {
removed, err = bootstrapRemoveAll(r, cfg)
} else {
if err := req.ParseBodyArgs(); err != nil {
return err
}

input, perr := config.ParseBootstrapPeers(req.Arguments)
if perr != nil {
return perr
}

removed, err = bootstrapRemove(r, cfg, input)
removed, err = bootstrapRemove(r, cfg, req.Arguments)
}
if err != nil {
return err
}

return cmds.EmitOnce(res, &BootstrapOutput{config.BootstrapPeerStrings(removed)})
return cmds.EmitOnce(res, &BootstrapOutput{removed})
},
Type: BootstrapOutput{},
Encoders: cmds.EncoderMap{
Expand Down Expand Up @@ -257,7 +236,7 @@ var bootstrapRemoveAllCmd = &cmds.Command{
return err
}

return cmds.EmitOnce(res, &BootstrapOutput{config.BootstrapPeerStrings(removed)})
return cmds.EmitOnce(res, &BootstrapOutput{removed})
},
Type: BootstrapOutput{},
Encoders: cmds.EncoderMap{
Expand Down Expand Up @@ -315,23 +294,36 @@ func bootstrapWritePeers(w io.Writer, prefix string, peers []string) error {
return nil
}

func bootstrapAdd(r repo.Repo, cfg *config.Config, peers []config.BootstrapPeer) ([]config.BootstrapPeer, error) {
func bootstrapAdd(r repo.Repo, cfg *config.Config, peers []string) ([]string, error) {
for _, p := range peers {
m, err := ma.NewMultiaddr(p)
if err != nil {
return nil, err
}
tpt, p2ppart := ma.SplitLast(m)
if p2ppart == nil || p2ppart.Protocol().Code != ma.P_P2P {
return nil, fmt.Errorf("invalid bootstrap address: %s", p)
}
if tpt == nil {
return nil, fmt.Errorf("bootstrap address without a transport: %s", p)
}
}

addedMap := map[string]struct{}{}
addedList := make([]config.BootstrapPeer, 0, len(peers))
addedList := make([]string, 0, len(peers))

// re-add cfg bootstrap peers to rm dupes
bpeers := cfg.Bootstrap
cfg.Bootstrap = nil

// add new peers
for _, peer := range peers {
s := peer.String()
for _, s := range peers {
if _, found := addedMap[s]; found {
continue
}

cfg.Bootstrap = append(cfg.Bootstrap, s)
addedList = append(addedList, peer)
addedList = append(addedList, s)
addedMap[s] = struct{}{}
}

Expand All @@ -352,27 +344,56 @@ func bootstrapAdd(r repo.Repo, cfg *config.Config, peers []config.BootstrapPeer)
return addedList, nil
}

func bootstrapRemove(r repo.Repo, cfg *config.Config, toRemove []config.BootstrapPeer) ([]config.BootstrapPeer, error) {
removed := make([]config.BootstrapPeer, 0, len(toRemove))
keep := make([]config.BootstrapPeer, 0, len(cfg.Bootstrap))
func bootstrapRemove(r repo.Repo, cfg *config.Config, toRemove []string) ([]string, error) {
removed := make([]peer.AddrInfo, 0, len(toRemove))
keep := make([]peer.AddrInfo, 0, len(cfg.Bootstrap))

toRemoveAddr, err := config.ParseBootstrapPeers(toRemove)
if err != nil {
return nil, err
}
toRemoveMap := make(map[peer.ID][]ma.Multiaddr, len(toRemoveAddr))
for _, addr := range toRemoveAddr {
toRemoveMap[addr.ID] = addr.Addrs
}

peers, err := cfg.BootstrapPeers()
if err != nil {
return nil, err
}

for _, peer := range peers {
found := false
for _, peer2 := range toRemove {
if peer.Equal(peer2) {
found = true
removed = append(removed, peer)
break
for _, p := range peers {
addrs, ok := toRemoveMap[p.ID]
// not in the remove set?
if !ok {
keep = append(keep, p)
continue
}
// remove the entire peer?
if len(addrs) == 0 {
removed = append(removed, p)
continue
}
var (
keptAddrs, removedAddrs []ma.Multiaddr
)
// remove specific addresses
filter:
for _, addr := range p.Addrs {
for _, addr2 := range addrs {
if addr.Equal(addr2) {
removedAddrs = append(removedAddrs, addr)
continue filter
}
}
keptAddrs = append(keptAddrs, addr)
}
if len(removedAddrs) > 0 {
removed = append(removed, peer.AddrInfo{ID: p.ID, Addrs: removedAddrs})
}

if !found {
keep = append(keep, peer)
if len(keptAddrs) > 0 {
keep = append(keep, peer.AddrInfo{ID: p.ID, Addrs: keptAddrs})
}
}
cfg.SetBootstrapPeers(keep)
Expand All @@ -381,10 +402,10 @@ func bootstrapRemove(r repo.Repo, cfg *config.Config, toRemove []config.Bootstra
return nil, err
}

return removed, nil
return config.BootstrapPeerStrings(removed), nil
}

func bootstrapRemoveAll(r repo.Repo, cfg *config.Config) ([]config.BootstrapPeer, error) {
func bootstrapRemoveAll(r repo.Repo, cfg *config.Config) ([]string, error) {
removed, err := cfg.BootstrapPeers()
if err != nil {
return nil, err
Expand All @@ -394,8 +415,7 @@ func bootstrapRemoveAll(r repo.Repo, cfg *config.Config) ([]config.BootstrapPeer
if err := r.SetConfig(cfg); err != nil {
return nil, err
}

return removed, nil
return config.BootstrapPeerStrings(removed), nil
}

const bootstrapSecurityWarning = `
Expand Down
Loading

0 comments on commit 5d468e2

Please sign in to comment.