use gossip for all consensus messages

Signed-off-by: Leo Chen <leo@harmony.one>
pull/428/head
Leo Chen 6 years ago
parent f2a5e5c5c9
commit a55b4514dd
  1. 3
      cmd/harmony.go
  2. 6
      consensus/consensus.go
  3. 21
      consensus/consensus_leader.go
  4. 13
      consensus/consensus_validator.go
  5. 4
      internal/utils/singleton.go
  6. 4
      p2p/host/hostv2/hostv2.go

@ -199,6 +199,7 @@ func main() {
// Attack determination. // Attack determination.
attack.GetInstance().SetAttackEnabled(attackDetermination(*attackedMode)) attack.GetInstance().SetAttackEnabled(attackDetermination(*attackedMode))
} }
utils.UseLibP2P = false
} else { } else {
if *isLeader { if *isLeader {
role = "leader" role = "leader"
@ -206,6 +207,7 @@ func main() {
} else { } else {
role = "validator" role = "validator"
} }
utils.UseLibP2P = true
} }
// Init logging. // Init logging.
loggingInit(*logFolder, role, *ip, *port, *onlyLogTps) loggingInit(*logFolder, role, *ip, *port, *onlyLogTps)
@ -287,7 +289,6 @@ func main() {
if consensus.IsLeader { if consensus.IsLeader {
go currentNode.SendPongMessage() go currentNode.SendPongMessage()
} }
currentNode.UseLibP2P = true
} }
go currentNode.SupportSyncing() go currentNode.SupportSyncing()

@ -432,7 +432,11 @@ func (consensus *Consensus) RemovePeers(peers []p2p.Peer) int {
pong := proto_discovery.NewPongMessage(validators, consensus.PublicKeys) pong := proto_discovery.NewPongMessage(validators, consensus.PublicKeys)
buffer := pong.ConstructPongMessage() buffer := pong.ConstructPongMessage()
host.BroadcastMessageFromLeader(consensus.host, validators, buffer, consensus.OfflinePeers) if utils.UseLibP2P {
consensus.host.SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeacon}, buffer)
} else {
host.BroadcastMessageFromLeader(consensus.host, validators, buffer, consensus.OfflinePeers)
}
} }
return count2 return count2

@ -14,6 +14,7 @@ import (
bls_cosi "github.com/harmony-one/harmony/crypto/bls" bls_cosi "github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/profiler" "github.com/harmony-one/harmony/internal/profiler"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/p2p/host" "github.com/harmony-one/harmony/p2p/host"
) )
@ -107,7 +108,11 @@ func (consensus *Consensus) startConsensus(newBlock *types.Block) {
// Leader sign the block hash itself // Leader sign the block hash itself
consensus.prepareSigs[consensus.nodeID] = consensus.priKey.SignHash(consensus.blockHash[:]) consensus.prepareSigs[consensus.nodeID] = consensus.priKey.SignHash(consensus.blockHash[:])
host.BroadcastMessageFromLeader(consensus.host, consensus.GetValidatorPeers(), msgToSend, consensus.OfflinePeers) if utils.UseLibP2P {
consensus.host.SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeacon}, msgToSend)
} else {
host.BroadcastMessageFromLeader(consensus.host, consensus.GetValidatorPeers(), msgToSend, consensus.OfflinePeers)
}
} }
// processPrepareMessage processes the prepare message sent from validators // processPrepareMessage processes the prepare message sent from validators
@ -164,7 +169,12 @@ func (consensus *Consensus) processPrepareMessage(message consensus_proto.Messag
// Construct and broadcast prepared message // Construct and broadcast prepared message
msgToSend, aggSig := consensus.constructPreparedMessage() msgToSend, aggSig := consensus.constructPreparedMessage()
consensus.aggregatedPrepareSig = aggSig consensus.aggregatedPrepareSig = aggSig
host.BroadcastMessageFromLeader(consensus.host, consensus.GetValidatorPeers(), msgToSend, consensus.OfflinePeers)
if utils.UseLibP2P {
consensus.host.SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeacon}, msgToSend)
} else {
host.BroadcastMessageFromLeader(consensus.host, consensus.GetValidatorPeers(), msgToSend, consensus.OfflinePeers)
}
// Set state to targetState // Set state to targetState
consensus.state = targetState consensus.state = targetState
@ -230,7 +240,12 @@ func (consensus *Consensus) processCommitMessage(message consensus_proto.Message
// Construct and broadcast committed message // Construct and broadcast committed message
msgToSend, aggSig := consensus.constructCommittedMessage() msgToSend, aggSig := consensus.constructCommittedMessage()
consensus.aggregatedCommitSig = aggSig consensus.aggregatedCommitSig = aggSig
host.BroadcastMessageFromLeader(consensus.host, consensus.GetValidatorPeers(), msgToSend, consensus.OfflinePeers)
if utils.UseLibP2P {
consensus.host.SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeacon}, msgToSend)
} else {
host.BroadcastMessageFromLeader(consensus.host, consensus.GetValidatorPeers(), msgToSend, consensus.OfflinePeers)
}
var blockObj types.Block var blockObj types.Block
err := rlp.DecodeBytes(consensus.block, &blockObj) err := rlp.DecodeBytes(consensus.block, &blockObj)

