Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double the usefulness interval for peers in the Routing Table #651

Merged
merged 5 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines +283 to +284
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aarshkshah1992 what do you think about having the usefulness here be an option so that we can play around with it in testground?

Perhaps something like an option that takes func(refreshThreshold time.Duration) time.Duration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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