-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_watcher.go
94 lines (84 loc) · 2.42 KB
/
version_watcher.go
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
package verwatcher
import (
"fmt"
"math/big"
"sync"
"time"
"github.com/ethereum/go-ethereum/core/types"
"github.com/corex-mn/go-corex/logger"
"github.com/corex-mn/go-corex/opera/genesis/driver"
"github.com/corex-mn/go-corex/opera/genesis/driver/driverpos"
"github.com/corex-mn/go-corex/utils/errlock"
"github.com/corex-mn/go-corex/version"
)
type VerWarcher struct {
cfg Config
store *Store
done chan struct{}
wg sync.WaitGroup
logger.Instance
}
func New(cfg Config, store *Store) *VerWarcher {
return &VerWarcher{
cfg: cfg,
store: store,
done: make(chan struct{}),
Instance: logger.MakeInstance(),
}
}
func (w *VerWarcher) OnNewLog(l *types.Log) {
if l.Address != driver.ContractAddress {
return
}
if l.Topics[0] == driverpos.Topics.UpdateNetworkVersion && len(l.Data) >= 32 {
netVersion := new(big.Int).SetBytes(l.Data[24:32]).Uint64()
w.store.SetNetworkVersion(netVersion)
if netVersion > version.AsU64() {
if w.cfg.ShutDownIfNotUpgraded {
errlock.Permanent(fmt.Errorf("The network's supported version of %s was activated at block %d.\n"+
"Node's current version is %s and a shutdown is required as ShutDownIfNotUpgraded flag was set.\n"+
"Please upgrade the node to continue.", version.U64ToString(netVersion), l.BlockNumber, version.AsString()))
panic("unreachable")
} else if w.store.GetMissedVersion() == 0 {
w.store.SetMissedVersion(netVersion)
}
}
w.log()
}
}
func (w *VerWarcher) log() {
if w.cfg.WarningIfNotUpgradedEvery == 0 {
return
}
if w.store.GetNetworkVersion() > version.AsU64() {
w.Log.Warn(fmt.Sprintf("Network upgrade %s was activated. Current node version is %s. "+
"Please upgrade your node and re-sync the chain data.", version.U64ToString(w.store.GetNetworkVersion()), version.AsString()))
} else if w.store.GetMissedVersion() > 0 {
w.Log.Warn(fmt.Sprintf("Node's state is dirty because node was upgraded after the network upgrade %s was activated. "+
"Please re-sync the chain data to continue.", version.U64ToString(w.store.GetMissedVersion())))
}
}
func (w *VerWarcher) Start() {
if w.cfg.WarningIfNotUpgradedEvery == 0 {
return
}
w.log()
w.wg.Add(1)
go func() {
defer w.wg.Done()
ticker := time.NewTicker(w.cfg.WarningIfNotUpgradedEvery)
defer ticker.Stop()
for {
select {
case <-ticker.C:
w.log()
case <-w.done:
return
}
}
}()
}
func (w *VerWarcher) Stop() {
close(w.done)
w.wg.Wait()
}