-
Notifications
You must be signed in to change notification settings - Fork 199
/
displayMetaBlock.go
188 lines (149 loc) · 5.39 KB
/
displayMetaBlock.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
package block
import (
"fmt"
"sync"
"github.com/ElrondNetwork/elrond-go/data/block"
"github.com/ElrondNetwork/elrond-go/display"
)
type headersCounter struct {
shardMBHeaderCounterMutex sync.RWMutex
shardMBHeadersCurrentBlockProcessed uint64
shardMBHeadersTotalProcessed uint64
}
// NewHeaderCounter returns a new object that keeps track of how many headers
// were processed in total, and in the current block
func NewHeaderCounter() *headersCounter {
return &headersCounter{
shardMBHeaderCounterMutex: sync.RWMutex{},
shardMBHeadersCurrentBlockProcessed: 0,
shardMBHeadersTotalProcessed: 0,
}
}
func (hc *headersCounter) subtractRestoredMBHeaders(numMiniBlockHeaders int) {
hc.shardMBHeaderCounterMutex.Lock()
hc.shardMBHeadersTotalProcessed -= uint64(numMiniBlockHeaders)
hc.shardMBHeaderCounterMutex.Unlock()
}
func (hc *headersCounter) countShardMBHeaders(numShardMBHeaders int) {
hc.shardMBHeaderCounterMutex.Lock()
hc.shardMBHeadersCurrentBlockProcessed += uint64(numShardMBHeaders)
hc.shardMBHeadersTotalProcessed += uint64(numShardMBHeaders)
hc.shardMBHeaderCounterMutex.Unlock()
}
func (hc *headersCounter) calculateNumOfShardMBHeaders(header *block.MetaBlock) {
hc.shardMBHeaderCounterMutex.Lock()
hc.shardMBHeadersCurrentBlockProcessed = 0
hc.shardMBHeaderCounterMutex.Unlock()
for i := 0; i < len(header.ShardInfo); i++ {
shardData := header.ShardInfo[i]
hc.countShardMBHeaders(len(shardData.ShardMiniBlockHeaders))
}
}
func (hc *headersCounter) displayLogInfo(
header *block.MetaBlock,
body block.Body,
headerHash []byte,
numHeadersFromPool int,
) {
hc.calculateNumOfShardMBHeaders(header)
dispHeader, dispLines := hc.createDisplayableMetaHeader(header)
dispLines = hc.displayTxBlockBody(dispLines, body)
tblString, err := display.CreateTableString(dispHeader, dispLines)
if err != nil {
log.Debug("CreateTableString", "error", err.Error())
return
}
hc.shardMBHeaderCounterMutex.RLock()
message := fmt.Sprintf("header hash: %s\n%s", display.DisplayByteSlice(headerHash), tblString)
arguments := []interface{}{
"total MB processed", hc.shardMBHeadersTotalProcessed,
"block MB processed", hc.shardMBHeadersCurrentBlockProcessed,
"shard headers in pool", numHeadersFromPool,
}
hc.shardMBHeaderCounterMutex.RUnlock()
log.Debug(message, arguments...)
}
func (hc *headersCounter) createDisplayableMetaHeader(
header *block.MetaBlock,
) ([]string, []*display.LineData) {
tableHeader := []string{"Part", "Parameter", "Value"}
metaLinesHeader := []*display.LineData{
display.NewLineData(false, []string{
"Header",
"Block type",
"MetaBlock"}),
}
lines := displayHeader(header)
metaLines := make([]*display.LineData, 0, len(lines)+len(metaLinesHeader))
metaLines = append(metaLines, metaLinesHeader...)
metaLines = append(metaLines, lines...)
metaLines = hc.displayShardInfo(metaLines, header)
return tableHeader, metaLines
}
func (hc *headersCounter) displayShardInfo(lines []*display.LineData, header *block.MetaBlock) []*display.LineData {
for i := 0; i < len(header.ShardInfo); i++ {
shardData := header.ShardInfo[i]
lines = append(lines, display.NewLineData(false, []string{
fmt.Sprintf("ShardData_%d", shardData.ShardID),
"Header hash",
display.DisplayByteSlice(shardData.HeaderHash)}))
if shardData.ShardMiniBlockHeaders == nil || len(shardData.ShardMiniBlockHeaders) == 0 {
lines = append(lines, display.NewLineData(false, []string{
"", "ShardMiniBlockHeaders", "<EMPTY>"}))
}
for j := 0; j < len(shardData.ShardMiniBlockHeaders); j++ {
if j == 0 || j >= len(shardData.ShardMiniBlockHeaders)-1 {
senderShard := shardData.ShardMiniBlockHeaders[j].SenderShardID
receiverShard := shardData.ShardMiniBlockHeaders[j].ReceiverShardID
lines = append(lines, display.NewLineData(false, []string{
"",
fmt.Sprintf("%d ShardMiniBlockHeaderHash_%d_%d", j+1, senderShard, receiverShard),
display.DisplayByteSlice(shardData.ShardMiniBlockHeaders[j].Hash)}))
} else if j == 1 {
lines = append(lines, display.NewLineData(false, []string{
"",
fmt.Sprintf("..."),
fmt.Sprintf("...")}))
}
}
lines[len(lines)-1].HorizontalRuleAfter = true
}
return lines
}
func (hc *headersCounter) displayTxBlockBody(lines []*display.LineData, body block.Body) []*display.LineData {
currentBlockTxs := 0
for i := 0; i < len(body); i++ {
miniBlock := body[i]
part := fmt.Sprintf("%s_MiniBlock_%d->%d",
miniBlock.Type.String(),
miniBlock.SenderShardID,
miniBlock.ReceiverShardID)
if miniBlock.TxHashes == nil || len(miniBlock.TxHashes) == 0 {
lines = append(lines, display.NewLineData(false, []string{
part, "", "<EMPTY>"}))
}
currentBlockTxs += len(miniBlock.TxHashes)
for j := 0; j < len(miniBlock.TxHashes); j++ {
if j == 0 || j >= len(miniBlock.TxHashes)-1 {
lines = append(lines, display.NewLineData(false, []string{
part,
fmt.Sprintf("TxHash_%d", j+1),
display.DisplayByteSlice(miniBlock.TxHashes[j])}))
part = ""
} else if j == 1 {
lines = append(lines, display.NewLineData(false, []string{
part,
fmt.Sprintf("..."),
fmt.Sprintf("...")}))
part = ""
}
}
lines[len(lines)-1].HorizontalRuleAfter = true
}
return lines
}
func (hc *headersCounter) getNumShardMBHeadersTotalProcessed() uint64 {
hc.shardMBHeaderCounterMutex.Lock()
defer hc.shardMBHeaderCounterMutex.Unlock()
return hc.shardMBHeadersTotalProcessed
}