Skip to content

Commit

Permalink
early return in retrieval validation to prevent unnecessary disk access
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlott authored and hannahhoward committed Oct 2, 2020
1 parent f7227b2 commit 953083d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
24 changes: 12 additions & 12 deletions retrievalmarket/impl/requestvalidation/requestvalidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,9 @@ func (rv *ProviderRequestValidator) validatePull(receiver peer.ID, proposal *ret
}

func (rv *ProviderRequestValidator) acceptDeal(deal *retrievalmarket.ProviderDealState) (retrievalmarket.DealStatus, error) {
// verify we have the piece
pieceInfo, err := rv.env.GetPiece(deal.PayloadCID, deal.PieceCID)
if err != nil {
if err == retrievalmarket.ErrNotFound {
return retrievalmarket.DealStatusDealNotFound, err
}
return retrievalmarket.DealStatusErrored, err
}

deal.PieceInfo = &pieceInfo

// check that the deal parameters match our required parameters or
// reject outright
err = rv.env.CheckDealParams(deal.PricePerByte, deal.PaymentInterval, deal.PaymentIntervalIncrease, deal.UnsealPrice)
err := rv.env.CheckDealParams(deal.PricePerByte, deal.PaymentInterval, deal.PaymentIntervalIncrease, deal.UnsealPrice)
if err != nil {
return retrievalmarket.DealStatusRejected, err
}
Expand All @@ -162,6 +151,17 @@ func (rv *ProviderRequestValidator) acceptDeal(deal *retrievalmarket.ProviderDea
return retrievalmarket.DealStatusRejected, errors.New(reason)
}

// verify we have the piece
pieceInfo, err := rv.env.GetPiece(deal.PayloadCID, deal.PieceCID)
if err != nil {
if err == retrievalmarket.ErrNotFound {
return retrievalmarket.DealStatusDealNotFound, err
}
return retrievalmarket.DealStatusErrored, err
}

deal.PieceInfo = &pieceInfo

deal.StoreID, err = rv.env.NextStoreID()
if err != nil {
return retrievalmarket.DealStatusErrored, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func TestValidatePull(t *testing.T) {
},
"get piece other err": {
fve: fakeValidationEnvironment{
GetPieceErr: errors.New("something went wrong"),
RunDealDecisioningLogicAccepted: true,
GetPieceErr: errors.New("something went wrong"),
},
baseCid: proposal.PayloadCID,
selector: shared.AllSelector(),
Expand All @@ -87,7 +88,8 @@ func TestValidatePull(t *testing.T) {
},
"get piece not found err": {
fve: fakeValidationEnvironment{
GetPieceErr: retrievalmarket.ErrNotFound,
RunDealDecisioningLogicAccepted: true,
GetPieceErr: retrievalmarket.ErrNotFound,
},
baseCid: proposal.PayloadCID,
selector: shared.AllSelector(),
Expand Down

0 comments on commit 953083d

Please sign in to comment.