Exemplo n.º 1
0
func main() {
	port, firstContact := parseFlags()

	if port == nil {
		panic("Must supply desired port number")
	}

	fmt.Println("Initializing Kademlia DHT ...")

	selfID := kademlia.NewRandomNodeID()

	selfAddress := fmt.Sprintf("127.0.0.1:%d", *port)
	self := kademlia.NewContact(selfID, selfAddress)
	fmt.Println("Self:", selfID, selfAddress)

	selfNetwork := kademlia.NewKademlia(self, "Certcoin-DHT")

	selfNetwork.Serve()

	if firstContact != nil {
		contacts, err := selfNetwork.Bootstrap(*firstContact, self)
		if err != nil {
			fmt.Println("Bootstrap error:", err)
		}

		final := make(chan kademlia.Contacts)
		selfNetwork.IterativeFindNode(firstContact.ID, kademlia.Delta, final)
		contacts = <-final
		fmt.Println("Iterative Find Node:", contacts)
	}

	done := make(chan bool)
	_ = <-done
}
Exemplo n.º 2
0
func TestContactsSwap(t *testing.T) {
	contact0 := kademlia.NewContact(kademlia.NewRandomNodeID(), "")
	contact1 := kademlia.NewContact(kademlia.NewRandomNodeID(), "")
	contacts := kademlia.Contacts{contact0, contact1}

	// swap once
	contacts.Swap(0, 1)
	if contact1 != contacts[0] || contact0 != contacts[1] {
		t.Error("Contacts were not swapped properly")
	}

	// return to original ordering
	contacts.Swap(0, 1)
	if contact0 != contacts[0] || contact1 != contacts[1] {
		t.Error("Contacts were not swapped properly")
	}
}
Exemplo n.º 3
0
func TestContactsPush(t *testing.T) {
	contactToAdd := kademlia.NewContact(kademlia.NewRandomNodeID(), "")
	contacts := kademlia.Contacts{}

	contacts.Push(contactToAdd)

	if contacts.Len() != 1 {
		t.Error("Contact was not pushed to Contacts array")
	}
	if contacts[0].ID != contactToAdd.ID || contacts[0].Address != contactToAdd.Address {
		t.Error("Contact not copied correctly during Push")
	}
}
Exemplo n.º 4
0
func TestNewContact(t *testing.T) {
	expectedNodeID := kademlia.NewRandomNodeID()
	expectedAddress := "127.0.0.1:1234"

	contact := kademlia.NewContact(expectedNodeID, expectedAddress)

	if expectedNodeID != contact.ID {
		t.Error(fmt.Sprintf("Expected ID %s, got %s", expectedNodeID, contact.ID))
	}
	if expectedAddress != contact.Address {
		t.Error(fmt.Sprintf("Expected address %s, got %s", expectedAddress, contact.Address))
	}
}
Exemplo n.º 5
0
func TestContactsPop(t *testing.T) {
	contactToRemove := kademlia.NewContact(kademlia.NewRandomNodeID(), "")
	contacts := kademlia.Contacts{contactToRemove}

	removedContact := contacts.Pop().(kademlia.Contact)

	if contacts.Len() != 0 {
		t.Error("Contact was not popped from Contacts array")
	}
	if contactToRemove.ID != removedContact.ID || contactToRemove.Address != removedContact.Address {
		t.Error("Contact was not copied correctly during Pop")
	}
}
Exemplo n.º 6
0
func parseFlags() (port *int, firstContact *kademlia.Contact) {
	port = flag.Int("port", 6000, "a int")
	firstID := flag.String("first-id", "", "a hexideicimal node ID")
	firstIP := flag.String("first-ip", "", "the TCP address of an existing node")

	flag.Parse()

	if *firstID == "" || *firstIP == "" {
		firstID = nil
		firstIP = nil
	} else {
		firstContact = &kademlia.Contact{}
		*firstContact = kademlia.NewContact(kademlia.NewNodeID(*firstID), *firstIP)
	}

	return
}
Exemplo n.º 7
0
func TestContactsLen(t *testing.T) {
	expectedLen := 20
	contacts := kademlia.Contacts{}

	for i := 0; i < expectedLen; i++ {
		if contacts.Len() != i {
			t.Error(fmt.Sprintf("Contacts length should be %d, got %d", i, contacts.Len()))
		}

		id := kademlia.NewRandomNodeID()
		address := fmt.Sprintf("127.0.0.1:60%d", i)
		contacts = append(contacts, kademlia.NewContact(id, address))
	}

	if contacts.Len() != expectedLen {
		t.Error(fmt.Sprintf("Contacts length should be %d, got %d", expectedLen, contacts.Len()))
	}
}
Exemplo n.º 8
0
		contacts = append(contacts, kademlia.NewContact(id, address))
	}

	if contacts.Len() != expectedLen {
		t.Error(fmt.Sprintf("Contacts length should be %d, got %d", expectedLen, contacts.Len()))
	}
}

var contactsLessTests = []struct {
	contact1          kademlia.Contact
	contact2          kademlia.Contact
	lessThanForwards  bool
	lessThanBackwards bool
}{
	{
		kademlia.NewContact(kademlia.NewNodeID("0000000000000000000000000000000000000000"), ""),
		kademlia.NewContact(kademlia.NewNodeID("0000000000000000000000000000000000000001"), ""),
		true,
		false,
	},
	{
		kademlia.NewContact(kademlia.NewNodeID("000000000000000000000000000000000FFFFFFF"), ""),
		kademlia.NewContact(kademlia.NewNodeID("0000000000000000000000000000000000000000"), ""),
		false,
		true,
	},
	{
		kademlia.NewContact(kademlia.NewNodeID("0000000000000000000000000000000000000000"), ""),
		kademlia.NewContact(kademlia.NewNodeID("0000000000000000000000000000000000000000"), ""),
		false,
		false,