Skip to content

Commit

Permalink
Double the usefulness interval for peers in the Routing Table (ipfs#651)
Browse files Browse the repository at this point in the history
* method for getting bootstrap peers and double the usefulness interval
  • Loading branch information
aarshkshah1992 committed May 22, 2020
1 parent 6a14d9c commit c38060f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
3 changes: 2 additions & 1 deletion dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ func makeDHT(ctx context.Context, h host.Host, cfg config) (*IpfsDHT, error) {
}

// construct routing table
rt, err := makeRoutingTable(dht, cfg, maxLastSuccessfulOutboundThreshold)
// use twice the theoritical usefulness threhold to keep older peers around longer
rt, err := makeRoutingTable(dht, cfg, 2*maxLastSuccessfulOutboundThreshold)
if err != nil {
return nil, fmt.Errorf("failed to construct routing table,err=%s", err)
}
Expand Down
20 changes: 20 additions & 0 deletions dht_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"time"

"github.com/libp2p/go-libp2p-core/peer"

"github.com/multiformats/go-multiaddr"
)

Expand Down Expand Up @@ -35,6 +37,24 @@ func init() {
}
}

// GetDefaultBootstrapPeerAddrInfos returns the peer.AddrInfos for the default
// bootstrap peers so we can use these for initializing the DHT by passing these to the
// BootstrapPeers(...) option.
func GetDefaultBootstrapPeerAddrInfos() []peer.AddrInfo {
ds := make([]peer.AddrInfo, 0, len(DefaultBootstrapPeers))

for i := range DefaultBootstrapPeers {
info, err := peer.AddrInfoFromP2pAddr(DefaultBootstrapPeers[i])
if err != nil {
logger.Errorw("failed to convert bootstrapper address to peer addr info", "address",
DefaultBootstrapPeers[i].String(), err, "err")
continue
}
ds = append(ds, *info)
}
return ds
}

// Bootstrap tells the DHT to get into a bootstrapped state satisfying the
// IpfsRouter interface.
func (dht *IpfsDHT) Bootstrap(ctx context.Context) error {
Expand Down
22 changes: 22 additions & 0 deletions dht_bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/libp2p/go-libp2p-core/event"
"github.com/libp2p/go-libp2p-core/peer"
kb "github.com/libp2p/go-libp2p-kbucket"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -46,3 +47,24 @@ func TestSelfWalkOnAddressChange(t *testing.T) {
require.Contains(t, ps, d2.self)
require.Contains(t, ps, d3.self)
}

func TestDefaultBootstrappers(t *testing.T) {
ds := GetDefaultBootstrapPeerAddrInfos()
require.NotEmpty(t, ds)
require.Len(t, ds, len(DefaultBootstrapPeers))

dfmap := make(map[peer.ID]peer.AddrInfo)
for _, p := range DefaultBootstrapPeers {
info, err := peer.AddrInfoFromP2pAddr(p)
require.NoError(t, err)
dfmap[info.ID] = *info
}

for _, p := range ds {
inf, ok := dfmap[p.ID]
require.True(t, ok)
require.ElementsMatch(t, p.Addrs, inf.Addrs)
delete(dfmap, p.ID)
}
require.Empty(t, dfmap)
}
7 changes: 1 addition & 6 deletions dht_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/libp2p/go-libp2p-kad-dht/providers"
record "github.com/libp2p/go-libp2p-record"
ma "github.com/multiformats/go-multiaddr"
)

// ModeOpt describes what mode the dht should operate in
Expand Down Expand Up @@ -398,12 +397,8 @@ func V1CompatibleMode(enable bool) Option {

// BootstrapPeers configures the bootstrapping nodes that we will connect to to seed
// and refresh our Routing Table if it becomes empty.
func BootstrapPeers(addrs ...ma.Multiaddr) Option {
func BootstrapPeers(bootstrappers ...peer.AddrInfo) Option {
return func(c *config) error {
bootstrappers, err := peer.AddrInfosFromP2pAddrs(addrs...)
if err != nil {
return fmt.Errorf("failed to parse bootstrap peer addresses: %w", err)
}
c.bootstrapPeers = bootstrappers
return nil
}
Expand Down
9 changes: 4 additions & 5 deletions dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2021,12 +2021,11 @@ func TestBootStrapWhenRTIsEmpty(t *testing.T) {
}

// convert the bootstrap addresses to a p2p address
bootstrapAddrs := make([]ma.Multiaddr, nBootStraps)
bootstrapAddrs := make([]peer.AddrInfo, nBootStraps)
for i := 0; i < nBootStraps; i++ {
b, err := peer.AddrInfoToP2pAddrs(&peer.AddrInfo{ID: bootstrappers[i].self,
Addrs: bootstrappers[i].host.Addrs()})
require.NoError(t, err)
bootstrapAddrs[i] = b[0]
b := peer.AddrInfo{ID: bootstrappers[i].self,
Addrs: bootstrappers[i].host.Addrs()}
bootstrapAddrs[i] = b
}

//----------------
Expand Down

0 comments on commit c38060f

Please sign in to comment.