Skip to content

Commit

Permalink
Start consensus timer using proposal received time
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Dec 3, 2020
1 parent c37fa9d commit 6cb20fd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
7 changes: 3 additions & 4 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"sync"
"time"

"github.com/nknorg/nkn/v2/block"
"github.com/nknorg/nkn/v2/chain"
"github.com/nknorg/nkn/v2/common"
"github.com/nknorg/nkn/v2/config"
Expand Down Expand Up @@ -33,7 +32,7 @@ type Consensus struct {
elections common.Cache

proposalLock sync.RWMutex
proposalChan chan *block.Block
proposalChan chan *proposalInfo
expectedHeight uint32

nextConsensusHeightLock sync.Mutex
Expand All @@ -51,7 +50,7 @@ func NewConsensus(account *vault.Account, localNode *node.LocalNode) (*Consensus
localNode: localNode,
elections: common.NewGoCache(cacheExpiration, cacheCleanupInterval),
proposals: common.NewGoCache(cacheExpiration, cacheCleanupInterval),
proposalChan: make(chan *block.Block, proposalChanLen),
proposalChan: make(chan *proposalInfo, proposalChanLen),
requestProposalChan: make(chan *requestProposalInfo, requestProposalChanLen),
mining: chain.NewBuiltinMining(account, txnCollector),
txnCollector: txnCollector,
Expand Down Expand Up @@ -248,7 +247,7 @@ func (consensus *Consensus) setExpectedHeight(expectedHeight uint32) {
}

consensus.expectedHeight = expectedHeight
consensus.proposalChan = make(chan *block.Block, proposalChanLen)
consensus.proposalChan = make(chan *proposalInfo, proposalChanLen)
}
consensus.proposalLock.Unlock()
}
Expand Down
18 changes: 12 additions & 6 deletions consensus/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ type requestProposalInfo struct {
blockHash common.Uint256
}

type proposalInfo struct {
block *block.Block
receivedTime time.Time
}

// getBlockProposal gets a proposal from proposal cache and convert to block
func (consensus *Consensus) getBlockProposal(blockHash common.Uint256) (*block.Block, error) {
value, ok := consensus.proposals.Get(blockHash.ToArray())
Expand Down Expand Up @@ -98,8 +103,10 @@ func (consensus *Consensus) waitAndHandleProposal() (*election.Election, error)
proposalCount := 0
for {
select {
case proposal := <-proposalChan:
case proposalInfo := <-proposalChan:
proposalCount++
proposal := proposalInfo.block
receivedTime := proposalInfo.receivedTime
blockHash := proposal.Hash()

if !consensus.canVerifyHeight(consensusHeight) {
Expand All @@ -112,10 +119,9 @@ func (consensus *Consensus) waitAndHandleProposal() (*election.Election, error)

timerStartOnce.Do(func() {
timer.StopTimer(timeoutTimer)
electionStartTimer.Reset(electionStartDelay)
now := time.Now()
verifyDeadline = now.Add(proposalVerificationTimeout)
initialVoteDeadline = now.Add(initialVoteDelay)
electionStartTimer.Reset(electionStartDelay - time.Since(receivedTime))
verifyDeadline = receivedTime.Add(proposalVerificationTimeout)
initialVoteDeadline = receivedTime.Add(initialVoteDelay)
})

acceptProposal := true
Expand Down Expand Up @@ -254,7 +260,7 @@ func (consensus *Consensus) receiveProposal(block *block.Block) error {
}

select {
case consensus.proposalChan <- block:
case consensus.proposalChan <- &proposalInfo{block: block, receivedTime: time.Now()}:
default:
return errors.New("prososal chan full, discarding proposal")
}
Expand Down

0 comments on commit 6cb20fd

Please sign in to comment.