diff --git a/api/client/client.go b/api/client/client.go index 74b75fa8b..4ea7e8cf8 100644 --- a/api/client/client.go +++ b/api/client/client.go @@ -4,7 +4,6 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/p2p" - "github.com/harmony-one/harmony/p2p/host" ) // Client represents a node (e.g. a wallet) which sends transactions and receives responses from the harmony network @@ -15,11 +14,11 @@ type Client struct { log log.Logger // Log utility // The p2p host used to send/receive p2p messages - host host.Host + host p2p.Host } // NewClient creates a new Client -func NewClient(host host.Host, leaders *map[uint32]p2p.Peer) *Client { +func NewClient(host p2p.Host, leaders *map[uint32]p2p.Peer) *Client { client := Client{} client.Leaders = leaders client.host = host diff --git a/consensus/consensus.go b/consensus/consensus.go index 9f50b5f2b..d9e6a7256 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -107,7 +107,7 @@ type Consensus struct { uniqueIDInstance *utils.UniqueValidatorID // The p2p host used to send/receive p2p messages - host host.Host + host p2p.Host // Signal channel for lost validators OfflinePeers chan p2p.Peer @@ -128,7 +128,7 @@ type BlockConsensusStatus struct { } // New creates a new Consensus object -func New(host host.Host, ShardID string, peers []p2p.Peer, leader p2p.Peer) *Consensus { +func New(host p2p.Host, ShardID string, peers []p2p.Peer, leader p2p.Peer) *Consensus { consensus := Consensus{} consensus.host = host diff --git a/internal/beaconchain/libs/beaconchain.go b/internal/beaconchain/libs/beaconchain.go index 40ce153d9..8b4d2bc09 100644 --- a/internal/beaconchain/libs/beaconchain.go +++ b/internal/beaconchain/libs/beaconchain.go @@ -46,7 +46,7 @@ type BeaconChain struct { log log.Logger ShardLeaderMap map[int]*node.Info PubKey kyber.Point - host host.Host + host p2p.Host state BCState rpcServer *beaconchain.Server Peer p2p.Peer diff --git a/internal/newnode/newnode.go b/internal/newnode/newnode.go index 09bc09b16..b3a0da198 100644 --- a/internal/newnode/newnode.go +++ b/internal/newnode/newnode.go @@ -35,7 +35,7 @@ type NewNode struct { priK kyber.Scalar log log.Logger SetInfo chan bool - host host.Host + host p2p.Host } // New candidatenode initialization diff --git a/node/node.go b/node/node.go index 53bdbd62c..05dc42b9e 100644 --- a/node/node.go +++ b/node/node.go @@ -35,7 +35,6 @@ import ( "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/node/worker" "github.com/harmony-one/harmony/p2p" - "github.com/harmony-one/harmony/p2p/host" ) // State is a state of a node. @@ -133,7 +132,7 @@ type Node struct { peerRegistrationRecord map[uint32]*syncConfig // record registration time (unixtime) of peers begin in syncing // The p2p host used to send/receive p2p messages - host host.Host + host p2p.Host // Channel to stop sending ping message StopPing chan struct{} @@ -218,7 +217,7 @@ func DeserializeNode(d []byte) *NetworkNode { } // New creates a new node. -func New(host host.Host, consensus *bft.Consensus, db ethdb.Database) *Node { +func New(host p2p.Host, consensus *bft.Consensus, db ethdb.Database) *Node { node := Node{} if host != nil { diff --git a/node/p2p.go b/node/p2p.go index 5cdc1c080..1f38f0227 100644 --- a/node/p2p.go +++ b/node/p2p.go @@ -16,6 +16,6 @@ func (node *Node) BroadcastMessage(peers []p2p.Peer, data []byte) { } // GetHost returns the p2p host -func (node *Node) GetHost() host.Host { +func (node *Node) GetHost() p2p.Host { return node.host } diff --git a/p2p/host.go b/p2p/host.go new file mode 100644 index 000000000..2a61d9c99 --- /dev/null +++ b/p2p/host.go @@ -0,0 +1,13 @@ +package p2p + +import "github.com/libp2p/go-libp2p-peer" + +// Host is the client + server in p2p network. +type Host interface { + GetSelfPeer() Peer + SendMessage(Peer, []byte) error + BindHandlerAndServe(handler StreamHandler) + Close() error + AddPeer(*Peer) error + GetID() peer.ID +} diff --git a/p2p/host/host.go b/p2p/host/host.go deleted file mode 100644 index 483692710..000000000 --- a/p2p/host/host.go +++ /dev/null @@ -1,16 +0,0 @@ -package host - -import ( - "github.com/harmony-one/harmony/p2p" - peer "github.com/libp2p/go-libp2p-peer" -) - -// Host is the client + server in p2p network. -type Host interface { - GetSelfPeer() p2p.Peer - SendMessage(p2p.Peer, []byte) error - BindHandlerAndServe(handler p2p.StreamHandler) - Close() error - AddPeer(*p2p.Peer) error - GetID() peer.ID -} diff --git a/p2p/host/message.go b/p2p/host/message.go index 3736c9a61..26b9a3fce 100644 --- a/p2p/host/message.go +++ b/p2p/host/message.go @@ -13,14 +13,14 @@ import ( // SendMessage is to connect a socket given a port and send the given message. // TODO(minhdoan, rj): need to check if a peer is reachable or not. -func SendMessage(host Host, p p2p.Peer, message []byte, lostPeer chan p2p.Peer) { +func SendMessage(host p2p.Host, p p2p.Peer, message []byte, lostPeer chan p2p.Peer) { // Construct normal p2p message content := ConstructP2pMessage(byte(0), message) go send(host, p, content, lostPeer) } // BroadcastMessage sends the message to a list of peers -func BroadcastMessage(h Host, peers []p2p.Peer, msg []byte, lostPeer chan p2p.Peer) { +func BroadcastMessage(h p2p.Host, peers []p2p.Peer, msg []byte, lostPeer chan p2p.Peer) { if len(peers) == 0 { return } @@ -44,7 +44,7 @@ func BroadcastMessage(h Host, peers []p2p.Peer, msg []byte, lostPeer chan p2p.Pe } // BroadcastMessageFromLeader sends the message to a list of peers from a leader. -func BroadcastMessageFromLeader(h Host, peers []p2p.Peer, msg []byte, lostPeer chan p2p.Peer) { +func BroadcastMessageFromLeader(h p2p.Host, peers []p2p.Peer, msg []byte, lostPeer chan p2p.Peer) { // TODO(minhdoan): Enable back for multicast. peers = SelectMyPeers(peers, 1, MaxBroadCast) BroadcastMessage(h, peers, msg, lostPeer) @@ -66,7 +66,7 @@ func ConstructP2pMessage(msgType byte, content []byte) []byte { } // BroadcastMessageFromValidator sends the message to a list of peers from a validator. -func BroadcastMessageFromValidator(h Host, selfPeer p2p.Peer, peers []p2p.Peer, msg []byte) { +func BroadcastMessageFromValidator(h p2p.Host, selfPeer p2p.Peer, peers []p2p.Peer, msg []byte) { peers = SelectMyPeers(peers, selfPeer.ValidatorID*MaxBroadCast+1, (selfPeer.ValidatorID+1)*MaxBroadCast) BroadcastMessage(h, peers, msg, nil) } @@ -87,7 +87,7 @@ func SelectMyPeers(peers []p2p.Peer, min int, max int) []p2p.Peer { } // Send a message to another node with given port. -func send(h Host, peer p2p.Peer, message []byte, lostPeer chan p2p.Peer) { +func send(h p2p.Host, peer p2p.Peer, message []byte, lostPeer chan p2p.Peer) { // Add attack code here. //attack.GetInstance().Run() backoff := p2p.NewExpBackoff(150*time.Millisecond, 5*time.Second, 2) diff --git a/p2p/p2pimpl/p2pimpl.go b/p2p/p2pimpl/p2pimpl.go index 484d39820..5c589c86a 100644 --- a/p2p/p2pimpl/p2pimpl.go +++ b/p2p/p2pimpl/p2pimpl.go @@ -5,7 +5,6 @@ import ( "net" "github.com/harmony-one/harmony/p2p" - "github.com/harmony-one/harmony/p2p/host" "github.com/harmony-one/harmony/p2p/host/hostv1" "github.com/harmony-one/harmony/p2p/host/hostv2" @@ -23,7 +22,7 @@ const Version = 2 // for hostv2, it generates multiaddress, keypair and add PeerID to peer, add priKey to host // TODO (leo) the PriKey of the host has to be persistent in disk, so that we don't need to regenerate it // on the same host if the node software restarted. The peerstore has to be persistent as well. -func NewHost(self *p2p.Peer) (host.Host, error) { +func NewHost(self *p2p.Peer) (p2p.Host, error) { if Version == 1 { h := hostv1.New(self) return h, nil