aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dexon-foundation/dexon-consensus/core/syncer/watch-cat.go
blob: f2e197ebe5c6405230686d739dba05c169e92e6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2019 The dexon-consensus Authors
// This file is part of the dexon-consensus-core library.
//
// The dexon-consensus-core library is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The dexon-consensus-core library is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the dexon-consensus-core library. If not, see
// <http://www.gnu.org/licenses/>.

package syncer

import (
    "context"
    "time"

    "github.com/dexon-foundation/dexon-consensus/common"
    "github.com/dexon-foundation/dexon-consensus/core"
    "github.com/dexon-foundation/dexon-consensus/core/types"
    "github.com/dexon-foundation/dexon-consensus/core/utils"
)

type configReader interface {
    Configuration(round uint64) *types.Config
}

// WatchCat is reponsible for signaling if syncer object should be terminated.
type WatchCat struct {
    recovery     core.Recovery
    timeout      time.Duration
    configReader configReader
    feed         chan types.Position
    lastPosition types.Position
    polling      time.Duration
    ctx          context.Context
    cancel       context.CancelFunc
    logger       common.Logger
}

// NewWatchCat creats a new WatchCat 🐱 object.
func NewWatchCat(
    recovery core.Recovery,
    configReader configReader,
    polling time.Duration,
    timeout time.Duration,
    logger common.Logger) *WatchCat {
    wc := &WatchCat{
        recovery:     recovery,
        timeout:      timeout,
        configReader: configReader,
        feed:         make(chan types.Position),
        polling:      polling,
        logger:       logger,
    }
    return wc
}

// Feed the WatchCat so it won't produce the termination signal.
func (wc *WatchCat) Feed(position types.Position) {
    wc.feed <- position
}

// Start the WatchCat.
func (wc *WatchCat) Start() {
    wc.Stop()
    wc.lastPosition = types.Position{}
    wc.ctx, wc.cancel = context.WithCancel(context.Background())
    go func() {
        var lastPos types.Position
    MonitorLoop:
        for {
            select {
            case <-wc.ctx.Done():
                return
            default:
            }
            select {
            case <-wc.ctx.Done():
                return
            case pos := <-wc.feed:
                if !pos.Newer(lastPos) {
                    wc.logger.Warn("Feed with older height",
                        "pos", pos, "lastPos", lastPos)
                    continue
                }
                lastPos = pos
            case <-time.After(wc.timeout):
                break MonitorLoop
            }
        }
        go func() {
            for {
                select {
                case <-wc.ctx.Done():
                    return
                case <-wc.feed:
                }
            }
        }()
        defer wc.cancel()
        proposed := false
        threshold := uint64(
            utils.GetConfigWithPanic(wc.configReader, lastPos.Round, wc.logger).
                NotarySetSize / 2)
        wc.logger.Info("Threshold for recovery", "votes", threshold)
    ResetLoop:
        for {
            if !proposed {
                wc.logger.Info("Calling Recovery.ProposeSkipBlock",
                    "height", lastPos.Height)
                if err := wc.recovery.ProposeSkipBlock(lastPos.Height); err != nil {
                    wc.logger.Warn("Failed to proposeSkipBlock", "height", lastPos.Height, "error", err)
                } else {
                    proposed = true
                }
            }
            votes, err := wc.recovery.Votes(lastPos.Height)
            if err != nil {
                wc.logger.Error("Failed to get recovery votes", "height", lastPos.Height, "error", err)
            } else if votes > threshold {
                wc.logger.Info("Threshold for recovery reached!")
                wc.lastPosition = lastPos
                break ResetLoop
            }
            select {
            case <-wc.ctx.Done():
                return
            case <-time.After(wc.polling):
            }
        }
    }()
}

// Stop the WatchCat.
func (wc *WatchCat) Stop() {
    if wc.cancel != nil {
        wc.cancel()
    }
}

// Meow return a closed channel if syncer should be terminated.
func (wc *WatchCat) Meow() <-chan struct{} {
    return wc.ctx.Done()
}

// LastPosition returns the last position for recovery.
func (wc *WatchCat) LastPosition() types.Position {
    return wc.lastPosition
}