• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revisionda52ab7b01cfcc8f55a1284befc478cd9fabf25d (tree)
Time2019-08-21 12:17:25
AuthorHAOYUatHZ <haoyu@prot...>
CommiterHAOYUatHZ

Log Message

use dbTX

Change Summary

Incremental Difference

--- a/toolbar/precog/monitor/stats.go
+++ b/toolbar/precog/monitor/stats.go
@@ -50,20 +50,25 @@ func (m *monitor) upSertNode(node *config.Node) error {
5050 }
5151
5252 func (m *monitor) savePeerInfos(peerInfos []*peers.PeerInfo) error {
53+ dbTx := m.db.Begin()
5354 for _, peerInfo := range peerInfos {
54- m.savePeerInfo(peerInfo)
55+ if err := m.savePeerInfo(dbTx, peerInfo); err != nil {
56+ dbTx.Rollback()
57+ return err
58+ }
5559 }
56- return nil
60+
61+ return dbTx.Commit().Error
5762 }
5863
59-func (m *monitor) savePeerInfo(peerInfo *peers.PeerInfo) error {
64+func (m *monitor) savePeerInfo(dbTx *gorm.DB, peerInfo *peers.PeerInfo) error {
6065 xPub := &chainkd.XPub{}
6166 if err := xPub.UnmarshalText([]byte(peerInfo.ID)); err != nil {
6267 return err
6368 }
6469
6570 ormNode := &orm.Node{}
66- if err := m.db.Model(&orm.Node{}).Where(&orm.Node{PublicKey: xPub.PublicKey().String()}).First(ormNode).Error; err != nil {
71+ if err := dbTx.Model(&orm.Node{}).Where(&orm.Node{PublicKey: xPub.PublicKey().String()}).First(ormNode).Error; err != nil {
6772 return err
6873 }
6974
@@ -81,7 +86,7 @@ func (m *monitor) savePeerInfo(peerInfo *peers.PeerInfo) error {
8186 // PingTimes uint64
8287 // PongTimes uint64
8388 }
84- if err := m.db.Model(&orm.NodeLiveness{}).Where("node_id = ? AND status != ?", ormNode.ID, common.NodeOfflineStatus).
89+ if err := dbTx.Model(&orm.NodeLiveness{}).Where("node_id = ? AND status != ?", ormNode.ID, common.NodeOfflineStatus).
8590 UpdateColumn(&orm.NodeLiveness{
8691 BestHeight: ormNodeLiveness.BestHeight,
8792 AvgLantencyMS: ormNodeLiveness.AvgLantencyMS,
@@ -89,7 +94,7 @@ func (m *monitor) savePeerInfo(peerInfo *peers.PeerInfo) error {
8994 return err
9095 }
9196
92- if err := m.db.Model(&orm.Node{}).Where(&orm.Node{PublicKey: xPub.PublicKey().String()}).
97+ if err := dbTx.Model(&orm.Node{}).Where(&orm.Node{PublicKey: xPub.PublicKey().String()}).
9398 UpdateColumn(&orm.Node{
9499 Alias: peerInfo.Moniker,
95100 Xpub: peerInfo.ID,
Show on old repository browser