-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
349 lines (297 loc) · 7.77 KB
/
process.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
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
341
342
343
344
345
346
347
348
349
package filacct
import (
"encoding/json"
"fmt"
"io/ioutil"
"math"
"sort"
"strconv"
"time"
)
const dateFmt = "2006-01-02"
type balance struct {
Available string `json:"availableBalance"`
Balance string `json:"balance"`
Height int `json:"height"`
Pledged string `json:"sectorPledgeBalance"`
Timestramp int64 `json:"timestamp"`
Vesting string `json:"vestingFunds"`
}
type Balance struct {
Available string `json:"available,omitempty"`
Pledged string `json:"pledged,omitempty"`
Locked string `json:"locked,omitempty"`
}
type transfer struct {
From string `json:"from,omitempty"`
Height int `json:"height,omitempty"`
Message string `json:"message,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
To string `json:"to,omitempty"`
Type string `json:"type,omitempty"`
Value string `json:"value,omitempty"`
}
type transfers struct {
TotalCount int `json:"totalCount"`
Transfers []transfer `json:"transfers"`
Types []string `json:"types"`
}
type Assets struct {
Transferred string `json:"transferred,omitempty"`
}
type blocks struct {
Blocks []block `json:"blocks"`
TotalCount int `json:"totalCount"`
}
type block struct {
CID string `json:"cid"`
Height int `json:"height"`
Reward string `json:"reward"`
Timestamp int64 `json:"timestamp"`
WinCount int `json:"winCount"`
}
type Blocks struct {
Count int `json:"count,omitempty"`
Reward string `json:"reward,omitempty"`
}
type Fees struct {
MinerFee string `json:"miner_fee,omitempty"`
BurnFee string `json:"burn_fee,omitempty"`
WindowPoSt string `json:"window_post,omitempty"`
PreCommit string `json:"pre_commit,omitempty"`
ProveCommit string `json:"prove_commit,omitempty"`
MinerPenalty string `json:"miner_penalty,omitempty"`
Other string `json:"other,omitempty"`
}
type Penalty struct {
Value string
}
type Sent struct {
Value string
}
type Result struct {
Balance `json:"balance,omitempty"`
Assets `json:"assets,omitempty"`
Blocks `json:"blocks,omitempty"`
Fees `json:"fees,omitempty"`
Penalty `json:"penalty,omitempty"`
Sent `json:"sent,omitempty"`
}
type Query struct {
StartDate, EndDate time.Time
Address string
}
func QueryMiner(q Query) (*Result, error) {
m := &Miner{Address: q.Address}
// turn time.Time to time.Unix
start := q.StartDate.Unix()
end := q.EndDate.Unix()
balance, err := m.GetBalance()
if err != nil {
return nil, err
}
assets, err := m.GetAssets(start, end)
if err != nil {
return nil, err
}
penalties, err := m.GetPenalties(start, end)
if err != nil {
return nil, err
}
sent, err := m.GetSent(start, end)
if err != nil {
return nil, err
}
blocks, err := m.GetBlocks(start, end)
if err != nil {
return nil, err
}
fees, err := m.GetFees(start, end)
if err != nil {
return nil, err
}
res := &Result{
Balance: balance,
Assets: assets,
Blocks: blocks,
Fees: fees,
Penalty: penalties,
Sent: sent,
}
return res, nil
}
func (m *Miner) GetBalance() (Balance, error) {
// read balance json file
content, err := ioutil.ReadFile(fmt.Sprintf("%s/balance-stats.json", m.Address))
if err != nil {
return Balance{}, err
}
b := []balance{}
err = json.Unmarshal(content, &b)
if err != nil {
return Balance{}, err
}
sort.SliceStable(b, func(i, j int) bool { return b[i].Height > b[j].Height })
available, err := strconv.ParseFloat(b[0].Available, 64)
if err != nil {
return Balance{}, fmt.Errorf("parse available: %w", err)
}
pledged, err := strconv.ParseFloat(b[0].Pledged, 64)
if err != nil {
return Balance{}, fmt.Errorf("parse pledged: %w", err)
}
locked, err := strconv.ParseFloat(b[0].Vesting, 64)
if err != nil {
return Balance{}, fmt.Errorf("parse locked: %w", err)
}
// get latest balance element
return Balance{Available: FilFloat(available), Pledged: FilFloat(pledged), Locked: FilFloat(locked)}, nil
}
func (m *Miner) GetPenalties(start, end int64) (Penalty, error) {
subset, err := m.GetTransfers(start, end)
if err != nil {
return Penalty{}, err
}
var amount float64
for _, s := range subset {
if s.Message == "" && s.Type == "burn" {
a, _ := strconv.ParseFloat(s.Value, 64)
amount += a
}
}
faults := math.Abs(amount)
return Penalty{Value: FilFloat(faults)}, nil
}
func (m *Miner) GetAssets(start, end int64) (Assets, error) {
subset, err := m.GetTransfers(start, end)
if err != nil {
return Assets{}, err
}
var amount float64
for _, s := range subset {
if s.Type == "receive" {
a, _ := strconv.ParseFloat(s.Value, 64)
amount += a
}
}
asset := Assets{Transferred: FilFloat(amount)}
return asset, nil
}
func (m *Miner) GetSent(start, end int64) (Sent, error) {
subset, err := m.GetTransfers(start, end)
if err != nil {
return Sent{}, err
}
var amount float64
for _, s := range subset {
if s.Type == "send" {
a, _ := strconv.ParseFloat(s.Value, 64)
amount += a
}
}
return Sent{Value: FilFloat(amount)}, nil
}
func (m *Miner) GetBlocks(start, end int64) (Blocks, error) {
content, err := ioutil.ReadFile(fmt.Sprintf("%s/blocks.json", m.Address))
if err != nil {
return Blocks{}, err
}
blks := blocks{}
err = json.Unmarshal(content, &blks)
if err != nil {
return Blocks{}, err
}
sort.SliceStable(blks.Blocks, func(i, j int) bool { return blks.Blocks[i].Height < blks.Blocks[j].Height })
filtered := []block{}
for _, b := range blks.Blocks {
if b.Timestamp >= start && b.Timestamp <= end {
filtered = append(filtered, b)
}
}
var reward float64
for _, f := range filtered {
r, _ := strconv.ParseFloat(f.Reward, 64)
reward += r
}
blk := Blocks{
Count: len(filtered),
Reward: FilFloat(reward),
}
return blk, nil
}
func (m *Miner) GetFees(start, end int64) (Fees, error) {
messages, err := m.MarshalAllMsgs()
if err != nil {
return Fees{}, err
}
sort.SliceStable(messages, func(i, j int) bool { return messages[i].Timestamp > messages[j].Timestamp })
subset := []MsgCut{}
for _, m := range messages {
if m.Timestamp >= start && m.Timestamp <= end {
subset = append(subset, m)
}
}
fees := struct {
WindowPoSt float64
PreCommit float64
ProveCommit float64
TotalBurn float64
MinerFee float64
MinerPenalty float64
}{}
for _, msg := range subset {
if m.Address == msg.To {
burn, _ := strconv.ParseFloat(msg.Fee.BaseFeeBurn, 64)
oeb, _ := strconv.ParseFloat(msg.Fee.OverEstimationBurn, 64)
mfee, _ := strconv.ParseFloat(msg.Fee.MinerTip, 64)
mpen, _ := strconv.ParseFloat(msg.Fee.MinerPenalty, 64)
// gas = m.Receipt.GasUsed
switch msg.MethodNumber {
case 5:
fees.WindowPoSt += burn + oeb
case 6:
fees.PreCommit += burn + oeb
case 7:
fees.ProveCommit += burn + oeb
}
fees.TotalBurn += burn + oeb
fees.MinerFee += mfee
fees.MinerPenalty += mpen
}
}
fee := Fees{
WindowPoSt: FilFloat(fees.WindowPoSt),
PreCommit: FilFloat(fees.PreCommit),
ProveCommit: FilFloat(fees.ProveCommit),
MinerFee: FilFloat(fees.MinerFee),
MinerPenalty: FilFloat(fees.MinerPenalty),
BurnFee: FilFloat(fees.TotalBurn),
}
return fee, nil
}
func (m *Miner) GetTransfers(start, end int64) ([]transfer, error) {
filename, err := m.getLatestJsonFile("transfers")
if err != nil {
return nil, err
}
acontent, err := ioutil.ReadFile(fmt.Sprintf("%s/transfers/%s.json", m.Address, filename))
if err != nil {
return nil, err
}
transf := []transfer{}
err = json.Unmarshal(acontent, &transf)
if err != nil {
return nil, err
}
sort.SliceStable(transf, func(i, j int) bool { return transf[i].Height < transf[j].Height })
subset := []transfer{}
for _, t := range transf {
if t.Timestamp >= start && t.Timestamp <= end {
subset = append(subset, t)
}
}
return subset, nil
}
func FilFloat(v float64) string {
return fmt.Sprintf("%.18f", v*0.000000000000000001)
}