Skip to content

Commit

Permalink
Unified request validator (#247)
Browse files Browse the repository at this point in the history
* Extract ValidatePush, ValidatePull

* Add unified request validator

* Remove ClientRequestValidator, ProviderRequestValidator in favor of the unified one
  • Loading branch information
ingar authored May 12, 2020
1 parent 82b06c7 commit c0e6205
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,42 @@ import (
"github.com/filecoin-project/go-fil-markets/storagemarket"
)

var _ datatransfer.RequestValidator = &ClientRequestValidator{}

// ClientRequestValidator validates data transfer requests for the client
// in a storage market
type ClientRequestValidator struct {
deals *statestore.StateStore
}

// NewClientRequestValidator returns a new client request validator for the
// given datastore
func NewClientRequestValidator(deals *statestore.StateStore) *ClientRequestValidator {
crv := &ClientRequestValidator{
deals: deals,
}
return crv
}

// ValidatePush validates a push request received from the peer that will send data
// Will always error because clients should not accept push requests from a provider
// in a storage deal (i.e. send data to client).
func (c *ClientRequestValidator) ValidatePush(
// Will succeed only if:
// - voucher has correct type
// - voucher references an active deal
// - referenced deal matches the client
// - referenced deal matches the given base CID
// - referenced deal is in an acceptable state
func ValidatePush(
deals *statestore.StateStore,
sender peer.ID,
voucher datatransfer.Voucher,
baseCid cid.Cid,
Selector ipld.Node) error {
return ErrNoPushAccepted
dealVoucher, ok := voucher.(*StorageDataTransferVoucher)
if !ok {
return xerrors.Errorf("voucher type %s: %w", voucher.Type(), ErrWrongVoucherType)
}

var deal storagemarket.MinerDeal
err := deals.Get(dealVoucher.Proposal).Get(&deal)
if err != nil {
return xerrors.Errorf("Proposal CID %s: %w", dealVoucher.Proposal.String(), ErrNoDeal)
}
if deal.Client != sender {
return xerrors.Errorf("Deal Peer %s, Data Transfer Peer %s: %w", deal.Client.String(), sender.String(), ErrWrongPeer)
}

if !deal.Ref.Root.Equals(baseCid) {
return xerrors.Errorf("Deal Payload CID %s, Data Transfer CID %s: %w", deal.Proposal.PieceCID.String(), baseCid.String(), ErrWrongPiece)
}
for _, state := range DataTransferStates {
if deal.State == state {
return nil
}
}
return xerrors.Errorf("Deal State %s: %w", deal.State, ErrInacceptableDealState)
}

// ValidatePull validates a pull request received from the peer that will receive data
Expand All @@ -46,7 +56,8 @@ func (c *ClientRequestValidator) ValidatePush(
// - referenced deal matches the receiver (miner)
// - referenced deal matches the given base CID
// - referenced deal is in an acceptable state
func (c *ClientRequestValidator) ValidatePull(
func ValidatePull(
deals *statestore.StateStore,
receiver peer.ID,
voucher datatransfer.Voucher,
baseCid cid.Cid,
Expand All @@ -57,7 +68,7 @@ func (c *ClientRequestValidator) ValidatePull(
}

var deal storagemarket.ClientDeal
err := c.deals.Get(dealVoucher.Proposal).Get(&deal)
err := deals.Get(dealVoucher.Proposal).Get(&deal)
if err != nil {
return xerrors.Errorf("Proposal CID %s: %w", dealVoucher.Proposal.String(), ErrNoDeal)
}
Expand Down
77 changes: 0 additions & 77 deletions storagemarket/impl/requestvalidation/provider_request_validator.go

This file was deleted.

Loading

0 comments on commit c0e6205

Please sign in to comment.