Merge pull request #35 from getamis/feature/detect-no-block-to-generate

container: detect no block to generate
This commit is contained in:
bailantaotao 2017-08-23 14:51:59 +08:00 committed by GitHub
commit 47563a08da
1 changed files with 12 additions and 6 deletions

View File

@ -52,6 +52,7 @@ const (
)
var (
ErrNoBlock = errors.New("no block generated")
ErrConsensusTimeout = errors.New("consensus timeout")
)
@ -386,25 +387,30 @@ func (eth *ethereum) ConsensusMonitor(errCh chan<- error, quit chan struct{}) {
}
defer sub.Unsubscribe()
timer := time.NewTimer(10 * time.Second)
blockNumber := uint64(0)
for {
latestUpdate := time.Now()
select {
case err := <-sub.Err():
log.Printf("Connection lost: %v", err)
errCh <- err
return
case <-timer.C:
if blockNumber == 0 {
errCh <- ErrNoBlock
} else {
errCh <- ErrConsensusTimeout
}
return
case head := <-subCh:
blockNumber = head.Number.Uint64()
// Ensure that mining is stable.
if head.Number.Uint64() < 3 {
continue
}
// Block is generated by 2 seconds. We tolerate 1 second delay in consensus.
if time.Now().Sub(latestUpdate).Seconds() > 3.0 {
errCh <- ErrConsensusTimeout
return
}
latestUpdate = time.Now()
timer.Reset(3 * time.Second)
case <-quit:
return
}