-
Notifications
You must be signed in to change notification settings - Fork 8
/
connections.go
68 lines (55 loc) · 1.74 KB
/
connections.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
package types
import (
"encoding/json"
"net"
"strings"
)
// NodeType is the type of peer (farmer, full node, etc)
type NodeType uint8
const (
// NodeTypeFullNode NodeType for full node
NodeTypeFullNode NodeType = 1
// NodeTypeHarvester NodeType for Harvester
NodeTypeHarvester NodeType = 2
// NodeTypeFarmer NodeType for Farmer
NodeTypeFarmer NodeType = 3
// NodeTypeTimelord NodeType for Timelord
NodeTypeTimelord NodeType = 4
// NodeTypeIntroducer NodeType for Introducer
NodeTypeIntroducer NodeType = 5
// NodeTypeWallet NodeType for Wallet
NodeTypeWallet NodeType = 6
)
// IPAddress wraps net.IP so we can fix unmarshalling issues
// due to invalid [] wrapping ipv6 addresses in Chia RPC
type IPAddress struct {
net.IP
}
// Connection represents a single peer or internal connection
type Connection struct {
BytesRead uint64 `json:"bytes_read"`
BytesWritten uint64 `json:"bytes_written"`
//CreationTime // @TODO parse to time - is seconds as float
//LastMessageTime // @TODO parse to time - is seconds as float
LocalPort uint16 `json:"local_port"`
NodeID string `json:"node_id"`
PeakHash string `json:"peak_hash"`
PeakHeight uint32 `json:"peak_height"`
PeakWeight Uint128 `json:"peak_weight"`
PeerHost IPAddress `json:"peer_host"`
PeerPort uint16 `json:"peer_port"`
PeerServerPort uint16 `json:"peer_server_port"`
Type NodeType `json:"type"`
}
// UnmarshalJSON Unmarshals IP
func (ip *IPAddress) UnmarshalJSON(data []byte) error {
strData := strings.ReplaceAll(string(data), "[", "")
strData = strings.ReplaceAll(strData, "]", "")
ipval := &net.IP{}
err := json.Unmarshal([]byte(strData), ipval)
if err != nil {
return err
}
ip.IP = *ipval
return nil
}