Skip to content

Commit

Permalink
Merge pull request #13 from textileio/asutula/remote-cid
Browse files Browse the repository at this point in the history
Try to fetch a remote cid
  • Loading branch information
asutula authored Nov 8, 2019
2 parents 455d4c6 + b4914ee commit 1fd34a3
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 174 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test:
go test github.com/textileio/grpc-ipfs-lite/server
go test ./...

ios:
go get golang.org/x/mobile/cmd/...
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ go 1.13

require (
github.com/golang/protobuf v1.3.1
github.com/hsanjuan/ipfs-lite v0.1.6
github.com/hsanjuan/ipfs-lite v0.1.7
github.com/ipfs/go-block-format v0.0.2
github.com/ipfs/go-cid v0.0.3
github.com/ipfs/go-ipld-cbor v0.0.3
github.com/ipfs/go-ipld-format v0.0.2
github.com/ipfs/go-log v0.0.1
github.com/ipfs/go-merkledag v0.2.3
github.com/libp2p/go-libp2p-crypto v0.1.0
github.com/libp2p/go-libp2p-core v0.2.3
github.com/multiformats/go-multiaddr v0.1.1
github.com/multiformats/go-multihash v0.0.8
golang.org/x/mobile v0.0.0-20191031020345-0945064e013a // indirect
google.golang.org/grpc v1.20.1
)
65 changes: 48 additions & 17 deletions go.sum

Large diffs are not rendered by default.

52 changes: 17 additions & 35 deletions mobile/mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,39 @@ package mobile

import (
"context"
"fmt"

ipfslite "github.com/hsanjuan/ipfs-lite"
"github.com/ipfs/go-log"
crypto "github.com/libp2p/go-libp2p-crypto"
"github.com/multiformats/go-multiaddr"
"github.com/textileio/grpc-ipfs-lite/server"
"github.com/textileio/grpc-ipfs-lite/util"
)

// Start starts the mobile ipfs-lite peer and gRPC server
func Start(datastorePath string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

log.SetLogLevel("*", "warn")

ds, err := ipfslite.BadgerDatastore(datastorePath)
if err != nil {
return err
}
priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
if err != nil {
return err
}

listen, _ := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/4005")
var (
cancel context.CancelFunc
)

h, dht, err := ipfslite.SetupLibp2p(
ctx,
priv,
nil,
[]multiaddr.Multiaddr{listen},
)
const ipfsPort = 4006
const grpcPort = 10001

if err != nil {
return err
}
// Start starts the mobile ipfs-lite peer and gRPC server
func Start(datastorePath string, debug bool) (int, error) {
var ctx context.Context
ctx, cancel = context.WithCancel(context.Background())

lite, err := ipfslite.New(ctx, ds, h, dht, nil)
lite, err := util.NewPeer(ctx, datastorePath, ipfsPort, debug)
if err != nil {
return err
return 0, err
}

lite.Bootstrap(ipfslite.DefaultBootstrapPeers())
host := fmt.Sprintf("localhost:%d", grpcPort)

// TODO: run this in a goroutine, but need to get any error back out, but this blocks, so how do you know it is running with no error?
go server.StartServer(lite, "localhost:10000")
go server.StartServer(lite, host)

return nil
return grpcPort, nil
}

// Stop stops the embedded grpc and ipfs servers
func Stop() {
server.StopServer()
cancel()
}
31 changes: 31 additions & 0 deletions mobile/mobile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package mobile

import (
"testing"
"time"
)

func TestStart(t *testing.T) {
port, err := Start("/tmp/mobile-ipfs-lite", false)
if err != nil {
t.Fatalf("failed to Start: %v", err)
}
if port < 1 {
t.Fatal("invalid port")
}
}

func TestStop(t *testing.T) {
time.Sleep(10 * time.Second)
Stop()
}

// func TestStartAgain(t *testing.T) {
// port, err := Start("/tmp/mobile-ipfs-lite", false)
// if err != nil {
// t.Fatalf("failed to Start again: %v", err)
// }
// if port < 1 {
// t.Fatal("invalid port again")
// }
// }
40 changes: 20 additions & 20 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ func (s *ipfsLiteServer) AddFile(srv pb.IpfsLite_AddFileServer) error {
for {
req, err := srv.Recv()
if err == io.EOF {
writer.Close()
_ = writer.Close()
break
} else if err != nil {
writer.CloseWithError(err)
_ = writer.CloseWithError(err)
break
}
switch payload := req.GetPayload().(type) {
Expand Down Expand Up @@ -123,12 +123,12 @@ func (s *ipfsLiteServer) AddFile(srv pb.IpfsLite_AddFileServer) error {
}

func (s *ipfsLiteServer) GetFile(req *pb.GetFileRequest, srv pb.IpfsLite_GetFileServer) error {
cid, err := cid.Decode(req.GetCid())
id, err := cid.Decode(req.GetCid())
if err != nil {
return err
}

reader, err := s.peer.GetFile(srv.Context(), cid)
reader, err := s.peer.GetFile(srv.Context(), id)
if err != nil {
return err
}
Expand All @@ -147,11 +147,11 @@ func (s *ipfsLiteServer) GetFile(req *pb.GetFileRequest, srv pb.IpfsLite_GetFile
}

func (s *ipfsLiteServer) HasBlock(ctx context.Context, req *pb.HasBlockRequest) (*pb.HasBlockResponse, error) {
cid, err := cid.Decode(req.GetCid())
id, err := cid.Decode(req.GetCid())
if err != nil {
return nil, err
}
hasBlock, err := s.peer.HasBlock(cid)
hasBlock, err := s.peer.HasBlock(id)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -203,11 +203,11 @@ func (s *ipfsLiteServer) AddNodes(ctx context.Context, req *pb.AddNodesRequest)
}

func (s *ipfsLiteServer) GetNode(ctx context.Context, req *pb.GetNodeRequest) (*pb.GetNodeResponse, error) {
cid, err := cid.Decode(req.GetCid())
id, err := cid.Decode(req.GetCid())
if err != nil {
return nil, err
}
node, err := s.peer.Get(ctx, cid)
node, err := s.peer.Get(ctx, id)
if err != nil {
return nil, err
}
Expand All @@ -223,11 +223,11 @@ func (s *ipfsLiteServer) GetNode(ctx context.Context, req *pb.GetNodeRequest) (*
func (s *ipfsLiteServer) GetNodes(req *pb.GetNodesRequest, srv pb.IpfsLite_GetNodesServer) error {
cids := make([]cid.Cid, len(req.GetCids()))
for i, cidString := range req.GetCids() {
cid, err := cid.Decode(cidString)
id, err := cid.Decode(cidString)
if err != nil {
return err
}
cids[i] = cid
cids[i] = id
}
ch := s.peer.GetMany(srv.Context(), cids)
for {
Expand All @@ -253,11 +253,11 @@ func (s *ipfsLiteServer) GetNodes(req *pb.GetNodesRequest, srv pb.IpfsLite_GetNo
}

func (s *ipfsLiteServer) RemoveNode(ctx context.Context, req *pb.RemoveNodeRequest) (*pb.RemoveNodeResponse, error) {
cid, err := cid.Decode(req.GetCid())
id, err := cid.Decode(req.GetCid())
if err != nil {
return nil, err
}
err = s.peer.Remove(ctx, cid)
err = s.peer.Remove(ctx, id)
if err != nil {
return nil, err
}
Expand All @@ -267,11 +267,11 @@ func (s *ipfsLiteServer) RemoveNode(ctx context.Context, req *pb.RemoveNodeReque
func (s *ipfsLiteServer) RemoveNodes(ctx context.Context, req *pb.RemoveNodesRequest) (*pb.RemoveNodesResponse, error) {
cids := make([]cid.Cid, len(req.GetCids()))
for i, reqCid := range req.GetCids() {
cid, err := cid.Decode(reqCid)
id, err := cid.Decode(reqCid)
if err != nil {
return nil, err
}
cids[i] = cid
cids[i] = id
}
err := s.peer.RemoveMany(ctx, cids)
if err != nil {
Expand All @@ -281,12 +281,12 @@ func (s *ipfsLiteServer) RemoveNodes(ctx context.Context, req *pb.RemoveNodesReq
}

func (s *ipfsLiteServer) ResolveLink(ctx context.Context, req *pb.ResolveLinkRequest) (*pb.ResolveLinkResponse, error) {
cid, err := cid.Decode(req.GetNodeCid())
id, err := cid.Decode(req.GetNodeCid())
if err != nil {
return nil, err
}

node, err := s.peer.Get(ctx, cid)
node, err := s.peer.Get(ctx, id)
if err != nil {
return nil, err
}
Expand All @@ -311,12 +311,12 @@ func (s *ipfsLiteServer) ResolveLink(ctx context.Context, req *pb.ResolveLinkReq
}

func (s *ipfsLiteServer) Tree(ctx context.Context, req *pb.TreeRequest) (*pb.TreeResponse, error) {
cid, err := cid.Decode(req.GetNodeCid())
id, err := cid.Decode(req.GetNodeCid())
if err != nil {
return nil, err
}

node, err := s.peer.Get(ctx, cid)
node, err := s.peer.Get(ctx, id)
if err != nil {
return nil, err
}
Expand All @@ -331,11 +331,11 @@ func pbBlockToBlock(pbBlock *pb.Block) (blocks.Block, error) {
if len(pbBlock.GetCid()) == 0 {
block = blocks.NewBlock(pbBlock.GetRawData())
} else {
cid, err := cid.Decode(pbBlock.GetCid())
id, err := cid.Decode(pbBlock.GetCid())
if err != nil {
return nil, err
}
block, err = blocks.NewBlockWithCid(pbBlock.GetRawData(), cid)
block, err = blocks.NewBlockWithCid(pbBlock.GetRawData(), id)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 1fd34a3

Please sign in to comment.