Exemplo n.º 1
0
func handleRemoteLocate(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) {
	locateCmd := cmd.GetRemoteLocateCommand()
	idString, peerLimit := locateCmd.Id, int(locateCmd.PeerLimit)
	timeout := time.Duration(locateCmd.TimeoutMs) * time.Millisecond

	if timeout <= 0 {
		timeout = 5 * time.Second
	}

	id, err := id.Cast(idString)
	if err != nil {
		return nil, err
	}

	peers, err := ipfsutil.Locate(d.Repo.IPFS, id.Hash(), peerLimit, timeout)
	if err != nil {
		return nil, err
	}

	resp := &wire.Response_RemoteLocateResp{}
	for _, peer := range peers {
		resp.Hashes = append(resp.Hashes, peer.ID)
	}

	return &wire.Response{
		RemoteLocateResp: resp,
	}, nil
}
Exemplo n.º 2
0
func TestDHT(t *testing.T) {
	testwith.WithIpfs(t, func(node *ipfsutil.Node) {
		if err := node.Online(); err != nil {
			t.Errorf("Failed to go online: %v", err)
			return
		}

		t.Logf("Im online.")

		data := []byte("Im a moose")
		mh, err := ipfsutil.AddBlock(node, data)
		if err != nil {
			t.Errorf("Adding block failed: %v", err)
			return
		}

		t.Logf("Added block.")

		peers, err := ipfsutil.Locate(node, mh, 1, 5*time.Second)

		t.Logf("Located.")

		if err != nil {
			t.Errorf("Looking up providers failed: %v", err)
			return
		}

		for _, peer := range peers {
			// TODO: check if
			fmt.Println(peer)
		}

		blockData, err := ipfsutil.CatBlock(node, mh, 1*time.Second)
		if err != nil {
			t.Errorf("Retrieving block failed: %v", err)
			return
		}

		if !bytes.Equal(data, blockData) {
			t.Errorf("Returned block data differs.")
			t.Errorf("\tExpect: %v", data)
			t.Errorf("\tGot:    %v", blockData)
			return
		}

		// Modify the hash and hope it there is none like that yet.
		mh[0] = 0
		_, err = ipfsutil.CatBlock(node, mh, 1*time.Second)
		if err != util.ErrTimeout {
			t.Errorf("Oops, is there really a hash like that? %v", err)
			return
		}

	})
}
Exemplo n.º 3
0
func register(node *ipfsutil.Node, id ID) error {
	hash := id.Hash()

	peers, err := ipfsutil.Locate(node, hash, 1, 5*time.Second)
	if err != nil && err != util.ErrTimeout {
		return err
	}

	// Check if some id is our own:
	if len(peers) > 0 {
		self, err := node.Identity()
		if err != nil {
			return err
		}

		wasSelf := false
		for _, peer := range peers {
			if peer.ID == self {
				wasSelf = true
			}
		}

		if wasSelf {
			return ErrAlreadyRegistered
		}
	}

	// If it was an timeout, it's probably not yet registered.
	otherHash, err := ipfsutil.AddBlock(node, id.asBlockData())
	if !bytes.Equal(otherHash, hash) {
		log.Warningf("Hash differ during register; did the hash func changed?")
	}

	if err != nil {
		return err
	}

	return nil
}