-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
cmds: implement ipfs dht provide command #3106
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,15 @@ import ( | |
|
||
key "github.com/ipfs/go-ipfs/blocks/key" | ||
cmds "github.com/ipfs/go-ipfs/commands" | ||
dag "github.com/ipfs/go-ipfs/merkledag" | ||
notif "github.com/ipfs/go-ipfs/notifications" | ||
path "github.com/ipfs/go-ipfs/path" | ||
routing "github.com/ipfs/go-ipfs/routing" | ||
ipdht "github.com/ipfs/go-ipfs/routing/dht" | ||
pstore "gx/ipfs/QmQdnfvZQuhdT93LNc5bos52wAmdr3G2p6G8teLJMEN32P/go-libp2p-peerstore" | ||
peer "gx/ipfs/QmRBqJF7hb8ZSpRcMwUt8hNhydWcxGEhtk81HKq6oUwKvs/go-libp2p-peer" | ||
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util" | ||
"gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context" | ||
) | ||
|
||
var ErrNotDHT = errors.New("routing service is not a DHT") | ||
|
@@ -31,6 +34,7 @@ var DhtCmd = &cmds.Command{ | |
"findpeer": findPeerDhtCmd, | ||
"get": getValueDhtCmd, | ||
"put": putValueDhtCmd, | ||
"provide": provideRefDhtCmd, | ||
}, | ||
} | ||
|
||
|
@@ -227,6 +231,159 @@ var findProvidersDhtCmd = &cmds.Command{ | |
Type: notif.QueryEvent{}, | ||
} | ||
|
||
var provideRefDhtCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Announce to the network that you are providing given values.", | ||
}, | ||
|
||
Arguments: []cmds.Argument{ | ||
cmds.StringArg("key", true, true, "The key to find providers for.").EnableStdin(), | ||
}, | ||
Options: []cmds.Option{ | ||
cmds.BoolOption("verbose", "v", "Print extra information.").Default(false), | ||
cmds.BoolOption("recursive", "r", "Recursively provide entire graph.").Default(false), | ||
}, | ||
Run: func(req cmds.Request, res cmds.Response) { | ||
n, err := req.InvocContext().GetNode() | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
if n.Routing == nil { | ||
res.SetError(errNotOnline, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
rec, _, _ := req.Option("recursive").Bool() | ||
|
||
var keys []key.Key | ||
for _, arg := range req.Arguments() { | ||
k := key.B58KeyDecode(arg) | ||
if k == "" { | ||
res.SetError(fmt.Errorf("incorrectly formatted key: ", arg), cmds.ErrNormal) | ||
return | ||
} | ||
|
||
has, err := n.Blockstore.Has(k) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
if !has { | ||
res.SetError(fmt.Errorf("block %s not found locally, cannot provide", k), cmds.ErrNormal) | ||
return | ||
} | ||
|
||
keys = append(keys, k) | ||
} | ||
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. This makes the command work in buffer mode instead pipe, do we want that? 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. its not exactly in buffer mode. This part should be quite fast, and can error out. We'd like to be able to error out easily in these cases 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. What I mean is it will buffer whole stdin, instead of starting process for each line as we go. but if we don't do it then we hit problem what to do if command errors out on input later. Ok, let's leave it as it is, and create discussion issue about that. 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. Ah, i see what youre saying. Yeah... we need to work that out a bit more i think |
||
|
||
outChan := make(chan interface{}) | ||
res.SetOutput((<-chan interface{})(outChan)) | ||
|
||
events := make(chan *notif.QueryEvent) | ||
ctx := notif.RegisterForQueryEvents(req.Context(), events) | ||
|
||
go func() { | ||
defer close(outChan) | ||
for e := range events { | ||
outChan <- e | ||
} | ||
}() | ||
|
||
go func() { | ||
defer close(events) | ||
if rec { | ||
provideKeysRec(ctx, n.Routing, n.DAG, keys) | ||
} else { | ||
provideKeys(ctx, n.Routing, keys) | ||
} | ||
}() | ||
}, | ||
Marshalers: cmds.MarshalerMap{ | ||
cmds.Text: func(res cmds.Response) (io.Reader, error) { | ||
outChan, ok := res.Output().(<-chan interface{}) | ||
if !ok { | ||
return nil, u.ErrCast() | ||
} | ||
|
||
verbose, _, _ := res.Request().Option("v").Bool() | ||
pfm := pfuncMap{ | ||
notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) { | ||
if verbose { | ||
fmt.Fprintf(out, "sending provider record to peer %s\n", obj.ID) | ||
} | ||
}, | ||
} | ||
|
||
marshal := func(v interface{}) (io.Reader, error) { | ||
obj, ok := v.(*notif.QueryEvent) | ||
if !ok { | ||
return nil, u.ErrCast() | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
printEvent(obj, buf, verbose, pfm) | ||
return buf, nil | ||
} | ||
|
||
return &cmds.ChannelMarshaler{ | ||
Channel: outChan, | ||
Marshaler: marshal, | ||
Res: res, | ||
}, nil | ||
}, | ||
}, | ||
Type: notif.QueryEvent{}, | ||
} | ||
|
||
func provideKeys(ctx context.Context, r routing.IpfsRouting, keys []key.Key) { | ||
for _, k := range keys { | ||
err := r.Provide(ctx, k) | ||
if err != nil { | ||
notif.PublishQueryEvent(ctx, ¬if.QueryEvent{ | ||
Type: notif.QueryError, | ||
Extra: err.Error(), | ||
}) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func provideKeysRec(ctx context.Context, r routing.IpfsRouting, dserv dag.DAGService, keys []key.Key) { | ||
for _, k := range keys { | ||
kset := key.NewKeySet() | ||
node, err := dserv.Get(ctx, k) | ||
if err != nil { | ||
notif.PublishQueryEvent(ctx, ¬if.QueryEvent{ | ||
Type: notif.QueryError, | ||
Extra: err.Error(), | ||
}) | ||
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. Should there be a return here? 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. yeap, gonna refactor to make that class of problems go away |
||
} | ||
|
||
err = dag.EnumerateChildrenAsync(ctx, dserv, node, kset) | ||
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 might be better to have just a buffered channel here instead of keyset, it looks like This way we could do providing at the same time of fetching recursive graph. 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. Yeah, that would be nice. We should make a note to take another look at these interfaces and find better ways to do these things. |
||
if err != nil { | ||
notif.PublishQueryEvent(ctx, ¬if.QueryEvent{ | ||
Type: notif.QueryError, | ||
Extra: err.Error(), | ||
}) | ||
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. yes indeed 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. Should there be return here? |
||
} | ||
|
||
for _, k := range kset.Keys() { | ||
err = r.Provide(ctx, k) | ||
if err != nil { | ||
notif.PublishQueryEvent(ctx, ¬if.QueryEvent{ | ||
Type: notif.QueryError, | ||
Extra: err.Error(), | ||
}) | ||
return | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
var findPeerDhtCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Query the DHT for all of the multiaddresses associated with a Peer ID.", | ||
|
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.
"The key to find providers for."...