@ -3,6 +3,7 @@ package consensus
import ( import (
"github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/bls/ffi/go/bls"
bls_cosi "github.com/harmony-one/harmony/crypto/bls" bls_cosi "github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/p2p"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
protobuf "github.com/golang/protobuf/proto" protobuf "github.com/golang/protobuf/proto"
@ -103,7 +104,11 @@ func (consensus *Consensus) processAnnounceMessage(message consensus_proto.Messa
// Construct and send prepare message // Construct and send prepare message
msgToSend := consensus.constructPrepareMessage() msgToSend := consensus.constructPrepareMessage()
consensus.SendMessage(consensus.leader, msgToSend) if utils.UseLibP2P {
consensus.host.SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeacon}, msgToSend)
} else {
consensus.SendMessage(consensus.leader, msgToSend)
}
consensus.state = PrepareDone consensus.state = PrepareDone
} }
@ -163,7 +168,11 @@ func (consensus *Consensus) processPreparedMessage(message consensus_proto.Messa
// Construct and send the commit message // Construct and send the commit message
multiSigAndBitmap := append(multiSig, bitmap...) multiSigAndBitmap := append(multiSig, bitmap...)
msgToSend := consensus.constructCommitMessage(multiSigAndBitmap) msgToSend := consensus.constructCommitMessage(multiSigAndBitmap)
consensus.SendMessage(consensus.leader, msgToSend) if utils.UseLibP2P {
consensus.host.SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeacon}, msgToSend)
} else {
consensus.SendMessage(consensus.leader, msgToSend)
}
consensus.state = CommitDone consensus.state = CommitDone
} }

@ -13,6 +13,10 @@ import (
var ( var (
Port string Port string
IP string IP string
// Global Variable to use libp2p for networking
// FIXME: this is a temporary hack, once we totally switch to libp2p
// this variable shouldn't be used
UseLibP2P bool
) )
// SetPortAndIP used to print out loggings of node with Port and IP. // SetPortAndIP used to print out loggings of node with Port and IP.

@ -162,8 +162,8 @@ func New(self *p2p.Peer, priKey p2p_crypto.PrivKey, opts ...p2p_config.Option) *
append(opts, libp2p.ListenAddrs(listenAddr), libp2p.Identity(priKey))..., append(opts, libp2p.ListenAddrs(listenAddr), libp2p.Identity(priKey))...,
) )
catchError(err) catchError(err)
// pubsub, err := pubsub.NewGossipSub(ctx, p2pHost) pubsub, err := pubsub.NewGossipSub(ctx, p2pHost)
pubsub, err := pubsub.NewFloodSub(ctx, p2pHost) // pubsub, err := pubsub.NewFloodSub(ctx, p2pHost)
catchError(err) catchError(err)
self.PeerID = p2pHost.ID() self.PeerID = p2pHost.ID()

Loading…
Cancel
Save