Skip to content

Commit

Permalink
feat(routing): allow-offline during put
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentsenta committed Apr 12, 2023
1 parent a4092ba commit 290f29a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
34 changes: 25 additions & 9 deletions core/commands/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

cmdenv "github.com/ipfs/kubo/core/commands/cmdenv"

iface "github.com/ipfs/boxo/coreiface"
"github.com/ipfs/boxo/coreiface/options"
dag "github.com/ipfs/boxo/ipld/merkledag"
path "github.com/ipfs/boxo/path"
cid "github.com/ipfs/go-cid"
Expand All @@ -19,6 +21,16 @@ import (
routing "github.com/libp2p/go-libp2p/core/routing"
)

var (
errAllowOffline = errors.New("can't put while offline: pass `--allow-offline` to override")
)

const (
dhtVerboseOptionName = "verbose"
numProvidersOptionName = "num-providers"
allowOfflineOptionName = "allow-offline"
)

var RoutingCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Issue routing commands.",
Expand All @@ -34,14 +46,6 @@ var RoutingCmd = &cmds.Command{
},
}

const (
dhtVerboseOptionName = "verbose"
)

const (
numProvidersOptionName = "num-providers"
)

var findProvidersRoutingCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Find peers that can provide a specific value, given a key.",
Expand Down Expand Up @@ -420,6 +424,9 @@ identified by QmFoo.
cmds.StringArg("key", true, false, "The key to store the value at."),
cmds.FileArg("value-file", true, false, "A path to a file containing the value to store.").EnableStdin(),
},
Options: []cmds.Option{
cmds.BoolOption(allowOfflineOptionName, "When offline, save the IPNS record to the the local datastore without broadcasting to the network instead of simply failing."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
if err != nil {
Expand All @@ -437,13 +444,22 @@ identified by QmFoo.
return err
}

err = api.Routing().Put(req.Context, req.Arguments[0], data)
allowOffline, _ := req.Options[allowOfflineOptionName].(bool)

opts := []options.RoutingPutOption{
options.Put.AllowOffline(allowOffline),
}

err = api.Routing().Put(req.Context, req.Arguments[0], data, opts...)
if err != nil {
return err
}

id, err := api.Key().Self(req.Context)
if err != nil {
if err == iface.ErrOffline {
err = errAllowOffline
}
return err
}

Expand Down
13 changes: 10 additions & 3 deletions core/coreapi/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"

coreiface "github.com/ipfs/boxo/coreiface"
caopts "github.com/ipfs/boxo/coreiface/options"
"github.com/ipfs/boxo/path"
peer "github.com/libp2p/go-libp2p/core/peer"
)
Expand All @@ -24,9 +25,15 @@ func (r *RoutingAPI) Get(ctx context.Context, key string) ([]byte, error) {
return r.routing.GetValue(ctx, dhtKey)
}

func (r *RoutingAPI) Put(ctx context.Context, key string, value []byte) error {
if !r.nd.IsOnline {
return coreiface.ErrOffline
func (r *RoutingAPI) Put(ctx context.Context, key string, value []byte, opts ...caopts.RoutingPutOption) error {
options, err := caopts.RoutingPutOptions(opts...)
if err != nil {
return err
}

err = r.checkOnline(options.AllowOffline)
if err != nil {
return err
}

dhtKey, err := normalizeKey(key)
Expand Down

0 comments on commit 290f29a

Please sign in to comment.