SkyDB integration - GetEntry, SetEntry, GetJSON and SetJSON#29
SkyDB integration - GetEntry, SetEntry, GetJSON and SetJSON#29ilkamo wants to merge 17 commits intoNebulousLabs:masterfrom
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.
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.
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.
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.
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" |
| @@ -0,0 +1,278 @@ | |||
| package tests | |||
There was a problem hiding this comment.
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.
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.
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.
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.
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.
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.
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.
nit: I believe we have a EnsureSuffix helper in Sia that may be useful here.
mrcnski
left a comment
There was a problem hiding this comment.
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. |
MSevey
left a comment
There was a problem hiding this comment.
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.
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.
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.
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 { |
| } | ||
|
|
||
| // encodeString converts the given string into a byte array. | ||
| func encodeString(toEncode string) []byte { |
There was a problem hiding this comment.
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.
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.
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.
same, ignoring original error
| privateKey string, | ||
| dataKey string, | ||
| json io.Reader, | ||
| revision *uint64, |
There was a problem hiding this comment.
Is there a reason we are using a pointer here?
| } | ||
|
|
||
| opts := skynet.DefaultDownloadOptions | ||
| urlpath := strings.TrimRight(opts.EndpointPath, "/") + "/" + skylink |
Implemented
GetEntry,SetEntry,GetJSON,SetJSON.Added different test cases.
More tests and SetEntryOptions, GetEntryOptions, etc... with timeout will be added in a separate PR.