-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfil_deal_stats.go
135 lines (127 loc) · 3.72 KB
/
fil_deal_stats.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
package main
import (
"context"
"sync"
)
const (
// filEpochSeconds is the length of one FIL epoch in seconds.
// See: https://docs.filecoin.io/reference/general/glossary/#epoch
filEpochSeconds = 30
// filEpochDay is the number of FIL epochs in one day.
filEpochDay = 24 * 60 * 60 / filEpochSeconds
// filEpochWeek is the number of FIL epochs in one week.
filEpochWeek = 7 * filEpochDay
)
type (
dealStats struct {
hf *heyFil
refreshLock sync.RWMutex
dealCountByParticipant map[string]dealCounts
latestRefreshErr error
}
dealCounts struct {
count int64
bytes int64
countWithinDay int64
bytesWithinDay int64
countWithinWeek int64
bytesWithinWeek int64
}
)
func (ds *dealStats) start(ctx context.Context) {
go func() {
for {
ds.latestRefreshErr = ds.refresh(ctx)
select {
case <-ctx.Done():
return
case <-ds.hf.dealStatsRefreshInterval.C:
ds.latestRefreshErr = ds.refresh(ctx)
}
}
}()
}
func (ds *dealStats) refresh(ctx context.Context) error {
ch, err := ds.hf.chainHead(ctx)
if err != nil {
return err
}
currentEpoch := ch.Height
logger.Infow("fetched fil chain height", "height", currentEpoch)
var deals chan StateMarketDealResult
if ds.hf.marketDealsFilToolsEnabled {
deals, err = ds.hf.stateMarketDealsViaFilTools(ctx)
} else {
deals, err = ds.hf.stateMarketDealsViaS3Snapshot(ctx)
}
if err != nil {
return err
}
dealCountByParticipant := make(map[string]dealCounts)
latestPieceByParticipant := make(map[string]struct {
epoch int64
cid string
})
var totalDealCount, totalDealCountWithinWeek, totalDealCountWithinDay int64
for {
select {
case <-ctx.Done():
return ctx.Err()
case dr, ok := <-deals:
if !ok {
lp := make(recentPieces)
for k, v := range latestPieceByParticipant {
lp[k] = v.cid
}
ds.refreshLock.Lock()
ds.dealCountByParticipant = dealCountByParticipant
ds.refreshLock.Unlock()
ds.hf.storeRecentPieces(lp)
ds.hf.metrics.notifyDealCount(totalDealCount)
ds.hf.metrics.notifyDealCountWithinDay(totalDealCountWithinDay)
ds.hf.metrics.notifyDealCountWithinWeek(totalDealCountWithinWeek)
logger.Infow("fetched state market deals", "count", totalDealCount, "countWithinDay", totalDealCountWithinDay, "countWithinWeek", totalDealCountWithinWeek)
return nil
}
switch {
case dr.Err != nil:
return dr.Err
case dr.Deal.State.SectorStartEpoch == -1:
case dr.Deal.State.SlashEpoch != -1:
case dr.Deal.Proposal.EndEpoch < currentEpoch:
default:
totalDealCount++
provider := dr.Deal.Proposal.Provider
providerDealCount := dealCountByParticipant[provider]
providerDealCount.count++
providerDealCount.bytes += dr.Deal.Proposal.PieceSize
elapsedSinceStartEpoch := currentEpoch - dr.Deal.Proposal.StartEpoch
if elapsedSinceStartEpoch < filEpochDay {
totalDealCountWithinDay++
providerDealCount.countWithinDay++
providerDealCount.bytesWithinDay += dr.Deal.Proposal.PieceSize
}
if elapsedSinceStartEpoch < filEpochWeek {
totalDealCountWithinWeek++
providerDealCount.countWithinWeek++
providerDealCount.bytesWithinWeek += dr.Deal.Proposal.PieceSize
}
dealCountByParticipant[provider] = providerDealCount
}
if lp, ok := latestPieceByParticipant[dr.Deal.Proposal.Provider]; !ok || dr.Deal.Proposal.StartEpoch > lp.epoch {
latestPieceByParticipant[dr.Deal.Proposal.Provider] = struct {
epoch int64
cid string
}{
epoch: dr.Deal.Proposal.StartEpoch,
cid: dr.Deal.Proposal.PieceCID.CID,
}
}
}
}
}
func (ds *dealStats) getDealCounts(id string) dealCounts {
ds.refreshLock.RLock()
defer ds.refreshLock.RUnlock()
return ds.dealCountByParticipant[id]
}