@cryptotaxi247 / kubo / commits / 691d1b36b

integrate bitswap and blockservice into the core package

Jeromy committed Aug 29, 2014 at 11:34 UTC 691d1b36bb81a4587ed825dbd4d83b98b2e15428
8 files changed +68 -12
bitswap/bitswap.go
+2 -1
@@ -78,6 +78,7 @@ func NewBitSwap(p *peer.Peer, net swarm.Network, d ds.Datastore, r routing.IpfsR
78 // GetBlock attempts to retrieve a particular block from peers, within timeout.
79 func (bs *BitSwap) GetBlock(k u.Key, timeout time.Duration) (
80 *blocks.Block, error) {
81 + u.DOut("Bitswap GetBlock: '%s'\n", k.Pretty())
82 begin := time.Now()
83 tleft := timeout - time.Now().Sub(begin)
84 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
127 case resp_mes := <-resp:
128 return resp_mes.Data, nil
129 case <-after:
129 - u.PErr("getBlock for '%s' timed out.\n", k)
130 + u.PErr("getBlock for '%s' timed out.\n", k.Pretty())
131 return nil, u.ErrTimeout
132 }
133 }
blockservice/blockservice.go
+5
@@ -48,24 +48,29 @@ func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
48 // GetBlock retrieves a particular block from the service,
49 // Getting it from the datastore using the key (hash).
50 func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) {
51 + u.DOut("BlockService GetBlock: '%s'\n", k.Pretty())
52 dsk := ds.NewKey(string(k))
53 datai, err := s.Datastore.Get(dsk)
54 if err == nil {
55 + u.DOut("Blockservice: Got data in datastore.\n")
56 bdata, ok := datai.([]byte)
57 if !ok {
58 return nil, fmt.Errorf("data associated with %s is not a []byte", k)
59 }
60 + u.DOut("Got data: %v\n", bdata)
61 return &blocks.Block{
62 Multihash: mh.Multihash(k),
63 Data: bdata,
64 }, nil
65 } else if err == ds.ErrNotFound && s.Remote != nil {
66 + u.DOut("Blockservice: Searching bitswap.\n")
67 blk, err := s.Remote.GetBlock(k, time.Second*5)
68 if err != nil {
69 return nil, err
70 }
71 return blk, nil
72 } else {
73 + u.DOut("Blockservice GetBlock: Not found.\n")
74 return nil, u.ErrNotFound
75 }
76 }
cmd/ipfs/ipfs.go
+3 -1
@@ -2,12 +2,13 @@ package main
2
3 import (
4 "fmt"
5 + "os"
6 +
7 "github.com/gonuts/flag"
8 "github.com/jbenet/commander"
9 config "github.com/jbenet/go-ipfs/config"
10 core "github.com/jbenet/go-ipfs/core"
11 u "github.com/jbenet/go-ipfs/util"
10 - "os"
12 )
13
14 // The IPFS command tree. It is an instance of `commander.Command`.
@@ -55,6 +56,7 @@ func ipfsCmd(c *commander.Command, args []string) error {
56 }
57
58 func main() {
59 + u.Debug = true
60 err := CmdIpfs.Dispatch(os.Args[1:])
61 if err != nil {
62 if len(err.Error()) > 0 {
cmd/ipfs/mount_unix.go
+1
@@ -4,6 +4,7 @@ package main
4
5 import (
6 "fmt"
7 +
8 "github.com/gonuts/flag"
9 "github.com/jbenet/commander"
10 rofs "github.com/jbenet/go-ipfs/fuse/readonly"
core/core.go
+39 -4
@@ -4,11 +4,17 @@ import (
4 "fmt"
5
6 ds "github.com/jbenet/datastore.go"
7 + "github.com/jbenet/go-ipfs/bitswap"
8 bserv "github.com/jbenet/go-ipfs/blockservice"
9 config "github.com/jbenet/go-ipfs/config"
10 merkledag "github.com/jbenet/go-ipfs/merkledag"
11 path "github.com/jbenet/go-ipfs/path"
12 peer "github.com/jbenet/go-ipfs/peer"
13 + routing "github.com/jbenet/go-ipfs/routing"
14 + dht "github.com/jbenet/go-ipfs/routing/dht"
15 + swarm "github.com/jbenet/go-ipfs/swarm"
16 + u "github.com/jbenet/go-ipfs/util"
17 + ma "github.com/jbenet/go-multiaddr"
18 )
19
20 // IpfsNode is IPFS Core module. It represents an IPFS instance.
@@ -27,13 +33,13 @@ type IpfsNode struct {
33 Datastore ds.Datastore
34
35 // the network message stream
30 - // Network *netmux.Netux
36 + Swarm *swarm.Swarm
37
38 // the routing system. recommend ipfs-dht
33 - // Routing *routing.Routing
39 + Routing routing.IpfsRouting
40
41 // the block exchange + strategy (bitswap)
36 - // BitSwap *bitswap.BitSwap
42 + BitSwap *bitswap.BitSwap
43
44 // the block service, get/add blocks.
45 Blocks *bserv.BlockService
@@ -59,7 +65,36 @@ func NewIpfsNode(cfg *config.Config) (*IpfsNode, error) {
65 return nil, err
66 }
67
62 - bs, err := bserv.NewBlockService(d, nil)
68 + maddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/4001")
69 + if err != nil {
70 + return nil, err
71 + }
72 +
73 + local := &peer.Peer{
74 + ID: peer.ID(cfg.Identity.PeerID),
75 + Addresses: []*ma.Multiaddr{maddr},
76 + }
77 +
78 + if len(local.ID) == 0 {
79 + mh, err := u.Hash([]byte("blah blah blah ID"))
80 + if err != nil {
81 + return nil, err
82 + }
83 + local.ID = peer.ID(mh)
84 + }
85 +
86 + net := swarm.NewSwarm(local)
87 + err = net.Listen()
88 + if err != nil {
89 + return nil, err
90 + }
91 +
92 + route := dht.NewDHT(local, net, d)
93 + route.Start()
94 +
95 + swap := bitswap.NewBitSwap(local, net, d, route)
96 +
97 + bs, err := bserv.NewBlockService(d, swap)
98 if err != nil {
99 return nil, err
100 }
fuse/readonly/readonly_unix.go
+12 -4
@@ -5,17 +5,19 @@
5 package readonly
6
7 import (
8 - "bazil.org/fuse"
9 - "bazil.org/fuse/fs"
8 "fmt"
11 - core "github.com/jbenet/go-ipfs/core"
12 - mdag "github.com/jbenet/go-ipfs/merkledag"
9 "os"
10 "os/exec"
11 "os/signal"
12 "runtime"
13 "syscall"
14 "time"
15 +
16 + "bazil.org/fuse"
17 + "bazil.org/fuse/fs"
18 + core "github.com/jbenet/go-ipfs/core"
19 + mdag "github.com/jbenet/go-ipfs/merkledag"
20 + u "github.com/jbenet/go-ipfs/util"
21 )
22
23 // FileSystem is the readonly Ipfs Fuse Filesystem.
@@ -45,6 +47,7 @@ func (*Root) Attr() fuse.Attr {
47
48 // Lookup performs a lookup under this node.
49 func (s *Root) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
50 + u.DOut("Root Lookup: '%s'\n", name)
51 switch name {
52 case "mach_kernel", ".hidden", "._.":
53 // 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) {
65
66 // ReadDir reads a particular directory. Disallowed for root.
67 func (*Root) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
68 + u.DOut("Read Root.\n")
69 return nil, fuse.EPERM
70 }
71
@@ -73,6 +77,7 @@ type Node struct {
77
78 // Attr returns the attributes of a given node.
79 func (s *Node) Attr() fuse.Attr {
80 + u.DOut("Node attr.\n")
81 if len(s.Nd.Links) > 0 {
82 return fuse.Attr{Mode: os.ModeDir | 0555}
83 }
@@ -83,6 +88,7 @@ func (s *Node) Attr() fuse.Attr {
88
89 // Lookup performs a lookup under this node.
90 func (s *Node) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
91 + u.DOut("Lookup '%s'\n", name)
92 nd, err := s.Ipfs.Resolver.ResolveLinks(s.Nd, []string{name})
93 if err != nil {
94 // todo: make this error more versatile.
@@ -94,6 +100,7 @@ func (s *Node) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
100
101 // ReadDir reads the link structure as directory entries
102 func (s *Node) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
103 + u.DOut("Node ReadDir\n")
104 entries := make([]fuse.Dirent, len(s.Nd.Links))
105 for i, link := range s.Nd.Links {
106 n := link.Name
@@ -111,6 +118,7 @@ func (s *Node) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
118
119 // ReadAll reads the object data as file data
120 func (s *Node) ReadAll(intr fs.Intr) ([]byte, fuse.Error) {
121 + u.DOut("Read node.\n")
122 return []byte(s.Nd.Data), nil
123 }
124
merkledag/coding.go
+1
@@ -2,6 +2,7 @@ package merkledag
2
3 import (
4 "fmt"
5 +
6 mh "github.com/jbenet/go-multihash"
7 )
8
path/path.go
+5 -2
@@ -2,11 +2,12 @@ package path
2
3 import (
4 "fmt"
5 + "path"
6 + "strings"
7 +
8 merkledag "github.com/jbenet/go-ipfs/merkledag"
9 u "github.com/jbenet/go-ipfs/util"
10 mh "github.com/jbenet/go-multihash"
8 - "path"
9 - "strings"
11 )
12
13 // Resolver provides path resolution to IPFS
@@ -19,6 +20,7 @@ type Resolver struct {
20 // path component as a hash (key) of the first node, then resolves
21 // all other components walking the links, with ResolveLinks.
22 func (s *Resolver) ResolvePath(fpath string) (*merkledag.Node, error) {
23 + u.DOut("Resolve: '%s'\n", fpath)
24 fpath = path.Clean(fpath)
25
26 parts := strings.Split(fpath, "/")
@@ -39,6 +41,7 @@ func (s *Resolver) ResolvePath(fpath string) (*merkledag.Node, error) {
41 return nil, err
42 }
43
44 + u.DOut("Resolve dag get.\n")
45 nd, err := s.DAG.Get(u.Key(h))
46 if err != nil {
47 return nil, err