-
Notifications
You must be signed in to change notification settings - Fork 14
/
net.go
161 lines (134 loc) · 3.52 KB
/
net.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package api
import (
"fmt"
p2pmetrics "github.com/libp2p/go-libp2p-core/metrics"
"go.uber.org/zap"
"github.com/qlcchain/go-qlc/common"
"github.com/qlcchain/go-qlc/common/event"
"github.com/qlcchain/go-qlc/common/types"
"github.com/qlcchain/go-qlc/ledger"
"github.com/qlcchain/go-qlc/log"
)
type NetApi struct {
ledger *ledger.Ledger
eb event.EventBus
logger *zap.SugaredLogger
}
type OnlineRepTotal struct {
Reps []*OnlineRepInfo
ValidVotes types.Balance
ValidVotesPercent string
}
type OnlineRepInfo struct {
Account types.Address
Vote types.Balance
}
func NewNetApi(l *ledger.Ledger, eb event.EventBus) *NetApi {
return &NetApi{ledger: l, eb: eb, logger: log.NewLogger("api_net")}
}
func (q *NetApi) OnlineRepresentatives() []types.Address {
as, _ := q.ledger.GetOnlineRepresentations()
if as == nil {
return make([]types.Address, 0)
}
return as
}
func (q *NetApi) OnlineRepsInfo() *OnlineRepTotal {
as, _ := q.ledger.GetOnlineRepresentations()
if as == nil {
return &OnlineRepTotal{}
}
ot := &OnlineRepTotal{
Reps: make([]*OnlineRepInfo, 0),
ValidVotes: types.ZeroBalance,
}
supply := common.GenesisBlock().Balance
minWeight, _ := supply.Div(common.DposVoteDivisor)
for _, account := range as {
weight := q.ledger.Weight(account)
oi := &OnlineRepInfo{
Account: account,
Vote: weight,
}
ot.Reps = append(ot.Reps, oi)
if weight.Compare(minWeight) == types.BalanceCompBigger {
ot.ValidVotes = ot.ValidVotes.Add(weight)
}
}
ot.ValidVotesPercent = fmt.Sprintf("%.2f%%", float64(ot.ValidVotes.Uint64()*100)/float64(supply.Uint64()))
return ot
}
//type PeersInfo struct {
// Count int `json:"count"`
// Infos []*types.PeerInfo `json:"infos"`
//}
func (q *NetApi) ConnectPeersInfo(count int, offset *int) ([]*types.PeerInfo, error) {
c, o, err := checkOffset(count, offset)
if err != nil {
return nil, err
}
var p []*types.PeerInfo
q.eb.Publish(common.EventPeersInfo, &p)
r := p[o : c+o]
return r, nil
}
func (q *NetApi) GetOnlinePeersInfo(count int, offset *int) ([]*types.PeerInfo, error) {
c, o, err := checkOffset(count, offset)
if err != nil {
return nil, err
}
var p []*types.PeerInfo
q.eb.Publish(common.EventOnlinePeersInfo, &p)
r := p[o : c+o]
return r, nil
}
func (q *NetApi) GetAllPeersInfo(count int, offset *int) ([]*types.PeerInfo, error) {
c, o, err := checkOffset(count, offset)
if err != nil {
return nil, err
}
pis := make([]*types.PeerInfo, 0)
err = q.ledger.GetPeersInfo(func(pi *types.PeerInfo) error {
pis = append(pis, pi)
return nil
})
if err != nil {
return nil, err
}
pis2 := pis[o : c+o]
return pis2, nil
}
func (q *NetApi) PeersCount() (map[string]uint64, error) {
var p []*types.PeerInfo
q.eb.Publish(common.EventPeersInfo, &p)
connectCount := len(p)
q.eb.Publish(common.EventOnlinePeersInfo, &p)
onlineCount := len(p)
var pa []*types.PeerInfo
err := q.ledger.GetPeersInfo(func(pi *types.PeerInfo) error {
pa = append(pa, pi)
return nil
})
if err != nil {
return nil, err
}
allCount := len(pa)
c := make(map[string]uint64)
c["connect"] = uint64(connectCount)
c["online"] = uint64(onlineCount)
c["all"] = uint64(allCount)
return c, nil
}
func (q *NetApi) GetBandwidthStats() *p2pmetrics.Stats {
stats := new(p2pmetrics.Stats)
q.eb.Publish(common.EventGetBandwidthStats, stats)
return stats
}
func (q *NetApi) Syncing() bool {
var ss common.SyncState
q.eb.Publish(common.EventSyncStatus, &ss)
if ss == common.Syncing || ss == common.SyncDone {
return true
}
return false
}