diff --git a/.gitignore b/.gitignore index d15db3cb..ad76f5bc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea/ noise.exe noise +chat vendor \ No newline at end of file diff --git a/crypto/hash.go b/crypto/hash.go index 1c04a810..4a342bc4 100644 --- a/crypto/hash.go +++ b/crypto/hash.go @@ -1,8 +1,9 @@ package crypto import ( - "golang.org/x/crypto/blake2b" "math/big" + + "golang.org/x/crypto/blake2b" ) func Hash(s *big.Int) *big.Int { diff --git a/crypto/hash_test.go b/crypto/hash_test.go new file mode 100644 index 00000000..bfee73f7 --- /dev/null +++ b/crypto/hash_test.go @@ -0,0 +1,32 @@ +package crypto + +import ( + "bytes" + "fmt" + "math/big" + "testing" +) + +func TestHash(t *testing.T) { + r := Hash(big.NewInt(123)) + + n := new(big.Int) + n, ok := n.SetString("89391711502145780362310349925943903708999319576398061903082165979787487688967", 10) + if ok { + if n.String() != r.String() { + fmt.Printf("%v \n%v \n", r, n) + panic("Hash error") + } + } else { + panic("Big Int error") + } +} + +func TestHashBytes(t *testing.T) { + r := HashBytes([]byte("123")) + n := []byte{245, 214, 123, 174, 115, 176, 225, 13, 13, 253, 48, 67, 179, 244, 241, 0, 173, 160, 20, 197, 195, 123, 213, 206, 151, 129, 59, 19, 245, 171, 43, 207} + if !bytes.Equal(n, r) { + fmt.Printf("%v \n%v \n", r, n) + panic("Hash error") + } +} diff --git a/dht/routes.go b/dht/routes.go index 57093873..c2b8f359 100644 --- a/dht/routes.go +++ b/dht/routes.go @@ -77,7 +77,7 @@ func (t *RoutingTable) Update(target peer.ID) { // Returns an unique list of all peers within the routing network (excluding yourself). func (t *RoutingTable) GetPeers() (peers []peer.ID) { visited := make(map[string]struct{}) - visited[t.self.Hex()] = struct{}{} + visited[t.self.PublicKeyHex()] = struct{}{} t.buckets.Range(func(key, value interface{}) bool { bucket := value.(*Bucket) @@ -86,9 +86,9 @@ func (t *RoutingTable) GetPeers() (peers []peer.ID) { for e := bucket.Front(); e != nil; e = e.Next() { id := e.Value.(peer.ID) - if _, seen := visited[id.Hex()]; !seen { + if _, seen := visited[id.PublicKeyHex()]; !seen { peers = append(peers, id) - visited[id.Hex()] = struct{}{} + visited[id.PublicKeyHex()] = struct{}{} } } @@ -102,7 +102,7 @@ func (t *RoutingTable) GetPeers() (peers []peer.ID) { // Returns an unique list of all peer addresses within the routing network. func (t *RoutingTable) GetPeerAddresses() (peers []string) { visited := make(map[string]struct{}) - visited[t.self.Hex()] = struct{}{} + visited[t.self.PublicKeyHex()] = struct{}{} t.buckets.Range(func(key, value interface{}) bool { bucket := value.(*Bucket) @@ -111,9 +111,9 @@ func (t *RoutingTable) GetPeerAddresses() (peers []string) { for e := bucket.Front(); e != nil; e = e.Next() { id := e.Value.(peer.ID) - if _, seen := visited[id.Hex()]; !seen { + if _, seen := visited[id.PublicKeyHex()]; !seen { peers = append(peers, id.Address) - visited[id.Hex()] = struct{}{} + visited[id.PublicKeyHex()] = struct{}{} } } diff --git a/dht/routes_test.go b/dht/routes_test.go new file mode 100644 index 00000000..4b092391 --- /dev/null +++ b/dht/routes_test.go @@ -0,0 +1,3 @@ +package dht + +//TODO make function testable diff --git a/network/discovery/rpc.go b/network/discovery/rpc.go index 2d895c96..eed55f27 100644 --- a/network/discovery/rpc.go +++ b/network/discovery/rpc.go @@ -2,11 +2,12 @@ package discovery import ( "context" + "sync" + "github.com/perlin-network/noise/log" "github.com/perlin-network/noise/network" "github.com/perlin-network/noise/peer" "github.com/perlin-network/noise/protobuf" - "sync" ) func bootstrapPeers(network *network.Network, target peer.ID, count int) (addresses []string, publicKeys [][]byte) { @@ -14,7 +15,7 @@ func bootstrapPeers(network *network.Network, target peer.ID, count int) (addres visited := make(map[string]struct{}) visited[network.Keys.PublicKeyHex()] = struct{}{} - visited[target.Hex()] = struct{}{} + visited[target.PublicKeyHex()] = struct{}{} for len(queue) > 0 { var wait sync.WaitGroup @@ -69,9 +70,9 @@ func bootstrapPeers(network *network.Network, target peer.ID, count int) (addres for _, id := range response.Peers { p := peer.ID(*id) - if _, seen := visited[p.Hex()]; !seen { + if _, seen := visited[p.PublicKeyHex()]; !seen { queue = append(queue, p) - visited[p.Hex()] = struct{}{} + visited[p.PublicKeyHex()] = struct{}{} addresses = append(addresses, p.Address) diff --git a/peer/identity.go b/peer/identity.go index 3bb6e74b..21bf49f9 100644 --- a/peer/identity.go +++ b/peer/identity.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/hex" "fmt" + "github.com/perlin-network/noise/protobuf" "golang.org/x/crypto/ed25519" ) @@ -31,10 +32,12 @@ func (id ID) Less(other interface{}) bool { return false } -func (id ID) Hex() string { - return hex.EncodeToString(id.PublicKey[:]) +// PublicKeyHex generate EncodeToString return of Public Key +func (id ID) PublicKeyHex() string { + return hex.EncodeToString(id.PublicKey) } +// Xor (^) operation of public key func (id ID) Xor(other ID) ID { var result [IdSize]byte for i := 0; i < IdSize; i++ { @@ -54,7 +57,3 @@ func (id ID) PrefixLen() int { } return IdSize*8 - 1 } - -func (id ID) PublicKeyHex() string { - return hex.EncodeToString(id.PublicKey) -} diff --git a/peer/identity_test.go b/peer/identity_test.go new file mode 100644 index 00000000..795f581c --- /dev/null +++ b/peer/identity_test.go @@ -0,0 +1,61 @@ +package peer + +import ( + "bytes" + "fmt" + "testing" + + "github.com/perlin-network/noise/peer" +) + +func TestID(t *testing.T) { + + testPublicKey := []byte("12345678901234567890123456789012") + testPublicKey1 := []byte("12345678901234567890123456789011") + testPublicKey2 := []byte("12345678901234567890123456789013") + testAddr := "localhost:12345" + + id := peer.CreateID(testAddr, testPublicKey) + + if !bytes.Equal(id.PublicKey, testPublicKey) { + fmt.Printf("%s \n%s", id.PublicKey, testPublicKey) + panic("Wrong Public Key") + } + + if id.Address != testAddr { + panic("Wrong Address") + } + + if id.String() != "ID{PublicKey: [49 50 51 52 53 54 55 56 57 48 49 50 51 52 53 54 55 56 57 48 49 50 51 52 53 54 55 56 57 48 49 50], Address: localhost:12345}" { + fmt.Printf(id.String()) + panic("String() error") + } + + if !id.Equals(peer.CreateID(testAddr, testPublicKey)) { + panic("Equals() error") + } + + if id.Less(peer.CreateID(testAddr, testPublicKey1)) { + panic("Less() error 1") + } + + if !id.Less(peer.CreateID(testAddr, testPublicKey2)) { + panic("Less() error 2") + } + + if id.PublicKeyHex() != "3132333435363738393031323334353637383930313233343536373839303132" { + fmt.Print(id.PublicKeyHex()) + panic("PublicKeyHex() error or hex.EncodeToString() changed defination?") + } + + comparee := peer.CreateID( + testAddr, + []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, + ) + + if !comparee.Equals(id.Xor(peer.CreateID(testAddr, testPublicKey2))) { + fmt.Printf("%v\n%v", comparee, id.Xor(peer.CreateID(testAddr, testPublicKey2))) + panic("Xor() error") + } + +}