lotus/chain/deals/client_states.go

98 lines
2.1 KiB
Go
Raw Normal View History

2019-09-10 12:35:43 +00:00
package deals
import (
"context"
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/api"
"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
}
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) {
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!")
dealId, err := c.node.ValidatePublishedDeal(ctx, deal.ClientDeal)
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) {
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) {
cb := func(err error) {
select {
case c.updated <- clientDealUpdate{
newState: api.DealComplete,
id: deal.ProposalCid,
err: err,
}:
case <-c.stop:
}
}
err := c.node.OnDealSectorCommitted(ctx, deal.Proposal.Provider, deal.DealID, cb)
2019-09-10 12:35:43 +00:00
return nil, err
}
2019-09-10 12:35:43 +00:00
func (c *Client) checkAskSignature(ask *types.SignedStorageAsk) error {
return c.node.ValidateAskSignature(ask)
2019-09-10 12:35:43 +00:00
}