-
Notifications
You must be signed in to change notification settings - Fork 228
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
Add example of a find peer query using go-kademlia #865
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/libp2p/go-libp2p" | ||
dht "github.com/libp2p/go-libp2p-kad-dht/v2" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
|
||
h, err := libp2p.New(libp2p.NoListenAddrs) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
d, err := dht.New(h, dht.DefaultConfig()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
go d.Start(ctx) | ||
|
||
addrInfo, err := d.FindPeer(ctx, "") | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(addrInfo) | ||
} |
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 @@ | ||
package dht | ||
|
||
import ( | ||
"github.com/plprobelab/go-kademlia/kad" | ||
"github.com/plprobelab/go-kademlia/network/address" | ||
) | ||
|
||
var protoID = address.ProtocolID("/statemachine/1.0.0") // protocol ID for the test | ||
|
||
type FindNodeRequest[K kad.Key[K], A kad.Address[A]] struct { | ||
NodeID kad.NodeID[K] | ||
} | ||
|
||
func (r FindNodeRequest[K, A]) Target() K { | ||
return r.NodeID.Key() | ||
} | ||
|
||
func (FindNodeRequest[K, A]) EmptyResponse() kad.Response[K, A] { | ||
return &FindNodeResponse[K, A]{} | ||
} | ||
|
||
type FindNodeResponse[K kad.Key[K], A kad.Address[A]] struct { | ||
NodeID kad.NodeID[K] // node we were looking for | ||
CloserPeers []kad.NodeInfo[K, A] | ||
} | ||
|
||
func (r *FindNodeResponse[K, A]) CloserNodes() []kad.NodeInfo[K, A] { | ||
return r.CloserPeers | ||
} |
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 |
---|---|---|
|
@@ -2,42 +2,129 @@ package dht | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ipfs/go-cid" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/core/routing" | ||
ma "github.com/multiformats/go-multiaddr" | ||
"github.com/plprobelab/go-kademlia/coord" | ||
"github.com/plprobelab/go-kademlia/kad" | ||
"github.com/plprobelab/go-kademlia/key" | ||
"github.com/plprobelab/go-kademlia/query" | ||
) | ||
|
||
func (d *DHT) Start(ctx context.Context) { | ||
ctx, span := startSpan(ctx, "mainLoop") | ||
defer span.End() | ||
|
||
kadEvents := d.kad.Events() | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case ev := <-kadEvents: | ||
switch tev := ev.(type) { | ||
case *coord.KademliaOutboundQueryProgressedEvent[key.Key256, ma.Multiaddr]: | ||
// TODO: locking | ||
ch, ok := d.qSubs[tev.QueryID] | ||
if !ok { | ||
// we have lost the query waiter somehow | ||
d.kad.StopQuery(ctx, tev.QueryID) | ||
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. StopQuery returns an error but in go-kademlia this is always nil |
||
continue | ||
} | ||
|
||
// notify the waiter | ||
ch <- tev.Response | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Assert that IPFS assumptions about interfaces aren't broken. These aren't a | ||
// guarantee, but we can use them to aid refactoring. | ||
var ( | ||
_ routing.Routing = (*DHT)(nil) | ||
) | ||
|
||
func (d DHT) Provide(ctx context.Context, cid cid.Cid, b bool) error { | ||
func (d *DHT) Provide(ctx context.Context, cid cid.Cid, b bool) error { | ||
panic("implement me") | ||
} | ||
|
||
func (d DHT) FindProvidersAsync(ctx context.Context, cid cid.Cid, i int) <-chan peer.AddrInfo { | ||
func (d *DHT) FindProvidersAsync(ctx context.Context, cid cid.Cid, i int) <-chan peer.AddrInfo { | ||
panic("implement me") | ||
} | ||
|
||
func (d DHT) FindPeer(ctx context.Context, id peer.ID) (peer.AddrInfo, error) { | ||
panic("implement me") | ||
func (d *DHT) registerQuery() (query.QueryID, chan kad.Response[key.Key256, ma.Multiaddr]) { | ||
ch := make(chan kad.Response[key.Key256, ma.Multiaddr]) | ||
|
||
d.qSubsLk.Lock() | ||
d.qSubCnt += 1 | ||
qid := query.QueryID(fmt.Sprintf("query-%d", d.qSubCnt)) | ||
d.qSubs[qid] = ch | ||
d.qSubsLk.Unlock() | ||
|
||
return qid, ch | ||
} | ||
|
||
func (d *DHT) FindPeer(ctx context.Context, id peer.ID) (peer.AddrInfo, error) { | ||
// TODO: look in local peer store first | ||
|
||
qid, ch := d.registerQuery() | ||
|
||
nid := nodeID(d.host.ID()) | ||
err := d.kad.StartQuery(ctx, qid, protoID, &FindNodeRequest[key.Key256, ma.Multiaddr]{NodeID: nid}) | ||
if err != nil { | ||
return peer.AddrInfo{}, fmt.Errorf("failed to start query: %w", err) | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
d.kad.StopQuery(ctx, qid) | ||
return peer.AddrInfo{}, ctx.Err() | ||
case resp, ok := <-ch: | ||
if !ok { | ||
// channel was closed, so query can't progress | ||
d.kad.StopQuery(ctx, qid) | ||
return peer.AddrInfo{}, fmt.Errorf("query was unexpectedly stopped") | ||
} | ||
|
||
// we got a response from a message sent by query | ||
switch resp := resp.(type) { | ||
case *FindNodeResponse[key.Key256, ma.Multiaddr]: | ||
// interpret the response | ||
println("IpfsHandler.FindNode: got FindNode response") | ||
for _, found := range resp.CloserPeers { | ||
if key.Equal(found.ID().Key(), nid.Key()) { | ||
// found the node we were looking for | ||
d.kad.StopQuery(ctx, qid) | ||
pid := peer.AddrInfo{ | ||
ID: peer.ID(found.ID().(nodeID)), | ||
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. It would be cooler if we didn't need to do this type cast here |
||
Addrs: found.Addresses(), | ||
} | ||
return pid, nil | ||
} | ||
} | ||
default: | ||
return peer.AddrInfo{}, fmt.Errorf("unknown response: %v", resp) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (d DHT) PutValue(ctx context.Context, s string, bytes []byte, option ...routing.Option) error { | ||
func (d *DHT) PutValue(ctx context.Context, s string, bytes []byte, option ...routing.Option) error { | ||
panic("implement me") | ||
} | ||
|
||
func (d DHT) GetValue(ctx context.Context, s string, option ...routing.Option) ([]byte, error) { | ||
func (d *DHT) GetValue(ctx context.Context, s string, option ...routing.Option) ([]byte, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (d DHT) SearchValue(ctx context.Context, s string, option ...routing.Option) (<-chan []byte, error) { | ||
func (d *DHT) SearchValue(ctx context.Context, s string, option ...routing.Option) (<-chan []byte, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (d DHT) Bootstrap(ctx context.Context) error { | ||
func (d *DHT) Bootstrap(ctx context.Context) error { | ||
panic("implement me") | ||
} |
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,13 @@ | ||
package dht | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
func startSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) { | ||
return otel.Tracer("go-libp2p-kad-dht").Start(ctx, fmt.Sprintf("Libp2pDHT.%s", name), opts...) | ||
} |
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.
I still think this would better handled in go-kademlia
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.
Yeah it would be nicer but the compromise is that the logic for stopping the query in the event loop has to be injected