• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: コミット

Golang implemented sidechain for Bytom


コミットメタ情報

リビジョンa24d9d6d08105f3db232c0a1c54e2a466ce779fd (tree)
日時2019-08-19 16:37:41
作者HAOYUatHZ <haoyu@prot...>
コミッターHAOYUatHZ

ログメッセージ

fk

変更サマリ

差分

--- a/toolbar/precog/monitor/connection.go
+++ b/toolbar/precog/monitor/connection.go
@@ -20,10 +20,11 @@ func (m *monitor) connectNodesRoutine() {
2020 }
2121
2222 func (m *monitor) dialNodes() error {
23- // m.Lock()
2423 log.Info("Start to reconnect to nodes...")
24+ dbTx := m.db.Begin()
2525 var nodes []*orm.Node
26- if err := m.db.Model(&orm.Node{}).Find(&nodes).Error; err != nil {
26+ if err := dbTx.Model(&orm.Node{}).Find(&nodes).Error; err != nil {
27+ dbTx.Rollback()
2728 return err
2829 }
2930
@@ -46,6 +47,6 @@ func (m *monitor) dialNodes() error {
4647 // connected peers will be skipped in switch.DialPeers()
4748 m.sw.DialPeers(addresses)
4849 log.Info("DialPeers done.")
49- // m.Unlock()
50+ dbTx.Commit()
5051 return nil
5152 }
--- a/toolbar/precog/monitor/discover.go
+++ b/toolbar/precog/monitor/discover.go
@@ -33,16 +33,18 @@ func (m *monitor) collectDiscoveredNodes() {
3333 continue
3434 }
3535 log.Info("discover new node: ", node)
36- // m.Lock()
37- if err := m.upSertNode(&config.Node{
36+
37+ dbTx := m.db.Begin()
38+ if err := m.upSertNode(dbTx, &config.Node{
3839 PublicKey: node.ID.String(),
3940 Host: node.IP.String(),
4041 Port: node.TCP,
4142 }); err != nil {
43+ dbTx.Rollback()
4244 log.Error(err)
45+ } else {
46+ nodeMap[node.ID.String()] = node
47+ dbTx.Commit()
4348 }
44-
45- nodeMap[node.ID.String()] = node
46- // m.Unlock()
4749 }
4850 }
--- a/toolbar/precog/monitor/monitor.go
+++ b/toolbar/precog/monitor/monitor.go
@@ -103,13 +103,16 @@ func makePath() (string, error) {
103103 func (m *monitor) Run() {
104104 defer os.RemoveAll(m.nodeCfg.DBPath)
105105
106+ dbTx := m.db.Begin()
106107 var seeds []string
107108 for _, node := range m.cfg.Nodes {
108109 seeds = append(seeds, fmt.Sprintf("%s:%d", node.Host, node.Port))
109- if err := m.upSertNode(&node); err != nil {
110+ if err := m.upSertNode(dbTx, &node); err != nil {
111+ dbTx.Rollback()
110112 log.Error(err)
111113 }
112114 }
115+ dbTx.Commit()
113116 m.nodeCfg.P2P.Seeds = strings.Join(seeds, ",")
114117 if err := m.makeSwitch(); err != nil {
115118 log.Fatal(err)
@@ -177,7 +180,7 @@ func (m *monitor) checkStatusRoutine() {
177180 bestHeight := uint64(0)
178181 ticker := time.NewTicker(time.Duration(m.cfg.CheckFreqSeconds) * time.Second)
179182 for range ticker.C {
180- // m.Lock()
183+ dbTx := m.db.Begin()
181184 log.Info("connected peers: ", m.sw.GetPeers().List())
182185
183186 for _, peer := range m.sw.GetPeers().List() {
@@ -200,8 +203,6 @@ func (m *monitor) checkStatusRoutine() {
200203 if peerInfo.Height > bestHeight {
201204 bestHeight = peerInfo.Height
202205 }
203-
204- m.savePeerInfo(peerInfo)
205206 }
206207 log.Info("bestHeight: ", bestHeight)
207208
@@ -220,6 +221,14 @@ func (m *monitor) checkStatusRoutine() {
220221 peers.RemovePeer(p.ID())
221222 }
222223 log.Info("Disonnect all peers.")
223- // m.Unlock()
224+
225+ for _, peerInfo := range peers.GetPeerInfos() {
226+ if err := m.savePeerInfo(dbTx, peerInfo); err != nil {
227+ dbTx.Rollback()
228+ log.Error(err)
229+ break
230+ }
231+ }
232+ dbTx.Commit()
224233 }
225234 }
--- a/toolbar/precog/monitor/stats.go
+++ b/toolbar/precog/monitor/stats.go
@@ -21,13 +21,13 @@ import (
2121 // TODO: update lantency, active_time and status
2222
2323 // create or update: https://github.com/jinzhu/gorm/issues/1307
24-func (m *monitor) upSertNode(node *config.Node) error {
24+func (m *monitor) upSertNode(dbTx *gorm.DB, node *config.Node) error {
2525 if node.XPub != nil {
2626 node.PublicKey = fmt.Sprintf("%v", node.XPub.PublicKey().String())
2727 }
2828
2929 ormNode := &orm.Node{PublicKey: node.PublicKey}
30- if err := m.db.Where(&orm.Node{PublicKey: node.PublicKey}).First(ormNode).Error; err != nil && err != gorm.ErrRecordNotFound {
30+ if err := dbTx.Where(&orm.Node{PublicKey: node.PublicKey}).First(ormNode).Error; err != nil && err != gorm.ErrRecordNotFound {
3131 return err
3232 }
3333
@@ -39,7 +39,7 @@ func (m *monitor) upSertNode(node *config.Node) error {
3939 }
4040 ormNode.Host = node.Host
4141 ormNode.Port = node.Port
42- return m.db.Where(&orm.Node{PublicKey: ormNode.PublicKey}).
42+ return dbTx.Where(&orm.Node{PublicKey: ormNode.PublicKey}).
4343 Assign(&orm.Node{
4444 Xpub: ormNode.Xpub,
4545 Alias: ormNode.Alias,
@@ -48,14 +48,14 @@ func (m *monitor) upSertNode(node *config.Node) error {
4848 }).FirstOrCreate(ormNode).Error
4949 }
5050
51-func (m *monitor) savePeerInfo(peerInfo *peers.PeerInfo) error {
51+func (m *monitor) savePeerInfo(dbTx *gorm.DB, peerInfo *peers.PeerInfo) error {
5252 xPub := &chainkd.XPub{}
5353 if err := xPub.UnmarshalText([]byte(peerInfo.ID)); err != nil {
5454 return err
5555 }
5656
5757 ormNode := &orm.Node{}
58- if err := m.db.Model(&orm.Node{}).Where(&orm.Node{PublicKey: xPub.PublicKey().String()}).
58+ if err := dbTx.Model(&orm.Node{}).Where(&orm.Node{PublicKey: xPub.PublicKey().String()}).
5959 UpdateColumn(&orm.Node{
6060 Alias: peerInfo.Moniker,
6161 Xpub: peerInfo.ID,
@@ -74,7 +74,7 @@ func (m *monitor) savePeerInfo(peerInfo *peers.PeerInfo) error {
7474 // PingTimes uint64
7575 // PongTimes uint64
7676 }
77- if err := m.db.Model(&orm.NodeLiveness{}).Where("node_id = ? AND status != ?", ormNode.ID, common.NodeOfflineStatus).
77+ if err := dbTx.Model(&orm.NodeLiveness{}).Where("node_id = ? AND status != ?", ormNode.ID, common.NodeOfflineStatus).
7878 UpdateColumn(&orm.NodeLiveness{
7979 BestHeight: ormNodeLiveness.BestHeight,
8080 AvgLantencyMS: ormNodeLiveness.AvgLantencyMS,
旧リポジトリブラウザで表示