aboutsummaryrefslogtreecommitdiffstats
path: root/dex/blockproposer.go
blob: 122540ba54d984a46d1cef38658987d39b51baba (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package dex

import (
    "errors"
    "fmt"
    "os"
    "sync"
    "sync/atomic"
    "time"

    dexCore "github.com/tangerine-network/tangerine-consensus/core"
    coreEcdsa "github.com/tangerine-network/tangerine-consensus/core/crypto/ecdsa"
    "github.com/tangerine-network/tangerine-consensus/core/syncer"
    coreTypes "github.com/tangerine-network/tangerine-consensus/core/types"

    "github.com/tangerine-network/go-tangerine/core"
    "github.com/tangerine-network/go-tangerine/dex/db"
    "github.com/tangerine-network/go-tangerine/log"
    "github.com/tangerine-network/go-tangerine/node"
    "github.com/tangerine-network/go-tangerine/rlp"
)

var (
    forceSyncTimeout = 20 * time.Second
)

type blockProposer struct {
    mu        sync.Mutex
    running   int32
    syncing   int32
    proposing int32
    dex       *Tangerine
    watchCat  *syncer.WatchCat
    dMoment   time.Time

    wg     sync.WaitGroup
    stopCh chan struct{}
}

func NewBlockProposer(dex *Tangerine, watchCat *syncer.WatchCat, dMoment time.Time) *blockProposer {
    return &blockProposer{
        dex:      dex,
        watchCat: watchCat,
        dMoment:  dMoment,
    }
}

func (b *blockProposer) Start(svc node.Service) error {
    b.mu.Lock()
    defer b.mu.Unlock()

    if !atomic.CompareAndSwapInt32(&b.running, 0, 1) {
        return fmt.Errorf("block proposer is already running")
    }
    log.Info("Started block proposer")

    b.stopCh = make(chan struct{})
    b.wg.Add(1)
    go func() {
        defer b.wg.Done()
        defer atomic.StoreInt32(&b.running, 0)

        var err error
        var c *dexCore.Consensus
        if b.dMoment.After(time.Now()) {
            // Start receiving core messages.
            b.dex.protocolManager.SetReceiveCoreMessage(true)

            c = b.initConsensus()
        } else {
            c, err = b.syncConsensus()
        }

        if err != nil {
            log.Error("Block proposer stopped, before start running", "err", err)
            return
        }

        log.Info("Start running consensus core")
        go c.Run(b.stopCh)
        atomic.StoreInt32(&b.proposing, 1)

        <-b.stopCh
        log.Debug("Block proposer receive stop signal")

        log.Info("Block proposer successfully stopped")
        go func() {
            svc.Stop()
            os.Exit(1)
        }()
    }()
    return nil
}

func (b *blockProposer) Stop() {
    log.Info("Stopping block proposer")
    b.mu.Lock()
    defer b.mu.Unlock()

    if atomic.LoadInt32(&b.running) == 1 {
        b.dex.protocolManager.SetReceiveCoreMessage(false)
        close(b.stopCh)
        b.wg.Wait()
        atomic.StoreInt32(&b.proposing, 0)
    }
    log.Info("Block proposer stopped")
}

func (b *blockProposer) IsCoreSyncing() bool {
    return atomic.LoadInt32(&b.syncing) == 1
}

func (b *blockProposer) IsProposing() bool {
    return atomic.LoadInt32(&b.proposing) == 1
}

func (b *blockProposer) initConsensus() *dexCore.Consensus {
    db := db.NewDatabase(b.dex.chainDb)
    privkey := coreEcdsa.NewPrivateKeyFromECDSA(b.dex.config.PrivateKey)
    return dexCore.NewConsensus(b.dMoment,
        b.dex.app, b.dex.governance, db, b.dex.network, privkey, log.Root())
}

func (b *blockProposer) syncConsensus() (*dexCore.Consensus, error) {
    atomic.StoreInt32(&b.syncing, 1)
    defer atomic.StoreInt32(&b.syncing, 0)

    cb := b.dex.blockchain.CurrentBlock()

    db := db.NewDatabase(b.dex.chainDb)
    privkey := coreEcdsa.NewPrivateKeyFromECDSA(b.dex.config.PrivateKey)
    consensusSync := syncer.NewConsensus(cb.NumberU64(), b.dMoment, b.dex.app,
        b.dex.governance, db, b.dex.network, privkey, log.Root())

    blocksToSync := func(coreHeight, height uint64) []*coreTypes.Block {
        var blocks []*coreTypes.Block
        for len(blocks) < 2048 && coreHeight < height {
            var block coreTypes.Block
            b := b.dex.blockchain.GetBlockByNumber(coreHeight + 1)
            if err := rlp.DecodeBytes(b.Header().DexconMeta, &block); err != nil {
                panic(err)
            }
            blocks = append(blocks, &block)
            coreHeight = coreHeight + 1
        }
        return blocks
    }

    // Sync all blocks in compaction chain to core.
    _, coreHeight := db.GetCompactionChainTipInfo()

Loop:
    for {
        currentBlock := b.dex.blockchain.CurrentBlock()
        log.Info("Syncing compaction chain", "core height", coreHeight,
            "height", currentBlock.NumberU64())
        blocks := blocksToSync(coreHeight, currentBlock.NumberU64())

        if len(blocks) == 0 {
            log.Debug("No new block to sync", "current", currentBlock.NumberU64())
            break Loop
        }

        log.Debug("Filling compaction chain", "num", len(blocks),
            "first", blocks[0].Position.Height,
            "last", blocks[len(blocks)-1].Position.Height)
        if _, err := consensusSync.SyncBlocks(blocks, false); err != nil {
            log.Debug("SyncBlocks fail", "err", err)
            return nil, err
        }
        coreHeight = blocks[len(blocks)-1].Position.Height

        select {
        case <-b.stopCh:
            return nil, errors.New("early stop")
        default:
        }
    }

    // Start the watchCat.
    b.watchCat.Start()
    defer b.watchCat.Stop()
    log.Info("Started sync WatchCat")

    // Feed the current block we have in local blockchain.
    if cb.NumberU64() > 0 {
        var block coreTypes.Block
        if err := rlp.DecodeBytes(cb.Header().DexconMeta, &block); err != nil {
            panic(err)
        }
        b.watchCat.Feed(block.Position)
    }

    ch := make(chan core.ChainHeadEvent)
    sub := b.dex.blockchain.SubscribeChainHeadEvent(ch)
    defer sub.Unsubscribe()

    // Listen chain head event until synced.
    nextDMoment := time.Now().Unix()
ListenLoop:
    for {
        select {
        case ev := <-ch:
            for {
                blocks := blocksToSync(coreHeight, ev.Block.NumberU64())
                if len(blocks) == 0 {
                    break
                }
                b.watchCat.Feed(blocks[len(blocks)-1].Position)
                log.Debug("Filling compaction chain", "num", len(blocks),
                    "first", blocks[0].Position.Height,
                    "last", blocks[len(blocks)-1].Position.Height)
                synced, err := consensusSync.SyncBlocks(blocks, true)
                if err != nil {
                    log.Error("SyncBlocks fail", "err", err)
                    return nil, err
                }
                b.dex.protocolManager.SetReceiveCoreMessage(true)
                if synced {
                    log.Debug("Consensus core synced")
                    break ListenLoop
                }
                coreHeight = blocks[len(blocks)-1].Position.Height
            }
        case <-sub.Err():
            log.Debug("System stopped when syncing consensus core")
            return nil, errors.New("system stop")
        case <-b.stopCh:
            log.Debug("Early stop, before consensus core can run")
            return nil, errors.New("early stop")
        case <-time.After(forceSyncTimeout):
            log.Debug("no new chain head for a while")
            if p := b.dex.protocolManager.peers.BestPeer(); p != nil {
                log.Debug("try force sync with peer", "id", p.id)
                go b.dex.protocolManager.synchronise(p, true)
            } else {
                log.Debug("no peer to sync")
            }
        case <-b.watchCat.Meow():
            log.Info("WatchCat signaled to stop syncing")

            // Sleep until the next consensus start time slot.
            // The interval T_i need to meet the following requirement:
            //
            //   T_i > T_timeout + T_panic + T_restart
            //
            // Currently, T_timeout = 120, T_panic = 60, T_restart ~ 60
            //
            // We set T_i = 600 to be safe.

            interval := int64(600)
            nextDMoment = (time.Now().Unix()/interval + 1) * interval
            log.Info("Sleeping until next starting time", "time", nextDMoment)

            b.dex.protocolManager.SetReceiveCoreMessage(true)
            consensusSync.ForceSync(b.watchCat.LastPosition(), true)
            break ListenLoop
        }
    }

    con, err := consensusSync.GetSyncedConsensus()
    time.Sleep(time.Duration(nextDMoment-time.Now().Unix()) * time.Second)
    return con, err
}