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