-
Notifications
You must be signed in to change notification settings - Fork 51
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
fix(i): Make peer connections deterministic #2888
Merged
nasdf
merged 7 commits into
sourcenetwork:develop
from
nasdf:nasdf/fix/peer-connections
Aug 26, 2024
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f8032b3
move host setup. refactor bootstrap. add reconnectPeers to tests
nasdf 93da8b7
add more net tests
nasdf 5ef80c6
restore peer context
nasdf 4447678
make tidy
nasdf 1baf383
fix failing net test
nasdf 48c998a
Merge branch 'develop' into nasdf/fix/peer-connections
nasdf 1654c0e
Merge branch 'develop' into nasdf/fix/peer-connections
nasdf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ type Options struct { | |
EnableRelay bool | ||
GRPCServerOptions []grpc.ServerOption | ||
GRPCDialOptions []grpc.DialOption | ||
BootstrapPeers []string | ||
} | ||
|
||
// DefaultOptions returns the default net options. | ||
|
@@ -64,3 +65,10 @@ func WithListenAddresses(addresses ...string) NodeOpt { | |
opt.ListenAddresses = addresses | ||
} | ||
} | ||
|
||
// WithBootstrapPeers sets the bootstrap peer addresses to attempt to connect to. | ||
func WithBootstrapPeers(peers ...string) NodeOpt { | ||
return func(opt *Options) { | ||
opt.BootstrapPeers = peers | ||
} | ||
} | ||
Comment on lines
+69
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: All other config functions are tested in this file except this one. Please add a test if not too much hassle. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a test here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2024 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package net | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
libp2p "github.com/libp2p/go-libp2p" | ||
dht "github.com/libp2p/go-libp2p-kad-dht" | ||
dualdht "github.com/libp2p/go-libp2p-kad-dht/dual" | ||
record "github.com/libp2p/go-libp2p-record" | ||
libp2pCrypto "github.com/libp2p/go-libp2p/core/crypto" | ||
"github.com/libp2p/go-libp2p/core/host" | ||
"github.com/libp2p/go-libp2p/core/routing" | ||
"github.com/libp2p/go-libp2p/p2p/net/connmgr" | ||
) | ||
|
||
// setupHost returns a host and router configured with the given options. | ||
func setupHost(ctx context.Context, options *Options) (host.Host, *dualdht.DHT, error) { | ||
connManager, err := connmgr.NewConnManager(100, 400, connmgr.WithGracePeriod(time.Second*20)) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
dhtOpts := []dualdht.Option{ | ||
dualdht.DHTOption(dht.NamespacedValidator("pk", record.PublicKeyValidator{})), | ||
dualdht.DHTOption(dht.Concurrency(10)), | ||
dualdht.DHTOption(dht.Mode(dht.ModeAuto)), | ||
} | ||
|
||
var ddht *dualdht.DHT | ||
routing := func(h host.Host) (routing.PeerRouting, error) { | ||
ddht, err = dualdht.New(ctx, h, dhtOpts...) | ||
return ddht, err | ||
} | ||
|
||
libp2pOpts := []libp2p.Option{ | ||
libp2p.ConnectionManager(connManager), | ||
libp2p.DefaultTransports, | ||
libp2p.ListenAddrStrings(options.ListenAddresses...), | ||
libp2p.Routing(routing), | ||
} | ||
|
||
// relay is enabled by default unless explicitly disabled | ||
if !options.EnableRelay { | ||
libp2pOpts = append(libp2pOpts, libp2p.DisableRelay()) | ||
} | ||
|
||
// use the private key from options or generate a random one | ||
if options.PrivateKey != nil { | ||
privateKey, err := libp2pCrypto.UnmarshalEd25519PrivateKey(options.PrivateKey) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
libp2pOpts = append(libp2pOpts, libp2p.Identity(privateKey)) | ||
} | ||
|
||
h, err := libp2p.New(libp2pOpts...) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return h, ddht, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2024 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package net | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSetupHostWithDefaultOptions(t *testing.T) { | ||
h, dht, err := setupHost(context.Background(), DefaultOptions()) | ||
require.NoError(t, err) | ||
|
||
require.NotNil(t, h) | ||
require.NotNil(t, dht) | ||
|
||
err = h.Close() | ||
require.NoError(t, err) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Sorry if I've misread, but I can't spot where this is actually read - is it dead code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used here:
defradb/net/peer.go
Line 84 in f8032b3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cheers!