Example #1
0
// DelBlock deletes the block pointed to by `hash`.
func DelBlock(node *Node, hash gmh.Multihash) error {
	nd, err := node.proc()
	if err != nil {
		log.Warningf("ipfs block-del: %v", err)
		return err
	}

	k := key.B58KeyDecode(hash.B58String())
	return nd.Blocks.DeleteBlock(k)
}
Example #2
0
// Locate finds the object pointed to by `hash`. It will wait
// for max `timeout` duration if it got less than `n` items in that time.
// If `n` is less than 0, all reachable peers that have `hash` will be returned.
// If `n` is 0, Locate will return immeditately.
// This operation requires online-mode.
func Locate(node *Node, hash gmh.Multihash, n int, t time.Duration) ([]*PeerInfo, error) {
	if n == 0 {
		return []*PeerInfo{}, nil
	}

	// Note: Do not use Maxint32. That makes ipfs allocate
	//       a whole lot of memory. Just assume that 100 is fine.
	if n < 0 {
		n = 100
	}

	if !node.IsOnline() {
		return nil, ErrIsOffline
	}

	nd, err := node.proc()
	if err != nil {
		log.Warningf("ipfs dht: %v", err)
		return nil, err
	}

	dht, ok := nd.Routing.(*ipdht.IpfsDHT)
	if !ok {
		return nil, commands.ErrNotDHT
	}

	ctx, cancel := context.WithTimeout(node.Context, t)
	defer cancel()

	k := key.B58KeyDecode(hash.B58String())
	peers := dht.FindProvidersAsync(ctx, k, n)
	infos := []*PeerInfo{}

	for info := range peers {
		// Converting equal struct into each other is my favourite thing.
		peerInfo := &PeerInfo{
			ID:     info.ID.Pretty(),
			PubKey: node.ipfsNode.Peerstore.PubKey(info.ID),
		}

		for _, addr := range info.Addrs {
			peerInfo.Addrs = append(peerInfo.Addrs, ma.Cast(addr.Bytes()))
		}

		infos = append(infos, peerInfo)
	}

	return infos, nil
}
Example #3
0
// CatBlock retuns the data stored in the block pointed to by `hash`.
// It will timeout with util.ErrTimeout if the operation takes too long,
// this includes querying for an non-existing hash.
//
// This operation works offline and online, but if the block is stored
// elsewhere on the net, node must be online to find the block.
func CatBlock(node *Node, hash gmh.Multihash, timeout time.Duration) ([]byte, error) {
	nd, err := node.proc()
	if err != nil {
		log.Warningf("ipfs block-cat: %v", err)
		return nil, err
	}

	ctx, cancel := context.WithTimeout(node.Context, timeout)
	defer cancel()

	k := key.B58KeyDecode(hash.B58String())
	block, err := nd.Blocks.GetBlock(ctx, k)
	if err == context.DeadlineExceeded {
		return nil, util.ErrTimeout
	}

	if err != nil {
		return nil, err
	}

	return block.Data(), nil
}