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