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

Feat/verify data before publishing deal #40

Merged
merged 3 commits into from
Jan 13, 2020
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
17 changes: 10 additions & 7 deletions storagemarket/impl/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (p *Provider) onIncoming(deal MinerDeal) {

go func() {
p.updated <- minerDealUpdate{
newState: storagemarket.DealAccepted,
newState: storagemarket.DealValidating,
id: deal.ProposalCid,
err: nil,
}
Expand Down Expand Up @@ -186,8 +186,14 @@ func (p *Provider) onUpdated(ctx context.Context, update minerDealUpdate) {
}

switch update.newState {
case storagemarket.DealAccepted:
p.handle(ctx, deal, p.accept, storagemarket.DealNoUpdate)
case storagemarket.DealValidating:
p.handle(ctx, deal, p.validating, storagemarket.DealTransferring)
case storagemarket.DealTransferring:
p.handle(ctx, deal, p.transferring, storagemarket.DealNoUpdate)
case storagemarket.DealVerifyData:
p.handle(ctx, deal, p.verifydata, storagemarket.DealPublishing)
case storagemarket.DealPublishing:
p.handle(ctx, deal, p.publishing, storagemarket.DealStaged)
case storagemarket.DealStaged:
p.handle(ctx, deal, p.staged, storagemarket.DealSealing)
case storagemarket.DealSealing:
Expand Down Expand Up @@ -215,10 +221,7 @@ func (p *Provider) onDataTransferEvent(event datatransfer.Event, channelState da
var mut func(*MinerDeal)
switch event.Code {
case datatransfer.Complete:
next = storagemarket.DealStaged
mut = func(deal *MinerDeal) {
deal.DealID = voucher.DealID
}
next = storagemarket.DealVerifyData
case datatransfer.Error:
next = storagemarket.DealFailed
err = ErrDataTransferFailed
Expand Down
102 changes: 60 additions & 42 deletions storagemarket/impl/provider_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ func (p *Provider) handle(ctx context.Context, deal MinerDeal, cb providerHandle
}()
}

// ACCEPTED
func (p *Provider) accept(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error) {

// DealValidating
func (p *Provider) validating(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error) {
head, err := p.spn.MostRecentStateId(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -70,6 +69,61 @@ func (p *Provider) accept(ctx context.Context, deal MinerDeal) (func(*MinerDeal)
return nil, xerrors.New("clientMarketBalance.Available too small")
}

// TODO: Send intent to accept
return nil, nil
}

// State: DealTransferring
func (p *Provider) transferring(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error) {
ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())

// this is the selector for "get the whole DAG"
// TODO: support storage deals with custom payload selectors
allSelector := ssb.ExploreRecursive(selector.RecursionLimitNone(),
ssb.ExploreAll(ssb.ExploreRecursiveEdge())).Node()

log.Infof("fetching data for a deal %d", deal.ProposalCid)

// initiate a pull data transfer. This will complete asynchronously and the
// completion of the data transfer will trigger a change in deal state
// (see onDataTransferEvent)
_, err := p.dataTransfer.OpenPullDataChannel(ctx,
deal.Client,
&StorageDataTransferVoucher{Proposal: deal.ProposalCid},
deal.Ref,
allSelector,
)
if err != nil {
return nil, xerrors.Errorf("failed to open pull data channel: %w", err)
}

return nil, nil
}

// State: DealVerifyData
func (p *Provider) verifydata(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error) {
// entire DAG selector
ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())
allSelector := ssb.ExploreRecursive(selector.RecursionLimitNone(),
ssb.ExploreAll(ssb.ExploreRecursiveEdge())).Node()

commp, file, err := p.pio.GeneratePieceCommitment(deal.Ref, allSelector)
if err != nil {
return nil, err
}

// Verify CommP matches
if !bytes.Equal(commp, deal.Proposal.PieceRef) {
return nil, xerrors.Errorf("proposal CommP doesn't match calculated CommP")
}

return func(deal *MinerDeal) {
deal.PiecePath = file.Path()
}, nil
}

// State: DealPublishing
func (p *Provider) publishing(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error) {
waddr, err := p.spn.GetMinerWorker(ctx, deal.Proposal.Provider)
if err != nil {
return nil, err
Expand All @@ -94,7 +148,6 @@ func (p *Provider) accept(ctx context.Context, deal MinerDeal) (func(*MinerDeal)
return nil, err
}

log.Infof("fetching data for a deal %d", dealId)
err = p.sendSignedResponse(ctx, &Response{
State: storagemarket.DealAccepted,

Expand All @@ -109,47 +162,13 @@ func (p *Provider) accept(ctx context.Context, deal MinerDeal) (func(*MinerDeal)
log.Warnf("closing client connection: %+v", err)
}

ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())

// this is the selector for "get the whole DAG"
// TODO: support storage deals with custom payload selectors
allSelector := ssb.ExploreRecursive(selector.RecursionLimitNone(),
ssb.ExploreAll(ssb.ExploreRecursiveEdge())).Node()

// initiate a pull data transfer. This will complete asynchronously and the
// completion of the data transfer will trigger a change in deal state
// (see onDataTransferEvent)
_, err = p.dataTransfer.OpenPullDataChannel(ctx,
deal.Client,
&StorageDataTransferVoucher{Proposal: deal.ProposalCid, DealID: uint64(dealId)},
deal.Ref,
allSelector,
)
if err != nil {
return nil, xerrors.Errorf("failed to open pull data channel: %w", err)
}

return nil, nil
return func(deal *MinerDeal) {
deal.DealID = uint64(dealId)
}, nil
}

// STAGED

func (p *Provider) staged(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error) {
// entire DAG selector
ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())
allSelector := ssb.ExploreRecursive(selector.RecursionLimitNone(),
ssb.ExploreAll(ssb.ExploreRecursiveEdge())).Node()

commp, file, err := p.pio.GeneratePieceCommitment(deal.Ref, allSelector)
if err != nil {
return nil, err
}

// Verify CommP matches
if !bytes.Equal(commp, deal.Proposal.PieceRef) {
return nil, xerrors.Errorf("proposal CommP doesn't match calculated CommP")
}

sectorID, err := p.spn.OnDealComplete(
ctx,
storagemarket.MinerDeal{
Expand All @@ -169,7 +188,6 @@ func (p *Provider) staged(ctx context.Context, deal MinerDeal) (func(*MinerDeal)

return func(deal *MinerDeal) {
deal.SectorID = sectorID
deal.PiecePath = file.Path()
}, nil
}

Expand Down
20 changes: 10 additions & 10 deletions storagemarket/impl/request_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestClientRequestValidation(t *testing.T) {
if err != nil {
t.Fatal("unable to construct piece cid")
}
if !xerrors.Is(crv.ValidatePull(minerID, &deals.StorageDataTransferVoucher{proposalNd.Cid(), 1}, pieceRef, nil), deals.ErrNoDeal) {
if !xerrors.Is(crv.ValidatePull(minerID, &deals.StorageDataTransferVoucher{proposalNd.Cid()}, pieceRef, nil), deals.ErrNoDeal) {
t.Fatal("Pull should fail if there is no deal stored")
}
})
Expand All @@ -145,7 +145,7 @@ func TestClientRequestValidation(t *testing.T) {
t.Fatal("deal tracking failed")
}
payloadCid := clientDeal.PayloadCid
if !xerrors.Is(crv.ValidatePull(minerID, &deals.StorageDataTransferVoucher{clientDeal.ProposalCid, 1}, payloadCid, nil), deals.ErrWrongPeer) {
if !xerrors.Is(crv.ValidatePull(minerID, &deals.StorageDataTransferVoucher{clientDeal.ProposalCid}, payloadCid, nil), deals.ErrWrongPeer) {
t.Fatal("Pull should fail if miner address is incorrect")
}
})
Expand All @@ -157,7 +157,7 @@ func TestClientRequestValidation(t *testing.T) {
if err := state.Begin(clientDeal.ProposalCid, &clientDeal); err != nil {
t.Fatal("deal tracking failed")
}
if !xerrors.Is(crv.ValidatePull(minerID, &deals.StorageDataTransferVoucher{clientDeal.ProposalCid, 1}, blockGenerator.Next().Cid(), nil), deals.ErrWrongPiece) {
if !xerrors.Is(crv.ValidatePull(minerID, &deals.StorageDataTransferVoucher{clientDeal.ProposalCid}, blockGenerator.Next().Cid(), nil), deals.ErrWrongPiece) {
t.Fatal("Pull should fail if piece ref is incorrect")
}
})
Expand All @@ -170,7 +170,7 @@ func TestClientRequestValidation(t *testing.T) {
t.Fatal("deal tracking failed")
}
payloadCid := clientDeal.PayloadCid
if !xerrors.Is(crv.ValidatePull(minerID, &deals.StorageDataTransferVoucher{clientDeal.ProposalCid, 1}, payloadCid, nil), deals.ErrInacceptableDealState) {
if !xerrors.Is(crv.ValidatePull(minerID, &deals.StorageDataTransferVoucher{clientDeal.ProposalCid}, payloadCid, nil), deals.ErrInacceptableDealState) {
t.Fatal("Pull should fail if deal is in a state that cannot be data transferred")
}
})
Expand All @@ -183,7 +183,7 @@ func TestClientRequestValidation(t *testing.T) {
t.Fatal("deal tracking failed")
}
payloadCid := clientDeal.PayloadCid
if crv.ValidatePull(minerID, &deals.StorageDataTransferVoucher{clientDeal.ProposalCid, 1}, payloadCid, nil) != nil {
if crv.ValidatePull(minerID, &deals.StorageDataTransferVoucher{clientDeal.ProposalCid}, payloadCid, nil) != nil {
t.Fatal("Pull should should succeed when all parameters are correct")
}
})
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestProviderRequestValidation(t *testing.T) {
if err != nil {
t.Fatal("unable to construct piece cid")
}
if !xerrors.Is(mrv.ValidatePush(clientID, &deals.StorageDataTransferVoucher{proposalNd.Cid(), 1}, pieceRef, nil), deals.ErrNoDeal) {
if !xerrors.Is(mrv.ValidatePush(clientID, &deals.StorageDataTransferVoucher{proposalNd.Cid()}, pieceRef, nil), deals.ErrNoDeal) {
t.Fatal("Push should fail if there is no deal stored")
}
})
Expand All @@ -229,7 +229,7 @@ func TestProviderRequestValidation(t *testing.T) {
t.Fatal("deal tracking failed")
}
ref := minerDeal.Ref
if !xerrors.Is(mrv.ValidatePush(clientID, &deals.StorageDataTransferVoucher{minerDeal.ProposalCid, 1}, ref, nil), deals.ErrWrongPeer) {
if !xerrors.Is(mrv.ValidatePush(clientID, &deals.StorageDataTransferVoucher{minerDeal.ProposalCid}, ref, nil), deals.ErrWrongPeer) {
t.Fatal("Push should fail if miner address is incorrect")
}
})
Expand All @@ -241,7 +241,7 @@ func TestProviderRequestValidation(t *testing.T) {
if err := state.Begin(minerDeal.ProposalCid, &minerDeal); err != nil {
t.Fatal("deal tracking failed")
}
if !xerrors.Is(mrv.ValidatePush(clientID, &deals.StorageDataTransferVoucher{minerDeal.ProposalCid, 1}, blockGenerator.Next().Cid(), nil), deals.ErrWrongPiece) {
if !xerrors.Is(mrv.ValidatePush(clientID, &deals.StorageDataTransferVoucher{minerDeal.ProposalCid}, blockGenerator.Next().Cid(), nil), deals.ErrWrongPiece) {
t.Fatal("Push should fail if piece ref is incorrect")
}
})
Expand All @@ -254,7 +254,7 @@ func TestProviderRequestValidation(t *testing.T) {
t.Fatal("deal tracking failed")
}
ref := minerDeal.Ref
if !xerrors.Is(mrv.ValidatePush(clientID, &deals.StorageDataTransferVoucher{minerDeal.ProposalCid, 1}, ref, nil), deals.ErrInacceptableDealState) {
if !xerrors.Is(mrv.ValidatePush(clientID, &deals.StorageDataTransferVoucher{minerDeal.ProposalCid}, ref, nil), deals.ErrInacceptableDealState) {
t.Fatal("Push should fail if deal is in a state that cannot be data transferred")
}
})
Expand All @@ -267,7 +267,7 @@ func TestProviderRequestValidation(t *testing.T) {
t.Fatal("deal tracking failed")
}
ref := minerDeal.Ref
if mrv.ValidatePush(clientID, &deals.StorageDataTransferVoucher{minerDeal.ProposalCid, 1}, ref, nil) != nil {
if mrv.ValidatePush(clientID, &deals.StorageDataTransferVoucher{minerDeal.ProposalCid}, ref, nil) != nil {
t.Fatal("Push should should succeed when all parameters are correct")
}
})
Expand Down
1 change: 0 additions & 1 deletion storagemarket/impl/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ type AskResponse struct {
// used by the storage market
type StorageDataTransferVoucher struct {
Proposal cid.Cid
DealID uint64
}

// ToBytes converts the StorageDataTransferVoucher to raw bytes
Expand Down
18 changes: 2 additions & 16 deletions storagemarket/impl/types_cbor_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions storagemarket/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type DealState = uint64
const (
DealUnknown = DealState(iota)
DealRejected // Provider didn't like the proposal
DealAccepted // Proposal accepted, data moved
DealAccepted // Proposal accepted
DealStaged // Data put into the sector
DealSealing // Data in process of being sealed

Expand All @@ -40,7 +40,11 @@ const (

// Internal

DealError // deal failed with an unexpected error
DealValidating // Verifying that deal parameters are good
DealTransferring // Moving data
DealVerifyData // Verify transferred data - generate CAR / piece data
DealPublishing // Publishing deal to chain
DealError // deal failed with an unexpected error

DealNoUpdate = DealUnknown
)
Expand All @@ -53,6 +57,11 @@ var DealStates = []string{
"DealSealing",
"DealFailed",
"DealComplete",

"DealValidating",
"DealTransferring",
"DealVerifyData",
"DealPublishing",
"DealError",
}

Expand Down
Loading