sendblob command

This commit is contained in:
Alex Grintsvayg 2018-08-15 14:25:15 -04:00
parent 4c8eda783f
commit 4284c3b1f9
3 changed files with 46 additions and 5 deletions

41
cmd/sendblob.go Normal file
View file

@ -0,0 +1,41 @@
package cmd
import (
"crypto/rand"
"github.com/lbryio/reflector.go/reflector"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
var cmd = &cobra.Command{
Use: "sendblob ADDRESS:PORT",
Short: "Send a random blob to a reflector server",
Args: cobra.ExactArgs(1),
Run: sendBlobCmd,
}
rootCmd.AddCommand(cmd)
}
func sendBlobCmd(cmd *cobra.Command, args []string) {
addr := args[0]
c := reflector.Client{}
err := c.Connect(addr)
if err != nil {
log.Fatal("error connecting client to server", err)
}
blob := make([]byte, 1024)
_, err = rand.Read(blob)
if err != nil {
log.Fatal("failed to make random blob", err)
}
err = c.SendBlob(blob)
if err != nil {
log.Error(err)
}
}

View file

@ -47,7 +47,7 @@ func (c *Client) SendBlob(blob []byte) error {
return errors.Err("blob is empty")
}
blobHash := getBlobHash(blob)
blobHash := BlobHash(blob)
sendRequest, err := json.Marshal(sendBlobRequest{
BlobSize: len(blob),
BlobHash: blobHash,

View file

@ -191,16 +191,16 @@ func (s *Server) receiveBlob(conn net.Conn) error {
blob, err := s.readRawBlob(conn, blobSize)
if err != nil {
return err
return errors.Prefix("error reading blob "+blobHash[:8], err)
}
receivedBlobHash := getBlobHash(blob)
receivedBlobHash := BlobHash(blob)
if blobHash != receivedBlobHash {
return errors.Err("hash of received blob data does not match hash from send request")
// this can also happen if the blob size is wrong, because the server will read the wrong number of bytes from the stream
}
log.Debugln("Got blob " + blobHash[:8])
log.Infoln("Got blob " + blobHash[:8])
if isSdBlob {
err = s.store.PutSD(blobHash, blob)
@ -337,7 +337,7 @@ func (s *Server) quitting() bool {
}
}
func getBlobHash(blob []byte) string {
func BlobHash(blob []byte) string {
hashBytes := sha512.Sum384(blob)
return hex.EncodeToString(hashBytes[:])
}