From d20251849e3cf15eb49bc89808e0178fef47236f Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Wed, 31 Dec 2014 16:46:49 -0800 Subject: [PATCH] Save blocks on catch-up too. --- consensus/reactor.go | 2 +- consensus/state.go | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index fc459b2d..bd555f8a 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -280,7 +280,7 @@ OUTER_LOOP: } // If the peer is on a previous height, help catch up. - if rs.Height > prs.Height { + if 0 < prs.Height && prs.Height < rs.Height { log.Debug("Data catchup", "height", rs.Height, "peerHeight", prs.Height) if index, ok := prs.ProposalBlockBitArray.Not().PickRandom(); ok { // Ensure that the peer's PartSetHeaeder is correct diff --git a/consensus/state.go b/consensus/state.go index ad0ba764..b7eb1058 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -237,9 +237,10 @@ type ConsensusState struct { mtx sync.Mutex RoundState - state *state.State // State until height-1. - stagedBlock *Block // Cache last staged block. - stagedState *state.State // Cache result of staged block. + state *state.State // State until height-1. + stagedBlock *Block // Cache last staged block. + stagedState *state.State // Cache result of staged block. + lastCommittedHeight uint // Last called saveCommitVoteBlock() on. } func NewConsensusState(state *state.State, blockStore *BlockStore, mempoolReactor *mempool.MempoolReactor) *ConsensusState { @@ -828,6 +829,8 @@ func (cs *ConsensusState) TryFinalizeCommit(height uint) bool { err := cs.stageBlock(cs.ProposalBlock, cs.ProposalBlockParts) if err == nil { log.Debug(Fmt("Finalizing commit of block: %v", cs.ProposalBlock)) + // We have the block, so save/stage/sign-commit-vote. + cs.saveCommitVoteBlock(cs.ProposalBlock, cs.ProposalBlockParts) // Increment height. cs.updateToState(cs.stagedState) // cs.Step is now RoundStepNewHeight or RoundStepNewRound @@ -1038,6 +1041,13 @@ func (cs *ConsensusState) signAddVote(type_ byte, hash []byte, header PartSetHea func (cs *ConsensusState) saveCommitVoteBlock(block *Block, blockParts *PartSet) { + // Only run once per height. + if cs.lastCommittedHeight >= block.Height { + return + } else { + cs.lastCommittedHeight = block.Height + } + // The proposal must be valid. if err := cs.stageBlock(block, blockParts); err != nil { // Prevent zombies.