Skip to content

Commit dd3241f

Browse files
committed
export internal status for monitor and debug
currently have exported the status of local node, peers, connections, transaction pool and ChainStore, which can be accessed from http://nodeip:HttpJsonPort/debug/vars Signed-off-by: laizy <laizhichao@onchain.com>
1 parent 3da9692 commit dd3241f

6 files changed

Lines changed: 133 additions & 3 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ Chain/*
1818
/core/*/*_test.go
1919
/core/*/*/*_test.go
2020
/config/*
21-
node
2221
nodectl
2322
cscope*

core/store/ChainStore/ChainStore.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,19 @@ func NewChainStore(file string) (*ChainStore, error) {
7373
return nil, err
7474
}
7575

76-
return &ChainStore{
76+
chain := &ChainStore{
7777
st: st,
7878
headerIndex: map[uint32]Uint256{},
7979
blockCache: map[Uint256]*Block{},
8080
headerCache: map[Uint256]*Header{},
8181
currentBlockHeight: 0,
8282
storedHeaderCount: 0,
8383
disposed: false,
84-
}, nil
84+
}
85+
86+
ExportStoreStatus(chain)
87+
88+
return chain, nil
8589
}
8690

8791
func (bd *ChainStore) InitLedgerStoreWithGenesisBlock(genesisBlock *Block, defaultBookKeeper []*crypto.PubKey) (uint32, error) {

core/store/ChainStore/debug.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ChainStore
2+
3+
import (
4+
// . "DNA/common"
5+
"expvar"
6+
"fmt"
7+
// "time"
8+
)
9+
10+
type storeStatus struct {
11+
CurrHeaderHeight uint32
12+
CurrBlockHeight uint32
13+
HeaderCache map[uint32]string
14+
BlockCache map[uint32]string
15+
HeaderIndex map[uint32]string
16+
}
17+
18+
func expvarStore(store *ChainStore) func() interface{} {
19+
20+
return func() interface{} {
21+
store.mu.RLock()
22+
defer store.mu.RUnlock()
23+
24+
ss := storeStatus{
25+
HeaderIndex: make(map[uint32]string, len(store.headerIndex)),
26+
CurrHeaderHeight: store.GetHeaderHeight(),
27+
CurrBlockHeight: store.GetHeight(),
28+
HeaderCache: make(map[uint32]string, len(store.headerCache)),
29+
BlockCache: make(map[uint32]string, len(store.blockCache)),
30+
}
31+
32+
for k, hash := range store.headerIndex {
33+
ss.HeaderIndex[k] = fmt.Sprintf("%x", hash)
34+
}
35+
36+
for k, header := range store.headerCache {
37+
ss.HeaderCache[header.Blockdata.Height] = fmt.Sprintf("%x", k)
38+
}
39+
for k, block := range store.blockCache {
40+
ss.BlockCache[block.Blockdata.Height] = fmt.Sprintf("%x", k)
41+
}
42+
43+
return ss
44+
}
45+
46+
}
47+
48+
func ExportStoreStatus(store *ChainStore) {
49+
50+
expvar.Publish("dna_store", expvar.Func(expvarStore(store)))
51+
}

net/net.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ func StartProtocol(pubKey *crypto.PubKey) protocol.Noder {
2525
net := node.InitNode(pubKey)
2626
net.ConnectSeeds()
2727

28+
node.ExportNodeStatus(net)
29+
2830
return net
2931
}

net/node/debug.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

net/node/transactionPool.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ func (this *TXNPool) init() {
2929
this.txnList = make(map[common.Uint256]*transaction.Transaction)
3030
}
3131

32+
func (this *TXNPool) Len() int {
33+
this.RLock()
34+
defer this.RUnlock()
35+
return len(this.txnList)
36+
}
37+
3238
//append transaction to txnpool when check ok.
3339
//1.check transaction. 2.check with ledger(db) 3.check with pool
3440
func (this *TXNPool) AppendTxnPool(txn *transaction.Transaction) bool {

0 commit comments

Comments
 (0)