aboutsummaryrefslogtreecommitdiffstats
path: root/core/utils/utils.go
diff options
context:
space:
mode:
authorMission Liao <mission.liao@dexon.org>2019-01-11 12:58:30 +0800
committerGitHub <noreply@github.com>2019-01-11 12:58:30 +0800
commit809e8def862fdfa792061a448f952747f1af4d3c (patch)
treebd038971e65a09bc9bb399f03a37b194ce67ae3c /core/utils/utils.go
parentfa25817354d5b7d40f5911004232392acfe7fe53 (diff)
downloadtangerine-consensus-809e8def862fdfa792061a448f952747f1af4d3c.tar
tangerine-consensus-809e8def862fdfa792061a448f952747f1af4d3c.tar.gz
tangerine-consensus-809e8def862fdfa792061a448f952747f1af4d3c.tar.bz2
tangerine-consensus-809e8def862fdfa792061a448f952747f1af4d3c.tar.lz
tangerine-consensus-809e8def862fdfa792061a448f952747f1af4d3c.tar.xz
tangerine-consensus-809e8def862fdfa792061a448f952747f1af4d3c.tar.zst
tangerine-consensus-809e8def862fdfa792061a448f952747f1af4d3c.zip
syncer: fix issues when switching to core.Consensus (#418)
- when confirmed blocks passed to core.Consensus aren't continuous in position in some chain, the pulling would skip those missing blocks. - fix: when some block is missing, avoid adding it and all blocks after it to core.Consensus. - we need to avoid the receive channel of network module full. - fix: during switching to core.Consensus, we need to launch a dummy receiver to receive from receive channel of network module. - fix: between the period during core.Consensus created and before running, a dummy receiver is also required to receive from receive channel of network module.
Diffstat (limited to 'core/utils/utils.go')
-rw-r--r--core/utils/utils.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/core/utils/utils.go b/core/utils/utils.go
index 8c9f77a..8486d28 100644
--- a/core/utils/utils.go
+++ b/core/utils/utils.go
@@ -18,6 +18,7 @@
package utils
import (
+ "context"
"fmt"
"github.com/dexon-foundation/dexon-consensus/common"
@@ -92,3 +93,36 @@ func VerifyDKGComplaint(
}
return ok, nil
}
+
+// LaunchDummyReceiver launches a go routine to receive from the receive
+// channel of a network module. An context is required to stop the go routine
+// automatically. An optinal message handler could be provided.
+func LaunchDummyReceiver(
+ ctx context.Context, recv <-chan interface{}, handler func(interface{})) (
+ context.CancelFunc, <-chan struct{}) {
+ var (
+ dummyCtx, dummyCancel = context.WithCancel(ctx)
+ finishedChan = make(chan struct{}, 1)
+ )
+ go func() {
+ defer func() {
+ finishedChan <- struct{}{}
+ }()
+ loop:
+ for {
+ select {
+ case <-dummyCtx.Done():
+ break loop
+ case v, ok := <-recv:
+ if !ok {
+ panic(fmt.Errorf(
+ "receive channel is closed before dummy receiver"))
+ }
+ if handler != nil {
+ handler(v)
+ }
+ }
+ }
+ }()
+ return dummyCancel, finishedChan
+}