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

WIP: contract deal making support #778

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
118 changes: 112 additions & 6 deletions storagemarket/impl/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,31 @@ import (
"context"
"errors"
"fmt"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-padreader"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/builtin/v9/market"
"github.com/hannahhoward/go-pubsub"
"github.com/hashicorp/go-multierror"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"io"
"os"
"sort"
"time"

"github.com/hannahhoward/go-pubsub"
"github.com/hashicorp/go-multierror"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
provider "github.com/ipni/index-provider"
"github.com/ipni/index-provider/metadata"
"github.com/libp2p/go-libp2p-core/peer"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-address"
cborutil "github.com/filecoin-project/go-cbor-util"
"github.com/filecoin-project/go-commp-utils/ffiwrapper"
datatransfer "github.com/filecoin-project/go-data-transfer"
versioning "github.com/filecoin-project/go-ds-versioning/pkg"
versionedfsm "github.com/filecoin-project/go-ds-versioning/pkg/fsm"
commcid "github.com/filecoin-project/go-fil-commcid"
commp "github.com/filecoin-project/go-fil-commp-hashhash"
"github.com/filecoin-project/go-padreader"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/exitcode"
Expand Down Expand Up @@ -339,6 +341,110 @@ func (p *Provider) receiveDeal(s network.StorageDealStream) error {
return p.deals.Send(proposalNd.Cid(), storagemarket.ProviderEventOpen)
}

func (p *Provider) InitiateDealWithClient(ctx context.Context, commP cid.Cid, commPSize uint64, clientAddr address.Address, startEpoch abi.ChainEpoch, duration uint64, carPath string) error {

// create data ref
dataRef := storagemarket.DataRef{
TransferType: storagemarket.TTManual,
PieceCid: &commP,
PieceSize: abi.UnpaddedPieceSize(commPSize),
RawBlockSize: 0,
}

// create deal proposal
var proposal network.Proposal
dealProposal := market.ClientDealProposal{
Proposal: market.DealProposal{
PieceCID: commP,
PieceSize: abi.UnpaddedPieceSize(commPSize).Padded(),
VerifiedDeal: false,
Client: clientAddr,
Provider: p.actor,
Label: market.DealLabel{},
StartEpoch: startEpoch,
EndEpoch: startEpoch + abi.ChainEpoch(duration),
StoragePricePerEpoch: big.Zero(),
ProviderCollateral: big.Zero(),
ClientCollateral: big.Zero(),
},
ClientSignature: crypto.Signature{
Type: crypto.SigTypeBLS,
Data: []byte{0xde, 0xad},
},
}

fmt.Println(dealProposal.Proposal)

proposal.DealProposal = &dealProposal
proposal.Piece = &dataRef
proposal.FastRetrieval = false

proposalNd, err := cborutil.AsIpld(proposal.DealProposal)
if err != nil {
return fmt.Errorf("getting deal proposal as IPLD: %w", err)
}

deal := storagemarket.MinerDeal{
//Client:
Miner: p.net.ID(),
ClientDealProposal: *proposal.DealProposal,
ProposalCid: proposalNd.Cid(),
State: storagemarket.StorageDealUnknown,
Ref: proposal.Piece,
FastRetrieval: proposal.FastRetrieval,
CreationTime: curTime(),
InboundCAR: carPath,
}

tok, _, err := p.spn.GetChainHead(ctx)
if err != nil {
return err
}

err = providerutils.VerifyProposal(ctx, deal.ClientDealProposal, tok, p.spn.VerifySignatureContract)
if err != nil {
return err
}

mcid, err := p.spn.PublishDeals(ctx, deal)
if err != nil {
return err
}
deal.PublishCid = &mcid

res, err := p.spn.WaitForPublishDeals(ctx, mcid, deal.Proposal)
if err != nil {
return err
}
deal.DealID = res.DealID

f, err := os.Open(carPath)
if err != nil {
return err
}

fi, err := f.Stat()
if err != nil {
return err
}
paddedReader, err := shared.NewInflatorReader(f, uint64(fi.Size()), deal.Proposal.PieceSize.Unpadded())
if err != nil {
return err
}

_, err = p.spn.OnDealComplete(
ctx,
deal,
deal.Proposal.PieceSize.Unpadded(),
paddedReader,
)
if err != nil {
return err
}

return nil
}

// Stop terminates processing of deals on a StorageProvider
func (p *Provider) Stop() error {
p.readyMgr.Stop()
Expand Down
3 changes: 3 additions & 0 deletions storagemarket/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type StorageCommon interface {
// GetBalance returns locked/unlocked for a storage participant. Used by both providers and clients.
GetBalance(ctx context.Context, addr address.Address, tok shared.TipSetToken) (Balance, error)

// VerifySignatureContract simulates AuthenticateMessage on the client contract address
VerifySignatureContract(ctx context.Context, signature crypto.Signature, signer address.Address, plaintext []byte, tok shared.TipSetToken) (bool, error)

// VerifySignature verifies a given set of data was signed properly by a given address's private key
VerifySignature(ctx context.Context, signature crypto.Signature, signer address.Address, plaintext []byte, tok shared.TipSetToken) (bool, error)

Expand Down
3 changes: 3 additions & 0 deletions storagemarket/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storagemarket

import (
"context"
"github.com/filecoin-project/go-address"
"io"

"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -67,4 +68,6 @@ type StorageProvider interface {
AnnounceDealToIndexer(ctx context.Context, proposalCid cid.Cid) error

AnnounceAllDealsToIndexer(ctx context.Context) error

InitiateDealWithClient(ctx context.Context, commP cid.Cid, commPSize uint64, clientAddr address.Address, startEpoch abi.ChainEpoch, duration uint64, carPath string) error
}