-
Notifications
You must be signed in to change notification settings - Fork 34
/
net.go
151 lines (132 loc) · 4.9 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
package rpc
import (
"fmt"
"math"
"time"
"github.com/copernet/copernicus/net/server"
"github.com/copernet/copernicus/net/wire"
"github.com/copernet/copernicus/rpc/btcjson"
"github.com/copernet/copernicus/service"
"github.com/copernet/copernicus/util"
)
var netHandlers = map[string]commandHandler{
"getconnectioncount": handleGetConnectionCount,
"ping": handlePing,
"getpeerinfo": handleGetPeerInfo,
"addnode": handleAddNode,
"disconnectnode": handleDisconnectNode,
"getaddednodeinfo": handleGetAddedNodeInfo,
"getnettotals": handleGetNetTotals,
"getnetworkinfo": handleGetnetWorkInfo,
"setban": handleSetBan,
"listbanned": handleListBanned,
"clearbanned": handleClearBanned,
"setnetworkactive": handleSetNetWorkActive,
}
func handleGetConnectionCount(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
request := &service.GetConnectionCountRequest{}
response, err := server.ProcessForRPC(request)
if err != nil {
return nil, btcjson.RPCError{
Code: btcjson.RPCInternalError,
Message: "Can not acquire connection count",
}
}
count, ok := response.(*service.GetConnectionCountResponse)
if !ok {
return nil, btcjson.RPCError{
Code: btcjson.RPCInternalError,
Message: "Server handle error",
}
}
return count.Count, nil
}
func handlePing(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
nonce := util.GetRand(math.MaxInt64)
pingCmd := wire.NewMsgPing(nonce)
_, err := server.ProcessForRPC(pingCmd)
if err != nil {
return nil, btcjson.RPCError{
Code: btcjson.RPCInternalError,
Message: "Can not acquire connection count",
}
}
return nil, nil
}
func handleGetPeerInfo(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
getPeerInfoCmd := &service.GetPeersInfoRequest{}
ret, _ := server.ProcessForRPC(getPeerInfoCmd) // todo Alert: match with return type
peers := ret.([]server.RPCServerPeer)
//syncPeerID := s.cfg.SyncMgr.SyncPeerID()
infos := make([]*btcjson.GetPeerInfoResult, 0, len(peers))
for _, item := range peers {
statsSnap := item.ToPeer().StatsSnapshot()
info := &btcjson.GetPeerInfoResult{
ID: statsSnap.ID,
Addr: statsSnap.Addr,
AddrLocal: item.ToPeer().LocalAddr().String(),
Services: fmt.Sprintf("%016x", statsSnap.Services),
RelayTxes: !item.IsTxRelayDisabled(),
LastSend: statsSnap.LastSend.Unix(),
LastRecv: statsSnap.LastRecv.Unix(),
BytesSent: statsSnap.BytesSent,
BytesRecv: statsSnap.BytesRecv,
ConnTime: statsSnap.ConnTime.Unix(),
TimeOffset: statsSnap.TimeOffset,
PingTime: float64(statsSnap.LastPingMicros),
MinPing: statsSnap.MingPing,
Version: statsSnap.Version,
SubVer: statsSnap.UserAgent,
Inbound: statsSnap.Inbound,
AddNode: statsSnap.AddNode,
StartingHeight: statsSnap.StartingHeight,
BanScore: int32(item.BanScore()), // TODO
SyncedHeaders: statsSnap.SyncedHeaders,
SyncedBlocks: statsSnap.SyncedBlocks,
Inflight: statsSnap.Inflight,
WhiteListed: statsSnap.WhiteListed,
CashMagic: statsSnap.UsesCashMagic,
BytesSendPerMsg: statsSnap.MapSendBytesPerMsgCmd,
BytesRecvPerMsg: statsSnap.MapRecvBytesPerMsgCmd,
}
if item.ToPeer().LastPingNonce() != 0 {
wait := float64(time.Since(statsSnap.LastPingTime).Nanoseconds())
// We actually want microseconds.
info.PingWait = wait / 1000
}
infos = append(infos, info)
}
return infos, nil
}
func handleAddNode(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
return server.ProcessForRPC(cmd)
}
func handleDisconnectNode(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
return server.ProcessForRPC(cmd)
}
func handleGetAddedNodeInfo(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
return server.ProcessForRPC(cmd)
}
func handleGetNetTotals(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
return server.ProcessForRPC(cmd)
}
func handleGetnetWorkInfo(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
return server.ProcessForRPC(cmd)
}
func handleSetBan(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
return server.ProcessForRPC(cmd)
}
func handleListBanned(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
return server.ProcessForRPC(cmd)
}
func handleClearBanned(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
return server.ProcessForRPC(cmd)
}
func handleSetNetWorkActive(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
return server.ProcessForRPC(cmd)
}
func registerNetRPCCommands() {
for name, handler := range netHandlers {
appendCommand(name, handler)
}
}