Send NewRoundStepMessage/CommitMessage on AddPeer()

This commit is contained in:
Jae Kwon 2015-01-13 17:52:27 -08:00
parent 3034e209d9
commit bf5738f9f8

View File

@ -98,6 +98,9 @@ func (conR *ConsensusReactor) AddPeer(peer *p2p.Peer) {
// Begin gossip routines for this peer. // Begin gossip routines for this peer.
go conR.gossipDataRoutine(peer, peerState) go conR.gossipDataRoutine(peer, peerState)
go conR.gossipVotesRoutine(peer, peerState) go conR.gossipVotesRoutine(peer, peerState)
// Send our state to peer.
conR.sendNewRoundStepRoutine(peer)
} }
// Implements Reactor // Implements Reactor
@ -207,6 +210,30 @@ func (conR *ConsensusReactor) SetPrivValidator(priv *state.PrivValidator) {
//-------------------------------------- //--------------------------------------
func makeRoundStepMessages(rs *RoundState) (nrsMsg *NewRoundStepMessage, csMsg *CommitStepMessage) {
// Get seconds since beginning of height.
timeElapsed := time.Now().Sub(rs.StartTime)
// Broadcast NewRoundStepMessage
nrsMsg = &NewRoundStepMessage{
Height: rs.Height,
Round: rs.Round,
Step: rs.Step,
SecondsSinceStartTime: uint(timeElapsed.Seconds()),
}
// If the step is commit, then also broadcast a CommitStepMessage.
if rs.Step == RoundStepCommit {
csMsg = &CommitStepMessage{
Height: rs.Height,
BlockParts: rs.ProposalBlockParts.Header(),
BlockBitArray: rs.ProposalBlockParts.BitArray(),
}
}
return
}
// Listens for changes to the ConsensusState.Step by pulling // Listens for changes to the ConsensusState.Step by pulling
// on conR.conS.NewStepCh(). // on conR.conS.NewStepCh().
func (conR *ConsensusReactor) broadcastNewRoundStepRoutine() { func (conR *ConsensusReactor) broadcastNewRoundStepRoutine() {
@ -219,33 +246,27 @@ func (conR *ConsensusReactor) broadcastNewRoundStepRoutine() {
return return
} }
// Get seconds since beginning of height. nrsMsg, csMsg := makeRoundStepMessages(rs)
// Due to the condition documented, this is safe. if nrsMsg != nil {
timeElapsed := time.Now().Sub(rs.StartTime) conR.sw.Broadcast(StateCh, nrsMsg)
// Broadcast NewRoundStepMessage
{
msg := &NewRoundStepMessage{
Height: rs.Height,
Round: rs.Round,
Step: rs.Step,
SecondsSinceStartTime: uint(timeElapsed.Seconds()),
}
conR.sw.Broadcast(StateCh, msg)
} }
if csMsg != nil {
// If the step is commit, then also broadcast a CommitStepMessage. conR.sw.Broadcast(StateCh, csMsg)
if rs.Step == RoundStepCommit {
msg := &CommitStepMessage{
Height: rs.Height,
BlockParts: rs.ProposalBlockParts.Header(),
BlockBitArray: rs.ProposalBlockParts.BitArray(),
}
conR.sw.Broadcast(StateCh, msg)
} }
} }
} }
func (conR *ConsensusReactor) sendNewRoundStepRoutine(peer *p2p.Peer) {
rs := conR.conS.GetRoundState()
nrsMsg, csMsg := makeRoundStepMessages(rs)
if nrsMsg != nil {
peer.Send(StateCh, nrsMsg)
}
if csMsg != nil {
peer.Send(StateCh, nrsMsg)
}
}
func (conR *ConsensusReactor) gossipDataRoutine(peer *p2p.Peer, ps *PeerState) { func (conR *ConsensusReactor) gossipDataRoutine(peer *p2p.Peer, ps *PeerState) {
OUTER_LOOP: OUTER_LOOP: