@cryptotaxi247 / kubo / commits / ef294431d

move util.Key into its own package under blocks

Jeromy committed Jun 1, 2015 at 16:10 UTC ef294431d4497433f69a77dc4981c1e5e575a07e
92 files changed +517 -487
blocks/blocks.go
+3 -2
@@ -7,6 +7,7 @@ import (
7 "fmt"
8
9 mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
10 + key "github.com/ipfs/go-ipfs/blocks/key"
11 u "github.com/ipfs/go-ipfs/util"
12 )
13
@@ -35,8 +36,8 @@ func NewBlockWithHash(data []byte, h mh.Multihash) (*Block, error) {
36 }
37
38 // Key returns the block's Multihash as a Key value.
38 -func (b *Block) Key() u.Key {
39 - return u.Key(b.Multihash)
39 +func (b *Block) Key() key.Key {
40 + return key.Key(b.Multihash)
41 }
42
43 func (b *Block) String() string {
blocks/blockstore/blockstore.go
+13 -13
@@ -11,8 +11,8 @@ import (
11 mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
12 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
13 blocks "github.com/ipfs/go-ipfs/blocks"
14 + key "github.com/ipfs/go-ipfs/blocks/key"
15 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
15 - u "github.com/ipfs/go-ipfs/util"
16 )
17
18 var log = eventlog.Logger("blockstore")
@@ -26,12 +26,12 @@ var ErrNotFound = errors.New("blockstore: block not found")
26
27 // Blockstore wraps a ThreadSafeDatastore
28 type Blockstore interface {
29 - DeleteBlock(u.Key) error
30 - Has(u.Key) (bool, error)
31 - Get(u.Key) (*blocks.Block, error)
29 + DeleteBlock(key.Key) error
30 + Has(key.Key) (bool, error)
31 + Get(key.Key) (*blocks.Block, error)
32 Put(*blocks.Block) error
33
34 - AllKeysChan(ctx context.Context) (<-chan u.Key, error)
34 + AllKeysChan(ctx context.Context) (<-chan key.Key, error)
35 }
36
37 func NewBlockstore(d ds.ThreadSafeDatastore) Blockstore {
@@ -47,7 +47,7 @@ type blockstore struct {
47 // we do check it on `NewBlockstore` though.
48 }
49
50 -func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) {
50 +func (bs *blockstore) Get(k key.Key) (*blocks.Block, error) {
51 maybeData, err := bs.datastore.Get(k.DsKey())
52 if err == ds.ErrNotFound {
53 return nil, ErrNotFound
@@ -74,11 +74,11 @@ func (bs *blockstore) Put(block *blocks.Block) error {
74 return bs.datastore.Put(k, block.Data)
75 }
76
77 -func (bs *blockstore) Has(k u.Key) (bool, error) {
77 +func (bs *blockstore) Has(k key.Key) (bool, error) {
78 return bs.datastore.Has(k.DsKey())
79 }
80
81 -func (s *blockstore) DeleteBlock(k u.Key) error {
81 +func (s *blockstore) DeleteBlock(k key.Key) error {
82 return s.datastore.Delete(k.DsKey())
83 }
84
@@ -86,7 +86,7 @@ func (s *blockstore) DeleteBlock(k u.Key) error {
86 // this is very simplistic, in the future, take dsq.Query as a param?
87 //
88 // AllKeysChan respects context
89 -func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
89 +func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan key.Key, error) {
90
91 // KeysOnly, because that would be _a lot_ of data.
92 q := dsq.Query{KeysOnly: true}
@@ -98,7 +98,7 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
98 }
99
100 // this function is here to compartmentalize
101 - get := func() (k u.Key, ok bool) {
101 + get := func() (k key.Key, ok bool) {
102 select {
103 case <-ctx.Done():
104 return k, false
@@ -111,8 +111,8 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
111 return k, false
112 }
113
114 - // need to convert to u.Key using u.KeyFromDsKey.
115 - k = u.KeyFromDsKey(ds.NewKey(e.Key))
114 + // need to convert to key.Key using key.KeyFromDsKey.
115 + k = key.KeyFromDsKey(ds.NewKey(e.Key))
116 log.Debug("blockstore: query got key", k)
117
118 // key must be a multihash. else ignore it.
@@ -125,7 +125,7 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
125 }
126 }
127
128 - output := make(chan u.Key)
128 + output := make(chan key.Key)
129 go func() {
130 defer func() {
131 res.Process().Close() // ensure exit (signals early exit, too)
blocks/blockstore/blockstore_test.go
+7 -7
@@ -11,14 +11,14 @@ import (
11 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
12
13 blocks "github.com/ipfs/go-ipfs/blocks"
14 - u "github.com/ipfs/go-ipfs/util"
14 + key "github.com/ipfs/go-ipfs/blocks/key"
15 )
16
17 // TODO(brian): TestGetReturnsNil
18
19 func TestGetWhenKeyNotPresent(t *testing.T) {
20 bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
21 - _, err := bs.Get(u.Key("not present"))
21 + _, err := bs.Get(key.Key("not present"))
22
23 if err != nil {
24 t.Log("As expected, block is not present")
@@ -45,13 +45,13 @@ func TestPutThenGetBlock(t *testing.T) {
45 }
46 }
47
48 -func newBlockStoreWithKeys(t *testing.T, d ds.Datastore, N int) (Blockstore, []u.Key) {
48 +func newBlockStoreWithKeys(t *testing.T, d ds.Datastore, N int) (Blockstore, []key.Key) {
49 if d == nil {
50 d = ds.NewMapDatastore()
51 }
52 bs := NewBlockstore(ds_sync.MutexWrap(d))
53
54 - keys := make([]u.Key, N)
54 + keys := make([]key.Key, N)
55 for i := 0; i < N; i++ {
56 block := blocks.NewBlock([]byte(fmt.Sprintf("some data %d", i)))
57 err := bs.Put(block)
@@ -63,8 +63,8 @@ func newBlockStoreWithKeys(t *testing.T, d ds.Datastore, N int) (Blockstore, []u
63 return bs, keys
64 }
65
66 -func collect(ch <-chan u.Key) []u.Key {
67 - var keys []u.Key
66 +func collect(ch <-chan key.Key) []key.Key {
67 + var keys []key.Key
68 for k := range ch {
69 keys = append(keys, k)
70 }
@@ -219,7 +219,7 @@ func TestValueTypeMismatch(t *testing.T) {
219 }
220 }
221
222 -func expectMatches(t *testing.T, expect, actual []u.Key) {
222 +func expectMatches(t *testing.T, expect, actual []key.Key) {
223
224 if len(expect) != len(actual) {
225 t.Errorf("expect and actual differ: %d != %d", len(expect), len(actual))
blocks/blockstore/write_cache.go
+5 -5
@@ -4,7 +4,7 @@ import (
4 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/hashicorp/golang-lru"
5 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
6 "github.com/ipfs/go-ipfs/blocks"
7 - u "github.com/ipfs/go-ipfs/util"
7 + key "github.com/ipfs/go-ipfs/blocks/key"
8 )
9
10 // WriteCached returns a blockstore that caches up to |size| unique writes (bs.Put).
@@ -21,19 +21,19 @@ type writecache struct {
21 blockstore Blockstore
22 }
23
24 -func (w *writecache) DeleteBlock(k u.Key) error {
24 +func (w *writecache) DeleteBlock(k key.Key) error {
25 w.cache.Remove(k)
26 return w.blockstore.DeleteBlock(k)
27 }
28
29 -func (w *writecache) Has(k u.Key) (bool, error) {
29 +func (w *writecache) Has(k key.Key) (bool, error) {
30 if _, ok := w.cache.Get(k); ok {
31 return true, nil
32 }
33 return w.blockstore.Has(k)
34 }
35
36 -func (w *writecache) Get(k u.Key) (*blocks.Block, error) {
36 +func (w *writecache) Get(k key.Key) (*blocks.Block, error) {
37 return w.blockstore.Get(k)
38 }
39
@@ -45,6 +45,6 @@ func (w *writecache) Put(b *blocks.Block) error {
45 return w.blockstore.Put(b)
46 }
47
48 -func (w *writecache) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
48 +func (w *writecache) AllKeysChan(ctx context.Context) (<-chan key.Key, error) {
49 return w.blockstore.AllKeysChan(ctx)
50 }
blocks/key/key.go renamed
+1 -35
@@ -1,4 +1,4 @@
1 -package util
1 +package key
2
3 import (
4 "encoding/json"
@@ -106,40 +106,6 @@ func (b58KeyConverter) InvertKey(dsk ds.Key) ds.Key {
106 return k
107 }
108
109 -// Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits
110 -func Hash(data []byte) mh.Multihash {
111 - h, err := mh.Sum(data, mh.SHA2_256, -1)
112 - if err != nil {
113 - // this error can be safely ignored (panic) because multihash only fails
114 - // from the selection of hash function. If the fn + length are valid, it
115 - // won't error.
116 - panic("multihash failed to hash using SHA2_256.")
117 - }
118 - return h
119 -}
120 -
121 -// IsValidHash checks whether a given hash is valid (b58 decodable, len > 0)
122 -func IsValidHash(s string) bool {
123 - out := b58.Decode(s)
124 - if out == nil || len(out) == 0 {
125 - return false
126 - }
127 - _, err := mh.Cast(out)
128 - if err != nil {
129 - return false
130 - }
131 - return true
132 -}
133 -
134 -// XOR takes two byte slices, XORs them together, returns the resulting slice.
135 -func XOR(a, b []byte) []byte {
136 - c := make([]byte, len(a))
137 - for i := 0; i < len(a); i++ {
138 - c[i] = a[i] ^ b[i]
139 - }
140 - return c
141 -}
142 -
109 // KeySlice is used for sorting Keys
110 type KeySlice []Key
111
blocks/key/key_set.go renamed
+1 -1
@@ -1,4 +1,4 @@
1 -package util
1 +package key
2
3 import (
4 "sync"
blocks/key/key_test.go new
+28
@@ -0,0 +1,28 @@
1 +package key
2 +
3 +import (
4 + "bytes"
5 + "testing"
6 +
7 + mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
8 +)
9 +
10 +func TestKey(t *testing.T) {
11 +
12 + h1, err := mh.Sum([]byte("beep boop"), mh.SHA2_256, -1)
13 + if err != nil {
14 + t.Error(err)
15 + }
16 +
17 + k1 := Key(h1)
18 + h2 := mh.Multihash(k1)
19 + k2 := Key(h2)
20 +
21 + if !bytes.Equal(h1, h2) {
22 + t.Error("Multihashes not equal.")
23 + }
24 +
25 + if k1 != k2 {
26 + t.Error("Keys not equal.")
27 + }
28 +}
blocks/set/dbset.go
+5 -5
@@ -3,7 +3,7 @@ package set
3 import (
4 ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
5 "github.com/ipfs/go-ipfs/blocks/bloom"
6 - "github.com/ipfs/go-ipfs/util"
6 + key "github.com/ipfs/go-ipfs/blocks/key"
7 )
8
9 type datastoreBlockSet struct {
@@ -19,7 +19,7 @@ func NewDBWrapperSet(d ds.Datastore, bset BlockSet) BlockSet {
19 }
20 }
21
22 -func (d *datastoreBlockSet) AddBlock(k util.Key) {
22 +func (d *datastoreBlockSet) AddBlock(k key.Key) {
23 err := d.dstore.Put(k.DsKey(), []byte{})
24 if err != nil {
25 log.Debugf("blockset put error: %s", err)
@@ -28,14 +28,14 @@ func (d *datastoreBlockSet) AddBlock(k util.Key) {
28 d.bset.AddBlock(k)
29 }
30
31 -func (d *datastoreBlockSet) RemoveBlock(k util.Key) {
31 +func (d *datastoreBlockSet) RemoveBlock(k key.Key) {
32 d.bset.RemoveBlock(k)
33 if !d.bset.HasKey(k) {
34 d.dstore.Delete(k.DsKey())
35 }
36 }
37
38 -func (d *datastoreBlockSet) HasKey(k util.Key) bool {
38 +func (d *datastoreBlockSet) HasKey(k key.Key) bool {
39 return d.bset.HasKey(k)
40 }
41
@@ -43,6 +43,6 @@ func (d *datastoreBlockSet) GetBloomFilter() bloom.Filter {
43 return d.bset.GetBloomFilter()
44 }
45
46 -func (d *datastoreBlockSet) GetKeys() []util.Key {
46 +func (d *datastoreBlockSet) GetKeys() []key.Key {
47 return d.bset.GetKeys()
48 }
blocks/set/set.go
+14 -13
@@ -3,6 +3,7 @@ package set
3
4 import (
5 "github.com/ipfs/go-ipfs/blocks/bloom"
6 + key "github.com/ipfs/go-ipfs/blocks/key"
7 "github.com/ipfs/go-ipfs/util"
8 )
9
@@ -10,16 +11,16 @@ var log = util.Logger("blockset")
11
12 // BlockSet represents a mutable set of keyed blocks
13 type BlockSet interface {
13 - AddBlock(util.Key)
14 - RemoveBlock(util.Key)
15 - HasKey(util.Key) bool
14 + AddBlock(key.Key)
15 + RemoveBlock(key.Key)
16 + HasKey(key.Key) bool
17 GetBloomFilter() bloom.Filter
18
18 - GetKeys() []util.Key
19 + GetKeys() []key.Key
20 }
21
21 -func SimpleSetFromKeys(keys []util.Key) BlockSet {
22 - sbs := &simpleBlockSet{blocks: make(map[util.Key]struct{})}
22 +func SimpleSetFromKeys(keys []key.Key) BlockSet {
23 + sbs := &simpleBlockSet{blocks: make(map[key.Key]struct{})}
24 for _, k := range keys {
25 sbs.blocks[k] = struct{}{}
26 }
@@ -27,22 +28,22 @@ func SimpleSetFromKeys(keys []util.Key) BlockSet {
28 }
29
30 func NewSimpleBlockSet() BlockSet {
30 - return &simpleBlockSet{blocks: make(map[util.Key]struct{})}
31 + return &simpleBlockSet{blocks: make(map[key.Key]struct{})}
32 }
33
34 type simpleBlockSet struct {
34 - blocks map[util.Key]struct{}
35 + blocks map[key.Key]struct{}
36 }
37
37 -func (b *simpleBlockSet) AddBlock(k util.Key) {
38 +func (b *simpleBlockSet) AddBlock(k key.Key) {
39 b.blocks[k] = struct{}{}
40 }
41
41 -func (b *simpleBlockSet) RemoveBlock(k util.Key) {
42 +func (b *simpleBlockSet) RemoveBlock(k key.Key) {
43 delete(b.blocks, k)
44 }
45
45 -func (b *simpleBlockSet) HasKey(k util.Key) bool {
46 +func (b *simpleBlockSet) HasKey(k key.Key) bool {
47 _, has := b.blocks[k]
48 return has
49 }
@@ -55,8 +56,8 @@ func (b *simpleBlockSet) GetBloomFilter() bloom.Filter {
56 return f
57 }
58
58 -func (b *simpleBlockSet) GetKeys() []util.Key {
59 - var out []util.Key
59 +func (b *simpleBlockSet) GetKeys() []key.Key {
60 + var out []key.Key
61 for k := range b.blocks {
62 out = append(out, k)
63 }
blockservice/blocks_test.go
+4 -3
@@ -11,6 +11,7 @@ import (
11 blocks "github.com/ipfs/go-ipfs/blocks"
12 blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
13 blocksutil "github.com/ipfs/go-ipfs/blocks/blocksutil"
14 + key "github.com/ipfs/go-ipfs/blocks/key"
15 offline "github.com/ipfs/go-ipfs/exchange/offline"
16 u "github.com/ipfs/go-ipfs/util"
17 )
@@ -30,7 +31,7 @@ func TestBlocks(t *testing.T) {
31 t.Error("Block Multihash and data multihash not equal")
32 }
33
33 - if b.Key() != u.Key(h) {
34 + if b.Key() != key.Key(h) {
35 t.Error("Block key and data multihash key not equal")
36 }
37
@@ -68,7 +69,7 @@ func TestGetBlocksSequential(t *testing.T) {
69 bg := blocksutil.NewBlockGenerator()
70 blks := bg.Blocks(50)
71
71 - var keys []u.Key
72 + var keys []key.Key
73 for _, blk := range blks {
74 keys = append(keys, blk.Key())
75 servs[0].AddBlock(blk)
@@ -79,7 +80,7 @@ func TestGetBlocksSequential(t *testing.T) {
80 for i := 1; i < len(servs); i++ {
81 ctx, _ := context.WithTimeout(context.TODO(), time.Second*50)
82 out := servs[i].GetBlocks(ctx, keys)
82 - gotten := make(map[u.Key]*blocks.Block)
83 + gotten := make(map[key.Key]*blocks.Block)
84 for blk := range out {
85 if _, ok := gotten[blk.Key()]; ok {
86 t.Fatal("Got duplicate block!")
blockservice/blockservice.go
+6 -5
@@ -10,6 +10,7 @@ import (
10 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
11 blocks "github.com/ipfs/go-ipfs/blocks"
12 "github.com/ipfs/go-ipfs/blocks/blockstore"
13 + key "github.com/ipfs/go-ipfs/blocks/key"
14 worker "github.com/ipfs/go-ipfs/blockservice/worker"
15 exchange "github.com/ipfs/go-ipfs/exchange"
16 u "github.com/ipfs/go-ipfs/util"
@@ -64,7 +65,7 @@ func New(bs blockstore.Blockstore, rem exchange.Interface) (*BlockService, error
65
66 // AddBlock adds a particular block to the service, Putting it into the datastore.
67 // TODO pass a context into this if the remote.HasBlock is going to remain here.
67 -func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
68 +func (s *BlockService) AddBlock(b *blocks.Block) (key.Key, error) {
69 k := b.Key()
70 err := s.Blockstore.Put(b)
71 if err != nil {
@@ -78,7 +79,7 @@ func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
79
80 // GetBlock retrieves a particular block from the service,
81 // Getting it from the datastore using the key (hash).
81 -func (s *BlockService) GetBlock(ctx context.Context, k u.Key) (*blocks.Block, error) {
82 +func (s *BlockService) GetBlock(ctx context.Context, k key.Key) (*blocks.Block, error) {
83 log.Debugf("BlockService GetBlock: '%s'", k)
84 block, err := s.Blockstore.Get(k)
85 if err == nil {
@@ -101,11 +102,11 @@ func (s *BlockService) GetBlock(ctx context.Context, k u.Key) (*blocks.Block, er
102 // GetBlocks gets a list of blocks asynchronously and returns through
103 // the returned channel.
104 // NB: No guarantees are made about order.
104 -func (s *BlockService) GetBlocks(ctx context.Context, ks []u.Key) <-chan *blocks.Block {
105 +func (s *BlockService) GetBlocks(ctx context.Context, ks []key.Key) <-chan *blocks.Block {
106 out := make(chan *blocks.Block, 0)
107 go func() {
108 defer close(out)
108 - var misses []u.Key
109 + var misses []key.Key
110 for _, k := range ks {
111 hit, err := s.Blockstore.Get(k)
112 if err != nil {
@@ -138,7 +139,7 @@ func (s *BlockService) GetBlocks(ctx context.Context, ks []u.Key) <-chan *blocks
139 }
140
141 // DeleteBlock deletes a block in the blockservice from the datastore
141 -func (s *BlockService) DeleteBlock(k u.Key) error {
142 +func (s *BlockService) DeleteBlock(k key.Key) error {
143 return s.Blockstore.DeleteBlock(k)
144 }
145
blockservice/worker/worker.go
+4 -3
@@ -9,6 +9,7 @@ import (
9 process "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
10 ratelimit "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/ratelimit"
11 blocks "github.com/ipfs/go-ipfs/blocks"
12 + key "github.com/ipfs/go-ipfs/blocks/key"
13 exchange "github.com/ipfs/go-ipfs/exchange"
14 waitable "github.com/ipfs/go-ipfs/thirdparty/waitable"
15 util "github.com/ipfs/go-ipfs/util"
@@ -141,12 +142,12 @@ func (w *Worker) start(c Config) {
142
143 type BlockList struct {
144 list list.List
144 - uniques map[util.Key]*list.Element
145 + uniques map[key.Key]*list.Element
146 }
147
148 func (s *BlockList) PushFront(b *blocks.Block) {
149 if s.uniques == nil {
149 - s.uniques = make(map[util.Key]*list.Element)
150 + s.uniques = make(map[key.Key]*list.Element)
151 }
152 _, ok := s.uniques[b.Key()]
153 if !ok {
@@ -157,7 +158,7 @@ func (s *BlockList) PushFront(b *blocks.Block) {
158
159 func (s *BlockList) Push(b *blocks.Block) {
160 if s.uniques == nil {
160 - s.uniques = make(map[util.Key]*list.Element)
161 + s.uniques = make(map[key.Key]*list.Element)
162 }
163 _, ok := s.uniques[b.Key()]
164 if !ok {
cmd/ipfs/init.go
+2 -2
@@ -10,6 +10,7 @@ import (
10
11 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
12 assets "github.com/ipfs/go-ipfs/assets"
13 + key "github.com/ipfs/go-ipfs/blocks/key"
14 cmds "github.com/ipfs/go-ipfs/commands"
15 core "github.com/ipfs/go-ipfs/core"
16 coreunix "github.com/ipfs/go-ipfs/core/coreunix"
@@ -17,7 +18,6 @@ import (
18 config "github.com/ipfs/go-ipfs/repo/config"
19 fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
20 uio "github.com/ipfs/go-ipfs/unixfs/io"
20 - u "github.com/ipfs/go-ipfs/util"
21 )
22
23 const nBitsForKeypairDefault = 2048
@@ -177,7 +177,7 @@ func addDefaultAssets(out io.Writer, repoRoot string) error {
177 return err
178 }
179
180 - k := u.B58KeyDecode(s)
180 + k := key.B58KeyDecode(s)
181 if err := dirb.AddChild(fname, k); err != nil {
182 return err
183 }
core/commands/block.go
+5 -4
@@ -11,6 +11,7 @@ import (
11 mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
12 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
13 "github.com/ipfs/go-ipfs/blocks"
14 + key "github.com/ipfs/go-ipfs/blocks/key"
15 cmds "github.com/ipfs/go-ipfs/commands"
16 u "github.com/ipfs/go-ipfs/util"
17 )
@@ -161,22 +162,22 @@ It reads from stdin, and <key> is a base58 encoded multihash.
162 Type: BlockStat{},
163 }
164
164 -func getBlockForKey(req cmds.Request, key string) (*blocks.Block, error) {
165 +func getBlockForKey(req cmds.Request, skey string) (*blocks.Block, error) {
166 n, err := req.Context().GetNode()
167 if err != nil {
168 return nil, err
169 }
170
170 - if !u.IsValidHash(key) {
171 + if !u.IsValidHash(skey) {
172 return nil, errors.New("Not a valid hash")
173 }
174
174 - h, err := mh.FromB58String(key)
175 + h, err := mh.FromB58String(skey)
176 if err != nil {
177 return nil, err
178 }
179
179 - k := u.Key(h)
180 + k := key.Key(h)
181 b, err := n.Blocks.GetBlock(context.TODO(), k)
182 if err != nil {
183 return nil, err
core/commands/dht.go
+5 -4
@@ -7,6 +7,7 @@ import (
7 "io"
8 "time"
9
10 + key "github.com/ipfs/go-ipfs/blocks/key"
11 cmds "github.com/ipfs/go-ipfs/commands"
12 notif "github.com/ipfs/go-ipfs/notifications"
13 peer "github.com/ipfs/go-ipfs/p2p/peer"
@@ -59,7 +60,7 @@ var queryDhtCmd = &cmds.Command{
60 events := make(chan *notif.QueryEvent)
61 ctx := notif.RegisterForQueryEvents(req.Context().Context, events)
62
62 - closestPeers, err := dht.GetClosestPeers(ctx, u.Key(req.Arguments()[0]))
63 + closestPeers, err := dht.GetClosestPeers(ctx, key.Key(req.Arguments()[0]))
64 if err != nil {
65 res.SetError(err, cmds.ErrNormal)
66 return
@@ -171,7 +172,7 @@ FindProviders will return a list of peers who are able to provide the value requ
172 events := make(chan *notif.QueryEvent)
173 ctx := notif.RegisterForQueryEvents(req.Context().Context, events)
174
174 - pchan := dht.FindProvidersAsync(ctx, u.B58KeyDecode(req.Arguments()[0]), numProviders)
175 + pchan := dht.FindProvidersAsync(ctx, key.B58KeyDecode(req.Arguments()[0]), numProviders)
176 go func() {
177 defer close(outChan)
178 for e := range events {
@@ -401,7 +402,7 @@ GetValue will return the value stored in the dht at the given key.
402
403 go func() {
404 defer close(events)
404 - val, err := dht.GetValue(ctx, u.B58KeyDecode(req.Arguments()[0]))
405 + val, err := dht.GetValue(ctx, key.B58KeyDecode(req.Arguments()[0]))
406 if err != nil {
407 notif.PublishQueryEvent(ctx, &notif.QueryEvent{
408 Type: notif.QueryError,
@@ -500,7 +501,7 @@ PutValue will store the given key value pair in the dht.
501 events := make(chan *notif.QueryEvent)
502 ctx := notif.RegisterForQueryEvents(req.Context().Context, events)
503
503 - key := u.B58KeyDecode(req.Arguments()[0])
504 + key := key.B58KeyDecode(req.Arguments()[0])
505 data := req.Arguments()[1]
506
507 go func() {
core/commands/pin.go
+2 -1
@@ -5,6 +5,7 @@ import (
5 "fmt"
6 "io"
7
8 + key "github.com/ipfs/go-ipfs/blocks/key"
9 cmds "github.com/ipfs/go-ipfs/commands"
10 corerepo "github.com/ipfs/go-ipfs/core/corerepo"
11 u "github.com/ipfs/go-ipfs/util"
@@ -23,7 +24,7 @@ var PinCmd = &cmds.Command{
24 }
25
26 type PinOutput struct {
26 - Pinned []u.Key
27 + Pinned []key.Key
28 }
29
30 var addPinCmd = &cmds.Command{
core/commands/publish.go
+2 -2
@@ -8,11 +8,11 @@ import (
8
9 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10
11 + key "github.com/ipfs/go-ipfs/blocks/key"
12 cmds "github.com/ipfs/go-ipfs/commands"
13 core "github.com/ipfs/go-ipfs/core"
14 crypto "github.com/ipfs/go-ipfs/p2p/crypto"
15 path "github.com/ipfs/go-ipfs/path"
15 - u "github.com/ipfs/go-ipfs/util"
16 )
17
18 var errNotOnline = errors.New("This command must be run in online mode. Try running 'ipfs daemon' first.")
@@ -128,7 +128,7 @@ func publish(ctx context.Context, n *core.IpfsNode, k crypto.PrivKey, ref path.P
128 }
129
130 return &IpnsEntry{
131 - Name: u.Key(hash).String(),
131 + Name: key.Key(hash).String(),
132 Value: ref.String(),
133 }, nil
134 }
core/commands/refs.go
+8 -7
@@ -8,6 +8,7 @@ import (
8 "strings"
9
10 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
11 + key "github.com/ipfs/go-ipfs/blocks/key"
12 cmds "github.com/ipfs/go-ipfs/commands"
13 "github.com/ipfs/go-ipfs/core"
14 dag "github.com/ipfs/go-ipfs/merkledag"
@@ -17,7 +18,7 @@ import (
18
19 // KeyList is a general type for outputting lists of keys
20 type KeyList struct {
20 - Keys []u.Key
21 + Keys []key.Key
22 }
23
24 // KeyListTextMarshaler outputs a KeyList as plaintext, one key per line
@@ -214,7 +215,7 @@ type RefWriter struct {
215 PrintEdge bool
216 PrintFmt string
217
217 - seen map[u.Key]struct{}
218 + seen map[key.Key]struct{}
219 }
220
221 // WriteRefs writes refs of the given object to the underlying writer.
@@ -238,7 +239,7 @@ func (rw *RefWriter) writeRefsRecursive(n *dag.Node) (int, error) {
239
240 var count int
241 for i, ng := range rw.DAG.GetDAG(rw.Ctx, n) {
241 - lk := u.Key(n.Links[i].Hash)
242 + lk := key.Key(n.Links[i].Hash)
243 if rw.skip(lk) {
244 continue
245 }
@@ -273,7 +274,7 @@ func (rw *RefWriter) writeRefsSingle(n *dag.Node) (int, error) {
274
275 count := 0
276 for _, l := range n.Links {
276 - lk := u.Key(l.Hash)
277 + lk := key.Key(l.Hash)
278
279 if rw.skip(lk) {
280 continue
@@ -288,13 +289,13 @@ func (rw *RefWriter) writeRefsSingle(n *dag.Node) (int, error) {
289 }
290
291 // skip returns whether to skip a key
291 -func (rw *RefWriter) skip(k u.Key) bool {
292 +func (rw *RefWriter) skip(k key.Key) bool {
293 if !rw.Unique {
294 return false
295 }
296
297 if rw.seen == nil {
297 - rw.seen = make(map[u.Key]struct{})
298 + rw.seen = make(map[key.Key]struct{})
299 }
300
301 _, found := rw.seen[k]
@@ -305,7 +306,7 @@ func (rw *RefWriter) skip(k u.Key) bool {
306 }
307
308 // Write one edge
308 -func (rw *RefWriter) WriteEdge(from, to u.Key, linkname string) error {
309 +func (rw *RefWriter) WriteEdge(from, to key.Key, linkname string) error {
310 if rw.Ctx != nil {
311 select {
312 case <-rw.Ctx.Done(): // just in case.
core/corehttp/gateway_handler.go
+3 -3
@@ -12,6 +12,7 @@ import (
12
13 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
14
15 + key "github.com/ipfs/go-ipfs/blocks/key"
16 core "github.com/ipfs/go-ipfs/core"
17 "github.com/ipfs/go-ipfs/importer"
18 chunk "github.com/ipfs/go-ipfs/importer/chunk"
@@ -19,7 +20,6 @@ import (
20 path "github.com/ipfs/go-ipfs/path"
21 "github.com/ipfs/go-ipfs/routing"
22 uio "github.com/ipfs/go-ipfs/unixfs/io"
22 - u "github.com/ipfs/go-ipfs/util"
23 )
24
25 const (
@@ -304,7 +304,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
304 tctx, cancel := context.WithTimeout(ctx, time.Minute)
305 defer cancel()
306 // TODO(cryptix): could this be core.Resolve() too?
307 - rootnd, err := i.node.Resolver.DAG.Get(tctx, u.Key(h))
307 + rootnd, err := i.node.Resolver.DAG.Get(tctx, key.Key(h))
308 if err != nil {
309 webError(w, "Could not resolve root object", err, http.StatusBadRequest)
310 return
@@ -374,7 +374,7 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
374
375 tctx, cancel := context.WithTimeout(ctx, time.Minute)
376 defer cancel()
377 - rootnd, err := i.node.Resolver.DAG.Get(tctx, u.Key(h))
377 + rootnd, err := i.node.Resolver.DAG.Get(tctx, key.Key(h))
378 if err != nil {
379 webError(w, "Could not resolve root object", err, http.StatusBadRequest)
380 return
core/corerepo/gc.go
+2 -2
@@ -2,8 +2,8 @@ package corerepo
2
3 import (
4 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
5 + key "github.com/ipfs/go-ipfs/blocks/key"
6 "github.com/ipfs/go-ipfs/core"
6 - u "github.com/ipfs/go-ipfs/util"
7
8 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
9 )
@@ -11,7 +11,7 @@ import (
11 var log = eventlog.Logger("corerepo")
12
13 type KeyRemoved struct {
14 - Key u.Key
14 + Key key.Key
15 }
16
17 func GarbageCollect(n *core.IpfsNode, ctx context.Context) error {
core/corerepo/pinning.go
+5 -5
@@ -19,13 +19,13 @@ import (
19
20 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
21
22 + key "github.com/ipfs/go-ipfs/blocks/key"
23 "github.com/ipfs/go-ipfs/core"
24 "github.com/ipfs/go-ipfs/merkledag"
25 path "github.com/ipfs/go-ipfs/path"
25 - u "github.com/ipfs/go-ipfs/util"
26 )
27
28 -func Pin(n *core.IpfsNode, paths []string, recursive bool) ([]u.Key, error) {
28 +func Pin(n *core.IpfsNode, paths []string, recursive bool) ([]key.Key, error) {
29 // TODO(cryptix): do we want a ctx as first param for (Un)Pin() as well, just like core.Resolve?
30 ctx := n.Context()
31
@@ -38,7 +38,7 @@ func Pin(n *core.IpfsNode, paths []string, recursive bool) ([]u.Key, error) {
38 dagnodes = append(dagnodes, dagnode)
39 }
40
41 - var out []u.Key
41 + var out []key.Key
42 for _, dagnode := range dagnodes {
43 k, err := dagnode.Key()
44 if err != nil {
@@ -62,7 +62,7 @@ func Pin(n *core.IpfsNode, paths []string, recursive bool) ([]u.Key, error) {
62 return out, nil
63 }
64
65 -func Unpin(n *core.IpfsNode, paths []string, recursive bool) ([]u.Key, error) {
65 +func Unpin(n *core.IpfsNode, paths []string, recursive bool) ([]key.Key, error) {
66 // TODO(cryptix): do we want a ctx as first param for (Un)Pin() as well, just like core.Resolve?
67 ctx := n.Context()
68
@@ -75,7 +75,7 @@ func Unpin(n *core.IpfsNode, paths []string, recursive bool) ([]u.Key, error) {
75 dagnodes = append(dagnodes, dagnode)
76 }
77
78 - var unpinned []u.Key
78 + var unpinned []key.Key
79 for _, dagnode := range dagnodes {
80 k, _ := dagnode.Key()
81
core/coreunix/metadata.go
+5 -5
@@ -5,14 +5,14 @@ import (
5
6 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
7
8 + key "github.com/ipfs/go-ipfs/blocks/key"
9 core "github.com/ipfs/go-ipfs/core"
10 dag "github.com/ipfs/go-ipfs/merkledag"
11 ft "github.com/ipfs/go-ipfs/unixfs"
11 - u "github.com/ipfs/go-ipfs/util"
12 )
13
14 -func AddMetadataTo(n *core.IpfsNode, key string, m *ft.Metadata) (string, error) {
15 - ukey := u.B58KeyDecode(key)
14 +func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error) {
15 + ukey := key.B58KeyDecode(skey)
16
17 ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
18 defer cancel()
@@ -41,8 +41,8 @@ func AddMetadataTo(n *core.IpfsNode, key string, m *ft.Metadata) (string, error)
41 return nk.B58String(), nil
42 }
43
44 -func Metadata(n *core.IpfsNode, key string) (*ft.Metadata, error) {
45 - ukey := u.B58KeyDecode(key)
44 +func Metadata(n *core.IpfsNode, skey string) (*ft.Metadata, error) {
45 + ukey := key.B58KeyDecode(skey)
46
47 ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
48 defer cancel()
core/coreunix/metadata_test.go
+2 -1
@@ -10,6 +10,7 @@ import (
10 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
11
12 bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
13 + key "github.com/ipfs/go-ipfs/blocks/key"
14 bserv "github.com/ipfs/go-ipfs/blockservice"
15 core "github.com/ipfs/go-ipfs/core"
16 offline "github.com/ipfs/go-ipfs/exchange/offline"
@@ -66,7 +67,7 @@ func TestMetadata(t *testing.T) {
67 t.Fatalf("something went wrong in conversion: '%s' != '%s'", rec.MimeType, m.MimeType)
68 }
69
69 - retnode, err := ds.Get(context.Background(), u.B58KeyDecode(mdk))
70 + retnode, err := ds.Get(context.Background(), key.B58KeyDecode(mdk))
71 if err != nil {
72 t.Fatal(err)
73 }
exchange/bitswap/bitswap.go
+13 -13
@@ -12,6 +12,7 @@ import (
12 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
13 blocks "github.com/ipfs/go-ipfs/blocks"
14 blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
15 + key "github.com/ipfs/go-ipfs/blocks/key"
16 exchange "github.com/ipfs/go-ipfs/exchange"
17 decision "github.com/ipfs/go-ipfs/exchange/bitswap/decision"
18 bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
@@ -21,7 +22,6 @@ import (
22 peer "github.com/ipfs/go-ipfs/p2p/peer"
23 "github.com/ipfs/go-ipfs/thirdparty/delay"
24 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
24 - u "github.com/ipfs/go-ipfs/util"
25 )
26
27 var log = eventlog.Logger("bitswap")
@@ -85,7 +85,7 @@ func New(parent context.Context, p peer.ID, network bsnet.BitSwapNetwork,
85 findKeys: make(chan *blockRequest, sizeBatchRequestChan),
86 process: px,
87 newBlocks: make(chan *blocks.Block, HasBlockBufferSize),
88 - provideKeys: make(chan u.Key),
88 + provideKeys: make(chan key.Key),
89 wm: NewWantManager(ctx, network),
90 }
91 go bs.wm.Run()
@@ -124,7 +124,7 @@ type Bitswap struct {
124
125 newBlocks chan *blocks.Block
126
127 - provideKeys chan u.Key
127 + provideKeys chan key.Key
128
129 counterLk sync.Mutex
130 blocksRecvd int
@@ -132,13 +132,13 @@ type Bitswap struct {
132 }
133
134 type blockRequest struct {
135 - keys []u.Key
135 + keys []key.Key
136 ctx context.Context
137 }
138
139 // GetBlock attempts to retrieve a particular block from peers within the
140 // deadline enforced by the context.
141 -func (bs *Bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, error) {
141 +func (bs *Bitswap) GetBlock(parent context.Context, k key.Key) (*blocks.Block, error) {
142
143 // Any async work initiated by this function must end when this function
144 // returns. To ensure this, derive a new context. Note that it is okay to
@@ -156,7 +156,7 @@ func (bs *Bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, err
156 cancelFunc()
157 }()
158
159 - promise, err := bs.GetBlocks(ctx, []u.Key{k})
159 + promise, err := bs.GetBlocks(ctx, []key.Key{k})
160 if err != nil {
161 return nil, err
162 }
@@ -177,8 +177,8 @@ func (bs *Bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, err
177 }
178 }
179
180 -func (bs *Bitswap) WantlistForPeer(p peer.ID) []u.Key {
181 - var out []u.Key
180 +func (bs *Bitswap) WantlistForPeer(p peer.ID) []key.Key {
181 + var out []key.Key
182 for _, e := range bs.engine.WantlistForPeer(p) {
183 out = append(out, e.Key)
184 }
@@ -192,7 +192,7 @@ func (bs *Bitswap) WantlistForPeer(p peer.ID) []u.Key {
192 // NB: Your request remains open until the context expires. To conserve
193 // resources, provide a context with a reasonably short deadline (ie. not one
194 // that lasts throughout the lifetime of the server)
195 -func (bs *Bitswap) GetBlocks(ctx context.Context, keys []u.Key) (<-chan *blocks.Block, error) {
195 +func (bs *Bitswap) GetBlocks(ctx context.Context, keys []key.Key) (<-chan *blocks.Block, error) {
196 select {
197 case <-bs.process.Closing():
198 return nil, errors.New("bitswap is closed")
@@ -246,7 +246,7 @@ func (bs *Bitswap) connectToProviders(ctx context.Context, entries []wantlist.En
246 wg := sync.WaitGroup{}
247 for _, e := range entries {
248 wg.Add(1)
249 - go func(k u.Key) {
249 + go func(k key.Key) {
250 defer wg.Done()
251
252 child, cancel := context.WithTimeout(ctx, providerRequestTimeout)
@@ -277,7 +277,7 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
277 }
278
279 // quickly send out cancels, reduces chances of duplicate block receives
280 - var keys []u.Key
280 + var keys []key.Key
281 for _, block := range iblocks {
282 if _, found := bs.wm.wl.Contains(block.Key()); !found {
283 log.Notice("received un-asked-for block: %s", block)
@@ -342,8 +342,8 @@ func (bs *Bitswap) Close() error {
342 return bs.process.Close()
343 }
344
345 -func (bs *Bitswap) GetWantlist() []u.Key {
346 - var out []u.Key
345 +func (bs *Bitswap) GetWantlist() []key.Key {
346 + var out []key.Key
347 for _, e := range bs.wm.wl.Entries() {
348 out = append(out, e.Key)
349 }
exchange/bitswap/bitswap_test.go
+3 -3
@@ -12,11 +12,11 @@ import (
12
13 blocks "github.com/ipfs/go-ipfs/blocks"
14 blocksutil "github.com/ipfs/go-ipfs/blocks/blocksutil"
15 + key "github.com/ipfs/go-ipfs/blocks/key"
16 tn "github.com/ipfs/go-ipfs/exchange/bitswap/testnet"
17 p2ptestutil "github.com/ipfs/go-ipfs/p2p/test/util"
18 mockrouting "github.com/ipfs/go-ipfs/routing/mock"
19 delay "github.com/ipfs/go-ipfs/thirdparty/delay"
19 - u "github.com/ipfs/go-ipfs/util"
20 )
21
22 // FIXME the tests are really sensitive to the network delay. fix them to work
@@ -155,7 +155,7 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
155
156 t.Log("Give the blocks to the first instance")
157
158 - var blkeys []u.Key
158 + var blkeys []key.Key
159 first := instances[0]
160 for _, b := range blocks {
161 blkeys = append(blkeys, b.Key())
@@ -227,7 +227,7 @@ func TestSendToWantingPeer(t *testing.T) {
227 alpha := bg.Next()
228 // peerA requests and waits for block alpha
229 ctx, _ := context.WithTimeout(context.TODO(), waitTime)
230 - alphaPromise, err := peerA.Exchange.GetBlocks(ctx, []u.Key{alpha.Key()})
230 + alphaPromise, err := peerA.Exchange.GetBlocks(ctx, []key.Key{alpha.Key()})
231 if err != nil {
232 t.Fatal(err)
233 }
exchange/bitswap/decision/bench_test.go
+2 -2
@@ -4,9 +4,9 @@ import (
4 "math"
5 "testing"
6
7 + key "github.com/ipfs/go-ipfs/blocks/key"
8 "github.com/ipfs/go-ipfs/exchange/bitswap/wantlist"
9 "github.com/ipfs/go-ipfs/p2p/peer"
9 - "github.com/ipfs/go-ipfs/util"
10 "github.com/ipfs/go-ipfs/util/testutil"
11 )
12
@@ -21,6 +21,6 @@ func BenchmarkTaskQueuePush(b *testing.B) {
21 }
22 b.ResetTimer()
23 for i := 0; i < b.N; i++ {
24 - q.Push(wantlist.Entry{Key: util.Key(i), Priority: math.MaxInt32}, peers[i%len(peers)])
24 + q.Push(wantlist.Entry{Key: key.Key(i), Priority: math.MaxInt32}, peers[i%len(peers)])
25 }
26 }
exchange/bitswap/decision/ledger.go
+7 -7
@@ -3,20 +3,20 @@ package decision
3 import (
4 "time"
5
6 + key "github.com/ipfs/go-ipfs/blocks/key"
7 wl "github.com/ipfs/go-ipfs/exchange/bitswap/wantlist"
8 peer "github.com/ipfs/go-ipfs/p2p/peer"
8 - u "github.com/ipfs/go-ipfs/util"
9 )
10
11 // keySet is just a convenient alias for maps of keys, where we only care
12 // access/lookups.
13 -type keySet map[u.Key]struct{}
13 +type keySet map[key.Key]struct{}
14
15 func newLedger(p peer.ID) *ledger {
16 return &ledger{
17 wantList: wl.New(),
18 Partner: p,
19 - sentToPeer: make(map[u.Key]time.Time),
19 + sentToPeer: make(map[key.Key]time.Time),
20 }
21 }
22
@@ -43,7 +43,7 @@ type ledger struct {
43
44 // sentToPeer is a set of keys to ensure we dont send duplicate blocks
45 // to a given peer
46 - sentToPeer map[u.Key]time.Time
46 + sentToPeer map[key.Key]time.Time
47 }
48
49 type debtRatio struct {
@@ -68,16 +68,16 @@ func (l *ledger) ReceivedBytes(n int) {
68 }
69
70 // TODO: this needs to be different. We need timeouts.
71 -func (l *ledger) Wants(k u.Key, priority int) {
71 +func (l *ledger) Wants(k key.Key, priority int) {
72 log.Debugf("peer %s wants %s", l.Partner, k)
73 l.wantList.Add(k, priority)
74 }
75
76 -func (l *ledger) CancelWant(k u.Key) {
76 +func (l *ledger) CancelWant(k key.Key) {
77 l.wantList.Remove(k)
78 }
79
80 -func (l *ledger) WantListContains(k u.Key) (wl.Entry, bool) {
80 +func (l *ledger) WantListContains(k key.Key) (wl.Entry, bool) {
81 return l.wantList.Contains(k)
82 }
83
exchange/bitswap/decision/peer_request_queue.go
+8 -8
@@ -4,17 +4,17 @@ import (
4 "sync"
5 "time"
6
7 + key "github.com/ipfs/go-ipfs/blocks/key"
8 wantlist "github.com/ipfs/go-ipfs/exchange/bitswap/wantlist"
9 peer "github.com/ipfs/go-ipfs/p2p/peer"
10 pq "github.com/ipfs/go-ipfs/thirdparty/pq"
10 - u "github.com/ipfs/go-ipfs/util"
11 )
12
13 type peerRequestQueue interface {
14 // Pop returns the next peerRequestTask. Returns nil if the peerRequestQueue is empty.
15 Pop() *peerRequestTask
16 Push(entry wantlist.Entry, to peer.ID)
17 - Remove(k u.Key, p peer.ID)
17 + Remove(k key.Key, p peer.ID)
18 // NB: cannot expose simply expose taskQueue.Len because trashed elements
19 // may exist. These trashed elements should not contribute to the count.
20 }
@@ -110,7 +110,7 @@ func (tl *prq) Pop() *peerRequestTask {
110 }
111
112 // Remove removes a task from the queue
113 -func (tl *prq) Remove(k u.Key, p peer.ID) {
113 +func (tl *prq) Remove(k key.Key, p peer.ID) {
114 tl.lock.Lock()
115 t, ok := tl.taskMap[taskKey(p, k)]
116 if ok {
@@ -155,7 +155,7 @@ func (t *peerRequestTask) SetIndex(i int) {
155 }
156
157 // taskKey returns a key that uniquely identifies a task.
158 -func taskKey(p peer.ID, k u.Key) string {
158 +func taskKey(p peer.ID, k key.Key) string {
159 return string(p) + string(k)
160 }
161
@@ -186,7 +186,7 @@ type activePartner struct {
186 activelk sync.Mutex
187 active int
188
189 - activeBlocks map[u.Key]struct{}
189 + activeBlocks map[key.Key]struct{}
190
191 // requests is the number of blocks this peer is currently requesting
192 // request need not be locked around as it will only be modified under
@@ -203,7 +203,7 @@ type activePartner struct {
203 func newActivePartner() *activePartner {
204 return &activePartner{
205 taskQueue: pq.New(wrapCmp(V1)),
206 - activeBlocks: make(map[u.Key]struct{}),
206 + activeBlocks: make(map[key.Key]struct{}),
207 }
208 }
209
@@ -230,7 +230,7 @@ func partnerCompare(a, b pq.Elem) bool {
230 }
231
232 // StartTask signals that a task was started for this partner
233 -func (p *activePartner) StartTask(k u.Key) {
233 +func (p *activePartner) StartTask(k key.Key) {
234 p.activelk.Lock()
235 p.activeBlocks[k] = struct{}{}
236 p.active++
@@ -238,7 +238,7 @@ func (p *activePartner) StartTask(k u.Key) {
238 }
239
240 // TaskDone signals that a task was completed for this partner
241 -func (p *activePartner) TaskDone(k u.Key) {
241 +func (p *activePartner) TaskDone(k key.Key) {
242 p.activelk.Lock()
243 delete(p.activeBlocks, k)
244 p.active--
exchange/bitswap/decision/peer_request_queue_test.go
+7 -7
@@ -7,8 +7,8 @@ import (
7 "strings"
8 "testing"
9
10 + key "github.com/ipfs/go-ipfs/blocks/key"
11 "github.com/ipfs/go-ipfs/exchange/bitswap/wantlist"
11 - "github.com/ipfs/go-ipfs/util"
12 "github.com/ipfs/go-ipfs/util/testutil"
13 )
14
@@ -41,10 +41,10 @@ func TestPushPop(t *testing.T) {
41 for _, index := range rand.Perm(len(alphabet)) { // add blocks for all letters
42 letter := alphabet[index]
43 t.Log(partner.String())
44 - prq.Push(wantlist.Entry{Key: util.Key(letter), Priority: math.MaxInt32 - index}, partner)
44 + prq.Push(wantlist.Entry{Key: key.Key(letter), Priority: math.MaxInt32 - index}, partner)
45 }
46 for _, consonant := range consonants {
47 - prq.Remove(util.Key(consonant), partner)
47 + prq.Remove(key.Key(consonant), partner)
48 }
49
50 var out []string
@@ -76,10 +76,10 @@ func TestPeerRepeats(t *testing.T) {
76 // Have each push some blocks
77
78 for i := 0; i < 5; i++ {
79 - prq.Push(wantlist.Entry{Key: util.Key(i)}, a)
80 - prq.Push(wantlist.Entry{Key: util.Key(i)}, b)
81 - prq.Push(wantlist.Entry{Key: util.Key(i)}, c)
82 - prq.Push(wantlist.Entry{Key: util.Key(i)}, d)
79 + prq.Push(wantlist.Entry{Key: key.Key(i)}, a)
80 + prq.Push(wantlist.Entry{Key: key.Key(i)}, b)
81 + prq.Push(wantlist.Entry{Key: key.Key(i)}, c)
82 + prq.Push(wantlist.Entry{Key: key.Key(i)}, d)
83 }
84
85 // now, pop off four entries, there should be one from each
exchange/bitswap/message/message.go
+11 -11
@@ -4,10 +4,10 @@ import (
4 "io"
5
6 blocks "github.com/ipfs/go-ipfs/blocks"
7 + key "github.com/ipfs/go-ipfs/blocks/key"
8 pb "github.com/ipfs/go-ipfs/exchange/bitswap/message/internal/pb"
9 wantlist "github.com/ipfs/go-ipfs/exchange/bitswap/wantlist"
10 inet "github.com/ipfs/go-ipfs/p2p/net"
10 - u "github.com/ipfs/go-ipfs/util"
11
12 ggio "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/io"
13 proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
@@ -25,9 +25,9 @@ type BitSwapMessage interface {
25 Blocks() []*blocks.Block
26
27 // AddEntry adds an entry to the Wantlist.
28 - AddEntry(key u.Key, priority int)
28 + AddEntry(key key.Key, priority int)
29
30 - Cancel(key u.Key)
30 + Cancel(key key.Key)
31
32 Empty() bool
33
@@ -47,8 +47,8 @@ type Exportable interface {
47
48 type impl struct {
49 full bool
50 - wantlist map[u.Key]Entry
51 - blocks map[u.Key]*blocks.Block
50 + wantlist map[key.Key]Entry
51 + blocks map[key.Key]*blocks.Block
52 }
53
54 func New(full bool) BitSwapMessage {
@@ -57,8 +57,8 @@ func New(full bool) BitSwapMessage {
57
58 func newMsg(full bool) *impl {
59 return &impl{
60 - blocks: make(map[u.Key]*blocks.Block),
61 - wantlist: make(map[u.Key]Entry),
60 + blocks: make(map[key.Key]*blocks.Block),
61 + wantlist: make(map[key.Key]Entry),
62 full: full,
63 }
64 }
@@ -71,7 +71,7 @@ type Entry struct {
71 func newMessageFromProto(pbm pb.Message) BitSwapMessage {
72 m := newMsg(pbm.GetWantlist().GetFull())
73 for _, e := range pbm.GetWantlist().GetEntries() {
74 - m.addEntry(u.Key(e.GetBlock()), int(e.GetPriority()), e.GetCancel())
74 + m.addEntry(key.Key(e.GetBlock()), int(e.GetPriority()), e.GetCancel())
75 }
76 for _, d := range pbm.GetBlocks() {
77 b := blocks.NewBlock(d)
@@ -104,16 +104,16 @@ func (m *impl) Blocks() []*blocks.Block {
104 return bs
105 }
106
107 -func (m *impl) Cancel(k u.Key) {
107 +func (m *impl) Cancel(k key.Key) {
108 delete(m.wantlist, k)
109 m.addEntry(k, 0, true)
110 }
111
112 -func (m *impl) AddEntry(k u.Key, priority int) {
112 +func (m *impl) AddEntry(k key.Key, priority int) {
113 m.addEntry(k, priority, false)
114 }
115
116 -func (m *impl) addEntry(k u.Key, priority int, cancel bool) {
116 +func (m *impl) addEntry(k key.Key, priority int, cancel bool) {
117 e, exists := m.wantlist[k]
118 if exists {
119 e.Priority = priority
exchange/bitswap/message/message_test.go
+11 -11
@@ -7,14 +7,14 @@ import (
7 proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
8
9 blocks "github.com/ipfs/go-ipfs/blocks"
10 + key "github.com/ipfs/go-ipfs/blocks/key"
11 pb "github.com/ipfs/go-ipfs/exchange/bitswap/message/internal/pb"
11 - u "github.com/ipfs/go-ipfs/util"
12 )
13
14 func TestAppendWanted(t *testing.T) {
15 const str = "foo"
16 m := New(true)
17 - m.AddEntry(u.Key(str), 1)
17 + m.AddEntry(key.Key(str), 1)
18
19 if !wantlistContains(m.ToProto().GetWantlist(), str) {
20 t.Fail()
@@ -63,7 +63,7 @@ func TestWantlist(t *testing.T) {
63 keystrs := []string{"foo", "bar", "baz", "bat"}
64 m := New(true)
65 for _, s := range keystrs {
66 - m.AddEntry(u.Key(s), 1)
66 + m.AddEntry(key.Key(s), 1)
67 }
68 exported := m.Wantlist()
69
@@ -86,7 +86,7 @@ func TestCopyProtoByValue(t *testing.T) {
86 const str = "foo"
87 m := New(true)
88 protoBeforeAppend := m.ToProto()
89 - m.AddEntry(u.Key(str), 1)
89 + m.AddEntry(key.Key(str), 1)
90 if wantlistContains(protoBeforeAppend.GetWantlist(), str) {
91 t.Fail()
92 }
@@ -94,11 +94,11 @@ func TestCopyProtoByValue(t *testing.T) {
94
95 func TestToNetFromNetPreservesWantList(t *testing.T) {
96 original := New(true)
97 - original.AddEntry(u.Key("M"), 1)
98 - original.AddEntry(u.Key("B"), 1)
99 - original.AddEntry(u.Key("D"), 1)
100 - original.AddEntry(u.Key("T"), 1)
101 - original.AddEntry(u.Key("F"), 1)
97 + original.AddEntry(key.Key("M"), 1)
98 + original.AddEntry(key.Key("B"), 1)
99 + original.AddEntry(key.Key("D"), 1)
100 + original.AddEntry(key.Key("T"), 1)
101 + original.AddEntry(key.Key("F"), 1)
102
103 buf := new(bytes.Buffer)
104 if err := original.ToNet(buf); err != nil {
@@ -110,7 +110,7 @@ func TestToNetFromNetPreservesWantList(t *testing.T) {
110 t.Fatal(err)
111 }
112
113 - keys := make(map[u.Key]bool)
113 + keys := make(map[key.Key]bool)
114 for _, k := range copied.Wantlist() {
115 keys[k.Key] = true
116 }
@@ -140,7 +140,7 @@ func TestToAndFromNetMessage(t *testing.T) {
140 t.Fatal(err)
141 }
142
143 - keys := make(map[u.Key]bool)
143 + keys := make(map[key.Key]bool)
144 for _, b := range m2.Blocks() {
145 keys[b.Key()] = true
146 }
exchange/bitswap/network/interface.go
+3 -3
@@ -2,10 +2,10 @@ package network
2
3 import (
4 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
5 + key "github.com/ipfs/go-ipfs/blocks/key"
6 bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
7 peer "github.com/ipfs/go-ipfs/p2p/peer"
8 protocol "github.com/ipfs/go-ipfs/p2p/protocol"
8 - u "github.com/ipfs/go-ipfs/util"
9 )
10
11 var ProtocolBitswap protocol.ID = "/ipfs/bitswap"
@@ -44,8 +44,8 @@ type Receiver interface {
44
45 type Routing interface {
46 // FindProvidersAsync returns a channel of providers for the given key
47 - FindProvidersAsync(context.Context, u.Key, int) <-chan peer.ID
47 + FindProvidersAsync(context.Context, key.Key, int) <-chan peer.ID
48
49 // Provide provides the key to the network
50 - Provide(context.Context, u.Key) error
50 + Provide(context.Context, key.Key) error
51 }
exchange/bitswap/network/ipfs_impl.go
+3 -3
@@ -3,13 +3,13 @@ package network
3 import (
4 ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
5 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
6 + key "github.com/ipfs/go-ipfs/blocks/key"
7 bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
8 host "github.com/ipfs/go-ipfs/p2p/host"
9 inet "github.com/ipfs/go-ipfs/p2p/net"
10 peer "github.com/ipfs/go-ipfs/p2p/peer"
11 routing "github.com/ipfs/go-ipfs/routing"
12 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
12 - util "github.com/ipfs/go-ipfs/util"
13 )
14
15 var log = eventlog.Logger("bitswap_network")
@@ -102,7 +102,7 @@ func (bsnet *impl) ConnectTo(ctx context.Context, p peer.ID) error {
102 }
103
104 // FindProvidersAsync returns a channel of providers for the given key
105 -func (bsnet *impl) FindProvidersAsync(ctx context.Context, k util.Key, max int) <-chan peer.ID {
105 +func (bsnet *impl) FindProvidersAsync(ctx context.Context, k key.Key, max int) <-chan peer.ID {
106
107 // Since routing queries are expensive, give bitswap the peers to which we
108 // have open connections. Note that this may cause issues if bitswap starts
@@ -138,7 +138,7 @@ func (bsnet *impl) FindProvidersAsync(ctx context.Context, k util.Key, max int)
138 }
139
140 // Provide provides the key to the network
141 -func (bsnet *impl) Provide(ctx context.Context, k util.Key) error {
141 +func (bsnet *impl) Provide(ctx context.Context, k key.Key) error {
142 return bsnet.routing.Provide(ctx, k)
143 }
144
exchange/bitswap/notifications/notifications.go
+4 -4
@@ -4,14 +4,14 @@ import (
4 pubsub "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/briantigerchow/pubsub"
5 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
6 blocks "github.com/ipfs/go-ipfs/blocks"
7 - u "github.com/ipfs/go-ipfs/util"
7 + key "github.com/ipfs/go-ipfs/blocks/key"
8 )
9
10 const bufferSize = 16
11
12 type PubSub interface {
13 Publish(block *blocks.Block)
14 - Subscribe(ctx context.Context, keys ...u.Key) <-chan *blocks.Block
14 + Subscribe(ctx context.Context, keys ...key.Key) <-chan *blocks.Block
15 Shutdown()
16 }
17
@@ -35,7 +35,7 @@ func (ps *impl) Shutdown() {
35 // Subscribe returns a channel of blocks for the given |keys|. |blockChannel|
36 // is closed if the |ctx| times out or is cancelled, or after sending len(keys)
37 // blocks.
38 -func (ps *impl) Subscribe(ctx context.Context, keys ...u.Key) <-chan *blocks.Block {
38 +func (ps *impl) Subscribe(ctx context.Context, keys ...key.Key) <-chan *blocks.Block {
39
40 blocksCh := make(chan *blocks.Block, len(keys))
41 valuesCh := make(chan interface{}, len(keys)) // provide our own channel to control buffer, prevent blocking
@@ -71,7 +71,7 @@ func (ps *impl) Subscribe(ctx context.Context, keys ...u.Key) <-chan *blocks.Blo
71 return blocksCh
72 }
73
74 -func toStrings(keys []u.Key) []string {
74 +func toStrings(keys []key.Key) []string {
75 strs := make([]string, 0)
76 for _, key := range keys {
77 strs = append(strs, string(key))
exchange/bitswap/notifications/notifications_test.go
+3 -3
@@ -8,7 +8,7 @@ import (
8 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
9 blocks "github.com/ipfs/go-ipfs/blocks"
10 blocksutil "github.com/ipfs/go-ipfs/blocks/blocksutil"
11 - "github.com/ipfs/go-ipfs/util"
11 + key "github.com/ipfs/go-ipfs/blocks/key"
12 )
13
14 func TestDuplicates(t *testing.T) {
@@ -131,8 +131,8 @@ func TestDoesNotDeadLockIfContextCancelledBeforePublish(t *testing.T) {
131
132 t.Log("generate a large number of blocks. exceed default buffer")
133 bs := g.Blocks(1000)
134 - ks := func() []util.Key {
135 - var keys []util.Key
134 + ks := func() []key.Key {
135 + var keys []key.Key
136 for _, b := range bs {
137 keys = append(keys, b.Key())
138 }
exchange/bitswap/stat.go
+2 -2
@@ -1,13 +1,13 @@
1 package bitswap
2
3 import (
4 - u "github.com/ipfs/go-ipfs/util"
4 + key "github.com/ipfs/go-ipfs/blocks/key"
5 "sort"
6 )
7
8 type Stat struct {
9 ProvideBufLen int
10 - Wantlist []u.Key
10 + Wantlist []key.Key
11 Peers []string
12 BlocksReceived int
13 DupBlksReceived int
exchange/bitswap/testnet/virtual.go
+3 -3
@@ -4,13 +4,13 @@ import (
4 "errors"
5
6 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
7 + key "github.com/ipfs/go-ipfs/blocks/key"
8 bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
9 bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
10 peer "github.com/ipfs/go-ipfs/p2p/peer"
11 routing "github.com/ipfs/go-ipfs/routing"
12 mockrouting "github.com/ipfs/go-ipfs/routing/mock"
13 delay "github.com/ipfs/go-ipfs/thirdparty/delay"
13 - util "github.com/ipfs/go-ipfs/util"
14 testutil "github.com/ipfs/go-ipfs/util/testutil"
15 )
16
@@ -91,7 +91,7 @@ func (nc *networkClient) SendMessage(
91 }
92
93 // FindProvidersAsync returns a channel of providers for the given key
94 -func (nc *networkClient) FindProvidersAsync(ctx context.Context, k util.Key, max int) <-chan peer.ID {
94 +func (nc *networkClient) FindProvidersAsync(ctx context.Context, k key.Key, max int) <-chan peer.ID {
95
96 // NB: this function duplicates the PeerInfo -> ID transformation in the
97 // bitswap network adapter. Not to worry. This network client will be
@@ -113,7 +113,7 @@ func (nc *networkClient) FindProvidersAsync(ctx context.Context, k util.Key, max
113 }
114
115 // Provide provides the key to the network
116 -func (nc *networkClient) Provide(ctx context.Context, k util.Key) error {
116 +func (nc *networkClient) Provide(ctx context.Context, k key.Key) error {
117 return nc.routing.Provide(ctx, k)
118 }
119
exchange/bitswap/wantlist/wantlist.go
+10 -10
@@ -3,7 +3,7 @@
3 package wantlist
4
5 import (
6 - u "github.com/ipfs/go-ipfs/util"
6 + key "github.com/ipfs/go-ipfs/blocks/key"
7 "sort"
8 "sync"
9 )
@@ -15,14 +15,14 @@ type ThreadSafe struct {
15
16 // not threadsafe
17 type Wantlist struct {
18 - set map[u.Key]Entry
18 + set map[key.Key]Entry
19 // TODO provide O(1) len accessor if cost becomes an issue
20 }
21
22 type Entry struct {
23 // TODO consider making entries immutable so they can be shared safely and
24 // slices can be copied efficiently.
25 - Key u.Key
25 + Key key.Key
26 Priority int
27 }
28
@@ -40,25 +40,25 @@ func NewThreadSafe() *ThreadSafe {
40
41 func New() *Wantlist {
42 return &Wantlist{
43 - set: make(map[u.Key]Entry),
43 + set: make(map[key.Key]Entry),
44 }
45 }
46
47 -func (w *ThreadSafe) Add(k u.Key, priority int) {
47 +func (w *ThreadSafe) Add(k key.Key, priority int) {
48 // TODO rm defer for perf
49 w.lk.Lock()
50 defer w.lk.Unlock()
51 w.Wantlist.Add(k, priority)
52 }
53
54 -func (w *ThreadSafe) Remove(k u.Key) {
54 +func (w *ThreadSafe) Remove(k key.Key) {
55 // TODO rm defer for perf
56 w.lk.Lock()
57 defer w.lk.Unlock()
58 w.Wantlist.Remove(k)
59 }
60
61 -func (w *ThreadSafe) Contains(k u.Key) (Entry, bool) {
61 +func (w *ThreadSafe) Contains(k key.Key) (Entry, bool) {
62 // TODO rm defer for perf
63 w.lk.RLock()
64 defer w.lk.RUnlock()
@@ -87,7 +87,7 @@ func (w *Wantlist) Len() int {
87 return len(w.set)
88 }
89
90 -func (w *Wantlist) Add(k u.Key, priority int) {
90 +func (w *Wantlist) Add(k key.Key, priority int) {
91 if _, ok := w.set[k]; ok {
92 return
93 }
@@ -97,11 +97,11 @@ func (w *Wantlist) Add(k u.Key, priority int) {
97 }
98 }
99
100 -func (w *Wantlist) Remove(k u.Key) {
100 +func (w *Wantlist) Remove(k key.Key) {
101 delete(w.set, k)
102 }
103
104 -func (w *Wantlist) Contains(k u.Key) (Entry, bool) {
104 +func (w *Wantlist) Contains(k key.Key) (Entry, bool) {
105 e, ok := w.set[k]
106 return e, ok
107 }
exchange/bitswap/wantmanager.go
+5 -5
@@ -5,12 +5,12 @@ import (
5 "time"
6
7 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
8 + key "github.com/ipfs/go-ipfs/blocks/key"
9 engine "github.com/ipfs/go-ipfs/exchange/bitswap/decision"
10 bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
11 bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
12 wantlist "github.com/ipfs/go-ipfs/exchange/bitswap/wantlist"
13 peer "github.com/ipfs/go-ipfs/p2p/peer"
13 - u "github.com/ipfs/go-ipfs/util"
14 )
15
16 type WantManager struct {
@@ -46,7 +46,7 @@ type msgPair struct {
46
47 type cancellation struct {
48 who peer.ID
49 - blk u.Key
49 + blk key.Key
50 }
51
52 type msgQueue struct {
@@ -60,16 +60,16 @@ type msgQueue struct {
60 done chan struct{}
61 }
62
63 -func (pm *WantManager) WantBlocks(ks []u.Key) {
63 +func (pm *WantManager) WantBlocks(ks []key.Key) {
64 log.Infof("want blocks: %s", ks)
65 pm.addEntries(ks, false)
66 }
67
68 -func (pm *WantManager) CancelWants(ks []u.Key) {
68 +func (pm *WantManager) CancelWants(ks []key.Key) {
69 pm.addEntries(ks, true)
70 }
71
72 -func (pm *WantManager) addEntries(ks []u.Key, cancel bool) {
72 +func (pm *WantManager) addEntries(ks []key.Key, cancel bool) {
73 var entries []*bsmsg.Entry
74 for i, k := range ks {
75 entries = append(entries, &bsmsg.Entry{
exchange/bitswap/workers.go
+4 -4
@@ -7,7 +7,7 @@ import (
7
8 process "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
9 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10 - u "github.com/ipfs/go-ipfs/util"
10 + key "github.com/ipfs/go-ipfs/blocks/key"
11 )
12
13 var TaskWorkerCount = 8
@@ -104,9 +104,9 @@ func (bs *Bitswap) provideWorker(ctx context.Context) {
104
105 func (bs *Bitswap) provideCollector(ctx context.Context) {
106 defer close(bs.provideKeys)
107 - var toProvide []u.Key
108 - var nextKey u.Key
109 - var keysOut chan u.Key
107 + var toProvide []key.Key
108 + var nextKey key.Key
109 + var keysOut chan key.Key
110
111 for {
112 select {
exchange/interface.go
+3 -3
@@ -6,16 +6,16 @@ import (
6
7 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
8 blocks "github.com/ipfs/go-ipfs/blocks"
9 - u "github.com/ipfs/go-ipfs/util"
9 + key "github.com/ipfs/go-ipfs/blocks/key"
10 )
11
12 // Any type that implements exchange.Interface may be used as an IPFS block
13 // exchange protocol.
14 type Interface interface {
15 // GetBlock returns the block associated with a given key.
16 - GetBlock(context.Context, u.Key) (*blocks.Block, error)
16 + GetBlock(context.Context, key.Key) (*blocks.Block, error)
17
18 - GetBlocks(context.Context, []u.Key) (<-chan *blocks.Block, error)
18 + GetBlocks(context.Context, []key.Key) (<-chan *blocks.Block, error)
19
20 // TODO Should callers be concerned with whether the block was made
21 // available on the network?
exchange/offline/offline.go
+4 -4
@@ -6,8 +6,8 @@ import (
6 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
7 blocks "github.com/ipfs/go-ipfs/blocks"
8 "github.com/ipfs/go-ipfs/blocks/blockstore"
9 + key "github.com/ipfs/go-ipfs/blocks/key"
10 exchange "github.com/ipfs/go-ipfs/exchange"
10 - u "github.com/ipfs/go-ipfs/util"
11 )
12
13 func Exchange(bs blockstore.Blockstore) exchange.Interface {
@@ -23,7 +23,7 @@ type offlineExchange struct {
23 // GetBlock returns nil to signal that a block could not be retrieved for the
24 // given key.
25 // NB: This function may return before the timeout expires.
26 -func (e *offlineExchange) GetBlock(_ context.Context, k u.Key) (*blocks.Block, error) {
26 +func (e *offlineExchange) GetBlock(_ context.Context, k key.Key) (*blocks.Block, error) {
27 return e.bs.Get(k)
28 }
29
@@ -39,11 +39,11 @@ func (_ *offlineExchange) Close() error {
39 return nil
40 }
41
42 -func (e *offlineExchange) GetBlocks(ctx context.Context, ks []u.Key) (<-chan *blocks.Block, error) {
42 +func (e *offlineExchange) GetBlocks(ctx context.Context, ks []key.Key) (<-chan *blocks.Block, error) {
43 out := make(chan *blocks.Block, 0)
44 go func() {
45 defer close(out)
46 - var misses []u.Key
46 + var misses []key.Key
47 for _, k := range ks {
48 hit, err := e.bs.Get(k)
49 if err != nil {
exchange/offline/offline_test.go
+4 -4
@@ -9,12 +9,12 @@ import (
9 blocks "github.com/ipfs/go-ipfs/blocks"
10 "github.com/ipfs/go-ipfs/blocks/blockstore"
11 "github.com/ipfs/go-ipfs/blocks/blocksutil"
12 - u "github.com/ipfs/go-ipfs/util"
12 + key "github.com/ipfs/go-ipfs/blocks/key"
13 )
14
15 func TestBlockReturnsErr(t *testing.T) {
16 off := Exchange(bstore())
17 - _, err := off.GetBlock(context.Background(), u.Key("foo"))
17 + _, err := off.GetBlock(context.Background(), key.Key("foo"))
18 if err != nil {
19 return // as desired
20 }
@@ -49,8 +49,8 @@ func TestGetBlocks(t *testing.T) {
49 }
50 }
51
52 - request := func() []u.Key {
53 - var ks []u.Key
52 + request := func() []key.Key {
53 + var ks []key.Key
54
55 for _, b := range expected {
56 ks = append(ks, b.Key())
fuse/ipns/ipns_unix.go
+3 -3
@@ -15,12 +15,12 @@ import (
15 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
16 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
17
18 + key "github.com/ipfs/go-ipfs/blocks/key"
19 core "github.com/ipfs/go-ipfs/core"
20 nsfs "github.com/ipfs/go-ipfs/ipnsfs"
21 dag "github.com/ipfs/go-ipfs/merkledag"
22 ci "github.com/ipfs/go-ipfs/p2p/crypto"
23 ft "github.com/ipfs/go-ipfs/unixfs"
23 - u "github.com/ipfs/go-ipfs/util"
24 )
25
26 var log = eventlog.Logger("fuse/ipns")
@@ -76,7 +76,7 @@ func CreateRoot(ipfs *core.IpfsNode, keys []ci.PrivKey, ipfspath, ipnspath strin
76 if err != nil {
77 return nil, err
78 }
79 - name := u.Key(pkh).B58String()
79 + name := key.Key(pkh).B58String()
80 root, err := ipfs.IpnsFs.GetRoot(name)
81 if err != nil {
82 return nil, err
@@ -194,7 +194,7 @@ func (r *Root) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
194 continue
195 }
196 ent := fuse.Dirent{
197 - Name: u.Key(hash).Pretty(),
197 + Name: key.Key(hash).Pretty(),
198 Type: fuse.DT_Dir,
199 }
200 listing = append(listing, ent)
fuse/readonly/ipfs_test.go
+2 -1
@@ -14,6 +14,7 @@ import (
14
15 fstest "github.com/ipfs/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs/fstestutil"
16
17 + key "github.com/ipfs/go-ipfs/blocks/key"
18 core "github.com/ipfs/go-ipfs/core"
19 coreunix "github.com/ipfs/go-ipfs/core/coreunix"
20 coremock "github.com/ipfs/go-ipfs/core/mock"
@@ -112,7 +113,7 @@ func TestIpfsStressRead(t *testing.T) {
113 nd, mnt := setupIpfsTest(t, nil)
114 defer mnt.Close()
115
115 - var ks []u.Key
116 + var ks []key.Key
117 var paths []string
118
119 nobj := 50
importer/helpers/helpers.go
+2 -2
@@ -5,11 +5,11 @@ import (
5 "time"
6
7 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
8 + key "github.com/ipfs/go-ipfs/blocks/key"
9 chunk "github.com/ipfs/go-ipfs/importer/chunk"
10 dag "github.com/ipfs/go-ipfs/merkledag"
11 "github.com/ipfs/go-ipfs/pin"
12 ft "github.com/ipfs/go-ipfs/unixfs"
12 - u "github.com/ipfs/go-ipfs/util"
13 )
14
15 // BlockSizeLimit specifies the maximum size an imported block can have.
@@ -123,7 +123,7 @@ func (n *UnixfsNode) AddChild(child *UnixfsNode, db *DagBuilderHelper) error {
123
124 // Removes the child node at the given index
125 func (n *UnixfsNode) RemoveChild(index int, dbh *DagBuilderHelper) {
126 - k := u.Key(n.node.Links[index].Hash)
126 + k := key.Key(n.node.Links[index].Hash)
127 if dbh.mp != nil {
128 dbh.mp.RemovePinWithMode(k, pin.Indirect)
129 }
ipnsfs/system.go
+3 -3
@@ -17,13 +17,13 @@ import (
17 "sync"
18 "time"
19
20 + key "github.com/ipfs/go-ipfs/blocks/key"
21 dag "github.com/ipfs/go-ipfs/merkledag"
22 namesys "github.com/ipfs/go-ipfs/namesys"
23 ci "github.com/ipfs/go-ipfs/p2p/crypto"
24 path "github.com/ipfs/go-ipfs/path"
25 pin "github.com/ipfs/go-ipfs/pin"
26 ft "github.com/ipfs/go-ipfs/unixfs"
26 - u "github.com/ipfs/go-ipfs/util"
27
28 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
29 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
@@ -66,7 +66,7 @@ func NewFilesystem(ctx context.Context, ds dag.DAGService, nsys namesys.NameSyst
66 if err != nil {
67 return nil, err
68 }
69 - roots[u.Key(pkh).Pretty()] = root
69 + roots[key.Key(pkh).Pretty()] = root
70 }
71
72 return fs, nil
@@ -141,7 +141,7 @@ func (fs *Filesystem) newKeyRoot(parent context.Context, k ci.PrivKey) (*KeyRoot
141 return nil, err
142 }
143
144 - name := "/ipns/" + u.Key(hash).String()
144 + name := "/ipns/" + key.Key(hash).String()
145
146 root := new(KeyRoot)
147 root.key = k
merkledag/merkledag.go
+13 -12
@@ -7,6 +7,7 @@ import (
7
8 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
9 blocks "github.com/ipfs/go-ipfs/blocks"
10 + key "github.com/ipfs/go-ipfs/blocks/key"
11 bserv "github.com/ipfs/go-ipfs/blockservice"
12 u "github.com/ipfs/go-ipfs/util"
13 )
@@ -16,15 +17,15 @@ var ErrNotFound = fmt.Errorf("merkledag: not found")
17
18 // DAGService is an IPFS Merkle DAG service.
19 type DAGService interface {
19 - Add(*Node) (u.Key, error)
20 + Add(*Node) (key.Key, error)
21 AddRecursive(*Node) error
21 - Get(context.Context, u.Key) (*Node, error)
22 + Get(context.Context, key.Key) (*Node, error)
23 Remove(*Node) error
24
25 // GetDAG returns, in order, all the single leve child
26 // nodes of the passed in node.
27 GetDAG(context.Context, *Node) []NodeGetter
27 - GetNodes(context.Context, []u.Key) []NodeGetter
28 + GetNodes(context.Context, []key.Key) []NodeGetter
29 }
30
31 func NewDAGService(bs *bserv.BlockService) DAGService {
@@ -41,7 +42,7 @@ type dagService struct {
42 }
43
44 // Add adds a node to the dagService, storing the block in the BlockService
44 -func (n *dagService) Add(nd *Node) (u.Key, error) {
45 +func (n *dagService) Add(nd *Node) (key.Key, error) {
46 if n == nil { // FIXME remove this assertion. protect with constructor invariant
47 return "", fmt.Errorf("dagService is nil")
48 }
@@ -82,7 +83,7 @@ func (n *dagService) AddRecursive(nd *Node) error {
83 }
84
85 // Get retrieves a node from the dagService, fetching the block in the BlockService
85 -func (n *dagService) Get(ctx context.Context, k u.Key) (*Node, error) {
86 +func (n *dagService) Get(ctx context.Context, k key.Key) (*Node, error) {
87 if n == nil {
88 return nil, fmt.Errorf("dagService is nil")
89 }
@@ -148,7 +149,7 @@ func FetchGraph(ctx context.Context, root *Node, serv DAGService) chan struct{}
149
150 // FindLinks searches this nodes links for the given key,
151 // returns the indexes of any links pointing to it
151 -func FindLinks(links []u.Key, k u.Key, start int) []int {
152 +func FindLinks(links []key.Key, k key.Key, start int) []int {
153 var out []int
154 for i, lnk_k := range links[start:] {
155 if k == lnk_k {
@@ -162,9 +163,9 @@ func FindLinks(links []u.Key, k u.Key, start int) []int {
163 // It returns a channel of nodes, which the caller can receive
164 // all the child nodes of 'root' on, in proper order.
165 func (ds *dagService) GetDAG(ctx context.Context, root *Node) []NodeGetter {
165 - var keys []u.Key
166 + var keys []key.Key
167 for _, lnk := range root.Links {
167 - keys = append(keys, u.Key(lnk.Hash))
168 + keys = append(keys, key.Key(lnk.Hash))
169 }
170
171 return ds.GetNodes(ctx, keys)
@@ -172,7 +173,7 @@ func (ds *dagService) GetDAG(ctx context.Context, root *Node) []NodeGetter {
173
174 // GetNodes returns an array of 'NodeGetter' promises, with each corresponding
175 // to the key with the same index as the passed in keys
175 -func (ds *dagService) GetNodes(ctx context.Context, keys []u.Key) []NodeGetter {
176 +func (ds *dagService) GetNodes(ctx context.Context, keys []key.Key) []NodeGetter {
177
178 // Early out if no work to do
179 if len(keys) == 0 {
@@ -219,9 +220,9 @@ func (ds *dagService) GetNodes(ctx context.Context, keys []u.Key) []NodeGetter {
220 }
221
222 // Remove duplicates from a list of keys
222 -func dedupeKeys(ks []u.Key) []u.Key {
223 - kmap := make(map[u.Key]struct{})
224 - var out []u.Key
223 +func dedupeKeys(ks []key.Key) []key.Key {
224 + kmap := make(map[key.Key]struct{})
225 + var out []key.Key
226 for _, k := range ks {
227 if _, ok := kmap[k]; !ok {
228 kmap[k] = struct{}{}
merkledag/merkledag_test.go
+2 -1
@@ -12,6 +12,7 @@ import (
12 dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
13 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
14 bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
15 + key "github.com/ipfs/go-ipfs/blocks/key"
16 blockservice "github.com/ipfs/go-ipfs/blockservice"
17 bserv "github.com/ipfs/go-ipfs/blockservice"
18 offline "github.com/ipfs/go-ipfs/exchange/offline"
@@ -81,7 +82,7 @@ func TestNode(t *testing.T) {
82 k, err := n.Key()
83 if err != nil {
84 t.Error(err)
84 - } else if k != u.Key(h) {
85 + } else if k != key.Key(h) {
86 t.Error("Key is not equivalent to multihash")
87 } else {
88 fmt.Println("key: ", k)
merkledag/node.go
+4 -9
@@ -6,14 +6,9 @@ import (
6 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
7
8 mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
9 - u "github.com/ipfs/go-ipfs/util"
9 + key "github.com/ipfs/go-ipfs/blocks/key"
10 )
11
12 -// NodeMap maps u.Keys to Nodes.
13 -// We cannot use []byte/Multihash for keys :(
14 -// so have to convert Multihash bytes to string (u.Key)
15 -type NodeMap map[u.Key]*Node
16 -
12 // Node represents a node in the IPFS Merkle DAG.
13 // nodes have opaque data and a set of navigable links.
14 type Node struct {
@@ -84,7 +79,7 @@ func (l *Link) GetNode(ctx context.Context, serv DAGService) (*Node, error) {
79 return l.Node, nil
80 }
81
87 - return serv.Get(ctx, u.Key(l.Hash))
82 + return serv.Get(ctx, key.Key(l.Hash))
83 }
84
85 // AddNodeLink adds a link to another node.
@@ -227,7 +222,7 @@ func (n *Node) Multihash() (mh.Multihash, error) {
222 }
223
224 // Key returns the Multihash as a key, for maps.
230 -func (n *Node) Key() (u.Key, error) {
225 +func (n *Node) Key() (key.Key, error) {
226 h, err := n.Multihash()
232 - return u.Key(h), err
227 + return key.Key(h), err
228 }
namesys/publisher.go
+4 -3
@@ -9,6 +9,7 @@ import (
9 proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
10 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
11
12 + key "github.com/ipfs/go-ipfs/blocks/key"
13 dag "github.com/ipfs/go-ipfs/merkledag"
14 pb "github.com/ipfs/go-ipfs/namesys/internal/pb"
15 ci "github.com/ipfs/go-ipfs/p2p/crypto"
@@ -55,7 +56,7 @@ func (p *ipnsPublisher) Publish(ctx context.Context, k ci.PrivKey, value path.Pa
56 }
57
58 nameb := u.Hash(pkbytes)
58 - namekey := u.Key("/pk/" + string(nameb))
59 + namekey := key.Key("/pk/" + string(nameb))
60
61 log.Debugf("Storing pubkey at: %s", namekey)
62 // Store associated public key
@@ -65,7 +66,7 @@ func (p *ipnsPublisher) Publish(ctx context.Context, k ci.PrivKey, value path.Pa
66 return err
67 }
68
68 - ipnskey := u.Key("/ipns/" + string(nameb))
69 + ipnskey := key.Key("/ipns/" + string(nameb))
70
71 log.Debugf("Storing ipns entry at: %s", ipnskey)
72 // Store ipns entry at "/ipns/"+b58(h(pubkey))
@@ -110,7 +111,7 @@ var IpnsRecordValidator = &record.ValidChecker{
111
112 // ValidateIpnsRecord implements ValidatorFunc and verifies that the
113 // given 'val' is an IpnsEntry and that that entry is valid.
113 -func ValidateIpnsRecord(k u.Key, val []byte) error {
114 +func ValidateIpnsRecord(k key.Key, val []byte) error {
115 entry := new(pb.IpnsEntry)
116 err := proto.Unmarshal(val, entry)
117 if err != nil {
namesys/resolve_test.go
+2 -1
@@ -4,6 +4,7 @@ import (
4 "testing"
5
6 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
7 + key "github.com/ipfs/go-ipfs/blocks/key"
8 path "github.com/ipfs/go-ipfs/path"
9 mockrouting "github.com/ipfs/go-ipfs/routing/mock"
10 u "github.com/ipfs/go-ipfs/util"
@@ -33,7 +34,7 @@ func TestRoutingResolve(t *testing.T) {
34 }
35
36 pkhash := u.Hash(pubkb)
36 - res, err := resolver.Resolve(context.Background(), u.Key(pkhash).Pretty())
37 + res, err := resolver.Resolve(context.Background(), key.Key(pkhash).Pretty())
38 if err != nil {
39 t.Fatal(err)
40 }
namesys/routing.go
+4 -3
@@ -7,6 +7,7 @@ import (
7 mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
8 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
9
10 + key "github.com/ipfs/go-ipfs/blocks/key"
11 pb "github.com/ipfs/go-ipfs/namesys/internal/pb"
12 path "github.com/ipfs/go-ipfs/path"
13 routing "github.com/ipfs/go-ipfs/routing"
@@ -64,7 +65,7 @@ func (r *routingResolver) resolveOnce(ctx context.Context, name string) (path.Pa
65 // /ipns/<name>
66 h := []byte("/ipns/" + string(hash))
67
67 - ipnsKey := u.Key(h)
68 + ipnsKey := key.Key(h)
69 val, err := r.routing.GetValue(ctx, ipnsKey)
70 if err != nil {
71 log.Warning("RoutingResolve get failed.")
@@ -84,7 +85,7 @@ func (r *routingResolver) resolveOnce(ctx context.Context, name string) (path.Pa
85 }
86
87 hsh, _ := pubkey.Hash()
87 - log.Debugf("pk hash = %s", u.Key(hsh))
88 + log.Debugf("pk hash = %s", key.Key(hsh))
89
90 // check sig with pk
91 if ok, err := pubkey.Verify(ipnsEntryDataForSig(entry), entry.GetSignature()); err != nil || !ok {
@@ -101,6 +102,6 @@ func (r *routingResolver) resolveOnce(ctx context.Context, name string) (path.Pa
102 } else {
103 // Its an old style multihash record
104 log.Warning("Detected old style multihash record")
104 - return path.FromKey(u.Key(valh)), nil
105 + return path.FromKey(key.Key(valh)), nil
106 }
107 }
p2p/net/conn/conn.go
+1 -1
@@ -157,7 +157,7 @@ func ID(c Conn) string {
157 lh := u.Hash([]byte(l))
158 rh := u.Hash([]byte(r))
159 ch := u.XOR(lh, rh)
160 - return u.Key(ch).Pretty()
160 + return peer.ID(ch).Pretty()
161 }
162
163 // String returns the user-friendly String representation of a conn
p2p/net/conn/interface.go
+2 -2
@@ -5,9 +5,9 @@ import (
5 "net"
6 "time"
7
8 + key "github.com/ipfs/go-ipfs/blocks/key"
9 ic "github.com/ipfs/go-ipfs/p2p/crypto"
10 peer "github.com/ipfs/go-ipfs/p2p/peer"
10 - u "github.com/ipfs/go-ipfs/util"
11
12 msgio "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio"
13 ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
@@ -15,7 +15,7 @@ import (
15 )
16
17 // Map maps Keys (Peer.IDs) to Connections.
18 -type Map map[u.Key]Conn
18 +type Map map[key.Key]Conn
19
20 type PeerConn interface {
21 io.Closer
p2p/peer/queue/distance.go
+2 -2
@@ -5,9 +5,9 @@ import (
5 "math/big"
6 "sync"
7
8 + key "github.com/ipfs/go-ipfs/blocks/key"
9 peer "github.com/ipfs/go-ipfs/p2p/peer"
10 ks "github.com/ipfs/go-ipfs/routing/keyspace"
10 - u "github.com/ipfs/go-ipfs/util"
11 )
12
13 // peerMetric tracks a peer and its distance to something else.
@@ -93,7 +93,7 @@ func (pq *distancePQ) Dequeue() peer.ID {
93 // NewXORDistancePQ returns a PeerQueue which maintains its peers sorted
94 // in terms of their distances to each other in an XORKeySpace (i.e. using
95 // XOR as a metric of distance).
96 -func NewXORDistancePQ(fromKey u.Key) PeerQueue {
96 +func NewXORDistancePQ(fromKey key.Key) PeerQueue {
97 return &distancePQ{
98 from: ks.XORKeySpace.Key([]byte(fromKey)),
99 heap: peerMetricHeap{},
p2p/peer/queue/queue_test.go
+3 -2
@@ -6,6 +6,7 @@ import (
6 "testing"
7 "time"
8
9 + key "github.com/ipfs/go-ipfs/blocks/key"
10 peer "github.com/ipfs/go-ipfs/p2p/peer"
11 u "github.com/ipfs/go-ipfs/util"
12
@@ -27,7 +28,7 @@ func TestQueue(t *testing.T) {
28 // [78 135 26 216 178 181 224 181 234 117 2 248 152 115 255 103 244 34 4 152 193 88 9 225 8 127 216 158 226 8 236 246]
29 // [125 135 124 6 226 160 101 94 192 57 39 12 18 79 121 140 190 154 147 55 44 83 101 151 63 255 94 179 51 203 241 51]
30
30 - pq := NewXORDistancePQ(u.Key("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31"))
31 + pq := NewXORDistancePQ(key.Key("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31"))
32 pq.Enqueue(p3)
33 pq.Enqueue(p1)
34 pq.Enqueue(p2)
@@ -81,7 +82,7 @@ func TestSyncQueue(t *testing.T) {
82 }
83
84 ctx := context.Background()
84 - pq := NewXORDistancePQ(u.Key("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31"))
85 + pq := NewXORDistancePQ(key.Key("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31"))
86 cq := NewChanQueue(ctx, pq)
87 wg := sync.WaitGroup{}
88
path/path.go
+3 -3
@@ -5,7 +5,7 @@ import (
5 "path"
6 "strings"
7
8 - u "github.com/ipfs/go-ipfs/util"
8 + key "github.com/ipfs/go-ipfs/blocks/key"
9
10 b58 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
11 mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
@@ -24,7 +24,7 @@ func FromString(s string) Path {
24 }
25
26 // FromKey safely converts a Key type to a Path type
27 -func FromKey(k u.Key) Path {
27 +func FromKey(k key.Key) Path {
28 return Path("/ipfs/" + k.String())
29 }
30
@@ -86,7 +86,7 @@ func ParseKeyToPath(txt string) (Path, error) {
86 if err != nil {
87 return "", err
88 }
89 - return FromKey(u.Key(chk)), nil
89 + return FromKey(key.Key(chk)), nil
90 }
91
92 func (p *Path) IsValid() error {
path/resolver.go
+5 -4
@@ -2,13 +2,14 @@
2 package path
3
4 import (
5 + "errors"
6 "fmt"
7 "time"
7 - "errors"
8
9 mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
10 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
11
12 + key "github.com/ipfs/go-ipfs/blocks/key"
13 merkledag "github.com/ipfs/go-ipfs/merkledag"
14 u "github.com/ipfs/go-ipfs/util"
15 )
@@ -83,7 +84,7 @@ func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]*me
84 log.Debug("Resolve dag get.")
85 ctx, cancel := context.WithTimeout(ctx, time.Minute)
86 defer cancel()
86 - nd, err := s.DAG.Get(ctx, u.Key(h))
87 + nd, err := s.DAG.Get(ctx, key.Key(h))
88 if err != nil {
89 return nil, err
90 }
@@ -107,12 +108,12 @@ func (s *Resolver) ResolveLinks(ctx context.Context, ndd *merkledag.Node, names
108 // for each of the path components
109 for _, name := range names {
110
110 - var next u.Key
111 + var next key.Key
112 var nlink *merkledag.Link
113 // for each of the links in nd, the current object
114 for _, link := range nd.Links {
115 if link.Name == name {
115 - next = u.Key(link.Hash)
116 + next = key.Key(link.Hash)
117 nlink = link
118 break
119 }
path/resolver_test.go
+2 -1
@@ -9,6 +9,7 @@ import (
9 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10
11 blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
12 + key "github.com/ipfs/go-ipfs/blocks/key"
13 blockservice "github.com/ipfs/go-ipfs/blockservice"
14 offline "github.com/ipfs/go-ipfs/exchange/offline"
15 merkledag "github.com/ipfs/go-ipfs/merkledag"
@@ -16,7 +17,7 @@ import (
17 util "github.com/ipfs/go-ipfs/util"
18 )
19
19 -func randNode() (*merkledag.Node, util.Key) {
20 +func randNode() (*merkledag.Node, key.Key) {
21 node := new(merkledag.Node)
22 node.Data = make([]byte, 32)
23 util.NewTimeSeededRand().Read(node.Data)
pin/indirect.go
+11 -11
@@ -2,19 +2,19 @@ package pin
2
3 import (
4 ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
5 + key "github.com/ipfs/go-ipfs/blocks/key"
6 "github.com/ipfs/go-ipfs/blocks/set"
6 - "github.com/ipfs/go-ipfs/util"
7 )
8
9 type indirectPin struct {
10 blockset set.BlockSet
11 - refCounts map[util.Key]int
11 + refCounts map[key.Key]int
12 }
13
14 func NewIndirectPin(dstore ds.Datastore) *indirectPin {
15 return &indirectPin{
16 blockset: set.NewDBWrapperSet(dstore, set.NewSimpleBlockSet()),
17 - refCounts: make(map[util.Key]int),
17 + refCounts: make(map[key.Key]int),
18 }
19 }
20
@@ -25,11 +25,11 @@ func loadIndirPin(d ds.Datastore, k ds.Key) (*indirectPin, error) {
25 return nil, err
26 }
27
28 - refcnt := make(map[util.Key]int)
29 - var keys []util.Key
28 + refcnt := make(map[key.Key]int)
29 + var keys []key.Key
30 for encK, v := range rcStore {
31 if v > 0 {
32 - k := util.B58KeyDecode(encK)
32 + k := key.B58KeyDecode(encK)
33 keys = append(keys, k)
34 refcnt[k] = v
35 }
@@ -43,12 +43,12 @@ func storeIndirPin(d ds.Datastore, k ds.Key, p *indirectPin) error {
43
44 rcStore := map[string]int{}
45 for k, v := range p.refCounts {
46 - rcStore[util.B58KeyEncode(k)] = v
46 + rcStore[key.B58KeyEncode(k)] = v
47 }
48 return storeSet(d, k, rcStore)
49 }
50
51 -func (i *indirectPin) Increment(k util.Key) {
51 +func (i *indirectPin) Increment(k key.Key) {
52 c := i.refCounts[k]
53 i.refCounts[k] = c + 1
54 if c <= 0 {
@@ -56,7 +56,7 @@ func (i *indirectPin) Increment(k util.Key) {
56 }
57 }
58
59 -func (i *indirectPin) Decrement(k util.Key) {
59 +func (i *indirectPin) Decrement(k key.Key) {
60 c := i.refCounts[k] - 1
61 i.refCounts[k] = c
62 if c <= 0 {
@@ -65,7 +65,7 @@ func (i *indirectPin) Decrement(k util.Key) {
65 }
66 }
67
68 -func (i *indirectPin) HasKey(k util.Key) bool {
68 +func (i *indirectPin) HasKey(k key.Key) bool {
69 return i.blockset.HasKey(k)
70 }
71
@@ -73,6 +73,6 @@ func (i *indirectPin) Set() set.BlockSet {
73 return i.blockset
74 }
75
76 -func (i *indirectPin) GetRefs() map[util.Key]int {
76 +func (i *indirectPin) GetRefs() map[key.Key]int {
77 return i.refCounts
78 }
pin/pin.go
+17 -16
@@ -11,6 +11,7 @@ import (
11 ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
12 nsds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/namespace"
13 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
14 + key "github.com/ipfs/go-ipfs/blocks/key"
15 "github.com/ipfs/go-ipfs/blocks/set"
16 mdag "github.com/ipfs/go-ipfs/merkledag"
17 "github.com/ipfs/go-ipfs/util"
@@ -31,22 +32,22 @@ const (
32 )
33
34 type Pinner interface {
34 - IsPinned(util.Key) bool
35 + IsPinned(key.Key) bool
36 Pin(context.Context, *mdag.Node, bool) error
36 - Unpin(context.Context, util.Key, bool) error
37 + Unpin(context.Context, key.Key, bool) error
38 Flush() error
39 GetManual() ManualPinner
39 - DirectKeys() []util.Key
40 - IndirectKeys() map[util.Key]int
41 - RecursiveKeys() []util.Key
40 + DirectKeys() []key.Key
41 + IndirectKeys() map[key.Key]int
42 + RecursiveKeys() []key.Key
43 }
44
45 // ManualPinner is for manually editing the pin structure
46 // Use with care! If used improperly, garbage collection
47 // may not be successful
48 type ManualPinner interface {
48 - PinWithMode(util.Key, PinMode)
49 - RemovePinWithMode(util.Key, PinMode)
49 + PinWithMode(key.Key, PinMode)
50 + RemovePinWithMode(key.Key, PinMode)
51 Pinner
52 }
53
@@ -120,7 +121,7 @@ func (p *pinner) Pin(ctx context.Context, node *mdag.Node, recurse bool) error {
121 }
122
123 // Unpin a given key
123 -func (p *pinner) Unpin(ctx context.Context, k util.Key, recursive bool) error {
124 +func (p *pinner) Unpin(ctx context.Context, k key.Key, recursive bool) error {
125 p.lock.Lock()
126 defer p.lock.Unlock()
127 if p.recursePin.HasKey(k) {
@@ -193,7 +194,7 @@ func (p *pinner) pinLinks(ctx context.Context, node *mdag.Node) error {
194 }
195
196 // IsPinned returns whether or not the given key is pinned
196 -func (p *pinner) IsPinned(key util.Key) bool {
197 +func (p *pinner) IsPinned(key key.Key) bool {
198 p.lock.RLock()
199 defer p.lock.RUnlock()
200 return p.recursePin.HasKey(key) ||
@@ -201,7 +202,7 @@ func (p *pinner) IsPinned(key util.Key) bool {
202 p.indirPin.HasKey(key)
203 }
204
204 -func (p *pinner) RemovePinWithMode(key util.Key, mode PinMode) {
205 +func (p *pinner) RemovePinWithMode(key key.Key, mode PinMode) {
206 p.lock.Lock()
207 defer p.lock.Unlock()
208 switch mode {
@@ -222,7 +223,7 @@ func LoadPinner(d ds.ThreadSafeDatastore, dserv mdag.DAGService) (Pinner, error)
223 p := new(pinner)
224
225 { // load recursive set
225 - var recurseKeys []util.Key
226 + var recurseKeys []key.Key
227 if err := loadSet(d, recursePinDatastoreKey, &recurseKeys); err != nil {
228 return nil, err
229 }
@@ -230,7 +231,7 @@ func LoadPinner(d ds.ThreadSafeDatastore, dserv mdag.DAGService) (Pinner, error)
231 }
232
233 { // load direct set
233 - var directKeys []util.Key
234 + var directKeys []key.Key
235 if err := loadSet(d, directPinDatastoreKey, &directKeys); err != nil {
236 return nil, err
237 }
@@ -253,17 +254,17 @@ func LoadPinner(d ds.ThreadSafeDatastore, dserv mdag.DAGService) (Pinner, error)
254 }
255
256 // DirectKeys returns a slice containing the directly pinned keys
256 -func (p *pinner) DirectKeys() []util.Key {
257 +func (p *pinner) DirectKeys() []key.Key {
258 return p.directPin.GetKeys()
259 }
260
261 // IndirectKeys returns a slice containing the indirectly pinned keys
261 -func (p *pinner) IndirectKeys() map[util.Key]int {
262 +func (p *pinner) IndirectKeys() map[key.Key]int {
263 return p.indirPin.GetRefs()
264 }
265
266 // RecursiveKeys returns a slice containing the recursively pinned keys
266 -func (p *pinner) RecursiveKeys() []util.Key {
267 +func (p *pinner) RecursiveKeys() []key.Key {
268 return p.recursePin.GetKeys()
269 }
270
@@ -314,7 +315,7 @@ func loadSet(d ds.Datastore, k ds.Key, val interface{}) error {
315
316 // PinWithMode is a method on ManualPinners, allowing the user to have fine
317 // grained control over pin counts
317 -func (p *pinner) PinWithMode(k util.Key, mode PinMode) {
318 +func (p *pinner) PinWithMode(k key.Key, mode PinMode) {
319 p.lock.Lock()
320 defer p.lock.Unlock()
321 switch mode {
pin/pin_test.go
+2 -1
@@ -9,13 +9,14 @@ import (
9 ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
10 dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
11 "github.com/ipfs/go-ipfs/blocks/blockstore"
12 + key "github.com/ipfs/go-ipfs/blocks/key"
13 bs "github.com/ipfs/go-ipfs/blockservice"
14 "github.com/ipfs/go-ipfs/exchange/offline"
15 mdag "github.com/ipfs/go-ipfs/merkledag"
16 "github.com/ipfs/go-ipfs/util"
17 )
18
18 -func randNode() (*mdag.Node, util.Key) {
19 +func randNode() (*mdag.Node, key.Key) {
20 nd := new(mdag.Node)
21 nd.Data = make([]byte, 32)
22 util.NewTimeSeededRand().Read(nd.Data)
routing/dht/dht.go
+14 -13
@@ -10,6 +10,7 @@ import (
10 "sync"
11 "time"
12
13 + key "github.com/ipfs/go-ipfs/blocks/key"
14 ci "github.com/ipfs/go-ipfs/p2p/crypto"
15 host "github.com/ipfs/go-ipfs/p2p/host"
16 peer "github.com/ipfs/go-ipfs/p2p/peer"
@@ -122,7 +123,7 @@ func (dht *IpfsDHT) Connect(ctx context.Context, npeer peer.ID) error {
123
124 // putValueToPeer stores the given key/value pair at the peer 'p'
125 func (dht *IpfsDHT) putValueToPeer(ctx context.Context, p peer.ID,
125 - key u.Key, rec *pb.Record) error {
126 + key key.Key, rec *pb.Record) error {
127
128 pmes := pb.NewMessage(pb.Message_PUT_VALUE, string(key), 0)
129 pmes.Record = rec
@@ -139,7 +140,7 @@ func (dht *IpfsDHT) putValueToPeer(ctx context.Context, p peer.ID,
140
141 // putProvider sends a message to peer 'p' saying that the local node
142 // can provide the value of 'key'
142 -func (dht *IpfsDHT) putProvider(ctx context.Context, p peer.ID, key string) error {
143 +func (dht *IpfsDHT) putProvider(ctx context.Context, p peer.ID, skey string) error {
144
145 // add self as the provider
146 pi := peer.PeerInfo{
@@ -150,18 +151,18 @@ func (dht *IpfsDHT) putProvider(ctx context.Context, p peer.ID, key string) erro
151 // // only share WAN-friendly addresses ??
152 // pi.Addrs = addrutil.WANShareableAddrs(pi.Addrs)
153 if len(pi.Addrs) < 1 {
153 - // log.Infof("%s putProvider: %s for %s error: no wan-friendly addresses", dht.self, p, u.Key(key), pi.Addrs)
154 + // log.Infof("%s putProvider: %s for %s error: no wan-friendly addresses", dht.self, p, key.Key(key), pi.Addrs)
155 return fmt.Errorf("no known addresses for self. cannot put provider.")
156 }
157
157 - pmes := pb.NewMessage(pb.Message_ADD_PROVIDER, string(key), 0)
158 + pmes := pb.NewMessage(pb.Message_ADD_PROVIDER, skey, 0)
159 pmes.ProviderPeers = pb.RawPeerInfosToPBPeers([]peer.PeerInfo{pi})
160 err := dht.sendMessage(ctx, p, pmes)
161 if err != nil {
162 return err
163 }
164
164 - log.Debugf("%s putProvider: %s for %s (%s)", dht.self, p, u.Key(key), pi.Addrs)
165 + log.Debugf("%s putProvider: %s for %s (%s)", dht.self, p, key.Key(skey), pi.Addrs)
166 return nil
167 }
168
@@ -170,7 +171,7 @@ func (dht *IpfsDHT) putProvider(ctx context.Context, p peer.ID, key string) erro
171 // NOTE: it will update the dht's peerstore with any new addresses
172 // it finds for the given peer.
173 func (dht *IpfsDHT) getValueOrPeers(ctx context.Context, p peer.ID,
173 - key u.Key) ([]byte, []peer.PeerInfo, error) {
174 + key key.Key) ([]byte, []peer.PeerInfo, error) {
175
176 pmes, err := dht.getValueSingle(ctx, p, key)
177 if err != nil {
@@ -203,7 +204,7 @@ func (dht *IpfsDHT) getValueOrPeers(ctx context.Context, p peer.ID,
204
205 // getValueSingle simply performs the get value RPC with the given parameters
206 func (dht *IpfsDHT) getValueSingle(ctx context.Context, p peer.ID,
206 - key u.Key) (*pb.Message, error) {
207 + key key.Key) (*pb.Message, error) {
208 defer log.EventBegin(ctx, "getValueSingle", p, &key).Done()
209
210 pmes := pb.NewMessage(pb.Message_GET_VALUE, string(key), 0)
@@ -211,7 +212,7 @@ func (dht *IpfsDHT) getValueSingle(ctx context.Context, p peer.ID,
212 }
213
214 // getLocal attempts to retrieve the value from the datastore
214 -func (dht *IpfsDHT) getLocal(key u.Key) ([]byte, error) {
215 +func (dht *IpfsDHT) getLocal(key key.Key) ([]byte, error) {
216
217 log.Debug("getLocal %s", key)
218 v, err := dht.datastore.Get(key.DsKey())
@@ -254,7 +255,7 @@ func (dht *IpfsDHT) getOwnPrivateKey() (ci.PrivKey, error) {
255 }
256
257 // putLocal stores the key value pair in the datastore
257 -func (dht *IpfsDHT) putLocal(key u.Key, rec *pb.Record) error {
258 +func (dht *IpfsDHT) putLocal(key key.Key, rec *pb.Record) error {
259 data, err := proto.Marshal(rec)
260 if err != nil {
261 return err
@@ -287,7 +288,7 @@ func (dht *IpfsDHT) findPeerSingle(ctx context.Context, p peer.ID, id peer.ID) (
288 return dht.sendRequest(ctx, p, pmes)
289 }
290
290 -func (dht *IpfsDHT) findProvidersSingle(ctx context.Context, p peer.ID, key u.Key) (*pb.Message, error) {
291 +func (dht *IpfsDHT) findProvidersSingle(ctx context.Context, p peer.ID, key key.Key) (*pb.Message, error) {
292 defer log.EventBegin(ctx, "findProvidersSingle", p, &key).Done()
293
294 pmes := pb.NewMessage(pb.Message_GET_PROVIDERS, string(key), 0)
@@ -296,7 +297,7 @@ func (dht *IpfsDHT) findProvidersSingle(ctx context.Context, p peer.ID, key u.Ke
297
298 // nearestPeersToQuery returns the routing tables closest peers.
299 func (dht *IpfsDHT) nearestPeersToQuery(pmes *pb.Message, count int) []peer.ID {
299 - key := u.Key(pmes.GetKey())
300 + key := key.Key(pmes.GetKey())
301 closer := dht.routingTable.NearestPeers(kb.ConvertKey(key), count)
302 return closer
303 }
@@ -326,7 +327,7 @@ func (dht *IpfsDHT) betterPeersToQuery(pmes *pb.Message, p peer.ID, count int) [
327 }
328
329 // must all be closer than self
329 - key := u.Key(pmes.GetKey())
330 + key := key.Key(pmes.GetKey())
331 if !kb.Closer(dht.self, clp, key) {
332 filtered = append(filtered, clp)
333 }
@@ -355,7 +356,7 @@ func (dht *IpfsDHT) PingRoutine(t time.Duration) {
356 case <-tick:
357 id := make([]byte, 16)
358 rand.Read(id)
358 - peers := dht.routingTable.NearestPeers(kb.ConvertKey(u.Key(id)), 5)
359 + peers := dht.routingTable.NearestPeers(kb.ConvertKey(key.Key(id)), 5)
360 for _, p := range peers {
361 ctx, cancel := context.WithTimeout(dht.Context(), time.Second*5)
362 _, err := dht.Ping(ctx, p)
routing/dht/dht_test.go
+12 -11
@@ -14,6 +14,7 @@ import (
14 ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
15 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
16
17 + key "github.com/ipfs/go-ipfs/blocks/key"
18 peer "github.com/ipfs/go-ipfs/p2p/peer"
19 netutil "github.com/ipfs/go-ipfs/p2p/test/util"
20 routing "github.com/ipfs/go-ipfs/routing"
@@ -24,14 +25,14 @@ import (
25 travisci "github.com/ipfs/go-ipfs/util/testutil/ci/travis"
26 )
27
27 -var testCaseValues = map[u.Key][]byte{}
28 +var testCaseValues = map[key.Key][]byte{}
29
30 func init() {
31 testCaseValues["hello"] = []byte("world")
32 for i := 0; i < 100; i++ {
33 k := fmt.Sprintf("%d -- key", i)
34 v := fmt.Sprintf("%d -- value", i)
34 - testCaseValues[u.Key(k)] = []byte(v)
35 + testCaseValues[key.Key(k)] = []byte(v)
36 }
37 }
38
@@ -42,7 +43,7 @@ func setupDHT(ctx context.Context, t *testing.T) *IpfsDHT {
43 d := NewDHT(ctx, h, dss)
44
45 d.Validator["v"] = &record.ValidChecker{
45 - Func: func(u.Key, []byte) error {
46 + Func: func(key.Key, []byte) error {
47 return nil
48 },
49 Sign: false,
@@ -143,7 +144,7 @@ func TestValueGetSet(t *testing.T) {
144 defer dhtB.host.Close()
145
146 vf := &record.ValidChecker{
146 - Func: func(u.Key, []byte) error {
147 + Func: func(key.Key, []byte) error {
148 return nil
149 },
150 Sign: false,
@@ -460,7 +461,7 @@ func TestProvidesMany(t *testing.T) {
461 }
462 }
463
463 - var providers = map[u.Key]peer.ID{}
464 + var providers = map[key.Key]peer.ID{}
465
466 d := 0
467 for k, v := range testCaseValues {
@@ -501,7 +502,7 @@ func TestProvidesMany(t *testing.T) {
502 ctxT, _ = context.WithTimeout(ctx, 5*time.Second)
503
504 var wg sync.WaitGroup
504 - getProvider := func(dht *IpfsDHT, k u.Key) {
505 + getProvider := func(dht *IpfsDHT, k key.Key) {
506 defer wg.Done()
507
508 expected := providers[k]
@@ -561,7 +562,7 @@ func TestProvidesAsync(t *testing.T) {
562 connect(t, ctx, dhts[1], dhts[2])
563 connect(t, ctx, dhts[1], dhts[3])
564
564 - k := u.Key("hello")
565 + k := key.Key("hello")
566 val := []byte("world")
567 sk := dhts[3].peerstore.PrivKey(dhts[3].self)
568 rec, err := record.MakePutRecord(sk, k, val, false)
@@ -579,7 +580,7 @@ func TestProvidesAsync(t *testing.T) {
580 t.Fatal(err)
581 }
582
582 - err = dhts[3].Provide(ctx, u.Key("hello"))
583 + err = dhts[3].Provide(ctx, key.Key("hello"))
584 if err != nil {
585 t.Fatal(err)
586 }
@@ -587,7 +588,7 @@ func TestProvidesAsync(t *testing.T) {
588 time.Sleep(time.Millisecond * 60)
589
590 ctxT, _ := context.WithTimeout(ctx, time.Millisecond*300)
590 - provs := dhts[0].FindProvidersAsync(ctxT, u.Key("hello"), 5)
591 + provs := dhts[0].FindProvidersAsync(ctxT, key.Key("hello"), 5)
592 select {
593 case p, ok := <-provs:
594 if !ok {
@@ -624,7 +625,7 @@ func TestLayeredGet(t *testing.T) {
625 connect(t, ctx, dhts[1], dhts[2])
626 connect(t, ctx, dhts[1], dhts[3])
627
627 - err := dhts[3].Provide(ctx, u.Key("/v/hello"))
628 + err := dhts[3].Provide(ctx, key.Key("/v/hello"))
629 if err != nil {
630 t.Fatal(err)
631 }
@@ -633,7 +634,7 @@ func TestLayeredGet(t *testing.T) {
634
635 t.Log("interface was changed. GetValue should not use providers.")
636 ctxT, _ := context.WithTimeout(ctx, time.Second)
636 - val, err := dhts[0].GetValue(ctxT, u.Key("/v/hello"))
637 + val, err := dhts[0].GetValue(ctxT, key.Key("/v/hello"))
638 if err != routing.ErrNotFound {
639 t.Error(err)
640 }
routing/dht/ext_test.go
+6 -8
@@ -12,6 +12,7 @@ import (
12 dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
13 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
14
15 + key "github.com/ipfs/go-ipfs/blocks/key"
16 inet "github.com/ipfs/go-ipfs/p2p/net"
17 mocknet "github.com/ipfs/go-ipfs/p2p/net/mock"
18 peer "github.com/ipfs/go-ipfs/p2p/peer"
@@ -37,7 +38,6 @@ func TestGetFailures(t *testing.T) {
38 d := NewDHT(ctx, hosts[0], tsds)
39 d.Update(ctx, hosts[1].ID())
40
40 - // u.POut("NotFound Test\n")
41 // Reply with failures to every message
42 hosts[1].SetStreamHandler(ProtocolDHT, func(s inet.Stream) {
43 defer s.Close()
@@ -45,9 +45,8 @@ func TestGetFailures(t *testing.T) {
45 })
46
47 // This one should time out
48 - // u.POut("Timout Test\n")
48 ctx1, _ := context.WithTimeout(context.Background(), 200*time.Millisecond)
50 - if _, err := d.GetValue(ctx1, u.Key("test")); err != nil {
49 + if _, err := d.GetValue(ctx1, key.Key("test")); err != nil {
50 if merr, ok := err.(u.MultiErr); ok && len(merr) > 0 {
51 err = merr[0]
52 }
@@ -87,7 +86,7 @@ func TestGetFailures(t *testing.T) {
86 // (was 3 seconds before which should be _plenty_ of time, but maybe
87 // travis machines really have a hard time...)
88 ctx2, _ := context.WithTimeout(context.Background(), 20*time.Second)
90 - _, err = d.GetValue(ctx2, u.Key("test"))
89 + _, err = d.GetValue(ctx2, key.Key("test"))
90 if err != nil {
91 if merr, ok := err.(u.MultiErr); ok && len(merr) > 0 {
92 err = merr[0]
@@ -111,7 +110,7 @@ func TestGetFailures(t *testing.T) {
110 t.Fatal(err)
111 }
112
114 - rec, err := record.MakePutRecord(sk, u.Key(str), []byte("blah"), true)
113 + rec, err := record.MakePutRecord(sk, key.Key(str), []byte("blah"), true)
114 if err != nil {
115 t.Fatal(err)
116 }
@@ -121,7 +120,6 @@ func TestGetFailures(t *testing.T) {
120 Record: rec,
121 }
122
124 - // u.POut("handleGetValue Test\n")
123 s, err := hosts[1].NewStream(ProtocolDHT, hosts[0].ID())
124 if err != nil {
125 t.Fatal(err)
@@ -205,7 +203,7 @@ func TestNotFound(t *testing.T) {
203
204 // long timeout to ensure timing is not at play.
205 ctx, _ = context.WithTimeout(ctx, time.Second*20)
208 - v, err := d.GetValue(ctx, u.Key("hello"))
206 + v, err := d.GetValue(ctx, key.Key("hello"))
207 log.Debugf("get value got %v", v)
208 if err != nil {
209 if merr, ok := err.(u.MultiErr); ok && len(merr) > 0 {
@@ -277,7 +275,7 @@ func TestLessThanKResponses(t *testing.T) {
275 }
276
277 ctx, _ = context.WithTimeout(ctx, time.Second*30)
280 - if _, err := d.GetValue(ctx, u.Key("hello")); err != nil {
278 + if _, err := d.GetValue(ctx, key.Key("hello")); err != nil {
279 switch err {
280 case routing.ErrNotFound:
281 //Success!
routing/dht/handlers.go
+8 -8
@@ -7,9 +7,9 @@ import (
7 proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
8 ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
9 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10 + key "github.com/ipfs/go-ipfs/blocks/key"
11 peer "github.com/ipfs/go-ipfs/p2p/peer"
12 pb "github.com/ipfs/go-ipfs/routing/dht/pb"
12 - u "github.com/ipfs/go-ipfs/util"
13 lgbl "github.com/ipfs/go-ipfs/util/eventlog/loggables"
14 )
15
@@ -46,15 +46,15 @@ func (dht *IpfsDHT) handleGetValue(ctx context.Context, p peer.ID, pmes *pb.Mess
46 resp := pb.NewMessage(pmes.GetType(), pmes.GetKey(), pmes.GetClusterLevel())
47
48 // first, is there even a key?
49 - key := pmes.GetKey()
50 - if key == "" {
49 + k := pmes.GetKey()
50 + if k == "" {
51 return nil, errors.New("handleGetValue but no key was provided")
52 // TODO: send back an error response? could be bad, but the other node's hanging.
53 }
54
55 // let's first check if we have the value locally.
56 log.Debugf("%s handleGetValue looking into ds", dht.self)
57 - dskey := u.Key(pmes.GetKey()).DsKey()
57 + dskey := key.Key(k).DsKey()
58 iVal, err := dht.datastore.Get(dskey)
59 log.Debugf("%s handleGetValue looking into ds GOT %v", dht.self, iVal)
60
@@ -105,10 +105,10 @@ func (dht *IpfsDHT) handleGetValue(ctx context.Context, p peer.ID, pmes *pb.Mess
105 // Store a value in this peer local storage
106 func (dht *IpfsDHT) handlePutValue(ctx context.Context, p peer.ID, pmes *pb.Message) (*pb.Message, error) {
107 defer log.EventBegin(ctx, "handlePutValue", p).Done()
108 - dskey := u.Key(pmes.GetKey()).DsKey()
108 + dskey := key.Key(pmes.GetKey()).DsKey()
109
110 if err := dht.verifyRecordLocally(pmes.GetRecord()); err != nil {
111 - log.Debugf("Bad dht record in PUT from: %s. %s", u.Key(pmes.GetRecord().GetAuthor()), err)
111 + log.Debugf("Bad dht record in PUT from: %s. %s", key.Key(pmes.GetRecord().GetAuthor()), err)
112 return nil, err
113 }
114
@@ -163,7 +163,7 @@ func (dht *IpfsDHT) handleGetProviders(ctx context.Context, p peer.ID, pmes *pb.
163 defer log.EventBegin(ctx, "handleGetProviders", lm).Done()
164
165 resp := pb.NewMessage(pmes.GetType(), pmes.GetKey(), pmes.GetClusterLevel())
166 - key := u.Key(pmes.GetKey())
166 + key := key.Key(pmes.GetKey())
167 lm["key"] = func() interface{} { return key.Pretty() }
168
169 // debug logging niceness.
@@ -207,7 +207,7 @@ func (dht *IpfsDHT) handleAddProvider(ctx context.Context, p peer.ID, pmes *pb.M
207 lm["peer"] = func() interface{} { return p.Pretty() }
208
209 defer log.EventBegin(ctx, "handleAddProvider", lm).Done()
210 - key := u.Key(pmes.GetKey())
210 + key := key.Key(pmes.GetKey())
211 lm["key"] = func() interface{} { return key.Pretty() }
212
213 log.Debugf("%s adding %s as a provider for '%s'\n", dht.self, p, key)
routing/dht/lookup.go
+3 -3
@@ -2,10 +2,10 @@ package dht
2
3 import (
4 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
5 + key "github.com/ipfs/go-ipfs/blocks/key"
6 notif "github.com/ipfs/go-ipfs/notifications"
7 peer "github.com/ipfs/go-ipfs/p2p/peer"
8 kb "github.com/ipfs/go-ipfs/routing/kbucket"
8 - u "github.com/ipfs/go-ipfs/util"
9 pset "github.com/ipfs/go-ipfs/util/peerset"
10 )
11
@@ -21,7 +21,7 @@ func pointerizePeerInfos(pis []peer.PeerInfo) []*peer.PeerInfo {
21
22 // Kademlia 'node lookup' operation. Returns a channel of the K closest peers
23 // to the given key
24 -func (dht *IpfsDHT) GetClosestPeers(ctx context.Context, key u.Key) (<-chan peer.ID, error) {
24 +func (dht *IpfsDHT) GetClosestPeers(ctx context.Context, key key.Key) (<-chan peer.ID, error) {
25 e := log.EventBegin(ctx, "getClosestPeers", &key)
26 tablepeers := dht.routingTable.NearestPeers(kb.ConvertKey(key), AlphaValue)
27 if len(tablepeers) == 0 {
@@ -88,7 +88,7 @@ func (dht *IpfsDHT) GetClosestPeers(ctx context.Context, key u.Key) (<-chan peer
88 return out, nil
89 }
90
91 -func (dht *IpfsDHT) closerPeersSingle(ctx context.Context, key u.Key, p peer.ID) ([]peer.ID, error) {
91 +func (dht *IpfsDHT) closerPeersSingle(ctx context.Context, key key.Key, p peer.ID) ([]peer.ID, error) {
92 pmes, err := dht.findPeerSingle(ctx, p, peer.ID(key))
93 if err != nil {
94 return nil, err
routing/dht/pb/message.go
+2 -2
@@ -3,10 +3,10 @@ package dht_pb
3 import (
4 ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
5
6 + key "github.com/ipfs/go-ipfs/blocks/key"
7 inet "github.com/ipfs/go-ipfs/p2p/net"
8 peer "github.com/ipfs/go-ipfs/p2p/peer"
9 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
9 - util "github.com/ipfs/go-ipfs/util"
10 )
11
12 var log = eventlog.Logger("dht.pb")
@@ -143,7 +143,7 @@ func (m *Message) Loggable() map[string]interface{} {
143 return map[string]interface{}{
144 "message": map[string]string{
145 "type": m.Type.String(),
146 - "key": util.Key(m.GetKey()).Pretty(),
146 + "key": key.Key(m.GetKey()).Pretty(),
147 },
148 }
149 }
routing/dht/providers.go
+14 -14
@@ -4,8 +4,8 @@ import (
4 "time"
5
6 ctxgroup "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
7 + key "github.com/ipfs/go-ipfs/blocks/key"
8 peer "github.com/ipfs/go-ipfs/p2p/peer"
8 - u "github.com/ipfs/go-ipfs/util"
9
10 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
11 )
@@ -16,10 +16,10 @@ type providerInfo struct {
16 }
17
18 type ProviderManager struct {
19 - providers map[u.Key][]*providerInfo
20 - local map[u.Key]struct{}
19 + providers map[key.Key][]*providerInfo
20 + local map[key.Key]struct{}
21 lpeer peer.ID
22 - getlocal chan chan []u.Key
22 + getlocal chan chan []key.Key
23 newprovs chan *addProv
24 getprovs chan *getProv
25 period time.Duration
@@ -27,12 +27,12 @@ type ProviderManager struct {
27 }
28
29 type addProv struct {
30 - k u.Key
30 + k key.Key
31 val peer.ID
32 }
33
34 type getProv struct {
35 - k u.Key
35 + k key.Key
36 resp chan []peer.ID
37 }
38
@@ -40,9 +40,9 @@ func NewProviderManager(ctx context.Context, local peer.ID) *ProviderManager {
40 pm := new(ProviderManager)
41 pm.getprovs = make(chan *getProv)
42 pm.newprovs = make(chan *addProv)
43 - pm.providers = make(map[u.Key][]*providerInfo)
44 - pm.getlocal = make(chan chan []u.Key)
45 - pm.local = make(map[u.Key]struct{})
43 + pm.providers = make(map[key.Key][]*providerInfo)
44 + pm.getlocal = make(chan chan []key.Key)
45 + pm.local = make(map[key.Key]struct{})
46 pm.ContextGroup = ctxgroup.WithContext(ctx)
47
48 pm.Children().Add(1)
@@ -76,7 +76,7 @@ func (pm *ProviderManager) run() {
76 gp.resp <- parr
77
78 case lc := <-pm.getlocal:
79 - var keys []u.Key
79 + var keys []key.Key
80 for k := range pm.local {
81 keys = append(keys, k)
82 }
@@ -99,7 +99,7 @@ func (pm *ProviderManager) run() {
99 }
100 }
101
102 -func (pm *ProviderManager) AddProvider(ctx context.Context, k u.Key, val peer.ID) {
102 +func (pm *ProviderManager) AddProvider(ctx context.Context, k key.Key, val peer.ID) {
103 prov := &addProv{
104 k: k,
105 val: val,
@@ -110,7 +110,7 @@ func (pm *ProviderManager) AddProvider(ctx context.Context, k u.Key, val peer.ID
110 }
111 }
112
113 -func (pm *ProviderManager) GetProviders(ctx context.Context, k u.Key) []peer.ID {
113 +func (pm *ProviderManager) GetProviders(ctx context.Context, k key.Key) []peer.ID {
114 gp := &getProv{
115 k: k,
116 resp: make(chan []peer.ID, 1), // buffered to prevent sender from blocking
@@ -128,8 +128,8 @@ func (pm *ProviderManager) GetProviders(ctx context.Context, k u.Key) []peer.ID
128 }
129 }
130
131 -func (pm *ProviderManager) GetLocal() []u.Key {
132 - resp := make(chan []u.Key)
131 +func (pm *ProviderManager) GetLocal() []key.Key {
132 + resp := make(chan []key.Key)
133 pm.getlocal <- resp
134 return <-resp
135 }
routing/dht/providers_test.go
+2 -2
@@ -3,8 +3,8 @@ package dht
3 import (
4 "testing"
5
6 + key "github.com/ipfs/go-ipfs/blocks/key"
7 peer "github.com/ipfs/go-ipfs/p2p/peer"
7 - u "github.com/ipfs/go-ipfs/util"
8
9 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10 )
@@ -13,7 +13,7 @@ func TestProviderManager(t *testing.T) {
13 ctx := context.Background()
14 mid := peer.ID("testing")
15 p := NewProviderManager(ctx, mid)
16 - a := u.Key("test")
16 + a := key.Key("test")
17 p.AddProvider(ctx, a, peer.ID("testingprovider"))
18 resp := p.GetProviders(ctx, a)
19 if len(resp) != 1 {
routing/dht/query.go
+3 -2
@@ -3,6 +3,7 @@ package dht
3 import (
4 "sync"
5
6 + key "github.com/ipfs/go-ipfs/blocks/key"
7 notif "github.com/ipfs/go-ipfs/notifications"
8 peer "github.com/ipfs/go-ipfs/p2p/peer"
9 queue "github.com/ipfs/go-ipfs/p2p/peer/queue"
@@ -21,7 +22,7 @@ var maxQueryConcurrency = AlphaValue
22
23 type dhtQuery struct {
24 dht *IpfsDHT
24 - key u.Key // the key we're querying for
25 + key key.Key // the key we're querying for
26 qfunc queryFunc // the function to execute per peer
27 concurrency int // the concurrency parameter
28 }
@@ -35,7 +36,7 @@ type dhtQueryResult struct {
36 }
37
38 // constructs query
38 -func (dht *IpfsDHT) newQuery(k u.Key, f queryFunc) *dhtQuery {
39 +func (dht *IpfsDHT) newQuery(k key.Key, f queryFunc) *dhtQuery {
40 return &dhtQuery{
41 key: k,
42 dht: dht,
routing/dht/routing.go
+9 -9
@@ -5,6 +5,7 @@ import (
5 "time"
6
7 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
8 + key "github.com/ipfs/go-ipfs/blocks/key"
9 notif "github.com/ipfs/go-ipfs/notifications"
10 inet "github.com/ipfs/go-ipfs/p2p/net"
11 peer "github.com/ipfs/go-ipfs/p2p/peer"
@@ -12,7 +13,6 @@ import (
13 pb "github.com/ipfs/go-ipfs/routing/dht/pb"
14 kb "github.com/ipfs/go-ipfs/routing/kbucket"
15 record "github.com/ipfs/go-ipfs/routing/record"
15 - u "github.com/ipfs/go-ipfs/util"
16 pset "github.com/ipfs/go-ipfs/util/peerset"
17 )
18
@@ -28,7 +28,7 @@ var asyncQueryBuffer = 10
28
29 // PutValue adds value corresponding to given Key.
30 // This is the top level "Store" operation of the DHT
31 -func (dht *IpfsDHT) PutValue(ctx context.Context, key u.Key, value []byte) error {
31 +func (dht *IpfsDHT) PutValue(ctx context.Context, key key.Key, value []byte) error {
32 log.Debugf("PutValue %s", key)
33 sk, err := dht.getOwnPrivateKey()
34 if err != nil {
@@ -79,7 +79,7 @@ func (dht *IpfsDHT) PutValue(ctx context.Context, key u.Key, value []byte) error
79 // GetValue searches for the value corresponding to given Key.
80 // If the search does not succeed, a multiaddr string of a closer peer is
81 // returned along with util.ErrSearchIncomplete
82 -func (dht *IpfsDHT) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
82 +func (dht *IpfsDHT) GetValue(ctx context.Context, key key.Key) ([]byte, error) {
83 // If we have it local, dont bother doing an RPC!
84 val, err := dht.getLocal(key)
85 if err == nil {
@@ -141,7 +141,7 @@ func (dht *IpfsDHT) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
141 // This is what DSHTs (Coral and MainlineDHT) do to store large values in a DHT.
142
143 // Provide makes this node announce that it can provide a value for the given key
144 -func (dht *IpfsDHT) Provide(ctx context.Context, key u.Key) error {
144 +func (dht *IpfsDHT) Provide(ctx context.Context, key key.Key) error {
145 defer log.EventBegin(ctx, "provide", &key).Done()
146
147 // add self locally
@@ -169,7 +169,7 @@ func (dht *IpfsDHT) Provide(ctx context.Context, key u.Key) error {
169 }
170
171 // FindProviders searches until the context expires.
172 -func (dht *IpfsDHT) FindProviders(ctx context.Context, key u.Key) ([]peer.PeerInfo, error) {
172 +func (dht *IpfsDHT) FindProviders(ctx context.Context, key key.Key) ([]peer.PeerInfo, error) {
173 var providers []peer.PeerInfo
174 for p := range dht.FindProvidersAsync(ctx, key, KValue) {
175 providers = append(providers, p)
@@ -180,14 +180,14 @@ func (dht *IpfsDHT) FindProviders(ctx context.Context, key u.Key) ([]peer.PeerIn
180 // FindProvidersAsync is the same thing as FindProviders, but returns a channel.
181 // Peers will be returned on the channel as soon as they are found, even before
182 // the search query completes.
183 -func (dht *IpfsDHT) FindProvidersAsync(ctx context.Context, key u.Key, count int) <-chan peer.PeerInfo {
183 +func (dht *IpfsDHT) FindProvidersAsync(ctx context.Context, key key.Key, count int) <-chan peer.PeerInfo {
184 log.Event(ctx, "findProviders", &key)
185 peerOut := make(chan peer.PeerInfo, count)
186 go dht.findProvidersAsyncRoutine(ctx, key, count, peerOut)
187 return peerOut
188 }
189
190 -func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key u.Key, count int, peerOut chan peer.PeerInfo) {
190 +func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key key.Key, count int, peerOut chan peer.PeerInfo) {
191 defer log.EventBegin(ctx, "findProvidersAsync", &key).Done()
192 defer close(peerOut)
193
@@ -289,7 +289,7 @@ func (dht *IpfsDHT) FindPeer(ctx context.Context, id peer.ID) (peer.PeerInfo, er
289 }
290
291 // setup the Query
292 - query := dht.newQuery(u.Key(id), func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
292 + query := dht.newQuery(key.Key(id), func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
293 notif.PublishQueryEvent(ctx, &notif.QueryEvent{
294 Type: notif.SendingQuery,
295 ID: p,
@@ -347,7 +347,7 @@ func (dht *IpfsDHT) FindPeersConnectedToPeer(ctx context.Context, id peer.ID) (<
347 }
348
349 // setup the Query
350 - query := dht.newQuery(u.Key(id), func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
350 + query := dht.newQuery(key.Key(id), func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
351
352 pmes, err := dht.findPeerSingle(ctx, p, id)
353 if err != nil {
routing/kbucket/util.go
+3 -2
@@ -5,6 +5,7 @@ import (
5 "crypto/sha256"
6 "errors"
7
8 + key "github.com/ipfs/go-ipfs/blocks/key"
9 peer "github.com/ipfs/go-ipfs/p2p/peer"
10 ks "github.com/ipfs/go-ipfs/routing/keyspace"
11 u "github.com/ipfs/go-ipfs/util"
@@ -45,13 +46,13 @@ func ConvertPeerID(id peer.ID) ID {
46 }
47
48 // ConvertKey creates a DHT ID by hashing a local key (String)
48 -func ConvertKey(id u.Key) ID {
49 +func ConvertKey(id key.Key) ID {
50 hash := sha256.Sum256([]byte(id))
51 return hash[:]
52 }
53
54 // Closer returns true if a is closer to key than b is
54 -func Closer(a, b peer.ID, key u.Key) bool {
55 +func Closer(a, b peer.ID, key key.Key) bool {
56 aid := ConvertPeerID(a)
57 bid := ConvertPeerID(b)
58 tgt := ConvertKey(key)
routing/mock/centralized_client.go
+6 -5
@@ -7,6 +7,7 @@ import (
7 ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
8 ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
9 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10 + key "github.com/ipfs/go-ipfs/blocks/key"
11 peer "github.com/ipfs/go-ipfs/p2p/peer"
12 routing "github.com/ipfs/go-ipfs/routing"
13 u "github.com/ipfs/go-ipfs/util"
@@ -22,13 +23,13 @@ type client struct {
23 }
24
25 // FIXME(brian): is this method meant to simulate putting a value into the network?
25 -func (c *client) PutValue(ctx context.Context, key u.Key, val []byte) error {
26 +func (c *client) PutValue(ctx context.Context, key key.Key, val []byte) error {
27 log.Debugf("PutValue: %s", key)
28 return c.datastore.Put(key.DsKey(), val)
29 }
30
31 // FIXME(brian): is this method meant to simulate getting a value from the network?
31 -func (c *client) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
32 +func (c *client) GetValue(ctx context.Context, key key.Key) ([]byte, error) {
33 log.Debugf("GetValue: %s", key)
34 v, err := c.datastore.Get(key.DsKey())
35 if err != nil {
@@ -43,7 +44,7 @@ func (c *client) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
44 return data, nil
45 }
46
46 -func (c *client) FindProviders(ctx context.Context, key u.Key) ([]peer.PeerInfo, error) {
47 +func (c *client) FindProviders(ctx context.Context, key key.Key) ([]peer.PeerInfo, error) {
48 return c.server.Providers(key), nil
49 }
50
@@ -52,7 +53,7 @@ func (c *client) FindPeer(ctx context.Context, pid peer.ID) (peer.PeerInfo, erro
53 return peer.PeerInfo{}, nil
54 }
55
55 -func (c *client) FindProvidersAsync(ctx context.Context, k u.Key, max int) <-chan peer.PeerInfo {
56 +func (c *client) FindProvidersAsync(ctx context.Context, k key.Key, max int) <-chan peer.PeerInfo {
57 out := make(chan peer.PeerInfo)
58 go func() {
59 defer close(out)
@@ -72,7 +73,7 @@ func (c *client) FindProvidersAsync(ctx context.Context, k u.Key, max int) <-cha
73
74 // Provide returns once the message is on the network. Value is not necessarily
75 // visible yet.
75 -func (c *client) Provide(_ context.Context, key u.Key) error {
76 +func (c *client) Provide(_ context.Context, key key.Key) error {
77 info := peer.PeerInfo{
78 ID: c.peer.ID(),
79 Addrs: []ma.Multiaddr{c.peer.Address()},
routing/mock/centralized_server.go
+6 -6
@@ -7,15 +7,15 @@ import (
7
8 ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
9 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10 + key "github.com/ipfs/go-ipfs/blocks/key"
11 peer "github.com/ipfs/go-ipfs/p2p/peer"
11 - u "github.com/ipfs/go-ipfs/util"
12 "github.com/ipfs/go-ipfs/util/testutil"
13 )
14
15 // server is the mockrouting.Client's private interface to the routing server
16 type server interface {
17 - Announce(peer.PeerInfo, u.Key) error
18 - Providers(u.Key) []peer.PeerInfo
17 + Announce(peer.PeerInfo, key.Key) error
18 + Providers(key.Key) []peer.PeerInfo
19
20 Server
21 }
@@ -25,7 +25,7 @@ type s struct {
25 delayConf DelayConfig
26
27 lock sync.RWMutex
28 - providers map[u.Key]map[peer.ID]providerRecord
28 + providers map[key.Key]map[peer.ID]providerRecord
29 }
30
31 type providerRecord struct {
@@ -33,7 +33,7 @@ type providerRecord struct {
33 Created time.Time
34 }
35
36 -func (rs *s) Announce(p peer.PeerInfo, k u.Key) error {
36 +func (rs *s) Announce(p peer.PeerInfo, k key.Key) error {
37 rs.lock.Lock()
38 defer rs.lock.Unlock()
39
@@ -48,7 +48,7 @@ func (rs *s) Announce(p peer.PeerInfo, k u.Key) error {
48 return nil
49 }
50
51 -func (rs *s) Providers(k u.Key) []peer.PeerInfo {
51 +func (rs *s) Providers(k key.Key) []peer.PeerInfo {
52 rs.delayConf.Query.Wait() // before locking
53
54 rs.lock.RLock()
routing/mock/centralized_test.go
+7 -7
@@ -5,16 +5,16 @@ import (
5 "time"
6
7 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
8 + key "github.com/ipfs/go-ipfs/blocks/key"
9 peer "github.com/ipfs/go-ipfs/p2p/peer"
10 delay "github.com/ipfs/go-ipfs/thirdparty/delay"
10 - u "github.com/ipfs/go-ipfs/util"
11 "github.com/ipfs/go-ipfs/util/testutil"
12 )
13
14 func TestKeyNotFound(t *testing.T) {
15
16 var pi = testutil.RandIdentityOrFatal(t)
17 - var key = u.Key("mock key")
17 + var key = key.Key("mock key")
18 var ctx = context.Background()
19
20 rs := NewServer()
@@ -30,7 +30,7 @@ func TestClientFindProviders(t *testing.T) {
30 rs := NewServer()
31 client := rs.Client(pi)
32
33 - k := u.Key("hello")
33 + k := key.Key("hello")
34 err := client.Provide(context.Background(), k)
35 if err != nil {
36 t.Fatal(err)
@@ -40,7 +40,7 @@ func TestClientFindProviders(t *testing.T) {
40 time.Sleep(time.Millisecond * 300)
41 max := 100
42
43 - providersFromClient := client.FindProvidersAsync(context.Background(), u.Key("hello"), max)
43 + providersFromClient := client.FindProvidersAsync(context.Background(), key.Key("hello"), max)
44 isInClient := false
45 for pi := range providersFromClient {
46 if pi.ID == pi.ID {
@@ -54,7 +54,7 @@ func TestClientFindProviders(t *testing.T) {
54
55 func TestClientOverMax(t *testing.T) {
56 rs := NewServer()
57 - k := u.Key("hello")
57 + k := key.Key("hello")
58 numProvidersForHelloKey := 100
59 for i := 0; i < numProvidersForHelloKey; i++ {
60 pi := testutil.RandIdentityOrFatal(t)
@@ -81,7 +81,7 @@ func TestClientOverMax(t *testing.T) {
81 // TODO does dht ensure won't receive self as a provider? probably not.
82 func TestCanceledContext(t *testing.T) {
83 rs := NewServer()
84 - k := u.Key("hello")
84 + k := key.Key("hello")
85
86 // avoid leaking goroutine, without using the context to signal
87 // (we want the goroutine to keep trying to publish on a
@@ -139,7 +139,7 @@ func TestCanceledContext(t *testing.T) {
139 func TestValidAfter(t *testing.T) {
140
141 pi := testutil.RandIdentityOrFatal(t)
142 - var key = u.Key("mock key")
142 + var key = key.Key("mock key")
143 var ctx = context.Background()
144 conf := DelayConfig{
145 ValueVisibility: delay.Fixed(1 * time.Hour),
routing/mock/interface.go
+3 -3
@@ -7,10 +7,10 @@ package mockrouting
7 import (
8 ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
9 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10 + key "github.com/ipfs/go-ipfs/blocks/key"
11 peer "github.com/ipfs/go-ipfs/p2p/peer"
12 routing "github.com/ipfs/go-ipfs/routing"
13 delay "github.com/ipfs/go-ipfs/thirdparty/delay"
13 - u "github.com/ipfs/go-ipfs/util"
14 "github.com/ipfs/go-ipfs/util/testutil"
15 )
16
@@ -22,7 +22,7 @@ type Server interface {
22
23 // Client implements IpfsRouting
24 type Client interface {
25 - FindProviders(context.Context, u.Key) ([]peer.PeerInfo, error)
25 + FindProviders(context.Context, key.Key) ([]peer.PeerInfo, error)
26 routing.IpfsRouting
27 }
28
@@ -37,7 +37,7 @@ func NewServer() Server {
37 // NewServerWithDelay returns a mockrouting Server with a delay!
38 func NewServerWithDelay(conf DelayConfig) Server {
39 return &s{
40 - providers: make(map[u.Key]map[peer.ID]providerRecord),
40 + providers: make(map[key.Key]map[peer.ID]providerRecord),
41 delayConf: conf,
42 }
43 }
routing/offline/offline.go
+6 -6
@@ -7,13 +7,13 @@ import (
7 proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
8 ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
9 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10 + key "github.com/ipfs/go-ipfs/blocks/key"
11 ci "github.com/ipfs/go-ipfs/p2p/crypto"
12 "github.com/ipfs/go-ipfs/p2p/peer"
13 routing "github.com/ipfs/go-ipfs/routing"
14 pb "github.com/ipfs/go-ipfs/routing/dht/pb"
15 record "github.com/ipfs/go-ipfs/routing/record"
16 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
16 - u "github.com/ipfs/go-ipfs/util"
17 )
18
19 var log = eventlog.Logger("offlinerouting")
@@ -35,7 +35,7 @@ type offlineRouting struct {
35 sk ci.PrivKey
36 }
37
38 -func (c *offlineRouting) PutValue(ctx context.Context, key u.Key, val []byte) error {
38 +func (c *offlineRouting) PutValue(ctx context.Context, key key.Key, val []byte) error {
39 rec, err := record.MakePutRecord(c.sk, key, val, false)
40 if err != nil {
41 return err
@@ -48,7 +48,7 @@ func (c *offlineRouting) PutValue(ctx context.Context, key u.Key, val []byte) er
48 return c.datastore.Put(key.DsKey(), data)
49 }
50
51 -func (c *offlineRouting) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
51 +func (c *offlineRouting) GetValue(ctx context.Context, key key.Key) ([]byte, error) {
52 v, err := c.datastore.Get(key.DsKey())
53 if err != nil {
54 return nil, err
@@ -67,7 +67,7 @@ func (c *offlineRouting) GetValue(ctx context.Context, key u.Key) ([]byte, error
67 return rec.GetValue(), nil
68 }
69
70 -func (c *offlineRouting) FindProviders(ctx context.Context, key u.Key) ([]peer.PeerInfo, error) {
70 +func (c *offlineRouting) FindProviders(ctx context.Context, key key.Key) ([]peer.PeerInfo, error) {
71 return nil, ErrOffline
72 }
73
@@ -75,13 +75,13 @@ func (c *offlineRouting) FindPeer(ctx context.Context, pid peer.ID) (peer.PeerIn
75 return peer.PeerInfo{}, ErrOffline
76 }
77
78 -func (c *offlineRouting) FindProvidersAsync(ctx context.Context, k u.Key, max int) <-chan peer.PeerInfo {
78 +func (c *offlineRouting) FindProvidersAsync(ctx context.Context, k key.Key, max int) <-chan peer.PeerInfo {
79 out := make(chan peer.PeerInfo)
80 close(out)
81 return out
82 }
83
84 -func (c *offlineRouting) Provide(_ context.Context, key u.Key) error {
84 +func (c *offlineRouting) Provide(_ context.Context, key key.Key) error {
85 return ErrOffline
86 }
87
routing/record/record.go
+2 -2
@@ -5,16 +5,16 @@ import (
5
6 proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
7
8 + key "github.com/ipfs/go-ipfs/blocks/key"
9 ci "github.com/ipfs/go-ipfs/p2p/crypto"
10 pb "github.com/ipfs/go-ipfs/routing/dht/pb"
11 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
11 - u "github.com/ipfs/go-ipfs/util"
12 )
13
14 var log = eventlog.Logger("routing/record")
15
16 // MakePutRecord creates and signs a dht record for the given key/value pair
17 -func MakePutRecord(sk ci.PrivKey, key u.Key, value []byte, sign bool) (*pb.Record, error) {
17 +func MakePutRecord(sk ci.PrivKey, key key.Key, value []byte, sign bool) (*pb.Record, error) {
18 record := new(pb.Record)
19
20 record.Key = proto.String(string(key))
routing/record/validation.go
+6 -5
@@ -5,6 +5,7 @@ import (
5 "errors"
6 "strings"
7
8 + key "github.com/ipfs/go-ipfs/blocks/key"
9 ci "github.com/ipfs/go-ipfs/p2p/crypto"
10 pb "github.com/ipfs/go-ipfs/routing/dht/pb"
11 u "github.com/ipfs/go-ipfs/util"
@@ -12,7 +13,7 @@ import (
13
14 // ValidatorFunc is a function that is called to validate a given
15 // type of DHTRecord.
15 -type ValidatorFunc func(u.Key, []byte) error
16 +type ValidatorFunc func(key.Key, []byte) error
17
18 // ErrBadRecord is returned any time a dht record is found to be
19 // incorrectly formatted or signed.
@@ -38,7 +39,7 @@ func (v Validator) VerifyRecord(r *pb.Record) error {
39 // Now, check validity func
40 parts := strings.Split(r.GetKey(), "/")
41 if len(parts) < 3 {
41 - log.Infof("Record key does not have validator: %s", u.Key(r.GetKey()))
42 + log.Infof("Record key does not have validator: %s", key.Key(r.GetKey()))
43 return nil
44 }
45
@@ -48,10 +49,10 @@ func (v Validator) VerifyRecord(r *pb.Record) error {
49 return ErrInvalidRecordType
50 }
51
51 - return val.Func(u.Key(r.GetKey()), r.GetValue())
52 + return val.Func(key.Key(r.GetKey()), r.GetValue())
53 }
54
54 -func (v Validator) IsSigned(k u.Key) (bool, error) {
55 +func (v Validator) IsSigned(k key.Key) (bool, error) {
56 // Now, check validity func
57 parts := strings.Split(string(k), "/")
58 if len(parts) < 3 {
@@ -71,7 +72,7 @@ func (v Validator) IsSigned(k u.Key) (bool, error) {
72 // ValidatePublicKeyRecord implements ValidatorFunc and
73 // verifies that the passed in record value is the PublicKey
74 // that matches the passed in key.
74 -func ValidatePublicKeyRecord(k u.Key, val []byte) error {
75 +func ValidatePublicKeyRecord(k key.Key, val []byte) error {
76 keyparts := bytes.Split([]byte(k), []byte("/"))
77 if len(keyparts) < 3 {
78 return errors.New("invalid key")
routing/routing.go
+8 -8
@@ -6,9 +6,9 @@ import (
6 "time"
7
8 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
9 + key "github.com/ipfs/go-ipfs/blocks/key"
10 ci "github.com/ipfs/go-ipfs/p2p/crypto"
11 peer "github.com/ipfs/go-ipfs/p2p/peer"
11 - u "github.com/ipfs/go-ipfs/util"
12 )
13
14 // ErrNotFound is returned when a search fails to find anything
@@ -17,21 +17,21 @@ var ErrNotFound = errors.New("routing: not found")
17 // IpfsRouting is the routing module interface
18 // It is implemented by things like DHTs, etc.
19 type IpfsRouting interface {
20 - FindProvidersAsync(context.Context, u.Key, int) <-chan peer.PeerInfo
20 + FindProvidersAsync(context.Context, key.Key, int) <-chan peer.PeerInfo
21
22 // Basic Put/Get
23
24 // PutValue adds value corresponding to given Key.
25 - PutValue(context.Context, u.Key, []byte) error
25 + PutValue(context.Context, key.Key, []byte) error
26
27 // GetValue searches for the value corresponding to given Key.
28 - GetValue(context.Context, u.Key) ([]byte, error)
28 + GetValue(context.Context, key.Key) ([]byte, error)
29
30 // Value provider layer of indirection.
31 // This is what DSHTs (Coral and MainlineDHT) do to store large values in a DHT.
32
33 // Announce that this node can provide value for given key
34 - Provide(context.Context, u.Key) error
34 + Provide(context.Context, key.Key) error
35
36 // Find specific Peer
37 // FindPeer searches for a peer with given ID, returns a peer.PeerInfo
@@ -54,8 +54,8 @@ type PubKeyFetcher interface {
54
55 // KeyForPublicKey returns the key used to retrieve public keys
56 // from the dht.
57 -func KeyForPublicKey(id peer.ID) u.Key {
58 - return u.Key("/pk/" + string(id))
57 +func KeyForPublicKey(id peer.ID) key.Key {
58 + return key.Key("/pk/" + string(id))
59 }
60
61 func GetPublicKey(r IpfsRouting, ctx context.Context, pkhash []byte) (ci.PubKey, error) {
@@ -63,7 +63,7 @@ func GetPublicKey(r IpfsRouting, ctx context.Context, pkhash []byte) (ci.PubKey,
63 // If we have a DHT as our routing system, use optimized fetcher
64 return dht.GetPublicKey(ctx, peer.ID(pkhash))
65 } else {
66 - key := u.Key("/pk/" + string(pkhash))
66 + key := key.Key("/pk/" + string(pkhash))
67 pkval, err := r.GetValue(ctx, key)
68 if err != nil {
69 return nil, err
routing/supernode/client.go
+6 -6
@@ -8,13 +8,13 @@ import (
8 proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
9 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10
11 + key "github.com/ipfs/go-ipfs/blocks/key"
12 "github.com/ipfs/go-ipfs/p2p/host"
13 peer "github.com/ipfs/go-ipfs/p2p/peer"
14 routing "github.com/ipfs/go-ipfs/routing"
15 pb "github.com/ipfs/go-ipfs/routing/dht/pb"
16 proxy "github.com/ipfs/go-ipfs/routing/supernode/proxy"
17 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
17 - u "github.com/ipfs/go-ipfs/util"
18 )
19
20 var log = eventlog.Logger("supernode")
@@ -36,7 +36,7 @@ func NewClient(px proxy.Proxy, h host.Host, ps peer.Peerstore, local peer.ID) (*
36 }, nil
37 }
38
39 -func (c *Client) FindProvidersAsync(ctx context.Context, k u.Key, max int) <-chan peer.PeerInfo {
39 +func (c *Client) FindProvidersAsync(ctx context.Context, k key.Key, max int) <-chan peer.PeerInfo {
40 ctx = eventlog.ContextWithLoggable(ctx, eventlog.Uuid("findProviders"))
41 defer log.EventBegin(ctx, "findProviders", &k).Done()
42 ch := make(chan peer.PeerInfo)
@@ -60,7 +60,7 @@ func (c *Client) FindProvidersAsync(ctx context.Context, k u.Key, max int) <-cha
60 return ch
61 }
62
63 -func (c *Client) PutValue(ctx context.Context, k u.Key, v []byte) error {
63 +func (c *Client) PutValue(ctx context.Context, k key.Key, v []byte) error {
64 defer log.EventBegin(ctx, "putValue", &k).Done()
65 r, err := makeRecord(c.peerstore, c.local, k, v)
66 if err != nil {
@@ -71,7 +71,7 @@ func (c *Client) PutValue(ctx context.Context, k u.Key, v []byte) error {
71 return c.proxy.SendMessage(ctx, pmes) // wrap to hide the remote
72 }
73
74 -func (c *Client) GetValue(ctx context.Context, k u.Key) ([]byte, error) {
74 +func (c *Client) GetValue(ctx context.Context, k key.Key) ([]byte, error) {
75 defer log.EventBegin(ctx, "getValue", &k).Done()
76 msg := pb.NewMessage(pb.Message_GET_VALUE, string(k), 0)
77 response, err := c.proxy.SendRequest(ctx, msg) // TODO wrap to hide the remote
@@ -81,7 +81,7 @@ func (c *Client) GetValue(ctx context.Context, k u.Key) ([]byte, error) {
81 return response.Record.GetValue(), nil
82 }
83
84 -func (c *Client) Provide(ctx context.Context, k u.Key) error {
84 +func (c *Client) Provide(ctx context.Context, k key.Key) error {
85 defer log.EventBegin(ctx, "provide", &k).Done()
86 msg := pb.NewMessage(pb.Message_ADD_PROVIDER, string(k), 0)
87 // FIXME how is connectedness defined for the local node
@@ -113,7 +113,7 @@ func (c *Client) FindPeer(ctx context.Context, id peer.ID) (peer.PeerInfo, error
113 }
114
115 // creates and signs a record for the given key/value pair
116 -func makeRecord(ps peer.Peerstore, p peer.ID, k u.Key, v []byte) (*pb.Record, error) {
116 +func makeRecord(ps peer.Peerstore, p peer.ID, k key.Key, v []byte) (*pb.Record, error) {
117 blob := bytes.Join([][]byte{[]byte(k), v, []byte(p)}, []byte{})
118 sig, err := ps.PrivKey(p).Sign(blob)
119 if err != nil {
routing/supernode/proxy/standard.go
+3 -3
@@ -6,13 +6,13 @@ import (
6 ggio "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/io"
7 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
8
9 + key "github.com/ipfs/go-ipfs/blocks/key"
10 host "github.com/ipfs/go-ipfs/p2p/host"
11 inet "github.com/ipfs/go-ipfs/p2p/net"
12 peer "github.com/ipfs/go-ipfs/p2p/peer"
13 dhtpb "github.com/ipfs/go-ipfs/routing/dht/pb"
14 kbucket "github.com/ipfs/go-ipfs/routing/kbucket"
15 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
15 - util "github.com/ipfs/go-ipfs/util"
16 )
17
18 const ProtocolSNR = "/ipfs/supernoderouting"
@@ -162,7 +162,7 @@ func (px *standard) sendRequest(ctx context.Context, m *dhtpb.Message, remote pe
162 return response, nil
163 }
164
165 -func sortedByKey(peers []peer.ID, key string) []peer.ID {
166 - target := kbucket.ConvertKey(util.Key(key))
165 +func sortedByKey(peers []peer.ID, skey string) []peer.ID {
166 + target := kbucket.ConvertKey(key.Key(skey))
167 return kbucket.SortClosestPeers(peers, target)
168 }
routing/supernode/server.go
+10 -10
@@ -8,11 +8,11 @@ import (
8 datastore "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
9 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10
11 + key "github.com/ipfs/go-ipfs/blocks/key"
12 peer "github.com/ipfs/go-ipfs/p2p/peer"
13 dhtpb "github.com/ipfs/go-ipfs/routing/dht/pb"
14 record "github.com/ipfs/go-ipfs/routing/record"
15 proxy "github.com/ipfs/go-ipfs/routing/supernode/proxy"
15 - util "github.com/ipfs/go-ipfs/util"
16 )
17
18 // Server handles routing queries using a database backend
@@ -53,7 +53,7 @@ func (s *Server) handleMessage(
53 switch req.GetType() {
54
55 case dhtpb.Message_GET_VALUE:
56 - rawRecord, err := getRoutingRecord(s.routingBackend, util.Key(req.GetKey()))
56 + rawRecord, err := getRoutingRecord(s.routingBackend, key.Key(req.GetKey()))
57 if err != nil {
58 return "", nil
59 }
@@ -67,7 +67,7 @@ func (s *Server) handleMessage(
67 // log.Event(ctx, "validationFailed", req, p)
68 // return "", nil
69 // }
70 - putRoutingRecord(s.routingBackend, util.Key(req.GetKey()), req.GetRecord())
70 + putRoutingRecord(s.routingBackend, key.Key(req.GetKey()), req.GetRecord())
71 return p, req
72
73 case dhtpb.Message_FIND_NODE:
@@ -87,7 +87,7 @@ func (s *Server) handleMessage(
87 if providerID == p {
88 store := []*dhtpb.Message_Peer{provider}
89 storeProvidersToPeerstore(s.peerstore, p, store)
90 - if err := putRoutingProviders(s.routingBackend, util.Key(req.GetKey()), store); err != nil {
90 + if err := putRoutingProviders(s.routingBackend, key.Key(req.GetKey()), store); err != nil {
91 return "", nil
92 }
93 } else {
@@ -97,7 +97,7 @@ func (s *Server) handleMessage(
97 return "", nil
98
99 case dhtpb.Message_GET_PROVIDERS:
100 - providers, err := getRoutingProviders(s.routingBackend, util.Key(req.GetKey()))
100 + providers, err := getRoutingProviders(s.routingBackend, key.Key(req.GetKey()))
101 if err != nil {
102 return "", nil
103 }
@@ -114,7 +114,7 @@ func (s *Server) handleMessage(
114 var _ proxy.RequestHandler = &Server{}
115 var _ proxy.Proxy = &Server{}
116
117 -func getRoutingRecord(ds datastore.Datastore, k util.Key) (*dhtpb.Record, error) {
117 +func getRoutingRecord(ds datastore.Datastore, k key.Key) (*dhtpb.Record, error) {
118 dskey := k.DsKey()
119 val, err := ds.Get(dskey)
120 if err != nil {
@@ -131,7 +131,7 @@ func getRoutingRecord(ds datastore.Datastore, k util.Key) (*dhtpb.Record, error)
131 return &record, nil
132 }
133
134 -func putRoutingRecord(ds datastore.Datastore, k util.Key, value *dhtpb.Record) error {
134 +func putRoutingRecord(ds datastore.Datastore, k key.Key, value *dhtpb.Record) error {
135 data, err := proto.Marshal(value)
136 if err != nil {
137 return err
@@ -144,7 +144,7 @@ func putRoutingRecord(ds datastore.Datastore, k util.Key, value *dhtpb.Record) e
144 return nil
145 }
146
147 -func putRoutingProviders(ds datastore.Datastore, k util.Key, newRecords []*dhtpb.Message_Peer) error {
147 +func putRoutingProviders(ds datastore.Datastore, k key.Key, newRecords []*dhtpb.Message_Peer) error {
148 log.Event(context.Background(), "putRoutingProviders", &k)
149 oldRecords, err := getRoutingProviders(ds, k)
150 if err != nil {
@@ -183,7 +183,7 @@ func storeProvidersToPeerstore(ps peer.Peerstore, p peer.ID, providers []*dhtpb.
183 }
184 }
185
186 -func getRoutingProviders(ds datastore.Datastore, k util.Key) ([]*dhtpb.Message_Peer, error) {
186 +func getRoutingProviders(ds datastore.Datastore, k key.Key) ([]*dhtpb.Message_Peer, error) {
187 e := log.EventBegin(context.Background(), "getProviders", &k)
188 defer e.Done()
189 var providers []*dhtpb.Message_Peer
@@ -199,7 +199,7 @@ func getRoutingProviders(ds datastore.Datastore, k util.Key) ([]*dhtpb.Message_P
199 return providers, nil
200 }
201
202 -func providerKey(k util.Key) datastore.Key {
202 +func providerKey(k key.Key) datastore.Key {
203 return datastore.KeyWithNamespaces([]string{"routing", "providers", k.String()})
204 }
205
routing/supernode/server_test.go
+2 -2
@@ -4,13 +4,13 @@ import (
4 "testing"
5
6 datastore "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
7 + key "github.com/ipfs/go-ipfs/blocks/key"
8 dhtpb "github.com/ipfs/go-ipfs/routing/dht/pb"
8 - "github.com/ipfs/go-ipfs/util"
9 )
10
11 func TestPutProviderDoesntResultInDuplicates(t *testing.T) {
12 routingBackend := datastore.NewMapDatastore()
13 - k := util.Key("foo")
13 + k := key.Key("foo")
14 put := []*dhtpb.Message_Peer{
15 convPeer("bob", "127.0.0.1/tcp/4001"),
16 convPeer("alice", "10.0.0.10/tcp/4001"),
test/integration/grandcentral_test.go
+2 -2
@@ -12,6 +12,7 @@ import (
12 syncds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
13 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
14
15 + key "github.com/ipfs/go-ipfs/blocks/key"
16 core "github.com/ipfs/go-ipfs/core"
17 "github.com/ipfs/go-ipfs/core/corerouting"
18 "github.com/ipfs/go-ipfs/core/coreunix"
@@ -19,7 +20,6 @@ import (
20 "github.com/ipfs/go-ipfs/p2p/peer"
21 "github.com/ipfs/go-ipfs/thirdparty/iter"
22 "github.com/ipfs/go-ipfs/thirdparty/unit"
22 - "github.com/ipfs/go-ipfs/util"
23 ds2 "github.com/ipfs/go-ipfs/util/datastore2"
24 testutil "github.com/ipfs/go-ipfs/util/testutil"
25 )
@@ -166,7 +166,7 @@ func RunSupernodePutRecordGetRecord(conf testutil.LatencyConfig) error {
166 putter := clients[0]
167 getter := clients[1]
168
169 - k := util.Key("key")
169 + k := key.Key("key")
170 note := []byte("a note from putter")
171
172 if err := putter.Routing.PutValue(ctx, k, note); err != nil {
unixfs/io/dirbuilder.go
+2 -2
@@ -5,9 +5,9 @@ import (
5
6 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
7
8 + key "github.com/ipfs/go-ipfs/blocks/key"
9 mdag "github.com/ipfs/go-ipfs/merkledag"
10 format "github.com/ipfs/go-ipfs/unixfs"
10 - u "github.com/ipfs/go-ipfs/util"
11 )
12
13 type directoryBuilder struct {
@@ -29,7 +29,7 @@ func NewDirectory(dserv mdag.DAGService) *directoryBuilder {
29 }
30
31 // AddChild adds a (name, key)-pair to the root node.
32 -func (d *directoryBuilder) AddChild(name string, k u.Key) error {
32 +func (d *directoryBuilder) AddChild(name string, k key.Key) error {
33 // TODO(cryptix): consolidate context managment
34 ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
35 defer cancel()
unixfs/mod/dagmodifier.go
+3 -2
@@ -11,6 +11,7 @@ import (
11 mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
12 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
13
14 + key "github.com/ipfs/go-ipfs/blocks/key"
15 imp "github.com/ipfs/go-ipfs/importer"
16 chunk "github.com/ipfs/go-ipfs/importer/chunk"
17 help "github.com/ipfs/go-ipfs/importer/helpers"
@@ -226,7 +227,7 @@ func (dm *DagModifier) Sync() error {
227 // modifyDag writes the data in 'data' over the data in 'node' starting at 'offset'
228 // returns the new key of the passed in node and whether or not all the data in the reader
229 // has been consumed.
229 -func (dm *DagModifier) modifyDag(node *mdag.Node, offset uint64, data io.Reader) (u.Key, bool, error) {
230 +func (dm *DagModifier) modifyDag(node *mdag.Node, offset uint64, data io.Reader) (key.Key, bool, error) {
231 f, err := ft.FromBytes(node.Data)
232 if err != nil {
233 return "", false, err
@@ -266,7 +267,7 @@ func (dm *DagModifier) modifyDag(node *mdag.Node, offset uint64, data io.Reader)
267 // We found the correct child to write into
268 if cur+bs > offset {
269 // Unpin block
269 - ckey := u.Key(node.Links[i].Hash)
270 + ckey := key.Key(node.Links[i].Hash)
271 dm.mp.RemovePinWithMode(ckey, pin.Indirect)
272
273 child, err := node.Links[i].GetNode(dm.ctx, dm.dagserv)
unixfs/mod/dagmodifier_test.go
+4 -3
@@ -10,6 +10,7 @@ import (
10
11 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
12 "github.com/ipfs/go-ipfs/blocks/blockstore"
13 + key "github.com/ipfs/go-ipfs/blocks/key"
14 bs "github.com/ipfs/go-ipfs/blockservice"
15 "github.com/ipfs/go-ipfs/exchange/offline"
16 imp "github.com/ipfs/go-ipfs/importer"
@@ -574,10 +575,10 @@ func TestCorrectPinning(t *testing.T) {
575
576 }
577
577 -func enumerateChildren(t *testing.T, nd *mdag.Node, ds mdag.DAGService) []u.Key {
578 - var out []u.Key
578 +func enumerateChildren(t *testing.T, nd *mdag.Node, ds mdag.DAGService) []key.Key {
579 + var out []key.Key
580 for _, lnk := range nd.Links {
580 - out = append(out, u.Key(lnk.Hash))
581 + out = append(out, key.Key(lnk.Hash))
582 child, err := lnk.GetNode(context.Background(), ds)
583 if err != nil {
584 t.Fatal(err)
util/util.go
+36
@@ -12,7 +12,9 @@ import (
12 "strings"
13 "time"
14
15 + b58 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
16 ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
17 + mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
18
19 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir"
20 )
@@ -126,3 +128,37 @@ func RPartition(subject string, sep string) (string, string, string) {
128 return subject, "", ""
129 }
130 }
131 +
132 +// Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits
133 +func Hash(data []byte) mh.Multihash {
134 + h, err := mh.Sum(data, mh.SHA2_256, -1)
135 + if err != nil {
136 + // this error can be safely ignored (panic) because multihash only fails
137 + // from the selection of hash function. If the fn + length are valid, it
138 + // won't error.
139 + panic("multihash failed to hash using SHA2_256.")
140 + }
141 + return h
142 +}
143 +
144 +// IsValidHash checks whether a given hash is valid (b58 decodable, len > 0)
145 +func IsValidHash(s string) bool {
146 + out := b58.Decode(s)
147 + if out == nil || len(out) == 0 {
148 + return false
149 + }
150 + _, err := mh.Cast(out)
151 + if err != nil {
152 + return false
153 + }
154 + return true
155 +}
156 +
157 +// XOR takes two byte slices, XORs them together, returns the resulting slice.
158 +func XOR(a, b []byte) []byte {
159 + c := make([]byte, len(a))
160 + for i := 0; i < len(a); i++ {
161 + c[i] = a[i] ^ b[i]
162 + }
163 + return c
164 +}
util/util_test.go
-22
@@ -3,30 +3,8 @@ package util
3 import (
4 "bytes"
5 "testing"
6 -
7 - mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
6 )
7
10 -func TestKey(t *testing.T) {
11 -
12 - h1, err := mh.Sum([]byte("beep boop"), mh.SHA2_256, -1)
13 - if err != nil {
14 - t.Error(err)
15 - }
16 -
17 - k1 := Key(h1)
18 - h2 := mh.Multihash(k1)
19 - k2 := Key(h2)
20 -
21 - if !bytes.Equal(h1, h2) {
22 - t.Error("Multihashes not equal.")
23 - }
24 -
25 - if k1 != k2 {
26 - t.Error("Keys not equal.")
27 - }
28 -}
29 -
8 func TestXOR(t *testing.T) {
9 cases := [][3][]byte{
10 {