Browse Source

Send NewRoundStepMessage/CommitMessage on AddPeer()

pull/39/head
Jae Kwon 10 years ago
parent
commit
bf5738f9f8
1 changed files with 43 additions and 22 deletions
  1. +43
    -22
      consensus/reactor.go

+ 43
- 22
consensus/reactor.go View File

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


Loading…
Cancel
Save