Skip to content
This repository has been archived by the owner on Apr 16, 2021. It is now read-only.

SkyDB integration - GetEntry, SetEntry, GetJSON and SetJSON #29

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

ilkamo
Copy link

@ilkamo ilkamo commented Dec 17, 2020

Implemented GetEntry, SetEntry, GetJSON, SetJSON.
Added different test cases.

More tests and SetEntryOptions, GetEntryOptions, etc... with timeout will be added in a separate PR.

@mrcnski
Copy link
Contributor

mrcnski commented Dec 18, 2020

Awesome, thank you for this! I'll take a look.

}

// hashRegistryEntry hashes the given registry entry.
func hashRegistryEntry(s RegistryEntry) []byte {
Copy link
Contributor

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.

Copy link
Contributor

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(
Copy link
Contributor

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) {
Copy link
Contributor

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"
Copy link
Contributor

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
Copy link
Contributor

@mrcnski mrcnski Jan 13, 2021

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.

Copy link
Contributor

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")
Copy link
Contributor

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) {
Copy link
Contributor

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")
Copy link
Contributor

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)
Copy link
Contributor

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,
Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modules.EnsureSuffix 👍

Copy link
Contributor

@mrcnski mrcnski left a 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.

@mrcnski mrcnski requested a review from MSevey January 13, 2021 15:25
@ilkamo
Copy link
Author

ilkamo commented Jan 18, 2021

@m-cat thanks for the review. I will work on the proposed changes as soon as I have some free time.

Copy link
Contributor

@MSevey MSevey left a 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 {
Copy link
Contributor

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 {
Copy link
Contributor

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 {
Copy link
Contributor

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 {
Copy link
Contributor

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 {
Copy link
Contributor

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")
Copy link
Contributor

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")
Copy link
Contributor

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")
Copy link
Contributor

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,
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modules.EnsureSuffix 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants