From 691d1b36bb81a4587ed825dbd4d83b98b2e15428 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Fri, 29 Aug 2014 11:34:50 -0700 Subject: [PATCH] integrate bitswap and blockservice into the core package --- bitswap/bitswap.go | 3 ++- blockservice/blockservice.go | 5 ++++ cmd/ipfs/ipfs.go | 4 +++- cmd/ipfs/mount_unix.go | 1 + core/core.go | 43 ++++++++++++++++++++++++++++++---- fuse/readonly/readonly_unix.go | 16 +++++++++---- merkledag/coding.go | 1 + path/path.go | 7 ++++-- 8 files changed, 68 insertions(+), 12 deletions(-) diff --git a/bitswap/bitswap.go b/bitswap/bitswap.go index d71bc347a69..99096f2f757 100644 --- a/bitswap/bitswap.go +++ b/bitswap/bitswap.go @@ -78,6 +78,7 @@ func NewBitSwap(p *peer.Peer, net swarm.Network, d ds.Datastore, r routing.IpfsR // GetBlock attempts to retrieve a particular block from peers, within timeout. func (bs *BitSwap) GetBlock(k u.Key, timeout time.Duration) ( *blocks.Block, error) { + u.DOut("Bitswap GetBlock: '%s'\n", k.Pretty()) begin := time.Now() tleft := timeout - time.Now().Sub(begin) provs_ch := bs.routing.FindProvidersAsync(k, 20, timeout) @@ -126,7 +127,7 @@ func (bs *BitSwap) getBlock(k u.Key, p *peer.Peer, timeout time.Duration) ([]byt case resp_mes := <-resp: return resp_mes.Data, nil case <-after: - u.PErr("getBlock for '%s' timed out.\n", k) + u.PErr("getBlock for '%s' timed out.\n", k.Pretty()) return nil, u.ErrTimeout } } diff --git a/blockservice/blockservice.go b/blockservice/blockservice.go index a2924d73f91..ceb806f9b3c 100644 --- a/blockservice/blockservice.go +++ b/blockservice/blockservice.go @@ -48,24 +48,29 @@ func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) { // GetBlock retrieves a particular block from the service, // Getting it from the datastore using the key (hash). func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) { + u.DOut("BlockService GetBlock: '%s'\n", k.Pretty()) dsk := ds.NewKey(string(k)) datai, err := s.Datastore.Get(dsk) if err == nil { + u.DOut("Blockservice: Got data in datastore.\n") bdata, ok := datai.([]byte) if !ok { return nil, fmt.Errorf("data associated with %s is not a []byte", k) } + u.DOut("Got data: %v\n", bdata) return &blocks.Block{ Multihash: mh.Multihash(k), Data: bdata, }, nil } else if err == ds.ErrNotFound && s.Remote != nil { + u.DOut("Blockservice: Searching bitswap.\n") blk, err := s.Remote.GetBlock(k, time.Second*5) if err != nil { return nil, err } return blk, nil } else { + u.DOut("Blockservice GetBlock: Not found.\n") return nil, u.ErrNotFound } } diff --git a/cmd/ipfs/ipfs.go b/cmd/ipfs/ipfs.go index 5f0acc666c1..b141e039673 100644 --- a/cmd/ipfs/ipfs.go +++ b/cmd/ipfs/ipfs.go @@ -2,12 +2,13 @@ package main import ( "fmt" + "os" + "github.com/gonuts/flag" "github.com/jbenet/commander" config "github.com/jbenet/go-ipfs/config" core "github.com/jbenet/go-ipfs/core" u "github.com/jbenet/go-ipfs/util" - "os" ) // The IPFS command tree. It is an instance of `commander.Command`. @@ -55,6 +56,7 @@ func ipfsCmd(c *commander.Command, args []string) error { } func main() { + u.Debug = true err := CmdIpfs.Dispatch(os.Args[1:]) if err != nil { if len(err.Error()) > 0 { diff --git a/cmd/ipfs/mount_unix.go b/cmd/ipfs/mount_unix.go index 2f08b77b89b..b6c69db9a86 100644 --- a/cmd/ipfs/mount_unix.go +++ b/cmd/ipfs/mount_unix.go @@ -4,6 +4,7 @@ package main import ( "fmt" + "github.com/gonuts/flag" "github.com/jbenet/commander" rofs "github.com/jbenet/go-ipfs/fuse/readonly" diff --git a/core/core.go b/core/core.go index f86e3bfca47..01ff8c18af4 100644 --- a/core/core.go +++ b/core/core.go @@ -4,11 +4,17 @@ import ( "fmt" ds "github.com/jbenet/datastore.go" + "github.com/jbenet/go-ipfs/bitswap" bserv "github.com/jbenet/go-ipfs/blockservice" config "github.com/jbenet/go-ipfs/config" merkledag "github.com/jbenet/go-ipfs/merkledag" path "github.com/jbenet/go-ipfs/path" peer "github.com/jbenet/go-ipfs/peer" + routing "github.com/jbenet/go-ipfs/routing" + dht "github.com/jbenet/go-ipfs/routing/dht" + swarm "github.com/jbenet/go-ipfs/swarm" + u "github.com/jbenet/go-ipfs/util" + ma "github.com/jbenet/go-multiaddr" ) // IpfsNode is IPFS Core module. It represents an IPFS instance. @@ -27,13 +33,13 @@ type IpfsNode struct { Datastore ds.Datastore // the network message stream - // Network *netmux.Netux + Swarm *swarm.Swarm // the routing system. recommend ipfs-dht - // Routing *routing.Routing + Routing routing.IpfsRouting // the block exchange + strategy (bitswap) - // BitSwap *bitswap.BitSwap + BitSwap *bitswap.BitSwap // the block service, get/add blocks. Blocks *bserv.BlockService @@ -59,7 +65,36 @@ func NewIpfsNode(cfg *config.Config) (*IpfsNode, error) { return nil, err } - bs, err := bserv.NewBlockService(d, nil) + maddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/4001") + if err != nil { + return nil, err + } + + local := &peer.Peer{ + ID: peer.ID(cfg.Identity.PeerID), + Addresses: []*ma.Multiaddr{maddr}, + } + + if len(local.ID) == 0 { + mh, err := u.Hash([]byte("blah blah blah ID")) + if err != nil { + return nil, err + } + local.ID = peer.ID(mh) + } + + net := swarm.NewSwarm(local) + err = net.Listen() + if err != nil { + return nil, err + } + + route := dht.NewDHT(local, net, d) + route.Start() + + swap := bitswap.NewBitSwap(local, net, d, route) + + bs, err := bserv.NewBlockService(d, swap) if err != nil { return nil, err } diff --git a/fuse/readonly/readonly_unix.go b/fuse/readonly/readonly_unix.go index c5ea97c78af..2897fcd24f8 100644 --- a/fuse/readonly/readonly_unix.go +++ b/fuse/readonly/readonly_unix.go @@ -5,17 +5,19 @@ package readonly import ( - "bazil.org/fuse" - "bazil.org/fuse/fs" "fmt" - core "github.com/jbenet/go-ipfs/core" - mdag "github.com/jbenet/go-ipfs/merkledag" "os" "os/exec" "os/signal" "runtime" "syscall" "time" + + "bazil.org/fuse" + "bazil.org/fuse/fs" + core "github.com/jbenet/go-ipfs/core" + mdag "github.com/jbenet/go-ipfs/merkledag" + u "github.com/jbenet/go-ipfs/util" ) // FileSystem is the readonly Ipfs Fuse Filesystem. @@ -45,6 +47,7 @@ func (*Root) Attr() fuse.Attr { // Lookup performs a lookup under this node. func (s *Root) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) { + u.DOut("Root Lookup: '%s'\n", name) switch name { case "mach_kernel", ".hidden", "._.": // Just quiet some log noise on OS X. @@ -62,6 +65,7 @@ func (s *Root) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) { // ReadDir reads a particular directory. Disallowed for root. func (*Root) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) { + u.DOut("Read Root.\n") return nil, fuse.EPERM } @@ -73,6 +77,7 @@ type Node struct { // Attr returns the attributes of a given node. func (s *Node) Attr() fuse.Attr { + u.DOut("Node attr.\n") if len(s.Nd.Links) > 0 { return fuse.Attr{Mode: os.ModeDir | 0555} } @@ -83,6 +88,7 @@ func (s *Node) Attr() fuse.Attr { // Lookup performs a lookup under this node. func (s *Node) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) { + u.DOut("Lookup '%s'\n", name) nd, err := s.Ipfs.Resolver.ResolveLinks(s.Nd, []string{name}) if err != nil { // todo: make this error more versatile. @@ -94,6 +100,7 @@ func (s *Node) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) { // ReadDir reads the link structure as directory entries func (s *Node) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) { + u.DOut("Node ReadDir\n") entries := make([]fuse.Dirent, len(s.Nd.Links)) for i, link := range s.Nd.Links { n := link.Name @@ -111,6 +118,7 @@ func (s *Node) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) { // ReadAll reads the object data as file data func (s *Node) ReadAll(intr fs.Intr) ([]byte, fuse.Error) { + u.DOut("Read node.\n") return []byte(s.Nd.Data), nil } diff --git a/merkledag/coding.go b/merkledag/coding.go index d87f134f649..e27f03accd4 100644 --- a/merkledag/coding.go +++ b/merkledag/coding.go @@ -2,6 +2,7 @@ package merkledag import ( "fmt" + mh "github.com/jbenet/go-multihash" ) diff --git a/path/path.go b/path/path.go index 3f1c3997e4c..ffcfd47c25c 100644 --- a/path/path.go +++ b/path/path.go @@ -2,11 +2,12 @@ package path import ( "fmt" + "path" + "strings" + merkledag "github.com/jbenet/go-ipfs/merkledag" u "github.com/jbenet/go-ipfs/util" mh "github.com/jbenet/go-multihash" - "path" - "strings" ) // Resolver provides path resolution to IPFS @@ -19,6 +20,7 @@ type Resolver struct { // path component as a hash (key) of the first node, then resolves // all other components walking the links, with ResolveLinks. func (s *Resolver) ResolvePath(fpath string) (*merkledag.Node, error) { + u.DOut("Resolve: '%s'\n", fpath) fpath = path.Clean(fpath) parts := strings.Split(fpath, "/") @@ -39,6 +41,7 @@ func (s *Resolver) ResolvePath(fpath string) (*merkledag.Node, error) { return nil, err } + u.DOut("Resolve dag get.\n") nd, err := s.DAG.Get(u.Key(h)) if err != nil { return nil, err