Skip to content

Commit

Permalink
always try to return some message to the client (#498)
Browse files Browse the repository at this point in the history
* always try to return some message to the client

* fix: better error handling

Co-authored-by: Dirk McCormick <[email protected]>
  • Loading branch information
whyrusleeping and dirkmc authored Mar 22, 2021
1 parent 0f8f468 commit 46f73ec
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions storagemarket/impl/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,33 +465,60 @@ func (p *Provider) HandleDealStatusStream(s network.DealStatusStream) {
return
}

dealState, err := p.processDealStatusRequest(ctx, &request)
if err != nil {
log.Errorf("failed to process deal status request: %s", err)
dealState = &storagemarket.ProviderDealState{
State: storagemarket.StorageDealError,
Message: err.Error(),
}
}

signature, err := p.sign(ctx, dealState)
if err != nil {
log.Errorf("failed to sign deal status response: %s", err)
return
}

response := network.DealStatusResponse{
DealState: *dealState,
Signature: *signature,
}

if err := s.WriteDealStatusResponse(response, p.sign); err != nil {
log.Warnf("failed to write deal status response: %s", err)
return
}
}

func (p *Provider) processDealStatusRequest(ctx context.Context, request *network.DealStatusRequest) (*storagemarket.ProviderDealState, error) {
// fetch deal state
var md = storagemarket.MinerDeal{}
if err := p.deals.Get(request.Proposal).Get(&md); err != nil {
log.Errorf("proposal doesn't exist in state store: %s", err)
return
return nil, xerrors.Errorf("no such proposal")
}

// verify query signature
buf, err := cborutil.Dump(&request.Proposal)
if err != nil {
log.Errorf("failed to serialize status request: %s", err)
return
return nil, xerrors.Errorf("internal error")
}

tok, _, err := p.spn.GetChainHead(ctx)
if err != nil {
log.Errorf("failed to get chain head: %s", err)
return
return nil, xerrors.Errorf("internal error")
}

err = providerutils.VerifySignature(ctx, request.Signature, md.ClientDealProposal.Proposal.Client, buf, tok, p.spn.VerifySignature)
if err != nil {
log.Errorf("invalid deal status request signature: %s", err)
return
return nil, xerrors.Errorf("internal error")
}

dealState := storagemarket.ProviderDealState{
return &storagemarket.ProviderDealState{
State: md.State,
Message: md.Message,
Proposal: &md.Proposal,
Expand All @@ -500,23 +527,7 @@ func (p *Provider) HandleDealStatusStream(s network.DealStatusStream) {
PublishCid: md.PublishCid,
DealID: md.DealID,
FastRetrieval: md.FastRetrieval,
}

signature, err := p.sign(ctx, &dealState)
if err != nil {
log.Errorf("failed to sign deal status response: %s", err)
return
}

response := network.DealStatusResponse{
DealState: dealState,
Signature: *signature,
}

if err := s.WriteDealStatusResponse(response, p.sign); err != nil {
log.Warnf("failed to write deal status response: %s", err)
return
}
}, nil
}

// Configure applies the given list of StorageProviderOptions after a StorageProvider
Expand Down

0 comments on commit 46f73ec

Please sign in to comment.