-
Notifications
You must be signed in to change notification settings - Fork 58
/
client_states.go
362 lines (290 loc) · 13.8 KB
/
client_states.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
354
355
356
357
358
359
360
361
362
package clientstates
import (
"context"
"time"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
"github.com/ipld/go-ipld-prime/datamodel"
selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse"
"github.com/libp2p/go-libp2p/core/peer"
"golang.org/x/xerrors"
datatransfer "github.com/filecoin-project/go-data-transfer/v2"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/go-statemachine/fsm"
"github.com/filecoin-project/go-fil-markets/storagemarket"
"github.com/filecoin-project/go-fil-markets/storagemarket/impl/requestvalidation"
"github.com/filecoin-project/go-fil-markets/storagemarket/network"
)
var log = logging.Logger("storagemarket_impl")
// MaxGraceEpochsForDealAcceptance is the maximum number of epochs we will wait for past the start epoch
// for the provider to publish a deal.
const MaxGraceEpochsForDealAcceptance = 10
// ClientDealEnvironment is an abstraction for interacting with
// dependencies from the storage client environment
type ClientDealEnvironment interface {
// CleanBlockstore cleans up the read-only CARv2 blockstore that provides random access on top of client deal data.
// It's important to do this when the client deal finishes successfully or errors out.
CleanBlockstore(rootCid cid.Cid) error
Node() storagemarket.StorageClientNode
NewDealStream(ctx context.Context, p peer.ID) (network.StorageDealStream, error)
StartDataTransfer(ctx context.Context, to peer.ID, voucher datatransfer.TypedVoucher, baseCid cid.Cid, selector datamodel.Node) (datatransfer.ChannelID, error)
RestartDataTransfer(ctx context.Context, chid datatransfer.ChannelID) error
GetProviderDealState(ctx context.Context, proposalCid cid.Cid) (*storagemarket.ProviderDealState, error)
PollingInterval() time.Duration
network.PeerTagger
}
// ClientStateEntryFunc is the type for all state entry functions on a storage client
type ClientStateEntryFunc func(ctx fsm.Context, environment ClientDealEnvironment, deal storagemarket.ClientDeal) error
// ReserveClientFunds attempts to reserve funds for this deal and ensure they are available in the Storage Market Actor
func ReserveClientFunds(ctx fsm.Context, environment ClientDealEnvironment, deal storagemarket.ClientDeal) error {
node := environment.Node()
mcid, err := node.ReserveFunds(ctx.Context(), deal.Proposal.Client, deal.Proposal.Client, deal.Proposal.ClientBalanceRequirement())
if err != nil {
return ctx.Trigger(storagemarket.ClientEventReserveFundsFailed, err)
}
_ = ctx.Trigger(storagemarket.ClientEventFundsReserved, deal.Proposal.ClientBalanceRequirement())
// if no message was sent, and there was no error, funds were already available
if mcid == cid.Undef {
return ctx.Trigger(storagemarket.ClientEventFundingComplete)
}
// Otherwise wait for funds to be added
return ctx.Trigger(storagemarket.ClientEventFundingInitiated, mcid)
}
// WaitForFunding waits for an AddFunds message to appear on the chain
func WaitForFunding(ctx fsm.Context, environment ClientDealEnvironment, deal storagemarket.ClientDeal) error {
node := environment.Node()
return node.WaitForMessage(ctx.Context(), *deal.AddFundsCid, func(code exitcode.ExitCode, bytes []byte, finalCid cid.Cid, err error) error {
if err != nil {
return ctx.Trigger(storagemarket.ClientEventReserveFundsFailed, xerrors.Errorf("AddFunds err: %w", err))
}
if code != exitcode.Ok {
return ctx.Trigger(storagemarket.ClientEventReserveFundsFailed, xerrors.Errorf("AddFunds exit code: %s", code.String()))
}
return ctx.Trigger(storagemarket.ClientEventFundingComplete)
})
}
// ProposeDeal sends the deal proposal to the provider
func ProposeDeal(ctx fsm.Context, environment ClientDealEnvironment, deal storagemarket.ClientDeal) error {
proposal := network.Proposal{
DealProposal: &deal.ClientDealProposal,
Piece: deal.DataRef,
FastRetrieval: deal.FastRetrieval,
}
s, err := environment.NewDealStream(ctx.Context(), deal.Miner)
if err != nil {
return ctx.Trigger(storagemarket.ClientEventWriteProposalFailed, err)
}
environment.TagPeer(deal.Miner, deal.ProposalCid.String())
if err := s.WriteDealProposal(proposal); err != nil {
return ctx.Trigger(storagemarket.ClientEventWriteProposalFailed, err)
}
resp, origBytes, err := s.ReadDealResponse()
if err != nil {
return ctx.Trigger(storagemarket.ClientEventReadResponseFailed, err)
}
err = s.Close()
if err != nil {
// doesn't really matter but log the error
log.Debugw("failed to close deal stream", "error", err)
}
tok, _, err := environment.Node().GetChainHead(ctx.Context())
if err != nil {
return ctx.Trigger(storagemarket.ClientEventResponseVerificationFailed)
}
verified, err := environment.Node().VerifySignature(ctx.Context(), *resp.Signature, deal.MinerWorker, origBytes, tok)
if err != nil {
return ctx.Trigger(storagemarket.ClientEventResponseVerificationFailed)
}
if !verified {
return ctx.Trigger(storagemarket.ClientEventResponseVerificationFailed)
}
if resp.Response.State != storagemarket.StorageDealWaitingForData {
return ctx.Trigger(storagemarket.ClientEventUnexpectedDealState, resp.Response.State, resp.Response.Message)
}
return ctx.Trigger(storagemarket.ClientEventInitiateDataTransfer)
}
// RestartDataTransfer restarts a data transfer to the provider that was initiated earlier
func RestartDataTransfer(ctx fsm.Context, environment ClientDealEnvironment, deal storagemarket.ClientDeal) error {
log.Infof("restarting data transfer for deal %s", deal.ProposalCid)
if deal.TransferChannelID == nil {
return ctx.Trigger(storagemarket.ClientEventDataTransferRestartFailed, xerrors.New("channelId on client deal is nil"))
}
// restart the push data transfer. This will complete asynchronously and the
// completion of the data transfer will trigger a change in deal state
err := environment.RestartDataTransfer(ctx.Context(),
*deal.TransferChannelID,
)
if err != nil {
return ctx.Trigger(storagemarket.ClientEventDataTransferRestartFailed, err)
}
return nil
}
// InitiateDataTransfer initiates data transfer to the provider
func InitiateDataTransfer(ctx fsm.Context, environment ClientDealEnvironment, deal storagemarket.ClientDeal) error {
if deal.DataRef.TransferType == storagemarket.TTManual {
log.Infof("manual data transfer for deal %s", deal.ProposalCid)
return ctx.Trigger(storagemarket.ClientEventDataTransferComplete)
}
log.Infof("sending data for a deal %s", deal.ProposalCid)
voucher := requestvalidation.StorageDataTransferVoucher{Proposal: deal.ProposalCid}
node := requestvalidation.BindnodeRegistry.TypeToNode(&voucher)
// initiate a push data transfer. This will complete asynchronously and the
// completion of the data transfer will trigger a change in deal state
_, err := environment.StartDataTransfer(ctx.Context(),
deal.Miner,
datatransfer.TypedVoucher{Voucher: node, Type: requestvalidation.StorageDataTransferVoucherType},
deal.DataRef.Root,
selectorparse.CommonSelector_ExploreAllRecursively,
)
if err != nil {
return ctx.Trigger(storagemarket.ClientEventDataTransferFailed, xerrors.Errorf("failed to open push data channel: %w", err))
}
return nil
}
// CheckForDealAcceptance is run until the deal is sealed and published by the provider, or errors
func CheckForDealAcceptance(ctx fsm.Context, environment ClientDealEnvironment, deal storagemarket.ClientDeal) error {
_, currEpoch, err := environment.Node().GetChainHead(ctx.Context())
if err == nil {
if currEpoch > deal.Proposal.StartEpoch+MaxGraceEpochsForDealAcceptance {
return ctx.Trigger(storagemarket.ClientEventDealRejected, deal.State, "start epoch already elapsed")
}
}
dealState, err := environment.GetProviderDealState(ctx.Context(), deal.ProposalCid)
if err != nil {
log.Warnf("error when querying provider deal state: %w", err) // TODO: at what point do we fail the deal?
return waitAgain(ctx, environment, true, storagemarket.StorageDealUnknown)
}
if isFailed(dealState.State) {
return ctx.Trigger(storagemarket.ClientEventDealRejected, dealState.State, dealState.Message)
}
if isAccepted(dealState.State) {
if *dealState.ProposalCid != deal.ProposalCid {
return ctx.Trigger(storagemarket.ClientEventResponseDealDidNotMatch, *dealState.ProposalCid, deal.ProposalCid)
}
return ctx.Trigger(storagemarket.ClientEventDealAccepted, dealState.PublishCid)
}
return waitAgain(ctx, environment, false, dealState.State)
}
func waitAgain(ctx fsm.Context, environment ClientDealEnvironment, pollError bool, providerState storagemarket.StorageDealStatus) error {
t := time.NewTimer(environment.PollingInterval())
go func() {
select {
case <-t.C:
_ = ctx.Trigger(storagemarket.ClientEventWaitForDealState, pollError, providerState)
case <-ctx.Context().Done():
t.Stop()
return
}
}()
return nil
}
// ValidateDealPublished confirms with the chain that a deal was published
func ValidateDealPublished(ctx fsm.Context, environment ClientDealEnvironment, deal storagemarket.ClientDeal) error {
dealID, err := environment.Node().ValidatePublishedDeal(ctx.Context(), deal)
if err != nil {
return ctx.Trigger(storagemarket.ClientEventDealPublishFailed, err)
}
releaseReservedFunds(ctx, environment, deal)
// at this point data transfer is complete, so unprotect peer connection
environment.UntagPeer(deal.Miner, deal.ProposalCid.String())
return ctx.Trigger(storagemarket.ClientEventDealPublished, dealID)
}
// VerifyDealPreCommitted verifies that a deal has been pre-committed
func VerifyDealPreCommitted(ctx fsm.Context, environment ClientDealEnvironment, deal storagemarket.ClientDeal) error {
cb := func(sectorNumber abi.SectorNumber, isActive bool, err error) {
// It's possible that
// - we miss the pre-commit message and have to wait for prove-commit
// - the deal is already active (for example if the node is restarted
// while waiting for pre-commit)
// In either of these two cases, isActive will be true.
switch {
case err != nil:
_ = ctx.Trigger(storagemarket.ClientEventDealPrecommitFailed, err)
case isActive:
_ = ctx.Trigger(storagemarket.ClientEventDealActivated)
default:
_ = ctx.Trigger(storagemarket.ClientEventDealPrecommitted, sectorNumber)
}
}
err := environment.Node().OnDealSectorPreCommitted(ctx.Context(), deal.Proposal.Provider, deal.DealID, deal.Proposal, deal.PublishMessage, cb)
if err != nil {
return ctx.Trigger(storagemarket.ClientEventDealPrecommitFailed, err)
}
return nil
}
// VerifyDealActivated confirms that a deal was successfully committed to a sector and is active
func VerifyDealActivated(ctx fsm.Context, environment ClientDealEnvironment, deal storagemarket.ClientDeal) error {
cb := func(err error) {
if err != nil {
_ = ctx.Trigger(storagemarket.ClientEventDealActivationFailed, err)
} else {
_ = ctx.Trigger(storagemarket.ClientEventDealActivated)
}
}
if err := environment.Node().OnDealSectorCommitted(ctx.Context(), deal.Proposal.Provider, deal.DealID, deal.SectorNumber, deal.Proposal, deal.PublishMessage, cb); err != nil {
return ctx.Trigger(storagemarket.ClientEventDealActivationFailed, err)
}
return nil
}
// WaitForDealCompletion waits for the deal to be slashed or to expire
func WaitForDealCompletion(ctx fsm.Context, environment ClientDealEnvironment, deal storagemarket.ClientDeal) error {
node := environment.Node()
// deal is now active, clean up the blockstore.
if err := environment.CleanBlockstore(deal.DataRef.Root); err != nil {
log.Errorf("storage deal active but failed to cleanup rea-only blockstore, proposalCid=%s, err=%s", deal.ProposalCid, err)
}
// Called when the deal expires
expiredCb := func(err error) {
if err != nil {
_ = ctx.Trigger(storagemarket.ClientEventDealCompletionFailed, xerrors.Errorf("deal expiration err: %w", err))
} else {
_ = ctx.Trigger(storagemarket.ClientEventDealExpired)
}
}
// Called when the deal is slashed
slashedCb := func(slashEpoch abi.ChainEpoch, err error) {
if err != nil {
_ = ctx.Trigger(storagemarket.ClientEventDealCompletionFailed, xerrors.Errorf("deal slashing err: %w", err))
} else {
_ = ctx.Trigger(storagemarket.ClientEventDealSlashed, slashEpoch)
}
}
if err := node.OnDealExpiredOrSlashed(ctx.Context(), deal.DealID, expiredCb, slashedCb); err != nil {
return ctx.Trigger(storagemarket.ClientEventDealCompletionFailed, err)
}
return nil
}
// FailDeal cleans up a failing deal
func FailDeal(ctx fsm.Context, environment ClientDealEnvironment, deal storagemarket.ClientDeal) error {
releaseReservedFunds(ctx, environment, deal)
// TODO: store in some sort of audit log
log.Errorf("deal %s failed: %s", deal.ProposalCid, deal.Message)
environment.UntagPeer(deal.Miner, deal.ProposalCid.String())
if err := environment.CleanBlockstore(deal.DataRef.Root); err != nil {
log.Errorf("failed to cleanup read-only blockstore, proposalCid=%s: %s", deal.ProposalCid, err)
}
return ctx.Trigger(storagemarket.ClientEventFailed)
}
func releaseReservedFunds(ctx fsm.Context, environment ClientDealEnvironment, deal storagemarket.ClientDeal) {
if !deal.FundsReserved.Nil() && !deal.FundsReserved.IsZero() {
err := environment.Node().ReleaseFunds(ctx.Context(), deal.Proposal.Client, deal.FundsReserved)
if err != nil {
// nonfatal error
log.Warnf("failed to release funds: %s", err)
}
_ = ctx.Trigger(storagemarket.ClientEventFundsReleased, deal.FundsReserved)
}
}
func isAccepted(status storagemarket.StorageDealStatus) bool {
return status == storagemarket.StorageDealStaged ||
status == storagemarket.StorageDealAwaitingPreCommit ||
status == storagemarket.StorageDealSealing ||
status == storagemarket.StorageDealActive ||
status == storagemarket.StorageDealExpired ||
status == storagemarket.StorageDealSlashed
}
func isFailed(status storagemarket.StorageDealStatus) bool {
return status == storagemarket.StorageDealFailing ||
status == storagemarket.StorageDealError
}