2019-08-07 18:57:48 +00:00
|
|
|
package deals
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-08-12 21:48:18 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/lib/sectorbuilder"
|
2019-08-07 18:57:48 +00:00
|
|
|
|
|
|
|
files "github.com/ipfs/go-ipfs-files"
|
|
|
|
"github.com/ipfs/go-merkledag"
|
|
|
|
unixfile "github.com/ipfs/go-unixfs/file"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
2019-08-12 21:48:18 +00:00
|
|
|
type handlerFunc func(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error)
|
2019-08-07 18:57:48 +00:00
|
|
|
|
|
|
|
func (h *Handler) handle(ctx context.Context, deal MinerDeal, cb handlerFunc, next DealState) {
|
|
|
|
go func() {
|
2019-08-12 21:48:18 +00:00
|
|
|
mut, err := cb(ctx, deal)
|
2019-08-07 18:57:48 +00:00
|
|
|
select {
|
|
|
|
case h.updated <- dealUpdate{
|
|
|
|
newState: next,
|
|
|
|
id: deal.ProposalCid,
|
|
|
|
err: err,
|
2019-08-12 21:48:18 +00:00
|
|
|
mut: mut,
|
2019-08-07 18:57:48 +00:00
|
|
|
}:
|
|
|
|
case <-h.stop:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ACCEPTED
|
|
|
|
|
2019-08-12 21:48:18 +00:00
|
|
|
func (h *Handler) accept(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error) {
|
2019-08-07 18:57:48 +00:00
|
|
|
switch deal.Proposal.SerializationMode {
|
|
|
|
//case SerializationRaw:
|
|
|
|
//case SerializationIPLD:
|
|
|
|
case SerializationUnixFs:
|
|
|
|
default:
|
2019-08-12 21:48:18 +00:00
|
|
|
return nil, xerrors.Errorf("deal proposal with unsupported serialization: %s", deal.Proposal.SerializationMode)
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 20:16:26 +00:00
|
|
|
// TODO: check payment
|
|
|
|
|
2019-08-07 18:57:48 +00:00
|
|
|
log.Info("fetching data for a deal")
|
|
|
|
err := h.sendSignedResponse(StorageDealResponse{
|
|
|
|
State: Accepted,
|
|
|
|
Message: "",
|
|
|
|
Proposal: deal.ProposalCid,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-08-12 21:48:18 +00:00
|
|
|
return nil, err
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 21:48:18 +00:00
|
|
|
return nil, merkledag.FetchGraph(ctx, deal.Ref, h.dag)
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// STAGED
|
|
|
|
|
2019-08-12 21:48:18 +00:00
|
|
|
func (h *Handler) staged(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error) {
|
2019-08-07 18:57:48 +00:00
|
|
|
err := h.sendSignedResponse(StorageDealResponse{
|
|
|
|
State: Staged,
|
|
|
|
Proposal: deal.ProposalCid,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-08-07 19:48:53 +00:00
|
|
|
log.Warnf("Sending deal response failed: %s", err)
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
root, err := h.dag.Get(ctx, deal.Ref)
|
|
|
|
if err != nil {
|
2019-08-12 21:48:18 +00:00
|
|
|
return nil, xerrors.Errorf("failed to get file root for deal: %s", err)
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: abstract this away into ReadSizeCloser + implement different modes
|
|
|
|
n, err := unixfile.NewUnixfsFile(ctx, h.dag, root)
|
|
|
|
if err != nil {
|
2019-08-12 21:48:18 +00:00
|
|
|
return nil, xerrors.Errorf("cannot open unixfs file: %s", err)
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uf, ok := n.(files.File)
|
|
|
|
if !ok {
|
|
|
|
// we probably got directory, unsupported for now
|
2019-08-12 21:48:18 +00:00
|
|
|
return nil, xerrors.Errorf("unsupported unixfs type")
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size, err := uf.Size()
|
|
|
|
if err != nil {
|
2019-08-12 21:48:18 +00:00
|
|
|
return nil, xerrors.Errorf("failed to get file size: %s", err)
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sectorID uint64
|
|
|
|
err = withTemp(uf, func(f string) (err error) {
|
|
|
|
sectorID, err = h.sb.AddPiece(deal.Proposal.PieceRef, uint64(size), f)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-08-12 21:48:18 +00:00
|
|
|
return nil, xerrors.Errorf("AddPiece failed: %s", err)
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Warnf("New Sector: %d", sectorID)
|
2019-08-12 21:48:18 +00:00
|
|
|
return func(deal *MinerDeal) {
|
|
|
|
deal.SectorID = sectorID
|
|
|
|
}, nil
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SEALING
|
2019-08-12 21:48:18 +00:00
|
|
|
|
|
|
|
func getInclusionProof(ref string, status sectorbuilder.SectorSealingStatus) (PieceInclusionProof, error) {
|
|
|
|
for i, p := range status.Pieces {
|
|
|
|
if p.Key == ref {
|
|
|
|
return PieceInclusionProof{
|
|
|
|
Position: uint64(i),
|
|
|
|
ProofElements: p.InclusionProof,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PieceInclusionProof{}, xerrors.Errorf("pieceInclusionProof for %s in sector %d not found", ref, status.SectorID)
|
|
|
|
}
|
|
|
|
|
2019-08-12 23:09:08 +00:00
|
|
|
func (h *Handler) pollSectorSealed(deal MinerDeal) (status sectorbuilder.SectorSealingStatus, err error) {
|
2019-08-12 21:48:18 +00:00
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
status, err = h.sb.SealStatus(deal.SectorID)
|
|
|
|
if err != nil {
|
|
|
|
return sectorbuilder.SectorSealingStatus{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch status.SealStatusCode {
|
|
|
|
case 0: // sealed
|
|
|
|
break loop
|
|
|
|
case 2: // failed
|
|
|
|
return sectorbuilder.SectorSealingStatus{}, xerrors.Errorf("sealing sector %d for deal %s (ref=%s) failed: %s", deal.SectorID, deal.ProposalCid, deal.Ref, status.SealErrorMsg)
|
|
|
|
case 1: // pending
|
|
|
|
if err := h.sb.SealAllStagedSectors(); err != nil {
|
|
|
|
return sectorbuilder.SectorSealingStatus{}, err
|
|
|
|
}
|
|
|
|
// start seal
|
|
|
|
fallthrough
|
|
|
|
case 3: // sealing
|
|
|
|
// wait
|
|
|
|
default:
|
|
|
|
return sectorbuilder.SectorSealingStatus{}, xerrors.Errorf("unknown SealStatusCode: %d", status.SectorID)
|
|
|
|
}
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
}
|
|
|
|
return status, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) sealing(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error) {
|
|
|
|
status, err := h.pollSectorSealed(deal)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := getInclusionProof(deal.Ref.String(), status)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.sendSignedResponse(StorageDealResponse{
|
|
|
|
State: Sealing,
|
|
|
|
Proposal: deal.ProposalCid,
|
|
|
|
PieceInclusionProof: ip,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Sending deal response failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|