From 8afc252455dd6444c1756698f34c8ba3aa98c539 Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 22 Aug 2017 23:18:43 +0800 Subject: [PATCH 1/4] container: detect no block to generate --- container/ethereum.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/container/ethereum.go b/container/ethereum.go index 172f4375..cbf609be 100644 --- a/container/ethereum.go +++ b/container/ethereum.go @@ -50,6 +50,7 @@ const ( ) var ( + ErrNoBlock = errors.New("no block to generate") ErrConsensusTimeout = errors.New("consensus timeout") ) @@ -386,6 +387,9 @@ func (eth *ethereum) ConsensusMonitor(errCh chan<- error, quit chan struct{}) { log.Printf("Connection lost: %v", err) errCh <- err return + case <-time.After(10 * time.Second): + errCh <- ErrNoBlock + return case head := <-subCh: // Ensure that mining is stable. if head.Number.Uint64() < 3 { From 80adc3b9cccafedf75f2af648693b9337257b8e4 Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 22 Aug 2017 23:20:09 +0800 Subject: [PATCH 2/4] container: remove redundant code --- container/ethereum.go | 1 - 1 file changed, 1 deletion(-) diff --git a/container/ethereum.go b/container/ethereum.go index cbf609be..ddce77e5 100644 --- a/container/ethereum.go +++ b/container/ethereum.go @@ -401,7 +401,6 @@ func (eth *ethereum) ConsensusMonitor(errCh chan<- error, quit chan struct{}) { errCh <- ErrConsensusTimeout return } - latestUpdate = time.Now() case <-quit: return } From 4a86b74314846393ca6bbdc69fbd8fe4585d6669 Mon Sep 17 00:00:00 2001 From: Edwin Date: Wed, 23 Aug 2017 11:20:42 +0800 Subject: [PATCH 3/4] fix pr comments --- container/ethereum.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container/ethereum.go b/container/ethereum.go index ddce77e5..00977b9d 100644 --- a/container/ethereum.go +++ b/container/ethereum.go @@ -50,7 +50,7 @@ const ( ) var ( - ErrNoBlock = errors.New("no block to generate") + ErrNoBlock = errors.New("no block generated") ErrConsensusTimeout = errors.New("consensus timeout") ) From 73bdbc9545e71b0210e6248353aff374e5ad1f94 Mon Sep 17 00:00:00 2001 From: Edwin Date: Wed, 23 Aug 2017 13:43:19 +0800 Subject: [PATCH 4/4] container: change the ConsensusMonitor policy --- container/ethereum.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/container/ethereum.go b/container/ethereum.go index 00977b9d..c12a6d66 100644 --- a/container/ethereum.go +++ b/container/ethereum.go @@ -380,27 +380,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 <-time.After(10 * time.Second): - errCh <- ErrNoBlock + 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 - } + timer.Reset(3 * time.Second) case <-quit: return }