-
Notifications
You must be signed in to change notification settings - Fork 177
Add additional publish function for more fine grained control of IPNS record publishing #91
Conversation
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.
Thanks!
I'm not exactly in favour of adding new stuff here as it will eventually get replaced with coreapi implementation, but this seems useful enough.
Few things:
- Some stuff in code
- A test or two would be great
- currently we don't test ipns here, and it might be a bit flaky, so feel free to skip this part.
- Instead of erroring on missing args, I'd make the arguments optional
ipns.go
Outdated
resolveString = "false" | ||
} | ||
args := []string{contentHash, resolveString, lifetime, ttl, key} | ||
resp, err := s.newRequest(context.TODO(), "name/publish", args...).Send(s.httpcli) |
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.
should be context.Background()
as in other places
ipns.go
Outdated
if contentHash == "" { | ||
return nil, errors.New("empty contentHash provided") | ||
} | ||
if lifetime == "" { |
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'd pass this as time.Duration
ipns.go
Outdated
if lifetime == "" { | ||
return nil, errors.New("empty lifetime provided") | ||
} | ||
if ttl == "" { |
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.
Same with time.Duration
ipns.go
Outdated
if key == "" { | ||
return nil, errors.New("empty key provided") | ||
} | ||
if resolve { |
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.
can use strconv.FormatBool
Thanks for the review, all good suggestions. I'll implement the changes you suggested and try to spec out some tests for this function (I don't think this part should be toooo difficult since it's just one function call). Depending on how busy my work week is I should have this done ~Friday. Thanks again. |
At the expense of slight duplicated code, I create two new functions getURLIPNS and SendIPNS to accommodate for how the daemon handles web requests. Currently when using `Send` every argument gets formatted with `arg=....&arg=....` causing defaults to be used for many arguments. This was preventing publishing of records using non-self keys as the arguments for the API calls have to be named.
@magik6k So I had to create some slight duplicated code. While writing the tests, I discovered that no matter what, I was unable to sign IPNS entries with keys other than so when making the API call you had a URL that looked like
instead of
This would cause system defaults to be used for the key, lifetime, ttl, etc.... I was able to get around this without impacting any existing functionality by creating special functions to be called when publishing IPNS entries. After making the changes I was able to publish IPNS entries using any keys. |
Did you try to use Lines 172 to 186 in de157ca
|
Oh I'm dumb haha I didn't see that! I'll try with that, thanks. Update: Works perfectly! Thanks for the suggestion much cleaner now. |
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.
Thanks for the effort!
I'd just skip the tests (with t.Skip()
at the begging of each test), as I can't think of an easy way of executing them without interfering with the daemon.
Other than that, one nitpick and it should be good to go!
ipns_test.go
Outdated
var examplesHash = "/ipfs/Qmbu7x6gJbsKDcseQv66pSbUcAA3Au6f7MfTYVXwvBxN2K" | ||
|
||
func TestPublishDetailsWithKey(t *testing.T) { | ||
shell := ipfsapi.NewShell("localhost:5001") |
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.
other tests do NewShell(shellUrl)
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.
(Note that you'll need to change the package to just shell
)
ipns_test.go
Outdated
func TestPublishDetailsWithKey(t *testing.T) { | ||
shell := ipfsapi.NewShell("localhost:5001") | ||
|
||
resp, err := shell.PublishWithDetails(examplesHash, "key1", time.Hour, time.Hour, false) |
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.
This expects key1
to be generated when calling this
ipns_test.go
Outdated
func TestPublishDetailsWithoutKey(t *testing.T) { | ||
shell := ipfsapi.NewShell("localhost:5001") | ||
|
||
resp, err := shell.PublishWithDetails(examplesHash, "", time.Hour, time.Hour, false) |
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.
This will publish over whatever is published on the machine it's invoked on, which is.. suboptimal
ipns.go
Outdated
key = "self" | ||
} | ||
req.Opts["key"] = key | ||
req.Opts["lifetime"] = lifetime.String() |
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'd wrap this and ttl
with an if and only set this when duration is > 0
go-ipfs cares whether or not a option is present - https://github.com/ipfs/go-ipfs/blob/master/core/commands/publish.go#L117
@magik6k No problem! Happy to be of some help, you all have been doing fantastic work with IPFS and the related protocols, kudos. I added I haven't done complete tests on this yet and its not really related to this PR so feel free to ignore, but it appears that if my node tries to publish an IPNS entry for content that it does not have locally via cache or pinning then the node hangs on the publishing process indefinitely. I tried this last week and I let my node run for 15 minutes before killing the process. |
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.
LGTM (with 1 tiny nitpick)
ipns.go
Outdated
@@ -25,6 +33,36 @@ func (s *Shell) Publish(node string, value string) error { | |||
return nil | |||
} | |||
|
|||
// PublishWithDetails is used for fine grained control over record publishing | |||
func (s *Shell) PublishWithDetails(contentHash, key string, lifetime, ttl time.Duration, resolve bool) (*PublishResponse, error) { | |||
var pubResp PublishResponse |
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'd put it above/closer to json.Unmarshal
line
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.
Done! :D
(needs gofmt, see Travis) |
Done! Thank you for putting up with some of my silly mistakes! If I submit another PR in the future, I will make sure to thoroughly parse it before-hand to make sure the stuff that came up in this PR, doesn't come up in the future. |
Looks good, going to merge now. Also a note - Commit messages should be scoped by roughly what subsystem they affect, like |
Thanks once again! The advice is greatly appreciated 😀 |
Hello,
I recently ran into an issue with go-ipfs-api where I wanted additional control over IPNS record publishing (key specification, ttl, etc..., retrieving the response) so I have created an additional function
PublishWithDetails
, which returns the response from the IPFS daemon as well as allows for manipulation of the api parameters.