• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: コミット

Golang implemented sidechain for Bytom


コミットメタ情報

リビジョン78058dccd6766b55cf325cf4b4eb2c3d8f77328e (tree)
日時2019-08-20 17:35:05
作者HAOYUatHZ <haoyu@prot...>
コミッターHAOYUatHZ

ログメッセージ

try

変更サマリ

差分

--- a/toolbar/precog/monitor/connection.go
+++ b/toolbar/precog/monitor/connection.go
@@ -11,7 +11,7 @@ import (
1111
1212 func (m *monitor) connectNodesRoutine() {
1313 // TODO: change name?
14- ticker := time.NewTicker(time.Duration(m.cfg.CheckFreqSeconds+15) * time.Second)
14+ ticker := time.NewTicker(time.Duration(m.cfg.CheckFreqSeconds) * time.Second)
1515 for ; true; <-ticker.C {
1616 if err := m.dialNodes(); err != nil {
1717 log.Error(err)
@@ -20,7 +20,7 @@ func (m *monitor) connectNodesRoutine() {
2020 }
2121
2222 func (m *monitor) dialNodes() error {
23- m.Lock()
23+ // m.Lock()
2424 log.Info("Start to reconnect to nodes...")
2525 var nodes []*orm.Node
2626 if err := m.db.Model(&orm.Node{}).Find(&nodes).Error; err != nil {
@@ -46,6 +46,7 @@ func (m *monitor) dialNodes() error {
4646 // connected peers will be skipped in switch.DialPeers()
4747 m.sw.DialPeers(addresses)
4848 log.Info("DialPeers done.")
49- m.Unlock()
49+ m.checkStatusCh <- struct{}{}
50+ // m.Unlock()
5051 return nil
5152 }
--- a/toolbar/precog/monitor/discover.go
+++ b/toolbar/precog/monitor/discover.go
@@ -33,7 +33,7 @@ func (m *monitor) collectDiscoveredNodes() {
3333 continue
3434 }
3535 log.Info("discover new node: ", node)
36- m.Lock()
36+ // m.Lock()
3737
3838 if err := m.upSertNode(&config.Node{
3939 PublicKey: node.ID.String(),
@@ -45,6 +45,6 @@ func (m *monitor) collectDiscoveredNodes() {
4545 log.Error(err)
4646 }
4747
48- m.Unlock()
48+ // m.Unlock()
4949 }
5050 }
--- a/toolbar/precog/monitor/monitor.go
+++ b/toolbar/precog/monitor/monitor.go
@@ -7,7 +7,7 @@ import (
77 "os/user"
88 "strings"
99 "sync"
10- "time"
10+ // "time"
1111
1212 "github.com/jinzhu/gorm"
1313 log "github.com/sirupsen/logrus"
@@ -30,21 +30,21 @@ import (
3030
3131 type monitor struct {
3232 *sync.RWMutex
33- cfg *config.Config
34- db *gorm.DB
35- nodeCfg *vaporCfg.Config
36- sw *p2p.Switch
37- discvCh chan *dht.Node
38- privKey chainkd.XPrv
39- chain *mock.Chain
40- txPool *mock.Mempool
41- connected uint32
33+ cfg *config.Config
34+ db *gorm.DB
35+ nodeCfg *vaporCfg.Config
36+ sw *p2p.Switch
37+ discvCh chan *dht.Node
38+ privKey chainkd.XPrv
39+ chain *mock.Chain
40+ txPool *mock.Mempool
41+ checkStatusCh chan struct{}
4242 }
4343
4444 // TODO: set myself as SPV?
4545 func NewMonitor(cfg *config.Config, db *gorm.DB) *monitor {
4646 //TODO: for test
47- cfg.CheckFreqSeconds = 15
47+ cfg.CheckFreqSeconds = 180
4848
4949 dbPath, err := makePath()
5050 if err != nil {
@@ -70,14 +70,15 @@ func NewMonitor(cfg *config.Config, db *gorm.DB) *monitor {
7070 }
7171
7272 return &monitor{
73- RWMutex: &sync.RWMutex{},
74- cfg: cfg,
75- db: db,
76- nodeCfg: nodeCfg,
77- discvCh: discvCh,
78- privKey: privKey.(chainkd.XPrv),
79- chain: chain,
80- txPool: txPool,
73+ RWMutex: &sync.RWMutex{},
74+ cfg: cfg,
75+ db: db,
76+ nodeCfg: nodeCfg,
77+ discvCh: discvCh,
78+ privKey: privKey.(chainkd.XPrv),
79+ chain: chain,
80+ txPool: txPool,
81+ checkStatusCh: make(chan struct{}, 1),
8182 }
8283 }
8384
@@ -175,9 +176,8 @@ func (m *monitor) checkStatusRoutine() {
175176 }
176177
177178 bestHeight := uint64(0)
178- ticker := time.NewTicker(time.Duration(m.cfg.CheckFreqSeconds) * time.Second)
179- for range ticker.C {
180- m.Lock()
179+ for range m.checkStatusCh {
180+ // m.Lock()
181181 log.Info("connected peers: ", m.sw.GetPeers().List())
182182
183183 for _, peer := range m.sw.GetPeers().List() {
@@ -220,6 +220,6 @@ func (m *monitor) checkStatusRoutine() {
220220 peers.RemovePeer(p.ID())
221221 }
222222 log.Info("Disonnect all peers.")
223- m.Unlock()
223+ // m.Unlock()
224224 }
225225 }
--- a/toolbar/precog/monitor/stats.go
+++ b/toolbar/precog/monitor/stats.go
@@ -56,13 +56,7 @@ func (m *monitor) savePeerInfo(peerInfo *peers.PeerInfo) error {
5656 }
5757
5858 ormNode := &orm.Node{}
59- if err := m.db.Model(&orm.Node{}).Where(&orm.Node{PublicKey: xPub.PublicKey().String()}).
60- UpdateColumn(&orm.Node{
61- Alias: peerInfo.Moniker,
62- Xpub: peerInfo.ID,
63- BestHeight: peerInfo.Height,
64- // LatestDailyUptimeMinutes uint64
65- }).First(ormNode).Error; err != nil {
59+ if err := m.db.Model(&orm.Node{}).Where(&orm.Node{PublicKey: xPub.PublicKey().String()}).First(ormNode).Error; err != nil {
6660 return err
6761 }
6862
@@ -72,6 +66,7 @@ func (m *monitor) savePeerInfo(peerInfo *peers.PeerInfo) error {
7266 log.Debugf("Parse ping time err: %v", err)
7367 }
7468
69+ // TODO: preload?
7570 ormNodeLiveness := &orm.NodeLiveness{
7671 NodeID: ormNode.ID,
7772 BestHeight: ormNode.BestHeight,
@@ -79,9 +74,23 @@ func (m *monitor) savePeerInfo(peerInfo *peers.PeerInfo) error {
7974 // PingTimes uint64
8075 // PongTimes uint64
8176 }
82- return m.db.Model(&orm.NodeLiveness{}).Where("node_id = ? AND status != ?", ormNode.ID, common.NodeOfflineStatus).
77+ if err := m.db.Model(&orm.NodeLiveness{}).Where("node_id = ? AND status != ?", ormNode.ID, common.NodeOfflineStatus).
8378 UpdateColumn(&orm.NodeLiveness{
8479 BestHeight: ormNodeLiveness.BestHeight,
8580 AvgLantencyMS: ormNodeLiveness.AvgLantencyMS,
86- }).FirstOrCreate(ormNodeLiveness).Error
81+ }).FirstOrCreate(ormNodeLiveness).Error; err != nil {
82+ return err
83+ }
84+
85+ if err := m.db.Model(&orm.Node{}).Where(&orm.Node{PublicKey: xPub.PublicKey().String()}).
86+ UpdateColumn(&orm.Node{
87+ Alias: peerInfo.Moniker,
88+ Xpub: peerInfo.ID,
89+ BestHeight: peerInfo.Height,
90+ // LatestDailyUptimeMinutes uint64
91+ }).First(ormNode).Error; err != nil {
92+ return err
93+ }
94+
95+ return nil
8796 }
旧リポジトリブラウザで表示