Merge pull request #289 from harmony-ek/move_host_to_p2p_package

Move Host interface into the top-level p2p package
pull/293/head
Eugene Kim 6 years ago committed by GitHub
commit 4b21d33495
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      api/client/client.go
  2. 4
      consensus/consensus.go
  3. 2
      internal/beaconchain/libs/beaconchain.go
  4. 2
      internal/newnode/newnode.go
  5. 5
      node/node.go
  6. 2
      node/p2p.go
  7. 13
      p2p/host.go
  8. 16
      p2p/host/host.go
  9. 10
      p2p/host/message.go
  10. 3
      p2p/p2pimpl/p2pimpl.go

@ -4,7 +4,6 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/p2p" "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 // 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 log log.Logger // Log utility
// The p2p host used to send/receive p2p messages // The p2p host used to send/receive p2p messages
host host.Host host p2p.Host
} }
// NewClient creates a new Client // 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 := Client{}
client.Leaders = leaders client.Leaders = leaders
client.host = host client.host = host

@ -107,7 +107,7 @@ type Consensus struct {
uniqueIDInstance *utils.UniqueValidatorID uniqueIDInstance *utils.UniqueValidatorID
// The p2p host used to send/receive p2p messages // The p2p host used to send/receive p2p messages
host host.Host host p2p.Host
// Signal channel for lost validators // Signal channel for lost validators
OfflinePeers chan p2p.Peer OfflinePeers chan p2p.Peer
@ -128,7 +128,7 @@ type BlockConsensusStatus struct {
} }
// New creates a new Consensus object // 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 := Consensus{}
consensus.host = host consensus.host = host

@ -46,7 +46,7 @@ type BeaconChain struct {
log log.Logger log log.Logger
ShardLeaderMap map[int]*node.Info ShardLeaderMap map[int]*node.Info
PubKey kyber.Point PubKey kyber.Point
host host.Host host p2p.Host
state BCState state BCState
rpcServer *beaconchain.Server rpcServer *beaconchain.Server
Peer p2p.Peer Peer p2p.Peer

@ -35,7 +35,7 @@ type NewNode struct {
priK kyber.Scalar priK kyber.Scalar
log log.Logger log log.Logger
SetInfo chan bool SetInfo chan bool
host host.Host host p2p.Host
} }
// New candidatenode initialization // New candidatenode initialization

@ -35,7 +35,6 @@ import (
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/node/worker" "github.com/harmony-one/harmony/node/worker"
"github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/p2p/host"
) )
// State is a state of a node. // 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 peerRegistrationRecord map[uint32]*syncConfig // record registration time (unixtime) of peers begin in syncing
// The p2p host used to send/receive p2p messages // The p2p host used to send/receive p2p messages
host host.Host host p2p.Host
// Channel to stop sending ping message // Channel to stop sending ping message
StopPing chan struct{} StopPing chan struct{}
@ -218,7 +217,7 @@ func DeserializeNode(d []byte) *NetworkNode {
} }
// New creates a new node. // 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{} node := Node{}
if host != nil { if host != nil {

@ -16,6 +16,6 @@ func (node *Node) BroadcastMessage(peers []p2p.Peer, data []byte) {
} }
// GetHost returns the p2p host // GetHost returns the p2p host
func (node *Node) GetHost() host.Host { func (node *Node) GetHost() p2p.Host {
return node.host return node.host
} }

@ -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
}

@ -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
}

@ -13,14 +13,14 @@ import (
// SendMessage is to connect a socket given a port and send the given message. // 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. // 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 // Construct normal p2p message
content := ConstructP2pMessage(byte(0), message) content := ConstructP2pMessage(byte(0), message)
go send(host, p, content, lostPeer) go send(host, p, content, lostPeer)
} }
// BroadcastMessage sends the message to a list of peers // 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 { if len(peers) == 0 {
return 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. // 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. // TODO(minhdoan): Enable back for multicast.
peers = SelectMyPeers(peers, 1, MaxBroadCast) peers = SelectMyPeers(peers, 1, MaxBroadCast)
BroadcastMessage(h, peers, msg, lostPeer) 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. // 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) peers = SelectMyPeers(peers, selfPeer.ValidatorID*MaxBroadCast+1, (selfPeer.ValidatorID+1)*MaxBroadCast)
BroadcastMessage(h, peers, msg, nil) 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. // 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. // Add attack code here.
//attack.GetInstance().Run() //attack.GetInstance().Run()
backoff := p2p.NewExpBackoff(150*time.Millisecond, 5*time.Second, 2) backoff := p2p.NewExpBackoff(150*time.Millisecond, 5*time.Second, 2)

@ -5,7 +5,6 @@ import (
"net" "net"
"github.com/harmony-one/harmony/p2p" "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/hostv1"
"github.com/harmony-one/harmony/p2p/host/hostv2" "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 // 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 // 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. // 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 { if Version == 1 {
h := hostv1.New(self) h := hostv1.New(self)
return h, nil return h, nil

Loading…
Cancel
Save