Skip to content

Commit

Permalink
Exclude last reject proposers
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang authored and gdmmx committed Jun 12, 2019
1 parent 5cf2675 commit 3b00f70
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
32 changes: 22 additions & 10 deletions consensus/moca/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ func (consensus *Consensus) Start() {

// startConsensus starts the voting routine
func (consensus *Consensus) startConsensus() {
var excludedProposers [][]byte
for {
consensus.maybeUpdateConsensusHeight()
if consensus.maybeUpdateConsensusHeight() {
excludedProposers = nil
}

consensusHeight := consensus.GetExpectedHeight()

Expand All @@ -82,7 +85,7 @@ func (consensus *Consensus) startConsensus() {
continue
}

elc, err := consensus.waitAndHandleProposal()
elc, lastProposals, err := consensus.waitAndHandleProposal(excludedProposers)
if err != nil {
log.Warningf("Handle proposal error: %v", err)
time.Sleep(50 * time.Millisecond)
Expand All @@ -92,20 +95,27 @@ func (consensus *Consensus) startConsensus() {
consensus.setExpectedHeight(consensusHeight + 1)

electedBlockHash, err := consensus.startElection(consensusHeight, elc)
if err != nil {
log.Errorf("Election error: %v", err)
consensus.setExpectedHeight(consensusHeight)
continue
}
if err != nil || electedBlockHash == common.EmptyUint256 {
if err != nil {
log.Errorf("Election error: %v", err)
}

if electedBlockHash == common.EmptyUint256 {
log.Warningf("Reject block at height %d", consensusHeight)

if len(lastProposals) > 0 {
for _, proposal := range lastProposals {
excludedProposers = append(excludedProposers, proposal.Header.Signer)
}
}

consensus.setExpectedHeight(consensusHeight)
continue
}

log.Infof("Accept block %s at height %d", electedBlockHash.ToHexString(), consensusHeight)

excludedProposers = nil

err = consensus.saveAcceptedBlock(electedBlockHash)
if err != nil {
log.Errorf("Error saving accepted block: %v", err)
Expand Down Expand Up @@ -245,13 +255,15 @@ func (consensus *Consensus) setAcceptedHeight(height uint32) {

// maybeUpdateConsensusHeight change expectedHeight to nextConsensusHeight if
// nextConsensusHeight is not zero.
func (consensus *Consensus) maybeUpdateConsensusHeight() {
func (consensus *Consensus) maybeUpdateConsensusHeight() bool {
consensus.nextConsensusHeightLock.Lock()
defer consensus.nextConsensusHeightLock.Unlock()
if consensus.nextConsensusHeight > 0 {
consensus.setExpectedHeight(consensus.nextConsensusHeight)
consensus.nextConsensusHeight = 0
return true
}
consensus.nextConsensusHeightLock.Unlock()
return false
}

func (consensus *Consensus) saveAcceptedBlock(electedBlockHash common.Uint256) error {
Expand Down
21 changes: 16 additions & 5 deletions consensus/moca/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (consensus *Consensus) getBlockProposal(blockHash common.Uint256) (*ledger.

// waitAndHandleProposal waits for first valid proposal, and continues to handle
// proposal for electionStartDelay duration.
func (consensus *Consensus) waitAndHandleProposal() (*election.Election, error) {
func (consensus *Consensus) waitAndHandleProposal(excludedProposers [][]byte) (*election.Election, map[common.Uint256]*ledger.Block, error) {
var timerStartOnce sync.Once
electionStartTimer := time.NewTimer(math.MaxInt64)
electionStartTimer.Stop()
Expand All @@ -55,7 +55,7 @@ func (consensus *Consensus) waitAndHandleProposal() (*election.Election, error)

elc, _, err := consensus.loadOrCreateElection(heightToKey(consensusHeight))
if err != nil {
return nil, err
return nil, nil, err
}

for {
Expand All @@ -73,7 +73,7 @@ func (consensus *Consensus) waitAndHandleProposal() (*election.Election, error)

select {
case <-timeoutTimer.C:
return nil, errors.New("Wait for proposal timeout")
return nil, nil, errors.New("Wait for proposal timeout")
default:
time.Sleep(50 * time.Millisecond)
}
Expand All @@ -82,6 +82,17 @@ func (consensus *Consensus) waitAndHandleProposal() (*election.Election, error)
for {
select {
case proposal := <-proposalChan:
found := false
for _, excludedProposer := range excludedProposers {
if bytes.Equal(proposal.Header.Signer, excludedProposer) {
found = true
break
}
}
if found {
continue
}

blockHash := proposal.Header.Hash()

if !ledger.CanVerifyHeight(consensusHeight) {
Expand Down Expand Up @@ -157,10 +168,10 @@ func (consensus *Consensus) waitAndHandleProposal() (*election.Election, error)
}

case <-electionStartTimer.C:
return elc, nil
return elc, proposals, nil

case <-timeoutTimer.C:
return nil, errors.New("Wait for proposal timeout")
return nil, nil, errors.New("Wait for proposal timeout")
}
}
}
Expand Down

0 comments on commit 3b00f70

Please sign in to comment.