Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: assign direct deal #515

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions storageprovider/deal_assign_util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storageprovider

import (
"context"
"fmt"
"math/bits"

Expand Down Expand Up @@ -231,7 +232,7 @@ func fillersFromRem(in abi.PaddedPieceSize) ([]abi.PaddedPieceSize, error) {
return out, nil
}

func pickAndAlignDirectDeal(deals []*mtypes.DirectDealInfo, ssize abi.SectorSize, currentHeight abi.ChainEpoch, spec *mtypes.GetDealSpec) ([]*mtypes.DirectDealInfo, error) {
func (ps *dealAssigner) pickAndAlignDirectDeal(ctx context.Context, deals []*mtypes.DirectDealInfo, ssize abi.SectorSize, currentHeight abi.ChainEpoch, spec *mtypes.GetDealSpec) ([]*mtypes.DirectDealInfo, error) {
space := abi.PaddedPieceSize(ssize)

if err := space.Validate(); err != nil {
Expand All @@ -257,16 +258,19 @@ func pickAndAlignDirectDeal(deals []*mtypes.DirectDealInfo, ssize abi.SectorSize
continue
}

// See: https://github.com/filecoin-project/builtin-actors/blob/c0aed11801cb434c989695ad67721c410b9ada33/actors/market/src/lib.rs#L1073-L1094
// https://github.com/filecoin-project/builtin-actors/blob/c0aed11801cb434c989695ad67721c410b9ada33/actors/verifreg/src/lib.rs#L1056-L1071
if spec.SectorExpiration != nil {
allocTermMin := deal.EndEpoch - deal.StartEpoch
allocTermMax := allocTermMin + market.MarketDefaultAllocationTermBuffer
if allocTermMax > types.MaximumVerifiedAllocationTerm {
allocTermMax = types.MaximumVerifiedAllocationTerm
alloc, err := ps.full.StateGetAllocation(ctx, deal.Client, deal.AllocationID, types.EmptyTSK)
if err != nil {
return nil, fmt.Errorf("failed to get deal allocation: %w", err)
}
if alloc == nil {
continue
}

sectorLifetime := *spec.SectorExpiration - currentHeight
if !(sectorLifetime >= allocTermMin && sectorLifetime <= allocTermMax) {
if !(sectorLifetime >= alloc.TermMin && sectorLifetime <= alloc.TermMax &&
currentHeight < alloc.Expiration) {
continue
}
}
Expand Down
15 changes: 11 additions & 4 deletions storageprovider/deal_assigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ipfs/go-cid"

"github.com/filecoin-project/venus/venus-shared/actors/builtin/verifreg"
v1api "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
shared "github.com/filecoin-project/venus/venus-shared/types"
types "github.com/filecoin-project/venus/venus-shared/types/market"
"github.com/ipfs-force-community/droplet/v2/models/repo"
Expand All @@ -32,8 +33,8 @@ type DealAssiger interface {

var _ DealAssiger = (*dealAssigner)(nil)

func NewDealAssigner(r repo.Repo) (DealAssiger, error) {
ps, err := newPieceStoreEx(r)
func NewDealAssigner(r repo.Repo, full v1api.FullNode) (DealAssiger, error) {
ps, err := newPieceStoreEx(r, full)
if err != nil {
return nil, fmt.Errorf("construct extend piece store %w", err)
}
Expand All @@ -42,12 +43,14 @@ func NewDealAssigner(r repo.Repo) (DealAssiger, error) {

type dealAssigner struct {
repo repo.Repo
full v1api.FullNode
}

// NewDsPieceStore returns a new piecestore based on the given datastore
func newPieceStoreEx(r repo.Repo) (DealAssiger, error) {
func newPieceStoreEx(r repo.Repo, full v1api.FullNode) (DealAssiger, error) {
return &dealAssigner{
repo: r,
full: full,
}, nil
}

Expand Down Expand Up @@ -362,6 +365,7 @@ func (ps *dealAssigner) assignDirectDeals(ctx context.Context, sid abi.SectorID,
Length: md.PieceSize,
PayloadSize: md.PayloadSize,
StartEpoch: md.StartEpoch,
EndEpoch: md.EndEpoch,
})
}

Expand All @@ -379,7 +383,7 @@ func (ps *dealAssigner) assignDirectDeals(ctx context.Context, sid abi.SectorID,
return left.StartEpoch < right.StartEpoch
})

pieces, err = pickAndAlignDirectDeal(deals, ssize, currentHeight, spec)
pieces, err = ps.pickAndAlignDirectDeal(ctx, deals, ssize, currentHeight, spec)
if err != nil {
return fmt.Errorf("unable to pick and align pieces from deals: %w", err)
}
Expand Down Expand Up @@ -436,6 +440,9 @@ func (ps *dealAssigner) AssignDeals(ctx context.Context, sid abi.SectorID, ssize
EndEpoch: d.EndEpoch,
})
}
if len(out) > 0 {
return out, nil
}

oldDeals, err := ps.AssignUnPackedDeals(ctx, sid, ssize, currentHeight, spec)
if err == nil {
Expand Down
Loading