Exemple #1
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
		}

	})
}
Exemple #2
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
}