github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/radar/mainchain/node_league_height.go (about) 1 package mainchain 2 3 import ( 4 "time" 5 6 "bytes" 7 "sort" 8 "sync" 9 10 "fmt" 11 12 "github.com/ahmetb/go-linq" 13 "github.com/sixexorg/magnetic-ring/common" 14 "github.com/sixexorg/magnetic-ring/node" 15 ) 16 17 var nodeLHC *nodeLHCache 18 var m sync.RWMutex 19 20 type nodeLHCache struct { 21 //nodeId leagueId height 22 nodeLH map[string]map[common.Address]uint64 23 star []string 24 lastTime time.Time 25 nodeRoot common.Hash 26 useable bool 27 28 m sync.RWMutex 29 } 30 31 func NewNodeLHCache() { 32 nodeLHC = &nodeLHCache{ 33 nodeLH: make(map[string]map[common.Address]uint64), 34 lastTime: time.Now(), 35 useable: false, 36 } 37 } 38 39 func (this *nodeLHCache) refreshStarNode(stars []string) { 40 this.m.RLock() 41 defer this.m.RUnlock() 42 this.star = stars 43 } 44 func GetNodeLHCacheInstance() *nodeLHCache { 45 return nodeLHC 46 } 47 48 func (this *nodeLHCache) GetNodeLeagueHeight(leagueId common.Address) map[string]uint64 { 49 this.m.RLock() 50 defer this.m.RUnlock() 51 nodeHMap := make(map[string]uint64, len(this.nodeLH)) 52 for k, v := range this.nodeLH { 53 nodeHMap[k] = v[leagueId] 54 } 55 return nodeHMap 56 } 57 func (this *nodeLHCache) checkNodeRoot(nodeIds []string) { 58 m.Lock() 59 defer m.Unlock() 60 sort.Strings(nodeIds) 61 buff := bytes.NewBuffer(nil) 62 for _, v := range nodeIds { 63 buff.WriteString(v) 64 } 65 hash := common.CalcHash(buff.Bytes()) 66 if bytes.Equal(this.nodeRoot.ToBytes(), hash.ToBytes()) { 67 return 68 } 69 this.nodeRoot = hash 70 this.useable = false 71 earth := node.CurEarth() 72 nodeIds = append(nodeIds, earth) 73 existNodes := make([]string, 0, len(nodeIds)) 74 for k, _ := range this.nodeLH { 75 needRemove := true 76 for _, id := range nodeIds { 77 if k == id { 78 existNodes = append(existNodes, k) 79 needRemove = false 80 break 81 } 82 } 83 if needRemove { 84 delete(this.nodeLH, k) 85 } 86 } 87 for _, v := range nodeIds { 88 if !linq.From(existNodes).Contains(v) { 89 this.nodeLH[v] = make(map[common.Address]uint64) 90 } 91 } 92 } 93 94 func (this *nodeLHCache) receiveNodeLH(nlh *common.NodeLH) { 95 m.Lock() 96 defer m.Unlock() 97 tmp := this.nodeLH[nlh.NodeId] 98 if tmp == nil { 99 return 100 } else if tmp != nil && tmp[nlh.LeagueId] >= nlh.Height { 101 return 102 } 103 this.nodeLH[nlh.NodeId][nlh.LeagueId] = nlh.Height 104 this.lastTime = time.Now() 105 fmt.Printf("🚫 🆙 receiveNodeLH end nodeId:%s leagueId:%s height:%d\n", nlh.NodeId, nlh.LeagueId.ToString(), nlh.Height) 106 } 107 108 //getNodeLHs is the league processing progress in the stars and earth 109 func (this *nodeLHCache) getNodeLHs() (earth map[common.Address]uint64, nodes []map[common.Address]uint64) { 110 m.RLock() 111 defer m.RUnlock() 112 m := make([]map[common.Address]uint64, 0, len(this.nodeLH)-1) 113 earthId := node.CurEarth() 114 earth = this.nodeLH[earthId] 115 for _, v := range this.star { 116 tmp := this.nodeLH[v] 117 m = append(m, tmp) 118 } 119 /*for k, v := range this.nodeLH { 120 tmp := v 121 if k == earthId { 122 earth = tmp 123 } else { 124 m = append(m, tmp) 125 } 126 }*/ 127 return earth, m 128 }