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 }
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") } }
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") } }
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)) } }
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") } }
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())) } }