diff --git a/p2p/host/hostv2/hostv2.go b/p2p/host/hostv2/hostv2.go index 83a3bc883..c721c08de 100644 --- a/p2p/host/hostv2/hostv2.go +++ b/p2p/host/hostv2/hostv2.go @@ -37,10 +37,8 @@ func (host *HostV2) Peerstore() peerstore.Peerstore { func New(self p2p.Peer) *HostV2 { sourceAddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%s", self.Port)) catchError(err) - priv := addrToPrivKey(fmt.Sprintf("/ip4/%s/tcp/%s", self.IP, self.Port)) p2pHost, err := libp2p.New(context.Background(), libp2p.ListenAddrs(sourceAddr), - libp2p.Identity(priv), libp2p.NoSecurity, // The security (signature generation and verification) is, for now, taken care by ourselves. // TODO(ricl): Other features to probe // libp2p.EnableRelay; libp2p.Routing; @@ -73,8 +71,7 @@ func (host *HostV2) SendMessage(p p2p.Peer, message []byte) error { addr := fmt.Sprintf("/ip4/%s/tcp/%s", p.IP, p.Port) targetAddr, err := multiaddr.NewMultiaddr(addr) - priv := addrToPrivKey(addr) - peerID, _ := peer.IDFromPrivateKey(priv) + peerID := peer.ID(addr) host.Peerstore().AddAddrs(peerID, []multiaddr.Multiaddr{targetAddr}, peerstore.PermanentAddrTTL) s, err := host.h.NewStream(context.Background(), peerID, ProtocolID) if err != nil { diff --git a/p2p/host/hostv2/util.go b/p2p/host/hostv2/util.go index 01c29a86d..a91f1559e 100644 --- a/p2p/host/hostv2/util.go +++ b/p2p/host/hostv2/util.go @@ -2,11 +2,8 @@ package hostv2 import ( "bufio" - "hash/fnv" - "math/rand" "github.com/harmony-one/harmony/log" - ic "github.com/libp2p/go-libp2p-crypto" ) func catchError(err error) { @@ -16,15 +13,6 @@ func catchError(err error) { } } -func addrToPrivKey(addr string) ic.PrivKey { - h := fnv.New32a() - _, err := h.Write([]byte(addr)) - catchError(err) - r := rand.New(rand.NewSource(int64(h.Sum32()))) // Hack: forcing the random see to be the hash of addr so that we can recover priv from ip + port. - priv, _, err := ic.GenerateKeyPairWithReader(ic.RSA, 512, r) - return priv -} - func writeData(w *bufio.Writer, data []byte) { w.Write(data) w.Flush() diff --git a/p2p/host/message_test.go b/p2p/host/message_test.go new file mode 100644 index 000000000..7711ad33f --- /dev/null +++ b/p2p/host/message_test.go @@ -0,0 +1,34 @@ +package host + +import ( + "reflect" + "testing" + "time" + + "github.com/harmony-one/harmony/p2p" + "github.com/harmony-one/harmony/p2p/host/hostv2" +) + +func TestSendMessage(test *testing.T) { + peer1 := p2p.Peer{IP: "127.0.0.1", Port: "9000"} + peer2 := p2p.Peer{IP: "127.0.0.1", Port: "9001"} + msg := []byte{0x00, 0x01, 0x02, 0x03, 0x04} + host1 := hostv2.New(peer1) + host2 := hostv2.New(peer2) + go host2.BindHandlerAndServe(handler) + SendMessage(host1, peer2, msg, nil) + time.Sleep(3 * time.Second) +} + +func handler(s p2p.Stream) { + defer s.Close() + content, err := p2p.ReadMessageContent(s) + if err != nil { + panic("Read p2p data failed") + } + golden := []byte{0x00, 0x01, 0x02, 0x03, 0x04} + + if !reflect.DeepEqual(content, golden) { + panic("received message not equal original message") + } +}