2019-08-02 14:09:54 +00:00
|
|
|
package deals
|
|
|
|
|
|
|
|
import (
|
2019-08-06 22:04:21 +00:00
|
|
|
"context"
|
2019-09-13 21:00:36 +00:00
|
|
|
"sync"
|
2019-08-06 22:04:21 +00:00
|
|
|
|
2019-09-13 21:00:36 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
datastore "github.com/ipfs/go-datastore"
|
2019-08-07 18:57:48 +00:00
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
2019-08-02 14:09:54 +00:00
|
|
|
inet "github.com/libp2p/go-libp2p-core/network"
|
2019-08-06 22:04:21 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-09-13 21:00:36 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-09-10 12:35:43 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2019-10-21 18:12:11 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-10-22 19:29:05 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/cborrpc"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
|
|
|
"github.com/filecoin-project/lotus/storage/commitment"
|
|
|
|
"github.com/filecoin-project/lotus/storage/sectorblocks"
|
2019-08-02 14:09:54 +00:00
|
|
|
)
|
|
|
|
|
2019-08-06 22:04:21 +00:00
|
|
|
type MinerDeal struct {
|
|
|
|
Client peer.ID
|
2019-10-21 18:12:11 +00:00
|
|
|
Proposal actors.StorageDealProposal
|
2019-08-06 22:04:21 +00:00
|
|
|
ProposalCid cid.Cid
|
2019-09-10 14:13:24 +00:00
|
|
|
State api.DealState
|
2019-08-06 22:04:21 +00:00
|
|
|
|
|
|
|
Ref cid.Cid
|
2019-08-07 18:57:48 +00:00
|
|
|
|
2019-10-24 14:24:31 +00:00
|
|
|
DealID uint64
|
2019-09-10 14:13:24 +00:00
|
|
|
SectorID uint64 // Set when State >= DealStaged
|
2019-08-12 21:48:18 +00:00
|
|
|
|
2019-08-07 18:57:48 +00:00
|
|
|
s inet.Stream
|
2019-08-06 22:04:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
type Provider struct {
|
2019-09-06 22:39:47 +00:00
|
|
|
pricePerByteBlock types.BigInt // how much we want for storing one byte for one block
|
2019-09-13 21:00:36 +00:00
|
|
|
minPieceSize uint64
|
|
|
|
|
|
|
|
ask *types.SignedStorageAsk
|
|
|
|
askLk sync.Mutex
|
2019-09-06 22:39:47 +00:00
|
|
|
|
2019-08-26 10:04:57 +00:00
|
|
|
secst *sectorblocks.SectorBlocks
|
2019-09-16 16:40:26 +00:00
|
|
|
commt *commitment.Tracker
|
2019-08-14 20:27:10 +00:00
|
|
|
full api.FullNode
|
2019-08-06 22:04:21 +00:00
|
|
|
|
|
|
|
// TODO: Use a custom protocol or graphsync in the future
|
|
|
|
// TODO: GC
|
|
|
|
dag dtypes.StagingDAG
|
|
|
|
|
2019-09-13 19:19:13 +00:00
|
|
|
deals MinerStateStore
|
2019-09-13 21:00:36 +00:00
|
|
|
ds dtypes.MetadataDS
|
|
|
|
|
2019-08-07 18:57:48 +00:00
|
|
|
conns map[cid.Cid]inet.Stream
|
2019-08-06 22:04:21 +00:00
|
|
|
|
2019-08-06 23:08:34 +00:00
|
|
|
actor address.Address
|
|
|
|
|
2019-08-07 18:57:48 +00:00
|
|
|
incoming chan MinerDeal
|
2019-09-10 12:35:43 +00:00
|
|
|
updated chan minerDealUpdate
|
2019-08-07 18:57:48 +00:00
|
|
|
stop chan struct{}
|
|
|
|
stopped chan struct{}
|
2019-08-02 14:09:54 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 12:35:43 +00:00
|
|
|
type minerDealUpdate struct {
|
2019-09-10 14:13:24 +00:00
|
|
|
newState api.DealState
|
2019-08-07 18:57:48 +00:00
|
|
|
id cid.Cid
|
|
|
|
err error
|
2019-08-12 21:48:18 +00:00
|
|
|
mut func(*MinerDeal)
|
2019-08-07 11:46:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
func NewProvider(ds dtypes.MetadataDS, secst *sectorblocks.SectorBlocks, commt *commitment.Tracker, dag dtypes.StagingDAG, fullNode api.FullNode) (*Provider, error) {
|
2019-08-06 23:08:34 +00:00
|
|
|
addr, err := ds.Get(datastore.NewKey("miner-address"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
minerAddress, err := address.NewFromBytes(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
h := &Provider{
|
2019-08-14 20:27:10 +00:00
|
|
|
secst: secst,
|
2019-09-16 16:40:26 +00:00
|
|
|
commt: commt,
|
2019-08-14 20:27:10 +00:00
|
|
|
dag: dag,
|
|
|
|
full: fullNode,
|
2019-08-06 22:04:21 +00:00
|
|
|
|
2019-09-06 22:39:47 +00:00
|
|
|
pricePerByteBlock: types.NewInt(3), // TODO: allow setting
|
2019-09-13 21:00:36 +00:00
|
|
|
minPieceSize: 1,
|
2019-09-06 22:39:47 +00:00
|
|
|
|
2019-08-07 18:57:48 +00:00
|
|
|
conns: map[cid.Cid]inet.Stream{},
|
|
|
|
|
2019-08-06 23:08:34 +00:00
|
|
|
incoming: make(chan MinerDeal),
|
2019-09-10 12:35:43 +00:00
|
|
|
updated: make(chan minerDealUpdate),
|
2019-08-07 18:57:48 +00:00
|
|
|
stop: make(chan struct{}),
|
|
|
|
stopped: make(chan struct{}),
|
2019-08-06 23:08:34 +00:00
|
|
|
|
|
|
|
actor: minerAddress,
|
|
|
|
|
2019-09-13 19:19:13 +00:00
|
|
|
deals: MinerStateStore{StateStore{ds: namespace.Wrap(ds, datastore.NewKey("/deals/client"))}},
|
2019-09-13 21:00:36 +00:00
|
|
|
ds: ds,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.tryLoadAsk(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.ask == nil {
|
|
|
|
// TODO: we should be fine with this state, and just say it means 'not actively accepting deals'
|
|
|
|
// for now... lets just set a price
|
|
|
|
if err := h.SetPrice(types.NewInt(3), 1000000); err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed setting a default price: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h, nil
|
2019-08-02 14:09:54 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
func (p *Provider) Run(ctx context.Context) {
|
2019-08-07 20:16:26 +00:00
|
|
|
// TODO: restore state
|
|
|
|
|
2019-08-06 22:04:21 +00:00
|
|
|
go func() {
|
2019-10-21 18:12:11 +00:00
|
|
|
defer log.Warn("quitting deal provider loop")
|
|
|
|
defer close(p.stopped)
|
2019-08-06 22:04:21 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2019-10-21 18:12:11 +00:00
|
|
|
case deal := <-p.incoming: // DealAccepted
|
|
|
|
p.onIncoming(deal)
|
|
|
|
case update := <-p.updated: // DealStaged
|
|
|
|
p.onUpdated(ctx, update)
|
|
|
|
case <-p.stop:
|
2019-08-06 22:04:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
func (p *Provider) onIncoming(deal MinerDeal) {
|
2019-08-07 18:57:48 +00:00
|
|
|
log.Info("incoming deal")
|
2019-08-02 14:09:54 +00:00
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
p.conns[deal.ProposalCid] = deal.s
|
2019-08-02 16:25:10 +00:00
|
|
|
|
2019-10-23 12:59:57 +00:00
|
|
|
if err := p.deals.Begin(deal.ProposalCid, &deal); err != nil {
|
2019-08-07 18:57:48 +00:00
|
|
|
// This can happen when client re-sends proposal
|
2019-10-21 18:12:11 +00:00
|
|
|
p.failDeal(deal.ProposalCid, err)
|
2019-08-07 18:57:48 +00:00
|
|
|
log.Errorf("deal tracking failed: %s", err)
|
2019-08-02 14:09:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-07 18:57:48 +00:00
|
|
|
go func() {
|
2019-10-21 18:12:11 +00:00
|
|
|
p.updated <- minerDealUpdate{
|
2019-09-10 14:13:24 +00:00
|
|
|
newState: api.DealAccepted,
|
2019-08-07 18:57:48 +00:00
|
|
|
id: deal.ProposalCid,
|
|
|
|
err: nil,
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2019-08-06 23:08:34 +00:00
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
func (p *Provider) onUpdated(ctx context.Context, update minerDealUpdate) {
|
2019-08-07 18:57:48 +00:00
|
|
|
log.Infof("Deal %s updated state to %d", update.id, update.newState)
|
|
|
|
if update.err != nil {
|
2019-10-23 17:39:14 +00:00
|
|
|
log.Errorf("deal %s (newSt: %d) failed: %s", update.id, update.newState, update.err)
|
2019-10-21 18:12:11 +00:00
|
|
|
p.failDeal(update.id, update.err)
|
2019-08-06 22:04:21 +00:00
|
|
|
return
|
|
|
|
}
|
2019-08-07 18:57:48 +00:00
|
|
|
var deal MinerDeal
|
2019-10-21 18:12:11 +00:00
|
|
|
err := p.deals.MutateMiner(update.id, func(d *MinerDeal) error {
|
2019-08-07 18:57:48 +00:00
|
|
|
d.State = update.newState
|
2019-08-12 21:48:18 +00:00
|
|
|
if update.mut != nil {
|
|
|
|
update.mut(d)
|
|
|
|
}
|
2019-08-07 18:57:48 +00:00
|
|
|
deal = *d
|
|
|
|
return nil
|
|
|
|
})
|
2019-08-06 12:31:13 +00:00
|
|
|
if err != nil {
|
2019-10-21 18:12:11 +00:00
|
|
|
p.failDeal(update.id, err)
|
2019-08-06 12:31:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-07 18:57:48 +00:00
|
|
|
switch update.newState {
|
2019-09-10 14:13:24 +00:00
|
|
|
case api.DealAccepted:
|
2019-10-21 18:12:11 +00:00
|
|
|
p.handle(ctx, deal, p.accept, api.DealStaged)
|
2019-09-10 14:13:24 +00:00
|
|
|
case api.DealStaged:
|
2019-10-21 18:12:11 +00:00
|
|
|
p.handle(ctx, deal, p.staged, api.DealSealing)
|
2019-09-10 14:13:24 +00:00
|
|
|
case api.DealSealing:
|
2019-10-21 18:12:11 +00:00
|
|
|
p.handle(ctx, deal, p.sealing, api.DealComplete)
|
2019-09-16 18:04:15 +00:00
|
|
|
case api.DealComplete:
|
2019-10-21 18:12:11 +00:00
|
|
|
p.handle(ctx, deal, p.complete, api.DealNoUpdate)
|
2019-08-02 14:09:54 +00:00
|
|
|
}
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
2019-08-06 12:31:13 +00:00
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
func (p *Provider) newDeal(s inet.Stream, proposal actors.StorageDealProposal) (MinerDeal, error) {
|
2019-10-23 12:59:57 +00:00
|
|
|
proposalNd, err := cborrpc.AsIpld(&proposal)
|
2019-08-06 12:31:13 +00:00
|
|
|
if err != nil {
|
2019-08-07 18:57:48 +00:00
|
|
|
return MinerDeal{}, err
|
2019-08-06 12:31:13 +00:00
|
|
|
}
|
2019-08-06 23:08:34 +00:00
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
ref, err := cid.Cast(proposal.PieceRef)
|
2019-08-06 23:08:34 +00:00
|
|
|
if err != nil {
|
2019-08-07 18:57:48 +00:00
|
|
|
return MinerDeal{}, err
|
2019-08-06 23:08:34 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 18:57:48 +00:00
|
|
|
return MinerDeal{
|
|
|
|
Client: s.Conn().RemotePeer(),
|
|
|
|
Proposal: proposal,
|
|
|
|
ProposalCid: proposalNd.Cid(),
|
2019-09-10 14:13:24 +00:00
|
|
|
State: api.DealUnknown,
|
2019-08-06 12:31:13 +00:00
|
|
|
|
2019-08-07 18:57:48 +00:00
|
|
|
Ref: ref,
|
2019-08-06 23:08:34 +00:00
|
|
|
|
2019-08-07 18:57:48 +00:00
|
|
|
s: s,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
func (p *Provider) HandleStream(s inet.Stream) {
|
2019-08-07 18:57:48 +00:00
|
|
|
log.Info("Handling storage deal proposal!")
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
proposal, err := p.readProposal(s)
|
2019-08-07 18:57:48 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
s.Close()
|
2019-08-02 14:09:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
deal, err := p.newDeal(s, proposal)
|
2019-08-06 22:04:21 +00:00
|
|
|
if err != nil {
|
2019-08-06 23:08:34 +00:00
|
|
|
log.Error(err)
|
2019-08-07 18:57:48 +00:00
|
|
|
s.Close()
|
2019-08-06 22:04:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
p.incoming <- deal
|
2019-08-02 14:09:54 +00:00
|
|
|
}
|
2019-08-06 23:08:34 +00:00
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
func (p *Provider) Stop() {
|
|
|
|
close(p.stop)
|
|
|
|
<-p.stopped
|
2019-08-06 23:08:34 +00:00
|
|
|
}
|