2019-09-10 12:35:43 +00:00
|
|
|
package deals
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-07 22:55:24 +00:00
|
|
|
|
2019-10-16 07:07:16 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2019-11-07 07:57:10 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-09-10 12:35:43 +00:00
|
|
|
)
|
|
|
|
|
2019-11-07 13:29:43 +00:00
|
|
|
type clientHandlerFunc func(ctx context.Context, deal ClientDeal) (func(*ClientDeal), error)
|
2019-09-10 12:35:43 +00:00
|
|
|
|
2019-09-10 14:13:24 +00:00
|
|
|
func (c *Client) handle(ctx context.Context, deal ClientDeal, cb clientHandlerFunc, next api.DealState) {
|
2019-09-10 12:35:43 +00:00
|
|
|
go func() {
|
2019-11-07 13:29:43 +00:00
|
|
|
mut, err := cb(ctx, deal)
|
2019-09-13 19:43:33 +00:00
|
|
|
if err != nil {
|
|
|
|
next = api.DealError
|
|
|
|
}
|
2019-11-07 09:06:06 +00:00
|
|
|
|
|
|
|
if err == nil && next == api.DealNoUpdate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-10 12:35:43 +00:00
|
|
|
select {
|
|
|
|
case c.updated <- clientDealUpdate{
|
|
|
|
newState: next,
|
|
|
|
id: deal.ProposalCid,
|
|
|
|
err: err,
|
2019-11-07 13:29:43 +00:00
|
|
|
mut: mut,
|
2019-09-10 12:35:43 +00:00
|
|
|
}:
|
|
|
|
case <-c.stop:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-11-07 13:29:43 +00:00
|
|
|
func (c *Client) new(ctx context.Context, deal ClientDeal) (func(*ClientDeal), error) {
|
2019-09-10 12:35:43 +00:00
|
|
|
resp, err := c.readStorageDealResp(deal)
|
|
|
|
if err != nil {
|
2019-11-07 13:29:43 +00:00
|
|
|
return nil, err
|
2019-09-10 12:35:43 +00:00
|
|
|
}
|
2019-12-07 14:12:10 +00:00
|
|
|
|
|
|
|
// TODO: verify StorageDealSubmission
|
|
|
|
|
2019-11-07 12:57:00 +00:00
|
|
|
if err := c.disconnect(deal); err != nil {
|
2019-11-07 13:29:43 +00:00
|
|
|
return nil, err
|
2019-11-07 12:57:00 +00:00
|
|
|
}
|
2019-09-10 12:35:43 +00:00
|
|
|
|
2019-11-07 13:29:43 +00:00
|
|
|
/* data transfer happens */
|
2019-09-10 14:13:24 +00:00
|
|
|
if resp.State != api.DealAccepted {
|
2019-11-07 13:29:43 +00:00
|
|
|
return nil, xerrors.Errorf("deal wasn't accepted (State=%d)", resp.State)
|
2019-09-10 12:35:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 13:29:43 +00:00
|
|
|
return func(info *ClientDeal) {
|
2019-11-04 19:57:54 +00:00
|
|
|
info.PublishMessage = resp.PublishMessage
|
2019-11-07 13:29:43 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) accepted(ctx context.Context, deal ClientDeal) (func(*ClientDeal), error) {
|
|
|
|
log.Infow("DEAL ACCEPTED!")
|
|
|
|
|
2019-11-04 19:57:54 +00:00
|
|
|
dealId, err := c.node.ValidatePublishedDeal(ctx, deal.ClientDeal)
|
2019-10-23 17:39:14 +00:00
|
|
|
if err != nil {
|
2019-11-07 13:29:43 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-10 12:35:43 +00:00
|
|
|
|
2019-11-07 13:29:43 +00:00
|
|
|
return func(info *ClientDeal) {
|
2019-11-04 19:57:54 +00:00
|
|
|
info.DealID = dealId
|
2019-11-07 13:29:43 +00:00
|
|
|
}, nil
|
2019-09-10 12:35:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 13:29:43 +00:00
|
|
|
func (c *Client) staged(ctx context.Context, deal ClientDeal) (func(*ClientDeal), error) {
|
|
|
|
// TODO: Maybe wait for pre-commit
|
2019-09-10 12:35:43 +00:00
|
|
|
|
2019-11-07 13:29:43 +00:00
|
|
|
return nil, nil
|
2019-09-10 12:35:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 13:29:43 +00:00
|
|
|
func (c *Client) sealing(ctx context.Context, deal ClientDeal) (func(*ClientDeal), error) {
|
2019-11-04 19:57:54 +00:00
|
|
|
cb := func(err error) {
|
2019-11-07 09:06:06 +00:00
|
|
|
select {
|
|
|
|
case c.updated <- clientDealUpdate{
|
|
|
|
newState: api.DealComplete,
|
|
|
|
id: deal.ProposalCid,
|
2019-11-04 19:57:54 +00:00
|
|
|
err: err,
|
2019-11-07 09:06:06 +00:00
|
|
|
}:
|
|
|
|
case <-c.stop:
|
|
|
|
}
|
2019-11-07 07:57:10 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 19:57:54 +00:00
|
|
|
err := c.node.OnDealSectorCommitted(ctx, deal.Proposal.Provider, deal.DealID, cb)
|
2019-09-10 12:35:43 +00:00
|
|
|
|
2019-11-04 19:57:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-10 12:35:43 +00:00
|
|
|
|
2019-11-04 19:57:54 +00:00
|
|
|
func (c *Client) checkAskSignature(ask *types.SignedStorageAsk) error {
|
|
|
|
return c.node.ValidateAskSignature(ask)
|
2019-09-10 12:35:43 +00:00
|
|
|
}
|