forked from soteria-dag/soterdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.go
213 lines (185 loc) · 5.05 KB
/
models.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) 2018-2019 The Soteria DAG developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"html/template"
"time"
"github.com/totaloutput/soterdash/census"
"github.com/totaloutput/soterd/chaincfg/chainhash"
"github.com/totaloutput/soterd/rpcclient"
"github.com/totaloutput/soterd/soterjson"
"github.com/totaloutput/soterd/soterutil"
"github.com/totaloutput/soterd/wire"
)
const (
// How many generations from tips we'll render for RecentDagSvg
recentDagRange = int32(3)
)
// Represent block data that we're interested in rendering
type soterdBlock struct {
Header wire.BlockHeader
Parents wire.ParentSubHeader
Transactions []*wire.MsgTx
Confirmations int64
Height int32
MerkleRoot string
NextHashes []string
Difficulty float64
}
// Represents census-enumerated node data that we're interested in rendering
type soterdNode struct {
Address string
Version string
Online bool
Connections []*census.Node
LastChecked time.Time
Stale bool
}
// Represent node data that we're interested in rendering
type soterdRPCNode struct {
Id int
// The dag net of the node (testnet, etc)
Net string
Version string
InboundPeers map[int32]*soterjson.GetPeerInfoResult
OutboundPeers map[int32]*soterjson.GetPeerInfoResult
InboundPeerCount int
OutboundPeerCount int
// Hashes of dag tips of node
Tips []string
// Hash of tips of node
VirtualHash string
MinHeight int32
MaxHeight int32
BlkCount uint32
// SVG rendering of recent dag (MaxHeight - recentDagRange generations) to MaxHeight
RecentDagSvg template.HTML
}
// sortPeers returns the number of **unique** peer connections
func sortPeers(peers []soterjson.GetPeerInfoResult) (map[int32]*soterjson.GetPeerInfoResult, map[int32]*soterjson.GetPeerInfoResult) {
inbound := make(map[int32]*soterjson.GetPeerInfoResult)
outbound := make(map[int32]*soterjson.GetPeerInfoResult)
for _, p := range peers {
var targetMap *map[int32]*soterjson.GetPeerInfoResult
if p.Inbound {
targetMap = &inbound
} else {
targetMap = &outbound
}
_, exists := (*targetMap)[p.ID]
if exists {
continue
}
(*targetMap)[p.ID] = &p
}
return inbound, outbound
}
// blockInfo returns a soterdBlock, which can be rendered
func blockInfo(c *rpcclient.Client, hash string) (soterdBlock, error) {
h, err := chainhash.NewHashFromStr(hash)
if err != nil {
return soterdBlock{}, err
}
block, err := c.GetBlock(h)
if err != nil {
return soterdBlock{}, err
}
header, err := c.GetBlockHeaderVerbose(h)
if err != nil {
return soterdBlock{}, err
}
// NOTE(cedric): soterd node(s) we're connecting to need to be running a wallet service, in order for us to use
// gettransaction to pull more transaction info.
sb := soterdBlock{
Header: block.Header,
Parents: block.Parents,
Transactions: block.Transactions,
Confirmations: header.Confirmations,
Height: header.Height,
MerkleRoot: header.MerkleRoot,
NextHashes: header.NextHashes,
Difficulty: header.Difficulty,
}
return sb, nil
}
// rpcNodeInfo returns a soterdRPCNode struct, which can be rendered
func rpcNodeInfo(c *rpcclient.Client) (soterdRPCNode, error) {
// Node network
net, err := c.GetCurrentNet()
if err != nil {
return soterdRPCNode{}, err
}
// Version
result, err := c.Version()
if err != nil {
return soterdRPCNode{}, err
}
verInfo, verExists := result["soterdjsonrpcapi"]
// Peers
peers, err := c.GetPeerInfo()
if err != nil {
return soterdRPCNode{}, err
}
inbound, outbound := sortPeers(peers)
// Dag tip
tips, err := c.GetDAGTips()
if err != nil {
return soterdRPCNode{}, err
}
// Determine dag rendering range
minHeight := (tips.MaxHeight - recentDagRange)
if minHeight < 0 {
minHeight = 0
}
// Dag svg rendering
nodes := []*rpcclient.Client{c}
dot, err := RenderDagsDot(nodes, minHeight, tips.MaxHeight)
if err != nil {
return soterdRPCNode{}, err
}
svg, err := soterutil.DotToSvg(dot)
if err != nil {
return soterdRPCNode{}, err
}
svgEmbed, err := soterutil.StripSvgXmlDecl(svg)
if err != nil {
return soterdRPCNode{}, err
}
// NOTE(cedric): Could call getnettotals to add to node Network info
// Assemble the data into a soterdRPCNode model
n := soterdRPCNode{
Net: net.String(),
InboundPeers: inbound,
OutboundPeers: outbound,
InboundPeerCount: len(inbound),
OutboundPeerCount: len(outbound),
Tips: tips.Tips,
VirtualHash: tips.Hash,
MinHeight: tips.MinHeight,
MaxHeight: tips.MaxHeight,
BlkCount: tips.BlkCount,
RecentDagSvg: template.HTML(svgEmbed),
}
if verExists {
n.Version = verInfo.VersionString
}
return n, nil
}
// nodeInfo returns a soterdNode, which can be rendered
func nodeInfo(address string) (soterdNode, error) {
cNode, exists := e.Get(address)
if !exists {
return soterdNode{}, fmt.Errorf("node with address %s not found in census", address)
}
n := soterdNode{
Address: cNode.Address,
Version: cNode.Version,
Online: cNode.Online,
Connections: cNode.Connections(),
LastChecked: cNode.LastChecked,
Stale: cNode.IsStale(e.Interval * 3),
}
return n, nil
}