/
trackdeals.go
271 lines (243 loc) · 6.36 KB
/
trackdeals.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
package main
import (
"context"
"fmt"
"strconv"
"time"
"github.com/dustin/go-humanize"
filaddr "github.com/filecoin-project/go-address"
filabi "github.com/filecoin-project/go-state-types/abi"
filprovider "github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/ipfs/go-cid"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
)
type filClient struct {
robust filaddr.Address
dataCapRemaining *filabi.StoragePower
}
type filDeal struct {
pieceCid cid.Cid
aggregateCid cid.Cid
status string
}
var trackDeals = &cli.Command{
Usage: "Track state of filecoin deals related to known PieceCIDs",
Name: "track-deals",
Flags: []cli.Flag{},
Action: func(cctx *cli.Context) error {
ctx, closer := context.WithCancel(cctx.Context)
defer closer()
clientLookup := make(map[filaddr.Address]filClient, 32)
knownDeals := make(map[int64]filDeal)
aggCidLookup := make(map[cid.Cid]cid.Cid)
rows, err := cargoDb.Query(
ctx,
`
SELECT a.aggregate_cid, a.piece_cid, d.deal_id, d.status
FROM cargo.aggregates a
LEFT JOIN cargo.deals d USING ( aggregate_cid )
`,
)
if err != nil {
return err
}
for rows.Next() {
var aCidStr string
var pCidStr string
var dealID *int64
var dealStatus *string
if err = rows.Scan(&aCidStr, &pCidStr, &dealID, &dealStatus); err != nil {
return err
}
aCid, err := cid.Parse(aCidStr)
if err != nil {
return err
}
pCid, err := cid.Parse(pCidStr)
if err != nil {
return err
}
if dealID != nil {
knownDeals[*dealID] = filDeal{
pieceCid: pCid,
aggregateCid: aCid,
status: *dealStatus,
}
}
aggCidLookup[pCid] = aCid
}
if err := rows.Err(); err != nil {
return err
}
var newDealCount, terminatedDealCount int
dealTotals := make(map[string]int64)
defer func() {
log.Infow("summary",
"knownPieces", len(aggCidLookup),
"relatedDeals", dealTotals,
"newlyAdded", newDealCount,
"newlyTerminated", terminatedDealCount,
)
}()
if len(aggCidLookup) == 0 {
return nil
}
log.Infof("checking the status of %s known Piece CIDs", humanize.Comma(int64(len(aggCidLookup))))
api, apiClose, err := lotusAPI(cctx)
if err != nil {
return xerrors.Errorf("connecting to lotus failed: %w", err)
}
defer apiClose()
lts, err := lotusLookbackTipset(cctx, api)
if err != nil {
return err
}
log.Infow("retrieving Market Deals from", "state", lts.Key(), "epoch", lts.Height(), "wallTime", time.Unix(int64(lts.Blocks()[0].Timestamp), 0))
deals, err := api.StateMarketDeals(ctx, lts.Key())
if err != nil {
return err
}
log.Infof("retrieved %s state deal records", humanize.Comma(int64(len(deals))))
for dealIDString, d := range deals {
aggCid, known := aggCidLookup[d.Proposal.PieceCID]
if !known {
continue
}
dealID, err := strconv.ParseInt(dealIDString, 10, 64)
if err != nil {
return err
}
var initialEncounter bool
if _, known := knownDeals[dealID]; !known {
initialEncounter = true
} else {
// at the end whatever remains is not in SMA list, thus will be marked "terminated"
delete(knownDeals, dealID)
}
_, err = cargoDb.Exec(
ctx,
`
INSERT INTO cargo.providers ( provider ) VALUES ( $1 )
ON CONFLICT ( provider ) DO NOTHING
`,
d.Proposal.Provider.String(),
)
if err != nil {
return err
}
if _, found := clientLookup[d.Proposal.Client]; !found {
var fc filClient
fc.robust, err = api.StateAccountKey(ctx, d.Proposal.Client, lts.Key())
if err != nil {
return err
}
fc.dataCapRemaining, err = api.StateVerifiedClientStatus(ctx, fc.robust, lts.Key())
if err != nil {
return err
}
if fc.dataCapRemaining == nil {
z := filabi.NewStoragePower(0)
fc.dataCapRemaining = &z
}
_, err = cargoDb.Exec(
ctx,
`
INSERT INTO cargo.clients ( client, filp_available ) VALUES ( $1, $2 )
ON CONFLICT ( client ) DO UPDATE SET
filp_available = EXCLUDED.filp_available
`,
fc.robust.String(),
fc.dataCapRemaining.Int64(),
)
if err != nil {
return err
}
clientLookup[d.Proposal.Client] = fc
}
var statusMeta *string
var sectorStart *filabi.ChainEpoch
status := "published"
if d.State.SectorStartEpoch > 0 {
sectorStart = &d.State.SectorStartEpoch
status = "active"
m := fmt.Sprintf(
"containing sector active as of %s at epoch %d",
mainnetTime(d.State.SectorStartEpoch).Format("2006-01-02 15:04:05"),
d.State.SectorStartEpoch,
)
statusMeta = &m
} else if d.Proposal.StartEpoch+filprovider.WPoStChallengeWindow < lts.Height() {
// if things are lookback+one deadlines late: they are never going to make it
status = "terminated"
m := fmt.Sprintf(
"containing sector missed expected sealing epoch %d",
d.Proposal.StartEpoch,
)
statusMeta = &m
}
dealTotals[status]++
if initialEncounter {
if status == "terminated" {
terminatedDealCount++
} else {
newDealCount++
}
}
_, err = cargoDb.Exec(
ctx,
`
INSERT INTO cargo.deals ( aggregate_cid, client, provider, deal_id, start_epoch, end_epoch, status, status_meta, sector_start_epoch )
VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9 )
ON CONFLICT ( deal_id ) DO UPDATE SET
status = EXCLUDED.status,
status_meta = EXCLUDED.status_meta,
sector_start_epoch = COALESCE( EXCLUDED.sector_start_epoch, cargo.deals.sector_start_epoch )
`,
aggCid.String(),
clientLookup[d.Proposal.Client].robust.String(),
d.Proposal.Provider.String(),
dealID,
d.Proposal.StartEpoch,
d.Proposal.EndEpoch,
status,
statusMeta,
sectorStart,
)
if err != nil {
return err
}
}
// we may have some terminations ( no longer in the market state )
toFail := make([]int64, 0, len(knownDeals))
for dID, d := range knownDeals {
dealTotals["terminated"]++
if d.status == "terminated" {
continue
}
terminatedDealCount++
toFail = append(toFail, dID)
}
if len(toFail) > 0 {
_, err = cargoDb.Exec(
ctx,
`
UPDATE cargo.deals SET
status = $1,
status_meta = $2
WHERE
deal_id = ANY ( $3::BIGINT[] )
AND
status != 'terminated'
`,
`terminated`,
`deal no longer part of market-actor state`,
toFail,
)
if err != nil {
return err
}
}
return nil
},
}