This repository was archived by the owner on Mar 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDisplay-Functions.ps1
340 lines (321 loc) · 11.7 KB
/
Display-Functions.ps1
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
function Format-Human {
Param(
[int]$num
)
$suffix = ''
if($num -ge 1000*1000*1000*1000) {
$num = ($num*1.0) / (1000*1000*1000*1000)
$suffix = ' T'
} elseif($num -ge 1000*1000*1000) {
$num = ($num*1.0) / (1000*1000*1000)
$suffix = ' G'
} elseif($num -ge 1000*1000) {
$num = ($num*1.0) / (1000*1000)
$suffix = ' M'
} elseif($num -ge 1000) {
$num = ($num*1.0) / (1000)
$suffix = ' K'
}
return "$($num.ToString('0.00'))$($suffix)"
}
function Render-Header {
Param(
[int]$NumNodes
)
Write-Host -ForegroundColor White "------------------------------------------------------------------------"
$now = Get-Date
Write-Host -ForegroundColor White "Last refresh: $($now.ToString())"
Write-Host -ForegroundColor Yellow "Testnet has $($NumNodes) nodes."
}
function Render-Indexes {
Param(
[System.Collections.ArrayList]$Clients
)
Write-Host -ForegroundColor White -NoNewline "PID:".PadRight(10)
foreach($c in $Clients) {
if($c.idx -ge 13) { break }
if($c.Display -eq $false) { continue }
Write-Host -ForegroundColor Green -NoNewline "$($c.Process.Id.ToString().PadLeft(10))"
}
Write-Host ""
}
function Render-Status {
Param(
[System.Collections.ArrayList]$dlt_statuses,
[System.Collections.ArrayList]$bp_statuses
)
Write-Host -ForegroundColor White -NoNewline "STATUS:".PadRight(10)
$num_elem = $dlt_statuses.Count
if($num_elem -gt $bp_statuses.Count) {
$num_elem = $bp_statuses.Count
}
for($i = 0; $i -lt $num_elem; $i++) {
$dlt_s = $dlt_statuses[$i]
$bp_s = $bp_statuses[$i]
$dlt_c = [ConsoleColor]::Green
$bp_c = [ConsoleColor]::Green
if($dlt_s -eq "SYN") {
$dlt_c = [ConsoleColor]::Yellow
} elseif($dlt_s -eq "DEAD") {
$dlt_c = [ConsoleColor]::Magenta
}
if($bp_s -eq "STOP") {
$bp_c = [ConsoleColor]::Red
} elseif($bp_s -eq "DEAD") {
$bp_c = [ConsoleColor]::Magenta
}
$len = 10 - $dlt_s.Length - $bp_s.Length - 1
Write-Host -NoNewline "$(New-Object String (' ', $len))"
Write-Host -NoNewline -ForegroundColor $dlt_c $dlt_s
Write-Host -NoNewline -ForegroundColor White "/"
Write-Host -NoNewline -ForegroundColor $bp_c $bp_s
}
Write-Host ""
}
function Render-BlockHeight {
Param(
[System.Collections.ArrayList]$bhs
)
Write-Host -NoNewline -ForegroundColor White "B.HEIGHT:".PadRight(10)
foreach($bh in $bhs) {
Write-Host -NoNewline -ForegroundColor Cyan $bh.ToString().PadLeft(10)
}
Write-Host ""
}
function Render-Consensus {
Param(
[System.Collections.ArrayList]$consensus
)
Write-Host -NoNewline -ForegroundColor White "CONSENSUS:"
foreach($c in $consensus) {
Write-Host -NoNewline -ForegroundColor Yellow $c.ToString().PadLeft(10)
}
Write-Host ""
}
function Render-Presences {
Param(
[System.Collections.ArrayList]$presences
)
Write-Host -NoNewline -ForegroundColor White "PRESENCES:"
foreach($p in $presences) {
Write-Host -NoNewline -ForegroundColor Yellow $p.ToString().PadLeft(10)
}
Write-Host ""
}
function Render-Connections {
Param(
[System.Collections.ArrayList]$in_conn,
[System.Collections.ArrayList]$out_conn
)
Write-Host -ForegroundColor White -NoNewline "N(In/Out):"
$num_elem = $in_conn.Count
if($num_elem -gt $out_conn.Count) {
$num_elem = $out_conn.Count
}
for($i = 0; $i -lt $num_elem; $i++) {
$in_s = $in_conn[$i].ToString()
$out_s = $out_conn[$i].ToString()
$len = 10 - $in_s.Length - $out_s.Length - 1
Write-Host -NoNewline "$(New-Object String (' ', $len))"
Write-Host -NoNewline -ForegroundColor Cyan $in_s
Write-Host -NoNewline -ForegroundColor White "/"
Write-Host -NoNewline -ForegroundColor Cyan $out_s
}
Write-Host ""
}
function Render-TXs {
Param(
[System.Collections.ArrayList]$applied,
[System.Collections.ArrayList]$unapplied
)
Write-Host -ForegroundColor White -NoNewline "TX(A/U): "
$num_elem = $applied.Count
if($num_elem -gt $unapplied.Count) {
$num_elem = $unapplied.Count
}
for($i = 0; $i -lt $num_elem; $i++) {
$a_s = $applied[$i].ToString()
$u_s = $unapplied[$i].ToString()
$len = 10 - $a_s.Length - $u_s.Length - 1
Write-Host -NoNewline "$(New-Object String (' ', $len))"
Write-Host -NoNewline -ForegroundColor Cyan $a_s
Write-Host -NoNewline -ForegroundColor White "/"
Write-Host -NoNewline -ForegroundColor Cyan $u_s
}
Write-Host ""
}
function Render-MinerHashrate {
Param(
[System.Collections.ArrayList]$hashrates
)
Write-Host -NoNewline -ForegroundColor White "HASH:".PadRight(10)
foreach($hr in $hashrates) {
if($hr -eq 0) {
Write-Host -NoNewline -ForegroundColor Red "N/A".PadLeft(10)
} else {
$hr_t = Format-Human $hr
Write-Host -NoNewline -ForegroundColor Green $hr_t.PadLeft(10)
}
}
Write-Host ""
}
function Render-MinerSolved {
Param(
[System.Collections.ArrayList]$solved_counts
)
Write-Host -NoNewline -ForegroundColor White "SOLVED:".PadRight(10)
foreach($s in $solved_counts) {
Write-Host -NoNewline -ForegroundColor Green $s.ToString().PadLeft(10)
}
Write-Host ""
}
function Render-GlobalStats {
Param(
[int]$solved,
[int]$unsolved,
[int]$wallets,
[uint64]$difficulty
)
Write-Host -ForegroundColor Gray "========================================================================"
Write-Host -ForegroundColor White "Global stats:"
# wallets
Write-Host -ForegroundColor White -NoNewline "Wallets: "
Write-Host -ForegroundColor Cyan "$($wallets)"
# Block solution stats
Write-Host -ForegroundColor White -NoNewline "Solved/Unsolved blocks: "
Write-Host -ForegroundColor Cyan -NoNewline "$($solved)"
Write-Host -ForegroundColor White -NoNewline "/"
Write-Host -ForegroundColor Green -NoNewline "$($unsolved)"
$ratio = 100.0 * $solved / $unsolved
Write-Host -ForegroundColor White -NoNewline " ("
$col = [ConsoleColor]::Cyan
if($solved -eq $unsolved) {
$col = [ConsoleColor]::Green
} elseif($solved -gt $unsolved) {
$col = [ConsoleColor]::Yellow
}
Write-Host -ForegroundColor $col -NoNewline "$($ratio.ToString('0.00'))"
Write-Host -ForegroundColor White ")"
# difficulty
Write-Host -ForegroundColor White -NoNewline "Difficulty: "
Write-Host -ForegroundColor Cyan -NoNewline "$($difficulty)"
Write-Host -ForegroundColor White -NoNewline " ("
$hex_diff = [System.BitConverter]::ToString([System.BitConverter]::GetBytes($difficulty)).Replace("-", "")
Write-Host -ForegroundColor Green -NoNewline "$($hex_diff)"
Write-Host -ForegroundColor White ")"
}
function Render-Footer {
Write-Host -ForegroundColor White "Keys:"
Write-Host -ForegroundColor White " - N : Add another DLT node to the network"
Write-Host -ForegroundColor White " - M : Add another Mining node to the network"
Write-Host -ForegroundColor White " - C : Cleanup dead nodes from the table"
Write-Host -ForegroundColor White " - E : Stop all nodes and exit"
Write-Host -ForegroundColor Yellow " - X : DETACH FROM NODE PROCESSES AND LEAVE THE NETWORK RUNNING"
}
function Display-ClientStatus {
Param(
[System.Collections.ArrayList]$Clients
)
$count = 0
$dlt_statuses = New-Object System.Collections.ArrayList (13)
$bp_statuses = New-Object System.Collections.ArrayList (13)
$node_bhs = New-Object System.Collections.ArrayList (13)
$consensus = New-Object System.Collections.ArrayList (13)
$presences = New-Object System.Collections.ArrayList (13)
$in_connections = New-Object System.Collections.ArrayList (13)
$out_connections = New-Object System.Collections.ArrayList (13)
$applied_txs = New-Object System.Collections.ArrayList (13)
$unapplied_txs = New-Object System.Collections.ArrayList (13)
# Miner:
$hashrates = New-Object System.Collections.ArrayList (13)
$solved_counts = New-Object System.Collections.ArrayList (13)
# Globals
$global_solved = 0
$global_unsolved = 0
$global_wallets = 0
$global_difficulty = 0
$mining_globals_captured = $false
foreach($node in $Clients) {
if($node.Display -eq $false) { continue }
$ns = $null
if($node.Dead -eq $false) {
$ns = Get-DLTNodeStatus -Clients $Clients -NodeIdx $node.idx
}
if($ns -eq $null) {
# Node probably died
$node.Dead = $true
[void]$dlt_statuses.Add("DEAD")
[void]$bp_statuses.Add("DEAD")
[void]$node_bhs.Add(0)
[void]$consensus.Add(0)
[void]$presences.Add(0)
[void]$in_connections.Add(0)
[void]$out_connections.Add(0)
[void]$applied_txs.Add(0)
[void]$unapplied_txs.Add(0)
[void]$hashrates.Add(0)
[void]$solved_counts.Add(0)
} else {
# DLT Status
[void]$dlt_statuses.Add(($ns.'DLT Status').Substring(0,3).ToUpper())
# Block processor status
$bp_status = ($ns.'Block Processor Status')
if($bp_status -eq "Stopped") { $bp_status = "STOP" }
if($bp_status -eq "Running") { $bp_status = "OK" }
[void]$bp_statuses.Add($bp_status)
# Block Height
[void]$node_bhs.Add(($ns.'Block Height'))
# Consensus
[void]$consensus.Add(($ns.'Required Consensus'))
# Presences
[void]$presences.Add(($ns.'Presences'))
# In / Out connections
[void]$in_connections.Add(($ns.'Network Clients').Count)
[void]$out_connections.Add(($ns.'Network Servers').Count)
# Applied / Unapplied TXs
[void]$applied_txs.Add(($ns.'Applied TX Count'))
[void]$unapplied_txs.Add(($ns.'Unapplied TX Count'))
# Miner
$ms = Get-DLTMinerStatus -Clients $Clients -NodeIdx $node.idx
if($ms -eq $null) {
$node.Dead = $true
[void]$hashrates.Add(0)
[void]$solved_counts.Add(0)
} else {
# Hashrates
[void]$hashrates.Add(($ms.'Hashrate'))
# Solved
[void]$solved_counts.Add(($ms.'Solved Blocks (Local)'))
if($count -eq 0) {
# Caputer global stats from first node
$global_wallets = $ns.'Wallets'
}
if($node.Miner -eq $true -and $mining_globals_captured -eq $false) {
# Capture mining globals from the first miner
$global_solved = $ms.'Solved Blocks (Network)'
$global_unsolved = $ms.'Empty Blocks'
$global_difficulty = $ms.'Current Difficulty'
$mining_globals_captured = $true
}
}
$count++
if($Count -ge 13) {
break
}
}
}
#
Clear-Host
Render-Header $Clients.Count
Render-Indexes $Clients
Render-Status $dlt_statuses $bp_statuses
Render-BlockHeight $node_bhs
Render-Consensus $consensus
Render-Presences $presences
Render-Connections $in_connections $out_connections
Render-TXs $applied_txs $unapplied_txs
Render-MinerHashrate $hashrates
Render-MinerSolved $solved_counts
Render-GlobalStats $global_solved $global_unsolved $global_wallets $global_difficulty
Render-Footer
}