|
| 1 | +package node |
| 2 | + |
| 3 | +import ( |
| 4 | + . "DNA/net/protocol" |
| 5 | + "expvar" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +type PeerStatus struct { |
| 11 | + State uint32 |
| 12 | + FlightHeights []uint32 |
| 13 | + LastContact string |
| 14 | + TryTimes uint32 |
| 15 | + Addr string // The address of the node |
| 16 | + Height uint64 // The node latest block height |
| 17 | +} |
| 18 | + |
| 19 | +type TxPoolStatus struct { |
| 20 | + TxCount int |
| 21 | +} |
| 22 | + |
| 23 | +type nodeStatus struct { |
| 24 | + Id uint64 |
| 25 | + TxnCnt uint64 // The transactions be transmit by this node |
| 26 | + RxTxnCnt uint64 // The transaction received by this node |
| 27 | + PublicKey string |
| 28 | + Peers []PeerStatus |
| 29 | + Connectings []string |
| 30 | + TxPool TxPoolStatus |
| 31 | +} |
| 32 | + |
| 33 | +func expvarNodeInfo(node *node) func() interface{} { |
| 34 | + |
| 35 | + return func() interface{} { |
| 36 | + pbkey, _ := node.publicKey.EncodePoint(true) |
| 37 | + |
| 38 | + ns := nodeStatus{ |
| 39 | + Id: node.id, |
| 40 | + TxnCnt: node.txnCnt, |
| 41 | + RxTxnCnt: node.rxTxnCnt, |
| 42 | + PublicKey: fmt.Sprintf("%x", pbkey), |
| 43 | + TxPool: TxPoolStatus{TxCount: node.TXNPool.Len()}, |
| 44 | + Connectings: node.ConnectingAddrs, |
| 45 | + } |
| 46 | + |
| 47 | + node.nbrNodes.RLock() |
| 48 | + for _, n := range node.nbrNodes.List { |
| 49 | + peer := PeerStatus{ |
| 50 | + State: n.state, |
| 51 | + Height: n.height, |
| 52 | + FlightHeights: n.flightHeights, |
| 53 | + LastContact: fmt.Sprintf("%gs", float64(time.Now().Sub(n.link.time))/float64(time.Second)), |
| 54 | + TryTimes: n.tryTimes, |
| 55 | + Addr: fmt.Sprintf("%s:%d", n.link.addr, n.link.port), |
| 56 | + } |
| 57 | + |
| 58 | + ns.Peers = append(ns.Peers, peer) |
| 59 | + } |
| 60 | + node.nbrNodes.RUnlock() |
| 61 | + |
| 62 | + return ns |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func ExportNodeStatus(nd Noder) { |
| 67 | + expvar.Publish("dna_node", expvar.Func(expvarNodeInfo(nd.(*node)))) |
| 68 | +} |
0 commit comments