-
Notifications
You must be signed in to change notification settings - Fork 8
SkyDB integration - GetEntry, SetEntry, GetJSON and SetJSON #29
base: master
Are you sure you want to change the base?
Conversation
Awesome, thank you for this! I'll take a look. |
} | ||
|
||
// hashRegistryEntry hashes the given registry entry. | ||
func hashRegistryEntry(s RegistryEntry) []byte { |
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 you please add a test for this function? We have one in skynet-js
you can copy.
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 would use the encoding package encoding.Marshl
That would eliminate the need for the majority of this file I believe.
) | ||
|
||
// GetEntry gets the registry entry corresponding to the publicKey and dataKey. | ||
func (sc *SkynetClient) GetEntry( |
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.
API methods should take options, see e.g. (sc *SkynetClient) Download
.
func (sc *SkynetClient) GetEntry( | ||
publicKey string, | ||
dataKey string, | ||
) (r RegistryEntryResponse, err error) { |
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.
Why not return the same object as in skynet-js
? RegistryEntryResponse
is slightly different from the SignedRegistryEntry
returned in skynet-js
.
} | ||
|
||
options := sc.Options | ||
options.customContentType = "application/json" |
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.
Why this line?
@@ -0,0 +1,278 @@ | |||
package tests |
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 you please also add tests against the registry? (e.g. using the registry methods directly.) We have some in skynet-js
you can look at.
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.
Also I think we should have at least one end-to-end test that sets a random registry entry on siasky.net, queries the entry, and makes sure we get the same data back.
) (io.ReadCloser, error) { | ||
entry, err := sc.GetEntry(publicKey, dataKey) | ||
if err != nil { | ||
return nil, errors.AddContext(err, "could not get entry") |
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 shouldn't return an error if the entry wasn't found. In skynet-js
we return null, I guess this should return nil.
func (sc *SkynetClient) GetJSON( | ||
publicKey string, | ||
dataKey string, | ||
) (io.ReadCloser, error) { |
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 should return JSON instead of a ReadCloser
.
}, | ||
) | ||
if err != nil { | ||
return r, errors.AddContext(err, "could not execute request") |
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 return null
instead of an error on 404 (entry not found) - see skynet-js
.
} | ||
} | ||
|
||
tempFile, err := createTempFileFromJson(dataKey, json) |
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 don't think it should be necessary to create an actual physical file in order to upload data. We can just upload the streamed data directly. I believe there is a generic upload function in upload.go
that does this, but if you need help don't hesitate to ask.
func (sc *SkynetClient) SetJSON( | ||
privateKey string, | ||
dataKey string, | ||
json io.Reader, |
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 should take a json object instead of a Reader
.
} | ||
|
||
opts := skynet.DefaultDownloadOptions | ||
urlpath := strings.TrimRight(opts.EndpointPath, "/") + "/" + skylink |
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.
nit: I believe we have a EnsureSuffix
helper in Sia that may be useful here.
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.
modules.EnsureSuffix
👍
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.
Looks good overall, some parts need to be modified to be closer to the Javascript API, and to be more usable. I also think we need to verify this code with an end-to-end test.
@m-cat thanks for the review. I will work on the proposed changes as soon as I have some free time. |
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.
Sorry about the delay on this, I missed the notification somehow.
} | ||
|
||
// hashDataKey hashes the given data key. | ||
func hashDataKey(dataKey string) []byte { |
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.
We have a crypto.HashObject
method you can just use as oppose to implementing this function. Just need to import gitlab.com/NebulousLabs/Sia/crypto
) | ||
|
||
// encodeNumber converts the given number into a byte array. | ||
func encodeNumber(number uint64) []byte { |
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.
We have an encoding package that you can use instead of creating new methods.
gitlab.com/NebulousLabs/encoding
You can use encoding.Marshal
} | ||
|
||
// hashRegistryEntry hashes the given registry entry. | ||
func hashRegistryEntry(s RegistryEntry) []byte { |
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 would use the encoding package encoding.Marshl
That would eliminate the need for the majority of this file I believe.
} | ||
|
||
// hashAll takes all given arguments and hashes them. | ||
func hashAll(args ...[]byte) []byte { |
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.
crypto.HashAll
😄
} | ||
|
||
// encodeString converts the given string into a byte array. | ||
func encodeString(toEncode string) []byte { |
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 function is encoding the length of the string, not the string itself. Is that intentional?
|
||
publicKeyBytes, err := hex.DecodeString(publicKey) | ||
if err != nil { | ||
return false, errors.New("could not decode publicKey") |
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, ignoring original error.
) ([]byte, error) { | ||
privateKeyBytes, err := hex.DecodeString(privateKey) | ||
if err != nil { | ||
return nil, errors.New("could not decode privateKey") |
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, ignoring original error
|
||
skylink, err := hex.DecodeString(entry.Data) | ||
if err != nil { | ||
return nil, errors.New("could not decode data") |
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, ignoring original error
privateKey string, | ||
dataKey string, | ||
json io.Reader, | ||
revision *uint64, |
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.
Is there a reason we are using a pointer here?
} | ||
|
||
opts := skynet.DefaultDownloadOptions | ||
urlpath := strings.TrimRight(opts.EndpointPath, "/") + "/" + skylink |
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.
modules.EnsureSuffix
👍
Implemented
GetEntry
,SetEntry
,GetJSON
,SetJSON
.Added different test cases.
More tests and SetEntryOptions, GetEntryOptions, etc... with timeout will be added in a separate PR.