-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
types.go
353 lines (274 loc) · 7.92 KB
/
types.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
350
351
352
353
package sealing
import (
"context"
"encoding/json"
"io"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/actors/builtin/verifreg"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/storage/pipeline/piece"
"github.com/filecoin-project/lotus/storage/sealer"
"github.com/filecoin-project/lotus/storage/sealer/storiface"
)
//go:generate go run github.com/golang/mock/mockgen -destination=mocks/statemachine.go -package=mocks . Context
// Context is a go-statemachine context
type Context interface {
Context() context.Context
Send(evt interface{}) error
}
type Log struct {
Timestamp uint64
Trace string // for errors
Message string
// additional data (Event info)
Kind string
}
type ReturnState string
const (
RetPreCommit1 = ReturnState(PreCommit1)
RetPreCommitting = ReturnState(PreCommitting)
RetPreCommitFailed = ReturnState(PreCommitFailed)
RetCommitFailed = ReturnState(CommitFailed)
)
type UniversalPieceInfo interface {
Impl() piece.PieceDealInfo
String() string
Key() piece.PieceKey
Valid(nv network.Version) error
StartEpoch() (abi.ChainEpoch, error)
EndEpoch() (abi.ChainEpoch, error)
PieceCID() cid.Cid
KeepUnsealedRequested() bool
GetAllocation(ctx context.Context, aapi piece.AllocationAPI, tsk types.TipSetKey) (*verifreg.Allocation, error)
}
type SectorInfo struct {
State SectorState
SectorNumber abi.SectorNumber
SectorType abi.RegisteredSealProof
// Packing
CreationTime int64 // unix seconds
Pieces []SafeSectorPiece
// PreCommit1
TicketValue abi.SealRandomness
TicketEpoch abi.ChainEpoch
PreCommit1Out storiface.PreCommit1Out
PreCommit1Fails uint64
// PreCommit2
CommD *cid.Cid
CommR *cid.Cid // SectorKey
Proof []byte
PreCommitDeposit big.Int
PreCommitMessage *cid.Cid
PreCommitTipSet types.TipSetKey
PreCommit2Fails uint64
// WaitSeed
SeedValue abi.InteractiveSealRandomness
SeedEpoch abi.ChainEpoch
// Committing
CommitMessage *cid.Cid
InvalidProofs uint64 // failed proof computations (doesn't validate with proof inputs; can't compute)
// CCUpdate
CCUpdate bool
CCPieces []SafeSectorPiece
UpdateSealed *cid.Cid
UpdateUnsealed *cid.Cid
ReplicaUpdateProof storiface.ReplicaUpdateProof
ReplicaUpdateMessage *cid.Cid
// Faults
FaultReportMsg *cid.Cid
// Recovery / Import
Return ReturnState
// Termination
TerminateMessage *cid.Cid
TerminatedAt abi.ChainEpoch
// Remote import
RemoteDataUnsealed *storiface.SectorLocation
RemoteDataSealed *storiface.SectorLocation
RemoteDataCache *storiface.SectorLocation
RemoteCommit1Endpoint string
RemoteCommit2Endpoint string
RemoteSealingDoneEndpoint string
RemoteDataFinalized bool
// Debug
LastErr string
Log []Log
}
func (t *SectorInfo) pieceInfos() []abi.PieceInfo {
out := make([]abi.PieceInfo, len(t.Pieces))
for i, p := range t.Pieces {
out[i] = p.Piece()
}
return out
}
func (t *SectorInfo) nonPaddingPieceInfos() []abi.PieceInfo {
out := make([]abi.PieceInfo, len(t.Pieces))
for i, p := range t.Pieces {
if !p.HasDealInfo() {
continue
}
out[i] = p.Piece()
}
return out
}
func (t *SectorInfo) existingPieceSizes() []abi.UnpaddedPieceSize {
out := make([]abi.UnpaddedPieceSize, len(t.Pieces))
for i, p := range t.Pieces {
out[i] = p.Piece().Size.Unpadded()
}
return out
}
func (t *SectorInfo) hasData() bool {
for _, piece := range t.Pieces {
if piece.HasDealInfo() {
return true
}
}
return false
}
func (t *SectorInfo) sealingCtx(ctx context.Context) context.Context {
// TODO: can also take start epoch into account to give priority to sectors
// we need sealed sooner
if t.hasData() {
return sealer.WithPriority(ctx, DealSectorPriority)
}
return ctx
}
// Returns list of offset/length tuples of sector data ranges which clients
// requested to keep unsealed
func (t *SectorInfo) keepUnsealedRanges(pieces []SafeSectorPiece, invert, alwaysKeep bool) []storiface.Range {
var out []storiface.Range
var at abi.UnpaddedPieceSize
for _, piece := range pieces {
psize := piece.Piece().Size.Unpadded()
at += psize
if !piece.HasDealInfo() {
continue
}
keep := piece.DealInfo().KeepUnsealedRequested() || alwaysKeep
if keep == invert {
continue
}
out = append(out, storiface.Range{
Offset: at - psize,
Size: psize,
})
}
return out
}
// SealingStateEvt is a journal event that records a sector state transition.
type SealingStateEvt struct {
SectorNumber abi.SectorNumber
SectorType abi.RegisteredSealProof
From SectorState
After SectorState
Error string
}
// SafeSectorPiece is a wrapper around SectorPiece which makes it hard to misuse
// especially by making it hard to access raw Deal / DDO info
type SafeSectorPiece struct {
real api.SectorPiece
}
func SafePiece(piece api.SectorPiece) SafeSectorPiece {
return SafeSectorPiece{piece}
}
var _ UniversalPieceInfo = &SafeSectorPiece{}
func (sp *SafeSectorPiece) Piece() abi.PieceInfo {
return sp.real.Piece
}
func (sp *SafeSectorPiece) HasDealInfo() bool {
return sp.real.DealInfo != nil
}
func (sp *SafeSectorPiece) DealInfo() UniversalPieceInfo {
return sp.real.DealInfo
}
// cbor passthrough
func (sp *SafeSectorPiece) UnmarshalCBOR(r io.Reader) (err error) {
return sp.real.UnmarshalCBOR(r)
}
func (sp *SafeSectorPiece) MarshalCBOR(w io.Writer) error {
return sp.real.MarshalCBOR(w)
}
// json passthrough
func (sp *SafeSectorPiece) UnmarshalJSON(b []byte) error {
return json.Unmarshal(b, &sp.real)
}
func (sp *SafeSectorPiece) MarshalJSON() ([]byte, error) {
return json.Marshal(sp.real)
}
type handleDealInfoParams struct {
FillerHandler func(UniversalPieceInfo) error
BuiltinMarketHandler func(UniversalPieceInfo) error
DDOHandler func(UniversalPieceInfo) error
}
func (sp *SafeSectorPiece) handleDealInfo(params handleDealInfoParams) error {
if !sp.HasDealInfo() {
if params.FillerHandler == nil {
return xerrors.Errorf("FillerHandler is not provided")
}
return params.FillerHandler(sp)
}
if sp.real.DealInfo.PublishCid != nil {
if params.BuiltinMarketHandler == nil {
return xerrors.Errorf("BuiltinMarketHandler is not provided")
}
return params.BuiltinMarketHandler(sp)
}
if params.DDOHandler == nil {
return xerrors.Errorf("DDOHandler is not provided")
}
return params.DDOHandler(sp)
}
// SectorPiece Proxy
func (sp *SafeSectorPiece) Impl() piece.PieceDealInfo {
if !sp.HasDealInfo() {
return piece.PieceDealInfo{}
}
return sp.real.DealInfo.Impl()
}
func (sp *SafeSectorPiece) String() string {
if !sp.HasDealInfo() {
return "<no deal info>"
}
return sp.real.DealInfo.String()
}
func (sp *SafeSectorPiece) Key() piece.PieceKey {
return sp.real.DealInfo.Key()
}
func (sp *SafeSectorPiece) Valid(nv network.Version) error {
return sp.real.DealInfo.Valid(nv)
}
func (sp *SafeSectorPiece) StartEpoch() (abi.ChainEpoch, error) {
if !sp.HasDealInfo() {
return 0, xerrors.Errorf("no deal info")
}
return sp.real.DealInfo.StartEpoch()
}
func (sp *SafeSectorPiece) EndEpoch() (abi.ChainEpoch, error) {
if !sp.HasDealInfo() {
return 0, xerrors.Errorf("no deal info")
}
return sp.real.DealInfo.EndEpoch()
}
func (sp *SafeSectorPiece) PieceCID() cid.Cid {
if !sp.HasDealInfo() {
return sp.real.Piece.PieceCID
}
return sp.real.DealInfo.PieceCID()
}
func (sp *SafeSectorPiece) KeepUnsealedRequested() bool {
if !sp.HasDealInfo() {
return false
}
return sp.real.DealInfo.KeepUnsealedRequested()
}
func (sp *SafeSectorPiece) GetAllocation(ctx context.Context, aapi piece.AllocationAPI, tsk types.TipSetKey) (*verifreg.Allocation, error) {
if !sp.HasDealInfo() {
return nil, xerrors.Errorf("no deal info")
}
return sp.real.DealInfo.GetAllocation(ctx, aapi, tsk)
}