/
block_explorer_data_func.go
188 lines (155 loc) · 4.61 KB
/
block_explorer_data_func.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 blockexplorer
import (
"strconv"
"time"
"github.com/fletaio/core/block"
)
func (e *BlockExplorer) transactions() []*countInfo {
return e.transactionCountList
}
func (e *BlockExplorer) chainInfo() currentChainInfo {
return e.CurrentChainInfo
}
type typePerBlock struct {
BlockTime uint64 `json:"blockTime"`
Symbol string `json:"symbol"`
TxCount string `json:"txCount"`
// Types map[string]int `json:"types"`
}
type blockInfos struct {
BlockHeight uint32 `json:"Block Height"`
BlockHash string `json:"Block Hash"`
Time string `json:"Time"`
Status string `json:"Status"`
Txs string `json:"Txs"`
Formulator string `json:"Formulator"`
Msg string `json:"Msg"`
Signs []string `json:"Signs"`
BlockCount uint32 `json:"BlockCount"`
}
type blockInfosCase struct {
ITotalRecords int `json:"iTotalRecords"`
ITotalDisplayRecords int `json:"iTotalDisplayRecords"`
SEcho int `json:"sEcho"`
SColumns string `json:"sColumns"`
AaData []blockInfos `json:"aaData"`
}
func (e *BlockExplorer) lastestBlocks() (result blockInfosCase) {
currHeight := e.Kernel.Provider().Height()
result.AaData = []blockInfos{}
for i := currHeight; i > 0 && i > currHeight-8; i-- {
b, err := e.Kernel.Block(i)
if err != nil {
continue
}
b.Header.Hash().String()
cd, err := e.Kernel.Provider().Data(i)
if err != nil {
continue
}
status := 1
if b.Header.TimeoutCount > 0 {
status = 2
}
bs := block.Signed{
HeaderHash: cd.Header.Hash(),
GeneratorSignature: cd.Signatures[0],
}
Signs := []string{
cd.Signatures[1].String(),
cd.Signatures[2].String(),
cd.Signatures[3].String(),
}
tm := time.Unix(int64(cd.Header.Timestamp()/uint64(time.Second)), 0)
result.AaData = append(result.AaData, blockInfos{
BlockHeight: i,
BlockHash: cd.Header.Hash().String(),
Time: tm.Format("2006-01-02 15:04:05"),
Status: strconv.Itoa(status),
Txs: strconv.Itoa(len(b.Body.Transactions)),
Formulator: b.Header.Formulator.String(),
Msg: bs.Hash().String(),
Signs: Signs,
BlockCount: e.GetBlockCount(b.Header.Formulator.String()),
})
}
result.ITotalRecords = len(result.AaData)
result.ITotalDisplayRecords = len(result.AaData)
return
}
type txInfos struct {
TxHash string `json:"TxHash"`
BlockHash string `json:"BlockHash"`
ChainID string `json:"ChainID"`
Time uint64 `json:"Time"`
TxType string `json:"TxType"`
}
func (e *BlockExplorer) lastestTransactions() []txInfos {
if len(e.lastestTransactionList) < 8 {
return e.lastestTransactionList[0:len(e.lastestTransactionList)]
}
return e.lastestTransactionList[0:8]
}
func (e *BlockExplorer) blocks(start int, currHeight uint32) []blockInfos {
length := 10
aaData := []blockInfos{}
for i, j := currHeight-uint32(start), 0; i > 0 && j < length; i, j = i-1, j+1 {
b, err := e.Kernel.Block(i)
if err != nil {
continue
}
cd, err := e.Kernel.Provider().Data(i)
if err != nil {
continue
}
status := 1
if b.Header.TimeoutCount > 0 {
status = 2
}
tm := time.Unix(int64(cd.Header.Timestamp()/uint64(time.Second)), 0)
aaData = append(aaData, blockInfos{
BlockHeight: i,
BlockHash: cd.Header.Hash().String(),
Time: tm.Format("2006-01-02 15:04:05"),
Status: strconv.Itoa(status),
Txs: strconv.Itoa(len(b.Body.Transactions)),
})
}
return aaData
}
func (e *BlockExplorer) paginationBlocks(startStr string) (result blockInfosCase) {
start, err := strconv.Atoi(startStr)
if err != nil {
return
}
currHeight := e.Kernel.Provider().Height()
result.ITotalRecords = int(currHeight)
result.ITotalDisplayRecords = int(currHeight)
result.AaData = e.blocks(start, currHeight)
return
}
type txInfosCase struct {
ITotalRecords int `json:"iTotalRecords"`
ITotalDisplayRecords int `json:"iTotalDisplayRecords"`
SEcho int `json:"sEcho"`
SColumns string `json:"sColumns"`
AaData []txInfos `json:"aaData"`
}
func (e *BlockExplorer) txs(start int, length int) []txInfos {
max := start + length
if max > len(e.lastestTransactionList) {
max = len(e.lastestTransactionList)
}
return e.lastestTransactionList[start:max]
}
func (e *BlockExplorer) paginationTxs(startStr string) (result txInfosCase) {
start, err := strconv.Atoi(startStr)
if err != nil {
return
}
length := 10
result.ITotalRecords = len(e.lastestTransactionList)
result.ITotalDisplayRecords = len(e.lastestTransactionList)
result.AaData = e.txs(start, length)
return
}